| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147 |
- /*
- ** Command & Conquer Generals(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /***********************************************************************************************
- *** 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 ***
- ***********************************************************************************************
- * *
- * Project Name : ww3d *
- * *
- * $Archive:: /VSS_Sync/ww3d2/dx8wrapper.h $*
- * *
- * Original Author:: Jani Penttinen *
- * *
- * $Author:: Vss_sync $*
- * *
- * $Modtime:: 8/29/01 7:29p $*
- * *
- * $Revision:: 76 $*
- * *
- *---------------------------------------------------------------------------------------------*
- * Functions: *
- * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
- #if defined(_MSC_VER)
- #pragma once
- #endif
- #ifndef DX8_WRAPPER_H
- #define DX8_WRAPPER_H
- #include "always.h"
- #include "dllist.h"
- #include "d3d8.h"
- #include "matrix4.h"
- #include "statistics.h"
- #include "wwstring.h"
- #include "lightenvironment.h"
- #include "shader.h"
- #include "vector4.h"
- #include "cpudetect.h"
- #include "texture.h"
- #include "dx8vertexbuffer.h"
- #include "dx8indexbuffer.h"
- #include "vertmaterial.h"
- const unsigned MAX_TEXTURE_STAGES=2;
- enum {
- BUFFER_TYPE_DX8,
- BUFFER_TYPE_SORTING,
- BUFFER_TYPE_DYNAMIC_DX8,
- BUFFER_TYPE_DYNAMIC_SORTING,
- BUFFER_TYPE_INVALID
- };
- class VertexMaterialClass;
- class CameraClass;
- class LightEnvironmentClass;
- class RenderDeviceDescClass;
- class VertexBufferClass;
- class DynamicVBAccessClass;
- class IndexBufferClass;
- class DynamicIBAccessClass;
- class TextureClass;
- class LightClass;
- class SurfaceClass;
- #define DX8_RECORD_MATRIX_CHANGE() matrix_changes++
- #define DX8_RECORD_MATERIAL_CHANGE() material_changes++
- #define DX8_RECORD_VERTEX_BUFFER_CHANGE() vertex_buffer_changes++
- #define DX8_RECORD_INDEX_BUFFER_CHANGE() index_buffer_changes++
- #define DX8_RECORD_LIGHT_CHANGE() light_changes++
- #define DX8_RECORD_TEXTURE_CHANGE() texture_changes++
- #define DX8_RECORD_RENDER_STATE_CHANGE() render_state_changes++
- #define DX8_RECORD_TEXTURE_STAGE_STATE_CHANGE() texture_stage_state_changes++
- extern unsigned number_of_DX8_calls;
- extern bool _DX8SingleThreaded;
- void DX8_Assert();
- void Log_DX8_ErrorCode(unsigned res);
- WWINLINE void DX8_ErrorCode(unsigned res)
- {
- if (res==D3D_OK) return;
- Log_DX8_ErrorCode(res);
- }
- #ifdef WWDEBUG
- #define DX8CALL_HRES(x,res) DX8_Assert(); res = DX8Wrapper::_Get_D3D_Device8()->x; DX8_ErrorCode(res); number_of_DX8_calls++;
- #define DX8CALL(x) DX8_Assert(); DX8_ErrorCode(DX8Wrapper::_Get_D3D_Device8()->x); number_of_DX8_calls++;
- #define DX8CALL_D3D(x) DX8_Assert(); DX8_ErrorCode(DX8Wrapper::_Get_D3D8()->x); number_of_DX8_calls++;
- #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!"); }
- #else
- #define DX8CALL_HRES(x,res) res = DX8Wrapper::_Get_D3D_Device8()->x; number_of_DX8_calls++;
- #define DX8CALL(x) DX8Wrapper::_Get_D3D_Device8()->x; number_of_DX8_calls++;
- #define DX8CALL_D3D(x) DX8Wrapper::_Get_D3D8()->x; number_of_DX8_calls++;
- #define DX8_THREAD_ASSERT() ;
- #endif
- #define no_EXTENDED_STATS
- // EXTENDED_STATS collects additional timing statistics by turning off parts
- // of the 3D drawing system (terrain, objects, etc.)
- #ifdef EXTENDED_STATS
- class DX8_Stats
- {
- public:
- bool m_showingStats;
- bool m_disableTerrain;
- bool m_disableWater;
- bool m_disableObjects;
- bool m_disableOverhead;
- bool m_disableConsole;
- int m_debugLinesToShow;
- int m_sleepTime;
- public:
- DX8_Stats::DX8_Stats(void) {
- m_disableConsole = m_showingStats = m_disableTerrain = m_disableWater = m_disableOverhead = m_disableObjects = false;
- m_sleepTime = 0;
- m_debugLinesToShow = -1; // -1 means show all expected lines of output
- }
- };
- #endif
- // This virtual interface was added for the Generals RTS.
- // It is called before resetting the dx8 device to ensure
- // that all dx8 resources are released. Otherwise reset fails. jba.
- class DX8_CleanupHook
- {
- public:
- virtual void ReleaseResources(void)=0;
- virtual void ReAcquireResources(void)=0;
- };
- struct RenderStateStruct
- {
- ShaderClass shader;
- VertexMaterialClass* material;
- TextureClass * Textures[MAX_TEXTURE_STAGES];
- D3DLIGHT8 Lights[4];
- bool LightEnable[4];
- Matrix4 world;
- Matrix4 view;
- unsigned vertex_buffer_type;
- unsigned index_buffer_type;
- unsigned short vba_offset;
- unsigned short vba_count;
- unsigned short iba_offset;
- VertexBufferClass* vertex_buffer;
- IndexBufferClass* index_buffer;
- unsigned short index_base_offset;
- RenderStateStruct();
- ~RenderStateStruct();
- RenderStateStruct& operator= (const RenderStateStruct& src);
- };
- /**
- ** DX8Wrapper
- **
- ** DX8 interface wrapper class. This encapsulates the DX8 interface; adding redundant state
- ** detection, stat tracking, etc etc. In general, we will wrap all DX8 calls with at least
- ** an WWINLINE function so that we can add stat tracking, etc if needed. Direct access to the
- ** D3D device will require "friend" status and should be granted only in extreme circumstances :-)
- */
- class DX8Wrapper
- {
- enum ChangedStates {
- WORLD_CHANGED = 1<<0,
- VIEW_CHANGED = 1<<1,
- LIGHT0_CHANGED = 1<<2,
- LIGHT1_CHANGED = 1<<3,
- LIGHT2_CHANGED = 1<<4,
- LIGHT3_CHANGED = 1<<5,
- TEXTURE0_CHANGED= 1<<6,
- TEXTURE1_CHANGED= 1<<7,
- TEXTURE2_CHANGED= 1<<8,
- TEXTURE3_CHANGED= 1<<9,
- MATERIAL_CHANGED= 1<<14,
- SHADER_CHANGED = 1<<15,
- VERTEX_BUFFER_CHANGED = 1<<16,
- INDEX_BUFFER_CHANGED = 1 << 17,
- WORLD_IDENTITY= 1<<18,
- VIEW_IDENTITY= 1<<19,
- TEXTURES_CHANGED=
- TEXTURE0_CHANGED|TEXTURE1_CHANGED|TEXTURE2_CHANGED|TEXTURE3_CHANGED,
- LIGHTS_CHANGED=
- LIGHT0_CHANGED|LIGHT1_CHANGED|LIGHT2_CHANGED|LIGHT3_CHANGED,
- };
- static void Draw_Sorting_IB_VB(
- unsigned primitive_type,
- unsigned short start_index,
- unsigned short polygon_count,
- unsigned short min_vertex_index,
- unsigned short vertex_count);
- static void Draw(
- unsigned primitive_type,
- unsigned short start_index,
- unsigned short polygon_count,
- unsigned short min_vertex_index=0,
- unsigned short vertex_count=0);
- public:
- #ifdef EXTENDED_STATS
- static DX8_Stats stats;
- #endif
-
- static bool Init(void * hwnd);
- static void Shutdown(void);
- static void SetCleanupHook(DX8_CleanupHook *pCleanupHook) {m_pCleanupHook = pCleanupHook;};
- /*
- ** Some WW3D sub-systems need to be initialized after the device is created and shutdown
- ** before the device is released.
- */
- static void Do_Onetime_Device_Dependent_Inits(void);
- static void Do_Onetime_Device_Dependent_Shutdowns(void);
- static bool Has_Stencil (void);
- static void Get_Format_Name(unsigned int format, StringClass *tex_format);
- /*
- ** Rendering
- */
- static void Begin_Scene(void);
- static void End_Scene(bool flip_frame = true);
- // Flip until the primary buffer is visible.
- static void Flip_To_Primary(void);
- 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);
- static void Set_Viewport(CONST D3DVIEWPORT8* pViewport);
- static void Set_Vertex_Buffer(const VertexBufferClass* vb);
- static void Set_Vertex_Buffer(const DynamicVBAccessClass& vba);
- static void Set_Index_Buffer(const IndexBufferClass* ib,unsigned short index_base_offset);
- static void Set_Index_Buffer(const DynamicIBAccessClass& iba,unsigned short index_base_offset);
- static void Set_Index_Buffer_Index_Offset(unsigned offset);
- static void Get_Render_State(RenderStateStruct& state);
- static void Set_Render_State(const RenderStateStruct& state);
- static void Release_Render_State();
-
- static void Set_DX8_Material(const D3DMATERIAL8* mat);
- static void Set_Gamma(float gamma,float bright,float contrast,bool calibrate=true,bool uselimit=true);
- // Set_ and Get_Transform() functions take the matrix in Westwood convention format.
- static void Set_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix4& m);
- static void Set_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix3D& m);
- static void Get_Transform(D3DTRANSFORMSTATETYPE transform, Matrix4& m);
- static void Set_World_Identity();
- static void Set_View_Identity();
- static bool Is_World_Identity();
- static bool Is_View_Identity();
- // Note that *_DX8_Transform() functions take the matrix in DX8 format - transposed from Westwood convention.
- static void _Set_DX8_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix4& m);
- static void _Set_DX8_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix3D& m);
- static void _Get_DX8_Transform(D3DTRANSFORMSTATETYPE transform, Matrix4& m);
- static void Set_DX8_Light(int index,D3DLIGHT8* light);
- static void Set_DX8_Render_State(D3DRENDERSTATETYPE state, unsigned value);
- static void Set_DX8_Clip_Plane(DWORD Index, CONST float* pPlane);
- static void Set_DX8_Texture_Stage_State(unsigned stage, D3DTEXTURESTAGESTATETYPE state, unsigned value);
- static void Set_DX8_Texture(unsigned int stage, IDirect3DBaseTexture8* texture);
- static void Set_Light_Environment(LightEnvironmentClass* light_env);
- static void Set_Fog(bool enable, const Vector3 &color, float start, float end);
- static WWINLINE const D3DLIGHT8& Peek_Light(unsigned index);
- static WWINLINE bool Is_Light_Enabled(unsigned index);
- static bool Validate_Device(void);
- // Deferred
- static void Set_Shader(const ShaderClass& shader);
- static void Get_Shader(ShaderClass& shader);
- static void Set_Texture(unsigned stage,TextureClass* texture);
- static void Set_Material(const VertexMaterialClass* material);
- static void Set_Light(unsigned index,const D3DLIGHT8* light);
- static void Set_Light(unsigned index,const LightClass &light);
- static void Apply_Render_State_Changes(); // Apply deferred render state changes (will be called automatically by Draw...)
- static void Draw_Triangles(
- unsigned buffer_type,
- unsigned short start_index,
- unsigned short polygon_count,
- unsigned short min_vertex_index,
- unsigned short vertex_count);
- static void Draw_Triangles(
- unsigned short start_index,
- unsigned short polygon_count,
- unsigned short min_vertex_index,
- unsigned short vertex_count);
- static void Draw_Strip(
- unsigned short start_index,
- unsigned short index_count,
- unsigned short min_vertex_index,
- unsigned short vertex_count);
- /*
- ** Resources
- */
- static IDirect3DTexture8 * _Create_DX8_Texture(
- unsigned int width,
- unsigned int height,
- WW3DFormat format,
- TextureClass::MipCountType mip_level_count,
- D3DPOOL pool=D3DPOOL_MANAGED,
- bool rendertarget=false);
- static IDirect3DTexture8 * _Create_DX8_Texture(const char *filename, TextureClass::MipCountType mip_level_count);
- static IDirect3DTexture8 * _Create_DX8_Texture(IDirect3DSurface8 *surface, TextureClass::MipCountType mip_level_count);
- static IDirect3DSurface8 * _Create_DX8_Surface(unsigned int width, unsigned int height, WW3DFormat format);
- static IDirect3DSurface8 * _Create_DX8_Surface(const char *filename);
- static IDirect3DSurface8 * _Get_DX8_Front_Buffer();
- static SurfaceClass * _Get_DX8_Back_Buffer(unsigned int num=0);
- static void _Copy_DX8_Rects(
- IDirect3DSurface8* pSourceSurface,
- CONST RECT* pSourceRectsArray,
- UINT cRects,
- IDirect3DSurface8* pDestinationSurface,
- CONST POINT* pDestPointsArray
- );
- static void _Update_Texture(TextureClass *system, TextureClass *video);
- static void Flush_DX8_Resource_Manager(unsigned int bytes=0);
- static unsigned int Get_Free_Texture_RAM();
- static unsigned _Get_Main_Thread_ID() { return _MainThreadID; }
- static const D3DADAPTER_IDENTIFIER8& Get_Current_Adapter_Identifier() { return CurrentAdapterIdentifier; }
- /*
- ** Statistics
- */
- static void Begin_Statistics();
- static void End_Statistics();
- static unsigned Get_Last_Frame_Matrix_Changes();
- static unsigned Get_Last_Frame_Material_Changes();
- static unsigned Get_Last_Frame_Vertex_Buffer_Changes();
- static unsigned Get_Last_Frame_Index_Buffer_Changes();
- static unsigned Get_Last_Frame_Light_Changes();
- static unsigned Get_Last_Frame_Texture_Changes();
- static unsigned Get_Last_Frame_Render_State_Changes();
- static unsigned Get_Last_Frame_Texture_Stage_State_Changes();
- static unsigned Get_Last_Frame_DX8_Calls();
- static unsigned long Get_FrameCount(void);
- // Needed by shader class
- static bool Get_Fog_Enable() { return FogEnable; }
- static D3DCOLOR Get_Fog_Color() { return FogColor; }
- // Utilities
- static Vector4 Convert_Color(unsigned color);
- static unsigned int Convert_Color(const Vector4& color);
- static unsigned int Convert_Color(const Vector3& color, const float alpha);
- static void Clamp_Color(Vector4& color);
- static unsigned int Convert_Color_Clamp(const Vector4& color);
- static void Set_Alpha (const float alpha, unsigned int &color);
- static void _Enable_Triangle_Draw(bool enable) { _EnableTriangleDraw=enable; }
- static bool _Is_Triangle_Draw_Enabled() { return _EnableTriangleDraw; }
- /*
- ** Additional swap chain interface
- **
- ** Use this interface to render to multiple windows (in windowed mode).
- ** To render to an additional window, the sequence of calls should look
- ** something like this:
- **
- ** DX8Wrapper::Set_Render_Target (swap_chain_ptr);
- **
- ** WW3D::Begin_Render (true, true, Vector3 (0, 0, 0));
- ** WW3D::Render (scene, camera, FALSE, FALSE);
- ** WW3D::End_Render ();
- **
- ** swap_chain_ptr->Present (NULL, NULL, NULL, NULL);
- **
- ** DX8Wrapper::Set_Render_Target ((IDirect3DSurface8 *)NULL);
- **
- */
- static IDirect3DSwapChain8 * Create_Additional_Swap_Chain (HWND render_window);
- /*
- ** Render target interface
- */
- static TextureClass * Create_Render_Target (int width, int height, bool alpha=false);
-
- static void Set_Render_Target (TextureClass * texture);
- static void Set_Render_Target (IDirect3DSurface8 *render_target);
- static void Set_Render_Target (IDirect3DSwapChain8 *swap_chain);
- static IDirect3DDevice8* _Get_D3D_Device8() { return D3DDevice; }
- static IDirect3D8* _Get_D3D8() { return D3DInterface; }
- static void Invalidate_Cached_Render_States(void);
- /// Returns the display format - added by TR for video playback - not part of W3D
- static WW3DFormat getBackBufferFormat( void );
- static bool Reset_Device(bool reload_assets=true);
- protected:
- static bool Create_Device(void);
- static void Release_Device(void);
- static void Reset_Statistics();
- static void Enumerate_Devices();
- static void Set_Default_Global_Render_States(void);
- /*
- ** Device Selection Code.
- ** For backward compatibility, the public interface for these functions is in the ww3d.
- ** header file. These functions are protected so that we aren't exposing two interfaces.
- */
- static bool Set_Any_Render_Device(void);
- 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);
- 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);
- static bool Set_Next_Render_Device(void);
- static bool Toggle_Windowed(void);
- static int Get_Render_Device_Count(void);
- static int Get_Render_Device(void);
- static const RenderDeviceDescClass & Get_Render_Device_Desc(int deviceidx);
- static const char * Get_Render_Device_Name(int device_index);
- static bool Set_Device_Resolution(int width=-1,int height=-1,int bits=-1,int windowed=-1, bool resize_window=false);
- static void Get_Device_Resolution(int & set_w,int & set_h,int & set_bits,bool & set_windowed);
- static void Get_Render_Target_Resolution(int & set_w,int & set_h,int & set_bits,bool & set_windowed);
- static int Get_Device_Resolution_Width(void) { return ResolutionWidth; }
- static int Get_Device_Resolution_Height(void) { return ResolutionHeight; }
- static bool Registry_Save_Render_Device( const char * sub_key );
- static bool Registry_Save_Render_Device( const char *sub_key, int device, int width, int height, int depth, bool windowed, int texture_depth);
- static bool Registry_Load_Render_Device( const char * sub_key, bool resize_window );
- 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);
- static bool Is_Windowed(void) { return IsWindowed; }
-
- static void Set_Texture_Bitdepth(int depth) { WWASSERT(depth==16 || depth==32); TextureBitDepth = depth; }
- static int Get_Texture_Bitdepth(void) { return TextureBitDepth; }
- static void Set_Swap_Interval(int swap);
- static int Get_Swap_Interval(void);
- static void Set_Polygon_Mode(int mode);
- /*
- ** Internal functions
- */
- static bool Find_Color_And_Z_Mode(int resx,int resy,int bitdepth,D3DFORMAT * set_colorbuffer,D3DFORMAT * set_backbuffer, D3DFORMAT * set_zmode);
- static bool Find_Color_Mode(D3DFORMAT colorbuffer, int resx, int resy, UINT *mode);
- static bool Find_Z_Mode(D3DFORMAT colorbuffer,D3DFORMAT backbuffer, D3DFORMAT *zmode);
- static bool Test_Z_Mode(D3DFORMAT colorbuffer,D3DFORMAT backbuffer, D3DFORMAT zmode);
- static void Compute_Caps(D3DFORMAT display_format,D3DFORMAT depth_stencil_format);
- /*
- ** Protected Member Variables
- */
- static DX8_CleanupHook *m_pCleanupHook;
- static RenderStateStruct render_state;
- static unsigned render_state_changed;
- static bool IsInitted;
- static void * Hwnd;
- static unsigned _MainThreadID;
- static bool _EnableTriangleDraw;
- static int CurRenderDevice;
- static int ResolutionWidth;
- static int ResolutionHeight;
- static int BitDepth;
- static int TextureBitDepth;
- static bool IsWindowed;
- static D3DFORMAT DisplayFormat;
-
- static D3DMATRIX old_world;
- static D3DMATRIX old_view;
- static D3DMATRIX old_prj;
- static bool world_identity;
- static unsigned RenderStates[256];
- static unsigned TextureStageStates[MAX_TEXTURE_STAGES][32];
- static IDirect3DBaseTexture8 * Textures[MAX_TEXTURE_STAGES];
- // These fog settings are constant for all objects in a given scene,
- // unlike the matching renderstates which vary based on shader settings.
- static bool FogEnable;
- static D3DCOLOR FogColor;
- static unsigned matrix_changes;
- static unsigned material_changes;
- static unsigned vertex_buffer_changes;
- static unsigned index_buffer_changes;
- static unsigned light_changes;
- static unsigned texture_changes;
- static unsigned render_state_changes;
- static unsigned texture_stage_state_changes;
- static bool CurrentDX8LightEnables[4];
- static unsigned long FrameCount;
- static D3DADAPTER_IDENTIFIER8 CurrentAdapterIdentifier;
- static IDirect3D8 * D3DInterface; //d3d8;
- static IDirect3DDevice8 * D3DDevice; //d3ddevice8;
- static IDirect3DSurface8 * CurrentRenderTarget;
- static IDirect3DSurface8 * DefaultRenderTarget;
- friend void DX8_Assert();
- friend class WW3D;
- friend class DX8IndexBufferClass;
- friend class DX8VertexBufferClass;
- };
- WWINLINE void DX8Wrapper::_Set_DX8_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix4& m)
- {
- SNAPSHOT_SAY(("DX8 - SetTransform\n"));
- DX8_RECORD_MATRIX_CHANGE();
- DX8CALL(SetTransform(transform,(D3DMATRIX*)&m));
- }
- WWINLINE void DX8Wrapper::_Set_DX8_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix3D& m)
- {
- SNAPSHOT_SAY(("DX8 - SetTransform\n"));
- DX8_RECORD_MATRIX_CHANGE();
- DX8CALL(SetTransform(transform,(D3DMATRIX*)&m));
- }
- WWINLINE void DX8Wrapper::_Get_DX8_Transform(D3DTRANSFORMSTATETYPE transform, Matrix4& m)
- {
- DX8CALL(GetTransform(transform,(D3DMATRIX*)&m));
- }
- // ----------------------------------------------------------------------------
- //
- // Set the index offset for the current index buffer
- //
- // ----------------------------------------------------------------------------
- WWINLINE void DX8Wrapper::Set_Index_Buffer_Index_Offset(unsigned offset)
- {
- if (render_state.index_base_offset==offset) return;
- render_state.index_base_offset=offset;
- render_state_changed|=INDEX_BUFFER_CHANGED;
- }
- // ----------------------------------------------------------------------------
- // Set the fog settings. This function should be used, rather than setting the
- // appropriate renderstates directly, because the shader sets some of the
- // renderstates on a per-mesh / per-pass basis depending on global fog states
- // (stored in the wrapper) as well as the shader settings.
- // This function should be called rarely - once per scene would be appropriate.
- // ----------------------------------------------------------------------------
- WWINLINE void DX8Wrapper::Set_Fog(bool enable, const Vector3 &color, float start, float end)
- {
- // Set global states
- FogEnable = enable;
- FogColor = Convert_Color(color,0.0f);
- // Invalidate the current shader (since the renderstates set by the shader
- // depend on the global fog settings as well as the actual shader settings)
- ShaderClass::Invalidate();
- // Set renderstates which are not affected by the shader
- Set_DX8_Render_State(D3DRS_FOGSTART, *(DWORD *)(&start));
- Set_DX8_Render_State(D3DRS_FOGEND, *(DWORD *)(&end));
- }
- // ----------------------------------------------------------------------------
- //
- // Set vertex buffer to be used in the subsequent render calls. If there was
- // a vertex buffer being used earlier, release the reference to it. Passing
- // NULL just will release the vertex buffer.
- //
- // ----------------------------------------------------------------------------
- WWINLINE void DX8Wrapper::Set_DX8_Material(const D3DMATERIAL8* mat)
- {
- DX8_RECORD_MATERIAL_CHANGE();
- WWASSERT(mat);
- SNAPSHOT_SAY(("DX8 - SetMaterial\n"));
- DX8CALL(SetMaterial(mat));
- }
- WWINLINE void DX8Wrapper::Set_DX8_Light(int index, D3DLIGHT8* light)
- {
- if (light) {
- DX8_RECORD_LIGHT_CHANGE();
- DX8CALL(SetLight(index,light));
- DX8CALL(LightEnable(index,TRUE));
- CurrentDX8LightEnables[index]=true;
- SNAPSHOT_SAY(("DX8 - SetLight\n"));
- }
- else if (CurrentDX8LightEnables[index]) {
- DX8_RECORD_LIGHT_CHANGE();
- CurrentDX8LightEnables[index]=false;
- DX8CALL(LightEnable(index,FALSE));
- SNAPSHOT_SAY(("DX8 - DisableLight\n"));
- }
- }
- WWINLINE void DX8Wrapper::Set_DX8_Render_State(D3DRENDERSTATETYPE state, unsigned value)
- {
- // Can't monitor state changes because setShader call to GERD may change the states!
- if (RenderStates[state]==value) return;
- SNAPSHOT_SAY(("DX8 - SetRenderState(%d,%d)\n",state,value));
- RenderStates[state]=value;
- DX8CALL(SetRenderState( state, value ));
- DX8_RECORD_RENDER_STATE_CHANGE();
- }
- WWINLINE void DX8Wrapper::Set_DX8_Clip_Plane(DWORD Index, CONST float* pPlane)
- {
- DX8CALL(SetClipPlane( Index, pPlane ));
- }
- WWINLINE void DX8Wrapper::Set_DX8_Texture_Stage_State(unsigned stage, D3DTEXTURESTAGESTATETYPE state, unsigned value)
- {
- if (stage >= MAX_TEXTURE_STAGES)
- { DX8CALL(SetTextureStageState( stage, state, value ));
- return;
- }
- // Can't monitor state changes because setShader call to GERD may change the states!
- if (TextureStageStates[stage][(unsigned int)state]==value) return;
- SNAPSHOT_SAY(("DX8 - SetTextureStageState(%d,%d,%d)\n",stage,state,value));
- TextureStageStates[stage][(unsigned int)state]=value;
- DX8CALL(SetTextureStageState( stage, state, value ));
- DX8_RECORD_TEXTURE_STAGE_STATE_CHANGE();
- }
- WWINLINE void DX8Wrapper::Set_DX8_Texture(unsigned int stage, IDirect3DBaseTexture8* texture)
- {
- if (stage >= MAX_TEXTURE_STAGES)
- { DX8CALL(SetTexture(stage, texture));
- return;
- }
- if (Textures[stage]==texture) return;
- SNAPSHOT_SAY(("DX8 - SetTexture(%x) \n",texture));
- if (Textures[stage]) Textures[stage]->Release();
- Textures[stage] = texture;
- if (Textures[stage]) Textures[stage]->AddRef();
- DX8CALL(SetTexture(stage, texture));
- DX8_RECORD_TEXTURE_CHANGE();
- }
- WWINLINE void DX8Wrapper::_Copy_DX8_Rects(
- IDirect3DSurface8* pSourceSurface,
- CONST RECT* pSourceRectsArray,
- UINT cRects,
- IDirect3DSurface8* pDestinationSurface,
- CONST POINT* pDestPointsArray
- )
- {
- DX8CALL(CopyRects(
- pSourceSurface,
- pSourceRectsArray,
- cRects,
- pDestinationSurface,
- pDestPointsArray));
- }
- WWINLINE Vector4 DX8Wrapper::Convert_Color(unsigned color)
- {
- Vector4 col;
- col[3]=((color&0xff000000)>>24)/255.0f;
- col[0]=((color&0xff0000)>>16)/255.0f;
- col[1]=((color&0xff00)>>8)/255.0f;
- col[2]=((color&0xff)>>0)/255.0f;
- // col=Vector4(1.0f,1.0f,1.0f,1.0f);
- return col;
- }
- #if 0
- WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector3& color, const float alpha)
- {
- WWASSERT(color.X<=1.0f);
- WWASSERT(color.Y<=1.0f);
- WWASSERT(color.Z<=1.0f);
- WWASSERT(alpha<=1.0f);
- WWASSERT(color.X>=0.0f);
- WWASSERT(color.Y>=0.0f);
- WWASSERT(color.Z>=0.0f);
- WWASSERT(alpha>=0.0f);
- return D3DCOLOR_COLORVALUE(color.X,color.Y,color.Z,alpha);
- }
- WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector4& color)
- {
- WWASSERT(color.X<=1.0f);
- WWASSERT(color.Y<=1.0f);
- WWASSERT(color.Z<=1.0f);
- WWASSERT(color.W<=1.0f);
- WWASSERT(color.X>=0.0f);
- WWASSERT(color.Y>=0.0f);
- WWASSERT(color.Z>=0.0f);
- WWASSERT(color.W>=0.0f);
- return D3DCOLOR_COLORVALUE(color.X,color.Y,color.Z,color.W);
- }
- #else
- // ----------------------------------------------------------------------------
- //
- // Convert RGBA color from float vector to 32 bit integer
- // Note: Color vector needs to be clamped to [0...1] range!
- //
- // ----------------------------------------------------------------------------
- WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector3& color,float alpha)
- {
- const float scale = 255.0;
- unsigned int col;
- // Multiply r, g, b and a components (0.0,...,1.0) by 255 and convert to integer. Or the integer values togerher
- // such that 32 bit ingeger has AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB.
- __asm
- {
- sub esp,20 // space for a, r, g and b float plus fpu rounding mode
- // Store the fpu rounding mode
- fwait
- fstcw [esp+16] // store control word to stack
- mov eax,[esp+16] // load it to eax
- mov edi,eax // take copy
- and eax,~(1024|2048) // mask out certain bits
- or eax,(1024|2048) // or with precision control value "truncate"
- sub edi,eax // did it change?
- jz skip // .. if not, skip
- mov [esp],eax // .. change control word
- fldcw [esp]
- skip:
- // Convert the color
- mov esi,dword ptr color
- fld dword ptr[scale]
- fld dword ptr[esi] // r
- fld dword ptr[esi+4] // g
- fld dword ptr[esi+8] // b
- fld dword ptr[alpha] // a
- fld st(4)
- fmul st(4),st
- fmul st(3),st
- fmul st(2),st
- fmulp st(1),st
- fistp dword ptr[esp+0] // a
- fistp dword ptr[esp+4] // b
- fistp dword ptr[esp+8] // g
- fistp dword ptr[esp+12] // r
- mov ecx,[esp] // a
- mov eax,[esp+4] // b
- mov edx,[esp+8] // g
- mov ebx,[esp+12] // r
- shl ecx,24 // a << 24
- shl ebx,16 // r << 16
- shl edx,8 // g << 8
- or eax,ecx // (a << 24) | b
- or eax,ebx // (a << 24) | (r << 16) | b
- or eax,edx // (a << 24) | (r << 16) | (g << 8) | b
-
- fstp st(0)
- // Restore fpu rounding mode
- cmp edi,0 // did we change the value?
- je not_changed // nope... skip now...
- fwait
- fldcw [esp+16];
- not_changed:
- add esp,20
- mov col,eax
- }
- return col;
- }
- // ----------------------------------------------------------------------------
- //
- // Clamp color vertor to [0...1] range
- //
- // ----------------------------------------------------------------------------
- WWINLINE void DX8Wrapper::Clamp_Color(Vector4& color)
- {
- if (!CPUDetectClass::Has_CMOV_Instruction()) {
- for (int i=0;i<4;++i) {
- float f=(color[i]<0.0f) ? 0.0f : color[i];
- color[i]=(f>1.0f) ? 1.0f : f;
- }
- return;
- }
- __asm
- {
- mov esi,dword ptr color
- mov edx,0x3f800000
- mov edi,dword ptr[esi]
- mov ebx,edi
- sar edi,31
- not edi // mask is now zero if negative value
- and edi,ebx
- cmp edi,edx // if no less than 1.0 set to 1.0
- cmovnb edi,edx
- mov dword ptr[esi],edi
- mov edi,dword ptr[esi+4]
- mov ebx,edi
- sar edi,31
- not edi // mask is now zero if negative value
- and edi,ebx
- cmp edi,edx // if no less than 1.0 set to 1.0
- cmovnb edi,edx
- mov dword ptr[esi+4],edi
- mov edi,dword ptr[esi+8]
- mov ebx,edi
- sar edi,31
- not edi // mask is now zero if negative value
- and edi,ebx
- cmp edi,edx // if no less than 1.0 set to 1.0
- cmovnb edi,edx
- mov dword ptr[esi+8],edi
- mov edi,dword ptr[esi+12]
- mov ebx,edi
- sar edi,31
- not edi // mask is now zero if negative value
- and edi,ebx
- cmp edi,edx // if no less than 1.0 set to 1.0
- cmovnb edi,edx
- mov dword ptr[esi+12],edi
- }
- }
- // ----------------------------------------------------------------------------
- //
- // Convert RGBA color from float vector to 32 bit integer
- //
- // ----------------------------------------------------------------------------
- WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector4& color)
- {
- return Convert_Color(reinterpret_cast<const Vector3&>(color),color[3]);
- }
- WWINLINE unsigned int DX8Wrapper::Convert_Color_Clamp(const Vector4& color)
- {
- Vector4 clamped_color=color;
- DX8Wrapper::Clamp_Color(clamped_color);
- return Convert_Color(reinterpret_cast<const Vector3&>(clamped_color),clamped_color[3]);
- }
- #endif
- WWINLINE void DX8Wrapper::Set_Alpha (const float alpha, unsigned int &color)
- {
- unsigned char *component = (unsigned char*) &color;
- component [3] = 255.0f * alpha;
- }
- WWINLINE void DX8Wrapper::Get_Render_State(RenderStateStruct& state)
- {
- state=render_state;
- }
- WWINLINE void DX8Wrapper::Get_Shader(ShaderClass& shader)
- {
- shader=render_state.shader;
- }
- WWINLINE void DX8Wrapper::Set_Texture(unsigned stage,TextureClass* texture)
- {
- WWASSERT(stage<MAX_TEXTURE_STAGES);
- if (texture==render_state.Textures[stage]) return;
- REF_PTR_SET(render_state.Textures[stage],texture);
- render_state_changed|=(TEXTURE0_CHANGED<<stage);
- }
- WWINLINE void DX8Wrapper::Set_Material(const VertexMaterialClass* material)
- {
- if (material==render_state.material) return;
- REF_PTR_SET(render_state.material,const_cast<VertexMaterialClass*>(material));
- render_state_changed|=MATERIAL_CHANGED;
- }
- WWINLINE void DX8Wrapper::Set_Shader(const ShaderClass& shader)
- {
- if (!ShaderClass::ShaderDirty && ((unsigned&)shader==(unsigned&)render_state.shader)) return;
- render_state.shader=shader;
- render_state_changed|=SHADER_CHANGED;
- }
- WWINLINE void DX8Wrapper::Set_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix4& m)
- {
- switch ((int)transform) {
- case D3DTS_WORLD:
- render_state.world=m.Transpose();
- render_state_changed|=(unsigned)WORLD_CHANGED;
- render_state_changed&=~(unsigned)WORLD_IDENTITY;
- break;
- case D3DTS_VIEW:
- render_state.view=m.Transpose();
- render_state_changed|=(unsigned)VIEW_CHANGED;
- render_state_changed&=~(unsigned)VIEW_IDENTITY;
- break;
- default:
- DX8_RECORD_MATRIX_CHANGE();
- Matrix4 m2=m.Transpose();
- DX8CALL(SetTransform(transform,(D3DMATRIX*)&m2));
- break;
- }
- }
- WWINLINE void DX8Wrapper::Set_Transform(D3DTRANSFORMSTATETYPE transform,const Matrix3D& m)
- {
- Matrix4 m2(m);
- switch ((int)transform) {
- case D3DTS_WORLD:
- render_state.world=m2.Transpose();
- render_state_changed|=(unsigned)WORLD_CHANGED;
- render_state_changed&=~(unsigned)WORLD_IDENTITY;
- break;
- case D3DTS_VIEW:
- render_state.view=m2.Transpose();
- render_state_changed|=(unsigned)VIEW_CHANGED;
- render_state_changed&=~(unsigned)VIEW_IDENTITY;
- break;
- default:
- DX8_RECORD_MATRIX_CHANGE();
- m2=m2.Transpose();
- DX8CALL(SetTransform(transform,(D3DMATRIX*)&m2));
- break;
- }
- }
- WWINLINE void DX8Wrapper::Set_World_Identity()
- {
- if (render_state_changed&(unsigned)WORLD_IDENTITY) return;
- render_state.world.Make_Identity();
- render_state_changed|=(unsigned)WORLD_CHANGED|(unsigned)WORLD_IDENTITY;
- }
- WWINLINE void DX8Wrapper::Set_View_Identity()
- {
- if (render_state_changed&(unsigned)VIEW_IDENTITY) return;
- render_state.view.Make_Identity();
- render_state_changed|=(unsigned)VIEW_CHANGED|(unsigned)VIEW_IDENTITY;
- }
- WWINLINE bool DX8Wrapper::Is_World_Identity()
- {
- return !!(render_state_changed&(unsigned)WORLD_IDENTITY);
- }
- WWINLINE bool DX8Wrapper::Is_View_Identity()
- {
- return !!(render_state_changed&(unsigned)VIEW_IDENTITY);
- }
- WWINLINE void DX8Wrapper::Get_Transform(D3DTRANSFORMSTATETYPE transform, Matrix4& m)
- {
- D3DMATRIX mat;
- switch ((int)transform) {
- case D3DTS_WORLD:
- if (render_state_changed&WORLD_IDENTITY) m.Make_Identity();
- else m=render_state.world.Transpose();
- break;
- case D3DTS_VIEW:
- if (render_state_changed&VIEW_IDENTITY) m.Make_Identity();
- else m=render_state.view.Transpose();
- break;
- default:
- DX8CALL(GetTransform(transform,&mat));
- m=*(Matrix4*)&mat;
- m=m.Transpose();
- break;
- }
- }
- WWINLINE void DX8Wrapper::Set_Light(unsigned index, const D3DLIGHT8* light)
- {
- if (light) {
- render_state.Lights[index]=*light;
- render_state.LightEnable[index]=true;
- }
- else {
- render_state.LightEnable[index]=false;
- }
- render_state_changed|=(LIGHT0_CHANGED<<index);
- }
- WWINLINE const D3DLIGHT8& DX8Wrapper::Peek_Light(unsigned index)
- {
- return render_state.Lights[index];;
- }
- WWINLINE bool DX8Wrapper::Is_Light_Enabled(unsigned index)
- {
- return render_state.LightEnable[index];
- }
- WWINLINE void DX8Wrapper::Set_Render_State(const RenderStateStruct& state)
- {
- if (render_state.index_buffer) {
- render_state.index_buffer->Release_Engine_Ref();
- }
- if (render_state.vertex_buffer) {
- render_state.vertex_buffer->Release_Engine_Ref();
- }
- render_state=state;
- render_state_changed=0xffffffff;
- if (render_state.index_buffer) {
- render_state.index_buffer->Add_Engine_Ref();
- }
- if (render_state.vertex_buffer) {
- render_state.vertex_buffer->Add_Engine_Ref();
- }
- }
- WWINLINE void DX8Wrapper::Release_Render_State()
- {
- if (render_state.index_buffer) {
- render_state.index_buffer->Release_Engine_Ref();
- }
- if (render_state.vertex_buffer) {
- render_state.vertex_buffer->Release_Engine_Ref();
- }
- REF_PTR_RELEASE(render_state.vertex_buffer);
- REF_PTR_RELEASE(render_state.index_buffer);
- REF_PTR_RELEASE(render_state.material);
- for (unsigned i=0;i<MAX_TEXTURE_STAGES;++i) REF_PTR_RELEASE(render_state.Textures[i]);
- }
- WWINLINE RenderStateStruct::RenderStateStruct()
- :
- material(0),
- vertex_buffer(0),
- index_buffer(0)
- {
- for (unsigned i=0;i<MAX_TEXTURE_STAGES;++i) Textures[i]=0;
- }
- WWINLINE RenderStateStruct::~RenderStateStruct()
- {
- REF_PTR_RELEASE(material);
- REF_PTR_RELEASE(vertex_buffer);
- REF_PTR_RELEASE(index_buffer);
- for (unsigned i=0;i<MAX_TEXTURE_STAGES;++i) REF_PTR_RELEASE(Textures[i]);
- }
- WWINLINE RenderStateStruct& RenderStateStruct::operator= (const RenderStateStruct& src)
- {
- REF_PTR_SET(material,src.material);
- REF_PTR_SET(vertex_buffer,src.vertex_buffer);
- REF_PTR_SET(index_buffer,src.index_buffer);
- for (unsigned i=0;i<MAX_TEXTURE_STAGES;++i) REF_PTR_SET(Textures[i],src.Textures[i]);
- LightEnable[0]=src.LightEnable[0];
- LightEnable[1]=src.LightEnable[1];
- LightEnable[2]=src.LightEnable[2];
- LightEnable[3]=src.LightEnable[3];
- if (LightEnable[0]) {
- Lights[0]=src.Lights[0];
- if (LightEnable[1]) {
- Lights[1]=src.Lights[1];
- if (LightEnable[2]) {
- Lights[2]=src.Lights[2];
- if (LightEnable[3]) {
- Lights[3]=src.Lights[3];
- }
- }
- }
- }
- shader=src.shader;
- world=src.world;
- view=src.view;
- vertex_buffer_type=src.vertex_buffer_type;
- index_buffer_type=src.index_buffer_type;
- vba_offset=src.vba_offset;
- vba_count=src.vba_count;
- iba_offset=src.iba_offset;
- index_base_offset=src.index_base_offset;
- return *this;
- }
- #endif
|