OGRE  1.9
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreD3D9RenderSystem.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __D3D9RENDERSYSTEM_H__
29 #define __D3D9RENDERSYSTEM_H__
30 
31 #include "OgreD3D9Prerequisites.h"
32 #include "OgreString.h"
33 #include "OgreStringConverter.h"
34 #include "OgreRenderSystem.h"
36 #include "OgreD3D9Mappings.h"
37 
38 namespace Ogre
39 {
40 #define MAX_LIGHTS 8
41 
42  class D3D9DriverList;
43  class D3D9Driver;
44  class D3D9Device;
45  class D3D9DeviceManager;
46  class D3D9ResourceManager;
47 
52  {
53  public:
55  {
58  mutNo
59  };
60 
61  private:
63  IDirect3D9* mD3D;
64  // Stored options
66  size_t mFSAASamples;
68 
70  HINSTANCE mhInstance;
71 
88 
91  {
95  size_t coordIndex;
99  const Frustum *frustum;
101  IDirect3DBaseTexture9 *pTex;
103  IDirect3DBaseTexture9 *pVertexTex;
104  } mTexStageDesc[OGRE_MAX_TEXTURE_LAYERS];
105 
106  // Array of up to 8 lights, indexed as per API
107  // Note that a null value indicates a free slot
108  Light* mLights[MAX_LIGHTS];
109  D3D9DriverList* getDirect3DDrivers();
110  void refreshD3DSettings();
111  void refreshFSAAOptions();
112 
113  void setD3D9Light( size_t index, Light* light );
114 
115  // state management methods, very primitive !!!
116  HRESULT __SetRenderState(D3DRENDERSTATETYPE state, DWORD value);
117  HRESULT __SetSamplerState(DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value);
118  HRESULT __SetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value);
119 
120  HRESULT __SetFloatRenderState(D3DRENDERSTATETYPE state, Real value)
121  {
122 #if OGRE_DOUBLE_PRECISION == 1
123  float temp = static_cast<float>(value);
124  return __SetRenderState(state, *((LPDWORD)(&temp)));
125 #else
126  return __SetRenderState(state, *((LPDWORD)(&value)));
127 #endif
128  }
129 
131  DWORD _getCurrentAnisotropy(size_t unit);
133  bool _checkMultiSampleQuality(D3DMULTISAMPLE_TYPE type, DWORD *outQuality, D3DFORMAT format, UINT adapterNum, D3DDEVTYPE deviceType, BOOL fullScreen);
134 
140 
142 
143 
145  virtual RenderSystemCapabilities* createRenderSystemCapabilities() const;
146  RenderSystemCapabilities* updateRenderSystemCapabilities(D3D9RenderWindow* renderWindow);
147 
149  virtual void initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary);
150 
151 
152  void convertVertexShaderCaps(RenderSystemCapabilities* rsc) const;
153  void convertPixelShaderCaps(RenderSystemCapabilities* rsc) const;
154  bool checkVertexTextureFormats(D3D9RenderWindow* renderWindow) const;
155  void detachRenderTargetImpl(const String& name);
156 
157  HashMap<IDirect3DDevice9*, unsigned short> mCurrentLights;
160 
161  D3DXMATRIX mDxViewMat, mDxProjMat, mDxWorldMat;
162 
164  // List of additional windows after the first (swap chains)
166 
169  typedef HashMap<unsigned int, D3DFORMAT> DepthStencilHash;
171 
173 
174  protected:
175  void setClipPlanesImpl(const PlaneList& clipPlanes);
176  public:
177  // constructor
178  D3D9RenderSystem( HINSTANCE hInstance );
179  // destructor
180  ~D3D9RenderSystem();
181 
182  virtual void initConfigOptions();
183 
184  // Overridden RenderSystem functions
185  ConfigOptionMap& getConfigOptions();
186  String validateConfigOptions();
187  RenderWindow* _initialise( bool autoCreateWindow, const String& windowTitle = "OGRE Render Window" );
189  RenderWindow* _createRenderWindow(const String &name, unsigned int width, unsigned int height,
190  bool fullScreen, const NameValuePairList *miscParams = 0);
191 
193  bool _createRenderWindows(const RenderWindowDescriptionList& renderWindowDescriptions,
194  RenderWindowList& createdWindows);
195 
197  DepthBuffer* _createDepthBufferFor( RenderTarget *renderTarget );
198 
204  DepthBuffer* _addManualDepthBuffer( IDirect3DDevice9* depthSurfaceDevice, IDirect3DSurface9 *surf );
205 
216  void _cleanupDepthBuffers( IDirect3DDevice9 *creator );
217 
225  void _cleanupDepthBuffers( IDirect3DSurface9 *manualSurface );
226 
230  void _setRenderTarget(RenderTarget *target);
231 
233  virtual MultiRenderTarget * createMultiRenderTarget(const String & name);
234 
236  virtual RenderTarget * detachRenderTarget(const String &name);
237 
238  String getErrorDescription( long errorNumber ) const;
239  const String& getName() const;
240  // Low-level overridden members
241  void setConfigOption( const String &name, const String &value );
242  void reinitialise();
243  void shutdown();
244  void setAmbientLight( float r, float g, float b );
245  void setShadingType( ShadeOptions so );
246  void setLightingEnabled( bool enabled );
247  void destroyRenderTarget(const String& name);
248  VertexElementType getColourVertexElementType() const;
249  void setStencilCheckEnabled(bool enabled);
250  void setStencilBufferParams(CompareFunction func = CMPF_ALWAYS_PASS,
251  uint32 refValue = 0, uint32 compareMask = 0xFFFFFFFF, uint32 writeMask = 0xFFFFFFFF,
252  StencilOperation stencilFailOp = SOP_KEEP,
253  StencilOperation depthFailOp = SOP_KEEP,
254  StencilOperation passOp = SOP_KEEP,
255  bool twoSidedOperation = false);
256  void setNormaliseNormals(bool normalise);
257 
258  // Low-level overridden members, mainly for internal use
259  void _useLights(const LightList& lights, unsigned short limit);
260  void _setWorldMatrix( const Matrix4 &m );
261  void _setViewMatrix( const Matrix4 &m );
262  void _setProjectionMatrix( const Matrix4 &m );
263  void _setSurfaceParams( const ColourValue &ambient, const ColourValue &diffuse, const ColourValue &specular, const ColourValue &emissive, Real shininess, TrackVertexColourType tracking );
264  void _setPointSpritesEnabled(bool enabled);
265  void _setPointParameters(Real size, bool attenuationEnabled,
266  Real constant, Real linear, Real quadratic, Real minSize, Real maxSize);
267  void _setTexture(size_t unit, bool enabled, const TexturePtr &texPtr);
268  void _setVertexTexture(size_t unit, const TexturePtr& tex);
269  void _disableTextureUnit(size_t texUnit);
270  void _setTextureCoordSet( size_t unit, size_t index );
271  void _setTextureCoordCalculation(size_t unit, TexCoordCalcMethod m,
272  const Frustum* frustum = 0);
273  void _setTextureBlendMode( size_t unit, const LayerBlendModeEx& bm );
274  void _setTextureAddressingMode(size_t stage, const TextureUnitState::UVWAddressingMode& uvw);
275  void _setTextureBorderColour(size_t stage, const ColourValue& colour);
276  void _setTextureMipmapBias(size_t unit, float bias);
277  void _setTextureMatrix( size_t unit, const Matrix4 &xform );
278  void _setSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op );
279  void _setSeparateSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha, SceneBlendOperation op, SceneBlendOperation alphaOp );
280  void _setAlphaRejectSettings( CompareFunction func, unsigned char value, bool alphaToCoverage );
281  void _setViewport( Viewport *vp );
282  void _beginFrame();
283  virtual RenderSystemContext* _pauseFrame(void);
284  virtual void _resumeFrame(RenderSystemContext* context);
285  void _endFrame();
286  void _setCullingMode( CullingMode mode );
287  void _setDepthBufferParams( bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL );
288  void _setDepthBufferCheckEnabled( bool enabled = true );
289  void _setColourBufferWriteEnabled(bool red, bool green, bool blue, bool alpha);
290  void _setDepthBufferWriteEnabled(bool enabled = true);
291  void _setDepthBufferFunction( CompareFunction func = CMPF_LESS_EQUAL );
292  void _setDepthBias(float constantBias, float slopeScaleBias);
293  void _setFog( FogMode mode = FOG_NONE, const ColourValue& colour = ColourValue::White, Real expDensity = 1.0, Real linearStart = 0.0, Real linearEnd = 1.0 );
294  void _convertProjectionMatrix(const Matrix4& matrix,
295  Matrix4& dest, bool forGpuProgram = false);
296  void _makeProjectionMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane,
297  Matrix4& dest, bool forGpuProgram = false);
298  void _makeProjectionMatrix(Real left, Real right, Real bottom, Real top, Real nearPlane,
299  Real farPlane, Matrix4& dest, bool forGpuProgram = false);
300  void _makeOrthoMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane,
301  Matrix4& dest, bool forGpuProgram = false);
302  void _applyObliqueDepthProjection(Matrix4& matrix, const Plane& plane,
303  bool forGpuProgram);
304  void _setPolygonMode(PolygonMode level);
305  void _setTextureUnitFiltering(size_t unit, FilterType ftype, FilterOptions filter);
306  void _setTextureUnitCompareFunction(size_t unit, CompareFunction function);
307  void _setTextureUnitCompareEnabled(size_t unit, bool compare);
308  void _setTextureLayerAnisotropy(size_t unit, unsigned int maxAnisotropy);
309  void setVertexDeclaration(VertexDeclaration* decl);
310  void setVertexDeclaration(VertexDeclaration* decl, bool useGlobalInstancingVertexBufferIsAvailable);
311  void setVertexBufferBinding(VertexBufferBinding* binding);
312  void setVertexBufferBinding(VertexBufferBinding* binding, size_t numberOfInstances, bool useGlobalInstancingVertexBufferIsAvailable, bool indexesUsed);
313  void _render(const RenderOperation& op);
317  void bindGpuProgram(GpuProgram* prg);
321  void unbindGpuProgram(GpuProgramType gptype);
325  void bindGpuProgramParameters(GpuProgramType gptype,
326  GpuProgramParametersSharedPtr params, uint16 variabilityMask);
327  void bindGpuProgramPassIterationParameters(GpuProgramType gptype);
328 
329  void setScissorTest(bool enabled, size_t left = 0, size_t top = 0, size_t right = 800, size_t bottom = 600);
330  void clearFrameBuffer(unsigned int buffers,
331  const ColourValue& colour = ColourValue::Black,
332  Real depth = 1.0f, unsigned short stencil = 0);
333  void setClipPlane (ushort index, Real A, Real B, Real C, Real D);
334  void enableClipPlane (ushort index, bool enable);
335  HardwareOcclusionQuery* createHardwareOcclusionQuery();
336  Real getHorizontalTexelOffset();
337  Real getVerticalTexelOffset();
338  Real getMinimumDepthInputValue();
339  Real getMaximumDepthInputValue();
340  void registerThread();
341  void unregisterThread();
342  void preExtraThreadsStarted();
343  void postExtraThreadsStarted();
344 
345  /*
346  Returns whether under the current render system buffers marked as TU_STATIC can be locked for update
347  */
348  virtual bool isStaticBufferLockable() const { return !mIsDirectX9Ex; }
349 
352  static bool isDirectX9Ex() { return msD3D9RenderSystem->mIsDirectX9Ex; }
353 
354  static D3D9ResourceManager* getResourceManager();
355  static D3D9DeviceManager* getDeviceManager();
356  static IDirect3D9* getDirect3D9();
357  static UINT getResourceCreationDeviceCount();
358  static IDirect3DDevice9* getResourceCreationDevice(UINT index);
359  static IDirect3DDevice9* getActiveD3D9Device();
360 
364  D3DFORMAT _getDepthStencilFormatFor(D3DFORMAT fmt);
365 
369  bool _checkTextureFilteringSupported(TextureType ttype, PixelFormat format, int usage);
370 
372  void determineFSAASettings(IDirect3DDevice9* d3d9Device, size_t fsaa, const String& fsaaHint, D3DFORMAT d3dPixelFormat,
373  bool fullScreen, D3DMULTISAMPLE_TYPE *outMultisampleType, DWORD *outMultisampleQuality);
374 
376  unsigned int getDisplayMonitorCount() const;
377 
379  virtual bool hasAnisotropicMipMapFilter() const { return false; }
380 
382  virtual void beginProfileEvent( const String &eventName );
383 
385  virtual void endProfileEvent( void );
386 
388  virtual void markProfileEvent( const String &eventName );
389 
391  void fireDeviceEvent( D3D9Device* device, const String & name );
392 
394  MultiheadUseType getMultiheadUse() const { return mMultiheadUse; }
395  protected:
397  DWORD getSamplerId(size_t unit);
398 
400  void notifyOnDeviceLost(D3D9Device* device);
401 
403  void notifyOnDeviceReset(D3D9Device* device);
404 
405  private:
406  friend class D3D9Device;
407  friend class D3D9DeviceManager;
408  };
409 }
410 #endif
Manages the target rendering window.
A 'canvas' which can receive the results of a rendering operation.
static bool isDirectX9Ex()
Tells whether the system is initialized with DirectX 9Ex driver Read more in http://msdn.microsoft.com/en-us/library/windows/desktop/ee890072(v=vs.85).aspx.
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: OgreMatrix4.h:78
HashMap< IDirect3DDevice9 *, unsigned short > mCurrentLights
float Real
Software floating point type.
unsigned int uint32
Definition: OgrePlatform.h:344
Records the state of all the vertex buffer bindings required to provide a vertex declaration with the...
map< String, String >::type NameValuePairList
Name / value parameter pair (first = name, second = value)
Definition: OgreCommon.h:550
Defines a plane in 3D space.
Definition: OgrePlane.h:61
FogMode
Fog modes.
Definition: OgreCommon.h:121
Leave the stencil buffer unchanged.
Class representing colour.
GpuProgramType
Enumerates the types of programs which can run on the GPU.
static const ColourValue White
A frustum represents a pyramid, capped at the near and far end which is used to represent either a vi...
Definition: OgreFrustum.h:85
eD3DTexType
enum identifying D3D9 tex. types
#define MAX_LIGHTS
virtual bool isStaticBufferLockable() const
Defines a program which runs on the GPU such as a vertex or fragment program.
TexCoordCalcMethod
Enum describing the ways to generate texture coordinates.
IDirect3DBaseTexture9 * pVertexTex
vertex texture
FilterType
Definition: OgreCommon.h:90
vector< D3D9RenderWindow * >::type D3D9RenderWindowList
structure holding texture unit settings for every stage
SceneBlendFactor
Blending factors for manually blending objects with the scene.
bool mAllowDirectX9Ex
Tells whether to attempt to initialize the system with DirectX 9Ex driver Read more in http://msdn...
bool mUseNVPerfHUD
NVPerfHUD allowed?
Device manager interface.
virtual bool hasAnisotropicMipMapFilter() const
Determines if the system has anisotropic mip map filter support.
D3D9GpuProgramManager * mGpuProgramManager
D3D9Mappings::eD3DTexType texType
the type of the texture
CullingMode
Hardware culling modes based on vertex winding.
Definition: OgreCommon.h:135
No fog. Duh.
Definition: OgreCommon.h:124
D3D9HLSLProgramFactory * mHLSLProgramFactory
This class represents a render target that renders to multiple RenderTextures at once.
This is a abstract class that that provides the interface for the query class for hardware occlusion...
vector< RenderWindow * >::type RenderWindowList
Render window container.
Definition: OgreCommon.h:764
static D3D9RenderSystem * msD3D9RenderSystem
Fast singleton access.
Representation of a dynamic light source in the scene.
Definition: OgreLight.h:73
vector< Plane >::type PlaneList
Definition: OgrePlane.h:160
#define OGRE_MAX_TEXTURE_LAYERS
Define max number of texture layers allowed per pass on any card.
Definition: OgreConfig.h:72
MultiheadUseType getMultiheadUse() const
Returns how multihead should be activated.
An abstract class that contains a depth/stencil buffer.
ShadeOptions
Light shading modes.
Definition: OgreCommon.h:113
TexCoordCalcMethod autoTexCoordType
type of auto tex. calc. used
PixelFormat
The pixel format used for images, textures, and render surfaces.
Texture addressing mode for each texture coordinate.
map< String, ConfigOption >::type ConfigOptionMap
#define _OgreD3D9Export
bool mIsDirectX9Ex
Tells whether the system is initialized with DirectX 9Ex driver Read more in http://msdn.microsoft.com/en-us/library/windows/desktop/ee890072(v=vs.85).aspx.
D3D9ResourceManager * mResourceManager
VertexElementType
Vertex element type, used to identify the base types of the vertex contents.
HRESULT __SetFloatRenderState(D3DRENDERSTATETYPE state, Real value)
D3D9HardwareBufferManagerBase as a Singleton.
MultiheadUseType mMultiheadUse
const Frustum * frustum
Frustum, used if the above is projection.
Implementation of DirectX9 as a rendering system.
unsigned short ushort
IDirect3D9 * mD3D
Direct3D.
HashMap< unsigned int, D3DFORMAT > DepthStencilHash
Mapping of texture format -> DepthStencil.
High level interface of Direct3D9 Device.
An abstraction of a viewport, i.e.
Definition: OgreViewport.h:57
'New' rendering operation using vertex buffers.
This class declares the format of a set of vertex inputs, which can be issued to the rendering API th...
D3D9HardwareBufferManager * mHardwareBufferManager
Wrapper class which indicates a given angle value is in Radians.
Definition: OgreMath.h:47
size_t coordIndex
which texCoordIndex to use
TextureType
Enum identifying the texture type.
Definition: OgreTexture.h:67
PolygonMode
The polygon mode to use when rasterising.
Definition: OgreCommon.h:179
HINSTANCE mhInstance
instance
bool mPerStageConstantSupport
Per-stage constant support? (not in main caps since D3D specific & minor)
static const ColourValue Black
SceneBlendOperation
Blending operations controls how objects are blended into the scene.
Matrix4 mViewMatrix
Saved last view matrix.
singleton class for storing the capabilities of the graphics card.
_StringBase String
vector< RenderWindowDescription >::type RenderWindowDescriptionList
Render window creation parameters container.
Definition: OgreCommon.h:761
unsigned short uint16
Definition: OgrePlatform.h:345
D3D9DriverList * mDriverList
List of D3D drivers installed (video cards)
D3D9DeviceManager * mDeviceManager
Factory class for D3D9 HLSL programs.
StencilOperation
Enum describing the various actions which can be taken on the stencil buffer.
FilterOptions
Filtering options for textures / mipmaps.
Definition: OgreCommon.h:100
Defines the functionality of a 3D API.
D3D9Driver * mActiveD3DDriver
Currently active driver.
CompareFunction
Comparison functions used for the depth/stencil buffer operations and others.
Definition: OgreCommon.h:64
int TrackVertexColourType
An enumeration describing which material properties should track the vertex colours.
Definition: OgreCommon.h:276
Class which manages blending of both colour and alpha components.
void _cleanupDepthBuffers(bool bCleanManualBuffers=true)
Removes all depth buffers.
DepthStencilHash mDepthStencilHash
D3D9RenderWindowList mRenderWindows