Panda3D
 All Classes Functions Variables Enumerations
dxGraphicsStateGuardian9.I
00001 // Filename: dxGraphicsStateGuardian9.I
00002 // Created by:  mike (02Feb99)
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 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: DXGraphicsStateGuardian9::LColor_to_D3DCOLOR
00018 //       Access: Public, Static
00019 //  Description: Converts Panda's floating-point LColor structure to
00020 //               DirectX's D3DCOLOR packed structure.
00021 ////////////////////////////////////////////////////////////////////
00022 INLINE DWORD DXGraphicsStateGuardian9::
00023 LColor_to_D3DCOLOR(const LColor &cLColor) {
00024 // MS VC defines _M_IX86 for x86.  gcc should define _X86_
00025 #if (defined(_M_IX86) || defined(_X86_)) && !defined(STDFLOAT_DOUBLE)
00026   DWORD d3dcolor, tempcolorval=255;
00027 
00028   // note the default FPU rounding mode will give 255*0.5f=0x80, not 0x7F as VC would force it to by resetting rounding mode
00029   // don't think this makes much difference
00030 
00031   __asm {
00032         push ebx   ; want to save this in case this fn is inlined
00033         push ecx
00034         mov ecx, cLColor
00035         fild tempcolorval
00036         fld DWORD PTR [ecx]
00037         fmul ST(0), ST(1)
00038         fistp tempcolorval  ; no way to store directly to int register
00039         mov eax, tempcolorval
00040         shl eax, 16
00041 
00042         fld DWORD PTR [ecx+4]  ;grn
00043         fmul ST(0), ST(1)
00044         fistp tempcolorval
00045         mov ebx, tempcolorval
00046         shl ebx, 8
00047         or eax, ebx
00048 
00049         fld DWORD PTR [ecx+8]  ;blue
00050         fmul ST(0), ST(1)
00051         fistp tempcolorval
00052         or eax, tempcolorval
00053 
00054         fld DWORD PTR [ecx+12] ;alpha
00055         fmul ST(0), ST(1)
00056         fistp tempcolorval
00057         ; simulate pop 255.0 off FP stack w/o store, mark top as empty and increment stk ptr
00058         ffree ST(0)
00059         fincstp
00060         mov ebx, tempcolorval
00061         shl ebx, 24
00062         or eax, ebx
00063         mov d3dcolor, eax
00064         pop ecx
00065         pop ebx
00066   }
00067 
00068   //   dxgsg9_cat.debug() << (void*)d3dcolor << endl;
00069   return d3dcolor;
00070 #else //!_X86_
00071   return D3DCOLOR_COLORVALUE(cLColor[0], cLColor[1], cLColor[2], cLColor[3]);
00072 #endif //!_X86_
00073 }
00074 
00075 ////////////////////////////////////////////////////////////////////
00076 //     Function: DXGraphicsStateGuardian9::get_texture_wrap_mode
00077 //       Access: Protected, Static
00078 //  Description: Maps from the Texture's internal wrap mode symbols to
00079 //               GL's.
00080 ////////////////////////////////////////////////////////////////////
00081 INLINE D3DTEXTUREADDRESS DXGraphicsStateGuardian9::
00082 get_texture_wrap_mode(Texture::WrapMode wm) {
00083   switch (wm) {
00084   case Texture::WM_clamp:
00085     return D3DTADDRESS_CLAMP;
00086   case Texture::WM_repeat:
00087     return D3DTADDRESS_WRAP;
00088   case Texture::WM_mirror:
00089     return D3DTADDRESS_MIRROR;
00090   case Texture::WM_mirror_once:
00091     return D3DTADDRESS_MIRRORONCE;
00092   case Texture::WM_border_color:
00093     return D3DTADDRESS_BORDER;
00094   }
00095   dxgsg9_cat.error() << "Invalid Texture::Mode value" << endl;
00096   return D3DTADDRESS_WRAP;
00097 }
00098 
00099 ////////////////////////////////////////////////////////////////////
00100 //     Function: DXGraphicsStateGuardian9::get_fog_mode_type
00101 //       Access: Protected, Static
00102 //  Description: Maps from the fog types to gl version
00103 ////////////////////////////////////////////////////////////////////
00104 INLINE D3DFOGMODE DXGraphicsStateGuardian9::
00105 get_fog_mode_type(Fog::Mode m) {
00106   switch (m) {
00107   case Fog::M_linear:
00108     return D3DFOG_LINEAR;
00109   case Fog::M_exponential:
00110     return D3DFOG_EXP;
00111   case Fog::M_exponential_squared:
00112     return D3DFOG_EXP2;
00113   }
00114   dxgsg9_cat.error() << "Invalid Fog::Mode value" << endl;
00115   return D3DFOG_EXP;
00116 }
00117 
00118 ////////////////////////////////////////////////////////////////////
00119 //     Function: DXGraphicsStateGuardian9::get_tex_mat_sym
00120 //       Access: Protected, Static
00121 //  Description: Returns the nth D3DTS_TEXTURE(n) constant.
00122 ////////////////////////////////////////////////////////////////////
00123 INLINE D3DTRANSFORMSTATETYPE DXGraphicsStateGuardian9::
00124 get_tex_mat_sym(int stage_index) {
00125   return (D3DTRANSFORMSTATETYPE)(D3DTS_TEXTURE0 + stage_index);
00126 }
00127 
00128 ////////////////////////////////////////////////////////////////////
00129 //     Function: DXGraphicsStateGuardian9::get_safe_buffer_start
00130 //       Access: Protected, Static
00131 //  Description: Returns the address of a 64K buffer that is allocated
00132 //               at the beginning of a 64K block.
00133 ////////////////////////////////////////////////////////////////////
00134 INLINE unsigned char *DXGraphicsStateGuardian9::
00135 get_safe_buffer_start() {
00136   if (_temp_buffer == NULL) {
00137     // Guarantee we get a buffer of size 0x10000 bytes that begins
00138     // on an even multiple of 0x10000.  We do this by allocating
00139     // double the required buffer, and then pointing to the first
00140     // multiple of 0x10000 within that buffer.
00141     _temp_buffer = new unsigned char[0x1ffff];
00142     _safe_buffer_start = (unsigned char *)(((long)_temp_buffer + 0xffff) & ~0xffff);
00143   }
00144 
00145   return _safe_buffer_start;
00146 }
00147 
00148 #define ALWAYS_SET_RENDER_STATE true
00149 
00150 ////////////////////////////////////////////////////////////////////
00151 //     Function: DXGraphicsStateGuardian9::set_render_state
00152 //       Access:
00153 //  Description: This function creates a common layer between DX
00154 //               and Panda for SetRenderState. It also keeps avoids
00155 //               setting redundant render states.
00156 ////////////////////////////////////////////////////////////////////
00157 INLINE HRESULT DXGraphicsStateGuardian9::
00158 set_render_state (D3DRENDERSTATETYPE state, DWORD value)
00159 {
00160   HRESULT hr;
00161 
00162   hr = D3D_OK;
00163   if (ALWAYS_SET_RENDER_STATE || _render_state_array [state] != value)
00164   {
00165     hr = _d3d_device->SetRenderState(state, value);
00166     _render_state_array [state] = value;
00167   }
00168 
00169   return hr;
00170 }
00171 
00172 ////////////////////////////////////////////////////////////////////
00173 //     Function: DXGraphicsStateGuardian9::set_texture_stage_state
00174 //       Access:
00175 //  Description: This function creates a common layer between DX
00176 //               and Panda. It also keeps avoids setting redundant
00177 //               render states.
00178 ////////////////////////////////////////////////////////////////////
00179 INLINE HRESULT DXGraphicsStateGuardian9::
00180 set_texture_stage_state (DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
00181 {
00182   HRESULT hr;
00183 
00184   hr = D3D_OK;
00185   if (ALWAYS_SET_RENDER_STATE || _texture_stage_states_array [stage].state_array [type] != value)
00186   {
00187     hr = _d3d_device->SetTextureStageState(stage, type, value);
00188     _texture_stage_states_array [stage].state_array [type] = value;
00189   }
00190 
00191   return hr;
00192 }
00193 
00194 ////////////////////////////////////////////////////////////////////
00195 //     Function: DXGraphicsStateGuardian9::set_sampler_state
00196 //       Access:
00197 //  Description: This function creates a common layer between DX
00198 //               and Panda. It also keeps avoids setting redundant
00199 //               render states.
00200 ////////////////////////////////////////////////////////////////////
00201 INLINE HRESULT DXGraphicsStateGuardian9::
00202 set_sampler_state (DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value)
00203 {
00204   HRESULT hr;
00205 
00206   hr = D3D_OK;
00207   if (ALWAYS_SET_RENDER_STATE || _texture_render_states_array [sampler].state_array [type] != value)
00208   {
00209     hr = _d3d_device->SetSamplerState(sampler, type, value);
00210     _texture_render_states_array [sampler].state_array [type] = value;
00211   }
00212 
00213   return hr;
00214 }
00215 
00216 
00217 ////////////////////////////////////////////////////////////////////
00218 //     Function: DXGraphicsStateGuardian9::get_supports_render_texture
00219 //       Access: Published
00220 //  Description: Returns true if this particular GSG can render
00221 //               from a wdxGraphicsBuffer9 directly into a texture, or
00222 //               false if it must always copy-to-texture at the end of
00223 //               each frame to achieve this effect.
00224 ////////////////////////////////////////////////////////////////////
00225 INLINE bool DXGraphicsStateGuardian9::
00226 get_supports_render_texture() const {
00227   return _supports_render_texture;
00228 }
 All Classes Functions Variables Enumerations