⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shaderapiempty.cpp

📁 hl2 source code. Do not use it illegal.
💻 CPP
📖 第 1 页 / 共 4 页
字号:
//=========== (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 empty implementation of the shader API
//=============================================================================

#include "utlvector.h"
#include "materialsystem/imaterialsystem.h"
#include "IHardwareConfigInternal.h"
#include "shadersystem.h"
#include "ishaderutil.h"
#include "shaderapi.h"
#include "materialsystem/imesh.h"
#include "tier0/dbg.h"


//-----------------------------------------------------------------------------
// The empty mesh
//-----------------------------------------------------------------------------
class CEmptyMesh : public IMesh
{
public:
	CEmptyMesh();
	virtual ~CEmptyMesh();

	// Locks/ unlocks the vertex buffer, returns the index of the first vertex
	// numIndices of -1 means don't lock the index buffer...
	void LockMesh( int numVerts, int numIndices, MeshDesc_t& desc );
	void UnlockMesh( int numVerts, int numIndices, MeshDesc_t& desc );

	void ModifyBegin( int firstVertex, int numVerts, int firstIndex, int numIndices, MeshDesc_t& desc );
	void ModifyEnd( );

	// returns the # of vertices (static meshes only)
	int  NumVertices() const;

	// Helper methods to create various standard index buffer types
	void GenerateSequentialIndexBuffer( unsigned short* pIndexMemory, 
										int numIndices, int firstVertex );
	void GenerateQuadIndexBuffer( unsigned short* pIndexMemory, 
									int numIndices, int firstVertex );
	void GeneratePolygonIndexBuffer( unsigned short* pIndexMemory, 
									int numIndices, int firstVertex );
	void GenerateLineStripIndexBuffer( unsigned short* pIndexMemory, 
									int numIndices, int firstVertex );
	void GenerateLineLoopIndexBuffer( unsigned short* pIndexMemory, 
									int numIndices, int firstVertex );

	// Sets the primitive type
	void SetPrimitiveType( MaterialPrimitiveType_t type );
	 
	// Draws the entire mesh
	void Draw(int firstIndex, int numIndices);

	void Draw(CPrimList *pPrims, int nPrims);

	// Copy verts and/or indices to a mesh builder. This only works for temp meshes!
	virtual void CopyToMeshBuilder( 
		int iStartVert,		// Which vertices to copy.
		int nVerts, 
		int iStartIndex,	// Which indices to copy.
		int nIndices, 
		int indexOffset,	// This is added to each index.
		CMeshBuilder &builder );

	// Spews the mesh data
	void Spew( int numVerts, int numIndices, MeshDesc_t const& desc );

	void ValidateData( int numVerts, int numIndices, MeshDesc_t const& desc );

	// gets the associated material
	IMaterial* GetMaterial();

	void SetSoftwareVertexShader( SoftwareVertexShader_t shader );
	void CallSoftwareVertexShader( CMeshBuilder *pMeshBuilder );

	void SetColorMesh( IMesh *pColorMesh )
	{
	}

private:
	enum
	{
		VERTEX_BUFFER_SIZE = 1024 * 1024
	};

	unsigned char* m_pVertexMemory;
};

//-----------------------------------------------------------------------------
// The empty shader shadow
//-----------------------------------------------------------------------------

class CShaderShadowEmpty : public IShaderShadow
{
public:
	CShaderShadowEmpty();
	virtual ~CShaderShadowEmpty();

	// Sets the default *shadow* state
	void SetDefaultState();

	// Methods related to depth buffering
	void DepthFunc( ShaderDepthFunc_t depthFunc );
	void EnableDepthWrites( bool bEnable );
	void EnableDepthTest( bool bEnable );
	void EnablePolyOffset( bool bEnable );

	// Suppresses/activates color writing 
	void EnableColorWrites( bool bEnable );
	void EnableAlphaWrites( bool bEnable );

	// Methods related to alpha blending
	void EnableBlending( bool bEnable );
	void BlendFunc( ShaderBlendFactor_t srcFactor, ShaderBlendFactor_t dstFactor );

	// Alpha testing
	void EnableAlphaTest( bool bEnable );
	void AlphaFunc( ShaderAlphaFunc_t alphaFunc, float alphaRef /* [0-1] */ );

	// Wireframe/filled polygons
	void PolyMode( ShaderPolyModeFace_t face, ShaderPolyMode_t polyMode );

	// Back face culling
	void EnableCulling( bool bEnable );
	
	// constant color + transparency
	void EnableConstantColor( bool bEnable );

	// Indicates we're preprocessing vertex data
	void EnableVertexDataPreprocess( bool bEnable );

	// Indicates the vertex format for use with a vertex shader
	// The flags to pass in here come from the VertexFormatFlags_t enum
	// If pTexCoordDimensions is *not* specified, we assume all coordinates
	// are 2-dimensional
	void VertexShaderVertexFormat( unsigned int flags, 
			int numTexCoords, int* pTexCoordDimensions, int numBoneWeights,
			int userDataSize );
	
	// Indicates we're going to light the model
	void EnableLighting( bool bEnable );
	void EnableSpecular( bool bEnable );

	// Indicates we're going to be using the ambient cube on stage0
	void EnableAmbientLightCubeOnStage0( bool bEnable );

	// vertex blending
	void EnableVertexBlend( bool bEnable );

	// per texture unit stuff
	void OverbrightValue( TextureStage_t stage, float value );
	void EnableTexture( TextureStage_t stage, bool bEnable );
	void EnableTexGen( TextureStage_t stage, bool bEnable );
	void TexGen( TextureStage_t stage, ShaderTexGenParam_t param );

	// alternate method of specifying per-texture unit stuff, more flexible and more complicated
	// Can be used to specify different operation per channel (alpha/color)...
	void EnableCustomPixelPipe( bool bEnable );
	void CustomTextureStages( int stageCount );
	void CustomTextureOperation( TextureStage_t stage, ShaderTexChannel_t channel, 
		ShaderTexOp_t op, ShaderTexArg_t arg1, ShaderTexArg_t arg2 );

	void VertexShaderOverbrightValue( float value );

	// indicates what per-vertex data we're providing
	void DrawFlags( unsigned int drawFlags );

	// A simpler method of dealing with alpha modulation
	void EnableAlphaPipe( bool bEnable );
	void EnableConstantAlpha( bool bEnable );
	void EnableVertexAlpha( bool bEnable );
	void EnableTextureAlpha( TextureStage_t stage, bool bEnable );

	// GR - Separate alpha blending
	void EnableBlendingSeparateAlpha( bool bEnable );
	void BlendFuncSeparateAlpha( ShaderBlendFactor_t srcFactor, ShaderBlendFactor_t dstFactor );

	// Sets the vertex and pixel shaders
	void SetVertexShader( const char *pFileName, int vshIndex );
	void SetPixelShader( const char *pFileName, int pshIndex );

	// Convert from linear to gamma color space on writes to frame buffer.
	void EnableSRGBWrite( bool bEnable )
	{
	}

	bool m_IsTranslucent;
	bool m_IsAlphaTested;
	bool m_UsesAmbientCube;
	bool m_bUsesVertexAndPixelShaders;
};


//-----------------------------------------------------------------------------
// The DX8 implementation of the shader API
//-----------------------------------------------------------------------------
class CShaderAPIEmpty : public IShaderAPI, public IHardwareConfigInternal
{
public:
	// constructor, destructor
	CShaderAPIEmpty( );
	virtual ~CShaderAPIEmpty();

	// 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;

	// Used to clear the transition table when we know it's become invalid.
	void ClearSnapshots();

	// Returns true if the material system should try to use 16 bit textures.
	bool Prefer16BitTextures( ) const;
	
	// Sets the mode...
	bool SetMode( void* hwnd, MaterialVideoMode_t const& mode, int flags, int nSampleCount );

	void GetWindowSize( int &width, int &height ) const;

	// Creates/ destroys a child window
	bool AddView( void* hwnd );
	void RemoveView( void* hwnd );

	// Activates a view
	void SetView( void* hwnd );

	// Sets the default *dynamic* state
	void SetDefaultState( );

	// Returns the snapshot id for the shader state
	StateSnapshot_t	 TakeSnapshot( );

	// Returns true if the state snapshot is transparent
	bool IsTranslucent( StateSnapshot_t id ) const;
	bool IsAlphaTested( StateSnapshot_t id ) const;
	bool UsesVertexAndPixelShaders( StateSnapshot_t id ) const;
	bool UsesAmbientCube( StateSnapshot_t id ) const;

	// Gets the vertex format for a set of snapshot ids
	VertexFormat_t ComputeVertexFormat( int numSnapshots, StateSnapshot_t* pIds ) const;

	// Gets the vertex format for a set of snapshot ids
	VertexFormat_t ComputeVertexUsage( int numSnapshots, StateSnapshot_t* pIds ) const;

	// Begins a rendering pass that uses a state snapshot
	void BeginPass( StateSnapshot_t snapshot  );

	// Uses a state snapshot
	void UseSnapshot( StateSnapshot_t snapshot );

	// Use this to get the mesh builder that allows us to modify vertex data
	CMeshBuilder* GetVertexModifyBuilder();

	// Sets the color to modulate by
	void Color3f( float r, float g, float b );
	void Color3fv( float const* pColor );
	void Color4f( float r, float g, float b, float a );
	void Color4fv( float const* pColor );

	// Faster versions of color
	void Color3ub( unsigned char r, unsigned char g, unsigned char b );
	void Color3ubv( unsigned char const* rgb );
	void Color4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a );
	void Color4ubv( unsigned char const* rgba );

	// Sets the lights
	void SetLight( int lightNum, LightDesc_t& desc );
	void SetAmbientLight( float r, float g, float b );
	void SetAmbientLightCube( Vector4D cube[6] );

	// Get the lights
	int GetMaxLights( void ) const;
	const LightDesc_t& GetLight( int lightNum ) const;

	// Render state for the ambient light cube (vertex shaders)
	void SetVertexShaderStateAmbientLightCube();

	void SetSkinningMatrices();

	// Render state for the ambient light cube (pixel shaders)
	void BindAmbientLightCubeToStage0( );

	// Lightmap texture binding
	void BindLightmap( TextureStage_t stage );
	void BindLightmapAlpha( TextureStage_t stage )
	{
	}
	void BindBumpLightmap( TextureStage_t stage );
	void BindWhite( TextureStage_t stage );
	void BindBlack( TextureStage_t stage );
	void BindGrey( TextureStage_t stage );
	void BindSyncTexture( TextureStage_t stage, int texture );
	void BindFBTexture( TextureStage_t stage );
	void CopyRenderTargetToTexture( int texID );


	// Special system flat normal map binding.
	void BindFlatNormalMap( TextureStage_t stage );
	void BindNormalizationCubeMap( TextureStage_t stage );

	// Set the number of bone weights
	void SetNumBoneWeights( int numBones );

	// Flushes any primitives that are buffered
	void FlushBufferedPrimitives();

	// Creates/destroys Mesh
	IMesh* CreateStaticMesh( IMaterial* pMaterial, bool bForceTempMesh );
	IMesh* CreateStaticMesh( VertexFormat_t fmt, bool bSoftwareVertexShader );
	void DestroyStaticMesh( IMesh* mesh );

	// Gets the dynamic mesh; note that you've got to render the mesh
	// before calling this function a second time. Clients should *not*
	// call DestroyStaticMesh on the mesh returned by this call.
	IMesh* GetDynamicMesh( IMaterial* pMaterial, bool buffered, IMesh* pVertexOverride, IMesh* pIndexOverride );

	// Renders a single pass of a material
	void RenderPass( );

	// Page flip
	void SwapBuffers( );

	// stuff related to matrix stacks
	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 );

	void Viewport( int x, int y, int width, int height );
	void GetViewport( int& x, int& y, int& width, int& height ) const;

	// Fog methods...
	void FogMode( MaterialFogMode_t fogMode );
	void FogStart( float fStart );
	void FogEnd( float fEnd );
	void SetFogZ( float fogZ );
	float GetFogStart( void ) const;
	float GetFogEnd( void ) const;
	void FogColor3f( float r, float g, float b );
	void FogColor3fv( float const* rgb );
	void FogColor3ub( unsigned char r, unsigned char g, unsigned char b );
	void FogColor3ubv( unsigned char const* rgb );

	virtual void SceneFogColor3ub( unsigned char r, unsigned char g, unsigned char b );
	virtual void GetSceneFogColor( unsigned char *rgb );
	virtual void SceneFogMode( MaterialFogMode_t fogMode );
	virtual MaterialFogMode_t GetSceneFogMode( );

	void SetHeightClipZ( float z );
	void SetHeightClipMode( enum MaterialHeightClipMode_t heightClipMode );

	void SetClipPlane( int index, const float *pPlane );
	void EnableClipPlane( int index, bool bEnable );
	
	void DestroyVertexBuffers();

	// Sets the vertex and pixel shaders
	void SetVertexShaderIndex( int vshIndex );
	void SetPixelShaderIndex( int pshIndex );

	// Sets the constant register for vertex and pixel shaders
	void SetVertexShaderConstant( int var, float const* pVec, int numConst = 1, bool bForce = false );
	void SetPixelShaderConstant( int var, float const* pVec, int numConst = 1, bool bForce = false );

	// 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 );

	// Forces Z buffering on or off
	void OverrideDepthEnable( bool bEnable, bool bDepthEnable );

	// Sets the shade mode
	void ShadeMode( ShaderShadeMode_t mode );

	// Binds a particular material to render with
	void Bind( IMaterial* pMaterial );

	// Returns the nearest supported format
	ImageFormat GetNearestSupportedFormat( ImageFormat fmt ) const;
 	ImageFormat GetNearestRenderTargetFormat( ImageFormat fmt ) const;

	void ClearColor3ub( unsigned char r, unsigned char g, unsigned char b );
	void ClearColor4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a );

	// Sets the texture state
	void BindTexture( TextureStage_t stage, unsigned int textureNum );

	void SetRenderTarget( unsigned int textureNum, unsigned int ztexture );

	ImageFormat GetBackBufferFormat() const;
	void GetBackBufferDimensions( int& width, int& height ) const;

	// Indicates we're going to be modifying this texture
	// TexImage2D, TexSubImage2D, TexWrap, TexMinFilter, and TexMagFilter
	// all use the texture specified by this function.
	void ModifyTexture( unsigned int textureNum );

	// Texture management methods
	void TexImage2D( int level, int cubeFace, ImageFormat dstFormat, int width, int height, 
							 ImageFormat srcFormat, void *imageData );
	void TexSubImage2D( int level, int cubeFace, int xOffset, int yOffset, int width, int height,
							 ImageFormat srcFormat, int srcStride, void *imageData );

	bool TexLock( int level, int cubeFaceID, int xOffset, int yOffset, 
									int width, int height, CPixelWriter& writer );
	void TexUnlock( );
	
	// These are bound to the texture, not the texture environment
	void TexMinFilter( ShaderTexFilterMode_t texFilterMode );
	void TexMagFilter( ShaderTexFilterMode_t texFilterMode );
	void TexWrap( ShaderTexCoordComponent_t coord, ShaderTexWrapMode_t wrapMode );
	void TexSetPriority( int priority );	
	void SetLightmapTextureIdRange( int firstId, int lastId );

	void CreateTexture( unsigned int textureNum, int width, int height,
		ImageFormat dstImageFormat, int numMipLevels, int numCopies, int flags );
	void CreateDepthTexture( unsigned int textureNum, ImageFormat renderFormat, int width, int height );
	void DeleteTexture( unsigned int textureNum );
	bool IsTexture( unsigned int textureNum );
	bool IsTextureResident( unsigned int textureNum );

	// stuff that isn't to be used from within a shader
	void DepthRange( float zNear, float zFar );
	void ClearBuffers( bool bClearColor, bool bClearDepth, int renderTargetWidth, int renderTargetHeight );
	void ReadPixels( int x, int y, int width, int height, unsigned char *data, ImageFormat dstFormat );

	// Point size
	void PointSize( float size );

	// Selection mode methods
	int SelectionMode( bool selectionMode );
	void SelectionBuffer( unsigned int* pBuffer, int size );
	void ClearSelectionNames( );
	void LoadSelectionName( int name );
	void PushSelectionName( int name );
	void PopSelectionName();

	void FlushHardware();
	void ResetRenderState();

	// Can we download textures?
	virtual bool CanDownloadTextures() const;

	// Are we using graphics?
	virtual bool IsUsingGraphics() const;

	// Board-independent calls, here to unify how shaders set state
	// Implementations should chain back to IShaderUtil->BindTexture(), etc.

	// Use this to spew information about the 3D layer 
	void SpewDriverInfo() const;

	// Use this to begin and end the frame
	void BeginFrame();
	void EndFrame();

	// returns current time
	double CurrentTime() const;

	// Get the current camera position in world space.
	void GetWorldSpaceCameraPosition( float * pPos ) const;

	// Members of IMaterialSystemHardwareConfig
	bool HasAlphaBuffer() const;
	bool HasStencilBuffer() const;
	bool HasARBMultitexture() const;
	bool HasNVRegisterCombiners() const;
	bool HasTextureEnvCombine() const;
	int	 GetFrameBufferColorDepth() const;
	int  GetNumTextureUnits() const;
	bool HasIteratorsPerTextureUnit() const;
	bool HasSetDeviceGammaRamp() const;
	bool SupportsCompressedTextures() const;
	bool SupportsVertexAndPixelShaders() const;
	bool SupportsPixelShaders_1_4() const;
	bool SupportsPixelShaders_2_0() const;
	bool SupportsVertexShaders_2_0() const;
	bool SupportsPartialTextureDownload() const;
	bool SupportsStateSnapshotting() const;
	int  MaximumAnisotropicLevel() const;
	int  MaxTextureWidth() const;
	int  MaxTextureHeight() const;
	int  MaxTextureAspectRatio() const;
	int  GetDXSupportLevel() const;
	const char *GetShaderDLLName() const
	{
		return "UNKNOWN";
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -