Menu

[025cc2]: / DirectPython11 / DPEffect.cpp  Maximize  Restore  History

Download this file

366 lines (311 with data), 12.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
Copyright (C) 2010, Heikki Salo
All rights reserved.
Distributed under the BSD license:
http://www.opensource.org/licenses/bsd-license.php
*/
#include "stdafx.h"
#include "DPEffect.hpp"
#include "DPDevice.hpp"
#include "DPView.hpp"
#include "DPMatrix.hpp"
#include "DPBuffer.hpp"
DPEffect::~DPEffect()
{
release();
}
D3D10_SHADER_MACRO DPMacroDefines = {
"DP_VERSION", DPVERSIONHEXSTR
};
DPEffect::DPEffect(const char* path, python::object& macros, UINT flags)
{
effect = 0;
DPDevice* device = getDPDevice();
std::vector<D3D10_SHADER_MACRO> defines;
if (macros != python::object()) {
/* Convert a dictionary into macro structures.
XXX It is possible that these strings are freed
before D3DX11CompileFromFile() has finished!
*/
python::dict pydict(macros);
python::object items = pydict.items();
python::ssize_t size = python::len(items);
for (python::ssize_t i=0; i < size; ++i) {
D3D10_SHADER_MACRO tmp;
tmp.Name = python::extract<const char*>(items[i][0]);
tmp.Definition = python::extract<const char*>(items[i][1]);
defines.push_back(tmp);
}
}
D3D10_SHADER_MACRO terminator = {0};
defines.push_back(DPMacroDefines);
defines.push_back(terminator);
ID3D10Blob* blob = 0;
ID3D10Blob* errorBlob = 0;
HRESULT hr;
if (DPIsDebugEnabled()) {
flags |= D3D10_SHADER_DEBUG | D3D10_SHADER_SKIP_OPTIMIZATION;
}
Py_BEGIN_ALLOW_THREADS //XXX - Not totally safe.
hr = D3DX11CompileFromFileA(path, &defines[0], NULL, NULL,
"fx_5_0", flags, 0, NULL, &blob, &errorBlob, NULL);
Py_END_ALLOW_THREADS
if (FAILED(hr)) {
DPFormatter error;
error << "Effect compilation failed: ";
if (errorBlob) {
const char* errorMsg = static_cast<const char*>(errorBlob->GetBufferPointer());
if (errorMsg)
error << errorMsg;
}
SafeRelease(errorBlob);
SafeRelease(blob);
error.raise();
}
else if (errorBlob) {
const char* warnings = static_cast<const char*>(errorBlob->GetBufferPointer());
if (warnings) {
DPFormatter formatter;
formatter << "Effect compilation warning(s): " << warnings;
formatter.warn(); //XXX can leak.
}
}
SafeRelease(errorBlob);
hr = D3DX11CreateEffectFromMemory(blob->GetBufferPointer(),
blob->GetBufferSize(), 0, device->getDevice(), &effect);
SafeRelease(blob);
if (FAILED(hr)) {
DPThrow("Effect creation failed");
}
DPBase::init(device->getDevice());
}
void DPEffect::release()
{
SafeRelease(effect);
}
template<typename T>
inline T isValid(T arg, const char* errorMsg=0) {
if (!arg->IsValid()) {
DPThrow(errorMsg ? errorMsg : "Type is not valid");
}
return arg;
}
D3DX11_EFFECT_TYPE_DESC getType(ID3DX11EffectVariable* variable)
{
D3DX11_EFFECT_TYPE_DESC typeDesc;
HRESULT hr = isValid(variable->GetType(), "Invalid type info")->GetDesc(&typeDesc);
if (FAILED(hr))
DPThrow("Can't get type info");
return typeDesc;
}
void DPEffect::apply(UINT techIndex, UINT passIndex)
{
ID3DX11EffectTechnique* technique = isValid(effect->GetTechniqueByIndex(techIndex), "Invalid technique");
ID3DX11EffectPass* fxPass = isValid(technique->GetPassByIndex(passIndex), "Invalid pass");
TestHR(fxPass->Apply(0, getContext()));
}
void DPEffect::applyStr(const char* techName, UINT passIndex)
{
ID3DX11EffectTechnique* technique = isValid(effect->GetTechniqueByName(techName), "Invalid technique");
ID3DX11EffectPass* fxPass = isValid(technique->GetPassByIndex(passIndex), "Invalid pass");
TestHR(fxPass->Apply(0, getContext()));
}
void DPEffect::set(const char* varName, python::object& value)
{
if (value == python::object())
DPThrow("Can't set None");
ID3DX11EffectVariable* variable = getVariable(varName);
D3DX11_EFFECT_TYPE_DESC typeDesc = getType(variable);
if (typeDesc.Rows == 4 && typeDesc.Columns == 4 && typeDesc.Elements > 0) {
//Probably a matrix array. (Single matrix has an overload.)
boost::scoped_array<XMMATRIX> temp(new XMMATRIX[typeDesc.Elements]);
for (UINT i=0; i < typeDesc.Elements; ++i) {
DPMatrix& matrix = python::extract<DPMatrix&>(value[i]);
temp[i] = matrix.getMatrix();
}
float* data = &temp[0]._11;
TestHR(isValid(variable->AsMatrix(), "Not a matrix")->SetMatrixArray(data, 0, typeDesc.Elements));
return;
}
if (typeDesc.Rows > 1)
DPThrow("No support for row data");
if (typeDesc.Elements == 0) {
if (typeDesc.Columns == 1 && typeDesc.Rows == 1) {
//Single value, for example "float somescalar;"
TestHR(isValid(variable->AsScalar(), "Not a scalar")->SetFloat(python::extract<float>(value)));
}
else {
//Vector, for example "float3 somevector;"
if (typeDesc.Columns > 4)
DPThrow("Too many columns");
float data[4] = {0.0f};
for (UINT i = 0; i < typeDesc.Columns; ++i) {
data[i] = python::extract<float>(value[i]);
}
TestHR(isValid(variable->AsVector(), "Not a vector")->SetFloatVector(data));
}
}
else {
boost::scoped_array<float> temp(new float[typeDesc.Elements * typeDesc.Columns]);
UINT index = 0;
if (typeDesc.Columns == 1 && typeDesc.Rows == 1) {
//Array of single values, for example "float singlearray[8];"
for (UINT i=0; i < typeDesc.Elements; ++i) {
temp[index++] = python::extract<float>(value[i]);
}
TestHR(isValid(variable->AsScalar(), "Not a scalar")->SetFloatArray(&temp[0], 0, typeDesc.Elements));
}
else {
//Array of vectors, for example "float2 somearray[3];"
for (UINT i=0; i < typeDesc.Elements; ++i) {
for (UINT x=0; x < typeDesc.Columns; ++x) {
temp[index++] = python::extract<float>(value[i][x]);
}
}
TestHR(isValid(variable->AsVector(), "Not a vector")->SetFloatVectorArray(&temp[0], 0, typeDesc.Elements));
}
}
}
void DPEffect::setView(const char* varName, DPView& view)
{
ID3DX11EffectVariable* variable = getVariable(varName);
D3DX11_EFFECT_TYPE_DESC typeDesc = getType(variable);
//Both D3D10_ and D3D11_ values are used, not a bug.
switch (typeDesc.Type) {
case D3D10_SVT_RENDERTARGETVIEW:
if (view.getRenderTargetView() == 0)
DPThrow("Not a render target view");
TestHR(isValid(variable->AsRenderTargetView())->SetRenderTarget(view.getRenderTargetView()));
break;
case D3D10_SVT_DEPTHSTENCILVIEW:
if (view.getDepthStencilView() == 0)
DPThrow("Not a render target view");
TestHR(isValid(variable->AsDepthStencilView())->SetDepthStencil(view.getDepthStencilView()));
break;
case D3D11_SVT_RWBYTEADDRESS_BUFFER:
case D3D11_SVT_RWSTRUCTURED_BUFFER:
if (view.getUnorderedAccessView() == 0)
DPThrow("Not an unordered buffer view");
TestHR(isValid(variable->AsUnorderedAccessView())->SetUnorderedAccessView(view.getUnorderedAccessView()));
break;
case D3D11_SVT_BYTEADDRESS_BUFFER:
case D3D11_SVT_STRUCTURED_BUFFER:
case D3D10_SVT_TEXTURE2DMS:
case D3D10_SVT_TEXTURE2DMSARRAY:
case D3D10_SVT_TEXTURE2D:
case D3D10_SVT_TEXTURE2DARRAY:
if (view.getShaderView() == 0)
DPThrow("Not a shader resource view");
TestHR(isValid(variable->AsShaderResource())->SetResource(view.getShaderView()));
break;
default:
DPThrow("Unsupported view type");
}
}
void DPEffect::setBuffer(const char* varName, DPBuffer& buffer)
{
ID3DX11EffectConstantBuffer* fxBuffer = isValid(effect->GetConstantBufferByName(varName), "Variable is not valid");
TestHR(isValid(fxBuffer->AsConstantBuffer(), "Not a constant buffer")->SetConstantBuffer(buffer.getBuffer()));
}
void DPEffect::setMatrix(const char* varName, DPMatrix& matrix)
{
const float* constPtr = &matrix.getMatrix()._11;
TestHR(isValid(getVariable(varName)->AsMatrix(), "Not a matrix")->SetMatrix(const_cast<float*>(constPtr)));
}
void DPEffect::setRaw(const char* varName, python::object& source)
{
ID3DX11EffectVariable* variable = getVariable(varName);
const void* data = 0;
Py_ssize_t size = 0;
if (PyObject_AsReadBuffer(source.ptr(), &data, &size) == -1)
python::throw_error_already_set();
TestHR(variable->SetRawValue(const_cast<void*>(data), 0, size));
}
#define RETURN_VAR(tp, getter, arraygetter, vartype) \
if (typeDesc.Elements > 0) { \
boost::scoped_array<vartype> raw(new vartype[typeDesc.Elements]); \
TestHR(variable->tp()->arraygetter(&raw[0], 0, typeDesc.Elements)); \
python::list results; \
for (UINT i=0; i < typeDesc.Elements; ++i) { \
results.append(raw[i]); \
} \
return results; \
} \
else { \
vartype temp; \
TestHR(variable->tp()->getter(&temp)); \
return python::object(temp); \
}
python::object DPEffect::get(const char* varName)
{
ID3DX11EffectVariable* variable = getVariable(varName);
D3DX11_EFFECT_TYPE_DESC typeDesc = getType(variable);
if (typeDesc.Rows > 1)
DPThrow("No support for row data");
switch (typeDesc.Type) {
case D3D10_SVT_STRING:
RETURN_VAR(AsString, GetString, GetStringArray, const char*)
case D3D10_SVT_INT:
RETURN_VAR(AsScalar, GetInt, GetIntArray, int)
case D3D10_SVT_FLOAT:
RETURN_VAR(AsScalar, GetFloat, GetFloatArray, float)
}
DPThrow("Unsupported type");
return python::object(); //Should never get in here.
}
ID3DX11EffectVariable* DPEffect::getVariable(const char* name)
{
ID3DX11EffectVariable* variable = 0;
if (name[0] == ':')
variable = effect->GetVariableBySemantic(name + 1);
else
variable = effect->GetVariableByName(name);
if (!variable->IsValid())
DPThrow("Variable is not valid");
return variable;
}
python::list DPEffect::getTechniques()
{
D3DX11_EFFECT_DESC desc;
TestHR(effect->GetDesc(&desc));
python::list result;
for (UINT i=0; i < desc.Techniques; ++i) {
ID3DX11EffectTechnique* tech = effect->GetTechniqueByIndex(i);
D3DX11_TECHNIQUE_DESC techDesc;
TestHR(tech->GetDesc(&techDesc));
result.append(techDesc.Name);
}
return result;
}
python::list DPEffect::getPasses(UINT techIndex)
{
ID3DX11EffectTechnique* tech = isValid(effect->GetTechniqueByIndex(techIndex), "Invalid technique");
D3DX11_TECHNIQUE_DESC techDesc;
TestHR(tech->GetDesc(&techDesc));
python::list result;
for (UINT i=0; i < techDesc.Passes; ++i) {
ID3DX11EffectPass* fxPass = tech->GetPassByIndex(i);
D3DX11_PASS_DESC passDesc;
TestHR(fxPass->GetDesc(&passDesc));
result.append(passDesc.Name);
}
return result;
}
void ExportDPEffect()
{
using namespace boost::python;
class_<DPEffect, boost::noncopyable>("Effect", init<const char*, object&, UINT>(
(arg("name"), arg("defines") = object(), arg("flags") = D3D10_SHADER_ENABLE_STRICTNESS) ))
.def("release", &DPEffect::release)
.def("get", &DPEffect::get)
.def("set", &DPEffect::set)
.def("set", &DPEffect::setView)
.def("set", &DPEffect::setBuffer)
.def("set", &DPEffect::setMatrix)
.def("setRaw", &DPEffect::setRaw)
.def("apply", &DPEffect::apply)
.def("apply", &DPEffect::applyStr)
.def("getTechniques", &DPEffect::getTechniques)
.def("getPasses", &DPEffect::getPasses)
;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.