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