OGRE  1.7
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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-2011 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  private:
55  IDirect3D9* mpD3D;
56  // Stored options
58  size_t mFSAASamples;
60 
62  HINSTANCE mhInstance;
63 
74 
77  {
81  size_t coordIndex;
85  const Frustum *frustum;
87  IDirect3DBaseTexture9 *pTex;
89  IDirect3DBaseTexture9 *pVertexTex;
90  } mTexStageDesc[OGRE_MAX_TEXTURE_LAYERS];
91 
92  // Array of up to 8 lights, indexed as per API
93  // Note that a null value indicates a free slot
94  Light* mLights[MAX_LIGHTS];
95  D3D9DriverList* getDirect3DDrivers();
96  void refreshD3DSettings();
97  void refreshFSAAOptions();
98 
99  void setD3D9Light( size_t index, Light* light );
100 
101  // state management methods, very primitive !!!
102  HRESULT __SetRenderState(D3DRENDERSTATETYPE state, DWORD value);
103  HRESULT __SetSamplerState(DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value);
104  HRESULT __SetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value);
105 
106  HRESULT __SetFloatRenderState(D3DRENDERSTATETYPE state, Real value)
107  {
108 #if OGRE_DOUBLE_PRECISION == 1
109  float temp = static_cast<float>(value);
110  return __SetRenderState(state, *((LPDWORD)(&temp)));
111 #else
112  return __SetRenderState(state, *((LPDWORD)(&value)));
113 #endif
114  }
115 
117  DWORD _getCurrentAnisotropy(size_t unit);
119  bool _checkMultiSampleQuality(D3DMULTISAMPLE_TYPE type, DWORD *outQuality, D3DFORMAT format, UINT adapterNum, D3DDEVTYPE deviceType, BOOL fullScreen);
120 
126 
128 
129 
131  virtual RenderSystemCapabilities* createRenderSystemCapabilities() const;
132  RenderSystemCapabilities* updateRenderSystemCapabilities(D3D9RenderWindow* renderWindow);
133 
135  virtual void initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary);
136 
137 
138  void convertVertexShaderCaps(RenderSystemCapabilities* rsc) const;
139  void convertPixelShaderCaps(RenderSystemCapabilities* rsc) const;
140  bool checkVertexTextureFormats(D3D9RenderWindow* renderWindow) const;
141 
142  HashMap<IDirect3DDevice9*, unsigned short> mCurrentLights;
145 
146  D3DXMATRIX mDxViewMat, mDxProjMat, mDxWorldMat;
147 
149  // List of additional windows after the first (swap chains)
151 
154  typedef HashMap<unsigned int, D3DFORMAT> DepthStencilHash;
156 
163  {
164  IDirect3DDevice9* device;
165  D3DFORMAT format;
166  D3DMULTISAMPLE_TYPE multisampleType;
167  };
168  struct ZBufferRef
169  {
170  IDirect3DSurface9 *surface;
171  size_t width, height;
172  };
174  {
175  bool operator()(const ZBufferIdentifier& z0, const ZBufferIdentifier& z1) const;
176  };
177 
181 
182  protected:
183  void setClipPlanesImpl(const PlaneList& clipPlanes);
184  public:
185  // constructor
186  D3D9RenderSystem( HINSTANCE hInstance );
187  // destructor
188  ~D3D9RenderSystem();
189 
190  virtual void initConfigOptions();
191 
192  // Overridden RenderSystem functions
193  ConfigOptionMap& getConfigOptions();
194  String validateConfigOptions();
195  RenderWindow* _initialise( bool autoCreateWindow, const String& windowTitle = "OGRE Render Window" );
197  RenderWindow* _createRenderWindow(const String &name, unsigned int width, unsigned int height,
198  bool fullScreen, const NameValuePairList *miscParams = 0);
199 
201  bool _createRenderWindows(const RenderWindowDescriptionList& renderWindowDescriptions,
202  RenderWindowList& createdWindows);
203 
207  void _setRenderTarget(RenderTarget *target);
208 
210  virtual MultiRenderTarget * createMultiRenderTarget(const String & name);
211 
212  String getErrorDescription( long errorNumber ) const;
213  const String& getName() const;
214  // Low-level overridden members
215  void setConfigOption( const String &name, const String &value );
216  void reinitialise();
217  void shutdown();
218  void setAmbientLight( float r, float g, float b );
219  void setShadingType( ShadeOptions so );
220  void setLightingEnabled( bool enabled );
221  void destroyRenderTarget(const String& name);
222  VertexElementType getColourVertexElementType() const;
223  void setStencilCheckEnabled(bool enabled);
224  void setStencilBufferParams(CompareFunction func = CMPF_ALWAYS_PASS,
225  uint32 refValue = 0, uint32 mask = 0xFFFFFFFF,
226  StencilOperation stencilFailOp = SOP_KEEP,
227  StencilOperation depthFailOp = SOP_KEEP,
228  StencilOperation passOp = SOP_KEEP,
229  bool twoSidedOperation = false);
230  void setNormaliseNormals(bool normalise);
231 
232  // Low-level overridden members, mainly for internal use
233  void _useLights(const LightList& lights, unsigned short limit);
234  void _setWorldMatrix( const Matrix4 &m );
235  void _setViewMatrix( const Matrix4 &m );
236  void _setProjectionMatrix( const Matrix4 &m );
237  void _setSurfaceParams( const ColourValue &ambient, const ColourValue &diffuse, const ColourValue &specular, const ColourValue &emissive, Real shininess, TrackVertexColourType tracking );
238  void _setPointSpritesEnabled(bool enabled);
239  void _setPointParameters(Real size, bool attenuationEnabled,
240  Real constant, Real linear, Real quadratic, Real minSize, Real maxSize);
241  void _setTexture(size_t unit, bool enabled, const TexturePtr &texPtr);
242  void _setVertexTexture(size_t unit, const TexturePtr& tex);
243  void _disableTextureUnit(size_t texUnit);
244  void _setTextureCoordSet( size_t unit, size_t index );
245  void _setTextureCoordCalculation(size_t unit, TexCoordCalcMethod m,
246  const Frustum* frustum = 0);
247  void _setTextureBlendMode( size_t unit, const LayerBlendModeEx& bm );
248  void _setTextureAddressingMode(size_t stage, const TextureUnitState::UVWAddressingMode& uvw);
249  void _setTextureBorderColour(size_t stage, const ColourValue& colour);
250  void _setTextureMipmapBias(size_t unit, float bias);
251  void _setTextureMatrix( size_t unit, const Matrix4 &xform );
252  void _setSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op );
253  void _setSeparateSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha, SceneBlendOperation op, SceneBlendOperation alphaOp );
254  void _setAlphaRejectSettings( CompareFunction func, unsigned char value, bool alphaToCoverage );
255  void _setViewport( Viewport *vp );
256  void _beginFrame();
257  virtual RenderSystemContext* _pauseFrame(void);
258  virtual void _resumeFrame(RenderSystemContext* context);
259  void _endFrame();
260  void _setCullingMode( CullingMode mode );
261  void _setDepthBufferParams( bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL );
262  void _setDepthBufferCheckEnabled( bool enabled = true );
263  void _setColourBufferWriteEnabled(bool red, bool green, bool blue, bool alpha);
264  void _setDepthBufferWriteEnabled(bool enabled = true);
265  void _setDepthBufferFunction( CompareFunction func = CMPF_LESS_EQUAL );
266  void _setDepthBias(float constantBias, float slopeScaleBias);
267  void _setFog( FogMode mode = FOG_NONE, const ColourValue& colour = ColourValue::White, Real expDensity = 1.0, Real linearStart = 0.0, Real linearEnd = 1.0 );
268  void _convertProjectionMatrix(const Matrix4& matrix,
269  Matrix4& dest, bool forGpuProgram = false);
270  void _makeProjectionMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane,
271  Matrix4& dest, bool forGpuProgram = false);
272  void _makeProjectionMatrix(Real left, Real right, Real bottom, Real top, Real nearPlane,
273  Real farPlane, Matrix4& dest, bool forGpuProgram = false);
274  void _makeOrthoMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane,
275  Matrix4& dest, bool forGpuProgram = false);
276  void _applyObliqueDepthProjection(Matrix4& matrix, const Plane& plane,
277  bool forGpuProgram);
278  void _setPolygonMode(PolygonMode level);
279  void _setTextureUnitFiltering(size_t unit, FilterType ftype, FilterOptions filter);
280  void _setTextureLayerAnisotropy(size_t unit, unsigned int maxAnisotropy);
281  void setVertexDeclaration(VertexDeclaration* decl);
282  void setVertexBufferBinding(VertexBufferBinding* binding);
283  void _render(const RenderOperation& op);
287  void bindGpuProgram(GpuProgram* prg);
291  void unbindGpuProgram(GpuProgramType gptype);
295  void bindGpuProgramParameters(GpuProgramType gptype,
296  GpuProgramParametersSharedPtr params, uint16 variabilityMask);
297  void bindGpuProgramPassIterationParameters(GpuProgramType gptype);
298 
299  void setScissorTest(bool enabled, size_t left = 0, size_t top = 0, size_t right = 800, size_t bottom = 600);
300  void clearFrameBuffer(unsigned int buffers,
301  const ColourValue& colour = ColourValue::Black,
302  Real depth = 1.0f, unsigned short stencil = 0);
303  void setClipPlane (ushort index, Real A, Real B, Real C, Real D);
304  void enableClipPlane (ushort index, bool enable);
305  HardwareOcclusionQuery* createHardwareOcclusionQuery();
306  Real getHorizontalTexelOffset();
307  Real getVerticalTexelOffset();
308  Real getMinimumDepthInputValue();
309  Real getMaximumDepthInputValue();
310  void registerThread();
311  void unregisterThread();
312  void preExtraThreadsStarted();
313  void postExtraThreadsStarted();
314 
315  static D3D9ResourceManager* getResourceManager();
316  static D3D9DeviceManager* getDeviceManager();
317  static IDirect3D9* getDirect3D9();
318  static UINT getResourceCreationDeviceCount();
319  static IDirect3DDevice9* getResourceCreationDevice(UINT index);
320  static IDirect3DDevice9* getActiveD3D9Device();
321 
325  ZBufferIdentifier getZBufferIdentifier(RenderTarget* rt);
326 
330  D3DFORMAT _getDepthStencilFormatFor(D3DFORMAT fmt);
331 
336  IDirect3DSurface9* _getDepthStencilFor(D3DFORMAT fmt, D3DMULTISAMPLE_TYPE multisample, DWORD multisample_quality, size_t width, size_t height);
337 
340  void _cleanupDepthStencils(IDirect3DDevice9* d3d9Device);
341 
345  bool _checkTextureFilteringSupported(TextureType ttype, PixelFormat format, int usage);
346 
348  void determineFSAASettings(IDirect3DDevice9* d3d9Device, size_t fsaa, const String& fsaaHint, D3DFORMAT d3dPixelFormat,
349  bool fullScreen, D3DMULTISAMPLE_TYPE *outMultisampleType, DWORD *outMultisampleQuality);
350 
352  unsigned int getDisplayMonitorCount() const;
353 
354  protected:
356  void notifyOnDeviceLost(D3D9Device* device);
357 
359  void notifyOnDeviceReset(D3D9Device* device);
360 
363 
364  private:
365  friend class D3D9Device;
366  friend class D3D9DeviceManager;
367  };
368 }
369 #endif
Manages the target rendering window.
A 'canvas' which can receive the results of a rendering operation.
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:246
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:524
Defines a plane in 3D space.
Definition: OgrePlane.h:61
FogMode
Fog modes.
Definition: OgreCommon.h:122
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:84
eD3DTexType
enum identifying D3D9 tex. types
#define MAX_LIGHTS
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:91
Mapping of depthstencil format -> depthstencil buffer Keep one depthstencil buffer around for every f...
vector< D3D9RenderWindow * >::type D3D9RenderWindowList
structure holding texture unit settings for every stage
SceneBlendFactor
Blending factors for manually blending objects with the scene.
bool mUseNVPerfHUD
NVPerfHUD allowed?
Device manager interface.
D3D9GpuProgramManager * mGpuProgramManager
D3D9Mappings::eD3DTexType texType
the type of the texture
CullingMode
Hardware culling modes based on vertex winding.
Definition: OgreCommon.h:136
No fog. Duh.
Definition: OgreCommon.h:125
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:738
static D3D9RenderSystem * msD3D9RenderSystem
Fast singleton access.
Representation of a dynamic light source in the scene.
Definition: OgreLight.h:72
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:70
ShadeOptions
Light shading modes.
Definition: OgreCommon.h:114
TargetDepthStencilMap mCheckedOutTextures
TexCoordCalcMethod autoTexCoordType
type of auto tex. calc. used
PixelFormat
The pixel format used for images, textures, and render surfaces.
IDirect3D9 * mpD3D
Direct3D.
Texture addressing mode for each texture coordinate.
map< String, ConfigOption >::type ConfigOptionMap
#define _OgreD3D9Export
Specialisation of SharedPtr to allow SharedPtr to be assigned to TexturePtr.
Definition: OgreTexture.h:440
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.
const Frustum * frustum
Frustum, used if the above is projection.
Implementation of DirectX9 as a rendering system.
unsigned short ushort
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:56
'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:46
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:180
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.
map< ZBufferIdentifier, ZBufferRefQueue, ZBufferIdentifierComparator >::type ZBufferHash
singleton class for storing the capabilities of the graphics card.
_StringBase String
deque< ZBufferRef >::type ZBufferRefQueue
vector< RenderWindowDescription >::type RenderWindowDescriptionList
Render window creation parameters container.
Definition: OgreCommon.h:735
unsigned short uint16
Definition: OgrePlatform.h:247
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 onthe stencil buffer.
FilterOptions
Filtering options for textures / mipmaps.
Definition: OgreCommon.h:101
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:65
int TrackVertexColourType
An enumeration describing which material properties should track the vertex colours.
Definition: OgreCommon.h:277
Class which manages blending of both colour and alpha components.
DepthStencilHash mDepthStencilHash
map< RenderTarget *, ZBufferRef >::type TargetDepthStencilMap
D3D9RenderWindowList mRenderWindows
std::deque< T, A > type