dx8wrapper.h 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : ww3d *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/dx8wrapper.h $*
  25. * *
  26. * Original Author:: Jani Penttinen *
  27. * *
  28. * $Author:: Patrick $*
  29. * *
  30. * $Modtime:: 2/26/02 4:04p $*
  31. * *
  32. * $Revision:: 90 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #if defined(_MSC_VER)
  38. #pragma once
  39. #endif
  40. #ifndef DX8_WRAPPER_H
  41. #define DX8_WRAPPER_H
  42. #include "always.h"
  43. #include "dllist.h"
  44. #include "d3d8.h"
  45. #include "matrix4.h"
  46. #include "statistics.h"
  47. #include "wwstring.h"
  48. #include "lightenvironment.h"
  49. #include "shader.h"
  50. #include "vector4.h"
  51. #include "cpudetect.h"
  52. #include "dx8caps.h"
  53. #include "texture.h"
  54. #include "dx8vertexbuffer.h"
  55. #include "dx8indexbuffer.h"
  56. #include "vertmaterial.h"
  57. /*
  58. ** Registry value names
  59. */
  60. #define VALUE_NAME_RENDER_DEVICE_NAME "RenderDeviceName"
  61. #define VALUE_NAME_RENDER_DEVICE_WIDTH "RenderDeviceWidth"
  62. #define VALUE_NAME_RENDER_DEVICE_HEIGHT "RenderDeviceHeight"
  63. #define VALUE_NAME_RENDER_DEVICE_DEPTH "RenderDeviceDepth"
  64. #define VALUE_NAME_RENDER_DEVICE_WINDOWED "RenderDeviceWindowed"
  65. #define VALUE_NAME_RENDER_DEVICE_TEXTURE_DEPTH "RenderDeviceTextureDepth"
  66. const unsigned MAX_TEXTURE_STAGES=2;
  67. enum {
  68. BUFFER_TYPE_DX8,
  69. BUFFER_TYPE_SORTING,
  70. BUFFER_TYPE_DYNAMIC_DX8,
  71. BUFFER_TYPE_DYNAMIC_SORTING,
  72. BUFFER_TYPE_INVALID
  73. };
  74. class VertexMaterialClass;
  75. class CameraClass;
  76. class LightEnvironmentClass;
  77. class RenderDeviceDescClass;
  78. class VertexBufferClass;
  79. class DynamicVBAccessClass;
  80. class IndexBufferClass;
  81. class DynamicIBAccessClass;
  82. class TextureClass;
  83. class LightClass;
  84. class SurfaceClass;
  85. class DX8Caps;
  86. #define DX8_RECORD_MATRIX_CHANGE() matrix_changes++
  87. #define DX8_RECORD_MATERIAL_CHANGE() material_changes++
  88. #define DX8_RECORD_VERTEX_BUFFER_CHANGE() vertex_buffer_changes++
  89. #define DX8_RECORD_INDEX_BUFFER_CHANGE() index_buffer_changes++
  90. #define DX8_RECORD_LIGHT_CHANGE() light_changes++
  91. #define DX8_RECORD_TEXTURE_CHANGE() texture_changes++
  92. #define DX8_RECORD_RENDER_STATE_CHANGE() render_state_changes++
  93. #define DX8_RECORD_TEXTURE_STAGE_STATE_CHANGE() texture_stage_state_changes++
  94. extern unsigned number_of_DX8_calls;
  95. extern bool _DX8SingleThreaded;
  96. void DX8_Assert();
  97. void Log_DX8_ErrorCode(unsigned res);
  98. WWINLINE void DX8_ErrorCode(unsigned res)
  99. {
  100. if (res==D3D_OK) return;
  101. Log_DX8_ErrorCode(res);
  102. }
  103. #ifdef WWDEBUG
  104. #define DX8CALL_HRES(x,res) DX8_Assert(); res = DX8Wrapper::_Get_D3D_Device8()->x; DX8_ErrorCode(res); number_of_DX8_calls++;
  105. #define DX8CALL(x) DX8_Assert(); DX8_ErrorCode(DX8Wrapper::_Get_D3D_Device8()->x); number_of_DX8_calls++;
  106. #define DX8CALL_D3D(x) DX8_Assert(); DX8_ErrorCode(DX8Wrapper::_Get_D3D8()->x); number_of_DX8_calls++;
  107. #define DX8_THREAD_ASSERT() if (_DX8SingleThreaded) { WWASSERT_PRINT(DX8Wrapper::_Get_Main_Thread_ID()==ThreadClass::_Get_Current_Thread_ID(),"DX8Wrapper::DX8 calls must be called from the main thread!"); }
  108. #else
  109. #define DX8CALL_HRES(x,res) res = DX8Wrapper::_Get_D3D_Device8()->x; number_of_DX8_calls++;
  110. #define DX8CALL(x) DX8Wrapper::_Get_D3D_Device8()->x; number_of_DX8_calls++;
  111. #define DX8CALL_D3D(x) DX8Wrapper::_Get_D3D8()->x; number_of_DX8_calls++;
  112. #define DX8_THREAD_ASSERT() ;
  113. #endif
  114. struct RenderStateStruct
  115. {
  116. ShaderClass shader;
  117. VertexMaterialClass* material;
  118. TextureClass * Textures[MAX_TEXTURE_STAGES];
  119. D3DLIGHT8 Lights[4];
  120. bool LightEnable[4];
  121. Matrix4 world;
  122. Matrix4 view;
  123. unsigned vertex_buffer_type;
  124. unsigned index_buffer_type;
  125. unsigned short vba_offset;
  126. unsigned short vba_count;
  127. unsigned short iba_offset;
  128. VertexBufferClass* vertex_buffer;
  129. IndexBufferClass* index_buffer;
  130. unsigned short index_base_offset;
  131. RenderStateStruct();
  132. ~RenderStateStruct();
  133. RenderStateStruct& operator= (const RenderStateStruct& src);
  134. };
  135. /**
  136. ** DX8Wrapper
  137. **
  138. ** DX8 interface wrapper class. This encapsulates the DX8 interface; adding redundant state
  139. ** detection, stat tracking, etc etc. In general, we will wrap all DX8 calls with at least
  140. ** an WWINLINE function so that we can add stat tracking, etc if needed. Direct access to the
  141. ** D3D device will require "friend" status and should be granted only in extreme circumstances :-)
  142. */
  143. class DX8Wrapper
  144. {
  145. enum ChangedStates {
  146. WORLD_CHANGED = 1<<0,
  147. VIEW_CHANGED = 1<<1,
  148. LIGHT0_CHANGED = 1<<2,
  149. LIGHT1_CHANGED = 1<<3,
  150. LIGHT2_CHANGED = 1<<4,
  151. LIGHT3_CHANGED = 1<<5,
  152. TEXTURE0_CHANGED= 1<<6,
  153. TEXTURE1_CHANGED= 1<<7,
  154. TEXTURE2_CHANGED= 1<<8,
  155. TEXTURE3_CHANGED= 1<<9,
  156. MATERIAL_CHANGED= 1<<14,
  157. SHADER_CHANGED = 1<<15,
  158. VERTEX_BUFFER_CHANGED = 1<<16,
  159. INDEX_BUFFER_CHANGED = 1 << 17,
  160. WORLD_IDENTITY= 1<<18,
  161. VIEW_IDENTITY= 1<<19,
  162. TEXTURES_CHANGED=
  163. TEXTURE0_CHANGED|TEXTURE1_CHANGED|TEXTURE2_CHANGED|TEXTURE3_CHANGED,
  164. LIGHTS_CHANGED=
  165. LIGHT0_CHANGED|LIGHT1_CHANGED|LIGHT2_CHANGED|LIGHT3_CHANGED,
  166. };
  167. static void Draw_Sorting_IB_VB(
  168. unsigned primitive_type,
  169. unsigned short start_index,
  170. unsigned short polygon_count,
  171. unsigned short min_vertex_index,
  172. unsigned short vertex_count);
  173. static void Draw(
  174. unsigned primitive_type,
  175. unsigned short start_index,
  176. unsigned short polygon_count,
  177. unsigned short min_vertex_index=0,
  178. unsigned short vertex_count=0);
  179. public:
  180. static bool Init(void * hwnd, bool lite = false);
  181. static void Shutdown(void);
  182. /*
  183. ** Some WW3D sub-systems need to be initialized after the device is created and shutdown
  184. ** before the device is released.
  185. */
  186. static void Do_Onetime_Device_Dependent_Inits(void);
  187. static void Do_Onetime_Device_Dependent_Shutdowns(void);
  188. static bool Is_Device_Lost() { return IsDeviceLost; }
  189. static bool Is_Initted(void) { return IsInitted; }
  190. /*
  191. ** Rendering
  192. */
  193. static void Begin_Scene(void);
  194. static void End_Scene(bool flip_frame = true);
  195. // Flip until the primary buffer is visible.
  196. static void Flip_To_Primary(void);
  197. static void Clear(bool clear_color, bool clear_z_stencil, const Vector3 &color, float z=1.0f, unsigned int stencil=0);
  198. static void Set_Viewport(CONST D3DVIEWPORT8* pViewport);
  199. static void Set_Vertex_Buffer(const VertexBufferClass* vb);
  200. static void Set_Vertex_Buffer(const DynamicVBAccessClass& vba);
  201. static void Set_Index_Buffer(const IndexBufferClass* ib,unsigned short index_base_offset);
  202. static void Set_Index_Buffer(const DynamicIBAccessClass& iba,unsigned short index_base_offset);
  203. static void Set_Index_Buffer_Index_Offset(unsigned offset);
  204. static void Get_Render_State(RenderStateStruct& state);
  205. static void Set_Render_State(const RenderStateStruct& state);
  206. static void Release_Render_State();
  207. static void Set_DX8_Material(const D3DMATERIAL8* mat);
  208. static void Set_Gamma(float gamma,float bright,float contrast,bool calibrate=true,bool uselimit=true);
  209. // Set_ and Get_Transform() functions take the matrix in Westwood convention format.
  210. static void Set_DX8_ZBias(int zbias);
  211. static void Set_Pseudo_ZBias(int zbias);
  212. static void Set_Projection_Transform_With_Z_Bias(const Matrix4& matrix,float znear, float zfar); // pointer to 16 matrices
  213. static void Set_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix4& m);
  214. static void Set_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix3D& m);
  215. static void Get_Transform(D3DTRANSFORMSTATETYPE transform, Matrix4& m);
  216. static void Set_World_Identity();
  217. static void Set_View_Identity();
  218. static bool Is_World_Identity();
  219. static bool Is_View_Identity();
  220. // Note that *_DX8_Transform() functions take the matrix in DX8 format - transposed from Westwood convention.
  221. static void _Set_DX8_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix4& m);
  222. static void _Set_DX8_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix3D& m);
  223. static void _Get_DX8_Transform(D3DTRANSFORMSTATETYPE transform, Matrix4& m);
  224. static void Set_DX8_Light(int index,D3DLIGHT8* light);
  225. static void Set_DX8_Render_State(D3DRENDERSTATETYPE state, unsigned value);
  226. static void Set_DX8_Texture_Stage_State(unsigned stage, D3DTEXTURESTAGESTATETYPE state, unsigned value);
  227. static void Set_DX8_Texture(unsigned int stage, IDirect3DBaseTexture8* texture);
  228. static void Set_Light_Environment(LightEnvironmentClass* light_env);
  229. static void Set_Fog(bool enable, const Vector3 &color, float start, float end);
  230. static WWINLINE const D3DLIGHT8& Peek_Light(unsigned index);
  231. static WWINLINE bool Is_Light_Enabled(unsigned index);
  232. // Deferred
  233. static void Set_Shader(const ShaderClass& shader);
  234. static void Get_Shader(ShaderClass& shader);
  235. static void Set_Texture(unsigned stage,TextureClass* texture);
  236. static void Set_Material(const VertexMaterialClass* material);
  237. static void Set_Light(unsigned index,const D3DLIGHT8* light);
  238. static void Set_Light(unsigned index,const LightClass &light);
  239. static void Apply_Render_State_Changes(); // Apply deferred render state changes (will be called automatically by Draw...)
  240. static void Draw_Triangles(
  241. unsigned buffer_type,
  242. unsigned short start_index,
  243. unsigned short polygon_count,
  244. unsigned short min_vertex_index,
  245. unsigned short vertex_count);
  246. static void Draw_Triangles(
  247. unsigned short start_index,
  248. unsigned short polygon_count,
  249. unsigned short min_vertex_index,
  250. unsigned short vertex_count);
  251. static void Draw_Strip(
  252. unsigned short start_index,
  253. unsigned short index_count,
  254. unsigned short min_vertex_index,
  255. unsigned short vertex_count);
  256. /*
  257. ** Resources
  258. */
  259. static IDirect3DTexture8 * _Create_DX8_Texture(
  260. unsigned int width,
  261. unsigned int height,
  262. WW3DFormat format,
  263. TextureClass::MipCountType mip_level_count,
  264. D3DPOOL pool=D3DPOOL_MANAGED,
  265. bool rendertarget=false);
  266. static IDirect3DTexture8 * _Create_DX8_Texture(const char *filename, TextureClass::MipCountType mip_level_count);
  267. static IDirect3DTexture8 * _Create_DX8_Texture(IDirect3DSurface8 *surface, TextureClass::MipCountType mip_level_count);
  268. static IDirect3DSurface8 * _Create_DX8_Surface(unsigned int width, unsigned int height, WW3DFormat format);
  269. static IDirect3DSurface8 * _Create_DX8_Surface(const char *filename);
  270. static IDirect3DSurface8 * _Get_DX8_Front_Buffer();
  271. static SurfaceClass * _Get_DX8_Back_Buffer(unsigned int num=0);
  272. static void _Copy_DX8_Rects(
  273. IDirect3DSurface8* pSourceSurface,
  274. CONST RECT* pSourceRectsArray,
  275. UINT cRects,
  276. IDirect3DSurface8* pDestinationSurface,
  277. CONST POINT* pDestPointsArray
  278. );
  279. static void _Update_Texture(TextureClass *system, TextureClass *video);
  280. static void Flush_DX8_Resource_Manager(unsigned int bytes=0);
  281. static unsigned int Get_Free_Texture_RAM();
  282. static unsigned _Get_Main_Thread_ID() { return _MainThreadID; }
  283. static const D3DADAPTER_IDENTIFIER8& Get_Current_Adapter_Identifier() { return CurrentAdapterIdentifier; }
  284. /*
  285. ** Statistics
  286. */
  287. static void Begin_Statistics();
  288. static void End_Statistics();
  289. static unsigned Get_Last_Frame_Matrix_Changes();
  290. static unsigned Get_Last_Frame_Material_Changes();
  291. static unsigned Get_Last_Frame_Vertex_Buffer_Changes();
  292. static unsigned Get_Last_Frame_Index_Buffer_Changes();
  293. static unsigned Get_Last_Frame_Light_Changes();
  294. static unsigned Get_Last_Frame_Texture_Changes();
  295. static unsigned Get_Last_Frame_Render_State_Changes();
  296. static unsigned Get_Last_Frame_Texture_Stage_State_Changes();
  297. static unsigned Get_Last_Frame_DX8_Calls();
  298. static unsigned long Get_FrameCount(void);
  299. // Needed by shader class
  300. static bool Get_Fog_Enable() { return FogEnable; }
  301. static D3DCOLOR Get_Fog_Color() { return FogColor; }
  302. // Utilities
  303. static Vector4 Convert_Color(unsigned color);
  304. static unsigned int Convert_Color(const Vector4& color);
  305. static unsigned int Convert_Color(const Vector3& color, const float alpha);
  306. static void Clamp_Color(Vector4& color);
  307. static unsigned int Convert_Color_Clamp(const Vector4& color);
  308. static void Set_Alpha (const float alpha, unsigned int &color);
  309. static void _Enable_Triangle_Draw(bool enable) { _EnableTriangleDraw=enable; }
  310. static bool _Is_Triangle_Draw_Enabled() { return _EnableTriangleDraw; }
  311. /*
  312. ** Additional swap chain interface
  313. **
  314. ** Use this interface to render to multiple windows (in windowed mode).
  315. ** To render to an additional window, the sequence of calls should look
  316. ** something like this:
  317. **
  318. ** DX8Wrapper::Set_Render_Target (swap_chain_ptr);
  319. **
  320. ** WW3D::Begin_Render (true, true, Vector3 (0, 0, 0));
  321. ** WW3D::Render (scene, camera, FALSE, FALSE);
  322. ** WW3D::End_Render ();
  323. **
  324. ** swap_chain_ptr->Present (NULL, NULL, NULL, NULL);
  325. **
  326. ** DX8Wrapper::Set_Render_Target ((IDirect3DSurface8 *)NULL);
  327. **
  328. */
  329. static IDirect3DSwapChain8 * Create_Additional_Swap_Chain (HWND render_window);
  330. /*
  331. ** Render target interface. If render target format is WW3D_FORMAT_UNKNOWN, current display format is used.
  332. */
  333. static TextureClass * Create_Render_Target (int width, int height, WW3DFormat format);
  334. static void Set_Render_Target (TextureClass * texture);
  335. static void Set_Render_Target (IDirect3DSurface8 *render_target, bool use_default_depth_buffer = false);
  336. static void Set_Render_Target (IDirect3DSwapChain8 *swap_chain);
  337. static bool Is_Render_To_Texture(void) { return IsRenderToTexture; }
  338. static IDirect3DDevice8* _Get_D3D_Device8() { return D3DDevice; }
  339. static IDirect3D8* _Get_D3D8() { return D3DInterface; }
  340. static const DX8Caps* Get_Current_Caps() { WWASSERT(CurrentCaps); return CurrentCaps; }
  341. static bool Registry_Save_Render_Device( const char * sub_key );
  342. static bool Registry_Load_Render_Device( const char * sub_key, bool resize_window );
  343. static const char* Get_DX8_Render_State_Name(D3DRENDERSTATETYPE state);
  344. static const char* Get_DX8_Texture_Stage_State_Name(D3DTEXTURESTAGESTATETYPE state);
  345. // Names of the specific values of render states and texture stage states
  346. static void Get_DX8_Texture_Stage_State_Value_Name(StringClass& name, D3DTEXTURESTAGESTATETYPE state, unsigned value);
  347. static void Get_DX8_Render_State_Value_Name(StringClass& name, D3DRENDERSTATETYPE state, unsigned value);
  348. static const char* Get_DX8_Texture_Address_Name(unsigned value);
  349. static const char* Get_DX8_Texture_Filter_Name(unsigned value);
  350. static const char* Get_DX8_Texture_Arg_Name(unsigned value);
  351. static const char* Get_DX8_Texture_Op_Name(unsigned value);
  352. static const char* Get_DX8_Texture_Transform_Flag_Name(unsigned value);
  353. static const char* Get_DX8_ZBuffer_Type_Name(unsigned value);
  354. static const char* Get_DX8_Fill_Mode_Name(unsigned value);
  355. static const char* Get_DX8_Shade_Mode_Name(unsigned value);
  356. static const char* Get_DX8_Blend_Name(unsigned value);
  357. static const char* Get_DX8_Cull_Mode_Name(unsigned value);
  358. static const char* Get_DX8_Cmp_Func_Name(unsigned value);
  359. static const char* Get_DX8_Fog_Mode_Name(unsigned value);
  360. static const char* Get_DX8_Stencil_Op_Name(unsigned value);
  361. static const char* Get_DX8_Material_Source_Name(unsigned value);
  362. static const char* Get_DX8_Vertex_Blend_Flag_Name(unsigned value);
  363. static const char* Get_DX8_Patch_Edge_Style_Name(unsigned value);
  364. static const char* Get_DX8_Debug_Monitor_Token_Name(unsigned value);
  365. static const char* Get_DX8_Blend_Op_Name(unsigned value);
  366. protected:
  367. static bool Create_Device(void);
  368. static bool Reset_Device(void);
  369. static void Release_Device(void);
  370. static void Reset_Statistics();
  371. static void Enumerate_Devices();
  372. static void Set_Default_Global_Render_States(void);
  373. static void Invalidate_Cached_Render_States(void);
  374. /*
  375. ** Device Selection Code.
  376. ** For backward compatibility, the public interface for these functions is in the ww3d.
  377. ** header file. These functions are protected so that we aren't exposing two interfaces.
  378. */
  379. static bool Set_Any_Render_Device(void);
  380. static bool Set_Render_Device(const char * dev_name,int width=-1,int height=-1,int bits=-1,int windowed=-1,bool resize_window=false);
  381. static bool Set_Render_Device(int dev=-1,int resx=-1,int resy=-1,int bits=-1,int windowed=-1,bool resize_window = false);
  382. static bool Set_Next_Render_Device(void);
  383. static bool Toggle_Windowed(void);
  384. static int Get_Render_Device_Count(void);
  385. static int Get_Render_Device(void);
  386. static const RenderDeviceDescClass & Get_Render_Device_Desc(int deviceidx);
  387. static const char * Get_Render_Device_Name(int device_index);
  388. static bool Set_Device_Resolution(int width=-1,int height=-1,int bits=-1,int windowed=-1, bool resize_window=false);
  389. static void Get_Device_Resolution(int & set_w,int & set_h,int & set_bits,bool & set_windowed);
  390. static void Get_Render_Target_Resolution(int & set_w,int & set_h,int & set_bits,bool & set_windowed);
  391. static int Get_Device_Resolution_Width(void) { return ResolutionWidth; }
  392. static int Get_Device_Resolution_Height(void) { return ResolutionHeight; }
  393. static bool Registry_Save_Render_Device( const char *sub_key, int device, int width, int height, int depth, bool windowed, int texture_depth);
  394. static bool Registry_Load_Render_Device( const char * sub_key, char *device, int device_len, int &width, int &height, int &depth, int &windowed, int &texture_depth);
  395. static bool Is_Windowed(void) { return IsWindowed; }
  396. static void Set_Texture_Bitdepth(int depth) { WWASSERT(depth==16 || depth==32); TextureBitDepth = depth; }
  397. static int Get_Texture_Bitdepth(void) { return TextureBitDepth; }
  398. static void Set_Swap_Interval(int swap);
  399. static int Get_Swap_Interval(void);
  400. static void Set_Polygon_Mode(int mode);
  401. /*
  402. ** Internal functions
  403. */
  404. static bool Find_Color_And_Z_Mode(int resx,int resy,int bitdepth,D3DFORMAT * set_colorbuffer,D3DFORMAT * set_zmode);
  405. static bool Find_Color_Mode(D3DFORMAT colorbuffer, int resx, int resy, UINT *mode);
  406. static bool Find_Z_Mode(D3DFORMAT colorbuffer,D3DFORMAT backbuffer, D3DFORMAT *zmode);
  407. static bool Test_Z_Mode(D3DFORMAT colorbuffer,D3DFORMAT backbuffer, D3DFORMAT zmode);
  408. static void Compute_Caps(WW3DFormat display_format);
  409. /*
  410. ** Protected Member Variables
  411. */
  412. static RenderStateStruct render_state;
  413. static unsigned render_state_changed;
  414. static bool IsInitted;
  415. static bool IsDeviceLost;
  416. static void * Hwnd;
  417. static unsigned _MainThreadID;
  418. static bool _EnableTriangleDraw;
  419. static int CurRenderDevice;
  420. static int ResolutionWidth;
  421. static int ResolutionHeight;
  422. static int BitDepth;
  423. static int TextureBitDepth;
  424. static bool IsWindowed;
  425. static D3DMATRIX old_world;
  426. static D3DMATRIX old_view;
  427. static D3DMATRIX old_prj;
  428. static bool world_identity;
  429. static unsigned RenderStates[256];
  430. static unsigned TextureStageStates[MAX_TEXTURE_STAGES][32];
  431. static IDirect3DBaseTexture8 * Textures[MAX_TEXTURE_STAGES];
  432. // These fog settings are constant for all objects in a given scene,
  433. // unlike the matching renderstates which vary based on shader settings.
  434. static bool FogEnable;
  435. static D3DCOLOR FogColor;
  436. static unsigned matrix_changes;
  437. static unsigned material_changes;
  438. static unsigned vertex_buffer_changes;
  439. static unsigned index_buffer_changes;
  440. static unsigned light_changes;
  441. static unsigned texture_changes;
  442. static unsigned render_state_changes;
  443. static unsigned texture_stage_state_changes;
  444. static bool CurrentDX8LightEnables[4];
  445. static unsigned long FrameCount;
  446. static DX8Caps* CurrentCaps;
  447. static D3DADAPTER_IDENTIFIER8 CurrentAdapterIdentifier;
  448. static IDirect3D8 * D3DInterface; //d3d8;
  449. static IDirect3DDevice8 * D3DDevice; //d3ddevice8;
  450. static IDirect3DSurface8 * CurrentRenderTarget;
  451. static IDirect3DSurface8 * DefaultRenderTarget;
  452. static IDirect3DSurface8 * DefaultDepthBuffer;
  453. static bool IsRenderToTexture;
  454. static int ZBias;
  455. static float ZNear;
  456. static float ZFar;
  457. static Matrix4 ProjectionMatrix;
  458. friend void DX8_Assert();
  459. friend class WW3D;
  460. friend class DX8IndexBufferClass;
  461. friend class DX8VertexBufferClass;
  462. };
  463. WWINLINE void DX8Wrapper::_Set_DX8_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix4& m)
  464. {
  465. SNAPSHOT_SAY(("DX8 - SetTransform\n"));
  466. DX8_RECORD_MATRIX_CHANGE();
  467. DX8CALL(SetTransform(transform,(D3DMATRIX*)&m));
  468. }
  469. WWINLINE void DX8Wrapper::_Set_DX8_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix3D& m)
  470. {
  471. SNAPSHOT_SAY(("DX8 - SetTransform\n"));
  472. DX8_RECORD_MATRIX_CHANGE();
  473. DX8CALL(SetTransform(transform,(D3DMATRIX*)&m));
  474. }
  475. WWINLINE void DX8Wrapper::_Get_DX8_Transform(D3DTRANSFORMSTATETYPE transform, Matrix4& m)
  476. {
  477. DX8CALL(GetTransform(transform,(D3DMATRIX*)&m));
  478. }
  479. // ----------------------------------------------------------------------------
  480. //
  481. // Set the index offset for the current index buffer
  482. //
  483. // ----------------------------------------------------------------------------
  484. WWINLINE void DX8Wrapper::Set_Index_Buffer_Index_Offset(unsigned offset)
  485. {
  486. if (render_state.index_base_offset==offset) return;
  487. render_state.index_base_offset=offset;
  488. render_state_changed|=INDEX_BUFFER_CHANGED;
  489. }
  490. // ----------------------------------------------------------------------------
  491. // Set the fog settings. This function should be used, rather than setting the
  492. // appropriate renderstates directly, because the shader sets some of the
  493. // renderstates on a per-mesh / per-pass basis depending on global fog states
  494. // (stored in the wrapper) as well as the shader settings.
  495. // This function should be called rarely - once per scene would be appropriate.
  496. // ----------------------------------------------------------------------------
  497. WWINLINE void DX8Wrapper::Set_Fog(bool enable, const Vector3 &color, float start, float end)
  498. {
  499. // Set global states
  500. FogEnable = enable;
  501. FogColor = Convert_Color(color,0.0f);
  502. // Invalidate the current shader (since the renderstates set by the shader
  503. // depend on the global fog settings as well as the actual shader settings)
  504. ShaderClass::Invalidate();
  505. // Set renderstates which are not affected by the shader
  506. Set_DX8_Render_State(D3DRS_FOGSTART, *(DWORD *)(&start));
  507. Set_DX8_Render_State(D3DRS_FOGEND, *(DWORD *)(&end));
  508. }
  509. // ----------------------------------------------------------------------------
  510. //
  511. // Set vertex buffer to be used in the subsequent render calls. If there was
  512. // a vertex buffer being used earlier, release the reference to it. Passing
  513. // NULL just will release the vertex buffer.
  514. //
  515. // ----------------------------------------------------------------------------
  516. WWINLINE void DX8Wrapper::Set_DX8_Material(const D3DMATERIAL8* mat)
  517. {
  518. DX8_RECORD_MATERIAL_CHANGE();
  519. WWASSERT(mat);
  520. SNAPSHOT_SAY(("DX8 - SetMaterial\n"));
  521. DX8CALL(SetMaterial(mat));
  522. }
  523. WWINLINE void DX8Wrapper::Set_DX8_Light(int index, D3DLIGHT8* light)
  524. {
  525. if (light) {
  526. DX8_RECORD_LIGHT_CHANGE();
  527. DX8CALL(SetLight(index,light));
  528. DX8CALL(LightEnable(index,TRUE));
  529. CurrentDX8LightEnables[index]=true;
  530. SNAPSHOT_SAY(("DX8 - SetLight\n"));
  531. }
  532. else if (CurrentDX8LightEnables[index]) {
  533. DX8_RECORD_LIGHT_CHANGE();
  534. CurrentDX8LightEnables[index]=false;
  535. DX8CALL(LightEnable(index,FALSE));
  536. SNAPSHOT_SAY(("DX8 - DisableLight\n"));
  537. }
  538. }
  539. WWINLINE void DX8Wrapper::Set_DX8_Render_State(D3DRENDERSTATETYPE state, unsigned value)
  540. {
  541. // Can't monitor state changes because setShader call to GERD may change the states!
  542. if (RenderStates[state]==value) return;
  543. #ifdef MESH_RENDER_SNAPSHOT_ENABLED
  544. if (WW3D::Is_Snapshot_Activated()) {
  545. StringClass value_name(0,true);
  546. Get_DX8_Render_State_Value_Name(value_name,state,value);
  547. SNAPSHOT_SAY(("DX8 - SetRenderState(state: %s, value: %s)\n",
  548. Get_DX8_Render_State_Name(state),
  549. value_name));
  550. }
  551. #endif
  552. RenderStates[state]=value;
  553. DX8CALL(SetRenderState( state, value ));
  554. DX8_RECORD_RENDER_STATE_CHANGE();
  555. }
  556. WWINLINE void DX8Wrapper::Set_DX8_Texture_Stage_State(unsigned stage, D3DTEXTURESTAGESTATETYPE state, unsigned value)
  557. {
  558. // Can't monitor state changes because setShader call to GERD may change the states!
  559. if (TextureStageStates[stage][(unsigned int)state]==value) return;
  560. #ifdef MESH_RENDER_SNAPSHOT_ENABLED
  561. if (WW3D::Is_Snapshot_Activated()) {
  562. StringClass value_name(0,true);
  563. Get_DX8_Texture_Stage_State_Value_Name(value_name,state,value);
  564. SNAPSHOT_SAY(("DX8 - SetTextureStageState(stage: %d, state: %s, value: %s)\n",
  565. stage,
  566. Get_DX8_Texture_Stage_State_Name(state),
  567. value_name));
  568. }
  569. #endif
  570. TextureStageStates[stage][(unsigned int)state]=value;
  571. DX8CALL(SetTextureStageState( stage, state, value ));
  572. DX8_RECORD_TEXTURE_STAGE_STATE_CHANGE();
  573. }
  574. WWINLINE void DX8Wrapper::Set_DX8_Texture(unsigned int stage, IDirect3DBaseTexture8* texture)
  575. {
  576. if (Textures[stage]==texture) return;
  577. SNAPSHOT_SAY(("DX8 - SetTexture(%x) \n",texture));
  578. if (Textures[stage]) Textures[stage]->Release();
  579. Textures[stage] = texture;
  580. if (Textures[stage]) Textures[stage]->AddRef();
  581. DX8CALL(SetTexture(stage, texture));
  582. DX8_RECORD_TEXTURE_CHANGE();
  583. }
  584. WWINLINE void DX8Wrapper::_Copy_DX8_Rects(
  585. IDirect3DSurface8* pSourceSurface,
  586. CONST RECT* pSourceRectsArray,
  587. UINT cRects,
  588. IDirect3DSurface8* pDestinationSurface,
  589. CONST POINT* pDestPointsArray
  590. )
  591. {
  592. DX8CALL(CopyRects(
  593. pSourceSurface,
  594. pSourceRectsArray,
  595. cRects,
  596. pDestinationSurface,
  597. pDestPointsArray));
  598. }
  599. WWINLINE Vector4 DX8Wrapper::Convert_Color(unsigned color)
  600. {
  601. Vector4 col;
  602. col[3]=((color&0xff000000)>>24)/255.0f;
  603. col[0]=((color&0xff0000)>>16)/255.0f;
  604. col[1]=((color&0xff00)>>8)/255.0f;
  605. col[2]=((color&0xff)>>0)/255.0f;
  606. // col=Vector4(1.0f,1.0f,1.0f,1.0f);
  607. return col;
  608. }
  609. #if 0
  610. WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector3& color, const float alpha)
  611. {
  612. WWASSERT(color.X<=1.0f);
  613. WWASSERT(color.Y<=1.0f);
  614. WWASSERT(color.Z<=1.0f);
  615. WWASSERT(alpha<=1.0f);
  616. WWASSERT(color.X>=0.0f);
  617. WWASSERT(color.Y>=0.0f);
  618. WWASSERT(color.Z>=0.0f);
  619. WWASSERT(alpha>=0.0f);
  620. return D3DCOLOR_COLORVALUE(color.X,color.Y,color.Z,alpha);
  621. }
  622. WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector4& color)
  623. {
  624. WWASSERT(color.X<=1.0f);
  625. WWASSERT(color.Y<=1.0f);
  626. WWASSERT(color.Z<=1.0f);
  627. WWASSERT(color.W<=1.0f);
  628. WWASSERT(color.X>=0.0f);
  629. WWASSERT(color.Y>=0.0f);
  630. WWASSERT(color.Z>=0.0f);
  631. WWASSERT(color.W>=0.0f);
  632. return D3DCOLOR_COLORVALUE(color.X,color.Y,color.Z,color.W);
  633. }
  634. #else
  635. // ----------------------------------------------------------------------------
  636. //
  637. // Convert RGBA color from float vector to 32 bit integer
  638. // Note: Color vector needs to be clamped to [0...1] range!
  639. //
  640. // ----------------------------------------------------------------------------
  641. WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector3& color,float alpha)
  642. {
  643. const float scale = 255.0;
  644. unsigned int col;
  645. // Multiply r, g, b and a components (0.0,...,1.0) by 255 and convert to integer. Or the integer values togerher
  646. // such that 32 bit ingeger has AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB.
  647. __asm
  648. {
  649. push ebp
  650. sub esp,20 // space for a, r, g and b float plus fpu rounding mode
  651. // Store the fpu rounding mode
  652. fwait
  653. fstcw [esp+16] // store control word to stack
  654. mov eax,[esp+16] // load it to eax
  655. mov edi,eax // take copy
  656. and eax,~(1024|2048) // mask out certain bits
  657. or eax,(1024|2048) // or with precision control value "truncate"
  658. sub edi,eax // did it change?
  659. jz skip // .. if not, skip
  660. mov [esp],eax // .. change control word
  661. fldcw [esp]
  662. skip:
  663. // Convert the color
  664. mov esi,dword ptr color
  665. fld dword ptr[scale]
  666. fld dword ptr[esi] // r
  667. fld dword ptr[esi+4] // g
  668. fld dword ptr[esi+8] // b
  669. fld dword ptr[alpha] // a
  670. fld st(4)
  671. fmul st(4),st
  672. fmul st(3),st
  673. fmul st(2),st
  674. fmulp st(1),st
  675. fistp dword ptr[esp+0] // a
  676. fistp dword ptr[esp+4] // b
  677. fistp dword ptr[esp+8] // g
  678. fistp dword ptr[esp+12] // r
  679. mov ebp,[esp] // a
  680. mov eax,[esp+4] // b
  681. mov edx,[esp+8] // g
  682. mov ebx,[esp+12] // r
  683. shl ebp,24 // a << 24
  684. shl ebx,16 // r << 16
  685. shl edx,8 // g << 8
  686. or eax,ebp // (a << 24) | b
  687. or eax,ebx // (a << 24) | (r << 16) | b
  688. or eax,edx // (a << 24) | (r << 16) | (g << 8) | b
  689. fstp st(0)
  690. // Restore fpu rounding mode
  691. cmp edi,0 // did we change the value?
  692. je not_changed // nope... skip now...
  693. fwait
  694. fldcw [esp+16];
  695. not_changed:
  696. add esp,20
  697. pop ebp
  698. mov col,eax
  699. }
  700. return col;
  701. }
  702. // ----------------------------------------------------------------------------
  703. //
  704. // Clamp color vertor to [0...1] range
  705. //
  706. // ----------------------------------------------------------------------------
  707. WWINLINE void DX8Wrapper::Clamp_Color(Vector4& color)
  708. {
  709. if (!CPUDetectClass::Has_CMOV_Instruction()) {
  710. for (int i=0;i<4;++i) {
  711. float f=(color[i]<0.0f) ? 0.0f : color[i];
  712. color[i]=(f>1.0f) ? 1.0f : f;
  713. }
  714. return;
  715. }
  716. __asm
  717. {
  718. mov esi,dword ptr color
  719. mov edx,0x3f800000
  720. mov edi,dword ptr[esi]
  721. mov ebx,edi
  722. sar edi,31
  723. not edi // mask is now zero if negative value
  724. and edi,ebx
  725. cmp edi,edx // if no less than 1.0 set to 1.0
  726. cmovnb edi,edx
  727. mov dword ptr[esi],edi
  728. mov edi,dword ptr[esi+4]
  729. mov ebx,edi
  730. sar edi,31
  731. not edi // mask is now zero if negative value
  732. and edi,ebx
  733. cmp edi,edx // if no less than 1.0 set to 1.0
  734. cmovnb edi,edx
  735. mov dword ptr[esi+4],edi
  736. mov edi,dword ptr[esi+8]
  737. mov ebx,edi
  738. sar edi,31
  739. not edi // mask is now zero if negative value
  740. and edi,ebx
  741. cmp edi,edx // if no less than 1.0 set to 1.0
  742. cmovnb edi,edx
  743. mov dword ptr[esi+8],edi
  744. mov edi,dword ptr[esi+12]
  745. mov ebx,edi
  746. sar edi,31
  747. not edi // mask is now zero if negative value
  748. and edi,ebx
  749. cmp edi,edx // if no less than 1.0 set to 1.0
  750. cmovnb edi,edx
  751. mov dword ptr[esi+12],edi
  752. }
  753. }
  754. // ----------------------------------------------------------------------------
  755. //
  756. // Convert RGBA color from float vector to 32 bit integer
  757. //
  758. // ----------------------------------------------------------------------------
  759. WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector4& color)
  760. {
  761. return Convert_Color(reinterpret_cast<const Vector3&>(color),color[3]);
  762. }
  763. WWINLINE unsigned int DX8Wrapper::Convert_Color_Clamp(const Vector4& color)
  764. {
  765. Vector4 clamped_color=color;
  766. DX8Wrapper::Clamp_Color(clamped_color);
  767. return Convert_Color(reinterpret_cast<const Vector3&>(clamped_color),clamped_color[3]);
  768. }
  769. #endif
  770. WWINLINE void DX8Wrapper::Set_Alpha (const float alpha, unsigned int &color)
  771. {
  772. unsigned char *component = (unsigned char*) &color;
  773. component [3] = 255.0f * alpha;
  774. }
  775. WWINLINE void DX8Wrapper::Get_Render_State(RenderStateStruct& state)
  776. {
  777. state=render_state;
  778. }
  779. WWINLINE void DX8Wrapper::Get_Shader(ShaderClass& shader)
  780. {
  781. shader=render_state.shader;
  782. }
  783. WWINLINE void DX8Wrapper::Set_Texture(unsigned stage,TextureClass* texture)
  784. {
  785. WWASSERT(stage<MAX_TEXTURE_STAGES);
  786. if (texture==render_state.Textures[stage]) return;
  787. REF_PTR_SET(render_state.Textures[stage],texture);
  788. render_state_changed|=(TEXTURE0_CHANGED<<stage);
  789. }
  790. WWINLINE void DX8Wrapper::Set_Material(const VertexMaterialClass* material)
  791. {
  792. if (material==render_state.material) return;
  793. REF_PTR_SET(render_state.material,const_cast<VertexMaterialClass*>(material));
  794. render_state_changed|=MATERIAL_CHANGED;
  795. }
  796. WWINLINE void DX8Wrapper::Set_Shader(const ShaderClass& shader)
  797. {
  798. if (!ShaderClass::ShaderDirty && ((unsigned&)shader==(unsigned&)render_state.shader)) return;
  799. render_state.shader=shader;
  800. render_state_changed|=SHADER_CHANGED;
  801. }
  802. WWINLINE void DX8Wrapper::Set_Projection_Transform_With_Z_Bias(const Matrix4& matrix, float znear, float zfar)
  803. {
  804. ZFar=zfar;
  805. ZNear=znear;
  806. ProjectionMatrix=matrix.Transpose();
  807. if (!Get_Current_Caps()->Support_ZBias() && ZNear!=ZFar) {
  808. Matrix4 tmp=ProjectionMatrix;
  809. float tmp_zbias=ZBias;
  810. tmp_zbias*=(1.0f/16.0f);
  811. tmp_zbias*=1.0f / (ZFar - ZNear);
  812. tmp[2][2]-=tmp_zbias*tmp[3][2];
  813. DX8CALL(SetTransform(D3DTS_PROJECTION,(D3DMATRIX*)&tmp));
  814. }
  815. else {
  816. DX8CALL(SetTransform(D3DTS_PROJECTION,(D3DMATRIX*)&ProjectionMatrix));
  817. }
  818. }
  819. WWINLINE void DX8Wrapper::Set_Pseudo_ZBias(int zbias)
  820. {
  821. if (zbias==ZBias) return;
  822. if (zbias>15) zbias=15;
  823. if (zbias<0) zbias=0;
  824. ZBias=zbias;
  825. Matrix4 tmp=ProjectionMatrix;
  826. float tmp_zbias=ZBias;
  827. tmp_zbias*=(1.0f/64.0f);
  828. tmp_zbias*=1.0f / (ZFar - ZNear);
  829. tmp[2][2]-=tmp_zbias*tmp[3][2];
  830. DX8CALL(SetTransform(D3DTS_PROJECTION,(D3DMATRIX*)&tmp));
  831. }
  832. WWINLINE void DX8Wrapper::Set_DX8_ZBias(int zbias)
  833. {
  834. if (zbias==ZBias) return;
  835. if (zbias>15) zbias=15;
  836. if (zbias<0) zbias=0;
  837. ZBias=zbias;
  838. if (!Get_Current_Caps()->Support_ZBias() && ZNear!=ZFar) {
  839. Matrix4 tmp=ProjectionMatrix;
  840. float tmp_zbias=ZBias;
  841. tmp_zbias*=(1.0f/16.0f);
  842. tmp_zbias*=1.0f / (ZFar - ZNear);
  843. tmp[2][2]-=tmp_zbias*tmp[3][2];
  844. DX8CALL(SetTransform(D3DTS_PROJECTION,(D3DMATRIX*)&tmp));
  845. }
  846. else {
  847. Set_DX8_Render_State (D3DRS_ZBIAS, ZBias);
  848. }
  849. }
  850. WWINLINE void DX8Wrapper::Set_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix4& m)
  851. {
  852. switch ((int)transform) {
  853. case D3DTS_WORLD:
  854. render_state.world=m.Transpose();
  855. render_state_changed|=(unsigned)WORLD_CHANGED;
  856. render_state_changed&=~(unsigned)WORLD_IDENTITY;
  857. break;
  858. case D3DTS_VIEW:
  859. render_state.view=m.Transpose();
  860. render_state_changed|=(unsigned)VIEW_CHANGED;
  861. render_state_changed&=~(unsigned)VIEW_IDENTITY;
  862. break;
  863. case D3DTS_PROJECTION:
  864. {
  865. Matrix4 ProjectionMatrix=m.Transpose();
  866. ZFar=0.0f;
  867. ZNear=0.0f;
  868. DX8CALL(SetTransform(D3DTS_PROJECTION,(D3DMATRIX*)&ProjectionMatrix));
  869. }
  870. break;
  871. default:
  872. DX8_RECORD_MATRIX_CHANGE();
  873. Matrix4 m2=m.Transpose();
  874. DX8CALL(SetTransform(transform,(D3DMATRIX*)&m2));
  875. break;
  876. }
  877. }
  878. WWINLINE void DX8Wrapper::Set_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix3D& m)
  879. {
  880. Matrix4 m2(m);
  881. switch ((int)transform) {
  882. case D3DTS_WORLD:
  883. render_state.world=m2.Transpose();
  884. render_state_changed|=(unsigned)WORLD_CHANGED;
  885. render_state_changed&=~(unsigned)WORLD_IDENTITY;
  886. break;
  887. case D3DTS_VIEW:
  888. render_state.view=m2.Transpose();
  889. render_state_changed|=(unsigned)VIEW_CHANGED;
  890. render_state_changed&=~(unsigned)VIEW_IDENTITY;
  891. break;
  892. default:
  893. DX8_RECORD_MATRIX_CHANGE();
  894. m2=m2.Transpose();
  895. DX8CALL(SetTransform(transform,(D3DMATRIX*)&m2));
  896. break;
  897. }
  898. }
  899. WWINLINE void DX8Wrapper::Set_World_Identity()
  900. {
  901. if (render_state_changed&(unsigned)WORLD_IDENTITY) return;
  902. render_state.world.Make_Identity();
  903. render_state_changed|=(unsigned)WORLD_CHANGED|(unsigned)WORLD_IDENTITY;
  904. }
  905. WWINLINE void DX8Wrapper::Set_View_Identity()
  906. {
  907. if (render_state_changed&(unsigned)VIEW_IDENTITY) return;
  908. render_state.view.Make_Identity();
  909. render_state_changed|=(unsigned)VIEW_CHANGED|(unsigned)VIEW_IDENTITY;
  910. }
  911. WWINLINE bool DX8Wrapper::Is_World_Identity()
  912. {
  913. return !!(render_state_changed&(unsigned)WORLD_IDENTITY);
  914. }
  915. WWINLINE bool DX8Wrapper::Is_View_Identity()
  916. {
  917. return !!(render_state_changed&(unsigned)VIEW_IDENTITY);
  918. }
  919. WWINLINE void DX8Wrapper::Get_Transform(D3DTRANSFORMSTATETYPE transform, Matrix4& m)
  920. {
  921. D3DMATRIX mat;
  922. switch ((int)transform) {
  923. case D3DTS_WORLD:
  924. if (render_state_changed&WORLD_IDENTITY) m.Make_Identity();
  925. else m=render_state.world.Transpose();
  926. break;
  927. case D3DTS_VIEW:
  928. if (render_state_changed&VIEW_IDENTITY) m.Make_Identity();
  929. else m=render_state.view.Transpose();
  930. break;
  931. default:
  932. DX8CALL(GetTransform(transform,&mat));
  933. m=*(Matrix4*)&mat;
  934. m=m.Transpose();
  935. break;
  936. }
  937. }
  938. WWINLINE void DX8Wrapper::Set_Light(unsigned index, const D3DLIGHT8* light)
  939. {
  940. if (light) {
  941. render_state.Lights[index]=*light;
  942. render_state.LightEnable[index]=true;
  943. }
  944. else {
  945. render_state.LightEnable[index]=false;
  946. }
  947. render_state_changed|=(LIGHT0_CHANGED<<index);
  948. }
  949. WWINLINE const D3DLIGHT8& DX8Wrapper::Peek_Light(unsigned index)
  950. {
  951. return render_state.Lights[index];;
  952. }
  953. WWINLINE bool DX8Wrapper::Is_Light_Enabled(unsigned index)
  954. {
  955. return render_state.LightEnable[index];
  956. }
  957. WWINLINE void DX8Wrapper::Set_Render_State(const RenderStateStruct& state)
  958. {
  959. if (render_state.index_buffer) {
  960. render_state.index_buffer->Release_Engine_Ref();
  961. }
  962. if (render_state.vertex_buffer) {
  963. render_state.vertex_buffer->Release_Engine_Ref();
  964. }
  965. render_state=state;
  966. render_state_changed=0xffffffff;
  967. if (render_state.index_buffer) {
  968. render_state.index_buffer->Add_Engine_Ref();
  969. }
  970. if (render_state.vertex_buffer) {
  971. render_state.vertex_buffer->Add_Engine_Ref();
  972. }
  973. }
  974. WWINLINE void DX8Wrapper::Release_Render_State()
  975. {
  976. if (render_state.index_buffer) {
  977. render_state.index_buffer->Release_Engine_Ref();
  978. }
  979. if (render_state.vertex_buffer) {
  980. render_state.vertex_buffer->Release_Engine_Ref();
  981. }
  982. REF_PTR_RELEASE(render_state.vertex_buffer);
  983. REF_PTR_RELEASE(render_state.index_buffer);
  984. REF_PTR_RELEASE(render_state.material);
  985. for (unsigned i=0;i<MAX_TEXTURE_STAGES;++i) REF_PTR_RELEASE(render_state.Textures[i]);
  986. }
  987. WWINLINE RenderStateStruct::RenderStateStruct()
  988. :
  989. material(0),
  990. vertex_buffer(0),
  991. index_buffer(0)
  992. {
  993. for (unsigned i=0;i<MAX_TEXTURE_STAGES;++i) Textures[i]=0;
  994. }
  995. WWINLINE RenderStateStruct::~RenderStateStruct()
  996. {
  997. REF_PTR_RELEASE(material);
  998. REF_PTR_RELEASE(vertex_buffer);
  999. REF_PTR_RELEASE(index_buffer);
  1000. for (unsigned i=0;i<MAX_TEXTURE_STAGES;++i) REF_PTR_RELEASE(Textures[i]);
  1001. }
  1002. WWINLINE RenderStateStruct& RenderStateStruct::operator= (const RenderStateStruct& src)
  1003. {
  1004. REF_PTR_SET(material,src.material);
  1005. REF_PTR_SET(vertex_buffer,src.vertex_buffer);
  1006. REF_PTR_SET(index_buffer,src.index_buffer);
  1007. for (unsigned i=0;i<MAX_TEXTURE_STAGES;++i) REF_PTR_SET(Textures[i],src.Textures[i]);
  1008. LightEnable[0]=src.LightEnable[0];
  1009. LightEnable[1]=src.LightEnable[1];
  1010. LightEnable[2]=src.LightEnable[2];
  1011. LightEnable[3]=src.LightEnable[3];
  1012. if (LightEnable[0]) {
  1013. Lights[0]=src.Lights[0];
  1014. if (LightEnable[1]) {
  1015. Lights[1]=src.Lights[1];
  1016. if (LightEnable[2]) {
  1017. Lights[2]=src.Lights[2];
  1018. if (LightEnable[3]) {
  1019. Lights[3]=src.Lights[3];
  1020. }
  1021. }
  1022. }
  1023. }
  1024. shader=src.shader;
  1025. world=src.world;
  1026. view=src.view;
  1027. vertex_buffer_type=src.vertex_buffer_type;
  1028. index_buffer_type=src.index_buffer_type;
  1029. vba_offset=src.vba_offset;
  1030. vba_count=src.vba_count;
  1031. iba_offset=src.iba_offset;
  1032. index_base_offset=src.index_base_offset;
  1033. return *this;
  1034. }
  1035. #endif