aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/source/Irrlicht/CD3D9Driver.h
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/CD3D9Driver.h')
-rw-r--r--libraries/irrlicht-1.8/source/Irrlicht/CD3D9Driver.h496
1 files changed, 496 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CD3D9Driver.h b/libraries/irrlicht-1.8/source/Irrlicht/CD3D9Driver.h
new file mode 100644
index 0000000..095ae9d
--- /dev/null
+++ b/libraries/irrlicht-1.8/source/Irrlicht/CD3D9Driver.h
@@ -0,0 +1,496 @@
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4
5#ifndef __C_VIDEO_DIRECTX_9_H_INCLUDED__
6#define __C_VIDEO_DIRECTX_9_H_INCLUDED__
7
8#include "IrrCompileConfig.h"
9
10#ifdef _IRR_COMPILE_WITH_DIRECT3D_9_
11
12#ifdef _IRR_WINDOWS_
13#define WIN32_LEAN_AND_MEAN
14#include <windows.h>
15#endif
16
17#include "CNullDriver.h"
18#include "SIrrCreationParameters.h"
19#include "IMaterialRendererServices.h"
20#if defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
21#include "irrMath.h" // needed by borland for sqrtf define
22#endif
23#include <d3d9.h>
24
25#ifdef _IRR_COMPILE_WITH_CG_
26#include "Cg/cg.h"
27#include "Cg/cgD3D9.h"
28#endif
29
30namespace irr
31{
32namespace video
33{
34 struct SDepthSurface : public IReferenceCounted
35 {
36 SDepthSurface() : Surface(0)
37 {
38 #ifdef _DEBUG
39 setDebugName("SDepthSurface");
40 #endif
41 }
42 virtual ~SDepthSurface()
43 {
44 if (Surface)
45 Surface->Release();
46 }
47
48 IDirect3DSurface9* Surface;
49 core::dimension2du Size;
50 };
51
52 class CD3D9Driver : public CNullDriver, IMaterialRendererServices
53 {
54 public:
55
56 friend class CD3D9Texture;
57
58 //! constructor
59 CD3D9Driver(const SIrrlichtCreationParameters& params, io::IFileSystem* io);
60
61 //! destructor
62 virtual ~CD3D9Driver();
63
64 //! applications must call this method before performing any rendering. returns false if failed.
65 virtual bool beginScene(bool backBuffer=true, bool zBuffer=true,
66 SColor color=SColor(255,0,0,0),
67 const SExposedVideoData& videoData=SExposedVideoData(),
68 core::rect<s32>* sourceRect=0);
69
70 //! applications must call this method after performing any rendering. returns false if failed.
71 virtual bool endScene();
72
73 //! queries the features of the driver, returns true if feature is available
74 virtual bool queryFeature(E_VIDEO_DRIVER_FEATURE feature) const;
75
76 //! sets transformation
77 virtual void setTransform(E_TRANSFORMATION_STATE state, const core::matrix4& mat);
78
79 //! sets a material
80 virtual void setMaterial(const SMaterial& material);
81
82 //! sets a render target
83 virtual bool setRenderTarget(video::ITexture* texture,
84 bool clearBackBuffer=true, bool clearZBuffer=true,
85 SColor color=video::SColor(0,0,0,0));
86
87 //! Sets multiple render targets
88 virtual bool setRenderTarget(const core::array<video::IRenderTarget>& texture,
89 bool clearBackBuffer=true, bool clearZBuffer=true,
90 SColor color=video::SColor(0,0,0,0));
91
92 //! sets a viewport
93 virtual void setViewPort(const core::rect<s32>& area);
94
95 //! gets the area of the current viewport
96 virtual const core::rect<s32>& getViewPort() const;
97
98 struct SHWBufferLink_d3d9 : public SHWBufferLink
99 {
100 SHWBufferLink_d3d9(const scene::IMeshBuffer *_MeshBuffer):
101 SHWBufferLink(_MeshBuffer),
102 vertexBuffer(0), indexBuffer(0),
103 vertexBufferSize(0), indexBufferSize(0) {}
104
105 IDirect3DVertexBuffer9* vertexBuffer;
106 IDirect3DIndexBuffer9* indexBuffer;
107
108 u32 vertexBufferSize;
109 u32 indexBufferSize;
110 };
111
112 bool updateVertexHardwareBuffer(SHWBufferLink_d3d9 *HWBuffer);
113 bool updateIndexHardwareBuffer(SHWBufferLink_d3d9 *HWBuffer);
114
115 //! updates hardware buffer if needed
116 virtual bool updateHardwareBuffer(SHWBufferLink *HWBuffer);
117
118 //! Create hardware buffer from mesh
119 virtual SHWBufferLink *createHardwareBuffer(const scene::IMeshBuffer* mb);
120
121 //! Delete hardware buffer (only some drivers can)
122 virtual void deleteHardwareBuffer(SHWBufferLink *HWBuffer);
123
124 //! Draw hardware buffer
125 virtual void drawHardwareBuffer(SHWBufferLink *HWBuffer);
126
127 //! Create occlusion query.
128 /** Use node for identification and mesh for occlusion test. */
129 virtual void addOcclusionQuery(scene::ISceneNode* node,
130 const scene::IMesh* mesh=0);
131
132 //! Remove occlusion query.
133 virtual void removeOcclusionQuery(scene::ISceneNode* node);
134
135 //! Run occlusion query. Draws mesh stored in query.
136 /** If the mesh shall not be rendered visible, use
137 overrideMaterial to disable the color and depth buffer. */
138 virtual void runOcclusionQuery(scene::ISceneNode* node, bool visible=false);
139
140 //! Update occlusion query. Retrieves results from GPU.
141 /** If the query shall not block, set the flag to false.
142 Update might not occur in this case, though */
143 virtual void updateOcclusionQuery(scene::ISceneNode* node, bool block=true);
144
145 //! Return query result.
146 /** Return value is the number of visible pixels/fragments.
147 The value is a safe approximation, i.e. can be larger then the
148 actual value of pixels. */
149 virtual u32 getOcclusionQueryResult(scene::ISceneNode* node) const;
150
151 //! draws a vertex primitive list
152 virtual void drawVertexPrimitiveList(const void* vertices, u32 vertexCount,
153 const void* indexList, u32 primitiveCount,
154 E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType,
155 E_INDEX_TYPE iType);
156
157 //! draws a vertex primitive list in 2d
158 virtual void draw2DVertexPrimitiveList(const void* vertices, u32 vertexCount,
159 const void* indexList, u32 primitiveCount,
160 E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType,
161 E_INDEX_TYPE iType);
162
163 //! 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.
164 virtual void draw2DImage(const video::ITexture* texture, const core::position2d<s32>& destPos,
165 const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect = 0,
166 SColor color=SColor(255,255,255,255), bool useAlphaChannelOfTexture=false);
167
168 //! Draws a part of the texture into the rectangle.
169 virtual void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect,
170 const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect = 0,
171 const video::SColor* const colors=0, bool useAlphaChannelOfTexture=false);
172
173 //! Draws a set of 2d images, using a color and the alpha channel of the texture.
174 virtual void draw2DImageBatch(const video::ITexture* texture,
175 const core::array<core::position2d<s32> >& positions,
176 const core::array<core::rect<s32> >& sourceRects,
177 const core::rect<s32>* clipRect=0,
178 SColor color=SColor(255,255,255,255),
179 bool useAlphaChannelOfTexture=false);
180
181 //!Draws an 2d rectangle with a gradient.
182 virtual void draw2DRectangle(const core::rect<s32>& pos,
183 SColor colorLeftUp, SColor colorRightUp, SColor colorLeftDown, SColor colorRightDown,
184 const core::rect<s32>* clip);
185
186 //! Draws a 2d line.
187 virtual void draw2DLine(const core::position2d<s32>& start,
188 const core::position2d<s32>& end,
189 SColor color=SColor(255,255,255,255));
190
191 //! Draws a pixel.
192 virtual void drawPixel(u32 x, u32 y, const SColor & color);
193
194 //! Draws a 3d line.
195 virtual void draw3DLine(const core::vector3df& start,
196 const core::vector3df& end, SColor color = SColor(255,255,255,255));
197
198 //! initialises the Direct3D API
199 bool initDriver(HWND hwnd, bool pureSoftware);
200
201 //! \return Returns the name of the video driver. Example: In case of the DIRECT3D8
202 //! driver, it would return "Direct3D8.1".
203 virtual const wchar_t* getName() const;
204
205 //! deletes all dynamic lights there are
206 virtual void deleteAllDynamicLights();
207
208 //! adds a dynamic light, returning an index to the light
209 //! \param light: the light data to use to create the light
210 //! \return An index to the light, or -1 if an error occurs
211 virtual s32 addDynamicLight(const SLight& light);
212
213 //! Turns a dynamic light on or off
214 //! \param lightIndex: the index returned by addDynamicLight
215 //! \param turnOn: true to turn the light on, false to turn it off
216 virtual void turnLightOn(s32 lightIndex, bool turnOn);
217
218 //! returns the maximal amount of dynamic lights the device can handle
219 virtual u32 getMaximalDynamicLightAmount() const;
220
221 //! Sets the dynamic ambient light color. The default color is
222 //! (0,0,0,0) which means it is dark.
223 //! \param color: New color of the ambient light.
224 virtual void setAmbientLight(const SColorf& color);
225
226 //! Draws a shadow volume into the stencil buffer.
227 virtual void drawStencilShadowVolume(const core::array<core::vector3df>& triangles, bool zfail=true, u32 debugDataVisible=0);
228
229 //! Fills the stencil shadow with color.
230 virtual void drawStencilShadow(bool clearStencilBuffer=false,
231 video::SColor leftUpEdge = video::SColor(0,0,0,0),
232 video::SColor rightUpEdge = video::SColor(0,0,0,0),
233 video::SColor leftDownEdge = video::SColor(0,0,0,0),
234 video::SColor rightDownEdge = video::SColor(0,0,0,0));
235
236 //! Returns the maximum amount of primitives (mostly vertices) which
237 //! the device is able to render with one drawIndexedTriangleList
238 //! call.
239 virtual u32 getMaximalPrimitiveCount() const;
240
241 //! Enables or disables a texture creation flag.
242 virtual void setTextureCreationFlag(E_TEXTURE_CREATION_FLAG flag, bool enabled);
243
244 //! Sets the fog mode.
245 virtual void setFog(SColor color, E_FOG_TYPE fogType, f32 start,
246 f32 end, f32 density, bool pixelFog, bool rangeFog);
247
248 //! Only used by the internal engine. Used to notify the driver that
249 //! the window was resized.
250 virtual void OnResize(const core::dimension2d<u32>& size);
251
252 //! Can be called by an IMaterialRenderer to make its work easier.
253 virtual void setBasicRenderStates(const SMaterial& material, const SMaterial& lastMaterial,
254 bool resetAllRenderstates);
255
256 //! Returns type of video driver
257 virtual E_DRIVER_TYPE getDriverType() const;
258
259 //! Returns the transformation set by setTransform
260 virtual const core::matrix4& getTransform(E_TRANSFORMATION_STATE state) const;
261
262 //! Sets a vertex shader constant.
263 virtual void setVertexShaderConstant(const f32* data, s32 startRegister, s32 constantAmount=1);
264
265 //! Sets a pixel shader constant.
266 virtual void setPixelShaderConstant(const f32* data, s32 startRegister, s32 constantAmount=1);
267
268 //! Sets a constant for the vertex shader based on a name.
269 virtual bool setVertexShaderConstant(const c8* name, const f32* floats, int count);
270
271 //! Bool interface for the above.
272 virtual bool setVertexShaderConstant(const c8* name, const bool* bools, int count);
273
274 //! Int interface for the above.
275 virtual bool setVertexShaderConstant(const c8* name, const s32* ints, int count);
276
277 //! Sets a constant for the pixel shader based on a name.
278 virtual bool setPixelShaderConstant(const c8* name, const f32* floats, int count);
279
280 //! Bool interface for the above.
281 virtual bool setPixelShaderConstant(const c8* name, const bool* bools, int count);
282
283 //! Int interface for the above.
284 virtual bool setPixelShaderConstant(const c8* name, const s32* ints, int count);
285
286 //! Returns a pointer to the IVideoDriver interface. (Implementation for
287 //! IMaterialRendererServices)
288 virtual IVideoDriver* getVideoDriver();
289
290 //! Creates a render target texture.
291 virtual ITexture* addRenderTargetTexture(const core::dimension2d<u32>& size,
292 const io::path& name, const ECOLOR_FORMAT format = ECF_UNKNOWN);
293
294 //! Clears the ZBuffer.
295 virtual void clearZBuffer();
296
297 //! Returns an image created from the last rendered frame.
298 virtual IImage* createScreenShot(video::ECOLOR_FORMAT format=video::ECF_UNKNOWN, video::E_RENDER_TARGET target=video::ERT_FRAME_BUFFER);
299
300 //! Set/unset a clipping plane.
301 virtual bool setClipPlane(u32 index, const core::plane3df& plane, bool enable=false);
302
303 //! Enable/disable a clipping plane.
304 virtual void enableClipPlane(u32 index, bool enable);
305
306 //! Returns the graphics card vendor name.
307 virtual core::stringc getVendorInfo() {return VendorName;}
308
309 //! Enable the 2d override material
310 virtual void enableMaterial2D(bool enable=true);
311
312 //! Check if the driver was recently reset.
313 virtual bool checkDriverReset() {return DriverWasReset;}
314
315 // removes the depth struct from the DepthSurface array
316 void removeDepthSurface(SDepthSurface* depth);
317
318 //! Get the current color format of the color buffer
319 /** \return Color format of the color buffer. */
320 virtual ECOLOR_FORMAT getColorFormat() const;
321
322 //! Returns the maximum texture size supported.
323 virtual core::dimension2du getMaxTextureSize() const;
324
325 //! Get the current color format of the color buffer
326 /** \return Color format of the color buffer as D3D color value. */
327 D3DFORMAT getD3DColorFormat() const;
328
329 //! Get D3D color format from Irrlicht color format.
330 D3DFORMAT getD3DFormatFromColorFormat(ECOLOR_FORMAT format) const;
331
332 //! Get Irrlicht color format from D3D color format.
333 ECOLOR_FORMAT getColorFormatFromD3DFormat(D3DFORMAT format) const;
334
335 //! Get Cg context
336 #ifdef _IRR_COMPILE_WITH_CG_
337 const CGcontext& getCgContext();
338 #endif
339
340 private:
341
342 //! enumeration for rendering modes such as 2d and 3d for minizing the switching of renderStates.
343 enum E_RENDER_MODE
344 {
345 ERM_NONE = 0, // no render state has been set yet.
346 ERM_2D, // 2d drawing rendermode
347 ERM_3D, // 3d rendering mode
348 ERM_STENCIL_FILL, // stencil fill mode
349 ERM_SHADOW_VOLUME_ZFAIL, // stencil volume draw mode
350 ERM_SHADOW_VOLUME_ZPASS // stencil volume draw mode
351 };
352
353 //! sets right vertex shader
354 void setVertexShader(video::E_VERTEX_TYPE newType);
355
356 //! sets the needed renderstates
357 bool setRenderStates3DMode();
358
359 //! sets the needed renderstates
360 void setRenderStates2DMode(bool alpha, bool texture, bool alphaChannel);
361
362 //! sets the needed renderstates
363 void setRenderStatesStencilFillMode(bool alpha);
364
365 //! sets the needed renderstates
366 void setRenderStatesStencilShadowMode(bool zfail, u32 debugDataVisible);
367
368 //! sets the current Texture
369 bool setActiveTexture(u32 stage, const video::ITexture* texture);
370
371 //! resets the device
372 bool reset();
373
374 //! returns a device dependent texture from a software surface (IImage)
375 //! THIS METHOD HAS TO BE OVERRIDDEN BY DERIVED DRIVERS WITH OWN TEXTURES
376 virtual video::ITexture* createDeviceDependentTexture(IImage* surface, const io::path& name, void* mipmapData=0);
377
378 //! returns the current size of the screen or rendertarget
379 virtual const core::dimension2d<u32>& getCurrentRenderTargetSize() const;
380
381 //! Check if a proper depth buffer for the RTT is available, otherwise create it.
382 void checkDepthBuffer(ITexture* tex);
383
384 //! Adds a new material renderer to the VideoDriver, using pixel and/or
385 //! vertex shaders to render geometry.
386 s32 addShaderMaterial(const c8* vertexShaderProgram, const c8* pixelShaderProgram,
387 IShaderConstantSetCallBack* callback,
388 E_MATERIAL_TYPE baseMaterial, s32 userData);
389
390 //! Adds a new material renderer to the VideoDriver, based on a high level shading
391 //! language.
392 virtual s32 addHighLevelShaderMaterial(
393 const c8* vertexShaderProgram,
394 const c8* vertexShaderEntryPointName,
395 E_VERTEX_SHADER_TYPE vsCompileTarget,
396 const c8* pixelShaderProgram,
397 const c8* pixelShaderEntryPointName,
398 E_PIXEL_SHADER_TYPE psCompileTarget,
399 const c8* geometryShaderProgram,
400 const c8* geometryShaderEntryPointName = "main",
401 E_GEOMETRY_SHADER_TYPE gsCompileTarget = EGST_GS_4_0,
402 scene::E_PRIMITIVE_TYPE inType = scene::EPT_TRIANGLES,
403 scene::E_PRIMITIVE_TYPE outType = scene::EPT_TRIANGLE_STRIP,
404 u32 verticesOut = 0,
405 IShaderConstantSetCallBack* callback = 0,
406 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID,
407 s32 userData = 0,
408 E_GPU_SHADING_LANGUAGE shadingLang = EGSL_DEFAULT);
409
410 void createMaterialRenderers();
411
412 void draw2D3DVertexPrimitiveList(const void* vertices,
413 u32 vertexCount, const void* indexList, u32 primitiveCount,
414 E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType,
415 E_INDEX_TYPE iType, bool is3D);
416
417 D3DTEXTUREADDRESS getTextureWrapMode(const u8 clamp);
418
419 inline D3DCOLORVALUE colorToD3D(const SColor& col)
420 {
421 const f32 f = 1.0f / 255.0f;
422 D3DCOLORVALUE v;
423 v.r = col.getRed() * f;
424 v.g = col.getGreen() * f;
425 v.b = col.getBlue() * f;
426 v.a = col.getAlpha() * f;
427 return v;
428 }
429
430 E_RENDER_MODE CurrentRenderMode;
431 D3DPRESENT_PARAMETERS present;
432
433 SMaterial Material, LastMaterial;
434 bool ResetRenderStates; // bool to make all renderstates be reseted if set.
435 bool Transformation3DChanged;
436 const ITexture* CurrentTexture[MATERIAL_MAX_TEXTURES];
437 bool LastTextureMipMapsAvailable[MATERIAL_MAX_TEXTURES];
438 core::matrix4 Matrices[ETS_COUNT]; // matrizes of the 3d mode we need to restore when we switch back from the 2d mode.
439
440 HINSTANCE D3DLibrary;
441 IDirect3D9* pID3D;
442 IDirect3DDevice9* pID3DDevice;
443
444 IDirect3DSurface9* PrevRenderTarget;
445 core::dimension2d<u32> CurrentRendertargetSize;
446
447 HWND WindowId;
448 core::rect<s32>* SceneSourceRect;
449
450 D3DCAPS9 Caps;
451
452 SIrrlichtCreationParameters Params;
453
454 E_VERTEX_TYPE LastVertexType;
455
456 SColorf AmbientLight;
457
458 core::stringc VendorName;
459 u16 VendorID;
460
461 core::array<SDepthSurface*> DepthBuffers;
462
463 u32 MaxTextureUnits;
464 u32 MaxUserClipPlanes;
465 u32 MaxMRTs;
466 u32 NumSetMRTs;
467 f32 MaxLightDistance;
468 s32 LastSetLight;
469
470 enum E_CACHE_2D_ATTRIBUTES
471 {
472 EC2D_ALPHA = 0x1,
473 EC2D_TEXTURE = 0x2,
474 EC2D_ALPHA_CHANNEL = 0x4
475 };
476
477 ECOLOR_FORMAT ColorFormat;
478 D3DFORMAT D3DColorFormat;
479 bool DeviceLost;
480 bool DriverWasReset;
481 bool OcclusionQuerySupport;
482 bool AlphaToCoverageSupport;
483
484 #ifdef _IRR_COMPILE_WITH_CG_
485 CGcontext CgContext;
486 #endif
487 };
488
489
490} // end namespace video
491} // end namespace irr
492
493
494#endif // _IRR_COMPILE_WITH_DIRECT3D_9_
495#endif // __C_VIDEO_DIRECTX_9_H_INCLUDED__
496