aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/irrlicht-1.8.1/source/Irrlicht/CD3D9ShaderMaterialRenderer.cpp
diff options
context:
space:
mode:
authorDavid Walter Seikel2016-03-28 22:28:34 +1000
committerDavid Walter Seikel2016-03-28 22:28:34 +1000
commit7028cbe09c688437910a25623098762bf0fa592d (patch)
tree10b5af58277d9880380c2251f109325542c4e6eb /src/others/irrlicht-1.8.1/source/Irrlicht/CD3D9ShaderMaterialRenderer.cpp
parentMove lemon to the src/others directory. (diff)
downloadSledjHamr-7028cbe09c688437910a25623098762bf0fa592d.zip
SledjHamr-7028cbe09c688437910a25623098762bf0fa592d.tar.gz
SledjHamr-7028cbe09c688437910a25623098762bf0fa592d.tar.bz2
SledjHamr-7028cbe09c688437910a25623098762bf0fa592d.tar.xz
Move Irrlicht to src/others.
Diffstat (limited to 'src/others/irrlicht-1.8.1/source/Irrlicht/CD3D9ShaderMaterialRenderer.cpp')
-rw-r--r--src/others/irrlicht-1.8.1/source/Irrlicht/CD3D9ShaderMaterialRenderer.cpp540
1 files changed, 540 insertions, 0 deletions
diff --git a/src/others/irrlicht-1.8.1/source/Irrlicht/CD3D9ShaderMaterialRenderer.cpp b/src/others/irrlicht-1.8.1/source/Irrlicht/CD3D9ShaderMaterialRenderer.cpp
new file mode 100644
index 0000000..7d2f76e
--- /dev/null
+++ b/src/others/irrlicht-1.8.1/source/Irrlicht/CD3D9ShaderMaterialRenderer.cpp
@@ -0,0 +1,540 @@
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4
5#include "IrrCompileConfig.h"
6#ifdef _IRR_COMPILE_WITH_DIRECT3D_9_
7
8#include "CD3D9ShaderMaterialRenderer.h"
9#include "IShaderConstantSetCallBack.h"
10#include "IMaterialRendererServices.h"
11#include "IVideoDriver.h"
12#include "os.h"
13#include "irrString.h"
14
15#ifndef _IRR_D3D_NO_SHADER_DEBUGGING
16#include <stdio.h>
17#endif
18
19
20namespace irr
21{
22namespace video
23{
24
25//! Public constructor
26CD3D9ShaderMaterialRenderer::CD3D9ShaderMaterialRenderer(IDirect3DDevice9* d3ddev, video::IVideoDriver* driver,
27 s32& outMaterialTypeNr, const c8* vertexShaderProgram, const c8* pixelShaderProgram,
28 IShaderConstantSetCallBack* callback, IMaterialRenderer* baseMaterial, s32 userData)
29: pID3DDevice(d3ddev), Driver(driver), CallBack(callback), BaseMaterial(baseMaterial),
30 VertexShader(0), OldVertexShader(0), PixelShader(0), UserData(userData)
31{
32 #ifdef _DEBUG
33 setDebugName("CD3D9ShaderMaterialRenderer");
34 #endif
35
36 if (BaseMaterial)
37 BaseMaterial->grab();
38
39 if (CallBack)
40 CallBack->grab();
41
42 init(outMaterialTypeNr, vertexShaderProgram, pixelShaderProgram);
43}
44
45
46//! constructor only for use by derived classes who want to
47//! create a fall back material for example.
48CD3D9ShaderMaterialRenderer::CD3D9ShaderMaterialRenderer(IDirect3DDevice9* d3ddev,
49 video::IVideoDriver* driver,
50 IShaderConstantSetCallBack* callback,
51 IMaterialRenderer* baseMaterial, s32 userData)
52: pID3DDevice(d3ddev), Driver(driver), CallBack(callback), BaseMaterial(baseMaterial),
53 VertexShader(0), OldVertexShader(0), PixelShader(0), UserData(userData)
54{
55 #ifdef _DEBUG
56 setDebugName("CD3D9ShaderMaterialRenderer");
57 #endif
58
59 if (BaseMaterial)
60 BaseMaterial->grab();
61
62 if (CallBack)
63 CallBack->grab();
64}
65
66
67void CD3D9ShaderMaterialRenderer::init(s32& outMaterialTypeNr,
68 const c8* vertexShaderProgram, const c8* pixelShaderProgram)
69{
70 outMaterialTypeNr = -1;
71
72 // create vertex shader
73 if (!createVertexShader(vertexShaderProgram))
74 return;
75
76 // create pixel shader
77 if (!createPixelShader(pixelShaderProgram))
78 return;
79
80 // register myself as new material
81 outMaterialTypeNr = Driver->addMaterialRenderer(this);
82}
83
84
85//! Destructor
86CD3D9ShaderMaterialRenderer::~CD3D9ShaderMaterialRenderer()
87{
88 if (CallBack)
89 CallBack->drop();
90
91 if (VertexShader)
92 VertexShader->Release();
93
94 if (PixelShader)
95 PixelShader->Release();
96
97 if (BaseMaterial)
98 BaseMaterial->drop();
99}
100
101
102bool CD3D9ShaderMaterialRenderer::OnRender(IMaterialRendererServices* service, E_VERTEX_TYPE vtxtype)
103{
104 // call callback to set shader constants
105 if (CallBack && (VertexShader || PixelShader))
106 CallBack->OnSetConstants(service, UserData);
107
108 return true;
109}
110
111
112void CD3D9ShaderMaterialRenderer::OnSetMaterial(const video::SMaterial& material, const video::SMaterial& lastMaterial,
113 bool resetAllRenderstates, video::IMaterialRendererServices* services)
114{
115 if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
116 {
117 if (VertexShader)
118 {
119 // save old vertex shader
120 pID3DDevice->GetVertexShader(&OldVertexShader);
121
122 // set new vertex shader
123 if (FAILED(pID3DDevice->SetVertexShader(VertexShader)))
124 os::Printer::log("Could not set vertex shader.", ELL_WARNING);
125 }
126
127 // set new pixel shader
128 if (PixelShader)
129 {
130 if (FAILED(pID3DDevice->SetPixelShader(PixelShader)))
131 os::Printer::log("Could not set pixel shader.", ELL_WARNING);
132 }
133
134 if (BaseMaterial)
135 BaseMaterial->OnSetMaterial(material, material, true, services);
136 }
137
138 //let callback know used material
139 if (CallBack)
140 CallBack->OnSetMaterial(material);
141
142 services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
143}
144
145
146void CD3D9ShaderMaterialRenderer::OnUnsetMaterial()
147{
148 if (VertexShader)
149 pID3DDevice->SetVertexShader(OldVertexShader);
150
151 if (PixelShader)
152 pID3DDevice->SetPixelShader(0);
153
154 if (BaseMaterial)
155 BaseMaterial->OnUnsetMaterial();
156}
157
158
159//! Returns if the material is transparent. The scene managment needs to know this
160//! for being able to sort the materials by opaque and transparent.
161bool CD3D9ShaderMaterialRenderer::isTransparent() const
162{
163 return BaseMaterial ? BaseMaterial->isTransparent() : false;
164}
165
166
167bool CD3D9ShaderMaterialRenderer::createPixelShader(const c8* pxsh)
168{
169 if (!pxsh)
170 return true;
171
172 // compile shader
173
174 LPD3DXBUFFER code = 0;
175 LPD3DXBUFFER errors = 0;
176
177 #ifdef _IRR_D3D_NO_SHADER_DEBUGGING
178
179 // compile shader without debug info
180 stubD3DXAssembleShader(pxsh, (UINT)strlen(pxsh), 0, 0, 0, &code, &errors);
181 #else
182
183 // compile shader and emitt some debug informations to
184 // make it possible to debug the shader in visual studio
185
186 static int irr_dbg_file_nr = 0;
187 ++irr_dbg_file_nr;
188 char tmp[32];
189 sprintf(tmp, "irr_d3d9_dbg_shader_%d.psh", irr_dbg_file_nr);
190
191 FILE* f = fopen(tmp, "wb");
192 fwrite(pxsh, strlen(pxsh), 1, f);
193 fflush(f);
194 fclose(f);
195
196 stubD3DXAssembleShaderFromFile(tmp, 0, 0, D3DXSHADER_DEBUG, &code, &errors);
197
198 #endif
199
200
201 if (errors)
202 {
203 // print out compilation errors.
204 os::Printer::log("Pixel shader compilation failed:", ELL_ERROR);
205 os::Printer::log((c8*)errors->GetBufferPointer(), ELL_ERROR);
206
207 if (code)
208 code->Release();
209
210 errors->Release();
211 return false;
212 }
213
214 if (FAILED(pID3DDevice->CreatePixelShader((DWORD*)code->GetBufferPointer(), &PixelShader)))
215 {
216 os::Printer::log("Could not create pixel shader.", ELL_ERROR);
217 code->Release();
218 return false;
219 }
220
221 code->Release();
222 return true;
223}
224
225
226bool CD3D9ShaderMaterialRenderer::createVertexShader(const char* vtxsh)
227{
228 if (!vtxsh)
229 return true;
230
231 // compile shader
232
233 LPD3DXBUFFER code = 0;
234 LPD3DXBUFFER errors = 0;
235
236 #ifdef _IRR_D3D_NO_SHADER_DEBUGGING
237
238 // compile shader without debug info
239 stubD3DXAssembleShader(vtxsh, (UINT)strlen(vtxsh), 0, 0, 0, &code, &errors);
240
241 #else
242
243 // compile shader and emitt some debug informations to
244 // make it possible to debug the shader in visual studio
245
246 static int irr_dbg_file_nr = 0;
247 ++irr_dbg_file_nr;
248 char tmp[32];
249 sprintf(tmp, "irr_d3d9_dbg_shader_%d.vsh", irr_dbg_file_nr);
250
251 FILE* f = fopen(tmp, "wb");
252 fwrite(vtxsh, strlen(vtxsh), 1, f);
253 fflush(f);
254 fclose(f);
255
256 stubD3DXAssembleShaderFromFile(tmp, 0, 0, D3DXSHADER_DEBUG, &code, &errors);
257
258 #endif
259
260 if (errors)
261 {
262 // print out compilation errors.
263 os::Printer::log("Vertex shader compilation failed:", ELL_ERROR);
264 os::Printer::log((c8*)errors->GetBufferPointer(), ELL_ERROR);
265
266 if (code)
267 code->Release();
268
269 errors->Release();
270 return false;
271 }
272
273 if (!code || FAILED(pID3DDevice->CreateVertexShader((DWORD*)code->GetBufferPointer(), &VertexShader)))
274 {
275 os::Printer::log("Could not create vertex shader.", ELL_ERROR);
276 if (code)
277 code->Release();
278 return false;
279 }
280
281 code->Release();
282 return true;
283}
284
285
286HRESULT CD3D9ShaderMaterialRenderer::stubD3DXAssembleShader(LPCSTR pSrcData,
287 UINT SrcDataLen, CONST D3DXMACRO* pDefines,
288 LPD3DXINCLUDE pInclude, DWORD Flags, LPD3DXBUFFER* ppShader,
289 LPD3DXBUFFER* ppErrorMsgs)
290{
291 // Because Irrlicht needs to be able to start up even without installed d3d dlls, it
292 // needs to load external d3d dlls manually. examples for the dlls are:
293 // SDK dll name D3DX_SDK_VERSION
294 // Summer 2004: no dll 22
295 // February 2005: d3dx9_24.dll 24
296 // April 2005: d3dx9_25.dll 25
297 // June 2005: d3dx9_26.dll 26
298 // August 2005: d3dx9_27.dll 27
299 // October 2005,
300 // December 2005: d3dx9_28.dll 28
301
302 #if ( D3DX_SDK_VERSION < 24 )
303 // directly link functions, old d3d sdks didn't try to load external dlls
304 // when linking to the d3dx9.lib
305 #ifdef _MSC_VER
306 #pragma comment (lib, "d3dx9.lib")
307 #endif
308
309 // invoke static linked function
310 return D3DXAssembleShader(pSrcData, SrcDataLen, pDefines, pInclude,
311 Flags, ppShader, ppErrorMsgs);
312 #else
313 {
314 // try to load shader functions from the dll and print error if failed.
315
316 // D3DXAssembleShader signature
317 typedef HRESULT (WINAPI *AssembleShaderFunction)(LPCSTR pSrcData, UINT SrcDataLen,
318 CONST D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude,
319 DWORD Flags, LPD3DXBUFFER* ppShader,
320 LPD3DXBUFFER* ppErrorMsgs);
321
322 static bool LoadFailed = false;
323 static AssembleShaderFunction pFn = 0;
324
325 if (!pFn && !LoadFailed)
326 {
327 // try to load dll
328 io::path strDllName = "d3dx9_";
329 strDllName += (int)D3DX_SDK_VERSION;
330 strDllName += ".dll";
331
332 HMODULE hMod = LoadLibrary(strDllName.c_str());
333 if (hMod)
334 pFn = (AssembleShaderFunction)GetProcAddress(hMod, "D3DXAssembleShader");
335
336 if (!pFn)
337 {
338 LoadFailed = true;
339 os::Printer::log("Could not load shader function D3DXAssembleShader from dll, shaders disabled",
340 strDllName.c_str(), ELL_ERROR);
341 }
342 }
343
344 if (pFn)
345 {
346 // call already loaded function
347 return (*pFn)(pSrcData, SrcDataLen, pDefines, pInclude, Flags, ppShader, ppErrorMsgs);
348 }
349 }
350 #endif // D3DX_SDK_VERSION < 24
351
352 return 0;
353}
354
355
356HRESULT CD3D9ShaderMaterialRenderer::stubD3DXAssembleShaderFromFile(LPCSTR pSrcFile,
357 CONST D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, DWORD Flags,
358 LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs)
359{
360 // wondering what I'm doing here?
361 // see comment in CD3D9ShaderMaterialRenderer::stubD3DXAssembleShader()
362
363 #if ( D3DX_SDK_VERSION < 24 )
364 // directly link functions, old d3d sdks didn't try to load external dlls
365 // when linking to the d3dx9.lib
366 #ifdef _MSC_VER
367 #pragma comment (lib, "d3dx9.lib")
368 #endif
369
370 // invoke static linked function
371 return D3DXAssembleShaderFromFileA(pSrcFile, pDefines, pInclude, Flags,
372 ppShader, ppErrorMsgs);
373 #else
374 {
375 // try to load shader functions from the dll and print error if failed.
376
377 // D3DXAssembleShaderFromFileA signature
378 typedef HRESULT (WINAPI *AssembleShaderFromFileFunction)(LPCSTR pSrcFile,
379 CONST D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, DWORD Flags,
380 LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs);
381
382 static bool LoadFailed = false;
383 static AssembleShaderFromFileFunction pFn = 0;
384
385 if (!pFn && !LoadFailed)
386 {
387 // try to load dll
388 io::path strDllName = "d3dx9_";
389 strDllName += (int)D3DX_SDK_VERSION;
390 strDllName += ".dll";
391
392 HMODULE hMod = LoadLibrary(strDllName.c_str());
393 if (hMod)
394 pFn = (AssembleShaderFromFileFunction)GetProcAddress(hMod, "D3DXAssembleShaderFromFileA");
395
396 if (!pFn)
397 {
398 LoadFailed = true;
399 os::Printer::log("Could not load shader function D3DXAssembleShaderFromFileA from dll, shaders disabled",
400 strDllName.c_str(), ELL_ERROR);
401 }
402 }
403
404 if (pFn)
405 {
406 // call already loaded function
407 return (*pFn)(pSrcFile, pDefines, pInclude, Flags, ppShader, ppErrorMsgs);
408 }
409 }
410 #endif // D3DX_SDK_VERSION < 24
411
412 return 0;
413}
414
415
416HRESULT CD3D9ShaderMaterialRenderer::stubD3DXCompileShader(LPCSTR pSrcData, UINT SrcDataLen, CONST D3DXMACRO* pDefines,
417 LPD3DXINCLUDE pInclude, LPCSTR pFunctionName,
418 LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader,
419 LPD3DXBUFFER* ppErrorMsgs, LPD3DXCONSTANTTABLE* ppConstantTable)
420{
421 // wondering what I'm doing here?
422 // see comment in CD3D9ShaderMaterialRenderer::stubD3DXAssembleShader()
423
424 #if ( D3DX_SDK_VERSION < 24 )
425 // directly link functions, old d3d sdks didn't try to load external dlls
426 // when linking to the d3dx9.lib
427 #ifdef _MSC_VER
428 #pragma comment (lib, "d3dx9.lib")
429 #endif
430
431 // invoke static linked function
432 return D3DXCompileShader(pSrcData, SrcDataLen, pDefines, pInclude, pFunctionName, pProfile, Flags, ppShader, ppErrorMsgs, ppConstantTable);
433 #else
434 {
435 // try to load shader functions from the dll and print error if failed.
436
437 // D3DXCompileShader
438 typedef HRESULT (WINAPI *D3DXCompileShaderFunction)(LPCSTR pSrcData, UINT SrcDataLen, CONST D3DXMACRO* pDefines,
439 LPD3DXINCLUDE pInclude, LPCSTR pFunctionName,
440 LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader,
441 LPD3DXBUFFER* ppErrorMsgs, LPD3DXCONSTANTTABLE* ppConstantTable);
442
443 static bool LoadFailed = false;
444 static D3DXCompileShaderFunction pFn = 0;
445
446 if (!pFn && !LoadFailed)
447 {
448 // try to load dll
449 io::path strDllName = "d3dx9_";
450 strDllName += (int)D3DX_SDK_VERSION;
451 strDllName += ".dll";
452
453 HMODULE hMod = LoadLibrary(strDllName.c_str());
454 if (hMod)
455 pFn = (D3DXCompileShaderFunction)GetProcAddress(hMod, "D3DXCompileShader");
456
457 if (!pFn)
458 {
459 LoadFailed = true;
460 os::Printer::log("Could not load shader function D3DXCompileShader from dll, shaders disabled",
461 strDllName.c_str(), ELL_ERROR);
462 }
463 }
464
465 if (pFn)
466 {
467 // call already loaded function
468 return (*pFn)(pSrcData, SrcDataLen, pDefines, pInclude, pFunctionName, pProfile, Flags, ppShader, ppErrorMsgs, ppConstantTable);
469 }
470 }
471 #endif // D3DX_SDK_VERSION < 24
472
473 return 0;
474}
475
476HRESULT CD3D9ShaderMaterialRenderer::stubD3DXCompileShaderFromFile(LPCSTR pSrcFile, CONST D3DXMACRO* pDefines,
477 LPD3DXINCLUDE pInclude, LPCSTR pFunctionName,
478 LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs,
479 LPD3DXCONSTANTTABLE* ppConstantTable)
480{
481 // wondering what I'm doing here?
482 // see comment in CD3D9ShaderMaterialRenderer::stubD3DXAssembleShader()
483
484 #if ( D3DX_SDK_VERSION < 24 )
485 // directly link functions, old d3d sdks didn't try to load external dlls
486 // when linking to the d3dx9.lib
487 #ifdef _MSC_VER
488 #pragma comment (lib, "d3dx9.lib")
489 #endif
490
491 // invoke static linked function
492 return D3DXCompileShaderFromFileA(pSrcFile, pDefines, pInclude, pFunctionName, pProfile, Flags, ppShader, ppErrorMsgs, ppConstantTable);
493 #else
494 {
495 // try to load shader functions from the dll and print error if failed.
496
497 // D3DXCompileShaderFromFileA
498 typedef HRESULT (WINAPI *D3DXCompileShaderFromFileFunction)(LPCSTR pSrcFile,
499 CONST D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, LPCSTR pFunctionName,
500 LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs,
501 LPD3DXCONSTANTTABLE* ppConstantTable);
502
503 static bool LoadFailed = false;
504 static D3DXCompileShaderFromFileFunction pFn = 0;
505
506 if (!pFn && !LoadFailed)
507 {
508 // try to load dll
509 io::path strDllName = "d3dx9_";
510 strDllName += (int)D3DX_SDK_VERSION;
511 strDllName += ".dll";
512
513 HMODULE hMod = LoadLibrary(strDllName.c_str());
514 if (hMod)
515 pFn = (D3DXCompileShaderFromFileFunction)GetProcAddress(hMod, "D3DXCompileShaderFromFileA");
516
517 if (!pFn)
518 {
519 LoadFailed = true;
520 os::Printer::log("Could not load shader function D3DXCompileShaderFromFileA from dll, shaders disabled",
521 strDllName.c_str(), ELL_ERROR);
522 }
523 }
524
525 if (pFn)
526 {
527 // call already loaded function
528 return (*pFn)(pSrcFile, pDefines, pInclude, pFunctionName, pProfile, Flags, ppShader, ppErrorMsgs, ppConstantTable);
529 }
530 }
531 #endif // D3DX_SDK_VERSION < 24
532
533 return 0;
534}
535
536
537} // end namespace video
538} // end namespace irr
539
540#endif // _IRR_COMPILE_WITH_DIRECT3D_9_