GFXGLShader::~GFXGLShader() { clearShaders(); for(HandleMap::Iterator i = mHandles.begin(); i != mHandles.end(); i++) delete i->value; delete[] mConstBuffer; }
ShaderGroup::~ShaderGroup() { clearShaders(); }
void GFXGLShader::zombify() { clearShaders(); dMemset(mConstBuffer, 0, mConstBufferSize); }
bool GFXGLShader::_init() { // Don't initialize empty shaders. if ( mVertexFile.isEmpty() && mPixelFile.isEmpty() ) return false; clearShaders(); mProgram = glCreateProgram(); // Set the macros and add the global ones. Vector<GFXShaderMacro> macros; macros.merge( mMacros ); macros.merge( smGlobalMacros ); // Add the shader version to the macros. const U32 mjVer = (U32)mFloor( mPixVersion ); const U32 mnVer = (U32)( ( mPixVersion - F32( mjVer ) ) * 10.01f ); macros.increment(); macros.last().name = "TORQUE_SM"; macros.last().value = String::ToString( mjVer * 10 + mnVer ); // Default to true so we're "successful" if a vertex/pixel shader wasn't specified. bool compiledVertexShader = true; bool compiledPixelShader = true; // Compile the vertex and pixel shaders if specified. if(!mVertexFile.isEmpty()) compiledVertexShader = initShader(mVertexFile, true, macros); if(!mPixelFile.isEmpty()) compiledPixelShader = initShader(mPixelFile, false, macros); // If either shader was present and failed to compile, bail. if(!compiledVertexShader || !compiledPixelShader) return false; // Link it! glLinkProgram( mProgram ); GLint linkStatus; glGetProgramiv( mProgram, GL_LINK_STATUS, &linkStatus ); // Dump the info log to the console U32 logLength = 0; glGetProgramiv(mProgram, GL_INFO_LOG_LENGTH, (GLint*)&logLength); if ( logLength ) { FrameAllocatorMarker fam; char* log = (char*)fam.alloc( logLength ); glGetProgramInfoLog( mProgram, logLength, NULL, log ); if ( linkStatus == GL_FALSE ) { if ( smLogErrors ) { Con::errorf( "GFXGLShader::init - Error linking shader!" ); Con::errorf( "Program %s / %s: %s", mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str(), log); } } else if ( smLogWarnings ) { Con::warnf( "Program %s / %s: %s", mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str(), log); } } // If we failed to link, bail. if ( linkStatus == GL_FALSE ) return false; initConstantDescs(); initHandles(); // Notify Buffers we might have changed in size. // If this was our first init then we won't have any activeBuffers // to worry about unnecessarily calling. Vector<GFXShaderConstBuffer*>::iterator biter = mActiveBuffers.begin(); for ( ; biter != mActiveBuffers.end(); biter++ ) ((GFXGLShaderConstBuffer*)(*biter))->onShaderReload( this ); return true; }
bool GFXGLShader::_init() { PROFILE_SCOPE(GFXGLShader_Init); // Don't initialize empty shaders. if ( mVertexFile.isEmpty() && mPixelFile.isEmpty() ) return false; clearShaders(); mProgram = glCreateProgram(); // Set the macros and add the global ones. Vector<GFXShaderMacro> macros; macros.merge( mMacros ); macros.merge( smGlobalMacros ); // Add the shader version to the macros. const U32 mjVer = (U32)mFloor( mPixVersion ); const U32 mnVer = (U32)( ( mPixVersion - F32( mjVer ) ) * 10.01f ); macros.increment(); macros.last().name = "TORQUE_SM"; macros.last().value = String::ToString( mjVer * 10 + mnVer ); macros.increment(); macros.last().name = "TORQUE_VERTEX_SHADER"; macros.last().value = ""; // Default to true so we're "successful" if a vertex/pixel shader wasn't specified. bool compiledVertexShader = true; bool compiledPixelShader = true; // Compile the vertex and pixel shaders if specified. if(!mVertexFile.isEmpty()) compiledVertexShader = initShader(mVertexFile, true, macros); macros.last().name = "TORQUE_PIXEL_SHADER"; if(!mPixelFile.isEmpty()) compiledPixelShader = initShader(mPixelFile, false, macros); // If either shader was present and failed to compile, bail. if(!compiledVertexShader || !compiledPixelShader) return false; //bind vertex attributes glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_Position, "vPosition"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_Normal, "vNormal"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_Color, "vColor"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_Tangent, "vTangent"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TangentW, "vTangentW"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_Binormal, "vBinormal"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord0, "vTexCoord0"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord1, "vTexCoord1"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord2, "vTexCoord2"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord3, "vTexCoord3"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord4, "vTexCoord4"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord5, "vTexCoord5"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord6, "vTexCoord6"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord7, "vTexCoord7"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord8, "vTexCoord8"); glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord9, "vTexCoord9"); //bind fragment out color glBindFragDataLocation(mProgram, 0, "OUT_col"); glBindFragDataLocation(mProgram, 1, "OUT_col1"); glBindFragDataLocation(mProgram, 2, "OUT_col2"); glBindFragDataLocation(mProgram, 3, "OUT_col3"); // Link it! glLinkProgram( mProgram ); GLint linkStatus; glGetProgramiv( mProgram, GL_LINK_STATUS, &linkStatus ); // Dump the info log to the console U32 logLength = 0; glGetProgramiv(mProgram, GL_INFO_LOG_LENGTH, (GLint*)&logLength); if ( logLength ) { FrameAllocatorMarker fam; char* log = (char*)fam.alloc( logLength ); glGetProgramInfoLog( mProgram, logLength, NULL, log ); if ( linkStatus == GL_FALSE ) { if ( smLogErrors ) { Con::errorf( "GFXGLShader::init - Error linking shader!" ); Con::errorf( "Program %s / %s: %s", mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str(), log); } } else if ( smLogWarnings ) { Con::warnf( "Program %s / %s: %s", mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str(), log); } } // If we failed to link, bail. if ( linkStatus == GL_FALSE ) return false; initConstantDescs(); initHandles(); // Notify Buffers we might have changed in size. // If this was our first init then we won't have any activeBuffers // to worry about unnecessarily calling. Vector<GFXShaderConstBuffer*>::iterator biter = mActiveBuffers.begin(); for ( ; biter != mActiveBuffers.end(); biter++ ) ((GFXGLShaderConstBuffer*)(*biter))->onShaderReload( this ); return true; }
liqShaderFactory::~liqShaderFactory() { clearShaders(); }