Panda3D
dxgsg9base.h
1 // Filename: dxgsg9base.h
2 // Created by: georges (07Oct01)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef DXGSG9BASE_H
16 #define DXGSG9BASE_H
17 
18 #include "pandabase.h"
19 #include "graphicsWindow.h"
20 #include "pmap.h"
21 
22 #ifndef WIN32_LEAN_AND_MEAN
23 #define WIN32_LEAN_AND_MEAN 1 // get rid of mfc win32 hdr stuff
24 #endif
25 #ifndef STRICT
26 // enable strict type checking in windows.h, see msdn
27 #define STRICT
28 #endif
29 
30 #include <windows.h>
31 
32 #define D3D_OVERLOADS // get D3DVECTOR '+' operator, etc from d3dtypes.h
33 //#define D3D_DEBUG_INFO
34 
35 #undef Configure
36 #include <d3d9.h>
37 #include <d3dx9.h>
38 
39 // This symbol is defined (or not defined) in Config.pp.
40 //#define USE_GENERIC_DXERR_LIBRARY 1
41 
42 #ifdef USE_GENERIC_DXERR_LIBRARY
43 #include <dxerr.h>
44 #define DX_GET_ERROR_STRING_FUNC DXGetErrorString
45 #define DX_GET_ERROR_DESCRIPTION_FUNC DXGetErrorDescription
46 #else
47 #include <dxerr9.h>
48 #define DX_GET_ERROR_STRING_FUNC DXGetErrorString9
49 #define DX_GET_ERROR_DESCRIPTION_FUNC DXGetErrorDescription9
50 #endif
51 
52 #undef WIN32_LEAN_AND_MEAN
53 
54 #if (D3D_SDK_VERSION & 0xffff) < 32
55 #error You need to install the latest DirectX9 SDK.
56 #endif
57 
58 #ifndef D3DERRORSTRING
59 #ifdef NDEBUG
60 #define D3DERRORSTRING(HRESULT) " at (" << __FILE__ << ":" << __LINE__ << "), hr=" << DX_GET_ERROR_STRING_FUNC(HRESULT) << endl // leave out descriptions to shrink release build
61 #else
62 #define D3DERRORSTRING(HRESULT) " at (" << __FILE__ << ":" << __LINE__ << "), hr=" << DX_GET_ERROR_STRING_FUNC(HRESULT) << ": " << DX_GET_ERROR_DESCRIPTION_FUNC(HRESULT) << endl
63 #endif
64 #endif
65 
66 // imperfect method to ID NVid? could also scan desc str, but that isnt fullproof either
67 #define IS_NVIDIA(DDDEVICEID) ((DDDEVICEID.VendorId==0x10DE) || (DDDEVICEID.VendorId==0x12D2))
68 #define IS_ATI(DDDEVICEID) (DDDEVICEID.VendorId==0x1002)
69 #define IS_MATROX(DDDEVICEID) (DDDEVICEID.VendorId==0x102B)
70 
71 #define D3D_MAXTEXTURESTAGES 8
72 
73 typedef enum {VertexShader,PixelShader} ShaderType;
74 typedef DWORD DXShaderHandle;
75 
76 #define ISPOW2(X) (((X) & ((X)-1))==0)
77 #define IS_VALID_PTR(PTR) (!IsBadWritePtr(PTR,sizeof(void*)))
78 
79 #define DX_DECLARE_CLEAN(type, var) \
80  type var; \
81  ZeroMemory(&var, sizeof(type)); \
82  var.dwSize = sizeof(type);
83 
84 #define SAFE_DELSHADER(TYPE,HANDLE,PDEVICE) \
85  if((HANDLE!=NULL)&&IS_VALID_PTR(PDEVICE)) { PDEVICE->Delete##TYPE##Shader(HANDLE); HANDLE=NULL; }
86 
87 #define SAFE_DELETE(p) { if(p) { assert(IS_VALID_PTR(p)); delete (p); (p)=NULL; } }
88 #define SAFE_DELETE_ARRAY(p) { if(p) { assert(IS_VALID_PTR(p)); delete [] (p); (p)=NULL; } }
89 
90 // for stuff outside a panda class
91 #define SAFE_RELEASE(p) { if(p) { assert(IS_VALID_PTR(p)); (p)->Release(); (p)=NULL; } }
92 #define SAFE_FREELIB(hDLL) { if(hDLL!=NULL) { FreeLibrary(hDLL);hDLL = NULL; } }
93 
94 // this is bDoDownToZero argument to RELEASE()
95 #define RELEASE_DOWN_TO_ZERO true
96 #define RELEASE_ONCE false
97 
98 
99 // uncomment to add refcnt debug output
100 // #define DEBUG_RELEASES
101 
102 #ifdef DEBUG_RELEASES
103 #define RELEASE(OBJECT,MODULE,DBGSTR,bDoDownToZero) { \
104  ULONG refcnt; \
105  if(IS_VALID_PTR(OBJECT)) { \
106  refcnt = (OBJECT)->Release(); \
107  MODULE##_cat.debug() << DBGSTR << " released, refcnt = " << refcnt << " at " << __FILE__ << ":" << __LINE__ << endl; \
108  if((bDoDownToZero) && (refcnt>0)) { \
109  MODULE##_cat.warning() << DBGSTR << " released but still has a non-zero refcnt(" << refcnt << "), multi-releasing it down to zero!\n"; \
110  do { \
111  refcnt = (OBJECT)->Release(); \
112  } while(refcnt>0); \
113  } \
114  (OBJECT) = NULL; \
115  } else { \
116  MODULE##_cat.debug() << DBGSTR << " not released, ptr == NULL" << endl; \
117  }}
118 
119 #define PRINT_REFCNT(MODULE,p) { ULONG refcnt; (p)->AddRef(); refcnt=(p)->Release(); \
120  MODULE##_cat.debug() << #p << " has refcnt = " << refcnt << " at " << __FILE__ << ":" << __LINE__ << endl; }
121 
122 #else
123 #define RELEASE(OBJECT,MODULE,DBGSTR,bDoDownToZero) { \
124  ULONG refcnt; \
125  if(IS_VALID_PTR(OBJECT)) { \
126  refcnt=(OBJECT)->Release(); \
127  if((bDoDownToZero) && (refcnt>0)) { \
128  MODULE##_cat.warning() << DBGSTR << " released but still has a non-zero refcnt(" << refcnt << "), multi-releasing it down to zero!\n"; \
129  do { \
130  refcnt = (OBJECT)->Release(); \
131  } while(refcnt>0); \
132  } \
133  (OBJECT) = NULL; \
134  }}
135 
136 #define PRINT_REFCNT(MODULE,p)
137 #endif
138 
139 #ifdef DO_PSTATS
140 #define DO_PSTATS_STUFF(XX) XX;
141 #else
142 #define DO_PSTATS_STUFF(XX)
143 #endif
144 
145 #define PANDA_MAXNUMVERTS 0xFFFF // Note Device may support more than this if it supports D3DFMT_INDEX32 indexbufs.
146 
147 #define FLG(NN) (1<<NN)
148 #define MAX_POSSIBLE_TEXFMTS 32
149 typedef enum {
150  R8G8B8_FLAG = FLG(0),
151  A8R8G8B8_FLAG = FLG(1),
152  X8R8G8B8_FLAG = FLG(2),
153  R5G6B5_FLAG = FLG(3),
154  X1R5G5B5_FLAG = FLG(4),
155  A1R5G5B5_FLAG = FLG(5),
156  A4R4G4B4_FLAG = FLG(6),
157  R3G3B2_FLAG = FLG(7),
158  A8_FLAG = FLG(8),
159  A8R3G3B2_FLAG = FLG(9),
160  X4R4G4B4_FLAG = FLG(10),
161  A2B10G10R10_FLAG = FLG(11),
162  G16R16_FLAG = FLG(12),
163  A8P8_FLAG = FLG(13),
164  P8_FLAG = FLG(14),
165  L8_FLAG = FLG(15),
166  A8L8_FLAG = FLG(16),
167  A4L4_FLAG = FLG(17),
168  V8U8_FLAG = FLG(18),
169  L6V5U5_FLAG = FLG(19),
170  X8L8V8U8_FLAG = FLG(20),
171  Q8W8V8U8_FLAG = FLG(21),
172  V16U16_FLAG = FLG(22),
173  W11V11U10_FLAG = FLG(23),
174  A2W10V10U10_FLAG = FLG(24),
175  UYVY_FLAG = FLG(25),
176  YUY2_FLAG = FLG(26),
177  DXT1_FLAG = FLG(27),
178  DXT2_FLAG = FLG(28),
179  DXT3_FLAG = FLG(29),
180  DXT4_FLAG = FLG(30),
181  DXT5_FLAG = FLG(31)
182 } D3DFORMAT_FLAG;
183 
184 // this is only used in conjunction w/rendertgt fmts, so just make it something that can never be a rtgt
185 #define DISPLAY_32BPP_REQUIRES_16BPP_ZBUFFER_FLAG DXT1_FLAG
186 #define DISPLAY_16BPP_REQUIRES_16BPP_ZBUFFER_FLAG DXT2_FLAG
187 
188 #define IS_16BPP_DISPLAY_FORMAT(FMT) (((FMT)==D3DFMT_R5G6B5)||((FMT)==D3DFMT_X1R5G5B5)||((FMT)==D3DFMT_A1R5G5B5))
189 #define IS_16BPP_ZBUFFER(FMT) ((FMT==D3DFMT_D16)||(FMT==D3DFMT_D15S1))
190 #define IS_STENCIL_FORMAT(FMT) (((FMT)==D3DFMT_D24S8) || ((FMT)==D3DFMT_D15S1) || ((FMT)==D3DFMT_D24X4S4))
191 #define RECT_XSIZE(REC) (REC.right-REC.left)
192 #define RECT_YSIZE(REC) (REC.bottom-REC.top)
193 
195 
196 struct DXScreenData {
197  LPDIRECT3DDEVICE9 _d3d_device;
198  IDirect3DSwapChain9 *_swap_chain;
199  LPDIRECT3D9 _d3d9; // copied from DXGraphicsPipe9 for convenience
200  HWND _window;
201  HMONITOR _monitor;
202  DWORD _max_available_video_memory;
203  ushort _card_id; // adapter ID
204  ushort _depth_buffer_bitdepth; //GetSurfaceDesc is not reliable so must store this explicitly
205  bool _can_direct_disable_color_writes; // if true, don't need blending for this
206  bool _is_low_memory_card;
207  bool _is_tnl_device;
208  bool _can_use_hw_vertex_shaders;
209  bool _can_use_pixel_shaders;
210  bool _is_dx9_1;
211  UINT _supported_screen_depths_mask;
212  UINT _supported_tex_formats_mask;
213  bool _supports_rgba16f_texture_format;
214  bool _supports_rgba32_texture_format;
215  D3DCAPS9 _d3dcaps;
216  D3DDISPLAYMODE _display_mode;
217  D3DPRESENT_PARAMETERS _presentation_params; // not redundant with _display_mode since width/height must be 0 for windowed mode
218  D3DADAPTER_IDENTIFIER9 _dx_device_id;
219  D3DFORMAT _render_to_texture_d3d_format;
220  D3DFORMAT _framebuffer_d3d_format;
221 
222  DXGraphicsStateGuardian9 *_dxgsg9;
223 
224  int _managed_textures;
225  int _managed_vertex_buffers;
226  int _managed_index_buffers;
227 
228  bool _supports_dynamic_textures;
229  bool _supports_automatic_mipmap_generation;
230  bool _intel_compressed_texture_bug;
231 };
232 
233 
234 //utility stuff
235 extern pmap<D3DFORMAT_FLAG,D3DFORMAT> g_D3DFORMATmap;
236 extern void Init_D3DFORMAT_map();
237 extern const char *D3DFormatStr(D3DFORMAT fmt);
238 
239 #endif
A GraphicsStateGuardian for rendering into DirectX9 contexts.
This is our own Panda specialization on the default STL map.
Definition: pmap.h:52