~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to source/Irrlicht/CD3D8Driver.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2002-2011 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
 
 
7
#define _IRR_DONT_DO_MEMORY_DEBUGGING_HERE
 
8
#include "CD3D8Driver.h"
 
9
 
 
10
#ifdef _IRR_COMPILE_WITH_DIRECT3D_8_
 
11
 
 
12
#include "os.h"
 
13
#include "S3DVertex.h"
 
14
#include "CD3D8Texture.h"
 
15
#include "CD3D8MaterialRenderer.h"
 
16
#include "CD3D8ShaderMaterialRenderer.h"
 
17
#include "CD3D8NormalMapRenderer.h"
 
18
#include "CD3D8ParallaxMapRenderer.h"
 
19
#include "SIrrCreationParameters.h"
 
20
 
 
21
namespace irr
 
22
{
 
23
namespace video
 
24
{
 
25
 
 
26
 
 
27
//! constructor
 
28
CD3D8Driver::CD3D8Driver(const core::dimension2d<u32>& screenSize, HWND window,
 
29
                        bool fullscreen, bool stencilbuffer,
 
30
                        io::IFileSystem* io, bool pureSoftware, bool vsync)
 
31
: CNullDriver(io, screenSize), CurrentRenderMode(ERM_NONE),
 
32
        ResetRenderStates(true), Transformation3DChanged(false), StencilBuffer(stencilbuffer),
 
33
        D3DLibrary(0), pID3D(0), pID3DDevice(0), PrevRenderTarget(0),
 
34
        WindowId(0), SceneSourceRect(0),
 
35
        LastVertexType((video::E_VERTEX_TYPE)-1), MaxTextureUnits(0), MaxUserClipPlanes(0),
 
36
        MaxLightDistance(0), LastSetLight(-1), DeviceLost(false),
 
37
        DriverWasReset(true), DisplayAdapter(0)
 
38
{
 
39
        #ifdef _DEBUG
 
40
        setDebugName("CD3D8Driver");
 
41
        #endif
 
42
 
 
43
        printVersion();
 
44
 
 
45
        for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
 
46
                CurrentTexture[i] = 0;
 
47
        MaxLightDistance=sqrtf(FLT_MAX);
 
48
        // create sphere map matrix
 
49
 
 
50
        SphereMapMatrixD3D8._11 = 0.5f; SphereMapMatrixD3D8._12 = 0.0f;
 
51
        SphereMapMatrixD3D8._13 = 0.0f; SphereMapMatrixD3D8._14 = 0.0f;
 
52
        SphereMapMatrixD3D8._21 = 0.0f; SphereMapMatrixD3D8._22 =-0.5f;
 
53
        SphereMapMatrixD3D8._23 = 0.0f; SphereMapMatrixD3D8._24 = 0.0f;
 
54
        SphereMapMatrixD3D8._31 = 0.0f; SphereMapMatrixD3D8._32 = 0.0f;
 
55
        SphereMapMatrixD3D8._33 = 1.0f; SphereMapMatrixD3D8._34 = 0.0f;
 
56
        SphereMapMatrixD3D8._41 = 0.5f; SphereMapMatrixD3D8._42 = 0.5f;
 
57
        SphereMapMatrixD3D8._43 = 0.0f; SphereMapMatrixD3D8._44 = 1.0f;
 
58
 
 
59
        UnitMatrixD3D8 = *(D3DMATRIX*)((void*)core::IdentityMatrix.pointer());
 
60
 
 
61
        // init direct 3d is done in the factory function
 
62
}
 
63
 
 
64
 
 
65
//! destructor
 
66
CD3D8Driver::~CD3D8Driver()
 
67
{
 
68
        deleteMaterialRenders();
 
69
 
 
70
        // drop d3d8
 
71
 
 
72
        if (pID3DDevice)
 
73
                pID3DDevice->Release();
 
74
 
 
75
        if (pID3D)
 
76
                pID3D->Release();
 
77
}
 
78
 
 
79
 
 
80
void CD3D8Driver::createMaterialRenderers()
 
81
{
 
82
        // create D3D8 material renderers
 
83
 
 
84
        addAndDropMaterialRenderer(new CD3D8MaterialRenderer_SOLID(pID3DDevice, this));
 
85
        addAndDropMaterialRenderer(new CD3D8MaterialRenderer_SOLID_2_LAYER(pID3DDevice, this));
 
86
 
 
87
        // add the same renderer for all lightmap types
 
88
        CD3D8MaterialRenderer_LIGHTMAP* lmr = new CD3D8MaterialRenderer_LIGHTMAP(pID3DDevice, this);
 
89
        addMaterialRenderer(lmr); // for EMT_LIGHTMAP:
 
90
        addMaterialRenderer(lmr); // for EMT_LIGHTMAP_ADD:
 
91
        addMaterialRenderer(lmr); // for EMT_LIGHTMAP_M2:
 
92
        addMaterialRenderer(lmr); // for EMT_LIGHTMAP_M4:
 
93
        addMaterialRenderer(lmr); // for EMT_LIGHTMAP_LIGHTING:
 
94
        addMaterialRenderer(lmr); // for EMT_LIGHTMAP_LIGHTING_M2:
 
95
        addMaterialRenderer(lmr); // for EMT_LIGHTMAP_LIGHTING_M4:
 
96
        lmr->drop();
 
97
 
 
98
        // add remaining material renderers
 
99
        addAndDropMaterialRenderer(new CD3D8MaterialRenderer_DETAIL_MAP(pID3DDevice, this));
 
100
        addAndDropMaterialRenderer(new CD3D8MaterialRenderer_SPHERE_MAP(pID3DDevice, this));
 
101
        addAndDropMaterialRenderer(new CD3D8MaterialRenderer_REFLECTION_2_LAYER(pID3DDevice, this));
 
102
        addAndDropMaterialRenderer(new CD3D8MaterialRenderer_TRANSPARENT_ADD_COLOR(pID3DDevice, this));
 
103
        addAndDropMaterialRenderer(new CD3D8MaterialRenderer_TRANSPARENT_ALPHA_CHANNEL(pID3DDevice, this));
 
104
        addAndDropMaterialRenderer(new CD3D8MaterialRenderer_TRANSPARENT_ALPHA_CHANNEL_REF(pID3DDevice, this));
 
105
        addAndDropMaterialRenderer(new CD3D8MaterialRenderer_TRANSPARENT_VERTEX_ALPHA(pID3DDevice, this));
 
106
        addAndDropMaterialRenderer(new CD3D8MaterialRenderer_TRANSPARENT_REFLECTION_2_LAYER(pID3DDevice, this));
 
107
 
 
108
        // add normal map renderers
 
109
        s32 tmp = 0;
 
110
        video::IMaterialRenderer* renderer = 0;
 
111
 
 
112
        renderer = new CD3D8NormalMapRenderer(pID3DDevice, this, tmp,
 
113
                MaterialRenderers[EMT_SOLID].Renderer);
 
114
        renderer->drop();
 
115
 
 
116
        renderer = new CD3D8NormalMapRenderer(pID3DDevice, this, tmp,
 
117
                MaterialRenderers[EMT_TRANSPARENT_ADD_COLOR].Renderer);
 
118
        renderer->drop();
 
119
 
 
120
        renderer = new CD3D8NormalMapRenderer(pID3DDevice, this, tmp,
 
121
                MaterialRenderers[EMT_TRANSPARENT_VERTEX_ALPHA].Renderer);
 
122
        renderer->drop();
 
123
 
 
124
        // add parallax map renderers
 
125
 
 
126
        renderer = new CD3D8ParallaxMapRenderer(pID3DDevice, this, tmp,
 
127
                MaterialRenderers[EMT_SOLID].Renderer);
 
128
        renderer->drop();
 
129
 
 
130
        renderer = new CD3D8ParallaxMapRenderer(pID3DDevice, this, tmp,
 
131
                MaterialRenderers[EMT_TRANSPARENT_ADD_COLOR].Renderer);
 
132
        renderer->drop();
 
133
 
 
134
                renderer = new CD3D8ParallaxMapRenderer(pID3DDevice, this, tmp,
 
135
                MaterialRenderers[EMT_TRANSPARENT_VERTEX_ALPHA].Renderer);
 
136
        renderer->drop();
 
137
 
 
138
        // add basic 1 texture blending
 
139
        addAndDropMaterialRenderer(new CD3D8MaterialRenderer_ONETEXTURE_BLEND(pID3DDevice, this));
 
140
}
 
141
 
 
142
 
 
143
//! initialises the Direct3D API
 
144
bool CD3D8Driver::initDriver(const core::dimension2d<u32>& screenSize,
 
145
                HWND hwnd, u32 bits, bool fullScreen, bool pureSoftware,
 
146
                bool highPrecisionFPU, bool vsync, u8 antiAlias, u32 displayAdapter)
 
147
{
 
148
        HRESULT hr;
 
149
        typedef IDirect3D8 * (__stdcall *D3DCREATETYPE)(UINT);
 
150
        DisplayAdapter = displayAdapter;
 
151
 
 
152
#if defined( _IRR_XBOX_PLATFORM_)
 
153
        D3DCREATETYPE d3dCreate = (D3DCREATETYPE) &Direct3DCreate8;
 
154
#else
 
155
        D3DLibrary = LoadLibrary( __TEXT("d3d8.dll") );
 
156
 
 
157
        if (!D3DLibrary)
 
158
        {
 
159
                os::Printer::log("Error, could not load d3d8.dll.", ELL_ERROR);
 
160
                return false;
 
161
        }
 
162
 
 
163
        D3DCREATETYPE d3dCreate = (D3DCREATETYPE) GetProcAddress(D3DLibrary, "Direct3DCreate8");
 
164
 
 
165
        if (!d3dCreate)
 
166
        {
 
167
                os::Printer::log("Error, could not get proc adress of Direct3DCreate8.", ELL_ERROR);
 
168
                return false;
 
169
        }
 
170
#endif
 
171
 
 
172
        //just like pID3D = Direct3DCreate8(D3D_SDK_VERSION);
 
173
        pID3D = (*d3dCreate)(D3D_SDK_VERSION);
 
174
 
 
175
        if (!pID3D)
 
176
        {
 
177
                os::Printer::log("Error initializing D3D.", ELL_ERROR);
 
178
                return false;
 
179
        }
 
180
 
 
181
        // print device information
 
182
        D3DADAPTER_IDENTIFIER8 dai;
 
183
        if (!FAILED(pID3D->GetAdapterIdentifier(DisplayAdapter, D3DENUM_NO_WHQL_LEVEL, &dai)))
 
184
        {
 
185
                char tmp[512];
 
186
 
 
187
                s32 Product = HIWORD(dai.DriverVersion.HighPart);
 
188
                s32 Version = LOWORD(dai.DriverVersion.HighPart);
 
189
                s32 SubVersion = HIWORD(dai.DriverVersion.LowPart);
 
190
                s32 Build = LOWORD(dai.DriverVersion.LowPart);
 
191
 
 
192
                sprintf(tmp, "%s %s %d.%d.%d.%d", dai.Description, dai.Driver, Product, Version,
 
193
                        SubVersion, Build);
 
194
                os::Printer::log(tmp, ELL_INFORMATION);
 
195
        }
 
196
 
 
197
        D3DDISPLAYMODE d3ddm;
 
198
        hr = pID3D->GetAdapterDisplayMode(DisplayAdapter, &d3ddm);
 
199
        if (FAILED(hr))
 
200
        {
 
201
                os::Printer::log("Error: Could not get Adapter Display mode.", ELL_ERROR);
 
202
                return false;
 
203
        }
 
204
 
 
205
        ZeroMemory(&present, sizeof(present));
 
206
 
 
207
        present.SwapEffect = D3DSWAPEFFECT_DISCARD;
 
208
        present.Windowed = TRUE;
 
209
        present.BackBufferFormat = d3ddm.Format;
 
210
        present.EnableAutoDepthStencil = TRUE;
 
211
 
 
212
        if (fullScreen)
 
213
        {
 
214
                present.SwapEffect = D3DSWAPEFFECT_FLIP;
 
215
                present.Windowed = FALSE;
 
216
                present.BackBufferWidth = screenSize.Width;
 
217
                present.BackBufferHeight = screenSize.Height;
 
218
                present.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
 
219
                present.FullScreen_PresentationInterval = vsync ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE;
 
220
 
 
221
                if (bits == 32)
 
222
                        present.BackBufferFormat = D3DFMT_X8R8G8B8;
 
223
                else
 
224
                        present.BackBufferFormat = D3DFMT_R5G6B5;
 
225
        }
 
226
 
 
227
        D3DDEVTYPE devtype = D3DDEVTYPE_HAL;
 
228
        #ifndef _IRR_D3D_NO_SHADER_DEBUGGING
 
229
        devtype = D3DDEVTYPE_REF;
 
230
        #endif
 
231
 
 
232
        // enable anti alias if possible and whished
 
233
        if (antiAlias > 0)
 
234
        {
 
235
                if(antiAlias > 16)
 
236
                        antiAlias = 16;
 
237
 
 
238
                while(antiAlias > 0)
 
239
                {
 
240
                        if(!FAILED(pID3D->CheckDeviceMultiSampleType(DisplayAdapter,
 
241
                                devtype , present.BackBufferFormat, !fullScreen,
 
242
                                (D3DMULTISAMPLE_TYPE)antiAlias)))
 
243
                        {
 
244
                                present.MultiSampleType = (D3DMULTISAMPLE_TYPE)antiAlias;
 
245
                                present.SwapEffect       = D3DSWAPEFFECT_DISCARD;
 
246
                                break;
 
247
                        }
 
248
                        --antiAlias;
 
249
                }
 
250
 
 
251
                if(antiAlias==0)
 
252
                        os::Printer::log("Anti aliasing disabled because hardware/driver lacks necessary caps.", ELL_WARNING);
 
253
        }
 
254
 
 
255
        // check stencil buffer compatibility
 
256
        if (StencilBuffer)
 
257
        {
 
258
                present.AutoDepthStencilFormat = D3DFMT_D24S8;
 
259
                if(FAILED(pID3D->CheckDeviceFormat(DisplayAdapter, devtype,
 
260
                        present.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
 
261
                        D3DRTYPE_SURFACE, present.AutoDepthStencilFormat)))
 
262
                {
 
263
#if !defined( _IRR_XBOX_PLATFORM_)
 
264
                        present.AutoDepthStencilFormat = D3DFMT_D24X4S4;
 
265
                        if(FAILED(pID3D->CheckDeviceFormat(DisplayAdapter, devtype,
 
266
                                present.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
 
267
                                D3DRTYPE_SURFACE, present.AutoDepthStencilFormat)))
 
268
                        {
 
269
                                present.AutoDepthStencilFormat = D3DFMT_D15S1;
 
270
                                if(FAILED(pID3D->CheckDeviceFormat(DisplayAdapter, devtype,
 
271
                                        present.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
 
272
                                        D3DRTYPE_SURFACE, present.AutoDepthStencilFormat)))
 
273
                                {
 
274
                                        os::Printer::log("Device does not support stencilbuffer, disabling stencil buffer.", ELL_WARNING);
 
275
                                        StencilBuffer = false;
 
276
                                }
 
277
                        }
 
278
#endif
 
279
                }
 
280
                else
 
281
                if(FAILED(pID3D->CheckDepthStencilMatch(DisplayAdapter, devtype,
 
282
                        present.BackBufferFormat, present.BackBufferFormat, present.AutoDepthStencilFormat)))
 
283
                {
 
284
                        os::Printer::log("Depth-stencil format is not compatible with display format, disabling stencil buffer.", ELL_WARNING);
 
285
                        StencilBuffer = false;
 
286
                }
 
287
        }
 
288
        // do not use else here to cope with flag change in previous block
 
289
        if (!StencilBuffer)
 
290
        {
 
291
#if !defined( _IRR_XBOX_PLATFORM_)
 
292
                present.AutoDepthStencilFormat = D3DFMT_D32;
 
293
                if(FAILED(pID3D->CheckDeviceFormat(DisplayAdapter, devtype,
 
294
                        present.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
 
295
                        D3DRTYPE_SURFACE, present.AutoDepthStencilFormat)))
 
296
                {
 
297
                        present.AutoDepthStencilFormat = D3DFMT_D24X8;
 
298
                        if(FAILED(pID3D->CheckDeviceFormat(DisplayAdapter, devtype,
 
299
                                present.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
 
300
                                D3DRTYPE_SURFACE, present.AutoDepthStencilFormat)))
 
301
                        {
 
302
                                present.AutoDepthStencilFormat = D3DFMT_D16;
 
303
                                if(FAILED(pID3D->CheckDeviceFormat(DisplayAdapter, devtype,
 
304
                                        present.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
 
305
                                        D3DRTYPE_SURFACE, present.AutoDepthStencilFormat)))
 
306
                                {
 
307
                                        os::Printer::log("Device does not support required depth buffer.", ELL_WARNING);
 
308
                                        return false;
 
309
                                }
 
310
                        }
 
311
                }
 
312
#else
 
313
                present.AutoDepthStencilFormat = D3DFMT_D16;
 
314
                if(FAILED(pID3D->CheckDeviceFormat(DisplayAdapter, devtype,
 
315
                        present.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
 
316
                        D3DRTYPE_SURFACE, present.AutoDepthStencilFormat)))
 
317
                {
 
318
                        os::Printer::log("Device does not support required depth buffer.", ELL_WARNING);
 
319
                        return false;
 
320
                }
 
321
#endif
 
322
        }
 
323
 
 
324
        // create device
 
325
#if defined( _IRR_XBOX_PLATFORM_)
 
326
        DWORD fpuPrecision = 0;
 
327
#else
 
328
        DWORD fpuPrecision = highPrecisionFPU ? D3DCREATE_FPU_PRESERVE : 0;
 
329
#endif
 
330
        if (pureSoftware)
 
331
        {
 
332
                hr = pID3D->CreateDevice(DisplayAdapter, D3DDEVTYPE_REF, hwnd,
 
333
                                fpuPrecision | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present, &pID3DDevice);
 
334
 
 
335
                if (FAILED(hr))
 
336
                        os::Printer::log("Was not able to create Direct3D8 software device.", ELL_ERROR);
 
337
        }
 
338
        else
 
339
        {
 
340
                hr = pID3D->CreateDevice(DisplayAdapter, devtype, hwnd,
 
341
                                fpuPrecision | D3DCREATE_HARDWARE_VERTEXPROCESSING, &present, &pID3DDevice);
 
342
 
 
343
                if(FAILED(hr))
 
344
                        hr = pID3D->CreateDevice(DisplayAdapter, devtype, hwnd,
 
345
                                        fpuPrecision | D3DCREATE_MIXED_VERTEXPROCESSING , &present, &pID3DDevice);
 
346
                if(FAILED(hr))
 
347
                        hr = pID3D->CreateDevice(DisplayAdapter, devtype, hwnd,
 
348
                                        fpuPrecision | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present, &pID3DDevice);
 
349
                if (FAILED(hr))
 
350
                        os::Printer::log("Was not able to create Direct3D8 device.", ELL_ERROR);
 
351
        }
 
352
 
 
353
        if (!pID3DDevice)
 
354
        {
 
355
                os::Printer::log("Was not able to create Direct3D8 device.", ELL_ERROR);
 
356
                return false;
 
357
        }
 
358
 
 
359
        // get caps
 
360
        pID3DDevice->GetDeviceCaps(&Caps);
 
361
 
 
362
        if (StencilBuffer &&
 
363
                (!(Caps.StencilCaps & D3DSTENCILCAPS_DECRSAT) ||
 
364
                !(Caps.StencilCaps & D3DSTENCILCAPS_INCRSAT) ||
 
365
                !(Caps.StencilCaps & D3DSTENCILCAPS_KEEP)))
 
366
        {
 
367
                os::Printer::log("Device not able to use stencil buffer, disabling stencil buffer.", ELL_WARNING);
 
368
                StencilBuffer = false;
 
369
        }
 
370
 
 
371
        // set default vertex shader
 
372
        setVertexShader(EVT_STANDARD);
 
373
 
 
374
        // enable antialiasing
 
375
        if (antiAlias>0)
 
376
                pID3DDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
 
377
 
 
378
        // set fog mode
 
379
        setFog(FogColor, FogType, FogStart, FogEnd, FogDensity, PixelFog, RangeFog);
 
380
 
 
381
        // set exposed data
 
382
        ExposedData.D3D8.D3D8 = pID3D;
 
383
        ExposedData.D3D8.D3DDev8 = pID3DDevice;
 
384
        ExposedData.D3D8.HWnd = hwnd;
 
385
 
 
386
        ResetRenderStates = true;
 
387
 
 
388
        // create materials
 
389
        createMaterialRenderers();
 
390
 
 
391
        MaxTextureUnits = core::min_((u32)Caps.MaxSimultaneousTextures, MATERIAL_MAX_TEXTURES);
 
392
        MaxUserClipPlanes = (u32)Caps.MaxUserClipPlanes;
 
393
 
 
394
        DriverAttributes->setAttribute("MaxTextures", (s32)MaxTextureUnits);
 
395
        DriverAttributes->setAttribute("MaxSupportedTextures", (s32)Caps.MaxSimultaneousTextures);
 
396
        DriverAttributes->setAttribute("MaxLights", (s32)Caps.MaxActiveLights);
 
397
        DriverAttributes->setAttribute("MaxAnisotropy", (s32)Caps.MaxAnisotropy);
 
398
        DriverAttributes->setAttribute("MaxUserClipPlanes", (s32)Caps.MaxUserClipPlanes);
 
399
        DriverAttributes->setAttribute("MaxIndices", (s32)Caps.MaxVertexIndex);
 
400
        DriverAttributes->setAttribute("MaxTextureSize", (s32)core::min_(Caps.MaxTextureHeight,Caps.MaxTextureWidth));
 
401
        DriverAttributes->setAttribute("MaxTextureLODBias", 16.f);
 
402
        DriverAttributes->setAttribute("Version", 800);
 
403
        DriverAttributes->setAttribute("ShaderLanguageVersion", (s32)Caps.VertexShaderVersion*100);
 
404
        DriverAttributes->setAttribute("AntiAlias", antiAlias);
 
405
 
 
406
        // set the renderstates
 
407
        setRenderStates3DMode();
 
408
 
 
409
        // so far so good.
 
410
        return true;
 
411
}
 
412
 
 
413
 
 
414
//! applications must call this method before performing any rendering. returns false if failed.
 
415
bool CD3D8Driver::beginScene(bool backBuffer, bool zBuffer, SColor color,
 
416
                const SExposedVideoData& videoData, core::rect<s32>* sourceRect)
 
417
{
 
418
        CNullDriver::beginScene(backBuffer, zBuffer, color, videoData, sourceRect);
 
419
        WindowId = (HWND)videoData.D3D8.HWnd;
 
420
        SceneSourceRect = sourceRect;
 
421
 
 
422
        if (!pID3DDevice)
 
423
                return false;
 
424
 
 
425
        HRESULT hr;
 
426
        if (DeviceLost)
 
427
        {
 
428
#ifndef _IRR_XBOX_PLATFORM_
 
429
                if(FAILED(hr = pID3DDevice->TestCooperativeLevel()))
 
430
                {
 
431
                        if (hr == D3DERR_DEVICELOST)
 
432
                        {
 
433
                                Sleep(100);
 
434
                                hr = pID3DDevice->TestCooperativeLevel();
 
435
                                if (hr == D3DERR_DEVICELOST)
 
436
                                        return false;
 
437
                        }
 
438
 
 
439
                        if ((hr == D3DERR_DEVICENOTRESET) && !reset())
 
440
                                return false;
 
441
                }
 
442
#endif
 
443
        }
 
444
 
 
445
        DWORD flags = 0;
 
446
 
 
447
        if (backBuffer)
 
448
                flags |= D3DCLEAR_TARGET;
 
449
 
 
450
        if (zBuffer)
 
451
                flags |= D3DCLEAR_ZBUFFER;
 
452
 
 
453
        if (StencilBuffer)
 
454
                flags |= D3DCLEAR_STENCIL;
 
455
 
 
456
        if (flags)
 
457
        {
 
458
                hr = pID3DDevice->Clear( 0, NULL, flags, color.color, 1.0, 0);
 
459
                if (FAILED(hr))
 
460
                        os::Printer::log("Direct3D8 clear failed.", ELL_WARNING);
 
461
        }
 
462
 
 
463
        hr = pID3DDevice->BeginScene();
 
464
        if (FAILED(hr))
 
465
        {
 
466
                os::Printer::log("Direct3D8 begin scene failed.", ELL_WARNING);
 
467
                return false;
 
468
        }
 
469
 
 
470
        return true;
 
471
}
 
472
 
 
473
 
 
474
//! applications must call this method after performing any rendering. returns false if failed.
 
475
bool CD3D8Driver::endScene()
 
476
{
 
477
        CNullDriver::endScene();
 
478
        DriverWasReset=false;
 
479
 
 
480
        HRESULT hr = pID3DDevice->EndScene();
 
481
        if (FAILED(hr))
 
482
        {
 
483
                os::Printer::log("DIRECT3D8 end scene failed.", ELL_WARNING);
 
484
                return false;
 
485
        }
 
486
 
 
487
        RECT* srcRct = 0;
 
488
        RECT sourceRectData;
 
489
        if ( SceneSourceRect)
 
490
        {
 
491
                srcRct = &sourceRectData;
 
492
                sourceRectData.left = SceneSourceRect->UpperLeftCorner.X;
 
493
                sourceRectData.top = SceneSourceRect->UpperLeftCorner.Y;
 
494
                sourceRectData.right = SceneSourceRect->LowerRightCorner.X;
 
495
                sourceRectData.bottom = SceneSourceRect->LowerRightCorner.Y;
 
496
        }
 
497
 
 
498
        hr = pID3DDevice->Present(srcRct, NULL, WindowId, NULL);
 
499
 
 
500
        if (SUCCEEDED(hr))
 
501
                return true;
 
502
 
 
503
        if (hr == D3DERR_DEVICELOST)
 
504
        {
 
505
                DeviceLost = true;
 
506
                os::Printer::log("DIRECT3D8 device lost.", ELL_WARNING);
 
507
        }
 
508
        else
 
509
                os::Printer::log("DIRECT3D8 present failed.", ELL_WARNING);
 
510
        return false;
 
511
}
 
512
 
 
513
 
 
514
//! resets the device
 
515
bool CD3D8Driver::reset()
 
516
{
 
517
        u32 i;
 
518
        os::Printer::log("Resetting D3D8 device.", ELL_INFORMATION);
 
519
 
 
520
        for (i=0; i<Textures.size(); ++i)
 
521
        {
 
522
                if (Textures[i].Surface->isRenderTarget())
 
523
                {
 
524
                        IDirect3DTexture8* tex = ((CD3D8Texture*)(Textures[i].Surface))->getDX8Texture();
 
525
                        if (tex)
 
526
                                tex->Release();
 
527
                }
 
528
        }
 
529
        DriverWasReset=true;
 
530
 
 
531
        HRESULT hr = pID3DDevice->Reset(&present);
 
532
 
 
533
        for (i=0; i<Textures.size(); ++i)
 
534
        {
 
535
                if (Textures[i].Surface->isRenderTarget())
 
536
                        ((CD3D8Texture*)(Textures[i].Surface))->createRenderTarget();
 
537
        }
 
538
 
 
539
        if (FAILED(hr))
 
540
        {
 
541
                if (hr == D3DERR_DEVICELOST)
 
542
                {
 
543
                        DeviceLost = true;
 
544
                        os::Printer::log("Resetting failed due to device lost.", ELL_WARNING);
 
545
                }
 
546
                else
 
547
                        os::Printer::log("Resetting failed.", ELL_WARNING);
 
548
                return false;
 
549
        }
 
550
 
 
551
        DeviceLost = false;
 
552
        ResetRenderStates = true;
 
553
        LastVertexType = (E_VERTEX_TYPE)-1;
 
554
 
 
555
        for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
 
556
                CurrentTexture[i] = 0;
 
557
 
 
558
        setVertexShader(EVT_STANDARD);
 
559
        setRenderStates3DMode();
 
560
        setFog(FogColor, FogType, FogStart, FogEnd, FogDensity, PixelFog, RangeFog);
 
561
        setAmbientLight(AmbientLight);
 
562
 
 
563
        return true;
 
564
}
 
565
 
 
566
 
 
567
//! queries the features of the driver, returns true if feature is available
 
568
bool CD3D8Driver::queryFeature(E_VIDEO_DRIVER_FEATURE feature) const
 
569
{
 
570
        if (!FeatureEnabled[feature])
 
571
                return false;
 
572
 
 
573
        switch (feature)
 
574
        {
 
575
        case EVDF_RENDER_TO_TARGET:
 
576
        case EVDF_MULTITEXTURE:
 
577
        case EVDF_BILINEAR_FILTER:
 
578
                return true;
 
579
        case EVDF_HARDWARE_TL:
 
580
                return (Caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) != 0;
 
581
        case EVDF_MIP_MAP:
 
582
                return (Caps.TextureCaps & D3DPTEXTURECAPS_MIPMAP) != 0;
 
583
        case EVDF_STENCIL_BUFFER:
 
584
                return StencilBuffer && Caps.StencilCaps;
 
585
        case EVDF_VERTEX_SHADER_1_1:
 
586
                return Caps.VertexShaderVersion >= D3DVS_VERSION(1,1);
 
587
        case EVDF_VERTEX_SHADER_2_0:
 
588
                return Caps.VertexShaderVersion >= D3DVS_VERSION(2,0);
 
589
        case EVDF_VERTEX_SHADER_3_0:
 
590
                return Caps.VertexShaderVersion >= D3DVS_VERSION(3,0);
 
591
        case EVDF_PIXEL_SHADER_1_1:
 
592
                return Caps.PixelShaderVersion >= D3DPS_VERSION(1,1);
 
593
        case EVDF_PIXEL_SHADER_1_2:
 
594
                return Caps.PixelShaderVersion >= D3DPS_VERSION(1,2);
 
595
        case EVDF_PIXEL_SHADER_1_3:
 
596
                return Caps.PixelShaderVersion >= D3DPS_VERSION(1,3);
 
597
        case EVDF_PIXEL_SHADER_1_4:
 
598
                return Caps.PixelShaderVersion >= D3DPS_VERSION(1,4);
 
599
        case EVDF_PIXEL_SHADER_2_0:
 
600
                return Caps.PixelShaderVersion >= D3DPS_VERSION(2,0);
 
601
        case EVDF_PIXEL_SHADER_3_0:
 
602
                return Caps.PixelShaderVersion >= D3DPS_VERSION(3,0);
 
603
        case EVDF_TEXTURE_NSQUARE:
 
604
                return (Caps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY) == 0;
 
605
        case EVDF_TEXTURE_NPOT:
 
606
                return (Caps.TextureCaps & D3DPTEXTURECAPS_POW2) == 0;
 
607
        case EVDF_COLOR_MASK:
 
608
                return (Caps.PrimitiveMiscCaps & D3DPMISCCAPS_COLORWRITEENABLE) != 0;
 
609
        case EVDF_BLEND_OPERATIONS:
 
610
        case EVDF_TEXTURE_MATRIX:
 
611
                return true;
 
612
        default:
 
613
                return false;
 
614
        };
 
615
}
 
616
 
 
617
 
 
618
//! sets transformation
 
619
void CD3D8Driver::setTransform(E_TRANSFORMATION_STATE state,
 
620
                const core::matrix4& mat)
 
621
{
 
622
        switch(state)
 
623
        {
 
624
        case ETS_VIEW:
 
625
                pID3DDevice->SetTransform(D3DTS_VIEW, (D3DMATRIX*)((void*)mat.pointer()));
 
626
                Transformation3DChanged = true;
 
627
                break;
 
628
        case ETS_WORLD:
 
629
                pID3DDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)((void*)mat.pointer()));
 
630
                Transformation3DChanged = true;
 
631
                break;
 
632
        case ETS_PROJECTION:
 
633
                pID3DDevice->SetTransform( D3DTS_PROJECTION, (D3DMATRIX*)((void*)mat.pointer()));
 
634
                Transformation3DChanged = true;
 
635
                break;
 
636
        case ETS_COUNT:
 
637
                return;
 
638
        default:
 
639
                if (state-ETS_TEXTURE_0 < MATERIAL_MAX_TEXTURES)
 
640
                {
 
641
                        pID3DDevice->SetTextureStageState( state - ETS_TEXTURE_0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
 
642
                        pID3DDevice->SetTransform((D3DTRANSFORMSTATETYPE)(D3DTS_TEXTURE0+ ( state - ETS_TEXTURE_0 )),
 
643
                                (D3DMATRIX*)((void*)mat.pointer()));
 
644
                }
 
645
                break;
 
646
        }
 
647
 
 
648
        Matrices[state] = mat;
 
649
}
 
650
 
 
651
 
 
652
//! sets the current Texture
 
653
bool CD3D8Driver::setActiveTexture(u32 stage, const video::ITexture* texture)
 
654
{
 
655
        if (CurrentTexture[stage] == texture)
 
656
                return true;
 
657
 
 
658
        if (texture && (texture->getDriverType() != EDT_DIRECT3D8))
 
659
        {
 
660
                os::Printer::log("Fatal Error: Tried to set a texture not owned by this driver.", ELL_ERROR);
 
661
                return false;
 
662
        }
 
663
 
 
664
        CurrentTexture[stage] = texture;
 
665
 
 
666
        if (!texture)
 
667
        {
 
668
                pID3DDevice->SetTexture(stage, 0);
 
669
                pID3DDevice->SetTextureStageState( stage, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
 
670
        }
 
671
        else
 
672
        {
 
673
                pID3DDevice->SetTexture(stage, ((const CD3D8Texture*)texture)->getDX8Texture());
 
674
        }
 
675
        return true;
 
676
}
 
677
 
 
678
 
 
679
//! sets a material
 
680
void CD3D8Driver::setMaterial(const SMaterial& material)
 
681
{
 
682
        Material = material;
 
683
        OverrideMaterial.apply(Material);
 
684
 
 
685
        for (u32 i=0; i<MaxTextureUnits; ++i)
 
686
        {
 
687
                setActiveTexture(i, Material.getTexture(i));
 
688
                setTransform((E_TRANSFORMATION_STATE) ( ETS_TEXTURE_0 + i ),
 
689
                                material.getTextureMatrix(i));
 
690
        }
 
691
}
 
692
 
 
693
 
 
694
//! returns a device dependent texture from a software surface (IImage)
 
695
video::ITexture* CD3D8Driver::createDeviceDependentTexture(IImage* surface,const io::path& name, void* mipmapData)
 
696
{
 
697
        return new CD3D8Texture(surface, this, TextureCreationFlags, name, mipmapData);
 
698
}
 
699
 
 
700
 
 
701
//! Enables or disables a texture creation flag.
 
702
void CD3D8Driver::setTextureCreationFlag(E_TEXTURE_CREATION_FLAG flag,
 
703
                bool enabled)
 
704
{
 
705
        if (flag == video::ETCF_CREATE_MIP_MAPS && !queryFeature(EVDF_MIP_MAP))
 
706
                enabled = false;
 
707
 
 
708
        CNullDriver::setTextureCreationFlag(flag, enabled);
 
709
}
 
710
 
 
711
 
 
712
//! sets a render target
 
713
bool CD3D8Driver::setRenderTarget(video::ITexture* texture,
 
714
                bool clearBackBuffer, bool clearZBuffer, SColor color)
 
715
{
 
716
        // check for right driver type
 
717
 
 
718
        if (texture && texture->getDriverType() != EDT_DIRECT3D8)
 
719
        {
 
720
                os::Printer::log("Fatal Error: Tried to set a texture not owned by this driver.", ELL_ERROR);
 
721
                return false;
 
722
        }
 
723
 
 
724
        // check for valid render target
 
725
 
 
726
        CD3D8Texture* tex = (CD3D8Texture*)texture;
 
727
 
 
728
        if (texture && !tex->isRenderTarget())
 
729
        {
 
730
                os::Printer::log("Fatal Error: Tried to set a non render target texture as render target.", ELL_ERROR);
 
731
                return false;
 
732
        }
 
733
 
 
734
        if (texture && (tex->getSize().Width > ScreenSize.Width ||
 
735
                tex->getSize().Height > ScreenSize.Height ))
 
736
        {
 
737
                os::Printer::log("Error: Tried to set a render target texture which is bigger than the screen.", ELL_ERROR);
 
738
                return false;
 
739
        }
 
740
 
 
741
        // check if we should set the previous RT back
 
742
 
 
743
        bool ret = true;
 
744
 
 
745
        if (tex == 0)
 
746
        {
 
747
                if (PrevRenderTarget)
 
748
                {
 
749
                        IDirect3DSurface8* dss = 0;
 
750
                        pID3DDevice->GetDepthStencilSurface(&dss);
 
751
 
 
752
                        if (FAILED(pID3DDevice->SetRenderTarget(PrevRenderTarget, dss)))
 
753
                        {
 
754
                                os::Printer::log("Error: Could not set back to previous render target.", ELL_ERROR);
 
755
                                ret = false;
 
756
                        }
 
757
 
 
758
                        if (dss)
 
759
                                dss->Release();
 
760
 
 
761
                        CurrentRendertargetSize = core::dimension2d<u32>(0,0);
 
762
                        PrevRenderTarget->Release();
 
763
                        PrevRenderTarget = 0;
 
764
                }
 
765
        }
 
766
        else
 
767
        {
 
768
                // we want to set a new target. so do this.
 
769
 
 
770
                // store previous target
 
771
 
 
772
                if (!PrevRenderTarget && (FAILED(pID3DDevice->GetRenderTarget(&PrevRenderTarget))))
 
773
                {
 
774
                        os::Printer::log("Could not get previous render target.", ELL_ERROR);
 
775
                        return false;
 
776
                }
 
777
 
 
778
                // set new render target
 
779
 
 
780
                IDirect3DSurface8* dss = 0;
 
781
                pID3DDevice->GetDepthStencilSurface(&dss);
 
782
 
 
783
                if (FAILED(pID3DDevice->SetRenderTarget(tex->getRenderTargetSurface(), dss)))
 
784
                {
 
785
                        os::Printer::log("Error: Could not set render target.", ELL_ERROR);
 
786
                        ret = false;
 
787
                }
 
788
 
 
789
                if (dss)
 
790
                        dss->Release();
 
791
 
 
792
                CurrentRendertargetSize = tex->getSize();
 
793
        }
 
794
        Transformation3DChanged = true; 
 
795
 
 
796
        if (clearBackBuffer || clearZBuffer)
 
797
        {
 
798
                DWORD flags = 0;
 
799
 
 
800
                if (clearBackBuffer)
 
801
                        flags |= D3DCLEAR_TARGET;
 
802
 
 
803
                if (clearZBuffer)
 
804
                        flags |= D3DCLEAR_ZBUFFER;
 
805
 
 
806
                pID3DDevice->Clear(0, NULL, flags, color.color, 1.0f, 0);
 
807
        }
 
808
 
 
809
        return ret;
 
810
}
 
811
 
 
812
 
 
813
//! Creates a render target texture.
 
814
ITexture* CD3D8Driver::addRenderTargetTexture(
 
815
                const core::dimension2d<u32>& size, const io::path& name,
 
816
                const ECOLOR_FORMAT format)
 
817
{
 
818
        CD3D8Texture* tex = new CD3D8Texture(this, size, name);
 
819
        if (tex)
 
820
        {
 
821
                if (!tex->Texture)
 
822
                {
 
823
                        tex->drop();
 
824
                        return 0;
 
825
                }
 
826
                addTexture(tex);
 
827
                tex->drop();
 
828
        }
 
829
        return tex;
 
830
}
 
831
 
 
832
 
 
833
//! sets a viewport
 
834
void CD3D8Driver::setViewPort(const core::rect<s32>& area)
 
835
{
 
836
        core::rect<s32> vp(area);
 
837
        core::rect<s32> rendert(0,0, ScreenSize.Width, ScreenSize.Height);
 
838
        vp.clipAgainst(rendert);
 
839
 
 
840
        D3DVIEWPORT8 viewPort;
 
841
        viewPort.X = vp.UpperLeftCorner.X;
 
842
        viewPort.Y = vp.UpperLeftCorner.Y;
 
843
        viewPort.Width = vp.getWidth();
 
844
        viewPort.Height = vp.getHeight();
 
845
        viewPort.MinZ = 0.0f;
 
846
        viewPort.MaxZ = 1.0f;
 
847
 
 
848
        HRESULT hr = D3DERR_INVALIDCALL;
 
849
        if (vp.getHeight()>0 && vp.getWidth()>0)
 
850
                hr = pID3DDevice->SetViewport(&viewPort);
 
851
 
 
852
        if (FAILED(hr))
 
853
                os::Printer::log("Failed setting the viewport.", ELL_WARNING);
 
854
 
 
855
        ViewPort = vp;
 
856
}
 
857
 
 
858
 
 
859
//! gets the area of the current viewport
 
860
const core::rect<s32>& CD3D8Driver::getViewPort() const
 
861
{
 
862
        return ViewPort;
 
863
}
 
864
 
 
865
 
 
866
//! draws a vertex primitive list
 
867
void CD3D8Driver::drawVertexPrimitiveList(const void* vertices,
 
868
                u32 vertexCount, const void* indexList, u32 primitiveCount,
 
869
                E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType,
 
870
                E_INDEX_TYPE iType)
 
871
{
 
872
        if (!checkPrimitiveCount(primitiveCount))
 
873
                return;
 
874
 
 
875
        CNullDriver::drawVertexPrimitiveList(vertices, vertexCount, indexList, primitiveCount, vType, pType,iType);
 
876
 
 
877
        if (!vertexCount || !primitiveCount)
 
878
                return;
 
879
 
 
880
        draw2D3DVertexPrimitiveList(vertices, vertexCount, indexList, primitiveCount,
 
881
                vType, pType, iType, true);
 
882
}
 
883
 
 
884
 
 
885
//! draws a vertex primitive list in 2d
 
886
void CD3D8Driver::draw2DVertexPrimitiveList(const void* vertices,
 
887
                u32 vertexCount, const void* indexList, u32 primitiveCount,
 
888
                E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType,
 
889
                E_INDEX_TYPE iType)
 
890
{
 
891
        if (!checkPrimitiveCount(primitiveCount))
 
892
                return;
 
893
 
 
894
        CNullDriver::draw2DVertexPrimitiveList(vertices, vertexCount, indexList, primitiveCount, vType, pType,iType);
 
895
 
 
896
        if (!vertexCount || !primitiveCount)
 
897
                return;
 
898
 
 
899
        draw2D3DVertexPrimitiveList(vertices, vertexCount, indexList, primitiveCount,
 
900
                vType, pType, iType, false);
 
901
}
 
902
 
 
903
 
 
904
void CD3D8Driver::draw2D3DVertexPrimitiveList(const void* vertices,
 
905
                u32 vertexCount, const void* indexList, u32 primitiveCount,
 
906
                E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType,
 
907
                E_INDEX_TYPE iType, bool is3D)
 
908
{
 
909
        setVertexShader(vType);
 
910
 
 
911
        const u32 stride = getVertexPitchFromType(vType);
 
912
 
 
913
        D3DFORMAT indexType=D3DFMT_UNKNOWN;
 
914
        switch (iType)
 
915
        {
 
916
                case (EIT_16BIT):
 
917
                {
 
918
                        indexType=D3DFMT_INDEX16;
 
919
                        break;
 
920
                }
 
921
                case (EIT_32BIT):
 
922
                {
 
923
                        indexType=D3DFMT_INDEX32;
 
924
                        break;
 
925
                }
 
926
        }
 
927
 
 
928
        if (is3D)
 
929
        {
 
930
                if (!setRenderStates3DMode())
 
931
                        return;
 
932
        }
 
933
        else
 
934
        {
 
935
                if (Material.MaterialType==EMT_ONETEXTURE_BLEND)
 
936
                {
 
937
                        E_BLEND_FACTOR srcFact;
 
938
                        E_BLEND_FACTOR dstFact;
 
939
                        E_MODULATE_FUNC modulo;
 
940
                        u32 alphaSource;
 
941
                        unpack_texureBlendFunc ( srcFact, dstFact, modulo, alphaSource, Material.MaterialTypeParam);
 
942
                        setRenderStates2DMode(alphaSource&video::EAS_VERTEX_COLOR, (Material.getTexture(0) != 0), (alphaSource&video::EAS_TEXTURE) != 0);
 
943
                }
 
944
                else
 
945
                        setRenderStates2DMode(Material.MaterialType==EMT_TRANSPARENT_VERTEX_ALPHA, (Material.getTexture(0) != 0), Material.MaterialType==EMT_TRANSPARENT_ALPHA_CHANNEL);
 
946
        }
 
947
 
 
948
        switch (pType)
 
949
        {
 
950
                case scene::EPT_POINT_SPRITES:
 
951
                case scene::EPT_POINTS:
 
952
                {
 
953
                        f32 tmp=Material.Thickness/getScreenSize().Height;
 
954
                        if (pType==scene::EPT_POINT_SPRITES)
 
955
                                pID3DDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
 
956
                        pID3DDevice->SetRenderState(D3DRS_POINTSCALEENABLE, TRUE);
 
957
                        pID3DDevice->SetRenderState(D3DRS_POINTSIZE, *(DWORD*)(&tmp));
 
958
                        tmp=1.0f;
 
959
                        pID3DDevice->SetRenderState(D3DRS_POINTSCALE_A, *(DWORD*)(&tmp));
 
960
                        pID3DDevice->SetRenderState(D3DRS_POINTSCALE_B, *(DWORD*)(&tmp));
 
961
                        pID3DDevice->SetRenderState(D3DRS_POINTSIZE_MIN, *(DWORD*)(&tmp));
 
962
                        tmp=0.0f;
 
963
                        pID3DDevice->SetRenderState(D3DRS_POINTSCALE_C, *(DWORD*)(&tmp));
 
964
                        pID3DDevice->DrawIndexedPrimitiveUP(D3DPT_POINTLIST, 0, vertexCount,
 
965
                                primitiveCount, indexList, indexType, vertices, stride);
 
966
                        pID3DDevice->SetRenderState(D3DRS_POINTSCALEENABLE, FALSE);
 
967
                        if (pType==scene::EPT_POINT_SPRITES)
 
968
                                pID3DDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, FALSE);
 
969
                }
 
970
                break;
 
971
                case scene::EPT_LINE_STRIP:
 
972
                        pID3DDevice->DrawIndexedPrimitiveUP(D3DPT_LINESTRIP, 0, vertexCount,
 
973
                                primitiveCount, indexList, indexType, vertices, stride);
 
974
                break;
 
975
                case scene::EPT_LINE_LOOP:
 
976
                {
 
977
                        pID3DDevice->DrawIndexedPrimitiveUP(D3DPT_LINESTRIP, 0, vertexCount,
 
978
                                primitiveCount - 1, indexList, indexType, vertices, stride);
 
979
                        u16 tmpIndices[] = {primitiveCount - 1, 0};
 
980
                        pID3DDevice->DrawIndexedPrimitiveUP(D3DPT_LINELIST, 0, vertexCount,
 
981
                                1, tmpIndices, indexType, vertices, stride);
 
982
                }
 
983
                break;
 
984
                case scene::EPT_LINES:
 
985
                        pID3DDevice->DrawIndexedPrimitiveUP(D3DPT_LINELIST, 0, vertexCount,
 
986
                                primitiveCount, indexList, indexType, vertices, stride);
 
987
                break;
 
988
                case scene::EPT_TRIANGLE_STRIP:
 
989
                        pID3DDevice->DrawIndexedPrimitiveUP(D3DPT_TRIANGLESTRIP, 0, vertexCount,
 
990
                                primitiveCount, indexList, indexType, vertices, stride);
 
991
                break;
 
992
                case scene::EPT_TRIANGLE_FAN:
 
993
                        pID3DDevice->DrawIndexedPrimitiveUP(D3DPT_TRIANGLEFAN, 0, vertexCount,
 
994
                                primitiveCount, indexList, indexType, vertices, stride);
 
995
                break;
 
996
                case scene::EPT_TRIANGLES:
 
997
                        pID3DDevice->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, vertexCount,
 
998
                                primitiveCount, indexList, indexType, vertices, stride);
 
999
                break;
 
1000
        }
 
1001
}
 
1002
 
 
1003
 
 
1004
//! draws an 2d image, using a color (if color is other then Color(255,255,255,255)) and the alpha channel of the texture if wanted.
 
1005
void CD3D8Driver::draw2DImage(const video::ITexture* texture,
 
1006
                const core::position2d<s32>& pos,
 
1007
                const core::rect<s32>& sourceRect,
 
1008
                const core::rect<s32>* clipRect, SColor color,
 
1009
                bool useAlphaChannelOfTexture)
 
1010
{
 
1011
        if (!texture)
 
1012
                return;
 
1013
 
 
1014
        if (!sourceRect.isValid())
 
1015
                return;
 
1016
 
 
1017
        if (!setActiveTexture(0, texture))
 
1018
                return;
 
1019
 
 
1020
        core::position2d<s32> targetPos = pos;
 
1021
        core::position2d<s32> sourcePos = sourceRect.UpperLeftCorner;
 
1022
        // This needs to be signed as it may go negative.
 
1023
        core::dimension2d<s32> sourceSize(sourceRect.getSize());
 
1024
        const core::dimension2d<u32>& renderTargetSize = getCurrentRenderTargetSize();
 
1025
 
 
1026
        if (clipRect)
 
1027
        {
 
1028
                if (targetPos.X < clipRect->UpperLeftCorner.X)
 
1029
                {
 
1030
                        sourceSize.Width += targetPos.X - clipRect->UpperLeftCorner.X;
 
1031
                        if (sourceSize.Width <= 0)
 
1032
                                return;
 
1033
 
 
1034
                        sourcePos.X -= targetPos.X - clipRect->UpperLeftCorner.X;
 
1035
                        targetPos.X = clipRect->UpperLeftCorner.X;
 
1036
                }
 
1037
 
 
1038
                if (targetPos.X + (s32)sourceSize.Width > clipRect->LowerRightCorner.X)
 
1039
                {
 
1040
                        sourceSize.Width -= (targetPos.X + sourceSize.Width) - clipRect->LowerRightCorner.X;
 
1041
                        if (sourceSize.Width <= 0)
 
1042
                                return;
 
1043
                }
 
1044
 
 
1045
                if (targetPos.Y < clipRect->UpperLeftCorner.Y)
 
1046
                {
 
1047
                        sourceSize.Height += targetPos.Y - clipRect->UpperLeftCorner.Y;
 
1048
                        if (sourceSize.Height <= 0)
 
1049
                                return;
 
1050
 
 
1051
                        sourcePos.Y -= targetPos.Y - clipRect->UpperLeftCorner.Y;
 
1052
                        targetPos.Y = clipRect->UpperLeftCorner.Y;
 
1053
                }
 
1054
 
 
1055
                if (targetPos.Y + (s32)sourceSize.Height > clipRect->LowerRightCorner.Y)
 
1056
                {
 
1057
                        sourceSize.Height -= (targetPos.Y + sourceSize.Height) - clipRect->LowerRightCorner.Y;
 
1058
                        if (sourceSize.Height <= 0)
 
1059
                                return;
 
1060
                }
 
1061
        }
 
1062
 
 
1063
        // clip these coordinates
 
1064
 
 
1065
        if (targetPos.X<0)
 
1066
        {
 
1067
                sourceSize.Width += targetPos.X;
 
1068
                if (sourceSize.Width <= 0)
 
1069
                        return;
 
1070
 
 
1071
                sourcePos.X -= targetPos.X;
 
1072
                targetPos.X = 0;
 
1073
        }
 
1074
 
 
1075
        if (targetPos.X + sourceSize.Width > (s32)renderTargetSize.Width)
 
1076
        {
 
1077
                sourceSize.Width -= (targetPos.X + sourceSize.Width) - renderTargetSize.Width;
 
1078
                if (sourceSize.Width <= 0)
 
1079
                        return;
 
1080
        }
 
1081
 
 
1082
        if (targetPos.Y<0)
 
1083
        {
 
1084
                sourceSize.Height += targetPos.Y;
 
1085
                if (sourceSize.Height <= 0)
 
1086
                        return;
 
1087
 
 
1088
                sourcePos.Y -= targetPos.Y;
 
1089
                targetPos.Y = 0;
 
1090
        }
 
1091
 
 
1092
        if (targetPos.Y + sourceSize.Height > (s32)renderTargetSize.Height)
 
1093
        {
 
1094
                sourceSize.Height -= (targetPos.Y + sourceSize.Height) - renderTargetSize.Height;
 
1095
                if (sourceSize.Height <= 0)
 
1096
                        return;
 
1097
        }
 
1098
 
 
1099
        // ok, we've clipped everything.
 
1100
        // now draw it.
 
1101
 
 
1102
        core::rect<f32> tcoords;
 
1103
        tcoords.UpperLeftCorner.X = (f32)sourcePos.X / texture->getOriginalSize().Width ;
 
1104
        tcoords.UpperLeftCorner.Y = (f32)sourcePos.Y / texture->getOriginalSize().Height;
 
1105
        tcoords.LowerRightCorner.X = tcoords.UpperLeftCorner.X + (f32)sourceSize.Width / texture->getOriginalSize().Width;
 
1106
        tcoords.LowerRightCorner.Y = tcoords.UpperLeftCorner.Y + (f32)sourceSize.Height / texture->getOriginalSize().Height;
 
1107
 
 
1108
        const core::rect<s32> poss(targetPos, sourceSize);
 
1109
 
 
1110
        setRenderStates2DMode(color.getAlpha()<255, true, useAlphaChannelOfTexture);
 
1111
 
 
1112
        S3DVertex vtx[4];
 
1113
        vtx[0] = S3DVertex((f32)poss.UpperLeftCorner.X,
 
1114
                        (f32)poss.UpperLeftCorner.Y, 0.0f,
 
1115
                        0.0f, 0.0f, 0.0f, color,
 
1116
                        tcoords.UpperLeftCorner.X, tcoords.UpperLeftCorner.Y);
 
1117
        vtx[1] = S3DVertex((f32)poss.LowerRightCorner.X,
 
1118
                        (f32)poss.UpperLeftCorner.Y, 0.0f,
 
1119
                        0.0f, 0.0f, 0.0f, color,
 
1120
                        tcoords.LowerRightCorner.X, tcoords.UpperLeftCorner.Y);
 
1121
        vtx[2] = S3DVertex((f32)poss.LowerRightCorner.X,
 
1122
                        (f32)poss.LowerRightCorner.Y, 0.0f,
 
1123
                        0.0f, 0.0f, 0.0f, color,
 
1124
                        tcoords.LowerRightCorner.X, tcoords.LowerRightCorner.Y);
 
1125
        vtx[3] = S3DVertex((f32)poss.UpperLeftCorner.X,
 
1126
                        (f32)poss.LowerRightCorner.Y, 0.0f,
 
1127
                        0.0f, 0.0f, 0.0f, color,
 
1128
                        tcoords.UpperLeftCorner.X, tcoords.LowerRightCorner.Y);
 
1129
 
 
1130
        const s16 indices[6] = {0,1,2,0,2,3};
 
1131
 
 
1132
        setVertexShader(EVT_STANDARD);
 
1133
 
 
1134
        pID3DDevice->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, 4, 2, &indices[0],
 
1135
                        D3DFMT_INDEX16, &vtx[0], sizeof(S3DVertex));
 
1136
}
 
1137
 
 
1138
 
 
1139
void CD3D8Driver::draw2DImage(const video::ITexture* texture,
 
1140
                const core::rect<s32>& destRect,
 
1141
                const core::rect<s32>& sourceRect,
 
1142
                const core::rect<s32>* clipRect,
 
1143
                const video::SColor* const colors,
 
1144
                bool useAlphaChannelOfTexture)
 
1145
{
 
1146
        if(!texture)
 
1147
                return;
 
1148
 
 
1149
        const core::dimension2d<u32>& ss = texture->getOriginalSize();
 
1150
        core::rect<f32> tcoords;
 
1151
        tcoords.UpperLeftCorner.X = (f32)sourceRect.UpperLeftCorner.X / (f32)ss.Width;
 
1152
        tcoords.UpperLeftCorner.Y = (f32)sourceRect.UpperLeftCorner.Y / (f32)ss.Height;
 
1153
        tcoords.LowerRightCorner.X = (f32)sourceRect.LowerRightCorner.X / (f32)ss.Width;
 
1154
        tcoords.LowerRightCorner.Y = (f32)sourceRect.LowerRightCorner.Y / (f32)ss.Height;
 
1155
 
 
1156
        core::rect<s32> clippedRect(destRect);
 
1157
        if (clipRect)
 
1158
        {
 
1159
                clippedRect.clipAgainst(*clipRect);
 
1160
 
 
1161
                //tcoords must be clipped by the same factors
 
1162
                const f32 tcWidth = tcoords.getWidth();
 
1163
                const f32 tcHeight = tcoords.getHeight();
 
1164
 
 
1165
                const f32 invDestRectWidth = 1.f / (f32)(destRect.getWidth());
 
1166
                f32 scale = (f32)(clippedRect.UpperLeftCorner.X - destRect.UpperLeftCorner.X) * invDestRectWidth;
 
1167
                tcoords.UpperLeftCorner.X += scale * tcWidth;
 
1168
                scale = (f32)(destRect.LowerRightCorner.X - clippedRect.LowerRightCorner.X) * invDestRectWidth;
 
1169
                tcoords.LowerRightCorner.X -= scale * tcWidth;
 
1170
 
 
1171
                const f32 invDestRectHeight = 1.f / (f32)(destRect.getHeight());
 
1172
                scale = (f32)(clippedRect.UpperLeftCorner.Y - destRect.UpperLeftCorner.Y) * invDestRectHeight;
 
1173
                tcoords.UpperLeftCorner.Y += scale * tcHeight;
 
1174
                scale = (f32)(destRect.LowerRightCorner.Y - clippedRect.LowerRightCorner.Y) * invDestRectHeight;
 
1175
                tcoords.LowerRightCorner.Y -= scale * tcHeight;
 
1176
        }
 
1177
 
 
1178
        const video::SColor temp[4] =
 
1179
        {
 
1180
                0xFFFFFFFF,
 
1181
                0xFFFFFFFF,
 
1182
                0xFFFFFFFF,
 
1183
                0xFFFFFFFF
 
1184
        };
 
1185
 
 
1186
        const video::SColor* const useColor = colors ? colors : temp;
 
1187
 
 
1188
        S3DVertex vtx[4]; // clock wise
 
1189
        vtx[0] = S3DVertex((f32)clippedRect.UpperLeftCorner.X, (f32)clippedRect.UpperLeftCorner.Y, 0.0f,
 
1190
                        0.0f, 0.0f, 0.0f, useColor[0],
 
1191
                        tcoords.UpperLeftCorner.X, tcoords.UpperLeftCorner.Y);
 
1192
        vtx[1] = S3DVertex((f32)clippedRect.LowerRightCorner.X, (f32)clippedRect.UpperLeftCorner.Y, 0.0f,
 
1193
                        0.0f, 0.0f, 0.0f, useColor[3],
 
1194
                        tcoords.LowerRightCorner.X, tcoords.UpperLeftCorner.Y);
 
1195
        vtx[2] = S3DVertex((f32)clippedRect.LowerRightCorner.X, (f32)clippedRect.LowerRightCorner.Y, 0.0f,
 
1196
                        0.0f, 0.0f, 0.0f, useColor[2],
 
1197
                        tcoords.LowerRightCorner.X, tcoords.LowerRightCorner.Y);
 
1198
        vtx[3] = S3DVertex((f32)clippedRect.UpperLeftCorner.X, (f32)clippedRect.LowerRightCorner.Y, 0.0f,
 
1199
                        0.0f, 0.0f, 0.0f, useColor[1],
 
1200
                        tcoords.UpperLeftCorner.X, tcoords.LowerRightCorner.Y);
 
1201
 
 
1202
        const s16 indices[6] = {0,1,2,0,2,3};
 
1203
 
 
1204
        setRenderStates2DMode(useColor[0].getAlpha()<255 || useColor[1].getAlpha()<255 ||
 
1205
                        useColor[2].getAlpha()<255 || useColor[3].getAlpha()<255,
 
1206
                        true, useAlphaChannelOfTexture);
 
1207
 
 
1208
        setActiveTexture(0, texture);
 
1209
 
 
1210
        setVertexShader(EVT_STANDARD);
 
1211
 
 
1212
        pID3DDevice->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, 4, 2, &indices[0],
 
1213
                D3DFMT_INDEX16, &vtx[0], sizeof(S3DVertex));
 
1214
}
 
1215
 
 
1216
 
 
1217
//!Draws an 2d rectangle with a gradient.
 
1218
void CD3D8Driver::draw2DRectangle(const core::rect<s32>& position,
 
1219
                SColor colorLeftUp, SColor colorRightUp, SColor colorLeftDown,
 
1220
                SColor colorRightDown, const core::rect<s32>* clip)
 
1221
{
 
1222
        core::rect<s32> pos(position);
 
1223
 
 
1224
        if (clip)
 
1225
                pos.clipAgainst(*clip);
 
1226
 
 
1227
        if (!pos.isValid())
 
1228
                return;
 
1229
 
 
1230
        S3DVertex vtx[4];
 
1231
        vtx[0] = S3DVertex((f32)pos.UpperLeftCorner.X, (f32)pos.UpperLeftCorner.Y, 0.0f,
 
1232
                        0.0f, 0.0f, 0.0f, colorLeftUp, 0.0f, 0.0f);
 
1233
        vtx[1] = S3DVertex((f32)pos.LowerRightCorner.X, (f32)pos.UpperLeftCorner.Y, 0.0f,
 
1234
                        0.0f, 0.0f, 0.0f, colorRightUp, 0.0f, 1.0f);
 
1235
        vtx[2] = S3DVertex((f32)pos.LowerRightCorner.X, (f32)pos.LowerRightCorner.Y, 0.0f,
 
1236
                        0.0f, 0.0f, 0.0f, colorRightDown, 1.0f, 0.0f);
 
1237
        vtx[3] = S3DVertex((f32)pos.UpperLeftCorner.X, (f32)pos.LowerRightCorner.Y, 0.0f,
 
1238
                        0.0f, 0.0f, 0.0f, colorLeftDown, 1.0f, 1.0f);
 
1239
 
 
1240
        const s16 indices[6] = {0,1,2,0,2,3};
 
1241
 
 
1242
        setRenderStates2DMode(
 
1243
                colorLeftUp.getAlpha() < 255 ||
 
1244
                colorRightUp.getAlpha() < 255 ||
 
1245
                colorLeftDown.getAlpha() < 255 ||
 
1246
                colorRightDown.getAlpha() < 255, false, false);
 
1247
 
 
1248
        setActiveTexture(0,0);
 
1249
 
 
1250
        setVertexShader(EVT_STANDARD);
 
1251
 
 
1252
        pID3DDevice->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, 4, 2, &indices[0],
 
1253
                D3DFMT_INDEX16, &vtx[0], sizeof(S3DVertex));
 
1254
}
 
1255
 
 
1256
 
 
1257
//! Draws a 2d line.
 
1258
void CD3D8Driver::draw2DLine(const core::position2d<s32>& start,
 
1259
                                        const core::position2d<s32>& end,
 
1260
                                        SColor color)
 
1261
{
 
1262
        // thanks to Vash TheStampede who sent in his implementation
 
1263
        S3DVertex vtx[2];
 
1264
        vtx[0] = S3DVertex((f32)start.X, (f32)start.Y, 0.0f,
 
1265
                                        0.0f, 0.0f, 0.0f, // normal
 
1266
                                        color, 0.0f, 0.0f); // texture
 
1267
 
 
1268
        vtx[1] = S3DVertex((f32)end.X, (f32)end.Y, 0.0f,
 
1269
                                        0.0f, 0.0f, 0.0f,
 
1270
                                        color, 0.0f, 0.0f);
 
1271
 
 
1272
        setRenderStates2DMode(color.getAlpha() < 255, false, false);
 
1273
        setActiveTexture(0,0);
 
1274
 
 
1275
        setVertexShader(EVT_STANDARD);
 
1276
 
 
1277
        pID3DDevice->DrawPrimitiveUP(D3DPT_LINELIST, 1, &vtx[0], sizeof(S3DVertex));
 
1278
}
 
1279
 
 
1280
 
 
1281
//! Draws a pixel
 
1282
void CD3D8Driver::drawPixel(u32 x, u32 y, const SColor & color)
 
1283
{
 
1284
        const core::dimension2d<u32>& renderTargetSize = getCurrentRenderTargetSize();
 
1285
        if (x > (u32)renderTargetSize.Width || y > (u32)renderTargetSize.Height)
 
1286
                return;
 
1287
 
 
1288
        setRenderStates2DMode(color.getAlpha() < 255, false, false);
 
1289
        setActiveTexture(0,0);
 
1290
 
 
1291
        setVertexShader(EVT_STANDARD);
 
1292
 
 
1293
        S3DVertex vertex((f32)x, (f32)y, 0.f, 0.f, 0.f, 0.f, color, 0.f, 0.f);
 
1294
 
 
1295
        pID3DDevice->DrawPrimitiveUP(D3DPT_POINTLIST, 1, &vertex, sizeof(vertex));
 
1296
}
 
1297
 
 
1298
 
 
1299
//! sets right vertex shader
 
1300
void CD3D8Driver::setVertexShader(E_VERTEX_TYPE newType)
 
1301
{
 
1302
        // Because we don't know if a vertex shader was set in a material instead of a
 
1303
        // fvf, this call cannot be prevented in D3D8.
 
1304
        //if (newType != LastVertexType)
 
1305
        {
 
1306
                LastVertexType = newType;
 
1307
                HRESULT hr = 0;
 
1308
 
 
1309
                switch(newType)
 
1310
                {
 
1311
                case EVT_STANDARD:
 
1312
                        hr = pID3DDevice->SetVertexShader(D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1);
 
1313
                        break;
 
1314
                case EVT_2TCOORDS:
 
1315
                        hr = pID3DDevice->SetVertexShader(D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX2);
 
1316
                        break;
 
1317
                case EVT_TANGENTS:
 
1318
                        hr = pID3DDevice->SetVertexShader(D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX3 |
 
1319
                                D3DFVF_TEXCOORDSIZE2(0) | // real texture coord
 
1320
                                D3DFVF_TEXCOORDSIZE3(1) | // misuse texture coord 2 for tangent
 
1321
                                D3DFVF_TEXCOORDSIZE3(2)   // misuse texture coord 3 for binormal
 
1322
                                );
 
1323
                        break;
 
1324
                }
 
1325
 
 
1326
                if (FAILED(hr))
 
1327
                {
 
1328
                        os::Printer::log("Could not set vertex Shader.", ELL_ERROR);
 
1329
                        return;
 
1330
                }
 
1331
        }
 
1332
}
 
1333
 
 
1334
 
 
1335
//! sets the needed renderstates
 
1336
bool CD3D8Driver::setRenderStates3DMode()
 
1337
{
 
1338
        if (!pID3DDevice)
 
1339
                return false;
 
1340
 
 
1341
        if (CurrentRenderMode != ERM_3D)
 
1342
        {
 
1343
                // switch back the matrices
 
1344
                pID3DDevice->SetTransform(D3DTS_VIEW, (D3DMATRIX*)((void*)&Matrices[ETS_VIEW]));
 
1345
                pID3DDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)((void*)&Matrices[ETS_WORLD]));
 
1346
                pID3DDevice->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)((void*)&Matrices[ETS_PROJECTION]));
 
1347
 
 
1348
                pID3DDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
 
1349
 
 
1350
                ResetRenderStates = true;
 
1351
        }
 
1352
 
 
1353
        if (ResetRenderStates || LastMaterial != Material)
 
1354
        {
 
1355
                // unset old material
 
1356
 
 
1357
                if (CurrentRenderMode == ERM_3D &&
 
1358
                        LastMaterial.MaterialType != Material.MaterialType &&
 
1359
                        LastMaterial.MaterialType >= 0 && LastMaterial.MaterialType < (s32)MaterialRenderers.size())
 
1360
                        MaterialRenderers[LastMaterial.MaterialType].Renderer->OnUnsetMaterial();
 
1361
 
 
1362
                // set new material.
 
1363
 
 
1364
                if (Material.MaterialType >= 0 && Material.MaterialType < (s32)MaterialRenderers.size())
 
1365
                        MaterialRenderers[Material.MaterialType].Renderer->OnSetMaterial(
 
1366
                                Material, LastMaterial, ResetRenderStates, this);
 
1367
        }
 
1368
 
 
1369
        bool shaderOK = true;
 
1370
 
 
1371
        if (Material.MaterialType >= 0 && Material.MaterialType < (s32)MaterialRenderers.size())
 
1372
                shaderOK = MaterialRenderers[Material.MaterialType].Renderer->OnRender(this, LastVertexType);
 
1373
 
 
1374
        LastMaterial = Material;
 
1375
 
 
1376
        ResetRenderStates = false;
 
1377
 
 
1378
        CurrentRenderMode = ERM_3D;
 
1379
 
 
1380
        return shaderOK;
 
1381
}
 
1382
 
 
1383
 
 
1384
//! Map Irrlicht texture wrap mode to native values
 
1385
D3DTEXTUREADDRESS CD3D8Driver::getTextureWrapMode(const u8 clamp)
 
1386
{
 
1387
        switch (clamp)
 
1388
        {
 
1389
                case ETC_REPEAT:
 
1390
                        if (Caps.TextureAddressCaps & D3DPTADDRESSCAPS_WRAP)
 
1391
                                return D3DTADDRESS_WRAP;
 
1392
                case ETC_CLAMP:
 
1393
                case ETC_CLAMP_TO_EDGE:
 
1394
                        if (Caps.TextureAddressCaps & D3DPTADDRESSCAPS_CLAMP)
 
1395
                                return D3DTADDRESS_CLAMP;
 
1396
                case ETC_MIRROR:
 
1397
                        if (Caps.TextureAddressCaps & D3DPTADDRESSCAPS_MIRROR)
 
1398
                                return D3DTADDRESS_MIRROR;
 
1399
                case ETC_CLAMP_TO_BORDER:
 
1400
                        if (Caps.TextureAddressCaps & D3DPTADDRESSCAPS_BORDER)
 
1401
                                return D3DTADDRESS_BORDER;
 
1402
                        else
 
1403
                                return D3DTADDRESS_CLAMP;
 
1404
                case ETC_MIRROR_CLAMP:
 
1405
                case ETC_MIRROR_CLAMP_TO_EDGE:
 
1406
                case ETC_MIRROR_CLAMP_TO_BORDER:
 
1407
                        if (Caps.TextureAddressCaps & D3DPTADDRESSCAPS_MIRRORONCE)
 
1408
                                return D3DTADDRESS_MIRRORONCE;
 
1409
                        else
 
1410
                                return D3DTADDRESS_CLAMP;
 
1411
                default:
 
1412
                        return D3DTADDRESS_WRAP;
 
1413
        }
 
1414
}
 
1415
 
 
1416
 
 
1417
//! Can be called by an IMaterialRenderer to make its work easier.
 
1418
void CD3D8Driver::setBasicRenderStates(const SMaterial& material, const SMaterial& lastmaterial,
 
1419
        bool resetAllRenderstates)
 
1420
{
 
1421
        if (resetAllRenderstates ||
 
1422
                lastmaterial.AmbientColor != material.AmbientColor ||
 
1423
                lastmaterial.DiffuseColor != material.DiffuseColor ||
 
1424
                lastmaterial.SpecularColor != material.SpecularColor ||
 
1425
                lastmaterial.EmissiveColor != material.EmissiveColor ||
 
1426
                lastmaterial.Shininess != material.Shininess)
 
1427
        {
 
1428
                D3DMATERIAL8 mat;
 
1429
                mat.Diffuse = colorToD3D(material.DiffuseColor);
 
1430
                mat.Ambient = colorToD3D(material.AmbientColor);
 
1431
                mat.Specular = colorToD3D(material.SpecularColor);
 
1432
                mat.Emissive = colorToD3D(material.EmissiveColor);
 
1433
                mat.Power = material.Shininess;
 
1434
                pID3DDevice->SetMaterial(&mat);
 
1435
        }
 
1436
 
 
1437
        if (lastmaterial.ColorMaterial != material.ColorMaterial)
 
1438
        {
 
1439
                pID3DDevice->SetRenderState(D3DRS_COLORVERTEX, (material.ColorMaterial != ECM_NONE));
 
1440
                pID3DDevice->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE,
 
1441
                        ((material.ColorMaterial == ECM_DIFFUSE)||
 
1442
                        (material.ColorMaterial == ECM_DIFFUSE_AND_AMBIENT))?D3DMCS_COLOR1:D3DMCS_MATERIAL);
 
1443
                pID3DDevice->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE,
 
1444
                        ((material.ColorMaterial == ECM_AMBIENT)||
 
1445
                        (material.ColorMaterial == ECM_DIFFUSE_AND_AMBIENT))?D3DMCS_COLOR1:D3DMCS_MATERIAL);
 
1446
                pID3DDevice->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE,
 
1447
                        (material.ColorMaterial == ECM_EMISSIVE)?D3DMCS_COLOR1:D3DMCS_MATERIAL);
 
1448
                pID3DDevice->SetRenderState(D3DRS_SPECULARMATERIALSOURCE,
 
1449
                        (material.ColorMaterial == ECM_SPECULAR)?D3DMCS_COLOR1:D3DMCS_MATERIAL);
 
1450
        }
 
1451
 
 
1452
        // fillmode
 
1453
        if (resetAllRenderstates || lastmaterial.Wireframe != material.Wireframe || lastmaterial.PointCloud != material.PointCloud)
 
1454
        {
 
1455
                if (material.Wireframe)
 
1456
                        pID3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
 
1457
                else
 
1458
                if (material.PointCloud)
 
1459
                        pID3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_POINT);
 
1460
                else
 
1461
                        pID3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
 
1462
        }
 
1463
 
 
1464
        // shademode
 
1465
        if (resetAllRenderstates || lastmaterial.GouraudShading != material.GouraudShading)
 
1466
        {
 
1467
                if (material.GouraudShading)
 
1468
                        pID3DDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
 
1469
                else
 
1470
                        pID3DDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
 
1471
        }
 
1472
 
 
1473
        // lighting
 
1474
        if (resetAllRenderstates || lastmaterial.Lighting != material.Lighting)
 
1475
        {
 
1476
                if (material.Lighting)
 
1477
                        pID3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
 
1478
                else
 
1479
                        pID3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
 
1480
        }
 
1481
 
 
1482
        // zbuffer
 
1483
        if (resetAllRenderstates || lastmaterial.ZBuffer != material.ZBuffer)
 
1484
        {
 
1485
                switch (material.ZBuffer)
 
1486
                {
 
1487
                case ECFN_NEVER:
 
1488
                        pID3DDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
 
1489
                        break;
 
1490
                case ECFN_LESSEQUAL:
 
1491
                        pID3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
 
1492
                        pID3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
 
1493
                        break;
 
1494
                case ECFN_EQUAL:
 
1495
                        pID3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
 
1496
                        pID3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_EQUAL);
 
1497
                        break;
 
1498
                case ECFN_LESS:
 
1499
                        pID3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
 
1500
                        pID3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
 
1501
                        break;
 
1502
                case ECFN_NOTEQUAL:
 
1503
                        pID3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
 
1504
                        pID3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_NOTEQUAL);
 
1505
                        break;
 
1506
                case ECFN_GREATEREQUAL:
 
1507
                        pID3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
 
1508
                        pID3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_GREATEREQUAL);
 
1509
                        break;
 
1510
                case ECFN_GREATER:
 
1511
                        pID3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
 
1512
                        pID3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_GREATER);
 
1513
                        break;
 
1514
                case ECFN_ALWAYS:
 
1515
                        pID3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
 
1516
                        pID3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
 
1517
                        break;
 
1518
                }
 
1519
        }
 
1520
 
 
1521
        // zwrite
 
1522
//      if (resetAllRenderstates || lastmaterial.ZWriteEnable != material.ZWriteEnable)
 
1523
        {
 
1524
                if (material.ZWriteEnable && (AllowZWriteOnTransparent || !material.isTransparent()))
 
1525
                        pID3DDevice->SetRenderState( D3DRS_ZWRITEENABLE, TRUE);
 
1526
                else
 
1527
                        pID3DDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE);
 
1528
        }
 
1529
 
 
1530
        // back face culling
 
1531
        if (resetAllRenderstates || (lastmaterial.FrontfaceCulling != material.FrontfaceCulling) || (lastmaterial.BackfaceCulling != material.BackfaceCulling))
 
1532
        {
 
1533
//              if (material.FrontfaceCulling && material.BackfaceCulling)
 
1534
//                      pID3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW|D3DCULL_CCW);
 
1535
//              else
 
1536
                if (material.FrontfaceCulling)
 
1537
                        pID3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
 
1538
                else
 
1539
                if (material.BackfaceCulling)
 
1540
                        pID3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
 
1541
                else
 
1542
                        pID3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
 
1543
        }
 
1544
 
 
1545
        // fog
 
1546
        if (resetAllRenderstates || lastmaterial.FogEnable != material.FogEnable)
 
1547
        {
 
1548
                pID3DDevice->SetRenderState(D3DRS_FOGENABLE, material.FogEnable);
 
1549
        }
 
1550
 
 
1551
        // specular highlights
 
1552
        if (resetAllRenderstates || !core::equals(lastmaterial.Shininess, material.Shininess))
 
1553
        {
 
1554
                bool enable = (material.Shininess!=0);
 
1555
                pID3DDevice->SetRenderState(D3DRS_SPECULARENABLE, enable);
 
1556
                pID3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, enable);
 
1557
                pID3DDevice->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
 
1558
        }
 
1559
 
 
1560
        // normalization
 
1561
        if (resetAllRenderstates || lastmaterial.NormalizeNormals != material.NormalizeNormals)
 
1562
        {
 
1563
                pID3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, material.NormalizeNormals);
 
1564
        }
 
1565
 
 
1566
        // Color Mask
 
1567
        if (queryFeature(EVDF_COLOR_MASK) &&
 
1568
                (resetAllRenderstates || lastmaterial.ColorMask != material.ColorMask))
 
1569
        {
 
1570
                const DWORD flag =
 
1571
                        ((material.ColorMask & ECP_RED)?D3DCOLORWRITEENABLE_RED:0) |
 
1572
                        ((material.ColorMask & ECP_GREEN)?D3DCOLORWRITEENABLE_GREEN:0) |
 
1573
                        ((material.ColorMask & ECP_BLUE)?D3DCOLORWRITEENABLE_BLUE:0) |
 
1574
                        ((material.ColorMask & ECP_ALPHA)?D3DCOLORWRITEENABLE_ALPHA:0);
 
1575
                pID3DDevice->SetRenderState(D3DRS_COLORWRITEENABLE, flag);
 
1576
        }
 
1577
 
 
1578
        if (queryFeature(EVDF_BLEND_OPERATIONS) &&
 
1579
                (resetAllRenderstates|| lastmaterial.BlendOperation != material.BlendOperation))
 
1580
        {
 
1581
                if (material.BlendOperation==EBO_NONE)
 
1582
                        pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
 
1583
                else
 
1584
                {
 
1585
                        pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
 
1586
                        switch (material.BlendOperation)
 
1587
                        {
 
1588
                        case EBO_MAX:
 
1589
                        case EBO_MAX_FACTOR:
 
1590
                        case EBO_MAX_ALPHA:
 
1591
                                pID3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_MAX);
 
1592
                                break;
 
1593
                        case EBO_MIN:
 
1594
                        case EBO_MIN_FACTOR:
 
1595
                        case EBO_MIN_ALPHA:
 
1596
                                pID3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_MIN);
 
1597
                                break;
 
1598
                        case EBO_SUBTRACT:
 
1599
                                pID3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
 
1600
                                break;
 
1601
                        case EBO_REVSUBTRACT:
 
1602
                                pID3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_REVSUBTRACT);
 
1603
                                break;
 
1604
                        default:
 
1605
                                pID3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
 
1606
                                break;
 
1607
                        }
 
1608
                }
 
1609
        }
 
1610
 
 
1611
        // Polygon offset
 
1612
        if (queryFeature(EVDF_POLYGON_OFFSET) && (resetAllRenderstates ||
 
1613
                lastmaterial.PolygonOffsetDirection != material.PolygonOffsetDirection ||
 
1614
                lastmaterial.PolygonOffsetFactor != material.PolygonOffsetFactor))
 
1615
        {
 
1616
                pID3DDevice->SetRenderState(D3DRS_ZBIAS, material.PolygonOffsetFactor);
 
1617
        }
 
1618
 
 
1619
        // thickness
 
1620
        if (resetAllRenderstates || lastmaterial.Thickness != material.Thickness)
 
1621
        {
 
1622
                pID3DDevice->SetRenderState(D3DRS_POINTSIZE, *((DWORD*)&material.Thickness));
 
1623
        }
 
1624
 
 
1625
        // texture address mode
 
1626
        for (u32 st=0; st<MaxTextureUnits; ++st)
 
1627
        {
 
1628
                if (resetAllRenderstates || lastmaterial.TextureLayer[st].LODBias != material.TextureLayer[st].LODBias)
 
1629
                {
 
1630
                        const float tmp = material.TextureLayer[st].LODBias * 0.125f;
 
1631
                        pID3DDevice->SetTextureStageState(st, D3DTSS_MIPMAPLODBIAS, *(DWORD*)(&tmp));
 
1632
                }
 
1633
 
 
1634
                if (resetAllRenderstates || lastmaterial.TextureLayer[st].TextureWrapU != material.TextureLayer[st].TextureWrapU)
 
1635
                        pID3DDevice->SetTextureStageState(st, D3DTSS_ADDRESSU, getTextureWrapMode(material.TextureLayer[st].TextureWrapU));
 
1636
                // If separate UV not supported reuse U for V
 
1637
                if (!(Caps.TextureAddressCaps & D3DPTADDRESSCAPS_INDEPENDENTUV))
 
1638
                        pID3DDevice->SetTextureStageState(st, D3DTSS_ADDRESSV, getTextureWrapMode(material.TextureLayer[st].TextureWrapU));
 
1639
                else if (resetAllRenderstates || lastmaterial.TextureLayer[st].TextureWrapV != material.TextureLayer[st].TextureWrapV)
 
1640
                        pID3DDevice->SetTextureStageState(st, D3DTSS_ADDRESSV, getTextureWrapMode(material.TextureLayer[st].TextureWrapV));
 
1641
 
 
1642
                // Bilinear and/or trilinear
 
1643
                if (resetAllRenderstates ||
 
1644
                        lastmaterial.TextureLayer[st].BilinearFilter != material.TextureLayer[st].BilinearFilter ||
 
1645
                        lastmaterial.TextureLayer[st].TrilinearFilter != material.TextureLayer[st].TrilinearFilter ||
 
1646
                        lastmaterial.TextureLayer[st].AnisotropicFilter != material.TextureLayer[st].AnisotropicFilter ||
 
1647
                        lastmaterial.UseMipMaps != material.UseMipMaps)
 
1648
                {
 
1649
                        if (material.TextureLayer[st].BilinearFilter || material.TextureLayer[st].TrilinearFilter || material.TextureLayer[st].AnisotropicFilter>1)
 
1650
                        {
 
1651
                                const D3DTEXTUREFILTERTYPE tftMag = ((Caps.TextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC) &&
 
1652
                                                material.TextureLayer[st].AnisotropicFilter) ? D3DTEXF_ANISOTROPIC : D3DTEXF_LINEAR;
 
1653
                                const D3DTEXTUREFILTERTYPE tftMin = ((Caps.TextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC) &&
 
1654
                                                material.TextureLayer[st].AnisotropicFilter) ? D3DTEXF_ANISOTROPIC : D3DTEXF_LINEAR;
 
1655
                                const D3DTEXTUREFILTERTYPE tftMip = material.UseMipMaps? (material.TextureLayer[st].TrilinearFilter ? D3DTEXF_LINEAR : D3DTEXF_POINT) : D3DTEXF_NONE;
 
1656
 
 
1657
                                if (tftMag==D3DTEXF_ANISOTROPIC || tftMin == D3DTEXF_ANISOTROPIC)
 
1658
                                        pID3DDevice->SetTextureStageState(st, D3DTSS_MAXANISOTROPY, core::min_((DWORD)material.TextureLayer[st].AnisotropicFilter, Caps.MaxAnisotropy));
 
1659
                                pID3DDevice->SetTextureStageState(st, D3DTSS_MAGFILTER, tftMag);
 
1660
                                pID3DDevice->SetTextureStageState(st, D3DTSS_MINFILTER, tftMin);
 
1661
                                pID3DDevice->SetTextureStageState(st, D3DTSS_MIPFILTER, tftMip);
 
1662
                        }
 
1663
                        else
 
1664
                        {
 
1665
                                pID3DDevice->SetTextureStageState(st, D3DTSS_MINFILTER, D3DTEXF_POINT);
 
1666
                                pID3DDevice->SetTextureStageState(st, D3DTSS_MIPFILTER, D3DTEXF_NONE);
 
1667
                                pID3DDevice->SetTextureStageState(st, D3DTSS_MAGFILTER, D3DTEXF_POINT);
 
1668
                        }
 
1669
                }
 
1670
        }
 
1671
}
 
1672
 
 
1673
 
 
1674
//! sets the needed renderstates
 
1675
void CD3D8Driver::setRenderStatesStencilShadowMode(bool zfail)
 
1676
{
 
1677
        if ((CurrentRenderMode != ERM_SHADOW_VOLUME_ZFAIL &&
 
1678
                CurrentRenderMode != ERM_SHADOW_VOLUME_ZPASS) ||
 
1679
                Transformation3DChanged)
 
1680
        {
 
1681
                // switch back the matrices
 
1682
                pID3DDevice->SetTransform(D3DTS_VIEW, (D3DMATRIX*)((void*)&Matrices[ETS_VIEW]));
 
1683
                pID3DDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)((void*)&Matrices[ETS_WORLD]));
 
1684
                pID3DDevice->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)((void*)&Matrices[ETS_PROJECTION]));
 
1685
 
 
1686
                Transformation3DChanged = false;
 
1687
 
 
1688
                setActiveTexture(0,0);
 
1689
                setActiveTexture(1,0);
 
1690
                setActiveTexture(2,0);
 
1691
                setActiveTexture(3,0);
 
1692
 
 
1693
                pID3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
 
1694
                pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
 
1695
                pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
 
1696
                pID3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
 
1697
                pID3DDevice->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
 
1698
                pID3DDevice->SetTextureStageState(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
 
1699
                pID3DDevice->SetTextureStageState(3, D3DTSS_COLOROP, D3DTOP_DISABLE);
 
1700
                pID3DDevice->SetTextureStageState(3, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
 
1701
 
 
1702
                pID3DDevice->SetVertexShader(D3DFVF_XYZ);
 
1703
                LastVertexType = (video::E_VERTEX_TYPE)(-1);
 
1704
 
 
1705
                pID3DDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
 
1706
                pID3DDevice->SetRenderState( D3DRS_STENCILENABLE, TRUE );
 
1707
                pID3DDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_FLAT);
 
1708
 
 
1709
                //pID3DDevice->SetRenderState(D3DRS_FOGENABLE, FALSE);
 
1710
                //pID3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
 
1711
 
 
1712
                // unset last 3d material
 
1713
                if (CurrentRenderMode == ERM_3D &&
 
1714
                        Material.MaterialType >= 0 && Material.MaterialType < (s32)MaterialRenderers.size())
 
1715
                        MaterialRenderers[Material.MaterialType].Renderer->OnUnsetMaterial();
 
1716
        }
 
1717
 
 
1718
        if (CurrentRenderMode != ERM_SHADOW_VOLUME_ZPASS && !zfail)
 
1719
        {
 
1720
                // USE THE ZPASS METHOD
 
1721
 
 
1722
                pID3DDevice->SetRenderState( D3DRS_STENCILFUNC,  D3DCMP_ALWAYS );
 
1723
                pID3DDevice->SetRenderState( D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP );
 
1724
                pID3DDevice->SetRenderState( D3DRS_STENCILFAIL,  D3DSTENCILOP_KEEP );
 
1725
 
 
1726
                pID3DDevice->SetRenderState( D3DRS_STENCILREF,       0x1 );
 
1727
                pID3DDevice->SetRenderState( D3DRS_STENCILMASK,      0xffffffff );
 
1728
                pID3DDevice->SetRenderState( D3DRS_STENCILWRITEMASK, 0xffffffff );
 
1729
 
 
1730
                pID3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE);
 
1731
                pID3DDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_ZERO );
 
1732
                pID3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
 
1733
        }
 
1734
        else
 
1735
        if (CurrentRenderMode != ERM_SHADOW_VOLUME_ZFAIL && zfail)
 
1736
        {
 
1737
                // USE THE ZFAIL METHOD
 
1738
 
 
1739
                pID3DDevice->SetRenderState( D3DRS_STENCILFUNC,  D3DCMP_ALWAYS );
 
1740
                pID3DDevice->SetRenderState( D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP );
 
1741
                pID3DDevice->SetRenderState( D3DRS_STENCILFAIL,  D3DSTENCILOP_KEEP );
 
1742
                pID3DDevice->SetRenderState( D3DRS_STENCILPASS,  D3DSTENCILOP_KEEP );
 
1743
 
 
1744
                pID3DDevice->SetRenderState( D3DRS_STENCILREF,       0x0 );
 
1745
                pID3DDevice->SetRenderState( D3DRS_STENCILMASK,      0xffffffff );
 
1746
                pID3DDevice->SetRenderState( D3DRS_STENCILWRITEMASK, 0xffffffff );
 
1747
 
 
1748
                pID3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
 
1749
                pID3DDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_ZERO );
 
1750
                pID3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
 
1751
        }
 
1752
 
 
1753
        CurrentRenderMode = zfail ? ERM_SHADOW_VOLUME_ZFAIL : ERM_SHADOW_VOLUME_ZPASS;
 
1754
}
 
1755
 
 
1756
 
 
1757
//! sets the needed renderstates
 
1758
void CD3D8Driver::setRenderStatesStencilFillMode(bool alpha)
 
1759
{
 
1760
        if (CurrentRenderMode != ERM_STENCIL_FILL || Transformation3DChanged)
 
1761
        {
 
1762
                pID3DDevice->SetTransform(D3DTS_VIEW, &UnitMatrixD3D8);
 
1763
                pID3DDevice->SetTransform(D3DTS_WORLD, &UnitMatrixD3D8);
 
1764
                pID3DDevice->SetTransform(D3DTS_PROJECTION, &UnitMatrixD3D8);
 
1765
 
 
1766
                pID3DDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
 
1767
                pID3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
 
1768
                pID3DDevice->SetRenderState(D3DRS_FOGENABLE, FALSE);
 
1769
 
 
1770
                pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
 
1771
                pID3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
 
1772
                pID3DDevice->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
 
1773
                pID3DDevice->SetTextureStageState(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
 
1774
                pID3DDevice->SetTextureStageState(3, D3DTSS_COLOROP, D3DTOP_DISABLE);
 
1775
                pID3DDevice->SetTextureStageState(3, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
 
1776
 
 
1777
                pID3DDevice->SetRenderState(D3DRS_STENCILREF, 0x1 );
 
1778
                pID3DDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_LESSEQUAL);
 
1779
                //pID3DDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_GREATEREQUAL);
 
1780
                pID3DDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP );
 
1781
                pID3DDevice->SetRenderState( D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP );
 
1782
                pID3DDevice->SetRenderState( D3DRS_STENCILMASK,      0xffffffff );
 
1783
                pID3DDevice->SetRenderState( D3DRS_STENCILWRITEMASK, 0xffffffff );
 
1784
 
 
1785
                pID3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW);
 
1786
 
 
1787
                Transformation3DChanged = false;
 
1788
 
 
1789
                if (alpha)
 
1790
                {
 
1791
                        pID3DDevice->SetTextureStageState(0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
 
1792
                        pID3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
 
1793
                        pID3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
 
1794
                        pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1);
 
1795
                        pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );
 
1796
                        pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
 
1797
                        pID3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
 
1798
                        pID3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
 
1799
                }
 
1800
                else
 
1801
                {
 
1802
                        pID3DDevice->SetTextureStageState(0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
 
1803
                        pID3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
 
1804
                        pID3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
 
1805
                        pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);
 
1806
                        pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
 
1807
                }
 
1808
        }
 
1809
 
 
1810
        CurrentRenderMode = ERM_STENCIL_FILL;
 
1811
}
 
1812
 
 
1813
 
 
1814
//! sets the needed renderstates
 
1815
void CD3D8Driver::setRenderStates2DMode(bool alpha, bool texture, bool alphaChannel)
 
1816
{
 
1817
        if (!pID3DDevice)
 
1818
                return;
 
1819
 
 
1820
        if (CurrentRenderMode != ERM_2D || Transformation3DChanged)
 
1821
        {
 
1822
                // unset last 3d material
 
1823
                if (CurrentRenderMode == ERM_3D)
 
1824
                {
 
1825
                        if (static_cast<u32>(LastMaterial.MaterialType) < MaterialRenderers.size())
 
1826
                                MaterialRenderers[LastMaterial.MaterialType].Renderer->OnUnsetMaterial();
 
1827
                }
 
1828
                if (!OverrideMaterial2DEnabled)
 
1829
                {
 
1830
                        setBasicRenderStates(InitMaterial2D, LastMaterial, true);
 
1831
                        LastMaterial=InitMaterial2D;
 
1832
 
 
1833
                        // fix everything that is wrongly set by SMaterial default
 
1834
                        pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
 
1835
                        pID3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
 
1836
                        pID3DDevice->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
 
1837
                        pID3DDevice->SetTextureStageState(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
 
1838
                        pID3DDevice->SetTextureStageState(3, D3DTSS_COLOROP, D3DTOP_DISABLE);
 
1839
                        pID3DDevice->SetTextureStageState(3, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
 
1840
 
 
1841
                        pID3DDevice->SetRenderState( D3DRS_STENCILENABLE, FALSE );
 
1842
 
 
1843
                }
 
1844
                pID3DDevice->SetTransform(D3DTS_WORLD, &UnitMatrixD3D8);
 
1845
                core::matrix4 m;
 
1846
                m.setTranslation(core::vector3df(-0.5f,-0.5f,0));
 
1847
                pID3DDevice->SetTransform(D3DTS_VIEW, (D3DMATRIX*)((void*)m.pointer()));
 
1848
 
 
1849
                const core::dimension2d<u32>& renderTargetSize = getCurrentRenderTargetSize();
 
1850
                m.buildProjectionMatrixOrthoLH(f32(renderTargetSize.Width), f32(-(s32)(renderTargetSize.Height)), -1.0, 1.0);
 
1851
                m.setTranslation(core::vector3df(-1,1,0));
 
1852
                pID3DDevice->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)((void*)m.pointer()));
 
1853
 
 
1854
                Transformation3DChanged = false;
 
1855
        }
 
1856
        if (OverrideMaterial2DEnabled)
 
1857
        {
 
1858
                OverrideMaterial2D.Lighting=false;
 
1859
                setBasicRenderStates(OverrideMaterial2D, LastMaterial, false);
 
1860
                LastMaterial = OverrideMaterial2D;
 
1861
        }
 
1862
 
 
1863
        // no alphaChannel without texture
 
1864
        alphaChannel &= texture;
 
1865
 
 
1866
        if (alpha || alphaChannel)
 
1867
        {
 
1868
                pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
 
1869
                pID3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
 
1870
                pID3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
 
1871
        }
 
1872
        else
 
1873
                pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
 
1874
        pID3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
 
1875
        pID3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
 
1876
        pID3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
 
1877
        if (texture)
 
1878
        {
 
1879
                setTransform(ETS_TEXTURE_0, core::IdentityMatrix);
 
1880
                // Due to the transformation change, the previous line would call a reset each frame
 
1881
                // but we can safely reset the variable as it was false before
 
1882
                Transformation3DChanged=false;
 
1883
        }
 
1884
        if (alphaChannel)
 
1885
        {
 
1886
                pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
 
1887
 
 
1888
                if (alpha)
 
1889
                {
 
1890
                        pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
 
1891
                        pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
 
1892
                }
 
1893
                else
 
1894
                {
 
1895
                        pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
 
1896
                }
 
1897
 
 
1898
        }
 
1899
        else
 
1900
        {
 
1901
                pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
 
1902
                if (alpha)
 
1903
                {
 
1904
                        pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
 
1905
                }
 
1906
                else
 
1907
                {
 
1908
                        pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
 
1909
                        pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
 
1910
                }
 
1911
        }
 
1912
 
 
1913
        CurrentRenderMode = ERM_2D;
 
1914
}
 
1915
 
 
1916
 
 
1917
//! deletes all dynamic lights there are
 
1918
void CD3D8Driver::deleteAllDynamicLights()
 
1919
{
 
1920
        for (s32 i=0; i<LastSetLight+1; ++i)
 
1921
                pID3DDevice->LightEnable(i, false);
 
1922
 
 
1923
        LastSetLight = -1;
 
1924
 
 
1925
        CNullDriver::deleteAllDynamicLights();
 
1926
}
 
1927
 
 
1928
 
 
1929
//! adds a dynamic light
 
1930
s32 CD3D8Driver::addDynamicLight(const SLight& dl)
 
1931
{
 
1932
        CNullDriver::addDynamicLight(dl);
 
1933
 
 
1934
        D3DLIGHT8 light;
 
1935
 
 
1936
        switch (dl.Type)
 
1937
        {
 
1938
        case ELT_POINT:
 
1939
                light.Type = D3DLIGHT_POINT;
 
1940
        break;
 
1941
        case ELT_SPOT:
 
1942
                light.Type = D3DLIGHT_SPOT;
 
1943
        break;
 
1944
        case ELT_DIRECTIONAL:
 
1945
                light.Type = D3DLIGHT_DIRECTIONAL;
 
1946
        break;
 
1947
        }
 
1948
 
 
1949
        light.Position = *(D3DVECTOR*)((void*)(&dl.Position));
 
1950
        light.Direction = *(D3DVECTOR*)((void*)(&dl.Direction));
 
1951
 
 
1952
        light.Range = core::min_(dl.Radius, MaxLightDistance);
 
1953
        light.Falloff = dl.Falloff;
 
1954
 
 
1955
        light.Diffuse = *(D3DCOLORVALUE*)((void*)(&dl.DiffuseColor));
 
1956
        light.Specular = *(D3DCOLORVALUE*)((void*)(&dl.SpecularColor));
 
1957
        light.Ambient = *(D3DCOLORVALUE*)((void*)(&dl.AmbientColor));
 
1958
 
 
1959
        light.Attenuation0 = dl.Attenuation.X;
 
1960
        light.Attenuation1 = dl.Attenuation.Y;
 
1961
        light.Attenuation2 = dl.Attenuation.Z;
 
1962
 
 
1963
        light.Theta = dl.InnerCone * 2.0f * core::DEGTORAD;
 
1964
        light.Phi = dl.OuterCone * 2.0f * core::DEGTORAD;
 
1965
 
 
1966
        ++LastSetLight;
 
1967
 
 
1968
        if(D3D_OK == pID3DDevice->SetLight(LastSetLight, &light))
 
1969
        {
 
1970
                // I don't care if this succeeds
 
1971
                (void)pID3DDevice->LightEnable(LastSetLight, true);
 
1972
                return LastSetLight;
 
1973
        }
 
1974
 
 
1975
        return -1;
 
1976
}
 
1977
 
 
1978
 
 
1979
void CD3D8Driver::turnLightOn(s32 lightIndex, bool turnOn)
 
1980
{
 
1981
        if(lightIndex < 0 || lightIndex > LastSetLight)
 
1982
                return;
 
1983
 
 
1984
        (void)pID3DDevice->LightEnable(lightIndex, turnOn);
 
1985
}
 
1986
 
 
1987
 
 
1988
//! returns the maximal amount of dynamic lights the device can handle
 
1989
u32 CD3D8Driver::getMaximalDynamicLightAmount() const
 
1990
{
 
1991
        return Caps.MaxActiveLights;
 
1992
}
 
1993
 
 
1994
 
 
1995
//! Sets the dynamic ambient light color. The default color is
 
1996
//! (0,0,0,0) which means it is dark.
 
1997
//! \param color: New color of the ambient light.
 
1998
void CD3D8Driver::setAmbientLight(const SColorf& color)
 
1999
{
 
2000
        if (!pID3DDevice)
 
2001
                return;
 
2002
 
 
2003
        AmbientLight = color;
 
2004
        D3DCOLOR col = color.toSColor().color;
 
2005
        pID3DDevice->SetRenderState(D3DRS_AMBIENT, col);
 
2006
}
 
2007
 
 
2008
 
 
2009
//! \return Returns the name of the video driver. Example: In case of the DIRECT3D8
 
2010
//! driver, it would return "Direct3D8.1".
 
2011
const wchar_t* CD3D8Driver::getName() const
 
2012
{
 
2013
        return L"Direct3D 8.1";
 
2014
}
 
2015
 
 
2016
 
 
2017
//! Draws a shadow volume into the stencil buffer. To draw a stencil shadow, do
 
2018
//! this: Frist, draw all geometry. Then use this method, to draw the shadow
 
2019
//! volume. Then, use IVideoDriver::drawStencilShadow() to visualize the shadow.
 
2020
void CD3D8Driver::drawStencilShadowVolume(const core::vector3df* triangles, s32 count, bool zfail)
 
2021
{
 
2022
        if (!StencilBuffer || !count)
 
2023
                return;
 
2024
 
 
2025
        setRenderStatesStencilShadowMode(zfail);
 
2026
 
 
2027
        if (!zfail)
 
2028
        {
 
2029
                // ZPASS Method
 
2030
 
 
2031
                // Draw front-side of shadow volume in stencil/z only
 
2032
                pID3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW );
 
2033
                pID3DDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_INCRSAT);
 
2034
                pID3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, count / 3, triangles, sizeof(core::vector3df));
 
2035
 
 
2036
                // Now reverse cull order so front sides of shadow volume are written.
 
2037
                pID3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );
 
2038
                pID3DDevice->SetRenderState( D3DRS_STENCILPASS, D3DSTENCILOP_DECRSAT);
 
2039
                pID3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, count / 3, triangles, sizeof(core::vector3df));
 
2040
        }
 
2041
        else
 
2042
        {
 
2043
                // ZFAIL Method
 
2044
 
 
2045
                // Draw front-side of shadow volume in stencil/z only
 
2046
                pID3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW );
 
2047
                pID3DDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_INCRSAT );
 
2048
                pID3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, count / 3, triangles, sizeof(core::vector3df));
 
2049
 
 
2050
                // Now reverse cull order so front sides of shadow volume are written.
 
2051
                pID3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
 
2052
                pID3DDevice->SetRenderState( D3DRS_STENCILZFAIL, D3DSTENCILOP_DECRSAT );
 
2053
                pID3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, count / 3, triangles, sizeof(core::vector3df));
 
2054
        }
 
2055
}
 
2056
 
 
2057
 
 
2058
//! Fills the stencil shadow with color. After the shadow volume has been drawn
 
2059
//! into the stencil buffer using IVideoDriver::drawStencilShadowVolume(), use this
 
2060
//! to draw the color of the shadow.
 
2061
void CD3D8Driver::drawStencilShadow(bool clearStencilBuffer, video::SColor leftUpEdge,
 
2062
                        video::SColor rightUpEdge, video::SColor leftDownEdge, video::SColor rightDownEdge)
 
2063
{
 
2064
        if (!StencilBuffer)
 
2065
                return;
 
2066
 
 
2067
        S3DVertex vtx[4];
 
2068
        vtx[0] = S3DVertex(1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, leftUpEdge, 0.0f, 0.0f);
 
2069
        vtx[1] = S3DVertex(1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, rightUpEdge, 0.0f, 1.0f);
 
2070
        vtx[2] = S3DVertex(-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, leftDownEdge, 1.0f, 0.0f);
 
2071
        vtx[3] = S3DVertex(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, rightDownEdge, 1.0f, 1.0f);
 
2072
 
 
2073
        const s16 indices[6] = {0,1,2,1,3,2};
 
2074
 
 
2075
        setRenderStatesStencilFillMode(
 
2076
                leftUpEdge.getAlpha() < 255 ||
 
2077
                rightUpEdge.getAlpha() < 255 ||
 
2078
                leftDownEdge.getAlpha() < 255 ||
 
2079
                rightDownEdge.getAlpha() < 255);
 
2080
 
 
2081
        setActiveTexture(0,0);
 
2082
 
 
2083
        setVertexShader(EVT_STANDARD);
 
2084
 
 
2085
        pID3DDevice->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, 4, 2, &indices[0],
 
2086
                D3DFMT_INDEX16, &vtx[0], sizeof(S3DVertex));
 
2087
 
 
2088
        if (clearStencilBuffer)
 
2089
                pID3DDevice->Clear(0, NULL, D3DCLEAR_STENCIL,0, 1.0, 0);
 
2090
}
 
2091
 
 
2092
 
 
2093
//! Returns the maximum amount of primitives (mostly vertices) which
 
2094
//! the device is able to render with one drawIndexedTriangleList
 
2095
//! call.
 
2096
u32 CD3D8Driver::getMaximalPrimitiveCount() const
 
2097
{
 
2098
        return Caps.MaxPrimitiveCount;
 
2099
}
 
2100
 
 
2101
 
 
2102
//! Sets the fog mode.
 
2103
void CD3D8Driver::setFog(SColor color, E_FOG_TYPE fogType, f32 start,
 
2104
        f32 end, f32 density, bool pixelFog, bool rangeFog)
 
2105
{
 
2106
        CNullDriver::setFog(color, fogType, start, end, density, pixelFog, rangeFog);
 
2107
 
 
2108
        if (!pID3DDevice)
 
2109
                return;
 
2110
 
 
2111
        pID3DDevice->SetRenderState(D3DRS_FOGCOLOR, color.color);
 
2112
 
 
2113
        pID3DDevice->SetRenderState(
 
2114
#if defined( _IRR_XBOX_PLATFORM_)
 
2115
                D3DRS_FOGTABLEMODE,
 
2116
#else
 
2117
                pixelFog ? D3DRS_FOGTABLEMODE : D3DRS_FOGVERTEXMODE,
 
2118
#endif
 
2119
                (fogType==EFT_FOG_LINEAR)? D3DFOG_LINEAR : (fogType==EFT_FOG_EXP)?D3DFOG_EXP:D3DFOG_EXP2);
 
2120
 
 
2121
        if (fogType==EFT_FOG_LINEAR)
 
2122
        {
 
2123
                pID3DDevice->SetRenderState(D3DRS_FOGSTART, *(DWORD*)(&start));
 
2124
                pID3DDevice->SetRenderState(D3DRS_FOGEND, *(DWORD*)(&end));
 
2125
        }
 
2126
        else
 
2127
                pID3DDevice->SetRenderState(D3DRS_FOGDENSITY, *(DWORD*)(&density));
 
2128
 
 
2129
        if(!pixelFog)
 
2130
                pID3DDevice->SetRenderState(D3DRS_RANGEFOGENABLE, rangeFog);
 
2131
}
 
2132
 
 
2133
 
 
2134
//! Draws a 3d line.
 
2135
void CD3D8Driver::draw3DLine(const core::vector3df& start,
 
2136
        const core::vector3df& end, SColor color)
 
2137
{
 
2138
        setVertexShader(EVT_STANDARD);
 
2139
        setRenderStates3DMode();
 
2140
        video::S3DVertex v[2];
 
2141
        v[0].Color = color;
 
2142
        v[1].Color = color;
 
2143
        v[0].Pos = start;
 
2144
        v[1].Pos = end;
 
2145
 
 
2146
        pID3DDevice->DrawPrimitiveUP(D3DPT_LINELIST, 1, v, sizeof(S3DVertex));
 
2147
}
 
2148
 
 
2149
 
 
2150
void CD3D8Driver::OnResize(const core::dimension2d<u32>& size)
 
2151
{
 
2152
        if (!pID3DDevice)
 
2153
                return;
 
2154
 
 
2155
        CNullDriver::OnResize(size);
 
2156
        reset();
 
2157
}
 
2158
 
 
2159
 
 
2160
//! Returns type of video driver
 
2161
E_DRIVER_TYPE CD3D8Driver::getDriverType() const
 
2162
{
 
2163
        return EDT_DIRECT3D8;
 
2164
}
 
2165
 
 
2166
 
 
2167
//! Returns the transformation set by setTransform
 
2168
const core::matrix4& CD3D8Driver::getTransform(E_TRANSFORMATION_STATE state) const
 
2169
{
 
2170
        return Matrices[state];
 
2171
}
 
2172
 
 
2173
 
 
2174
//! Sets a vertex shader constant.
 
2175
void CD3D8Driver::setVertexShaderConstant(const f32* data, s32 startRegister, s32 constantAmount)
 
2176
{
 
2177
        if (data)
 
2178
                pID3DDevice->SetVertexShaderConstant(startRegister, data, constantAmount);
 
2179
}
 
2180
 
 
2181
 
 
2182
//! Sets a pixel shader constant.
 
2183
void CD3D8Driver::setPixelShaderConstant(const f32* data, s32 startRegister, s32 constantAmount)
 
2184
{
 
2185
        if (data)
 
2186
                pID3DDevice->SetPixelShaderConstant(startRegister, data, constantAmount);
 
2187
}
 
2188
 
 
2189
 
 
2190
//! Sets a constant for the vertex shader based on a name.
 
2191
bool CD3D8Driver::setVertexShaderConstant(const c8* name, const f32* floats, int count)
 
2192
{
 
2193
        os::Printer::log("Cannot set constant, no HLSL supported in D3D8");
 
2194
        return false;
 
2195
}
 
2196
 
 
2197
 
 
2198
//! Sets a constant for the pixel shader based on a name.
 
2199
bool CD3D8Driver::setPixelShaderConstant(const c8* name, const f32* floats, int count)
 
2200
{
 
2201
        os::Printer::log("Cannot set constant, no HLSL supported in D3D8");
 
2202
        return false;
 
2203
}
 
2204
 
 
2205
 
 
2206
//! Adds a new material renderer to the VideoDriver, using pixel and/or
 
2207
//! vertex shaders to render geometry.
 
2208
s32 CD3D8Driver::addShaderMaterial(const c8* vertexShaderProgram,
 
2209
        const c8* pixelShaderProgram,
 
2210
        IShaderConstantSetCallBack* callback,
 
2211
        E_MATERIAL_TYPE baseMaterial, s32 userData)
 
2212
{
 
2213
        s32 nr = -1;
 
2214
        CD3D8ShaderMaterialRenderer* r = new CD3D8ShaderMaterialRenderer(
 
2215
                pID3DDevice, this, nr, vertexShaderProgram, pixelShaderProgram,
 
2216
                callback, getMaterialRenderer(baseMaterial), userData);
 
2217
 
 
2218
        r->drop();
 
2219
        return nr;
 
2220
}
 
2221
 
 
2222
 
 
2223
//! Returns a pointer to the IVideoDriver interface. (Implementation for
 
2224
//! IMaterialRendererServices)
 
2225
IVideoDriver* CD3D8Driver::getVideoDriver()
 
2226
{
 
2227
        return this;
 
2228
}
 
2229
 
 
2230
 
 
2231
//! Clears the ZBuffer.
 
2232
void CD3D8Driver::clearZBuffer()
 
2233
{
 
2234
        const HRESULT hr = pID3DDevice->Clear( 0, NULL, D3DCLEAR_ZBUFFER, 0, 1.0, 0);
 
2235
 
 
2236
        if (FAILED(hr))
 
2237
                os::Printer::log("CD3D8Driver clearZBuffer() failed.", ELL_WARNING);
 
2238
}
 
2239
 
 
2240
 
 
2241
//! Returns an image created from the last rendered frame.
 
2242
IImage* CD3D8Driver::createScreenShot(video::ECOLOR_FORMAT format, video::E_RENDER_TARGET target)
 
2243
{
 
2244
#if defined( _IRR_XBOX_PLATFORM_)
 
2245
        return 0;
 
2246
#else
 
2247
        if (target != video::ERT_FRAME_BUFFER)
 
2248
                return 0;
 
2249
 
 
2250
        // query the screen dimensions of the current adapter
 
2251
        D3DDISPLAYMODE displayMode;
 
2252
        pID3DDevice->GetDisplayMode(&displayMode);
 
2253
 
 
2254
        if (format==video::ECF_UNKNOWN)
 
2255
                format=video::ECF_A8R8G8B8;
 
2256
 
 
2257
        // create the image surface to store the front buffer image [always A8R8G8B8]
 
2258
        HRESULT hr;
 
2259
        LPDIRECT3DSURFACE8 lpSurface;
 
2260
        if (FAILED(hr = pID3DDevice->CreateImageSurface(displayMode.Width, displayMode.Height, D3DFMT_A8R8G8B8, &lpSurface)))
 
2261
                return 0;
 
2262
 
 
2263
        // read the front buffer into the image surface
 
2264
        if (FAILED(hr = pID3DDevice->GetFrontBuffer(lpSurface)))
 
2265
        {
 
2266
                lpSurface->Release();
 
2267
                return 0;
 
2268
        }
 
2269
 
 
2270
        RECT clientRect;
 
2271
        {
 
2272
                POINT clientPoint;
 
2273
                clientPoint.x = 0;
 
2274
                clientPoint.y = 0;
 
2275
 
 
2276
                ClientToScreen( (HWND)getExposedVideoData().D3D8.HWnd, &clientPoint );
 
2277
 
 
2278
                clientRect.left   = clientPoint.x;
 
2279
                clientRect.top    = clientPoint.y;
 
2280
                clientRect.right  = clientRect.left + ScreenSize.Width;
 
2281
                clientRect.bottom = clientRect.top  + ScreenSize.Height;
 
2282
 
 
2283
                // window can be off-screen partly, we can't take screenshots from that
 
2284
                clientRect.left = core::max_(clientRect.left, 0l);
 
2285
                clientRect.top = core::max_(clientRect.top, 0l);
 
2286
                clientRect.right = core::min_(clientRect.right, (long)displayMode.Width);
 
2287
                clientRect.bottom = core::min_(clientRect.bottom, (long)displayMode.Height );
 
2288
        }
 
2289
 
 
2290
        // lock our area of the surface
 
2291
        D3DLOCKED_RECT lockedRect;
 
2292
        if (FAILED(lpSurface->LockRect(&lockedRect, &clientRect, D3DLOCK_READONLY)))
 
2293
        {
 
2294
                lpSurface->Release();
 
2295
                return 0;
 
2296
        }
 
2297
 
 
2298
        irr::core::dimension2d<u32> shotSize;
 
2299
        shotSize.Width = core::min_( ScreenSize.Width, (u32)(clientRect.right-clientRect.left) );
 
2300
        shotSize.Height = core::min_( ScreenSize.Height, (u32)(clientRect.bottom-clientRect.top) );
 
2301
 
 
2302
        // this could throw, but we aren't going to worry about that case very much
 
2303
        IImage* newImage = createImage(format, shotSize);
 
2304
 
 
2305
        if (newImage)
 
2306
        {
 
2307
                // d3d pads the image, so we need to copy the correct number of bytes
 
2308
                u32* dP = (u32*)newImage->lock();
 
2309
                u8 * sP = (u8 *)lockedRect.pBits;
 
2310
 
 
2311
                // If the display mode format doesn't promise anything about the Alpha value
 
2312
                // and it appears that it's not presenting 255, then we should manually
 
2313
                // set each pixel alpha value to 255.
 
2314
                if(D3DFMT_X8R8G8B8 == displayMode.Format && (0xFF000000 != (*dP & 0xFF000000)))
 
2315
                {
 
2316
                        for (u32 y = 0; y < shotSize.Height; ++y)
 
2317
                        {
 
2318
                                for(u32 x = 0; x < shotSize.Width; ++x)
 
2319
                                {
 
2320
                                        newImage->setPixel(x,y,*((u32*)sP) | 0xFF000000);
 
2321
                                        sP += 4;
 
2322
                                }
 
2323
 
 
2324
                                sP += lockedRect.Pitch - (4 * shotSize.Width);
 
2325
                        }
 
2326
                }
 
2327
                else
 
2328
                {
 
2329
                        for (u32 y = 0; y < shotSize.Height; ++y)
 
2330
                        {
 
2331
                                convertColor(sP, video::ECF_A8R8G8B8, shotSize.Width, dP, format);
 
2332
                                sP += lockedRect.Pitch;
 
2333
                                dP += shotSize.Width;
 
2334
                        }
 
2335
                }
 
2336
 
 
2337
                newImage->unlock();
 
2338
        }
 
2339
 
 
2340
        // we can unlock and release the surface
 
2341
        lpSurface->UnlockRect();
 
2342
 
 
2343
        // release the image surface
 
2344
        lpSurface->Release();
 
2345
 
 
2346
        // return status of save operation to caller
 
2347
        return newImage;
 
2348
#endif
 
2349
}
 
2350
 
 
2351
 
 
2352
// returns the current size of the screen or rendertarget
 
2353
const core::dimension2d<u32>& CD3D8Driver::getCurrentRenderTargetSize() const
 
2354
{
 
2355
        if ( CurrentRendertargetSize.Width == 0 )
 
2356
                return ScreenSize;
 
2357
        else
 
2358
                return CurrentRendertargetSize;
 
2359
}
 
2360
 
 
2361
 
 
2362
// Set/unset a clipping plane.
 
2363
bool CD3D8Driver::setClipPlane(u32 index, const core::plane3df& plane, bool enable)
 
2364
{
 
2365
#if defined( _IRR_XBOX_PLATFORM_)
 
2366
        return false;
 
2367
#else
 
2368
        if (index >= MaxUserClipPlanes)
 
2369
                return false;
 
2370
        pID3DDevice->SetClipPlane(index, (const float*)&plane);
 
2371
        enableClipPlane(index, enable);
 
2372
        return true;
 
2373
#endif
 
2374
}
 
2375
 
 
2376
 
 
2377
// Enable/disable a clipping plane.
 
2378
void CD3D8Driver::enableClipPlane(u32 index, bool enable)
 
2379
{
 
2380
#if defined( _IRR_XBOX_PLATFORM_)
 
2381
        return;
 
2382
#else
 
2383
        if (index >= MaxUserClipPlanes)
 
2384
                return;
 
2385
        DWORD renderstate;
 
2386
        pID3DDevice->GetRenderState(D3DRS_CLIPPLANEENABLE, &renderstate);
 
2387
        if (enable)
 
2388
                renderstate |= (1 << index);
 
2389
        else
 
2390
                renderstate &= ~(1 << index);
 
2391
        pID3DDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, renderstate);
 
2392
#endif
 
2393
}
 
2394
 
 
2395
 
 
2396
core::dimension2du CD3D8Driver::getMaxTextureSize() const
 
2397
{
 
2398
        return core::dimension2du(Caps.MaxTextureWidth, Caps.MaxTextureHeight);
 
2399
}
 
2400
 
 
2401
 
 
2402
} // end namespace video
 
2403
} // end namespace irr
 
2404
 
 
2405
#endif // _IRR_COMPILE_WITH_DIRECT3D_8_
 
2406
 
 
2407
 
 
2408
namespace irr
 
2409
{
 
2410
namespace video
 
2411
{
 
2412
 
 
2413
#ifdef _IRR_COMPILE_WITH_DIRECT3D_8_
 
2414
//! creates a video driver
 
2415
IVideoDriver* createDirectX8Driver(const SIrrlichtCreationParameters& params,
 
2416
                        io::IFileSystem* io, HWND window)
 
2417
{
 
2418
        const bool pureSoftware = false;
 
2419
        CD3D8Driver* dx8 = new CD3D8Driver(params.WindowSize, window, params.Fullscreen, params.Stencilbuffer, io, pureSoftware);
 
2420
 
 
2421
        if (!dx8->initDriver(params.WindowSize, window, params.Bits, params.Fullscreen, pureSoftware, params.HighPrecisionFPU,
 
2422
                params.Vsync, params.AntiAlias, params.DisplayAdapter))
 
2423
        {
 
2424
                dx8->drop();
 
2425
                dx8 = 0;
 
2426
        }
 
2427
 
 
2428
        return dx8;
 
2429
}
 
2430
#endif // _IRR_COMPILE_WITH_DIRECT3D_8_
 
2431
 
 
2432
} // end namespace video
 
2433
} // end namespace irr