#include "defines.h" #include "shader.h" #include "globals.h" D3DXMATRIX Shader::s_matVP; void RenderLoading( int iPercent ); void Shader::Init() { m_pVSShader= NULL; m_pPSShader= NULL; #ifdef EXTRACODE m_iRevision= 0; #endif // per default Texturen einfach lassen wie sie sind m_iUsedTextureStageCount= -1; for( int i= 0; i < m_iMaxTextureStageCount; ++i ) { m_iTextureIndices[ i ]= TI_IgnoreSetting; m_iTextureSettings[ i ]= TI_IgnoreSetting; } } void Shader::DeInit() { Release(); } void Shader::Release() { if( m_pVSShader != NULL ) { m_pVSShader->Release(); m_pVSShader= NULL; } if( m_pPSShader != NULL ) { m_pPSShader->Release(); m_pPSShader= NULL; } } void Shader::CompileVS(const char* pShaderCode, const char* pEntryPoint, IDirect3DVertexShader9 **ppCachedShader ) { #ifdef EXTRACODE std::string strCurrentCode( pShaderCode ); if( strCurrentCode == m_strVSCode ) { return; } m_strVSCode= strCurrentCode; #endif if(ppCachedShader && *ppCachedShader) { m_pVSShader = *ppCachedShader; return; } ID3DXBuffer* tmp= NULL; #if LAPTOPMODE == 1 Compile( pShaderCode, pEntryPoint, "vs_2_0", &tmp ); #else Compile( pShaderCode, pEntryPoint, "vs_3_0", &tmp ); #endif if( tmp == NULL ) { return; } g_d3d_device->CreateVertexShader( (DWORD*)tmp->GetBufferPointer(), &m_pVSShader ); tmp->Release(); #ifdef EXTRACODE m_iRevision++; #endif if(ppCachedShader) *ppCachedShader = m_pVSShader; RenderLoading( 1 ); } void Shader::CompilePS(const char* pShaderCode, const char* pEntryPoint ) { #ifdef EXTRACODE std::string strCurrentCode( pShaderCode ); if( strCurrentCode == m_strPSCode ) { return; } m_strPSCode= strCurrentCode; #endif ID3DXBuffer* tmp= NULL; #if LAPTOPMODE == 1 Compile( pShaderCode, pEntryPoint, "ps_2_0", &tmp ); #else Compile( pShaderCode, pEntryPoint, "ps_3_0", &tmp ); #endif if( tmp == NULL ) { return; } g_d3d_device->CreatePixelShader( (DWORD*)tmp->GetBufferPointer(), &m_pPSShader ); tmp->Release(); #ifdef EXTRACODE m_iRevision++; #endif RenderLoading( 1 ); } void Shader::Compile( const char* pShaderCode, const char* pEntryPoint, const char* ShaderModel, ID3DXBuffer** tmp ) { int iLength= 0; while(pShaderCode[ iLength ] != 0 ) { iLength++; } #ifndef _DEBUG D3DXCompileShader( pShaderCode, iLength, 0, 0, pEntryPoint, ShaderModel, D3DXSHADER_OPTIMIZATION_LEVEL3|D3DXSHADER_PREFER_FLOW_CONTROL, tmp, 0, 0 ); #else ID3DXBuffer *errors; if( D3DXCompileShader( pShaderCode, iLength, 0, 0, pEntryPoint, ShaderModel, D3DXSHADER_DEBUG*0+1*(D3DXSHADER_OPTIMIZATION_LEVEL3|D3DXSHADER_PREFER_FLOW_CONTROL), tmp, &errors, 0 ) <0 ) { OutputDebugString( "Error in D3DXCompileShader:\n" ); OutputDebugString( pShaderCode ); OutputDebugString( "\n" ); OutputDebugString( (char*)errors->GetBufferPointer() ); errors->Release(); //assert( false ); return; } OutputDebugString( "Shader compiled.\n" ); #endif } void Shader::Activate() { g_d3d_device->SetPixelShader( m_pPSShader ); g_d3d_device->SetVertexShader( m_pVSShader ); for( int i= 0; i <= m_iUsedTextureStageCount; ++i ) { if( m_iTextureIndices[ i ] != TI_IgnoreSetting ) { g_d3d_device->SetTexture( i, g_pTextures[ m_iTextureIndices[ i ] ].GetPointer() ); g_d3d_device->SetSamplerState( i, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP ); g_d3d_device->SetSamplerState( i, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP ); g_d3d_device->SetSamplerState( i, D3DSAMP_ADDRESSW, D3DTADDRESS_WRAP ); if( m_iTextureSettings[ i ] == TI_CLAMP ) { g_d3d_device->SetSamplerState( i, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP ); g_d3d_device->SetSamplerState( i, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP ); g_d3d_device->SetSamplerState( i, D3DSAMP_ADDRESSW, D3DTADDRESS_CLAMP ); } } } } void Shader::Deactivate() { g_d3d_device->SetPixelShader( NULL ); g_d3d_device->SetVertexShader( NULL ); } void Shader::SetTransform(const D3DXMATRIX &mWorld, const D3DXMATRIX &mWorldInverse) { /***********************************************************************************/ // Konstanten fuer Shader setzen D3DXMATRIX float4x4Mat; //[VS] float4x4 matWVP : register(c0); D3DXMatrixMultiply( &float4x4Mat, &mWorld, &s_matVP ); D3DXMatrixTranspose( &float4x4Mat, &float4x4Mat ); g_d3d_device->SetVertexShaderConstantF( 0, (float*)&float4x4Mat, 4 ); //[VS] float4x4 matWorld : register(c12); D3DXMatrixTranspose( &float4x4Mat, &mWorld ); g_d3d_device->SetVertexShaderConstantF( 12, (float*)&float4x4Mat, 4 ); //[VS] float4x4 matWorldI : register(c16); D3DXMatrixTranspose( &float4x4Mat, &mWorldInverse ); g_d3d_device->SetVertexShaderConstantF( 16, (float*)&float4x4Mat, 4 ); } void Shader::SetCamera(const D3DXMATRIX &mView, const D3DXMATRIX &mProj, const D3DXVECTOR3 &vCamPos, const D3DXVECTOR3 &vCamDir, D3DXMATRIX *pMatVP) { /***********************************************************************************/ // Konstanten fuer Shader setzen D3DXMATRIX float4x4Mat; float float4Vec[4]; //[VS] float4x4 matWVP : register(c0); D3DXMatrixMultiply( &s_matVP, &mView, &mProj ); D3DXMatrixTranspose( &float4x4Mat, &s_matVP ); g_d3d_device->SetVertexShaderConstantF( 0, (float*)&float4x4Mat, 4 ); // Output view projection matrix, if requested if(pMatVP) memcpy(pMatVP, &s_matVP, sizeof(D3DXMATRIX)); //[VS] float3x3 viewMatrixRotInv : register(c7); g_d3d_device->SetVertexShaderConstantF( 7, (float*)&mView, 3 ); //[VS] float4 projScaleOffsetInv : register(c11); float4Vec[0] = 1.0f / mProj._11; float4Vec[1] = 1.0f / mProj._22; float4Vec[2] = -(mProj._31 + mProj._41) * float4Vec[0]; float4Vec[3] = -(mProj._32 + mProj._42) * float4Vec[1]; g_d3d_device->SetVertexShaderConstantF( 11, float4Vec, 1 ); //[VS] float3 viewPos : register(c4); //[PS] float3 viewPos : register(c2); memcpy(float4Vec, &vCamPos, sizeof(float)*3); g_d3d_device->SetVertexShaderConstantF( 4, float4Vec, 1 ); g_d3d_device->SetPixelShaderConstantF( 2, float4Vec, 1 ); //[VS] float3 viewDir : register(c10); //[PS] float3 viewDir : register(c4); memcpy(float4Vec, &vCamDir, sizeof(float)*3); g_d3d_device->SetVertexShaderConstantF( 10, float4Vec, 1 ); g_d3d_device->SetPixelShaderConstantF( 4, float4Vec, 1 ); } void Shader::SetResolution(int iResX, int iResY) { /***********************************************************************************/ // Konstanten fuer Shader setzen float float4Vec[4]; //[VS] float2 res : register(c5); //[PS] float2 res : register(c0); float4Vec[0] = (float)iResX; float4Vec[1] = (float)iResY; float4Vec[2] = 1.f / float4Vec[0]; float4Vec[3] = 1.f / float4Vec[1]; g_d3d_device->SetVertexShaderConstantF( 5, float4Vec, 1 ); g_d3d_device->SetPixelShaderConstantF( 0, float4Vec, 1 ); } void Shader::SetConstants(bool bHDR) { /***********************************************************************************/ // Konstanten fuer Shader setzen float float4Vec[4]; // Set screen resoluton SetResolution(d3dpp.BackBufferWidth, d3dpp.BackBufferHeight); //[VS] float3 lightDir : register(c6); //[PS] float3 lightDir : register(c1); memcpy(float4Vec, &g_LightDir, sizeof(float)*3); g_d3d_device->SetVertexShaderConstantF( 6, float4Vec, 1 ); g_d3d_device->SetPixelShaderConstantF( 1, float4Vec, 1 ); //[PS] float demoTime : register(c3); float4Vec[0]= 64.0f * (float)g_Camera.m_iDemoTime / (float)g_iCamTickFactor; float4Vec[1]= g_Camera.m_fDOF * g_Camera.m_fDOF; float4Vec[2]= g_fCamTime;//g_fClouds; float4Vec[3]= g_fShaderBeat;//g_fPlasmaGrid; g_d3d_device->SetPixelShaderConstantF( 3, float4Vec, 1 ); //[PS] float4 glow : register(c61); { float4Vec[0] = g_fGlow; float4Vec[1] = g_fGlow; float4Vec[2] = (g_Camera.m_iScene == g_CubeMapCamera.m_iScene) ? 1.0f : 0.0f; float4Vec[3] = bHDR ? 1.0f : 0.0f; g_d3d_device->SetPixelShaderConstantF( 61, float4Vec, 1 ); } } void Shader::SetTextureStageSettings( int iTextureStage, int iTextureIndex, int iTextureSettings ) { assert( iTextureStage < m_iMaxTextureStageCount ); if( iTextureStage > m_iUsedTextureStageCount ) { m_iUsedTextureStageCount= iTextureStage; } m_iTextureIndices[ iTextureStage ]= iTextureIndex; m_iTextureSettings[ iTextureStage ]= iTextureSettings; }