Panda3D
dxGraphicsStateGuardian9.I
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 dxGraphicsStateGuardian9.I
10  * @author mike
11  * @date 1999-02-02
12  */
13 
14 /**
15  * Converts Panda's floating-point LColor structure to DirectX's D3DCOLOR
16  * packed structure.
17  */
18 INLINE DWORD DXGraphicsStateGuardian9::
19 LColor_to_D3DCOLOR(const LColor &cLColor) {
20 // MS VC defines _M_IX86 for x86. gcc should define _X86_
21 #if (defined(_M_IX86) || defined(_X86_)) && !defined(STDFLOAT_DOUBLE)
22  DWORD d3dcolor, tempcolorval=255;
23 
24  // note the default FPU rounding mode will give 255*0.5f=0x80, not 0x7F as
25  // VC would force it to by resetting rounding mode don't think this makes
26  // much difference
27 
28  __asm {
29  push ebx ; want to save this in case this fn is inlined
30  push ecx
31  mov ecx, cLColor
32  fild tempcolorval
33  fld DWORD PTR [ecx]
34  fmul ST(0), ST(1)
35  fistp tempcolorval ; no way to store directly to int register
36  mov eax, tempcolorval
37  shl eax, 16
38 
39  fld DWORD PTR [ecx+4] ;grn
40  fmul ST(0), ST(1)
41  fistp tempcolorval
42  mov ebx, tempcolorval
43  shl ebx, 8
44  or eax, ebx
45 
46  fld DWORD PTR [ecx+8] ;blue
47  fmul ST(0), ST(1)
48  fistp tempcolorval
49  or eax, tempcolorval
50 
51  fld DWORD PTR [ecx+12] ;alpha
52  fmul ST(0), ST(1)
53  fistp tempcolorval
54  ; simulate pop 255.0 off FP stack w/o store, mark top as empty and increment stk ptr
55  ffree ST(0)
56  fincstp
57  mov ebx, tempcolorval
58  shl ebx, 24
59  or eax, ebx
60  mov d3dcolor, eax
61  pop ecx
62  pop ebx
63  }
64 
65  // dxgsg9_cat.debug() << (void*)d3dcolor << endl;
66  return d3dcolor;
67 #else //!_X86_
68  return D3DCOLOR_COLORVALUE(cLColor[0], cLColor[1], cLColor[2], cLColor[3]);
69 #endif //!_X86_
70 }
71 
72 /**
73  * Maps from the Texture's internal wrap mode symbols to GL's.
74  */
75 INLINE D3DTEXTUREADDRESS DXGraphicsStateGuardian9::
76 get_texture_wrap_mode(SamplerState::WrapMode wm) {
77  switch (wm) {
78  case SamplerState::WM_clamp:
79  return D3DTADDRESS_CLAMP;
80  case SamplerState::WM_repeat:
81  return D3DTADDRESS_WRAP;
82  case SamplerState::WM_mirror:
83  return D3DTADDRESS_MIRROR;
84  case SamplerState::WM_mirror_once:
85  return D3DTADDRESS_MIRRORONCE;
86  case SamplerState::WM_border_color:
87  return D3DTADDRESS_BORDER;
88  }
89  dxgsg9_cat.error() << "Invalid Texture::Mode value" << std::endl;
90  return D3DTADDRESS_WRAP;
91 }
92 
93 /**
94  * Maps from the fog types to gl version
95  */
96 INLINE D3DFOGMODE DXGraphicsStateGuardian9::
97 get_fog_mode_type(Fog::Mode m) {
98  switch (m) {
99  case Fog::M_linear:
100  return D3DFOG_LINEAR;
101  case Fog::M_exponential:
102  return D3DFOG_EXP;
103  case Fog::M_exponential_squared:
104  return D3DFOG_EXP2;
105  }
106  dxgsg9_cat.error() << "Invalid Fog::Mode value" << std::endl;
107  return D3DFOG_EXP;
108 }
109 
110 /**
111  * Returns the nth D3DTS_TEXTURE(n) constant.
112  */
113 INLINE D3DTRANSFORMSTATETYPE DXGraphicsStateGuardian9::
114 get_tex_mat_sym(int stage_index) {
115  return (D3DTRANSFORMSTATETYPE)(D3DTS_TEXTURE0 + stage_index);
116 }
117 
118 /**
119  * Returns the address of a 64K buffer that is allocated at the beginning of a
120  * 64K block.
121  */
122 INLINE unsigned char *DXGraphicsStateGuardian9::
123 get_safe_buffer_start() {
124  if (_temp_buffer == nullptr) {
125  // Guarantee we get a buffer of size 0x10000 bytes that begins on an even
126  // multiple of 0x10000. We do this by allocating double the required
127  // buffer, and then pointing to the first multiple of 0x10000 within that
128  // buffer.
129  _temp_buffer = new unsigned char[0x1ffff];
130  _safe_buffer_start = (unsigned char *)(((uintptr_t)_temp_buffer + 0xffff) & ~0xffff);
131  }
132 
133  return _safe_buffer_start;
134 }
135 
136 #define ALWAYS_SET_RENDER_STATE true
137 
138 /**
139  * This function creates a common layer between DX and Panda for
140  * SetRenderState. It also keeps avoids setting redundant render states.
141  */
142 INLINE HRESULT DXGraphicsStateGuardian9::
143 set_render_state (D3DRENDERSTATETYPE state, DWORD value)
144 {
145  HRESULT hr;
146 
147  hr = D3D_OK;
148  if (ALWAYS_SET_RENDER_STATE || _render_state_array [state] != value)
149  {
150  hr = _d3d_device->SetRenderState(state, value);
151  _render_state_array [state] = value;
152  }
153 
154  return hr;
155 }
156 
157 /**
158  * This function creates a common layer between DX and Panda. It also keeps
159  * avoids setting redundant render states.
160  */
161 INLINE HRESULT DXGraphicsStateGuardian9::
162 set_texture_stage_state (DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
163 {
164  HRESULT hr;
165 
166  hr = D3D_OK;
167  if (ALWAYS_SET_RENDER_STATE || _texture_stage_states_array [stage].state_array [type] != value)
168  {
169  hr = _d3d_device->SetTextureStageState(stage, type, value);
170  _texture_stage_states_array [stage].state_array [type] = value;
171  }
172 
173  return hr;
174 }
175 
176 /**
177  * This function creates a common layer between DX and Panda. It also keeps
178  * avoids setting redundant render states.
179  */
180 INLINE HRESULT DXGraphicsStateGuardian9::
181 set_sampler_state (DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value)
182 {
183  HRESULT hr;
184 
185  hr = D3D_OK;
186  if (ALWAYS_SET_RENDER_STATE || _texture_render_states_array [sampler].state_array [type] != value)
187  {
188  hr = _d3d_device->SetSamplerState(sampler, type, value);
189  _texture_render_states_array [sampler].state_array [type] = value;
190  }
191 
192  return hr;
193 }
194 
195 
196 /**
197  * Returns true if this particular GSG can render from a wdxGraphicsBuffer9
198  * directly into a texture, or false if it must always copy-to-texture at the
199  * end of each frame to achieve this effect.
200  */
201 INLINE bool DXGraphicsStateGuardian9::
203  return _supports_render_texture;
204 }
HRESULT set_sampler_state(DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value)
This function creates a common layer between DX and Panda.
HRESULT set_texture_stage_state(DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
This function creates a common layer between DX and Panda.
bool get_supports_render_texture() const
Returns true if this particular GSG can render from a wdxGraphicsBuffer9 directly into a texture,...
static DWORD LColor_to_D3DCOLOR(const LColor &cLColor)
Converts Panda's floating-point LColor structure to DirectX's D3DCOLOR packed structure.
HRESULT set_render_state(D3DRENDERSTATETYPE state, DWORD value)
This function creates a common layer between DX and Panda for SetRenderState.