Main Page Modules Namespace List Class Hierarchy Alphabetical List Compound List File List Namespace Members Compound Members File Members Related Pages

# DDirect3D9.hpp

Go to the documentation of this file.

    00001 ///////////////////////////////////////////////////////////////////////////////
    00002 /// @file DDirect3D9.hpp
    00003 ///
    00004 /// @brief class for Direct3D9 driver
    00005 ///
    00006 /// @author Lightning, Assassin, ibutsu
    00007 ///
    00008 /// This file is the intellectual property of Novus Delta, LLC.. Usage of the
    00009 /// contents of this file is subject to the Destiny3D Member License which
    00010 /// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
    00011 ///
    00012 /// This file is distributed "AS IS" without warranty of any kind.  Novus
    00013 /// Delta, LLC. does not guarantee the fitness of the contents of this file
    00014 /// for any particular purpose.
    00015 ///
    00016 /// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
    00017 ///
    00018 /// <hr>
    00019 ///                                 Change History
    00020 /// <hr>
    00021 ///
    00022 /// @date Aug 2001
    00023 /// @author Lightning
    00024 /// @remarks Creation
    00025 ///
    00026 /// @date May 2002
    00027 /// @author Assassin
    00028 /// @remarks Sub-VBuffer modifications
    00029 ///
    00030 /// @date Dec 2003
    00031 /// @author ibutsu
    00032 /// @remarks d3d9 port
    00033 ///
    00034 ///////////////////////////////////////////////////////////////////////////////
    00035
    00036 #ifndef DDIRECT3D_HPP
    00037 #define DDIRECT3D_HPP
    00038
    00039 //#pragma comment(lib, "d3d8.lib") // done dynamically
    00040 //#pragma comment(lib, "d3d9.lib")
    00041
    00042 #pragma comment(lib, "d3dx9")
    00043
    00044 #define DEDRIVER_DLL_EXPORTS
    00045 #define USING_DESTINY3D
    00046
    00047 //needed global headers
    00048 #include <d3d9.h>
    00049 #include "Destiny3D.hpp"
    00050 #include "deGlobalTypes.hpp"
    00051
    00052 //include the needed driver headers
    00053 #include "deDriver_priv.hpp"
    00054
    00055 #include "deHash.hpp"
    00056 #include "deList.hpp"
    00057
    00058 static const long deD3D9_MAX_LIGHTS = 8;
    00059
    00060 //the Direct3D class
    00061 class deDirect3D9 : public deDriver
    00062 {
    00063 public:
    00064     //constructor and destructor
    00065     deDirect3D9(long ID);
    00066     ~deDirect3D9();
    00067     long Release();
    00068     deBoolean Init(HWND WindowHandle);
    00069
    00070     //adapter and mode counts for adapters
    00071     const deDriverCaps* GetCaps(long Adapter) const;
    00072     long GetAdapterCount() const;
    00073     long GetModeCount(long Adapter) const;
    00074     deBoolean WindowedAllowed(long Adapter) const;
    00075
    00076     //display settings
    00077     deBoolean SetDisplay(deDisplay *Display);
    00078     deBoolean Reset(deDisplay *Display = NULL);
    00079     deBoolean GetCurrentDisplay(long Adapter, deDisplay * Display) const;
    00080     deBoolean GetDisplayMode(long Adapter, long Mode, deDisplay *Display) const;
    00081     deBoolean ValidFormat(long Adapter, Resource ResourceType, BPP BPPType, ZPP ZPPType, BPP FrontBuffer, long ExtraFlag) const;
    00082
    00083     //vertex buffer functions
    00084     deBoolean AddVertexBuffer(IdeVertexBuffer *Vertex);
    00085     deBoolean RemoveVertexBuffer(IdeVertexBuffer *Vertex);
    00086     deBoolean UpdateVertexBuffer(IdeVertexBuffer *Vertex);
    00087
    00088     // Shader functions
    00089     deBoolean AddShader(IdeShader * Shader);
    00090     deBoolean SetShader(IdeShader * Shader);
    00091     deBoolean RemoveShader(IdeShader * Shader);
    00092
    00093     //frame functions
    00094     deBoolean BeginFrame();
    00095     deBoolean EndFrame();
    00096     deBoolean ClearFrame(deARGB Color, deBoolean ClearTarget, deBoolean ClearZBuffer, deBoolean ClearStencil);
    00097     deBoolean Render(IdeVertexBuffer *Vertex, long IndexOffset, IdeVertexBuffer *IndexOverride);
    00098
    00099     //viewport/FOV functions
    00100     deBoolean SetViewport(long X, long Y, long Width, long Height, float MinZ, float MaxZ);
    00101     deBoolean SetDepthRange(deFloat MinZ, deFloat MaxZ);
    00102     deBoolean SetFOVClipping(float FOVY, float Aspect, float ClipNear, float ClipFar);
    00103     deBoolean SetOrthoClipping(float Width, float Height, float ClipNear, float ClipFar, deBoolean Invert);
    00104     deBoolean SetFOVClippingSubRect(deFloat FOVY, deFloat Aspect, deFloat ClipNear, deFloat ClipFar, deRect SubRect);
    00105     deBoolean SetFOVClippingSubRectFloat(deFloat FOVY, deFloat Aspect, deFloat ClipNear, deFloat ClipFar, deFloatRect SubRect);
    00106     deBoolean SetOrthoClippingSubRect(deFloat Width, deFloat Height, deFloat ClipNear, deFloat ClipFar, deBoolean Invert, deRect SubRect);
    00107     deBoolean SetOrthoClippingSubRectFloat(deFloat Width, deFloat Height, deFloat ClipNear, deFloat ClipFar, deBoolean Invert, deFloatRect SubRect);
    00108     deBoolean SetScissorRect(deRect ScissorRect);
    00109
    00110     //camera translation functions
    00111     deBoolean SetCameraTransform(deTransformInfo *M);
    00112     deBoolean SetCameraTransform(deVertex *Eye, deVertex *At, deVertex *Up);
    00113     deBoolean SetCameraTranslation(deVertex *Translation);
    00114     deBoolean SetCameraRotation(deVertex *Rotation);
    00115     deBoolean ResetCameraIdentity();
    00116
    00117     //world translation functions
    00118     deBoolean SetWorldTransform(deTransformInfo * Mat);
    00119     deBoolean SetWorldTransform(deVertex *Eye, deVertex *At, deVertex *Up);
    00120     deBoolean SetWorldTranslation(deVertex *Translation);
    00121     deBoolean SetWorldRotation(deVertex *Rotation);
    00122     deBoolean ResetWorldIdentity();
    00123
    00124     deBoolean SetTextureTransform(long StageNum, deTransformInfo * Mat);
    00125
    00126     //texture functions
    00127     deBoolean AddBitmap(IdeBitmap *Bitmap);
    00128     deBoolean AddBitmapProxy(IdeBitmapProxy *BitmapProxy);
    00129     deBoolean RemoveBitmap(IdeBitmap *Bitmap);
    00130
    00131     //render-to-texture functions
    00132     deBoolean RenderTo(IdeBitmap *Bitmap, long CubeMapFace);
    00133     deBoolean ScreenShot(IdeBitmap * &pBitmap, deBoolean GrabWindow);
    00134     deBoolean GetVideoTexture(IdeBitmap* pBitmap, long CubeMapFace);
    00135
    00136     //render states
    00137     unsigned long GetRenderState(RenderState State);
    00138     deBoolean SetRenderState(const IdeRenderState *State);
    00139     deBoolean SetRenderState(RenderState State, unsigned long Value);
    00140     deBoolean EnableLight(long LightID, deBoolean Enable);
    00141     deBoolean DisableAllLights();
    00142
    00143     //driver info
    00144     deDriverInfo* GetInfo() const;
    00145     IdeDriver::Types GetType() const;
    00146
    00147     void InvalidateRenderStates(deBoolean Valid);
    00148 private:
    00149     struct VertexShaderEntry
    00150     {
    00151         //long                      ID;             //ID to verify the structure
    00152         //unsigned long                     VertexShader;   //the vertex shader ID
    00153         IDirect3DVertexDeclaration9*        StreamDecl;     //the shader command info
    00154         //unsigned long             FVF;
    00155         long                                BufferFlags;    //record of which buffers are in it
    00156         // everything below has to go
    00157     };
    00158
    00159     struct VertexBufferEntry
    00160     {
    00161         long                        ID;             //ID to verify the structure
    00162         long                        EntrySize[IdeVertexBuffer::BUFFER_COUNT-1];     //size of each entry
    00163         LPDIRECT3DVERTEXBUFFER9     Buffers[IdeVertexBuffer::BUFFER_COUNT-1];       //the actual buffers to render
    00164         LPDIRECT3DINDEXBUFFER9      IndexBuffer;    //the index buffer
    00165         D3DPRIMITIVETYPE            RenderType;     //type of rendering to do
    00166         long                        PointCount;     //number of points
    00167         long                        TriCount;       //number of tris
    00168         long                        IndexCount;     //number of index entries
    00169         long                        PointStart;
    00170         long                        IndexStart;
    00171         long                        FrameUsed;
    00172         deBoolean                   SubBuffer;
    00173         unsigned long*              FVFShader;
    00174         IDirect3DVertexDeclaration9*    VertexDecl;
    00175     };
    00176
    00177     struct TextureEntry
    00178     {
    00179         long                    ID;             //ID to verify the struct
    00180         long                    NumFaces;       //if in a cubemap, the face ID
    00181         long                    FrameUsed;
    00182         LPDIRECT3DBASETEXTURE9  Texture;        //pointer to the texture
    00183         LPDIRECT3DSURFACE9      DepthStencil;   //pointer (possibly null) to depth-stencil surface if the texture is render-able
    00184     };
    00185
    00186     // static variables
    00187     static long             s_AdapterCount;         //number of adapters
    00188     static int *            s_ModeCount;            //total number of modes
    00189     static deBoolean *      s_WindowPossible;       //if each adapter can do windowed
    00190     static HINSTANCE        s_libD3D9;
    00191     static LPDIRECT3D9      s_D3D;
    00192     static D3DCAPS9 *       s_DevCaps;
    00193     static deDriverCaps*    s_Caps;
    00194     static long             s_NumDevices;
    00195     D3DDISPLAYMODE**    s_DisplayModes;
    00196
    00197     //private variables
    00198     IDirect3DDevice9 *          m_D3DDevice;
    00199
    00200     // cached entries to avoid state changes
    00201     deBoolean                   m_DeviceLost;
    00202     deBoolean                   m_RenderStatesValid;
    00203     deTList <VertexShaderEntry> m_ShaderList; // was a VertexShaderEntry
    00204     VertexShaderEntry*          m_LastShader; // was a LPDIRECT3DVERTEXSHADER9
    00205     deBoolean                   m_TextureRendered;
    00206     deBoolean                   m_LightsEnabled[deD3D9_MAX_LIGHTS];
    00207     IdeRenderTexture*           m_CachedTexture;
    00208     long                        m_FrameNum;
    00209
    00210     // cached data
    00211     D3DMATRIX m_WorldMatrix;
    00212     D3DMATRIX m_CameraMatrix;
    00213
    00214     // some optimizations and leak relief
    00215     IdeRenderMaterial::MatSource_t  m_AmbientSource, m_EmissiveSource;
    00216     IdeRenderMaterial::MatSource_t  m_DiffuseSource, m_SpecularSource;
    00217     deTHashInt <IdeBitmap*>         m_TexturesList;
    00218     deTHashInt <IdeVertexBuffer*>   m_VBufferList;
    00219     LPDIRECT3DSURFACE9              m_pZBuffer;
    00220     IdeBitmap*                      m_pCurrentTarget;
    00221
    00222     //private functions
    00223     void ReportErrorHR(HRESULT hr);
    00224     deBoolean TestDeviceStatus(deBoolean * DeviceCanReset);
    00225     deBoolean ResetDevice(D3DPRESENT_PARAMETERS *D3DParams);
    00226     deBoolean ReleaseVideoMemResources();
    00227     deBoolean BuildD3DParams(deDisplay * Display, D3DPRESENT_PARAMETERS *D3DParams);
    00228
    00229     deBoolean SetTextureState(IdeRenderTexture *State);
    00230     deBoolean SetLightState(IdeRenderLight *State);
    00231     deBoolean SetMaterialState(IdeRenderMaterial *State);
    00232
    00233     deBoolean FillBufferInfo(VertexBufferEntry *BufferEntry, IdeVertexBuffer *Vertex, IdeVertexBuffer::BufferType BufferType, deBoolean WipeBuffer);
    00234     deBoolean ProcessVertexBuffer(IdeVertexBuffer *Vertex, VertexBufferEntry *VertexBufferInfo, long BufferFlags);
    00235
    00236     deBoolean CreateDefaultCachedTexture();
    00237
    00238     // manages shader pool and creates/finds one for the specified flags
    00239     VertexShaderEntry* GetFixedFunctionShader(IdeVertexBuffer *Vertex);
    00240     VertexShaderEntry* LookUpVertexDeclaration(long BufferFlags) const;
    00241     VertexShaderEntry* MakeVertexDeclaration(long BufferFlags);
    00242     long DeleteShaders();
    00243     //convert to/from D3D BPP
    00244     D3DFORMAT ToD3DBPP(IdeDriver::BPP BPPType) const;
    00245     IdeDriver::BPP FromD3DBPP(D3DFORMAT BPPType) const;
    00246     D3DFORMAT ToD3DZPP(IdeDriver::ZPP ZPPType) const;
    00247     IdeDriver::ZPP FromD3DZPP(D3DFORMAT ZPPType) const;
    00248     D3DMATERIALCOLORSOURCE ToD3DMatSource(IdeRenderMaterial::MatSource_t source) const;
    00249 };
    00250
    00251 char* __cdecl HR2String(HRESULT hr, char* szHR);
    00252
    00253 extern "C"
    00254 {
    00255     //include the all important APIs so that we can be used
    00256 #ifdef _DEBUG
    00257     __declspec(dllexport) deBoolean IsDestiny3DDriverDebug();
    00258 #else
    00259     __declspec(dllexport) deBoolean IsDestiny3DDriver();
    00260 #endif
    00261     __declspec(dllexport) IdeDriver *GetDriverInstance(long ID);
    00262     __declspec(dllexport) const deDriverInfo *GetDriverInfo(long StructSize);
    00263 };
    00264
    00265 #endif