📄 shaderapidx8.cpp
字号:
//=========== (C) Copyright 1999 Valve, L.L.C. All rights reserved. ===========
//
// The copyright to the contents herein is the property of Valve, L.L.C.
// The contents may be used and/or copied only with the written permission of
// Valve, L.L.C., or in accordance with the terms and conditions stipulated in
// the agreement/contract under which the contents have been supplied.
//
// $Header: $
// $NoKeywords: $
//
// The dx8 implementation of the shader API
//=============================================================================
/*
DX9 todo:
-make the transforms in the older shaders match the transforms in lightmappedgeneric
-fix polyoffset for hardware that doesn't support D3DRS_SLOPESCALEDEPTHBIAS and D3DRS_DEPTHBIAS
- code is there, I think matrix offset just needs tweaking
-fix forcehardwaresync - implement texture locking for hardware that doesn't support async query
-get the format for GetAdapterModeCount and EnumAdapterModes from somewhere (shaderapidx8.cpp, GetModeCount, GetModeInfo)
-record frame sync objects (allocframesyncobjects, free framesync objects, ForceHardwareSync)
-Need to fix ENVMAPMASKSCALE, BUMPOFFSET in lightmappedgeneric*.cpp and vertexlitgeneric*.cpp
fix this:
// FIXME: This also depends on the vertex format and whether or not we are static lit in dx9
#ifndef SHADERAPIDX9
if (m_DynamicState.m_VertexShader != shader) // garymcthack
#endif // !SHADERAPIDX9
unrelated to dx9:
mat_fullbright 1 doesn't work properly on alpha materials in testroom_standards
*/
#include <windows.h>
#include "shaderapidx8.h"
#include "ShaderShadowDX8.h"
#include "locald3dtypes.h"
#include "UtlVector.h"
#include "IHardwareConfigInternal.h"
#include "UtlStack.h"
#include "IShaderUtil.h"
#include "ShaderAPIDX8_Global.h"
#include "materialsystem/IMaterialSystem.h"
#include "IMaterialInternal.h"
#include "IMeshDX8.h"
#include "ColorFormatDX8.h"
#include "TextureDX8.h"
#include <malloc.h>
#include "interface.h"
#include "CMaterialSystemStats.h"
#include "UtlRBTree.h"
#include "UtlSymbol.h"
#include "vstdlib/strTools.h"
#include "Recording.h"
#include <crtmemdebug.h>
#include "VertexShaderDX8.h"
#include "FileSystem.h"
#include "mathlib.h"
#include "materialsystem/MaterialSystem_Config.h"
#include "worldsize.h"
#include "TransitionTable.h"
#include "tier0/vcrmode.h"
#include "tier0/vprof.h"
#include "utlbuffer.h"
#include "vertexdecl.h"
#include "vstdlib/icommandline.h"
#include <KeyValues.h>
#include "../stdshaders/common_hlsl_cpp_consts.h" // hack hack hack!
// Define this if you want to use a stubbed d3d.
//#define STUBD3D
#ifdef STUBD3D
#include "stubd3ddevice.h"
#endif // STUBD3D
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#ifdef _WIN32
#pragma warning (disable:4189)
#endif
//-----------------------------------------------------------------------------
// Some important enumerations
//-----------------------------------------------------------------------------
#define TEXTURE_AMBIENT_CUBE 0xFFFFFFFE
enum
{
NUM_TEXTURE_UNITS = 4,
MAX_NUM_LIGHTS = 2,
// Hopefully, no one else will use this id..!
AMBIENT_CUBE_TEXTURE_SIZE = 8,
NUM_AMBIENT_CUBE_TEXTURES = 4,
};
//-----------------------------------------------------------------------------
// Standard vertex shader constants
//-----------------------------------------------------------------------------
enum
{
// Standard vertex shader constants
VERTEX_SHADER_CAMERA_POS = 2,
VERTEX_SHADER_LIGHT_INDEX = 3,
VERTEX_SHADER_MODELVIEWPROJ = 4,
VERTEX_SHADER_VIEWPROJ = 8,
VERTEX_SHADER_MODELVIEW = 12,
VERTEX_SHADER_FOG_PARAMS = 16,
VERTEX_SHADER_AMBIENT_LIGHT = 21,
VERTEX_SHADER_LIGHTS = 27,
VERTEX_SHADER_CLIP_DIRECTION = 37,
VERTEX_SHADER_MODEL = 42,
};
//-----------------------------------------------------------------------------
// These board states change with high frequency; are not shadowed
//-----------------------------------------------------------------------------
struct TextureStageState_t
{
int m_BoundTexture;
D3DTEXTUREADDRESS m_UTexWrap;
D3DTEXTUREADDRESS m_VTexWrap;
D3DTEXTUREFILTERTYPE m_MagFilter;
D3DTEXTUREFILTERTYPE m_MinFilter;
D3DTEXTUREFILTERTYPE m_MipFilter;
int m_nAnisotropyLevel;
D3DTEXTURETRANSFORMFLAGS m_TextureTransformFlags;
bool m_TextureEnable;
float m_BumpEnvMat00;
float m_BumpEnvMat01;
float m_BumpEnvMat10;
float m_BumpEnvMat11;
bool m_bSRGBReadEnabled;
};
enum TransformType_t
{
TRANSFORM_IS_IDENTITY = 0,
TRANSFORM_IS_CAMERA_TO_WORLD,
TRANSFORM_IS_GENERAL,
};
enum TransformDirtyBits_t
{
STATE_CHANGED_VERTEX_SHADER = 0x1,
STATE_CHANGED_FIXED_FUNCTION = 0x2,
STATE_CHANGED = 0x3
};
enum
{
MAXUSERCLIPPLANES = 6,
MAX_NUM_RENDERSTATES = ( D3DRS_BLENDOPALPHA+1 ),
};
struct DynamicState_t
{
// Constant color
unsigned int m_ConstantColor;
// point size
float m_PointSize;
// Normalize normals?
bool m_NormalizeNormals;
// Viewport state
D3DVIEWPORT m_Viewport;
// Transform state
D3DXMATRIX m_Transform[NUM_MATRIX_MODES];
unsigned char m_TransformType[NUM_MATRIX_MODES];
unsigned char m_TransformChanged[NUM_MATRIX_MODES];
// Ambient light color
D3DCOLOR m_Ambient;
D3DLIGHT m_Lights[MAX_NUM_LIGHTS];
LightDesc_t m_LightDescs[MAX_NUM_LIGHTS];
bool m_LightEnable[MAX_NUM_LIGHTS];
Vector4D m_AmbientLightCube[6];
unsigned char m_LightChanged[MAX_NUM_LIGHTS];
unsigned char m_LightEnableChanged[MAX_NUM_LIGHTS];
VertexShaderLightTypes_t m_LightType[MAX_NUM_LIGHTS];
int m_NumLights;
// Shade mode
D3DSHADEMODE m_ShadeMode;
// Clear color
D3DCOLOR m_ClearColor;
// Fog
D3DCOLOR m_FogColor;
bool m_FogEnable;
bool m_FogHeightEnabled;
D3DFOGMODE m_FogMode;
float m_FogStart;
float m_FogEnd;
float m_FogZ;
float m_HeightClipZ;
MaterialHeightClipMode_t m_HeightClipMode;
// user clip planes
int m_UserClipPlaneEnabled;
int m_UserClipPlaneChanged;
D3DXPLANE m_UserClipPlaneWorld[MAXUSERCLIPPLANES];
D3DXPLANE m_UserClipPlaneProj[MAXUSERCLIPPLANES];
bool m_FastClipEnabled;
D3DXPLANE m_FastClipPlane;
// Cull mode
D3DCULL m_CullMode;
// Skinning
D3DVERTEXBLENDFLAGS m_VertexBlend;
int m_NumBones;
// Pixel and vertex shader constants...
Vector4D* m_pPixelShaderConstant;
Vector4D* m_pVertexShaderConstant;
// Is the material reflection values divided?
float m_MaterialOverbrightVal;
float m_VertexShaderOverbright;
// Texture stage state
TextureStageState_t m_TextureStage[NUM_TEXTURE_STAGES];
DWORD m_RenderState[MAX_NUM_RENDERSTATES];
IDirect3DVertexDeclaration9 *m_pVertexDecl;
DynamicState_t() {}
private:
DynamicState_t( DynamicState_t const& );
};
//-----------------------------------------------------------------------------
// The DX8 implementation of the shader API
//-----------------------------------------------------------------------------
class CShaderAPIDX8 : public IShaderAPIDX8, public IHardwareConfigInternal
{
public:
virtual void EnableSRGBRead( TextureStage_t stage, bool bEnable );
// constructor, destructor
CShaderAPIDX8( );
virtual ~CShaderAPIDX8();
// Initialization, shutdown
bool Init( IShaderUtil* pShaderUtil, IBaseFileSystem* pFileSystem, int nAdapter, int nFlags );
void Shutdown( );
// Gets the number of adapters...
int GetDisplayAdapterCount() const;
// Returns info about each adapter
void GetDisplayAdapterInfo( int adapter, MaterialAdapterInfo_t& info ) const;
// Returns the number of modes
int GetModeCount( int adapter ) const;
// Returns mode information..
void GetModeInfo( int adapter, int mode, MaterialVideoMode_t& info ) const;
// Returns the mode info for the current display device
void GetDisplayMode( MaterialVideoMode_t& info ) const;
void Get3DDriverInfo( Material3DDriverInfo_t &info ) const;
// Returns true if the material system should try to use 16 bit textures.
virtual bool Prefer16BitTextures( ) const;
// Sets the mode...
bool SetMode( void* hwnd, MaterialVideoMode_t const& mode, int flags, int nSuperSamples = 0 );
// Creates/ destroys a child window
bool AddView( void* hwnd );
void RemoveView( void* hwnd );
// Activates a view
void SetView( void* hwnd );
// Sets the default render state
void SetDefaultState();
// Methods to ask about particular state snapshots
virtual bool IsTranslucent( StateSnapshot_t id ) const;
virtual bool IsAlphaTested( StateSnapshot_t id ) const;
virtual bool UsesVertexAndPixelShaders( StateSnapshot_t id ) const;
// returns true if the state uses an ambient cube
bool UsesAmbientCube( StateSnapshot_t id ) const;
// Computes the vertex format for a particular set of snapshot ids
VertexFormat_t ComputeVertexFormat( int num, StateSnapshot_t* pIds ) const;
VertexFormat_t ComputeVertexUsage( int num, StateSnapshot_t* pIds ) const;
// Page flip
void SwapBuffers( );
// Uses a state snapshot
void UseSnapshot( StateSnapshot_t snapshot );
// Color state
void Color3f( float r, float g, float b );
void Color4f( float r, float g, float b, float a );
void Color3fv( float const* c );
void Color4fv( float const* c );
void Color3ub( unsigned char r, unsigned char g, unsigned char b );
void Color3ubv( unsigned char const* pColor );
void Color4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a );
void Color4ubv( unsigned char const* pColor );
// Set the number of bone weights
void SetNumBoneWeights( int numBones );
// Sets the vertex and pixel shaders
virtual void SetVertexShaderIndex( int vshIndex = -1 );
virtual void SetPixelShaderIndex( int pshIndex = 0 );
// Matrix state
void MatrixMode( MaterialMatrixMode_t matrixMode );
void PushMatrix();
void PopMatrix();
void LoadMatrix( float *m );
void MultMatrix( float *m );
void MultMatrixLocal( float *m );
void GetMatrix( MaterialMatrixMode_t matrixMode, float *dst );
void LoadIdentity( void );
void LoadCameraToWorld( void );
void Ortho( double left, double top, double right, double bottom, double zNear, double zFar );
void PerspectiveX( double fovx, double aspect, double zNear, double zFar );
void PickMatrix( int x, int y, int width, int height );
void Rotate( float angle, float x, float y, float z );
void Translate( float x, float y, float z );
void Scale( float x, float y, float z );
void ScaleXY( float x, float y );
// Viewport methods
void Viewport( int x, int y, int width, int height );
void GetViewport( int& x, int& y, int& width, int& height ) const;
// Binds a particular material to render with
void Bind( IMaterial* pMaterial );
IMaterialInternal* GetBoundMaterial();
// Adds/removes precompiled shader dictionaries
virtual ShaderDLL_t AddShaderDLL( );
virtual void RemoveShaderDLL( ShaderDLL_t hShaderDLL );
virtual void AddShaderDictionary( ShaderDLL_t hShaderDLL, IPrecompiledShaderDictionary *pDict );
virtual void SetShaderDLL( ShaderDLL_t hShaderDLL );
// Level of anisotropic filtering
virtual void SetAnisotropicLevel( int nAnisotropyLevel );
virtual void SyncToken( const char *pToken );
// Point size...
void PointSize( float size );
// Cull mode
void CullMode( MaterialCullMode_t cullMode );
// Force writes only when z matches. . . useful for stenciling things out
// by rendering the desired Z values ahead of time.
void ForceDepthFuncEquals( bool bEnable );
// Turns off Z buffering
void OverrideDepthEnable( bool bEnable, bool bDepthEnable );
void SetHeightClipMode( enum MaterialHeightClipMode_t heightClipMode );
void SetHeightClipZ( float z );
void SetClipPlane( int index, const float *pPlane );
void EnableClipPlane( int index, bool bEnable );
void SetFastClipPlane(const float *pPlane);
void EnableFastClip(bool bEnable);
// The shade mode
void ShadeMode( ShaderShadeMode_t mode );
// Vertex blend state
void SetVertexBlendState( int numBones );
// Creates/destroys Mesh
IMesh* CreateStaticMesh( IMaterial* pMaterial, bool bForceTempMesh );
IMesh* CreateStaticMesh( VertexFormat_t format, bool bSoftwareVertexShader );
void DestroyStaticMesh( IMesh* mesh );
// Gets the dynamic mesh
IMesh* GetDynamicMesh( IMaterial* pMaterial, bool buffered,
IMesh* pVertexOverride, IMesh* pIndexOverride );
// Draws the mesh
void DrawMesh( IMeshDX8* mesh );
// modifies the vertex data when necessary
void ModifyVertexData( );
// Draws
void BeginPass( StateSnapshot_t snapshot );
void RenderPass( );
virtual void DestroyVertexBuffers();
void SetVertexDecl( VertexFormat_t vertexFormat, bool bHasColorMesh );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -