void CBaseShader::SetFixedFunctionTextureScale( MaterialMatrixMode_t textureTransform, int scaleVar ) { Assert( !IsSnapshotting() ); // handle scrolling of base texture Vector2D vScale; s_ppParams[scaleVar]->GetVecValue( vScale.Base(), 2 ); if( vScale[0] != 0.0f || vScale[1] != 0.0f ) { s_pShaderAPI->MatrixMode( textureTransform ); // only do the upper 3x3 since this is a 2D matrix float mat[16]; mat[0] = vScale[0]; mat[1] = 0.0f; mat[2] = 0.0f; mat[4] = 0.0f; mat[5] = vScale[1]; mat[6] = 0.0f; mat[8] = 0.0f; mat[9] = 0.0f; mat[10] = 1.0f; // Better set the stuff we don't set with some sort of value! mat[3] = mat[7] = mat[11] = 0; mat[12] = mat[13] = mat[14] = 0; mat[15] = 1; s_pShaderAPI->LoadMatrix( mat ); } else { LoadIdentity( textureTransform ); } }
void CBaseShader::DrawElements( IMaterialVar **ppParams, int nModulationFlags, IShaderShadow* pShaderShadow, IShaderDynamicAPI* pShaderAPI ) { // Re-entrancy check Assert( !s_ppParams ); s_ppParams = ppParams; s_pShaderAPI = pShaderAPI; s_pShaderShadow = pShaderShadow; s_nModulationFlags = nModulationFlags; s_pMeshBuilder = pShaderAPI ? pShaderAPI->GetVertexModifyBuilder() : NULL; if ( IsSnapshotting() ) { // Set up the shadow state SetInitialShadowState( ); } OnDrawElements( ppParams, pShaderShadow, pShaderAPI ); s_nModulationFlags = 0; s_ppParams = NULL; s_pShaderAPI = NULL; s_pShaderShadow = NULL; s_pMeshBuilder = NULL; }
//----------------------------------------------------------------------------- // Sets up ambient light cube... //----------------------------------------------------------------------------- void CBaseShader::SetAmbientCubeDynamicStateFixedFunction( ) { Assert( !IsSnapshotting() ); s_pShaderAPI->BindAmbientLightCubeToStage0( ); LoadCameraToWorldTransform( MATERIAL_TEXTURE0 ); }
void CBaseShader::SetFixedFunctionTextureTransform( MaterialMatrixMode_t textureTransform, int transformVar ) { Assert( !IsSnapshotting() ); IMaterialVar* pTransformationVar = s_ppParams[transformVar]; if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX)) { s_pShaderAPI->MatrixMode( textureTransform ); const VMatrix &transformation = pTransformationVar->GetMatrixValue(); // only do the upper 3x3 since this is a 2D matrix float mat[16]; mat[0] = transformation[0][0]; mat[1] = transformation[1][0]; mat[2] = transformation[3][0]; mat[4] = transformation[0][1]; mat[5] = transformation[1][1]; mat[6] = transformation[3][1]; mat[8] = transformation[0][3]; mat[9] = transformation[1][3]; mat[10] = transformation[3][3]; // Better set the stuff we don't set with some sort of value! mat[3] = mat[7] = mat[11] = 0; mat[12] = mat[13] = mat[14] = 0; mat[15] = 1; s_pShaderAPI->LoadMatrix( mat ); } else { LoadIdentity( textureTransform ); } }
//----------------------------------------------------------------------------- // Masked environment map //----------------------------------------------------------------------------- void CBaseShader::FixedFunctionMaskedEnvmapPass( int envMapVar, int envMapMaskVar, int baseTextureVar, int envMapFrameVar, int envMapMaskFrameVar, int frameVar, int maskOffsetVar, int maskScaleVar, int envMapTintVar ) { // IMaterialVar** params = ShaderState().m_ppParams; if (IsSnapshotting()) { // Alpha blending SetDefaultBlendingShadowState( envMapMaskVar, false ); // Disable overbright s_pShaderShadow->OverbrightValue( SHADER_TEXTURE_STAGE0, 1.0f ); s_pShaderShadow->OverbrightValue( SHADER_TEXTURE_STAGE1, 1.0f ); int flags = SetShadowEnvMappingState( envMapMaskVar, envMapTintVar ); s_pShaderShadow->DrawFlags( flags ); Draw(); s_pShaderShadow->EnableCustomPixelPipe( false ); s_pShaderShadow->EnableAlphaPipe( false ); } else { SetDynamicEnvMappingState( envMapVar, envMapMaskVar, baseTextureVar, envMapFrameVar, envMapMaskFrameVar, frameVar, maskOffsetVar, maskScaleVar, envMapTintVar ); DefaultFog(); Draw(); } }
void CBaseVSShader::SetPixelShaderConstantGammaToLinear( int pixelReg, int constantVar, int constantVar2 ) { Assert( !IsSnapshotting() ); if ((!s_ppParams) || (constantVar == -1) || (constantVar2 == -1)) return; IMaterialVar* pPixelVar = s_ppParams[constantVar]; Assert( pPixelVar ); IMaterialVar* pPixelVar2 = s_ppParams[constantVar2]; Assert( pPixelVar2 ); float val[4]; if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) { pPixelVar->GetVecValue( val, 3 ); } else { val[0] = val[1] = val[2] = pPixelVar->GetFloatValue(); } val[3] = pPixelVar2->GetFloatValue(); val[0] = val[0] > 1.0f ? val[0] : GammaToLinear( val[0] ); val[1] = val[1] > 1.0f ? val[1] : GammaToLinear( val[1] ); val[2] = val[2] > 1.0f ? val[2] : GammaToLinear( val[2] ); s_pShaderAPI->SetPixelShaderConstant( pixelReg, val ); }
void CBaseShader::DrawElements( IMaterialVar **ppParams, int nModulationFlags, IShaderShadow* pShaderShadow, IShaderDynamicAPI* pShaderAPI, VertexCompressionType_t vertexCompression, CBasePerMaterialContextData **pContextDataPtr, CBasePerInstanceContextData** pInstanceDataPtr ) { VPROF("CBaseShader::DrawElements"); // Re-entrancy check Assert( !s_ppParams ); s_ppParams = ppParams; s_pShaderAPI = pShaderAPI; s_pShaderShadow = pShaderShadow; s_nModulationFlags = nModulationFlags; s_pInstanceDataPtr = (CPerInstanceContextData**)( pInstanceDataPtr ); s_nPassCount = 0; if ( IsSnapshotting() ) { // Set up the shadow state SetInitialShadowState( ); } OnDrawElements( ppParams, pShaderShadow, pShaderAPI, vertexCompression, pContextDataPtr ); s_pInstanceDataPtr = NULL; s_nPassCount = 0; s_nModulationFlags = 0; s_ppParams = NULL; s_pShaderAPI = NULL; s_pShaderShadow = NULL; }
//----------------------------------------------------------------------------- // // Helper methods for alpha blending.... // //----------------------------------------------------------------------------- void CBaseShader::EnableAlphaBlending( ShaderBlendFactor_t src, ShaderBlendFactor_t dst ) { Assert( IsSnapshotting() ); s_pShaderShadow->EnableBlending( true ); s_pShaderShadow->BlendFunc( src, dst ); s_pShaderShadow->EnableDepthWrites(false); }
//----------------------------------------------------------------------------- // Sets lightmap blending mode for single texturing //----------------------------------------------------------------------------- void CBaseShader::SingleTextureLightmapBlendMode( ) { Assert( IsSnapshotting() ); s_pShaderShadow->EnableBlending( true ); s_pShaderShadow->BlendFunc( SHADER_BLEND_DST_COLOR, SHADER_BLEND_SRC_COLOR ); }
//----------------------------------------------------------------------------- // Sets the color + transparency //----------------------------------------------------------------------------- void CBaseShader::SetColorState( int colorVar, bool setAlpha ) { Assert( !IsSnapshotting() ); if ( !s_ppParams ) return; // Use tint instead of color if it was specified... IMaterialVar* pColorVar = (colorVar != -1) ? s_ppParams[colorVar] : 0; float color[4] = { 1.0, 1.0, 1.0, 1.0 }; if (pColorVar) { if (pColorVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) { pColorVar->GetVecValue( color, 3 ); } else { color[0] = color[1] = color[2] = pColorVar->GetFloatValue(); } // Clamp... color[0] = clamp( color[0], 0.0f, 1.0f ); color[1] = clamp( color[1], 0.0f, 1.0f ); color[2] = clamp( color[2], 0.0f, 1.0f ); } color[3] = setAlpha ? GetAlpha() : 1.0f; s_pShaderAPI->Color4fv( color ); }
//----------------------------------------------------------------------------- // Loads the identity transform into a matrix //----------------------------------------------------------------------------- void CBaseShader::LoadIdentity( MaterialMatrixMode_t matrixMode ) { Assert( !IsSnapshotting() ); s_pShaderAPI->MatrixMode( matrixMode ); s_pShaderAPI->LoadIdentity( ); }
void CBaseShader::BindVertexTexture( VertexTextureSampler_t vtSampler, int nTextureVar, int nFrame /* = 0 */) { Assert( !IsSnapshotting() ); IMaterialVar* pTextureVar = s_ppParams[nTextureVar]; if ( !pTextureVar ) return; GetShaderSystem()->BindVertexTexture( vtSampler, pTextureVar->GetTextureValue() ); }
bool CBaseShader::UsingEditor( IMaterialVar **params ) const { if( IsSnapshotting() ) { return CShader_IsFlag2Set( params, MATERIAL_VAR2_USE_EDITOR ); } else { return s_pShaderAPI->InEditorMode(); } }
bool CBaseShader::UsingFlashlight( IMaterialVar **params ) const { if( IsSnapshotting() ) { return CShader_IsFlag2Set( params, MATERIAL_VAR2_USE_FLASHLIGHT ); } else { return s_pShaderAPI->InFlashlightMode(); } }
ShaderAPITextureHandle_t CBaseShader::GetShaderAPITextureBindHandle( int nTextureVar, int nFrameVar, int nTextureChannel ) { Assert( !IsSnapshotting() ); Assert( nTextureVar != -1 ); Assert ( s_ppParams ); IMaterialVar* pTextureVar = s_ppParams[nTextureVar]; IMaterialVar* pFrameVar = (nFrameVar != -1) ? s_ppParams[nFrameVar] : NULL; int nFrame = pFrameVar ? pFrameVar->GetIntValue() : 0; return GetShaderSystem()->GetShaderAPITextureBindHandle( pTextureVar->GetTextureValue(), nFrame, nTextureChannel ); }
//----------------------------------------------------------------------------- // Methods related to building per-instance command buffers //----------------------------------------------------------------------------- void CBaseShader::PI_BeginCommandBuffer() { // NOTE: This assertion is here because the memory allocation strategy // is perhaps not the best if this is used in dynamic states; we should // rethink in that case. Assert( IsSnapshotting() ); Assert( !s_bBuildingInstanceCommandBuffer ); s_bBuildingInstanceCommandBuffer = true; s_InstanceCommandBuffer.Reset(); }
void CBaseShader::FogToFogColor( void ) { Assert( IsSnapshotting() ); if (( CurrentMaterialVarFlags() & MATERIAL_VAR_NOFOG ) == 0) { s_pShaderShadow->FogMode( SHADER_FOGMODE_FOGCOLOR ); } else { s_pShaderShadow->FogMode( SHADER_FOGMODE_DISABLED ); } }
//----------------------------------------------------------------------------- // // Helper methods for fog // //----------------------------------------------------------------------------- void CBaseShader::FogToOOOverbright( void ) { Assert( IsSnapshotting() ); if (( CurrentMaterialVarFlags() & MATERIAL_VAR_NOFOG ) == 0) { s_pShaderShadow->FogMode( SHADER_FOGMODE_OO_OVERBRIGHT ); } else { s_pShaderShadow->FogMode( SHADER_FOGMODE_DISABLED ); } }
void CBaseShader::FogToGrey( void ) { Assert( !IsSnapshotting() ); if (( CurrentMaterialVarFlags() & MATERIAL_VAR_NOFOG ) == 0) { s_pShaderAPI->FogMode( s_pShaderAPI->GetSceneFogMode() ); s_pShaderAPI->FogColor3ub( 127, 127, 127 ); } else { s_pShaderAPI->FogMode( MATERIAL_FOG_NONE ); } }
void CBaseShader::BindTexture( Sampler_t sampler1, Sampler_t sampler2, ITexture *pTexture, int nFrame /* = 0 */ ) { Assert( !IsSnapshotting() ); if ( sampler2 == -1 ) { GetShaderSystem()->BindTexture( sampler1, pTexture, nFrame ); } else { GetShaderSystem()->BindTexture( sampler1, sampler2, pTexture, nFrame ); } }
//----------------------------------------------------------------------------- // Binds a texture //----------------------------------------------------------------------------- void CBaseShader::BindTexture( TextureStage_t stage, int nTextureVar, int nFrameVar ) { Assert( !IsSnapshotting() ); Assert( nTextureVar != -1 ); Assert ( s_ppParams ); IMaterialVar* pTextureVar = s_ppParams[nTextureVar]; IMaterialVar* pFrameVar = (nFrameVar != -1) ? s_ppParams[nFrameVar] : NULL; if (pTextureVar) { int nFrame = pFrameVar ? pFrameVar->GetIntValue() : 0; GetShaderSystem()->BindTexture( stage, pTextureVar->GetTextureValue(), nFrame ); } }
//----------------------------------------------------------------------------- // Sets lightmap blending mode for single texturing //----------------------------------------------------------------------------- void CBaseShader::SingleTextureLightmapBlendMode( ) { Assert( IsSnapshotting() ); s_pShaderShadow->EnableBlending( true ); if( g_pConfig->overbright == 2.0f ) { s_pShaderShadow->BlendFunc( SHADER_BLEND_DST_COLOR, SHADER_BLEND_SRC_COLOR ); } else { s_pShaderShadow->BlendFunc( SHADER_BLEND_DST_COLOR, SHADER_BLEND_ZERO ); } }
//----------------------------------------------------------------------------- // Fixed function Self illumination pass //----------------------------------------------------------------------------- void CBaseShader::FixedFunctionSelfIlluminationPass( TextureStage_t stage, int baseTextureVar, int frameVar, int baseTextureTransformVar, int selfIllumTintVar ) { // IMaterialVar** params = s_ppParams; if (IsSnapshotting()) { // A little setup for self illum here... SetModulationShadowState( selfIllumTintVar ); s_pShaderShadow->EnableTexGen( SHADER_TEXTURE_STAGE0, false ); s_pShaderShadow->EnableTexGen( SHADER_TEXTURE_STAGE1, false ); s_pShaderShadow->EnableAlphaTest( false ); s_pShaderShadow->EnableTexture( SHADER_TEXTURE_STAGE0, false ); s_pShaderShadow->EnableTexture( SHADER_TEXTURE_STAGE1, false ); s_pShaderShadow->EnableTexture( stage, true ); // No overbrighting s_pShaderShadow->OverbrightValue( SHADER_TEXTURE_STAGE0, 1.0f ); s_pShaderShadow->OverbrightValue( SHADER_TEXTURE_STAGE1, 1.0f ); // Don't bother with z writes here... s_pShaderShadow->EnableDepthWrites( false ); // We're always blending EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA ); int flags = SHADER_DRAW_POSITION; if (stage == SHADER_TEXTURE_STAGE0) flags |= SHADER_DRAW_TEXCOORD0; else flags |= SHADER_DRAW_TEXCOORD1; s_pShaderShadow->DrawFlags( flags ); } else { SetFixedFunctionTextureTransform( (stage == SHADER_TEXTURE_STAGE0) ? MATERIAL_TEXTURE0 : MATERIAL_TEXTURE1, baseTextureTransformVar ); BindTexture( stage, baseTextureVar, frameVar ); // NOTE: Texture + texture offset are set from BaseTimesLightmap SetModulationDynamicState( selfIllumTintVar ); FogToFogColor(); } Draw(); }
void CBaseShader::FogToFogColor( void ) { Assert( !IsSnapshotting() ); if (( CurrentMaterialVarFlags() & MATERIAL_VAR_NOFOG ) == 0) { s_pShaderAPI->FogMode( s_pShaderAPI->GetSceneFogMode() ); unsigned char fogColor[3]; s_pShaderAPI->GetSceneFogColor( fogColor ); s_pShaderAPI->FogColor3ubv( fogColor ); } else { s_pShaderAPI->FogMode( MATERIAL_FOG_NONE ); } }
void CBaseVSShader::SetVertexShaderConstant( int vertexReg, int constantVar ) { Assert( !IsSnapshotting() ); if ((!s_ppParams) || (constantVar == -1)) return; IMaterialVar* pVertexVar = s_ppParams[constantVar]; Assert( pVertexVar ); float val[4]; if (pVertexVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) pVertexVar->GetVecValue( val, 4 ); else val[0] = val[1] = val[2] = val[3] = pVertexVar->GetFloatValue(); s_pShaderAPI->SetVertexShaderConstant( vertexReg, val ); }
void DrawDecal( IMaterialVar **params, IShaderDynamicAPI *pShaderAPI, IShaderShadow *pShaderShadow ) { if( IsSnapshotting() ) { // Be sure not to write to dest alpha pShaderShadow->EnableAlphaWrites( false ); pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); pShaderShadow->EnableTexture( SHADER_SAMPLER2, true ); pShaderShadow->EnableTexture( SHADER_SAMPLER3, true ); SetNormalBlendingShadowState( BASETEXTURE, true ); int pTexCoords[3] = { 2, 2, 1 }; pShaderShadow->VertexShaderVertexFormat( VERTEX_POSITION | VERTEX_COLOR, 3, pTexCoords, 0 ); sdk_lightmappedgeneric_decal_Static_Index vshIndex; pShaderShadow->SetVertexShader( "SDK_LightmappedGeneric_Decal", vshIndex.GetIndex() ); pShaderShadow->SetPixelShader( "SDK_LightmappedGeneric_Decal" ); FogToFogColor(); } else { BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME ); // Load the z^2 components of the lightmap coordinate axes only // This is (N dot basis)^2 Vector vecZValues( g_localBumpBasis[0].z, g_localBumpBasis[1].z, g_localBumpBasis[2].z ); vecZValues *= vecZValues; Vector4D basis[3]; basis[0].Init( vecZValues.x, vecZValues.x, vecZValues.x, 0.0f ); basis[1].Init( vecZValues.y, vecZValues.y, vecZValues.y, 0.0f ); basis[2].Init( vecZValues.z, vecZValues.z, vecZValues.z, 0.0f ); pShaderAPI->SetPixelShaderConstant( 0, (float*)basis, 3 ); pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_LIGHTMAP_BUMPED ); SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM ); SetModulationPixelShaderDynamicState( 3 ); sdk_lightmappedgeneric_decal_Dynamic_Index vshIndex; vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z ); pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() ); } Draw(); }
void CBaseVSShader::SetPixelShaderConstant_W( int pixelReg, int constantVar, float fWValue ) { Assert( !IsSnapshotting() ); if ((!s_ppParams) || (constantVar == -1)) return; IMaterialVar* pPixelVar = s_ppParams[constantVar]; Assert( pPixelVar ); float val[4]; if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR) pPixelVar->GetVecValue( val, 4 ); else val[0] = val[1] = val[2] = val[3] = pPixelVar->GetFloatValue(); val[3]=fWValue; s_pShaderAPI->SetPixelShaderConstant( pixelReg, val ); }
//----------------------------------------------------------------------------- // Draws a snapshot //----------------------------------------------------------------------------- void CBaseShader::Draw( ) { if ( IsSnapshotting() ) { // Turn off transparency if we're asked to.... if (g_pConfig->bNoTransparency && ((s_ppParams[FLAGS]->GetIntValue() & MATERIAL_VAR_NO_DEBUG_OVERRIDE) == 0)) { s_pShaderShadow->EnableDepthWrites( true ); s_pShaderShadow->EnableBlending( false ); } GetShaderSystem()->TakeSnapshot(); } else { GetShaderSystem()->DrawSnapshot(); } }
// FIXME: Figure out a better way to do this? //----------------------------------------------------------------------------- int CBaseShader::ComputeModulationFlags( IMaterialVar** params, IShaderDynamicAPI* pShaderAPI ) { s_pShaderAPI = pShaderAPI; int mod = 0; if( UsingFlashlight(params) ) { mod |= SHADER_USING_FLASHLIGHT; } if ( UsingEditor(params) ) { mod |= SHADER_USING_EDITOR; } if( IS_FLAG2_SET( MATERIAL_VAR2_USE_FIXED_FUNCTION_BAKED_LIGHTING ) ) { AssertOnce( IS_FLAG2_SET( MATERIAL_VAR2_NEEDS_BAKED_LIGHTING_SNAPSHOTS ) ); if( IS_FLAG2_SET( MATERIAL_VAR2_NEEDS_BAKED_LIGHTING_SNAPSHOTS ) ) { mod |= SHADER_USING_FIXED_FUNCTION_BAKED_LIGHTING; } } if ( IsSnapshotting() ) { if ( IS_FLAG2_SET( MATERIAL_VAR2_USE_GBUFFER0 ) ) mod |= SHADER_USING_GBUFFER0; if ( IS_FLAG2_SET( MATERIAL_VAR2_USE_GBUFFER1 ) ) mod |= SHADER_USING_GBUFFER1; } else { int nFixedLightingMode = pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_ENABLE_FIXED_LIGHTING ); if ( nFixedLightingMode & 1 ) mod |= SHADER_USING_GBUFFER0; if ( nFixedLightingMode & 2 ) mod |= SHADER_USING_GBUFFER1; } s_pShaderAPI = NULL; return mod; }
void CBaseShader::SetDynamicEnvMappingState( int envMapVar, int envMapMaskVar, int baseTextureVar, int envMapFrameVar, int envMapMaskFrameVar, int frameVar, int maskOffsetVar, int maskScaleVar, int tintVar ) { Assert( !IsSnapshotting() ); IMaterialVar** params = s_ppParams; int varFlags = params[FLAGS]->GetIntValue(); if( (varFlags & MATERIAL_VAR_ENVMAPSPHERE) == 0 ) { if ( (varFlags & MATERIAL_VAR_ENVMAPCAMERASPACE) == 0 ) { LoadCameraToWorldTransform( MATERIAL_TEXTURE0 ); } else { LoadIdentity( MATERIAL_TEXTURE0 ); } } else { LoadCameraSpaceSphereMapTransform( MATERIAL_TEXTURE0 ); } BindTexture( SHADER_TEXTURE_STAGE0, envMapVar, envMapFrameVar ); if (params[envMapMaskVar]->IsDefined()) { SetFixedFunctionTextureScaledTransform( MATERIAL_TEXTURE1, maskOffsetVar, maskScaleVar ); BindTexture( SHADER_TEXTURE_STAGE1, envMapMaskVar, envMapMaskFrameVar ); } else if (varFlags & MATERIAL_VAR_BASEALPHAENVMAPMASK) { SetFixedFunctionTextureScaledTransform( MATERIAL_TEXTURE1, maskOffsetVar, maskScaleVar ); BindTexture( SHADER_TEXTURE_STAGE1, baseTextureVar, frameVar ); } SetModulationDynamicState( tintVar ); }