dx8wrapper.h 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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 : Kenny Mitchell *
  29. * *
  30. * $Modtime:: 08/05/02 2:40p $*
  31. * *
  32. * $Revision:: 92 $*
  33. * *
  34. * 06/26/02 KM Matrix name change to avoid MAX conflicts *
  35. * 06/27/02 KM Render to shadow buffer texture support *
  36. * 08/05/02 KM Texture class redesign
  37. *---------------------------------------------------------------------------------------------*
  38. * Functions: *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #if defined(_MSC_VER)
  41. #pragma once
  42. #endif
  43. #ifndef DX8_WRAPPER_H
  44. #define DX8_WRAPPER_H
  45. #include "always.h"
  46. #include "dllist.h"
  47. #include "d3d8.h"
  48. #include "matrix4.h"
  49. #include "statistics.h"
  50. #include "wwstring.h"
  51. #include "lightenvironment.h"
  52. #include "shader.h"
  53. #include "vector4.h"
  54. #include "cpudetect.h"
  55. #include "dx8caps.h"
  56. #include "texture.h"
  57. #include "dx8vertexbuffer.h"
  58. #include "dx8indexbuffer.h"
  59. #include "vertmaterial.h"
  60. /*
  61. ** Registry value names
  62. */
  63. #define VALUE_NAME_RENDER_DEVICE_NAME "RenderDeviceName"
  64. #define VALUE_NAME_RENDER_DEVICE_WIDTH "RenderDeviceWidth"
  65. #define VALUE_NAME_RENDER_DEVICE_HEIGHT "RenderDeviceHeight"
  66. #define VALUE_NAME_RENDER_DEVICE_DEPTH "RenderDeviceDepth"
  67. #define VALUE_NAME_RENDER_DEVICE_WINDOWED "RenderDeviceWindowed"
  68. #define VALUE_NAME_RENDER_DEVICE_TEXTURE_DEPTH "RenderDeviceTextureDepth"
  69. const unsigned MAX_TEXTURE_STAGES=8;
  70. const unsigned MAX_VERTEX_STREAMS=2;
  71. const unsigned MAX_VERTEX_SHADER_CONSTANTS=96;
  72. const unsigned MAX_PIXEL_SHADER_CONSTANTS=8;
  73. const unsigned MAX_SHADOW_MAPS=1;
  74. #define prevVer
  75. #define nextVer
  76. #define __volatile unsigned
  77. enum {
  78. BUFFER_TYPE_DX8,
  79. BUFFER_TYPE_SORTING,
  80. BUFFER_TYPE_DYNAMIC_DX8,
  81. BUFFER_TYPE_DYNAMIC_SORTING,
  82. BUFFER_TYPE_INVALID
  83. };
  84. class VertexMaterialClass;
  85. class CameraClass;
  86. class LightEnvironmentClass;
  87. class RenderDeviceDescClass;
  88. class VertexBufferClass;
  89. class DynamicVBAccessClass;
  90. class IndexBufferClass;
  91. class DynamicIBAccessClass;
  92. class TextureClass;
  93. class ZTextureClass;
  94. class LightClass;
  95. class SurfaceClass;
  96. class DX8Caps;
  97. #define DX8_RECORD_MATRIX_CHANGE() matrix_changes++
  98. #define DX8_RECORD_MATERIAL_CHANGE() material_changes++
  99. #define DX8_RECORD_VERTEX_BUFFER_CHANGE() vertex_buffer_changes++
  100. #define DX8_RECORD_INDEX_BUFFER_CHANGE() index_buffer_changes++
  101. #define DX8_RECORD_LIGHT_CHANGE() light_changes++
  102. #define DX8_RECORD_TEXTURE_CHANGE() texture_changes++
  103. #define DX8_RECORD_RENDER_STATE_CHANGE() render_state_changes++
  104. #define DX8_RECORD_TEXTURE_STAGE_STATE_CHANGE() texture_stage_state_changes++
  105. #define DX8_RECORD_DRAW_CALLS() draw_calls++
  106. extern unsigned number_of_DX8_calls;
  107. extern bool _DX8SingleThreaded;
  108. void DX8_Assert();
  109. void Log_DX8_ErrorCode(unsigned res);
  110. WWINLINE void DX8_ErrorCode(unsigned res)
  111. {
  112. if (res==D3D_OK) return;
  113. Log_DX8_ErrorCode(res);
  114. }
  115. #ifdef WWDEBUG
  116. #define DX8CALL_HRES(x,res) DX8_Assert(); res = DX8Wrapper::_Get_D3D_Device8()->x; DX8_ErrorCode(res); number_of_DX8_calls++;
  117. #define DX8CALL(x) DX8_Assert(); DX8_ErrorCode(DX8Wrapper::_Get_D3D_Device8()->x); number_of_DX8_calls++;
  118. #define DX8CALL_D3D(x) DX8_Assert(); DX8_ErrorCode(DX8Wrapper::_Get_D3D8()->x); number_of_DX8_calls++;
  119. #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!"); }
  120. #else
  121. #define DX8CALL_HRES(x,res) res = DX8Wrapper::_Get_D3D_Device8()->x; number_of_DX8_calls++;
  122. #define DX8CALL(x) DX8Wrapper::_Get_D3D_Device8()->x; number_of_DX8_calls++;
  123. #define DX8CALL_D3D(x) DX8Wrapper::_Get_D3D8()->x; number_of_DX8_calls++;
  124. #define DX8_THREAD_ASSERT() ;
  125. #endif
  126. #define no_EXTENDED_STATS
  127. // EXTENDED_STATS collects additional timing statistics by turning off parts
  128. // of the 3D drawing system (terrain, objects, etc.)
  129. #ifdef EXTENDED_STATS
  130. class DX8_Stats
  131. {
  132. public:
  133. bool m_showingStats;
  134. bool m_disableTerrain;
  135. bool m_disableWater;
  136. bool m_disableObjects;
  137. bool m_disableOverhead;
  138. bool m_disableConsole;
  139. int m_debugLinesToShow;
  140. int m_sleepTime;
  141. public:
  142. DX8_Stats::DX8_Stats(void) {
  143. m_disableConsole = m_showingStats = m_disableTerrain = m_disableWater = m_disableOverhead = m_disableObjects = false;
  144. m_sleepTime = 0;
  145. m_debugLinesToShow = -1; // -1 means show all expected lines of output
  146. }
  147. };
  148. #endif
  149. // This virtual interface was added for the Generals RTS.
  150. // It is called before resetting the dx8 device to ensure
  151. // that all dx8 resources are released. Otherwise reset fails. jba.
  152. class DX8_CleanupHook
  153. {
  154. public:
  155. virtual void ReleaseResources(void)=0;
  156. virtual void ReAcquireResources(void)=0;
  157. };
  158. struct RenderStateStruct
  159. {
  160. ShaderClass shader;
  161. VertexMaterialClass* material;
  162. TextureBaseClass * Textures[MAX_TEXTURE_STAGES];
  163. D3DLIGHT8 Lights[4];
  164. bool LightEnable[4];
  165. //unsigned lightsHash;
  166. Matrix4x4 world;
  167. Matrix4x4 view;
  168. unsigned vertex_buffer_types[MAX_VERTEX_STREAMS];
  169. unsigned index_buffer_type;
  170. unsigned short vba_offset;
  171. unsigned short vba_count;
  172. unsigned short iba_offset;
  173. VertexBufferClass* vertex_buffers[MAX_VERTEX_STREAMS];
  174. IndexBufferClass* index_buffer;
  175. unsigned short index_base_offset;
  176. RenderStateStruct();
  177. ~RenderStateStruct();
  178. RenderStateStruct& operator= (const RenderStateStruct& src);
  179. };
  180. /**
  181. ** DX8Wrapper
  182. **
  183. ** DX8 interface wrapper class. This encapsulates the DX8 interface; adding redundant state
  184. ** detection, stat tracking, etc etc. In general, we will wrap all DX8 calls with at least
  185. ** an WWINLINE function so that we can add stat tracking, etc if needed. Direct access to the
  186. ** D3D device will require "friend" status and should be granted only in extreme circumstances :-)
  187. */
  188. class DX8Wrapper
  189. {
  190. enum ChangedStates {
  191. WORLD_CHANGED = 1<<0,
  192. VIEW_CHANGED = 1<<1,
  193. LIGHT0_CHANGED = 1<<2,
  194. LIGHT1_CHANGED = 1<<3,
  195. LIGHT2_CHANGED = 1<<4,
  196. LIGHT3_CHANGED = 1<<5,
  197. TEXTURE0_CHANGED= 1<<6,
  198. TEXTURE1_CHANGED= 1<<7,
  199. TEXTURE2_CHANGED= 1<<8,
  200. TEXTURE3_CHANGED= 1<<9,
  201. MATERIAL_CHANGED= 1<<14,
  202. SHADER_CHANGED = 1<<15,
  203. VERTEX_BUFFER_CHANGED = 1<<16,
  204. INDEX_BUFFER_CHANGED = 1 << 17,
  205. WORLD_IDENTITY= 1<<18,
  206. VIEW_IDENTITY= 1<<19,
  207. TEXTURES_CHANGED=
  208. TEXTURE0_CHANGED|TEXTURE1_CHANGED|TEXTURE2_CHANGED|TEXTURE3_CHANGED,
  209. LIGHTS_CHANGED=
  210. LIGHT0_CHANGED|LIGHT1_CHANGED|LIGHT2_CHANGED|LIGHT3_CHANGED,
  211. };
  212. static void Draw_Sorting_IB_VB(
  213. unsigned primitive_type,
  214. unsigned short start_index,
  215. unsigned short polygon_count,
  216. unsigned short min_vertex_index,
  217. unsigned short vertex_count);
  218. static void Draw(
  219. unsigned primitive_type,
  220. unsigned short start_index,
  221. unsigned short polygon_count,
  222. unsigned short min_vertex_index=0,
  223. unsigned short vertex_count=0);
  224. public:
  225. #ifdef EXTENDED_STATS
  226. static DX8_Stats stats;
  227. #endif
  228. static bool Init(void * hwnd, bool lite = false);
  229. static void Shutdown(void);
  230. static void SetCleanupHook(DX8_CleanupHook *pCleanupHook) {m_pCleanupHook = pCleanupHook;};
  231. /*
  232. ** Some WW3D sub-systems need to be initialized after the device is created and shutdown
  233. ** before the device is released.
  234. */
  235. static void Do_Onetime_Device_Dependent_Inits(void);
  236. static void Do_Onetime_Device_Dependent_Shutdowns(void);
  237. static bool Is_Device_Lost() { return IsDeviceLost; }
  238. static bool Is_Initted(void) { return IsInitted; }
  239. static bool Has_Stencil (void);
  240. static void Get_Format_Name(unsigned int format, StringClass *tex_format);
  241. /*
  242. ** Rendering
  243. */
  244. static void Begin_Scene(void);
  245. static void End_Scene(bool flip_frame = true);
  246. // Flip until the primary buffer is visible.
  247. static void Flip_To_Primary(void);
  248. static void Clear(bool clear_color, bool clear_z_stencil, const Vector3 &color, float dest_alpha=0.0f, float z=1.0f, unsigned int stencil=0);
  249. static void Set_Viewport(CONST D3DVIEWPORT8* pViewport);
  250. static void Set_Vertex_Buffer(const VertexBufferClass* vb, unsigned stream=0);
  251. static void Set_Vertex_Buffer(const DynamicVBAccessClass& vba);
  252. static void Set_Index_Buffer(const IndexBufferClass* ib,unsigned short index_base_offset);
  253. static void Set_Index_Buffer(const DynamicIBAccessClass& iba,unsigned short index_base_offset);
  254. static void Set_Index_Buffer_Index_Offset(unsigned offset);
  255. static void Get_Render_State(RenderStateStruct& state);
  256. static void Set_Render_State(const RenderStateStruct& state);
  257. static void Release_Render_State();
  258. static void Set_DX8_Material(const D3DMATERIAL8* mat);
  259. static void Set_Gamma(float gamma,float bright,float contrast,bool calibrate=true,bool uselimit=true);
  260. // Set_ and Get_Transform() functions take the matrix in Westwood convention format.
  261. static void Set_DX8_ZBias(int zbias);
  262. static void Set_Projection_Transform_With_Z_Bias(const Matrix4x4& matrix,float znear, float zfar); // pointer to 16 matrices
  263. static void Set_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix4x4& m);
  264. static void Set_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix3D& m);
  265. static void Get_Transform(D3DTRANSFORMSTATETYPE transform, Matrix4x4& m);
  266. static void Set_World_Identity();
  267. static void Set_View_Identity();
  268. static bool Is_World_Identity();
  269. static bool Is_View_Identity();
  270. // Note that *_DX8_Transform() functions take the matrix in DX8 format - transposed from Westwood convention.
  271. static void _Set_DX8_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix4x4& m);
  272. static void _Set_DX8_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix3D& m);
  273. static void _Get_DX8_Transform(D3DTRANSFORMSTATETYPE transform, Matrix4x4& m);
  274. static void Set_DX8_Light(int index,D3DLIGHT8* light);
  275. static void Set_DX8_Render_State(D3DRENDERSTATETYPE state, unsigned value);
  276. static void Set_DX8_Clip_Plane(DWORD Index, CONST float* pPlane);
  277. static void Set_DX8_Texture_Stage_State(unsigned stage, D3DTEXTURESTAGESTATETYPE state, unsigned value);
  278. static void Set_DX8_Texture(unsigned int stage, IDirect3DBaseTexture8* texture);
  279. static void Set_Light_Environment(LightEnvironmentClass* light_env);
  280. static LightEnvironmentClass* Get_Light_Environment() { return Light_Environment; }
  281. static void Set_Fog(bool enable, const Vector3 &color, float start, float end);
  282. static WWINLINE const D3DLIGHT8& Peek_Light(unsigned index);
  283. static WWINLINE bool Is_Light_Enabled(unsigned index);
  284. static bool Validate_Device(void);
  285. // Deferred
  286. static void Set_Shader(const ShaderClass& shader);
  287. static void Get_Shader(ShaderClass& shader);
  288. static void Set_Texture(unsigned stage,TextureBaseClass* texture);
  289. static void Set_Material(const VertexMaterialClass* material);
  290. static void Set_Light(unsigned index,const D3DLIGHT8* light);
  291. static void Set_Light(unsigned index,const LightClass &light);
  292. static void Apply_Render_State_Changes(); // Apply deferred render state changes (will be called automatically by Draw...)
  293. static void Draw_Triangles(
  294. unsigned buffer_type,
  295. unsigned short start_index,
  296. unsigned short polygon_count,
  297. unsigned short min_vertex_index,
  298. unsigned short vertex_count);
  299. static void Draw_Triangles(
  300. unsigned short start_index,
  301. unsigned short polygon_count,
  302. unsigned short min_vertex_index,
  303. unsigned short vertex_count);
  304. static void Draw_Strip(
  305. unsigned short start_index,
  306. unsigned short index_count,
  307. unsigned short min_vertex_index,
  308. unsigned short vertex_count);
  309. /*
  310. ** Resources
  311. */
  312. static IDirect3DVolumeTexture8* _Create_DX8_Volume_Texture
  313. (
  314. unsigned int width,
  315. unsigned int height,
  316. unsigned int depth,
  317. WW3DFormat format,
  318. MipCountType mip_level_count,
  319. D3DPOOL pool=D3DPOOL_MANAGED
  320. );
  321. static IDirect3DCubeTexture8* _Create_DX8_Cube_Texture
  322. (
  323. unsigned int width,
  324. unsigned int height,
  325. WW3DFormat format,
  326. MipCountType mip_level_count,
  327. D3DPOOL pool=D3DPOOL_MANAGED,
  328. bool rendertarget=false
  329. );
  330. static IDirect3DTexture8* _Create_DX8_ZTexture
  331. (
  332. unsigned int width,
  333. unsigned int height,
  334. WW3DZFormat zformat,
  335. MipCountType mip_level_count,
  336. D3DPOOL pool=D3DPOOL_MANAGED
  337. );
  338. static IDirect3DTexture8 * _Create_DX8_Texture
  339. (
  340. unsigned int width,
  341. unsigned int height,
  342. WW3DFormat format,
  343. MipCountType mip_level_count,
  344. D3DPOOL pool=D3DPOOL_MANAGED,
  345. bool rendertarget=false
  346. );
  347. static IDirect3DTexture8 * _Create_DX8_Texture(const char *filename, MipCountType mip_level_count);
  348. static IDirect3DTexture8 * _Create_DX8_Texture(IDirect3DSurface8 *surface, MipCountType mip_level_count);
  349. static IDirect3DSurface8 * _Create_DX8_Surface(unsigned int width, unsigned int height, WW3DFormat format);
  350. static IDirect3DSurface8 * _Create_DX8_Surface(const char *filename);
  351. static IDirect3DSurface8 * _Get_DX8_Front_Buffer();
  352. static SurfaceClass * _Get_DX8_Back_Buffer(unsigned int num=0);
  353. static void _Copy_DX8_Rects(
  354. IDirect3DSurface8* pSourceSurface,
  355. CONST RECT* pSourceRectsArray,
  356. UINT cRects,
  357. IDirect3DSurface8* pDestinationSurface,
  358. CONST POINT* pDestPointsArray
  359. );
  360. static void _Update_Texture(TextureClass *system, TextureClass *video);
  361. static void Flush_DX8_Resource_Manager(unsigned int bytes=0);
  362. static unsigned int Get_Free_Texture_RAM();
  363. static unsigned _Get_Main_Thread_ID() { return _MainThreadID; }
  364. static const D3DADAPTER_IDENTIFIER8& Get_Current_Adapter_Identifier() { return CurrentAdapterIdentifier; }
  365. /*
  366. ** Statistics
  367. */
  368. static void Begin_Statistics();
  369. static void End_Statistics();
  370. static unsigned Get_Last_Frame_Matrix_Changes();
  371. static unsigned Get_Last_Frame_Material_Changes();
  372. static unsigned Get_Last_Frame_Vertex_Buffer_Changes();
  373. static unsigned Get_Last_Frame_Index_Buffer_Changes();
  374. static unsigned Get_Last_Frame_Light_Changes();
  375. static unsigned Get_Last_Frame_Texture_Changes();
  376. static unsigned Get_Last_Frame_Render_State_Changes();
  377. static unsigned Get_Last_Frame_Texture_Stage_State_Changes();
  378. static unsigned Get_Last_Frame_DX8_Calls();
  379. static unsigned Get_Last_Frame_Draw_Calls();
  380. static unsigned long Get_FrameCount(void);
  381. // Needed by shader class
  382. static bool Get_Fog_Enable() { return FogEnable; }
  383. static D3DCOLOR Get_Fog_Color() { return FogColor; }
  384. // Utilities
  385. static Vector4 Convert_Color(unsigned color);
  386. static unsigned int Convert_Color(const Vector4& color);
  387. static unsigned int Convert_Color(const Vector3& color, const float alpha);
  388. static void Clamp_Color(Vector4& color);
  389. static unsigned int Convert_Color_Clamp(const Vector4& color);
  390. static void Set_Alpha (const float alpha, unsigned int &color);
  391. static void _Enable_Triangle_Draw(bool enable) { _EnableTriangleDraw=enable; }
  392. static bool _Is_Triangle_Draw_Enabled() { return _EnableTriangleDraw; }
  393. /*
  394. ** Additional swap chain interface
  395. **
  396. ** Use this interface to render to multiple windows (in windowed mode).
  397. ** To render to an additional window, the sequence of calls should look
  398. ** something like this:
  399. **
  400. ** DX8Wrapper::Set_Render_Target (swap_chain_ptr);
  401. **
  402. ** WW3D::Begin_Render (true, true, Vector3 (0, 0, 0));
  403. ** WW3D::Render (scene, camera, FALSE, FALSE);
  404. ** WW3D::End_Render ();
  405. **
  406. ** swap_chain_ptr->Present (NULL, NULL, NULL, NULL);
  407. **
  408. ** DX8Wrapper::Set_Render_Target ((IDirect3DSurface8 *)NULL);
  409. **
  410. */
  411. static IDirect3DSwapChain8 * Create_Additional_Swap_Chain (HWND render_window);
  412. /*
  413. ** Render target interface. If render target format is WW3D_FORMAT_UNKNOWN, current display format is used.
  414. */
  415. static TextureClass * Create_Render_Target (int width, int height, WW3DFormat format = WW3D_FORMAT_UNKNOWN);
  416. static void Set_Render_Target (IDirect3DSurface8 *render_target, bool use_default_depth_buffer = false);
  417. static void Set_Render_Target (IDirect3DSurface8* render_target, IDirect3DSurface8* dpeth_buffer);
  418. static void Set_Render_Target (IDirect3DSwapChain8 *swap_chain);
  419. static bool Is_Render_To_Texture(void) { return IsRenderToTexture; }
  420. // for depth map support KJM V
  421. static void Create_Render_Target
  422. (
  423. int width,
  424. int height,
  425. WW3DFormat format,
  426. WW3DZFormat zformat,
  427. TextureClass** target,
  428. ZTextureClass** depth_buffer
  429. );
  430. static void Set_Render_Target_With_Z (TextureClass * texture, ZTextureClass* ztexture=NULL);
  431. static void Set_Shadow_Map(int idx, ZTextureClass* ztex) { Shadow_Map[idx]=ztex; }
  432. static ZTextureClass* Get_Shadow_Map(int idx) { return Shadow_Map[idx]; }
  433. // for depth map support KJM ^
  434. // shader system udpates KJM v
  435. static void Apply_Default_State();
  436. static void Set_Vertex_Shader(DWORD vertex_shader);
  437. static void Set_Pixel_Shader(DWORD pixel_shader);
  438. static void Set_Vertex_Shader_Constant(int reg, const void* data, int count);
  439. static void Set_Pixel_Shader_Constant(int reg, const void* data, int count);
  440. static DWORD Get_Vertex_Processing_Behavior() { return Vertex_Processing_Behavior; }
  441. // Needed by scene lighting class
  442. static void Set_Ambient(const Vector3& color);
  443. static const Vector3& Get_Ambient() { return Ambient_Color; }
  444. // shader system updates KJM ^
  445. static IDirect3DDevice8* _Get_D3D_Device8() { return D3DDevice; }
  446. static IDirect3D8* _Get_D3D8() { return D3DInterface; }
  447. /// Returns the display format - added by TR for video playback - not part of W3D
  448. static WW3DFormat getBackBufferFormat( void );
  449. static bool Reset_Device(bool reload_assets=true);
  450. static const DX8Caps* Get_Current_Caps() { WWASSERT(CurrentCaps); return CurrentCaps; }
  451. static bool Registry_Save_Render_Device( const char * sub_key );
  452. static bool Registry_Load_Render_Device( const char * sub_key, bool resize_window );
  453. static const char* Get_DX8_Render_State_Name(D3DRENDERSTATETYPE state);
  454. static const char* Get_DX8_Texture_Stage_State_Name(D3DTEXTURESTAGESTATETYPE state);
  455. static unsigned Get_DX8_Render_State(D3DRENDERSTATETYPE state) { return RenderStates[state]; }
  456. // Names of the specific values of render states and texture stage states
  457. static void Get_DX8_Texture_Stage_State_Value_Name(StringClass& name, D3DTEXTURESTAGESTATETYPE state, unsigned value);
  458. static void Get_DX8_Render_State_Value_Name(StringClass& name, D3DRENDERSTATETYPE state, unsigned value);
  459. static const char* Get_DX8_Texture_Address_Name(unsigned value);
  460. static const char* Get_DX8_Texture_Filter_Name(unsigned value);
  461. static const char* Get_DX8_Texture_Arg_Name(unsigned value);
  462. static const char* Get_DX8_Texture_Op_Name(unsigned value);
  463. static const char* Get_DX8_Texture_Transform_Flag_Name(unsigned value);
  464. static const char* Get_DX8_ZBuffer_Type_Name(unsigned value);
  465. static const char* Get_DX8_Fill_Mode_Name(unsigned value);
  466. static const char* Get_DX8_Shade_Mode_Name(unsigned value);
  467. static const char* Get_DX8_Blend_Name(unsigned value);
  468. static const char* Get_DX8_Cull_Mode_Name(unsigned value);
  469. static const char* Get_DX8_Cmp_Func_Name(unsigned value);
  470. static const char* Get_DX8_Fog_Mode_Name(unsigned value);
  471. static const char* Get_DX8_Stencil_Op_Name(unsigned value);
  472. static const char* Get_DX8_Material_Source_Name(unsigned value);
  473. static const char* Get_DX8_Vertex_Blend_Flag_Name(unsigned value);
  474. static const char* Get_DX8_Patch_Edge_Style_Name(unsigned value);
  475. static const char* Get_DX8_Debug_Monitor_Token_Name(unsigned value);
  476. static const char* Get_DX8_Blend_Op_Name(unsigned value);
  477. static void Invalidate_Cached_Render_States(void);
  478. static void Set_Draw_Polygon_Low_Bound_Limit(unsigned n) { DrawPolygonLowBoundLimit=n; }
  479. protected:
  480. static bool Create_Device(void);
  481. static void Release_Device(void);
  482. static void Reset_Statistics();
  483. static void Enumerate_Devices();
  484. static void Set_Default_Global_Render_States(void);
  485. /*
  486. ** Device Selection Code.
  487. ** For backward compatibility, the public interface for these functions is in the ww3d.
  488. ** header file. These functions are protected so that we aren't exposing two interfaces.
  489. */
  490. static bool Set_Any_Render_Device(void);
  491. 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);
  492. static bool Set_Render_Device(int dev=-1,int resx=-1,int resy=-1,int bits=-1,int windowed=-1,bool resize_window = false, bool reset_device = false, bool restore_assets=true);
  493. static bool Set_Next_Render_Device(void);
  494. static bool Toggle_Windowed(void);
  495. static int Get_Render_Device_Count(void);
  496. static int Get_Render_Device(void);
  497. static const RenderDeviceDescClass & Get_Render_Device_Desc(int deviceidx);
  498. static const char * Get_Render_Device_Name(int device_index);
  499. static bool Set_Device_Resolution(int width=-1,int height=-1,int bits=-1,int windowed=-1, bool resize_window=false);
  500. static void Get_Device_Resolution(int & set_w,int & set_h,int & set_bits,bool & set_windowed);
  501. static void Get_Render_Target_Resolution(int & set_w,int & set_h,int & set_bits,bool & set_windowed);
  502. static int Get_Device_Resolution_Width(void) { return ResolutionWidth; }
  503. static int Get_Device_Resolution_Height(void) { return ResolutionHeight; }
  504. static bool Registry_Save_Render_Device( const char *sub_key, int device, int width, int height, int depth, bool windowed, int texture_depth);
  505. 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);
  506. static bool Is_Windowed(void) { return IsWindowed; }
  507. static void Set_Texture_Bitdepth(int depth) { WWASSERT(depth==16 || depth==32); TextureBitDepth = depth; }
  508. static int Get_Texture_Bitdepth(void) { return TextureBitDepth; }
  509. static void Set_Swap_Interval(int swap);
  510. static int Get_Swap_Interval(void);
  511. static void Set_Polygon_Mode(int mode);
  512. /*
  513. ** Internal functions
  514. */
  515. static bool Find_Color_And_Z_Mode(int resx,int resy,int bitdepth,D3DFORMAT * set_colorbuffer,D3DFORMAT * set_backbuffer, D3DFORMAT * set_zmode);
  516. static bool Find_Color_Mode(D3DFORMAT colorbuffer, int resx, int resy, UINT *mode);
  517. static bool Find_Z_Mode(D3DFORMAT colorbuffer,D3DFORMAT backbuffer, D3DFORMAT *zmode);
  518. static bool Test_Z_Mode(D3DFORMAT colorbuffer,D3DFORMAT backbuffer, D3DFORMAT zmode);
  519. static void Compute_Caps(WW3DFormat display_format);
  520. /*
  521. ** Protected Member Variables
  522. */
  523. static DX8_CleanupHook *m_pCleanupHook;
  524. static RenderStateStruct render_state;
  525. static unsigned render_state_changed;
  526. static Matrix4x4 DX8Transforms[D3DTS_WORLD+1];
  527. static bool IsInitted;
  528. static bool IsDeviceLost;
  529. static void * Hwnd;
  530. static unsigned _MainThreadID;
  531. static bool _EnableTriangleDraw;
  532. static int CurRenderDevice;
  533. static int ResolutionWidth;
  534. static int ResolutionHeight;
  535. static int BitDepth;
  536. static int TextureBitDepth;
  537. static bool IsWindowed;
  538. static D3DFORMAT DisplayFormat;
  539. static D3DMATRIX old_world;
  540. static D3DMATRIX old_view;
  541. static D3DMATRIX old_prj;
  542. // shader system updates KJM v
  543. static DWORD Vertex_Shader;
  544. static DWORD Pixel_Shader;
  545. static Vector4 Vertex_Shader_Constants[MAX_VERTEX_SHADER_CONSTANTS];
  546. static Vector4 Pixel_Shader_Constants[MAX_PIXEL_SHADER_CONSTANTS];
  547. static LightEnvironmentClass* Light_Environment;
  548. static RenderInfoClass* Render_Info;
  549. static DWORD Vertex_Processing_Behavior;
  550. static ZTextureClass* Shadow_Map[MAX_SHADOW_MAPS];
  551. static Vector3 Ambient_Color;
  552. // shader system updates KJM ^
  553. static bool world_identity;
  554. static unsigned RenderStates[256];
  555. static unsigned TextureStageStates[MAX_TEXTURE_STAGES][32];
  556. static IDirect3DBaseTexture8 * Textures[MAX_TEXTURE_STAGES];
  557. // These fog settings are constant for all objects in a given scene,
  558. // unlike the matching renderstates which vary based on shader settings.
  559. static bool FogEnable;
  560. static D3DCOLOR FogColor;
  561. static unsigned matrix_changes;
  562. static unsigned material_changes;
  563. static unsigned vertex_buffer_changes;
  564. static unsigned index_buffer_changes;
  565. static unsigned light_changes;
  566. static unsigned texture_changes;
  567. static unsigned render_state_changes;
  568. static unsigned texture_stage_state_changes;
  569. static unsigned draw_calls;
  570. static bool CurrentDX8LightEnables[4];
  571. static unsigned long FrameCount;
  572. static DX8Caps* CurrentCaps;
  573. static D3DADAPTER_IDENTIFIER8 CurrentAdapterIdentifier;
  574. static IDirect3D8 * D3DInterface; //d3d8;
  575. static IDirect3DDevice8 * D3DDevice; //d3ddevice8;
  576. static IDirect3DSurface8 * CurrentRenderTarget;
  577. static IDirect3DSurface8 * CurrentDepthBuffer;
  578. static IDirect3DSurface8 * DefaultRenderTarget;
  579. static IDirect3DSurface8 * DefaultDepthBuffer;
  580. static unsigned DrawPolygonLowBoundLimit;
  581. static bool IsRenderToTexture;
  582. static int ZBias;
  583. static float ZNear;
  584. static float ZFar;
  585. static Matrix4x4 ProjectionMatrix;
  586. friend void DX8_Assert();
  587. friend class WW3D;
  588. friend class DX8IndexBufferClass;
  589. friend class DX8VertexBufferClass;
  590. };
  591. // shader system updates KJM v
  592. WWINLINE void DX8Wrapper::Set_Vertex_Shader(DWORD vertex_shader)
  593. {
  594. #if 0 //(gth) some code is bypassing this acessor function so we can't count on this variable...
  595. // may be incorrect if shaders are created and destroyed dynamically
  596. if (Vertex_Shader==vertex_shader) return;
  597. #endif
  598. Vertex_Shader=vertex_shader;
  599. DX8CALL(SetVertexShader(Vertex_Shader));
  600. }
  601. WWINLINE void DX8Wrapper::Set_Pixel_Shader(DWORD pixel_shader)
  602. {
  603. // may be incorrect if shaders are created and destroyed dynamically
  604. if (Pixel_Shader==pixel_shader) return;
  605. Pixel_Shader=pixel_shader;
  606. DX8CALL(SetPixelShader(Pixel_Shader));
  607. }
  608. WWINLINE void DX8Wrapper::Set_Vertex_Shader_Constant(int reg, const void* data, int count)
  609. {
  610. int memsize=sizeof(Vector4)*count;
  611. // may be incorrect if shaders are created and destroyed dynamically
  612. if (memcmp(data, &Vertex_Shader_Constants[reg],memsize)==0) return;
  613. memcpy(&Vertex_Shader_Constants[reg],data,memsize);
  614. DX8CALL(SetVertexShaderConstant(reg,data,count));
  615. }
  616. WWINLINE void DX8Wrapper::Set_Pixel_Shader_Constant(int reg, const void* data, int count)
  617. {
  618. int memsize=sizeof(Vector4)*count;
  619. // may be incorrect if shaders are created and destroyed dynamically
  620. if (memcmp(data, &Pixel_Shader_Constants[reg],memsize)==0) return;
  621. memcpy(&Pixel_Shader_Constants[reg],data,memsize);
  622. DX8CALL(SetPixelShaderConstant(reg,data,count));
  623. }
  624. // shader system updates KJM ^
  625. WWINLINE void DX8Wrapper::_Set_DX8_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix4x4& m)
  626. {
  627. WWASSERT(transform<=D3DTS_WORLD);
  628. #if 0 // (gth) this optimization is breaking generals because they set the transform behind our backs.
  629. if (m!=DX8Transforms[transform])
  630. #endif
  631. {
  632. DX8Transforms[transform]=m;
  633. SNAPSHOT_SAY(("DX8 - SetTransform %d [%f,%f,%f,%f][%f,%f,%f,%f][%f,%f,%f,%f][%f,%f,%f,%f]\n",transform,m[0][0],m[0][1],m[0][2],m[0][3],m[1][0],m[1][1],m[1][2],m[1][3],m[2][0],m[2][1],m[2][2],m[2][3],m[3][0],m[3][1],m[3][2],m[3][3]));
  634. DX8_RECORD_MATRIX_CHANGE();
  635. DX8CALL(SetTransform(transform,(D3DMATRIX*)&m));
  636. }
  637. }
  638. WWINLINE void DX8Wrapper::_Set_DX8_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix3D& m)
  639. {
  640. WWASSERT(transform<=D3DTS_WORLD);
  641. Matrix4x4 mtx(m);
  642. #if 0 // (gth) this optimization is breaking generals because they set the transform behind our backs.
  643. if (mtx!=DX8Transforms[transform])
  644. #endif
  645. {
  646. DX8Transforms[transform]=mtx;
  647. SNAPSHOT_SAY(("DX8 - SetTransform %d [%f,%f,%f,%f][%f,%f,%f,%f][%f,%f,%f,%f]\n",transform,m[0][0],m[0][1],m[0][2],m[0][3],m[1][0],m[1][1],m[1][2],m[1][3],m[2][0],m[2][1],m[2][2],m[2][3]));
  648. DX8_RECORD_MATRIX_CHANGE();
  649. DX8CALL(SetTransform(transform,(D3DMATRIX*)&m));
  650. }
  651. }
  652. WWINLINE void DX8Wrapper::_Get_DX8_Transform(D3DTRANSFORMSTATETYPE transform, Matrix4x4& m)
  653. {
  654. DX8CALL(GetTransform(transform,(D3DMATRIX*)&m));
  655. }
  656. // ----------------------------------------------------------------------------
  657. //
  658. // Set the index offset for the current index buffer
  659. //
  660. // ----------------------------------------------------------------------------
  661. WWINLINE void DX8Wrapper::Set_Index_Buffer_Index_Offset(unsigned offset)
  662. {
  663. if (render_state.index_base_offset==offset) return;
  664. render_state.index_base_offset=offset;
  665. render_state_changed|=INDEX_BUFFER_CHANGED;
  666. }
  667. // ----------------------------------------------------------------------------
  668. // Set the fog settings. This function should be used, rather than setting the
  669. // appropriate renderstates directly, because the shader sets some of the
  670. // renderstates on a per-mesh / per-pass basis depending on global fog states
  671. // (stored in the wrapper) as well as the shader settings.
  672. // This function should be called rarely - once per scene would be appropriate.
  673. // ----------------------------------------------------------------------------
  674. WWINLINE void DX8Wrapper::Set_Fog(bool enable, const Vector3 &color, float start, float end)
  675. {
  676. // Set global states
  677. FogEnable = enable;
  678. FogColor = Convert_Color(color,0.0f);
  679. // Invalidate the current shader (since the renderstates set by the shader
  680. // depend on the global fog settings as well as the actual shader settings)
  681. ShaderClass::Invalidate();
  682. // Set renderstates which are not affected by the shader
  683. Set_DX8_Render_State(D3DRS_FOGSTART, *(DWORD *)(&start));
  684. Set_DX8_Render_State(D3DRS_FOGEND, *(DWORD *)(&end));
  685. }
  686. WWINLINE void DX8Wrapper::Set_Ambient(const Vector3& color)
  687. {
  688. Ambient_Color=color;
  689. Set_DX8_Render_State(D3DRS_AMBIENT, DX8Wrapper::Convert_Color(color,0.0f));
  690. }
  691. // ----------------------------------------------------------------------------
  692. //
  693. // Set vertex buffer to be used in the subsequent render calls. If there was
  694. // a vertex buffer being used earlier, release the reference to it. Passing
  695. // NULL just will release the vertex buffer.
  696. //
  697. // ----------------------------------------------------------------------------
  698. WWINLINE void DX8Wrapper::Set_DX8_Material(const D3DMATERIAL8* mat)
  699. {
  700. DX8_RECORD_MATERIAL_CHANGE();
  701. WWASSERT(mat);
  702. SNAPSHOT_SAY(("DX8 - SetMaterial\n"));
  703. DX8CALL(SetMaterial(mat));
  704. }
  705. WWINLINE void DX8Wrapper::Set_DX8_Light(int index, D3DLIGHT8* light)
  706. {
  707. if (light) {
  708. DX8_RECORD_LIGHT_CHANGE();
  709. DX8CALL(SetLight(index,light));
  710. DX8CALL(LightEnable(index,TRUE));
  711. CurrentDX8LightEnables[index]=true;
  712. SNAPSHOT_SAY(("DX8 - SetLight %d\n",index));
  713. }
  714. else if (CurrentDX8LightEnables[index]) {
  715. DX8_RECORD_LIGHT_CHANGE();
  716. CurrentDX8LightEnables[index]=false;
  717. DX8CALL(LightEnable(index,FALSE));
  718. SNAPSHOT_SAY(("DX8 - DisableLight %d\n",index));
  719. }
  720. }
  721. WWINLINE void DX8Wrapper::Set_DX8_Render_State(D3DRENDERSTATETYPE state, unsigned value)
  722. {
  723. // Can't monitor state changes because setShader call to GERD may change the states!
  724. if (RenderStates[state]==value) return;
  725. #ifdef MESH_RENDER_SNAPSHOT_ENABLED
  726. if (WW3D::Is_Snapshot_Activated()) {
  727. StringClass value_name(0,true);
  728. Get_DX8_Render_State_Value_Name(value_name,state,value);
  729. SNAPSHOT_SAY(("DX8 - SetRenderState(state: %s, value: %s)\n",
  730. Get_DX8_Render_State_Name(state),
  731. value_name));
  732. }
  733. #endif
  734. RenderStates[state]=value;
  735. DX8CALL(SetRenderState( state, value ));
  736. DX8_RECORD_RENDER_STATE_CHANGE();
  737. }
  738. WWINLINE void DX8Wrapper::Set_DX8_Clip_Plane(DWORD Index, CONST float* pPlane)
  739. {
  740. DX8CALL(SetClipPlane( Index, pPlane ));
  741. }
  742. WWINLINE void DX8Wrapper::Set_DX8_Texture_Stage_State(unsigned stage, D3DTEXTURESTAGESTATETYPE state, unsigned value)
  743. {
  744. if (stage >= MAX_TEXTURE_STAGES)
  745. { DX8CALL(SetTextureStageState( stage, state, value ));
  746. return;
  747. }
  748. // Can't monitor state changes because setShader call to GERD may change the states!
  749. if (TextureStageStates[stage][(unsigned int)state]==value) return;
  750. #ifdef MESH_RENDER_SNAPSHOT_ENABLED
  751. if (WW3D::Is_Snapshot_Activated()) {
  752. StringClass value_name(0,true);
  753. Get_DX8_Texture_Stage_State_Value_Name(value_name,state,value);
  754. SNAPSHOT_SAY(("DX8 - SetTextureStageState(stage: %d, state: %s, value: %s)\n",
  755. stage,
  756. Get_DX8_Texture_Stage_State_Name(state),
  757. value_name));
  758. }
  759. #endif
  760. TextureStageStates[stage][(unsigned int)state]=value;
  761. DX8CALL(SetTextureStageState( stage, state, value ));
  762. DX8_RECORD_TEXTURE_STAGE_STATE_CHANGE();
  763. }
  764. WWINLINE void DX8Wrapper::Set_DX8_Texture(unsigned int stage, IDirect3DBaseTexture8* texture)
  765. {
  766. if (stage >= MAX_TEXTURE_STAGES)
  767. { DX8CALL(SetTexture(stage, texture));
  768. return;
  769. }
  770. if (Textures[stage]==texture) return;
  771. SNAPSHOT_SAY(("DX8 - SetTexture(%x) \n",texture));
  772. if (Textures[stage]) Textures[stage]->Release();
  773. Textures[stage] = texture;
  774. if (Textures[stage]) Textures[stage]->AddRef();
  775. DX8CALL(SetTexture(stage, texture));
  776. DX8_RECORD_TEXTURE_CHANGE();
  777. }
  778. WWINLINE void DX8Wrapper::_Copy_DX8_Rects(
  779. IDirect3DSurface8* pSourceSurface,
  780. CONST RECT* pSourceRectsArray,
  781. UINT cRects,
  782. IDirect3DSurface8* pDestinationSurface,
  783. CONST POINT* pDestPointsArray
  784. )
  785. {
  786. DX8CALL(CopyRects(
  787. pSourceSurface,
  788. pSourceRectsArray,
  789. cRects,
  790. pDestinationSurface,
  791. pDestPointsArray));
  792. }
  793. WWINLINE Vector4 DX8Wrapper::Convert_Color(unsigned color)
  794. {
  795. Vector4 col;
  796. col[3]=((color&0xff000000)>>24)/255.0f;
  797. col[0]=((color&0xff0000)>>16)/255.0f;
  798. col[1]=((color&0xff00)>>8)/255.0f;
  799. col[2]=((color&0xff)>>0)/255.0f;
  800. // col=Vector4(1.0f,1.0f,1.0f,1.0f);
  801. return col;
  802. }
  803. #if 0
  804. WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector3& color, const float alpha)
  805. {
  806. WWASSERT(color.X<=1.0f);
  807. WWASSERT(color.Y<=1.0f);
  808. WWASSERT(color.Z<=1.0f);
  809. WWASSERT(alpha<=1.0f);
  810. WWASSERT(color.X>=0.0f);
  811. WWASSERT(color.Y>=0.0f);
  812. WWASSERT(color.Z>=0.0f);
  813. WWASSERT(alpha>=0.0f);
  814. return D3DCOLOR_COLORVALUE(color.X,color.Y,color.Z,alpha);
  815. }
  816. WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector4& color)
  817. {
  818. WWASSERT(color.X<=1.0f);
  819. WWASSERT(color.Y<=1.0f);
  820. WWASSERT(color.Z<=1.0f);
  821. WWASSERT(color.W<=1.0f);
  822. WWASSERT(color.X>=0.0f);
  823. WWASSERT(color.Y>=0.0f);
  824. WWASSERT(color.Z>=0.0f);
  825. WWASSERT(color.W>=0.0f);
  826. return D3DCOLOR_COLORVALUE(color.X,color.Y,color.Z,color.W);
  827. }
  828. #else
  829. // ----------------------------------------------------------------------------
  830. //
  831. // Convert RGBA color from float vector to 32 bit integer
  832. // Note: Color vector needs to be clamped to [0...1] range!
  833. //
  834. // ----------------------------------------------------------------------------
  835. WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector3& color,float alpha)
  836. {
  837. const float scale = 255.0;
  838. unsigned int col;
  839. // Multiply r, g, b and a components (0.0,...,1.0) by 255 and convert to integer. Or the integer values togerher
  840. // such that 32 bit ingeger has AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB.
  841. __asm
  842. {
  843. sub esp,20 // space for a, r, g and b float plus fpu rounding mode
  844. // Store the fpu rounding mode
  845. fwait
  846. fstcw [esp+16] // store control word to stack
  847. mov eax,[esp+16] // load it to eax
  848. mov edi,eax // take copy
  849. and eax,~(1024|2048) // mask out certain bits
  850. or eax,(1024|2048) // or with precision control value "truncate"
  851. sub edi,eax // did it change?
  852. jz skip // .. if not, skip
  853. mov [esp],eax // .. change control word
  854. fldcw [esp]
  855. skip:
  856. // Convert the color
  857. mov esi,dword ptr color
  858. fld dword ptr[scale]
  859. fld dword ptr[esi] // r
  860. fld dword ptr[esi+4] // g
  861. fld dword ptr[esi+8] // b
  862. fld dword ptr[alpha] // a
  863. fld st(4)
  864. fmul st(4),st
  865. fmul st(3),st
  866. fmul st(2),st
  867. fmulp st(1),st
  868. fistp dword ptr[esp+0] // a
  869. fistp dword ptr[esp+4] // b
  870. fistp dword ptr[esp+8] // g
  871. fistp dword ptr[esp+12] // r
  872. mov ecx,[esp] // a
  873. mov eax,[esp+4] // b
  874. mov edx,[esp+8] // g
  875. mov ebx,[esp+12] // r
  876. shl ecx,24 // a << 24
  877. shl ebx,16 // r << 16
  878. shl edx,8 // g << 8
  879. or eax,ecx // (a << 24) | b
  880. or eax,ebx // (a << 24) | (r << 16) | b
  881. or eax,edx // (a << 24) | (r << 16) | (g << 8) | b
  882. fstp st(0)
  883. // Restore fpu rounding mode
  884. cmp edi,0 // did we change the value?
  885. je not_changed // nope... skip now...
  886. fwait
  887. fldcw [esp+16];
  888. not_changed:
  889. add esp,20
  890. mov col,eax
  891. }
  892. return col;
  893. }
  894. // ----------------------------------------------------------------------------
  895. //
  896. // Clamp color vertor to [0...1] range
  897. //
  898. // ----------------------------------------------------------------------------
  899. WWINLINE void DX8Wrapper::Clamp_Color(Vector4& color)
  900. {
  901. if (!CPUDetectClass::Has_CMOV_Instruction()) {
  902. for (int i=0;i<4;++i) {
  903. float f=(color[i]<0.0f) ? 0.0f : color[i];
  904. color[i]=(f>1.0f) ? 1.0f : f;
  905. }
  906. return;
  907. }
  908. __asm
  909. {
  910. mov esi,dword ptr color
  911. mov edx,0x3f800000
  912. mov edi,dword ptr[esi]
  913. mov ebx,edi
  914. sar edi,31
  915. not edi // mask is now zero if negative value
  916. and edi,ebx
  917. cmp edi,edx // if no less than 1.0 set to 1.0
  918. cmovnb edi,edx
  919. mov dword ptr[esi],edi
  920. mov edi,dword ptr[esi+4]
  921. mov ebx,edi
  922. sar edi,31
  923. not edi // mask is now zero if negative value
  924. and edi,ebx
  925. cmp edi,edx // if no less than 1.0 set to 1.0
  926. cmovnb edi,edx
  927. mov dword ptr[esi+4],edi
  928. mov edi,dword ptr[esi+8]
  929. mov ebx,edi
  930. sar edi,31
  931. not edi // mask is now zero if negative value
  932. and edi,ebx
  933. cmp edi,edx // if no less than 1.0 set to 1.0
  934. cmovnb edi,edx
  935. mov dword ptr[esi+8],edi
  936. mov edi,dword ptr[esi+12]
  937. mov ebx,edi
  938. sar edi,31
  939. not edi // mask is now zero if negative value
  940. and edi,ebx
  941. cmp edi,edx // if no less than 1.0 set to 1.0
  942. cmovnb edi,edx
  943. mov dword ptr[esi+12],edi
  944. }
  945. }
  946. // ----------------------------------------------------------------------------
  947. //
  948. // Convert RGBA color from float vector to 32 bit integer
  949. //
  950. // ----------------------------------------------------------------------------
  951. WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector4& color)
  952. {
  953. return Convert_Color(reinterpret_cast<const Vector3&>(color),color[3]);
  954. }
  955. WWINLINE unsigned int DX8Wrapper::Convert_Color_Clamp(const Vector4& color)
  956. {
  957. Vector4 clamped_color=color;
  958. DX8Wrapper::Clamp_Color(clamped_color);
  959. return Convert_Color(reinterpret_cast<const Vector3&>(clamped_color),clamped_color[3]);
  960. }
  961. #endif
  962. WWINLINE void DX8Wrapper::Set_Alpha (const float alpha, unsigned int &color)
  963. {
  964. unsigned char *component = (unsigned char*) &color;
  965. component [3] = 255.0f * alpha;
  966. }
  967. WWINLINE void DX8Wrapper::Get_Render_State(RenderStateStruct& state)
  968. {
  969. state=render_state;
  970. }
  971. WWINLINE void DX8Wrapper::Get_Shader(ShaderClass& shader)
  972. {
  973. shader=render_state.shader;
  974. }
  975. WWINLINE void DX8Wrapper::Set_Texture(unsigned stage,TextureBaseClass* texture)
  976. {
  977. WWASSERT(stage<(unsigned int)CurrentCaps->Get_Max_Textures_Per_Pass());
  978. if (texture==render_state.Textures[stage]) return;
  979. REF_PTR_SET(render_state.Textures[stage],texture);
  980. render_state_changed|=(TEXTURE0_CHANGED<<stage);
  981. }
  982. WWINLINE void DX8Wrapper::Set_Material(const VertexMaterialClass* material)
  983. {
  984. /* if (material && render_state.material &&
  985. // !stricmp(material->Get_Name(),render_state.material->Get_Name())) {
  986. material->Get_CRC()!=render_state.material->Get_CRC()) {
  987. return;
  988. }
  989. */
  990. // if (material==render_state.material) {
  991. // return;
  992. // }
  993. REF_PTR_SET(render_state.material,const_cast<VertexMaterialClass*>(material));
  994. render_state_changed|=MATERIAL_CHANGED;
  995. SNAPSHOT_SAY(("DX8Wrapper::Set_Material(%s)\n",material ? material->Get_Name() : "NULL"));
  996. }
  997. WWINLINE void DX8Wrapper::Set_Shader(const ShaderClass& shader)
  998. {
  999. if (!ShaderClass::ShaderDirty && ((unsigned&)shader==(unsigned&)render_state.shader)) {
  1000. return;
  1001. }
  1002. render_state.shader=shader;
  1003. render_state_changed|=SHADER_CHANGED;
  1004. #ifdef MESH_RENDER_SNAPSHOT_ENABLED
  1005. StringClass str;
  1006. #endif
  1007. SNAPSHOT_SAY(("DX8Wrapper::Set_Shader(%s)\n",shader.Get_Description(str)));
  1008. }
  1009. WWINLINE void DX8Wrapper::Set_Projection_Transform_With_Z_Bias(const Matrix4x4& matrix, float znear, float zfar)
  1010. {
  1011. ZFar=zfar;
  1012. ZNear=znear;
  1013. ProjectionMatrix=matrix.Transpose();
  1014. if (!Get_Current_Caps()->Support_ZBias() && ZNear!=ZFar) {
  1015. Matrix4x4 tmp=ProjectionMatrix;
  1016. float tmp_zbias=ZBias;
  1017. tmp_zbias*=(1.0f/16.0f);
  1018. tmp_zbias*=1.0f / (ZFar - ZNear);
  1019. tmp[2][2]-=tmp_zbias*tmp[3][2];
  1020. DX8CALL(SetTransform(D3DTS_PROJECTION,(D3DMATRIX*)&tmp));
  1021. }
  1022. else {
  1023. DX8CALL(SetTransform(D3DTS_PROJECTION,(D3DMATRIX*)&ProjectionMatrix));
  1024. }
  1025. }
  1026. WWINLINE void DX8Wrapper::Set_DX8_ZBias(int zbias)
  1027. {
  1028. if (zbias==ZBias) return;
  1029. if (zbias>15) zbias=15;
  1030. if (zbias<0) zbias=0;
  1031. ZBias=zbias;
  1032. if (!Get_Current_Caps()->Support_ZBias() && ZNear!=ZFar) {
  1033. Matrix4x4 tmp=ProjectionMatrix;
  1034. float tmp_zbias=ZBias;
  1035. tmp_zbias*=(1.0f/16.0f);
  1036. tmp_zbias*=1.0f / (ZFar - ZNear);
  1037. tmp[2][2]-=tmp_zbias*tmp[3][2];
  1038. DX8CALL(SetTransform(D3DTS_PROJECTION,(D3DMATRIX*)&tmp));
  1039. }
  1040. else {
  1041. Set_DX8_Render_State (D3DRS_ZBIAS, ZBias);
  1042. }
  1043. }
  1044. WWINLINE void DX8Wrapper::Set_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix4x4& m)
  1045. {
  1046. switch ((int)transform) {
  1047. case D3DTS_WORLD:
  1048. render_state.world=m.Transpose();
  1049. render_state_changed|=(unsigned)WORLD_CHANGED;
  1050. render_state_changed&=~(unsigned)WORLD_IDENTITY;
  1051. break;
  1052. case D3DTS_VIEW:
  1053. render_state.view=m.Transpose();
  1054. render_state_changed|=(unsigned)VIEW_CHANGED;
  1055. render_state_changed&=~(unsigned)VIEW_IDENTITY;
  1056. break;
  1057. case D3DTS_PROJECTION:
  1058. {
  1059. Matrix4x4 ProjectionMatrix=m.Transpose();
  1060. ZFar=0.0f;
  1061. ZNear=0.0f;
  1062. DX8CALL(SetTransform(D3DTS_PROJECTION,(D3DMATRIX*)&ProjectionMatrix));
  1063. }
  1064. break;
  1065. default:
  1066. DX8_RECORD_MATRIX_CHANGE();
  1067. Matrix4x4 m2=m.Transpose();
  1068. DX8CALL(SetTransform(transform,(D3DMATRIX*)&m2));
  1069. break;
  1070. }
  1071. }
  1072. WWINLINE void DX8Wrapper::Set_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix3D& m)
  1073. {
  1074. Matrix4x4 m2(m);
  1075. switch ((int)transform) {
  1076. case D3DTS_WORLD:
  1077. render_state.world=m2.Transpose();
  1078. render_state_changed|=(unsigned)WORLD_CHANGED;
  1079. render_state_changed&=~(unsigned)WORLD_IDENTITY;
  1080. break;
  1081. case D3DTS_VIEW:
  1082. render_state.view=m2.Transpose();
  1083. render_state_changed|=(unsigned)VIEW_CHANGED;
  1084. render_state_changed&=~(unsigned)VIEW_IDENTITY;
  1085. break;
  1086. default:
  1087. DX8_RECORD_MATRIX_CHANGE();
  1088. m2=m2.Transpose();
  1089. DX8CALL(SetTransform(transform,(D3DMATRIX*)&m2));
  1090. break;
  1091. }
  1092. }
  1093. WWINLINE void DX8Wrapper::Set_World_Identity()
  1094. {
  1095. if (render_state_changed&(unsigned)WORLD_IDENTITY) return;
  1096. render_state.world.Make_Identity();
  1097. render_state_changed|=(unsigned)WORLD_CHANGED|(unsigned)WORLD_IDENTITY;
  1098. }
  1099. WWINLINE void DX8Wrapper::Set_View_Identity()
  1100. {
  1101. if (render_state_changed&(unsigned)VIEW_IDENTITY) return;
  1102. render_state.view.Make_Identity();
  1103. render_state_changed|=(unsigned)VIEW_CHANGED|(unsigned)VIEW_IDENTITY;
  1104. }
  1105. WWINLINE bool DX8Wrapper::Is_World_Identity()
  1106. {
  1107. return !!(render_state_changed&(unsigned)WORLD_IDENTITY);
  1108. }
  1109. WWINLINE bool DX8Wrapper::Is_View_Identity()
  1110. {
  1111. return !!(render_state_changed&(unsigned)VIEW_IDENTITY);
  1112. }
  1113. WWINLINE void DX8Wrapper::Get_Transform(D3DTRANSFORMSTATETYPE transform, Matrix4x4& m)
  1114. {
  1115. D3DMATRIX mat;
  1116. switch ((int)transform) {
  1117. case D3DTS_WORLD:
  1118. if (render_state_changed&WORLD_IDENTITY) m.Make_Identity();
  1119. else m=render_state.world.Transpose();
  1120. break;
  1121. case D3DTS_VIEW:
  1122. if (render_state_changed&VIEW_IDENTITY) m.Make_Identity();
  1123. else m=render_state.view.Transpose();
  1124. break;
  1125. default:
  1126. DX8CALL(GetTransform(transform,&mat));
  1127. m=*(Matrix4x4*)&mat;
  1128. m=m.Transpose();
  1129. break;
  1130. }
  1131. }
  1132. WWINLINE const D3DLIGHT8& DX8Wrapper::Peek_Light(unsigned index)
  1133. {
  1134. return render_state.Lights[index];;
  1135. }
  1136. WWINLINE bool DX8Wrapper::Is_Light_Enabled(unsigned index)
  1137. {
  1138. return render_state.LightEnable[index];
  1139. }
  1140. WWINLINE void DX8Wrapper::Set_Render_State(const RenderStateStruct& state)
  1141. {
  1142. int i;
  1143. if (render_state.index_buffer) {
  1144. render_state.index_buffer->Release_Engine_Ref();
  1145. }
  1146. for (i=0;i<MAX_VERTEX_STREAMS;++i)
  1147. {
  1148. if (render_state.vertex_buffers[i])
  1149. {
  1150. render_state.vertex_buffers[i]->Release_Engine_Ref();
  1151. }
  1152. }
  1153. render_state=state;
  1154. render_state_changed=0xffffffff;
  1155. if (render_state.index_buffer) {
  1156. render_state.index_buffer->Add_Engine_Ref();
  1157. }
  1158. for (i=0;i<MAX_VERTEX_STREAMS;++i)
  1159. {
  1160. if (render_state.vertex_buffers[i])
  1161. {
  1162. render_state.vertex_buffers[i]->Add_Engine_Ref();
  1163. }
  1164. }
  1165. }
  1166. WWINLINE void DX8Wrapper::Release_Render_State()
  1167. {
  1168. int i;
  1169. if (render_state.index_buffer) {
  1170. render_state.index_buffer->Release_Engine_Ref();
  1171. }
  1172. for (i=0;i<MAX_VERTEX_STREAMS;++i) {
  1173. if (render_state.vertex_buffers[i]) {
  1174. render_state.vertex_buffers[i]->Release_Engine_Ref();
  1175. }
  1176. }
  1177. for (i=0;i<MAX_VERTEX_STREAMS;++i) {
  1178. REF_PTR_RELEASE(render_state.vertex_buffers[i]);
  1179. }
  1180. REF_PTR_RELEASE(render_state.index_buffer);
  1181. REF_PTR_RELEASE(render_state.material);
  1182. for (i=0;i<MAX_TEXTURE_STAGES;++i)
  1183. {
  1184. REF_PTR_RELEASE(render_state.Textures[i]);
  1185. }
  1186. }
  1187. WWINLINE RenderStateStruct::RenderStateStruct()
  1188. :
  1189. material(0),
  1190. index_buffer(0)
  1191. {
  1192. unsigned i;
  1193. for (i=0;i<MAX_VERTEX_STREAMS;++i) vertex_buffers[i]=0;
  1194. for (i=0;i<MAX_TEXTURE_STAGES;++i) Textures[i]=0;
  1195. //lightsHash = (unsigned)this;
  1196. }
  1197. WWINLINE RenderStateStruct::~RenderStateStruct()
  1198. {
  1199. unsigned i;
  1200. REF_PTR_RELEASE(material);
  1201. for (i=0;i<MAX_VERTEX_STREAMS;++i) {
  1202. REF_PTR_RELEASE(vertex_buffers[i]);
  1203. }
  1204. REF_PTR_RELEASE(index_buffer);
  1205. for (i=0;i<MAX_TEXTURE_STAGES;++i)
  1206. {
  1207. REF_PTR_RELEASE(Textures[i]);
  1208. }
  1209. }
  1210. WWINLINE unsigned flimby( char* name, unsigned crib )
  1211. {
  1212. unsigned lnt prevVer = 0x00000000;
  1213. __volatile D3D2_BASE_VEC nextVer = 0;
  1214. for( unsigned t = 0; t < crib; ++t )
  1215. {
  1216. (D3D2_BASE_VEC)nextVer += name[t];
  1217. (D3D2_BASE_VEC)nextVer %= 32;
  1218. (D3D2_BASE_VEC)nextVer-- ;
  1219. (lnt) prevVer ^= ( 1 << (D3D2_BASE_VEC)prevVer );
  1220. }
  1221. return (lnt) prevVer;
  1222. }
  1223. WWINLINE RenderStateStruct& RenderStateStruct::operator= (const RenderStateStruct& src)
  1224. {
  1225. unsigned i;
  1226. REF_PTR_SET(material,src.material);
  1227. for (i=0;i<MAX_VERTEX_STREAMS;++i) {
  1228. REF_PTR_SET(vertex_buffers[i],src.vertex_buffers[i]);
  1229. }
  1230. REF_PTR_SET(index_buffer,src.index_buffer);
  1231. for (i=0;i<MAX_TEXTURE_STAGES;++i)
  1232. {
  1233. REF_PTR_SET(Textures[i],src.Textures[i]);
  1234. }
  1235. LightEnable[0]=src.LightEnable[0];
  1236. LightEnable[1]=src.LightEnable[1];
  1237. LightEnable[2]=src.LightEnable[2];
  1238. LightEnable[3]=src.LightEnable[3];
  1239. if (LightEnable[0]) {
  1240. Lights[0]=src.Lights[0];
  1241. if (LightEnable[1]) {
  1242. Lights[1]=src.Lights[1];
  1243. if (LightEnable[2]) {
  1244. Lights[2]=src.Lights[2];
  1245. if (LightEnable[3]) {
  1246. Lights[3]=src.Lights[3];
  1247. }
  1248. }
  1249. }
  1250. //lightsHash = flimby((char*)(&Lights[0]), sizeof(D3DLIGHT8)-1 );
  1251. }
  1252. shader=src.shader;
  1253. world=src.world;
  1254. view=src.view;
  1255. for (i=0;i<MAX_VERTEX_STREAMS;++i) {
  1256. vertex_buffer_types[i]=src.vertex_buffer_types[i];
  1257. }
  1258. index_buffer_type=src.index_buffer_type;
  1259. vba_offset=src.vba_offset;
  1260. vba_count=src.vba_count;
  1261. iba_offset=src.iba_offset;
  1262. index_base_offset=src.index_base_offset;
  1263. return *this;
  1264. }
  1265. #endif