2 * Copyright (c) 2007-2010 SlimDX Group
\r
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
5 * of this software and associated documentation files (the "Software"), to deal
\r
6 * in the Software without restriction, including without limitation the rights
\r
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
8 * copies of the Software, and to permit persons to whom the Software is
\r
9 * furnished to do so, subject to the following conditions:
\r
11 * The above copyright notice and this permission notice shall be included in
\r
12 * all copies or substantial portions of the Software.
\r
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
25 #include "../stack_array.h"
\r
26 #include "../DataStream.h"
\r
27 #include "../Utilities.h"
\r
29 #include "Direct3D10Exception.h"
\r
31 #include "Device10.h"
\r
32 #include "Texture1D.h"
\r
33 #include "Texture1DDescription.h"
\r
35 using namespace System;
\r
36 using namespace System::IO;
\r
40 namespace Direct3D10
\r
42 Texture1D::Texture1D( SlimDX::Direct3D10::Device^ device, Texture1DDescription description )
\r
44 Construct( Build( device, description, 0 ) );
\r
47 Texture1D::Texture1D( SlimDX::Direct3D10::Device^ device, Texture1DDescription description, DataStream^ data )
\r
49 if( data != nullptr )
\r
51 D3D10_SUBRESOURCE_DATA initialData;
\r
52 initialData.pSysMem = data->PositionPointer;
\r
53 Construct( Build( device, description, &initialData ) );
\r
57 Construct( Build( device, description, 0 ) );
\r
61 Texture1D::Texture1D( SlimDX::Direct3D10::Device^ device, Texture1DDescription description, array<DataStream^>^ data )
\r
63 if( data != nullptr )
\r
65 stack_array<D3D10_SUBRESOURCE_DATA> initialData = stackalloc( D3D10_SUBRESOURCE_DATA, data->Length );
\r
66 for( size_t dataIndex = 0; dataIndex < initialData.size(); ++dataIndex )
\r
67 initialData[dataIndex].pSysMem = data[static_cast<int>( dataIndex )]->PositionPointer;
\r
69 Construct( Build( device, description, &initialData[0] ) );
\r
73 Construct( Build( device, description, 0 ) );
\r
77 ID3D10Texture1D* Texture1D::Build( SlimDX::Direct3D10::Device^ device, Texture1DDescription description, D3D10_SUBRESOURCE_DATA* data )
\r
79 ID3D10Texture1D* texture = 0;
\r
80 D3D10_TEXTURE1D_DESC nativeDescription = description.CreateNativeVersion();
\r
82 if( RECORD_D3D10( device->InternalPointer->CreateTexture1D( &nativeDescription, data, &texture ) ).IsFailure )
\r
83 throw gcnew Direct3D10Exception( Result::Last );
\r
88 Texture1DDescription Texture1D::Description::get()
\r
90 D3D10_TEXTURE1D_DESC nativeDescription;
\r
91 InternalPointer->GetDesc( &nativeDescription );
\r
92 return Texture1DDescription( nativeDescription );
\r
95 SlimDX::DataStream^ Texture1D::Map( int mipSlice, MapMode mode, MapFlags flags )
\r
97 int subresource = D3D10CalcSubresource( mipSlice, 0, Description.MipLevels );
\r
98 int mipWidth = GetMipSize( mipSlice, Description.Width );
\r
99 int bufferSize = mipWidth * Utilities::SizeOfFormatElement( static_cast<DXGI_FORMAT>( Description.Format ) );
\r
101 void* mappedArray = 0;
\r
102 if( RECORD_D3D10( InternalPointer->Map( subresource, static_cast<D3D10_MAP>( mode ), static_cast<UINT>( flags ), &mappedArray ) ).IsFailure )
\r
105 return gcnew SlimDX::DataStream( mappedArray, bufferSize, true, true, false );
\r
108 void Texture1D::Unmap( int subresource )
\r
110 InternalPointer->Unmap( subresource );
\r
113 Texture1D^ Texture1D::FromFile( SlimDX::Direct3D10::Device^ device, String^ fileName )
\r
115 ID3D10Resource* resource = Resource::ConstructFromFile( device, fileName, 0 );
\r
116 if( resource == 0 )
\r
119 D3D10_RESOURCE_DIMENSION type;
\r
120 resource->GetType( &type );
\r
121 if( type != D3D10_RESOURCE_DIMENSION_TEXTURE1D )
\r
122 throw gcnew InvalidOperationException( "Could not load file as 1D texture." );
\r
124 return Texture1D::FromPointer( static_cast<ID3D10Texture1D*>( resource ) );
\r
127 Texture1D^ Texture1D::FromMemory( SlimDX::Direct3D10::Device^ device, array<Byte>^ memory )
\r
129 ID3D10Resource* resource = Resource::ConstructFromMemory( device, memory, 0 );
\r
130 if( resource == 0 )
\r
133 D3D10_RESOURCE_DIMENSION type;
\r
134 resource->GetType( &type );
\r
135 if( type != D3D10_RESOURCE_DIMENSION_TEXTURE1D )
\r
136 throw gcnew InvalidOperationException( "Could not load file as 1D texture." );
\r
138 return Texture1D::FromPointer( static_cast<ID3D10Texture1D*>( resource ) );
\r
141 Texture1D^ Texture1D::FromStream( SlimDX::Direct3D10::Device^ device, Stream^ stream, int sizeInBytes )
\r
143 ID3D10Resource* resource = Resource::ConstructFromStream( device, stream, sizeInBytes, 0 );
\r
144 if( resource == 0 )
\r
147 D3D10_RESOURCE_DIMENSION type;
\r
148 resource->GetType( &type );
\r
149 if( type != D3D10_RESOURCE_DIMENSION_TEXTURE1D )
\r
150 throw gcnew InvalidOperationException( "Could not load file as 1D texture." );
\r
152 return Texture1D::FromPointer( static_cast<ID3D10Texture1D*>( resource ) );
\r
155 Texture1D^ Texture1D::FromFile( SlimDX::Direct3D10::Device^ device, String^ fileName, ImageLoadInformation loadInfo )
\r
157 D3DX10_IMAGE_LOAD_INFO info = loadInfo.CreateNativeVersion();
\r
158 ID3D10Resource* resource = Resource::ConstructFromFile( device, fileName, &info );
\r
159 if( resource == 0 )
\r
162 D3D10_RESOURCE_DIMENSION type;
\r
163 resource->GetType( &type );
\r
164 if( type != D3D10_RESOURCE_DIMENSION_TEXTURE1D )
\r
165 throw gcnew InvalidOperationException( "Could not load file as 1D texture." );
\r
167 return Texture1D::FromPointer( static_cast<ID3D10Texture1D*>( resource ) );
\r
170 Texture1D^ Texture1D::FromMemory( SlimDX::Direct3D10::Device^ device, array<Byte>^ memory, ImageLoadInformation loadInfo )
\r
172 D3DX10_IMAGE_LOAD_INFO info = loadInfo.CreateNativeVersion();
\r
173 ID3D10Resource* resource = Resource::ConstructFromMemory( device, memory, &info );
\r
174 if( resource == 0 )
\r
177 D3D10_RESOURCE_DIMENSION type;
\r
178 resource->GetType( &type );
\r
179 if( type != D3D10_RESOURCE_DIMENSION_TEXTURE1D )
\r
180 throw gcnew InvalidOperationException( "Could not load file as 1D texture." );
\r
182 return Texture1D::FromPointer( static_cast<ID3D10Texture1D*>( resource ) );
\r
185 Texture1D^ Texture1D::FromStream( SlimDX::Direct3D10::Device^ device, Stream^ stream, int sizeInBytes, ImageLoadInformation loadInfo )
\r
187 D3DX10_IMAGE_LOAD_INFO info = loadInfo.CreateNativeVersion();
\r
188 ID3D10Resource* resource = Resource::ConstructFromStream( device, stream, sizeInBytes, &info );
\r
189 if( resource == 0 )
\r
192 D3D10_RESOURCE_DIMENSION type;
\r
193 resource->GetType( &type );
\r
194 if( type != D3D10_RESOURCE_DIMENSION_TEXTURE1D )
\r
195 throw gcnew InvalidOperationException( "Could not load file as 1D texture." );
\r
197 return Texture1D::FromPointer( static_cast<ID3D10Texture1D*>( resource ) );
\r
200 Result Texture1D::ToFile( Texture1D^ texture, ImageFileFormat format, String^ fileName )
\r
202 pin_ptr<const wchar_t> pinnedName = PtrToStringChars( fileName );
\r
204 HRESULT hr = D3DX10SaveTextureToFile( texture->InternalPointer, static_cast<D3DX10_IMAGE_FILE_FORMAT>( format ), pinnedName );
\r
205 return RECORD_D3D10( hr );
\r
208 Result Texture1D::ToStream( Texture1D^ texture, ImageFileFormat format, Stream^ stream )
\r
210 ID3D10Blob* blob = 0;
\r
211 HRESULT hr = D3DX10SaveTextureToMemory( texture->InternalPointer, static_cast<D3DX10_IMAGE_FILE_FORMAT>( format ), &blob, 0 );
\r
212 if( RECORD_D3D10( hr ).IsFailure )
\r
213 return Result::Last;
\r
215 // Write byte-by-byte to avoid allocating a managed byte[] that will wastefully persist.
\r
216 unsigned char* bytes = reinterpret_cast<unsigned char*>( blob->GetBufferPointer() );
\r
217 for(SIZE_T byteIndex = 0; byteIndex < blob->GetBufferSize(); ++byteIndex)
\r
218 stream->WriteByte( bytes[byteIndex] );
\r
221 return Result::Last;
\r