bool CGFXShader::LoadFromFile(const char* szFile) { HRESULT hr = S_OK; char buffer[128]; m_pEffect = cgCreateEffectFromFile( m_pCG, szFile, NULL ); CGerror error; const char *string = cgGetLastErrorString(&error); static char szError[1024]; if( error != CG_NO_ERROR ) { if( error == CG_COMPILER_ERROR ) { sprintf(szError, "Program: %s\n" "Error: %s\n\n" "Cg compiler output...\n%s", szFile, string, cgGetLastListing(m_pCG)); } else { sprintf(szError, "Program: %s\n" "Error: %s", szFile, string); } OutputDebugStringA(szError); OutputDebugStringA("\n"); } if(m_pEffect == NULL) { return false; } CGtechnique pTechnique = cgGetFirstTechnique( m_pEffect ); while( pTechnique && cgValidateTechnique( pTechnique ) == CG_FALSE ) { pTechnique = cgGetNextTechnique( pTechnique ); } if( pTechnique == NULL) { return false; } m_pTechnique = pTechnique; return true; }
bool cEffect::Init(const std::string &lacNameId, const std::string &lacFile) { //Initialization of the class atributes macFile = lacFile; macLastTechnique = ""; mEffect = NULL; mTechnique = NULL; mCurrentPass = NULL; mbLoaded = false; //Loading of the effect CGcontext lCGContext = cEffectManager::Get().GetCGContext(); mEffect = cgCreateEffectFromFile(lCGContext, lacFile.c_str(), NULL); if (!mEffect) { OutputDebugString("Unable to create effect\n"); const char *lacListing = cgGetLastListing(lCGContext); if (lacListing) { char lacBuffer[512]; sprintf(lacBuffer, "%s\n", lacListing); OutputDebugString(lacBuffer); } return false; } // Check if the techniques are valid CGtechnique lTechnique = cgGetFirstTechnique(mEffect); while (lTechnique) { if (cgValidateTechnique(lTechnique) == CG_FALSE) { char lacBuffer[512]; sprintf(lacBuffer, "Technique %s did not validate. Skipping. \n", cgGetTechniqueName(lTechnique)); OutputDebugString( lacBuffer ); const char *lacListing = cgGetLastListing(lCGContext); if (lacListing) { char lacBuffer[512]; sprintf(lacBuffer, "%s\n", lacListing); OutputDebugString(lacBuffer); } } lTechnique = cgGetNextTechnique(lTechnique); } mbLoaded = true; return true; }
void cgfxTechnique::validate() const { fValid = (cgValidateTechnique(fTechnique) == CG_TRUE); if (fValid) { fErrorString = ""; } else { CGerror error = cgGetError(); if (error != CG_NO_ERROR) { fErrorString = cgGetErrorString(cgGetError()); } fErrorString += "\nCg compilation errors for technique \""; fErrorString += fName; fErrorString += "\":\n"; fErrorString += cgGetLastListing(cgfxShaderNode::sCgContext); fErrorString += "\n"; } }
void Effect::load(const char* code) { effect.reset(cgCreateEffect(context, code, 0), EffectDeleter()); if (!effect) { throw std::runtime_error("Could not create effect"); } technique = cgGetNamedTechnique(effect.get(), "High"); if (technique == 0) { throw std::runtime_error("Technique not found"); } if (cgValidateTechnique(technique) == false) { throw std::runtime_error("Technique not valid"); } }
bool CgFXTechnique::validate(CgFXMaterial *pMat, DrawEnv *pEnv) { if(0x00 == (_uiValidationState & ValidationTried)) { #if 0 fprintf(stderr, "Validate %p\n", _pCGTechnique); #endif _uiValidationState = 0x03; #if 0 if(pWin != NULL) { pWin->activate(); } #endif if(_pCGTechnique == NULL || cgValidateTechnique(_pCGTechnique) == CG_FALSE ) { CgFXMaterial::checkForCgError("cgValidateTechnique", NULL); _uiValidationState = 0x02; } else { CgFXMaterial::checkForCgError("cgValidateTechnique", NULL); const CgFXMaterial::MFTexturesType *pTextures = pMat->getMFTextures(); CGpass pPass = cgGetFirstPass(_pCGTechnique); CgFXMaterial::checkForCgError("cgGetFirstPass", NULL); for(UInt32 i = 0; i < pTextures->size(); ++i) { const Char8 *szTexParamName = getName((*pTextures)[i]); Int32 iTexParamVal = -1; const ShaderVariable *pVar = pMat->getVariable(szTexParamName); OSG_ASSERT(pVar != NULL); const CgFXVariableTexObj *pTexVar = static_cast<const CgFXVariableTexObj *>(pVar); OSG_ASSERT(pTexVar != NULL); iTexParamVal = pTexVar->getValue(); if(iTexParamVal == -1) { CGparameter pParam = cgGetNamedEffectParameter(pMat->getEffect(), szTexParamName); CgFXMaterial::checkForCgError("cgGetNamedEffectParameter", NULL); (*pTextures)[i]->activate(pEnv, 0); Window::GLObjectId texObjId = pEnv->getWindow()->getGLObjectId( (*pTextures)[i]->getGLId()); cgGLSetTextureParameter(pParam, texObjId); CgFXMaterial::checkForCgError("cgGLSetTextureParameter", NULL); cgSetSamplerState (pParam ); CgFXMaterial::checkForCgError("cgSetSamplerState", NULL); CgFXVariableTexObj *pTexVarW = const_cast<CgFXVariableTexObj *>(pTexVar); pTexVarW->setValue(texObjId); (*pTextures)[i]->deactivate(pEnv); } } int count = 0; while(pPass) { StateUnrecPtr pState = State ::create(); CgFXPassChunkUnrecPtr pChunk = CgFXPassChunk::create(); pChunk->setPass (pPass); pChunk->setMaterial(pMat ); pState->addChunk(pChunk); this->addPassState(pState); pPass = cgGetNextPass(pPass); CgFXMaterial::checkForCgError("cgGetNextPass", NULL); count++; } pMat->updateUniformVariables(); } #if 0 if(_pCGTechnique != NULL) { fprintf(stderr, "Validated technique %s : %x\n", cgGetTechniqueName(_pCGTechnique), UInt32(_uiValidationState)); } #endif #if 0 if(pWin != NULL) { pWin->deactivate(); } #endif } return (_uiValidationState & TechniqueValid); }
/*------------------------------------------------------------------------- -------------------------------------------------------------------------*/ void FilterBox::Draw(float f) { if(!bValid) return; glPolygonMode( GL_FRONT_AND_BACK, GL_FILL); CGbool bRes; bRes = cgValidateTechnique(cgTechnique); if(!bRes) { bValid = false; const char * pszErrors = NULL; fprintf(stderr, "Validation of FilterRect failed"); fprintf(stderr, "CgFx Parse error : %s", pszErrors); const char *listing = cgGetLastListing(cgContext); return; } // // intermediate stage : bluring horizontal // glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb[1]); glPushAttrib(GL_VIEWPORT_BIT); glViewport(0,0,bufw,bufh); cgGLSetupSampler(srcSampler, textureID[0]); cgSetPassState(cgPassFilterH); FULLSCRQUAD(); glPopAttrib(); // // intermediate stage : bluring vertical // glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb[0]); glPushAttrib(GL_VIEWPORT_BIT); glViewport(0,0,bufw,bufh); cgGLSetupSampler(srcSampler, textureID[1]); cgSetPassState(cgPassFilterV); FULLSCRQUAD(); glPopAttrib(); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // // Final stage : Blend the final texture to the screen // cgGLSetupSampler(tempSampler, textureID[0]); cgSetPassState(cgPassBlend); glBlendColor(f,f,f,f); float xoffset = -1.0f + 2.0f*(float)posx/(float)vpw; float yoffset = -1.0f + 2.0f*(float)posy/(float)vph; float xoffset2 = xoffset + 2.0f*(float)width/(float)vpw; float yoffset2 = yoffset + 2.0f*(float)height/(float)vph; glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex4f(xoffset, yoffset, 0,1); glTexCoord2f(1,0); glVertex4f(xoffset2, yoffset,0,1); glTexCoord2f(1,1); glVertex4f(xoffset2, yoffset2,0,1); glTexCoord2f(0,1); glVertex4f(xoffset, yoffset2,0,1); glEnd(); cgResetPassState(cgPassBlend); }