OSDN Git Service

DTXMania089リリースに際してのtag付け。
[dtxmania/dtxmania.git] / 110401(DTXMania089) / SlimDXc_Jun2010(VC++2008) / source / direct3d9 / ConstantTable.cpp
1 #include "stdafx.h"\r
2 /*\r
3 * Copyright (c) 2007-2010 SlimDX Group\r
4\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy\r
6 * of this software and associated documentation files (the "Software"), to deal\r
7 * in the Software without restriction, including without limitation the rights\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
9 * copies of the Software, and to permit persons to whom the Software is\r
10 * furnished to do so, subject to the following conditions:\r
11\r
12 * The above copyright notice and this permission notice shall be included in\r
13 * all copies or substantial portions of the Software.\r
14\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
21 * THE SOFTWARE.\r
22 */\r
23 #include <windows.h>\r
24 #include <d3d9.h>\r
25 #include <d3dx9.h>\r
26 #include <vcclr.h>\r
27 \r
28 #include "../stack_array.h"\r
29 #include "../ComObject.h"\r
30 #include "../Utilities.h"\r
31 #include "../DataStream.h"\r
32 \r
33 #include "Direct3D9Exception.h"\r
34 \r
35 #include "Effect9.h"\r
36 #include "ConstantTable.h"\r
37 #include "Device.h"\r
38 \r
39 using namespace System;\r
40 using namespace System::Globalization;\r
41 \r
42 namespace SlimDX\r
43 {\r
44 namespace Direct3D9\r
45 {\r
46         EffectHandle^ ConstantTable::GetConstant(SlimDX::Direct3D9::EffectHandle ^handle, int index)\r
47         {\r
48                 D3DXHANDLE parentHandle = handle != nullptr ? handle->InternalHandle : NULL;\r
49                 D3DXHANDLE result = InternalPointer->GetConstant( parentHandle, index );\r
50                 GC::KeepAlive( handle );\r
51                 \r
52                 if( result == NULL )\r
53                         return nullptr;\r
54                 return gcnew EffectHandle( result );\r
55         }\r
56         \r
57         EffectHandle^ ConstantTable::GetConstant(SlimDX::Direct3D9::EffectHandle ^handle, String^ name )\r
58         {\r
59                 array<unsigned char>^ rawName = System::Text::ASCIIEncoding::ASCII->GetBytes( name );\r
60                 pin_ptr<unsigned char> pinnedName = &rawName[0];\r
61                 \r
62                 D3DXHANDLE parentHandle = handle != nullptr ? handle->InternalHandle : NULL;\r
63                 D3DXHANDLE result = InternalPointer->GetConstantByName( parentHandle, reinterpret_cast<const char*>( pinnedName ) );\r
64                 GC::KeepAlive( handle );\r
65                 \r
66                 if( result == NULL )\r
67                         return nullptr;\r
68                 return gcnew EffectHandle( result );\r
69         }\r
70         \r
71         EffectHandle^ ConstantTable::GetConstantElement( EffectHandle^ handle, int index )\r
72         {\r
73                 D3DXHANDLE parentHandle = handle != nullptr ? handle->InternalHandle : NULL;\r
74                 D3DXHANDLE result = InternalPointer->GetConstantElement( parentHandle, index );\r
75                 GC::KeepAlive( handle );\r
76                 \r
77                 if( result == NULL )\r
78                         return nullptr;\r
79                 return gcnew EffectHandle( result );\r
80         }\r
81 \r
82         ConstantDescription ConstantTable::GetConstantDescription( EffectHandle^ handle )\r
83         {\r
84                 D3DXCONSTANT_DESC nativeDesc;\r
85                 ConstantDescription description;\r
86 \r
87                 D3DXHANDLE nativeHandle = handle != nullptr ? handle->InternalHandle : NULL;\r
88                 unsigned int count = 1;\r
89 \r
90                 HRESULT hr = InternalPointer->GetConstantDesc( nativeHandle, &nativeDesc, &count );\r
91                 GC::KeepAlive( handle );\r
92                 if( RECORD_D3D9( hr ).IsFailure )\r
93                         return ConstantDescription();\r
94 \r
95                 description.Initialize( nativeDesc );\r
96                 return description;\r
97         }\r
98 \r
99         array<ConstantDescription>^ ConstantTable::GetConstantDescriptionArray( EffectHandle^ handle )\r
100         {\r
101                 D3DXHANDLE nativeHandle = handle != nullptr ? handle->InternalHandle : NULL;\r
102                 unsigned int count = 0;\r
103 \r
104                 HRESULT hr = InternalPointer->GetConstantDesc( nativeHandle, NULL, &count );\r
105                 \r
106                 if( RECORD_D3D9( hr ).IsFailure )\r
107                         return nullptr;\r
108 \r
109                 stack_array<D3DXCONSTANT_DESC> nativeDescArray = stackalloc( D3DXCONSTANT_DESC, count );\r
110                 hr = InternalPointer->GetConstantDesc( nativeHandle, &nativeDescArray[0], &count );\r
111                 \r
112                 if( RECORD_D3D9( hr ).IsFailure )\r
113                         return nullptr;\r
114 \r
115                 array<ConstantDescription>^ descArray = gcnew array<ConstantDescription>( count );\r
116                 for( unsigned int i = 0; i < count; ++i )\r
117                 {\r
118                         descArray[i].Initialize( nativeDescArray[i] );\r
119                 }\r
120 \r
121                 GC::KeepAlive( handle );\r
122                 return descArray;\r
123         }\r
124 \r
125         int ConstantTable::GetSamplerIndex( EffectHandle^ sampler )\r
126         {\r
127                 D3DXHANDLE handle = sampler != nullptr ? sampler->InternalHandle : NULL;\r
128                 int result = InternalPointer->GetSamplerIndex( handle );\r
129                 GC::KeepAlive( sampler );\r
130                 return result;\r
131         }\r
132 \r
133         DataStream^ ConstantTable::Buffer::get()\r
134         {\r
135                 return gcnew DataStream( InternalPointer->GetBufferPointer(), InternalPointer->GetBufferSize(), true, true, false );\r
136         }\r
137 \r
138         Result ConstantTable::SetDefaults( Device^ device )\r
139         {\r
140                 HRESULT hr = InternalPointer->SetDefaults( device->InternalPointer );\r
141                 return RECORD_D3D9( hr );\r
142         }\r
143 \r
144         generic<typename T> where T : value class\r
145         Result ConstantTable::SetValue( Device^ device, EffectHandle^ parameter, T value )\r
146         {\r
147                 HRESULT hr;\r
148                 D3DXHANDLE handle = parameter != nullptr ? parameter->InternalHandle : NULL;\r
149 \r
150                 if( T::typeid == bool::typeid )\r
151                 {\r
152                         BOOL newValue = Convert::ToInt32( value, CultureInfo::InvariantCulture );\r
153                         hr = InternalPointer->SetBool( device->InternalPointer, handle, newValue );\r
154                 }\r
155                 else if( T::typeid == float::typeid )\r
156                 {\r
157                         hr = InternalPointer->SetFloat( device->InternalPointer, handle, static_cast<FLOAT>( value ) );\r
158                 }\r
159                 else if( T::typeid == int::typeid )\r
160                 {\r
161                         hr = InternalPointer->SetInt( device->InternalPointer, handle, static_cast<INT>( value ) );\r
162                 }\r
163                 else if( T::typeid == Matrix::typeid )\r
164                 {\r
165                         hr = InternalPointer->SetMatrix( device->InternalPointer, handle, reinterpret_cast<D3DXMATRIX*>( &value ) );\r
166                 }\r
167                 else if( T::typeid == Vector4::typeid )\r
168                 {\r
169                         hr = InternalPointer->SetVector( device->InternalPointer, handle, reinterpret_cast<D3DXVECTOR4*>( &value ) );\r
170                 }\r
171                 else\r
172                 {\r
173                         hr = InternalPointer->SetValue( device->InternalPointer, handle, &value, static_cast<DWORD>( sizeof(T) ) );\r
174                 }\r
175 \r
176                 GC::KeepAlive( parameter );\r
177                 return RECORD_D3D9( hr );\r
178         }\r
179 \r
180         generic<typename T> where T : value class\r
181         Result ConstantTable::SetValue( Device^ device, EffectHandle^ parameter, array<T>^ values )\r
182         {\r
183                 HRESULT hr;\r
184                 D3DXHANDLE handle = parameter != nullptr ? parameter->InternalHandle : NULL;\r
185 \r
186                 if( T::typeid == bool::typeid )\r
187                 {\r
188                         array<BOOL>^ newValues = Array::ConvertAll<bool, int>( safe_cast<array<bool>>( values ), gcnew Converter<bool, int>( Convert::ToInt32 ) );\r
189                         pin_ptr<BOOL> pinnedValues = &newValues[0];\r
190 \r
191                         hr = InternalPointer->SetBoolArray( device->InternalPointer, handle, pinnedValues, values->Length );\r
192                 }\r
193                 else if( T::typeid == float::typeid )\r
194                 {\r
195                         pin_ptr<T> pinnedData = &values[0];\r
196                         hr = InternalPointer->SetFloatArray( device->InternalPointer, handle, reinterpret_cast<FLOAT*>( pinnedData ), values->Length );\r
197                 }\r
198                 else if( T::typeid == int::typeid )\r
199                 {\r
200                         pin_ptr<T> pinnedData = &values[0];\r
201                         hr = InternalPointer->SetIntArray( device->InternalPointer, handle, reinterpret_cast<INT*>( pinnedData ), values->Length );\r
202                 }\r
203                 else if( T::typeid == Matrix::typeid )\r
204                 {\r
205                         pin_ptr<T> pinnedData = &values[0];\r
206                         hr = InternalPointer->SetMatrixArray( device->InternalPointer, handle, reinterpret_cast<D3DXMATRIX*>( pinnedData ), values->Length );\r
207                 }\r
208                 else if( T::typeid == Vector4::typeid )\r
209                 {\r
210                         pin_ptr<T> pinnedData = &values[0];\r
211                         hr = InternalPointer->SetVectorArray( device->InternalPointer, handle, reinterpret_cast<D3DXVECTOR4*>( pinnedData ), values->Length );\r
212                 }\r
213                 else\r
214                 {\r
215                         pin_ptr<T> pinnedData = &values[0];\r
216                         hr = InternalPointer->SetValue( device->InternalPointer, handle, pinnedData, static_cast<DWORD>( sizeof(T) ) * values->Length );\r
217                 }\r
218 \r
219                 GC::KeepAlive( parameter );\r
220                 return RECORD_D3D9( hr );\r
221         }\r
222 \r
223         ConstantTableDescription ConstantTable::Description::get()\r
224         {\r
225                 D3DXCONSTANTTABLE_DESC nativeDesc;\r
226                 ConstantTableDescription description;\r
227 \r
228                 HRESULT hr = InternalPointer->GetDesc( &nativeDesc );\r
229                 \r
230                 if( RECORD_D3D9( hr ).IsFailure )\r
231                         return ConstantTableDescription();\r
232 \r
233                 description.Creator = gcnew String( nativeDesc.Creator );\r
234                 description.Version = gcnew Version( D3DSHADER_VERSION_MAJOR( nativeDesc.Version ), D3DSHADER_VERSION_MINOR( nativeDesc.Version ) );\r
235                 description.Constants = nativeDesc.Constants;\r
236                 return description;\r
237         }\r
238 }\r
239 }\r