+++ /dev/null
-#include "stdafx.h"\r
-/*\r
-* Copyright (c) 2007-2010 SlimDX Group\r
-* \r
-* Permission is hereby granted, free of charge, to any person obtaining a copy\r
-* of this software and associated documentation files (the "Software"), to deal\r
-* in the Software without restriction, including without limitation the rights\r
-* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
-* copies of the Software, and to permit persons to whom the Software is\r
-* furnished to do so, subject to the following conditions:\r
-* \r
-* The above copyright notice and this permission notice shall be included in\r
-* all copies or substantial portions of the Software.\r
-* \r
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
-* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
-* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
-* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
-* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
-* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
-* THE SOFTWARE.\r
-*/\r
-\r
-#include <d3d10.h>\r
-\r
-#include "../stack_array.h"\r
-#include "../DataRectangle.h"\r
-#include "../DataStream.h"\r
-\r
-#include "Direct3D10Exception.h"\r
-\r
-#include "Device10.h"\r
-#include "Texture2D.h"\r
-#include "Texture2DDescription.h"\r
-\r
-using namespace System;\r
-using namespace System::IO;\r
-\r
-namespace SlimDX\r
-{\r
-namespace Direct3D10\r
-{ \r
- Texture2D::Texture2D( SlimDX::Direct3D10::Device^ device, Texture2DDescription description )\r
- {\r
- Construct( Build( device, description, 0 ) ); \r
- }\r
- \r
- Texture2D::Texture2D( SlimDX::Direct3D10::Device^ device, Texture2DDescription description, DataRectangle^ data )\r
- {\r
- if( data != nullptr )\r
- {\r
- D3D10_SUBRESOURCE_DATA initialData;\r
- initialData.pSysMem = data->Data->PositionPointer;\r
- initialData.SysMemPitch = data->Pitch;\r
- Construct( Build( device, description, &initialData ) ); \r
- }\r
- else \r
- {\r
- Construct( Build( device, description, 0 ) ); \r
- }\r
- }\r
- \r
- Texture2D::Texture2D( SlimDX::Direct3D10::Device^ device, Texture2DDescription description, array<DataRectangle^>^ data )\r
- {\r
- if( data != nullptr )\r
- {\r
- stack_array<D3D10_SUBRESOURCE_DATA> initialData = stackalloc( D3D10_SUBRESOURCE_DATA, data->Length );\r
- for( size_t dataIndex = 0; dataIndex < initialData.size(); ++dataIndex ) \r
- {\r
- initialData[dataIndex].pSysMem = data[static_cast<int>( dataIndex )]->Data->PositionPointer;\r
- initialData[dataIndex].SysMemPitch = data[static_cast<int>( dataIndex )]->Pitch;\r
- }\r
- \r
- Construct( Build( device, description, &initialData[0] ) ); \r
- } \r
- else\r
- {\r
- Construct( Build( device, description, 0 ) ); \r
- }\r
- }\r
- \r
- ID3D10Texture2D* Texture2D::Build( SlimDX::Direct3D10::Device^ device, Texture2DDescription description, D3D10_SUBRESOURCE_DATA* data )\r
- {\r
- ID3D10Texture2D* texture = 0;\r
- D3D10_TEXTURE2D_DESC nativeDescription = description.CreateNativeVersion();\r
- \r
- if( RECORD_D3D10( device->InternalPointer->CreateTexture2D( &nativeDescription, data, &texture ) ).IsFailure )\r
- throw gcnew Direct3D10Exception( Result::Last );\r
- \r
- return texture;\r
- } \r
- \r
- Texture2DDescription Texture2D::Description::get()\r
- {\r
- D3D10_TEXTURE2D_DESC nativeDescription;\r
- InternalPointer->GetDesc( &nativeDescription );\r
- return Texture2DDescription( nativeDescription );\r
- }\r
- \r
- SlimDX::DataRectangle^ Texture2D::Map( int mipSlice, MapMode mode, MapFlags flags )\r
- {\r
- int subresource = D3D10CalcSubresource( mipSlice, 0, Description.MipLevels );\r
- int mipHeight = GetMipSize( mipSlice, Description.Height );\r
- \r
- D3D10_MAPPED_TEXTURE2D mappedRect;\r
- if( RECORD_D3D10( InternalPointer->Map( subresource, static_cast<D3D10_MAP>( mode ), static_cast<UINT>( flags ), &mappedRect ) ).IsFailure )\r
- return nullptr;\r
- \r
- int lockedSize = mipHeight * mappedRect.RowPitch;\r
- return gcnew SlimDX::DataRectangle( mappedRect.RowPitch, gcnew DataStream( mappedRect.pData, lockedSize, true, true, false ) );\r
- }\r
-\r
- void Texture2D::Unmap( int subresource )\r
- {\r
- InternalPointer->Unmap( subresource );\r
- }\r
- \r
- Texture2D^ Texture2D::FromFile( SlimDX::Direct3D10::Device^ device, String^ fileName )\r
- {\r
- ID3D10Resource* resource = Resource::ConstructFromFile( device, fileName, 0 );\r
- if( resource == 0 )\r
- return nullptr;\r
- \r
- D3D10_RESOURCE_DIMENSION type;\r
- resource->GetType( &type );\r
- if( type != D3D10_RESOURCE_DIMENSION_TEXTURE2D )\r
- throw gcnew InvalidOperationException( "Could not load file as 2D texture." );\r
-\r
- return Texture2D::FromPointer( static_cast<ID3D10Texture2D*>( resource ) );\r
- }\r
- \r
- Texture2D^ Texture2D::FromMemory( SlimDX::Direct3D10::Device^ device, array<Byte>^ memory )\r
- {\r
- ID3D10Resource* resource = Resource::ConstructFromMemory( device, memory, 0 );\r
- if( resource == 0 )\r
- return nullptr;\r
- \r
- D3D10_RESOURCE_DIMENSION type;\r
- resource->GetType( &type );\r
- if( type != D3D10_RESOURCE_DIMENSION_TEXTURE2D )\r
- throw gcnew InvalidOperationException( "Could not load file as 2D texture." );\r
-\r
- return Texture2D::FromPointer( static_cast<ID3D10Texture2D*>( resource ) );\r
- }\r
- \r
- Texture2D^ Texture2D::FromStream( SlimDX::Direct3D10::Device^ device, Stream^ stream, int sizeInBytes )\r
- {\r
- ID3D10Resource* resource = Resource::ConstructFromStream( device, stream, sizeInBytes, 0 );\r
- if( resource == 0 )\r
- return nullptr;\r
- \r
- D3D10_RESOURCE_DIMENSION type;\r
- resource->GetType( &type );\r
- if( type != D3D10_RESOURCE_DIMENSION_TEXTURE2D )\r
- throw gcnew InvalidOperationException( "Could not load file as 2D texture." );\r
-\r
- return Texture2D::FromPointer( static_cast<ID3D10Texture2D*>( resource ) );\r
- }\r
- \r
- Texture2D^ Texture2D::FromFile( SlimDX::Direct3D10::Device^ device, String^ fileName, ImageLoadInformation loadInfo )\r
- {\r
- D3DX10_IMAGE_LOAD_INFO info = loadInfo.CreateNativeVersion();\r
- ID3D10Resource* resource = Resource::ConstructFromFile( device, fileName, &info );\r
- if( resource == 0 )\r
- return nullptr;\r
-\r
- D3D10_RESOURCE_DIMENSION type;\r
- resource->GetType( &type );\r
- if( type != D3D10_RESOURCE_DIMENSION_TEXTURE2D )\r
- throw gcnew InvalidOperationException( "Could not load file as 2D texture." );\r
-\r
- return Texture2D::FromPointer( static_cast<ID3D10Texture2D*>( resource ) );\r
- }\r
-\r
- Texture2D^ Texture2D::FromMemory( SlimDX::Direct3D10::Device^ device, array<Byte>^ memory, ImageLoadInformation loadInfo )\r
- {\r
- D3DX10_IMAGE_LOAD_INFO info = loadInfo.CreateNativeVersion();\r
- ID3D10Resource* resource = Resource::ConstructFromMemory( device, memory, &info );\r
- if( resource == 0 )\r
- return nullptr;\r
-\r
- D3D10_RESOURCE_DIMENSION type;\r
- resource->GetType( &type );\r
- if( type != D3D10_RESOURCE_DIMENSION_TEXTURE2D )\r
- throw gcnew InvalidOperationException( "Could not load file as 2D texture." );\r
-\r
- return Texture2D::FromPointer( static_cast<ID3D10Texture2D*>( resource ) );\r
- }\r
-\r
- Texture2D^ Texture2D::FromStream( SlimDX::Direct3D10::Device^ device, Stream^ stream, int sizeInBytes, ImageLoadInformation loadInfo )\r
- {\r
- D3DX10_IMAGE_LOAD_INFO info = loadInfo.CreateNativeVersion();\r
- ID3D10Resource* resource = Resource::ConstructFromStream( device, stream, sizeInBytes, &info );\r
- if( resource == 0 )\r
- return nullptr;\r
-\r
- D3D10_RESOURCE_DIMENSION type;\r
- resource->GetType( &type );\r
- if( type != D3D10_RESOURCE_DIMENSION_TEXTURE2D )\r
- throw gcnew InvalidOperationException( "Could not load file as 2D texture." );\r
-\r
- return Texture2D::FromPointer( static_cast<ID3D10Texture2D*>( resource ) );\r
- }\r
-\r
- Result Texture2D::ToFile( Texture2D^ texture, ImageFileFormat format, String^ fileName )\r
- {\r
- pin_ptr<const wchar_t> pinnedName = PtrToStringChars( fileName );\r
-\r
- HRESULT hr = D3DX10SaveTextureToFile( texture->InternalPointer, static_cast<D3DX10_IMAGE_FILE_FORMAT>( format ), pinnedName );\r
- return RECORD_D3D10( hr );\r
- }\r
-\r
- Result Texture2D::ToStream( Texture2D^ texture, ImageFileFormat format, Stream^ stream )\r
- {\r
- ID3D10Blob* blob = 0;\r
- HRESULT hr = D3DX10SaveTextureToMemory( texture->InternalPointer, static_cast<D3DX10_IMAGE_FILE_FORMAT>( format ), &blob, 0 );\r
- if( RECORD_D3D10( hr ).IsFailure )\r
- return Result::Last;\r
- \r
- // Write byte-by-byte to avoid allocating a managed byte[] that will wastefully persist.\r
- unsigned char* bytes = reinterpret_cast<unsigned char*>( blob->GetBufferPointer() );\r
- for(SIZE_T byteIndex = 0; byteIndex < blob->GetBufferSize(); ++byteIndex)\r
- stream->WriteByte( bytes[byteIndex] );\r
- \r
- blob->Release();\r
- return Result::Last;\r
- }\r
-\r
- Result Texture2D::ComputeNormalMap(Texture2D^ source, Texture2D^ destination, NormalMapFlags flags, Channel channel, float amplitude)\r
- {\r
- HRESULT hr = D3DX10ComputeNormalMap(source->InternalPointer, static_cast<UINT>(flags), static_cast<UINT>(channel), amplitude, destination->InternalPointer);\r
- return RECORD_D3D10(hr);\r
- }\r
-}\r
-}\r