Panda3D
 All Classes Functions Variables Enumerations
dxgsg8base.h
00001 // Filename: dxgsg8base.h
00002 // Created by:  georges (07Oct01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef DXGSG8BASE_H
00016 #define DXGSG8BASE_H
00017 
00018 #include "pandabase.h"
00019 #include "graphicsWindow.h"
00020 #include "pmap.h"
00021 
00022 #define WIN32_LEAN_AND_MEAN   // get rid of mfc win32 hdr stuff
00023 #ifndef STRICT
00024 // enable strict type checking in windows.h, see msdn
00025 #define STRICT
00026 #endif
00027 
00028 #define DIRECT3D_VERSION 0x0800
00029 #include <windows.h>
00030 #include <d3d8.h>
00031 #include <d3dx8.h>
00032 #include <dxerr8.h>
00033 #include <d3dx8tex.h>
00034 #undef WIN32_LEAN_AND_MEAN
00035 
00036 #ifndef D3DERRORSTRING
00037 #ifdef NDEBUG
00038 #define D3DERRORSTRING(HRESULT) " at (" << __FILE__ << ":" << __LINE__ << "), hr=" <<  DXGetErrorString8(HRESULT) << endl  // leave out descriptions to shrink release build
00039 #else
00040 #define D3DERRORSTRING(HRESULT) " at (" << __FILE__ << ":" << __LINE__ << "), hr=" <<  DXGetErrorString8(HRESULT) << ": " << DXGetErrorDescription8(HRESULT) << endl
00041 #endif
00042 #endif
00043 
00044 // imperfect method to ID NVid? could also scan desc str, but that isnt fullproof either
00045 #define IS_NVIDIA(DDDEVICEID) ((DDDEVICEID.VendorId==0x10DE) || (DDDEVICEID.VendorId==0x12D2))
00046 #define IS_ATI(DDDEVICEID) (DDDEVICEID.VendorId==0x1002)
00047 #define IS_MATROX(DDDEVICEID) (DDDEVICEID.VendorId==0x102B)
00048 
00049 #define D3D_MAXTEXTURESTAGES 8
00050 
00051 typedef enum {VertexShader,PixelShader} ShaderType;
00052 typedef DWORD DXShaderHandle;
00053 
00054 #define ISPOW2(X) (((X) & ((X)-1))==0)
00055 #define IS_VALID_PTR(PTR)  (!IsBadWritePtr(PTR,sizeof(void*)))
00056 
00057 #define DX_DECLARE_CLEAN(type, var) \
00058     type var;                       \
00059     ZeroMemory(&var, sizeof(type)); \
00060     var.dwSize = sizeof(type);
00061 
00062 #define SAFE_DELSHADER(TYPE,HANDLE,PDEVICE)  \
00063   if((HANDLE!=NULL)&&IS_VALID_PTR(PDEVICE)) { PDEVICE->Delete##TYPE##Shader(HANDLE);  HANDLE=NULL; }
00064 
00065 #define SAFE_DELETE(p)       { if(p) { assert(IS_VALID_PTR(p));   delete (p);     (p)=NULL; } }
00066 #define SAFE_DELETE_ARRAY(p) { if(p) { assert(IS_VALID_PTR(p));   delete [] (p);   (p)=NULL; } }
00067 
00068 // for stuff outside a panda class
00069 #define SAFE_RELEASE(p)      { if(p) { assert(IS_VALID_PTR(p)); (p)->Release(); (p)=NULL; } }
00070 #define SAFE_FREELIB(hDLL)   { if(hDLL!=NULL) {  FreeLibrary(hDLL);hDLL = NULL; } }
00071 
00072 // this is bDoDownToZero argument to RELEASE()
00073 #define RELEASE_DOWN_TO_ZERO true
00074 #define RELEASE_ONCE false
00075 
00076 
00077 // uncomment to add refcnt debug output
00078 #define DEBUG_RELEASES
00079 
00080 #ifdef DEBUG_RELEASES
00081 #define RELEASE(OBJECT,MODULE,DBGSTR,bDoDownToZero)             {  \
00082    ULONG refcnt;                                                \
00083    if(IS_VALID_PTR(OBJECT)) {                                   \
00084         refcnt = (OBJECT)->Release();                           \
00085         MODULE##_cat.debug() << DBGSTR << " released, refcnt = " << refcnt << " at " << __FILE__ << ":" << __LINE__ << endl; \
00086         if((bDoDownToZero) && (refcnt>0)) {                     \
00087               MODULE##_cat.warning() << DBGSTR << " released but still has a non-zero refcnt(" << refcnt << "), multi-releasing it down to zero!\n"; \
00088               do {                                \
00089                 refcnt = (OBJECT)->Release();     \
00090               } while(refcnt>0);                  \
00091         }                                         \
00092         (OBJECT) = NULL;                          \
00093       } else {                                    \
00094         MODULE##_cat.debug() << DBGSTR << " not released, ptr == NULL" << endl;  \
00095       }}
00096 
00097 #define PRINT_REFCNT(MODULE,p) { ULONG refcnt;  (p)->AddRef();  refcnt=(p)->Release(); \
00098                                  MODULE##_cat.debug() << #p << " has refcnt = " << refcnt << " at " << __FILE__ << ":" << __LINE__ << endl; }
00099 
00100 #else
00101 #define RELEASE(OBJECT,MODULE,DBGSTR,bDoDownToZero)   { \
00102    ULONG refcnt;                                        \
00103    if(IS_VALID_PTR(OBJECT))                           { \
00104         refcnt=(OBJECT)->Release();                     \
00105         if((bDoDownToZero) && (refcnt>0)) {             \
00106               MODULE##_cat.warning() << DBGSTR << " released but still has a non-zero refcnt(" << refcnt << "), multi-releasing it down to zero!\n"; \
00107               do {                                \
00108                 refcnt = (OBJECT)->Release();     \
00109               } while(refcnt>0);                  \
00110         }                                         \
00111         (OBJECT) = NULL;                          \
00112    }}
00113 
00114 #define PRINT_REFCNT(MODULE,p)
00115 #endif
00116 
00117 #ifdef DO_PSTATS
00118 #define DO_PSTATS_STUFF(XX) XX;
00119 #else
00120 #define DO_PSTATS_STUFF(XX)
00121 #endif
00122 
00123 #define PANDA_MAXNUMVERTS 0xFFFF  // Note Device may support more than this if it supports D3DFMT_INDEX32 indexbufs.
00124 
00125 #define FLG(NN) (1<<NN)
00126 #define MAX_POSSIBLE_TEXFMTS 32
00127 typedef enum {
00128     R8G8B8_FLAG =       FLG(0),
00129     A8R8G8B8_FLAG =     FLG(1),
00130     X8R8G8B8_FLAG =     FLG(2),
00131     R5G6B5_FLAG =       FLG(3),
00132     X1R5G5B5_FLAG =     FLG(4),
00133     A1R5G5B5_FLAG =     FLG(5),
00134     A4R4G4B4_FLAG =     FLG(6),
00135     R3G3B2_FLAG =       FLG(7),
00136     A8_FLAG =           FLG(8),
00137     A8R3G3B2_FLAG =     FLG(9),
00138     X4R4G4B4_FLAG =     FLG(10),
00139     A2B10G10R10_FLAG =  FLG(11),
00140     G16R16_FLAG =       FLG(12),
00141     A8P8_FLAG =         FLG(13),
00142     P8_FLAG =           FLG(14),
00143     L8_FLAG =           FLG(15),
00144     A8L8_FLAG =         FLG(16),
00145     A4L4_FLAG =         FLG(17),
00146     V8U8_FLAG =         FLG(18),
00147     L6V5U5_FLAG =       FLG(19),
00148     X8L8V8U8_FLAG =     FLG(20),
00149     Q8W8V8U8_FLAG =     FLG(21),
00150     V16U16_FLAG =       FLG(22),
00151     W11V11U10_FLAG =    FLG(23),
00152     A2W10V10U10_FLAG =  FLG(24),
00153     UYVY_FLAG =         FLG(25),
00154     YUY2_FLAG =         FLG(26),
00155     DXT1_FLAG =         FLG(27),
00156     DXT2_FLAG =         FLG(28),
00157     DXT3_FLAG =         FLG(29),
00158     DXT4_FLAG =         FLG(30),
00159     DXT5_FLAG =         FLG(31)
00160 } D3DFORMAT_FLAG;
00161 
00162 // this is only used in conjunction w/rendertgt fmts, so just make it something that can never be a rtgt
00163 #define DISPLAY_32BPP_REQUIRES_16BPP_ZBUFFER_FLAG DXT1_FLAG
00164 #define DISPLAY_16BPP_REQUIRES_16BPP_ZBUFFER_FLAG DXT2_FLAG
00165 
00166 #define IS_16BPP_DISPLAY_FORMAT(FMT) (((FMT)==D3DFMT_R5G6B5)||((FMT)==D3DFMT_X1R5G5B5)||((FMT)==D3DFMT_A1R5G5B5))
00167 #define IS_16BPP_ZBUFFER(FMT) ((FMT==D3DFMT_D16)||(FMT==D3DFMT_D15S1))
00168 #define IS_STENCIL_FORMAT(FMT) (((FMT)==D3DFMT_D24S8) || ((FMT)==D3DFMT_D15S1) || ((FMT)==D3DFMT_D24X4S4))
00169 #define RECT_XSIZE(REC) (REC.right-REC.left)
00170 #define RECT_YSIZE(REC) (REC.bottom-REC.top)
00171 
00172 class DXGraphicsStateGuardian8;
00173 
00174 struct DXScreenData {
00175   LPDIRECT3DDEVICE8 _d3d_device;
00176   IDirect3DSwapChain8 *_swap_chain;
00177   LPDIRECT3D8 _d3d8;  // copied from DXGraphicsPipe8 for convenience
00178   HWND _window;
00179   HMONITOR _monitor;
00180   DWORD _max_available_video_memory;
00181   ushort _card_id;  // adapter ID
00182   ushort _depth_buffer_bitdepth;  //GetSurfaceDesc is not reliable so must store this explicitly
00183   bool _can_direct_disable_color_writes;  // if true, don't need blending for this
00184   bool _is_low_memory_card;
00185   bool _is_tnl_device;
00186   bool _can_use_hw_vertex_shaders;
00187   bool _can_use_pixel_shaders;
00188   bool _is_dx8_1;
00189   UINT _supported_screen_depths_mask;
00190   UINT _supported_tex_formats_mask;
00191   D3DCAPS8 _d3dcaps;
00192   D3DDISPLAYMODE _display_mode;
00193   D3DPRESENT_PARAMETERS _presentation_params;  // not redundant with _display_mode since width/height must be 0 for windowed mode
00194   D3DADAPTER_IDENTIFIER8 _dx_device_id;
00195   D3DFORMAT _render_to_texture_d3d_format;
00196   D3DFORMAT _framebuffer_d3d_format;
00197 
00198   DXGraphicsStateGuardian8 *_dxgsg8;
00199 };
00200 
00201 
00202 //utility stuff
00203 extern pmap<D3DFORMAT_FLAG,D3DFORMAT> g_D3DFORMATmap;
00204 extern void Init_D3DFORMAT_map();
00205 extern const char *D3DFormatStr(D3DFORMAT fmt);
00206 
00207 #endif
00208 
 All Classes Functions Variables Enumerations