Panda3D
|
00001 // Filename: dxgsg9base.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 DXGSG9BASE_H 00016 #define DXGSG9BASE_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 #include <windows.h> 00029 00030 #define D3D_OVERLOADS // get D3DVECTOR '+' operator, etc from d3dtypes.h 00031 //#define D3D_DEBUG_INFO 00032 00033 #undef Configure 00034 #include <d3d9.h> 00035 #include <d3dx9.h> 00036 00037 // This symbol is defined (or not defined) in Config.pp. 00038 //#define USE_GENERIC_DXERR_LIBRARY 1 00039 00040 #ifdef USE_GENERIC_DXERR_LIBRARY 00041 #include <dxerr.h> 00042 #define DX_GET_ERROR_STRING_FUNC DXGetErrorString 00043 #define DX_GET_ERROR_DESCRIPTION_FUNC DXGetErrorDescription 00044 #else 00045 #include <dxerr9.h> 00046 #define DX_GET_ERROR_STRING_FUNC DXGetErrorString9 00047 #define DX_GET_ERROR_DESCRIPTION_FUNC DXGetErrorDescription9 00048 #endif 00049 00050 #undef WIN32_LEAN_AND_MEAN 00051 00052 #if (D3D_SDK_VERSION & 0xffff) < 32 00053 #error You need to install the latest DirectX9 SDK. 00054 #endif 00055 00056 #ifndef D3DERRORSTRING 00057 #ifdef NDEBUG 00058 #define D3DERRORSTRING(HRESULT) " at (" << __FILE__ << ":" << __LINE__ << "), hr=" << DX_GET_ERROR_STRING_FUNC(HRESULT) << endl // leave out descriptions to shrink release build 00059 #else 00060 #define D3DERRORSTRING(HRESULT) " at (" << __FILE__ << ":" << __LINE__ << "), hr=" << DX_GET_ERROR_STRING_FUNC(HRESULT) << ": " << DX_GET_ERROR_DESCRIPTION_FUNC(HRESULT) << endl 00061 #endif 00062 #endif 00063 00064 // imperfect method to ID NVid? could also scan desc str, but that isnt fullproof either 00065 #define IS_NVIDIA(DDDEVICEID) ((DDDEVICEID.VendorId==0x10DE) || (DDDEVICEID.VendorId==0x12D2)) 00066 #define IS_ATI(DDDEVICEID) (DDDEVICEID.VendorId==0x1002) 00067 #define IS_MATROX(DDDEVICEID) (DDDEVICEID.VendorId==0x102B) 00068 00069 #define D3D_MAXTEXTURESTAGES 8 00070 00071 typedef enum {VertexShader,PixelShader} ShaderType; 00072 typedef DWORD DXShaderHandle; 00073 00074 #define ISPOW2(X) (((X) & ((X)-1))==0) 00075 #define IS_VALID_PTR(PTR) (!IsBadWritePtr(PTR,sizeof(void*))) 00076 00077 #define DX_DECLARE_CLEAN(type, var) \ 00078 type var; \ 00079 ZeroMemory(&var, sizeof(type)); \ 00080 var.dwSize = sizeof(type); 00081 00082 #define SAFE_DELSHADER(TYPE,HANDLE,PDEVICE) \ 00083 if((HANDLE!=NULL)&&IS_VALID_PTR(PDEVICE)) { PDEVICE->Delete##TYPE##Shader(HANDLE); HANDLE=NULL; } 00084 00085 #define SAFE_DELETE(p) { if(p) { assert(IS_VALID_PTR(p)); delete (p); (p)=NULL; } } 00086 #define SAFE_DELETE_ARRAY(p) { if(p) { assert(IS_VALID_PTR(p)); delete [] (p); (p)=NULL; } } 00087 00088 // for stuff outside a panda class 00089 #define SAFE_RELEASE(p) { if(p) { assert(IS_VALID_PTR(p)); (p)->Release(); (p)=NULL; } } 00090 #define SAFE_FREELIB(hDLL) { if(hDLL!=NULL) { FreeLibrary(hDLL);hDLL = NULL; } } 00091 00092 // this is bDoDownToZero argument to RELEASE() 00093 #define RELEASE_DOWN_TO_ZERO true 00094 #define RELEASE_ONCE false 00095 00096 00097 // uncomment to add refcnt debug output 00098 // #define DEBUG_RELEASES 00099 00100 #ifdef DEBUG_RELEASES 00101 #define RELEASE(OBJECT,MODULE,DBGSTR,bDoDownToZero) { \ 00102 ULONG refcnt; \ 00103 if(IS_VALID_PTR(OBJECT)) { \ 00104 refcnt = (OBJECT)->Release(); \ 00105 MODULE##_cat.debug() << DBGSTR << " released, refcnt = " << refcnt << " at " << __FILE__ << ":" << __LINE__ << endl; \ 00106 if((bDoDownToZero) && (refcnt>0)) { \ 00107 MODULE##_cat.warning() << DBGSTR << " released but still has a non-zero refcnt(" << refcnt << "), multi-releasing it down to zero!\n"; \ 00108 do { \ 00109 refcnt = (OBJECT)->Release(); \ 00110 } while(refcnt>0); \ 00111 } \ 00112 (OBJECT) = NULL; \ 00113 } else { \ 00114 MODULE##_cat.debug() << DBGSTR << " not released, ptr == NULL" << endl; \ 00115 }} 00116 00117 #define PRINT_REFCNT(MODULE,p) { ULONG refcnt; (p)->AddRef(); refcnt=(p)->Release(); \ 00118 MODULE##_cat.debug() << #p << " has refcnt = " << refcnt << " at " << __FILE__ << ":" << __LINE__ << endl; } 00119 00120 #else 00121 #define RELEASE(OBJECT,MODULE,DBGSTR,bDoDownToZero) { \ 00122 ULONG refcnt; \ 00123 if(IS_VALID_PTR(OBJECT)) { \ 00124 refcnt=(OBJECT)->Release(); \ 00125 if((bDoDownToZero) && (refcnt>0)) { \ 00126 MODULE##_cat.warning() << DBGSTR << " released but still has a non-zero refcnt(" << refcnt << "), multi-releasing it down to zero!\n"; \ 00127 do { \ 00128 refcnt = (OBJECT)->Release(); \ 00129 } while(refcnt>0); \ 00130 } \ 00131 (OBJECT) = NULL; \ 00132 }} 00133 00134 #define PRINT_REFCNT(MODULE,p) 00135 #endif 00136 00137 #ifdef DO_PSTATS 00138 #define DO_PSTATS_STUFF(XX) XX; 00139 #else 00140 #define DO_PSTATS_STUFF(XX) 00141 #endif 00142 00143 #define PANDA_MAXNUMVERTS 0xFFFF // Note Device may support more than this if it supports D3DFMT_INDEX32 indexbufs. 00144 00145 #define FLG(NN) (1<<NN) 00146 #define MAX_POSSIBLE_TEXFMTS 32 00147 typedef enum { 00148 R8G8B8_FLAG = FLG(0), 00149 A8R8G8B8_FLAG = FLG(1), 00150 X8R8G8B8_FLAG = FLG(2), 00151 R5G6B5_FLAG = FLG(3), 00152 X1R5G5B5_FLAG = FLG(4), 00153 A1R5G5B5_FLAG = FLG(5), 00154 A4R4G4B4_FLAG = FLG(6), 00155 R3G3B2_FLAG = FLG(7), 00156 A8_FLAG = FLG(8), 00157 A8R3G3B2_FLAG = FLG(9), 00158 X4R4G4B4_FLAG = FLG(10), 00159 A2B10G10R10_FLAG = FLG(11), 00160 G16R16_FLAG = FLG(12), 00161 A8P8_FLAG = FLG(13), 00162 P8_FLAG = FLG(14), 00163 L8_FLAG = FLG(15), 00164 A8L8_FLAG = FLG(16), 00165 A4L4_FLAG = FLG(17), 00166 V8U8_FLAG = FLG(18), 00167 L6V5U5_FLAG = FLG(19), 00168 X8L8V8U8_FLAG = FLG(20), 00169 Q8W8V8U8_FLAG = FLG(21), 00170 V16U16_FLAG = FLG(22), 00171 W11V11U10_FLAG = FLG(23), 00172 A2W10V10U10_FLAG = FLG(24), 00173 UYVY_FLAG = FLG(25), 00174 YUY2_FLAG = FLG(26), 00175 DXT1_FLAG = FLG(27), 00176 DXT2_FLAG = FLG(28), 00177 DXT3_FLAG = FLG(29), 00178 DXT4_FLAG = FLG(30), 00179 DXT5_FLAG = FLG(31) 00180 } D3DFORMAT_FLAG; 00181 00182 // this is only used in conjunction w/rendertgt fmts, so just make it something that can never be a rtgt 00183 #define DISPLAY_32BPP_REQUIRES_16BPP_ZBUFFER_FLAG DXT1_FLAG 00184 #define DISPLAY_16BPP_REQUIRES_16BPP_ZBUFFER_FLAG DXT2_FLAG 00185 00186 #define IS_16BPP_DISPLAY_FORMAT(FMT) (((FMT)==D3DFMT_R5G6B5)||((FMT)==D3DFMT_X1R5G5B5)||((FMT)==D3DFMT_A1R5G5B5)) 00187 #define IS_16BPP_ZBUFFER(FMT) ((FMT==D3DFMT_D16)||(FMT==D3DFMT_D15S1)) 00188 #define IS_STENCIL_FORMAT(FMT) (((FMT)==D3DFMT_D24S8) || ((FMT)==D3DFMT_D15S1) || ((FMT)==D3DFMT_D24X4S4)) 00189 #define RECT_XSIZE(REC) (REC.right-REC.left) 00190 #define RECT_YSIZE(REC) (REC.bottom-REC.top) 00191 00192 class DXGraphicsStateGuardian9; 00193 00194 struct DXScreenData { 00195 LPDIRECT3DDEVICE9 _d3d_device; 00196 IDirect3DSwapChain9 *_swap_chain; 00197 LPDIRECT3D9 _d3d9; // copied from DXGraphicsPipe9 for convenience 00198 HWND _window; 00199 HMONITOR _monitor; 00200 DWORD _max_available_video_memory; 00201 ushort _card_id; // adapter ID 00202 ushort _depth_buffer_bitdepth; //GetSurfaceDesc is not reliable so must store this explicitly 00203 bool _can_direct_disable_color_writes; // if true, don't need blending for this 00204 bool _is_low_memory_card; 00205 bool _is_tnl_device; 00206 bool _can_use_hw_vertex_shaders; 00207 bool _can_use_pixel_shaders; 00208 bool _is_dx9_1; 00209 UINT _supported_screen_depths_mask; 00210 UINT _supported_tex_formats_mask; 00211 bool _supports_rgba16f_texture_format; 00212 bool _supports_rgba32_texture_format; 00213 D3DCAPS9 _d3dcaps; 00214 D3DDISPLAYMODE _display_mode; 00215 D3DPRESENT_PARAMETERS _presentation_params; // not redundant with _display_mode since width/height must be 0 for windowed mode 00216 D3DADAPTER_IDENTIFIER9 _dx_device_id; 00217 D3DFORMAT _render_to_texture_d3d_format; 00218 D3DFORMAT _framebuffer_d3d_format; 00219 00220 DXGraphicsStateGuardian9 *_dxgsg9; 00221 00222 int _managed_textures; 00223 int _managed_vertex_buffers; 00224 int _managed_index_buffers; 00225 00226 bool _supports_dynamic_textures; 00227 bool _supports_automatic_mipmap_generation; 00228 bool _intel_compressed_texture_bug; 00229 }; 00230 00231 00232 //utility stuff 00233 extern pmap<D3DFORMAT_FLAG,D3DFORMAT> g_D3DFORMATmap; 00234 extern void Init_D3DFORMAT_map(); 00235 extern const char *D3DFormatStr(D3DFORMAT fmt); 00236 00237 #endif