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