OSDN Git Service

DTXMania089リリースに際してのtag付け。
[dtxmania/dtxmania.git] / 110401(DTXMania089) / SlimDXc_Jun2010(VC++2008) / source / direct3d10 / Texture1D.cpp
1 /*\r
2 * Copyright (c) 2007-2010 SlimDX Group\r
3\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
10\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
13\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
20 * THE SOFTWARE.\r
21 */\r
22 #include "stdafx.h"\r
23 #include <d3d10.h>\r
24 \r
25 #include "../stack_array.h"\r
26 #include "../DataStream.h"\r
27 #include "../Utilities.h"\r
28 \r
29 #include "Direct3D10Exception.h"\r
30 \r
31 #include "Device10.h"\r
32 #include "Texture1D.h"\r
33 #include "Texture1DDescription.h"\r
34 \r
35 using namespace System;\r
36 using namespace System::IO;\r
37 \r
38 namespace SlimDX\r
39 {\r
40 namespace Direct3D10\r
41\r
42         Texture1D::Texture1D( SlimDX::Direct3D10::Device^ device, Texture1DDescription description )\r
43         {\r
44                 Construct( Build( device, description, 0 ) );   \r
45         }\r
46         \r
47         Texture1D::Texture1D( SlimDX::Direct3D10::Device^ device, Texture1DDescription description, DataStream^ data )\r
48         {\r
49                 if( data != nullptr )\r
50                 {\r
51                         D3D10_SUBRESOURCE_DATA initialData;\r
52                         initialData.pSysMem = data->PositionPointer;\r
53                         Construct( Build( device, description, &initialData ) );        \r
54                 }\r
55                 else \r
56                 {\r
57                         Construct( Build( device, description, 0 ) );   \r
58                 }\r
59         }\r
60         \r
61         Texture1D::Texture1D( SlimDX::Direct3D10::Device^ device, Texture1DDescription description, array<DataStream^>^ data )\r
62         {\r
63                 if( data != nullptr )\r
64                 {\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
68                         \r
69                         Construct( Build( device, description, &initialData[0] ) );     \r
70                 } \r
71                 else\r
72                 {\r
73                         Construct( Build( device, description, 0 ) );   \r
74                 }\r
75         }\r
76         \r
77         ID3D10Texture1D* Texture1D::Build( SlimDX::Direct3D10::Device^ device, Texture1DDescription description, D3D10_SUBRESOURCE_DATA* data )\r
78         {\r
79                 ID3D10Texture1D* texture = 0;\r
80                 D3D10_TEXTURE1D_DESC nativeDescription = description.CreateNativeVersion();\r
81                 \r
82                 if( RECORD_D3D10( device->InternalPointer->CreateTexture1D( &nativeDescription, data, &texture ) ).IsFailure )\r
83                         throw gcnew Direct3D10Exception( Result::Last );\r
84                 \r
85                 return texture;\r
86         }\r
87         \r
88         Texture1DDescription Texture1D::Description::get()\r
89         {\r
90                 D3D10_TEXTURE1D_DESC nativeDescription;\r
91                 InternalPointer->GetDesc( &nativeDescription );\r
92                 return Texture1DDescription( nativeDescription );\r
93         }\r
94         \r
95         SlimDX::DataStream^ Texture1D::Map( int mipSlice, MapMode mode, MapFlags flags )\r
96         {\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
100                 \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
103                         return nullptr;\r
104                         \r
105                 return gcnew SlimDX::DataStream( mappedArray, bufferSize, true, true, false );\r
106         }\r
107 \r
108         void Texture1D::Unmap( int subresource )\r
109         {\r
110                 InternalPointer->Unmap( subresource );\r
111         }\r
112         \r
113         Texture1D^ Texture1D::FromFile( SlimDX::Direct3D10::Device^ device, String^ fileName )\r
114         {\r
115                 ID3D10Resource* resource = Resource::ConstructFromFile( device, fileName, 0 );\r
116                 if( resource == 0 )\r
117                         return nullptr;\r
118                         \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
123 \r
124                 return Texture1D::FromPointer( static_cast<ID3D10Texture1D*>( resource ) );\r
125         }\r
126         \r
127         Texture1D^ Texture1D::FromMemory( SlimDX::Direct3D10::Device^ device, array<Byte>^ memory )\r
128         {\r
129                 ID3D10Resource* resource = Resource::ConstructFromMemory( device, memory, 0 );\r
130                 if( resource == 0 )\r
131                         return nullptr;\r
132                         \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
137 \r
138                 return Texture1D::FromPointer( static_cast<ID3D10Texture1D*>( resource ) );\r
139         }\r
140         \r
141         Texture1D^ Texture1D::FromStream( SlimDX::Direct3D10::Device^ device, Stream^ stream, int sizeInBytes )\r
142         {\r
143                 ID3D10Resource* resource = Resource::ConstructFromStream( device, stream, sizeInBytes, 0 );\r
144                 if( resource == 0 )\r
145                         return nullptr;\r
146                         \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
151 \r
152                 return Texture1D::FromPointer( static_cast<ID3D10Texture1D*>( resource ) );\r
153         }\r
154         \r
155         Texture1D^ Texture1D::FromFile( SlimDX::Direct3D10::Device^ device, String^ fileName, ImageLoadInformation loadInfo )\r
156         {\r
157                 D3DX10_IMAGE_LOAD_INFO info = loadInfo.CreateNativeVersion();\r
158                 ID3D10Resource* resource = Resource::ConstructFromFile( device, fileName, &info );\r
159                 if( resource == 0 )\r
160                         return nullptr;\r
161 \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
166 \r
167                 return Texture1D::FromPointer( static_cast<ID3D10Texture1D*>( resource ) );\r
168         }\r
169 \r
170         Texture1D^ Texture1D::FromMemory( SlimDX::Direct3D10::Device^ device, array<Byte>^ memory, ImageLoadInformation loadInfo )\r
171         {\r
172                 D3DX10_IMAGE_LOAD_INFO info = loadInfo.CreateNativeVersion();\r
173                 ID3D10Resource* resource = Resource::ConstructFromMemory( device, memory, &info );\r
174                 if( resource == 0 )\r
175                         return nullptr;\r
176 \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
181 \r
182                 return Texture1D::FromPointer( static_cast<ID3D10Texture1D*>( resource ) );\r
183         }\r
184 \r
185         Texture1D^ Texture1D::FromStream( SlimDX::Direct3D10::Device^ device, Stream^ stream, int sizeInBytes, ImageLoadInformation loadInfo )\r
186         {\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
190                         return nullptr;\r
191 \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
196 \r
197                 return Texture1D::FromPointer( static_cast<ID3D10Texture1D*>( resource ) );\r
198         }\r
199 \r
200         Result Texture1D::ToFile( Texture1D^ texture, ImageFileFormat format, String^ fileName )\r
201         {\r
202                 pin_ptr<const wchar_t> pinnedName = PtrToStringChars( fileName );\r
203 \r
204                 HRESULT hr = D3DX10SaveTextureToFile( texture->InternalPointer, static_cast<D3DX10_IMAGE_FILE_FORMAT>( format ), pinnedName );\r
205                 return RECORD_D3D10( hr );\r
206         }\r
207 \r
208         Result Texture1D::ToStream( Texture1D^ texture, ImageFileFormat format, Stream^ stream )\r
209         {\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
214                 \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
219                 \r
220                 blob->Release();\r
221                 return Result::Last;\r
222         }\r
223 }\r
224 }\r