static bool AddTechnique(JSON &json, CGtechnique technique, UniformsMap &uniformRemapping) { bool success = true; const char * const techniqueName = cgGetTechniqueName(technique); if (sVerbose) { puts(techniqueName); } json.AddArray(techniqueName); CGpass pass = cgGetFirstPass(technique); if (NULL == pass) { success = false; } while (NULL != pass) { success &= AddPass(technique, json, pass, uniformRemapping); pass = cgGetNextPass(pass); } json.CloseArray(); // techniqueName return success; }
cgfxTechnique::cgfxTechnique( CGtechnique technique, const cgfxProfile* profile ) : fTechnique( technique), fValid(false), fPasses(NULL), fNumPasses(0), fNext(NULL) { if (technique) { fName = cgGetTechniqueName(technique); CGpass pass = cgGetFirstPass(technique); cgfxPass** nextPass = &fPasses; while (pass) { ++fNumPasses; *nextPass = new cgfxPass(pass, profile); nextPass = &(*nextPass)->fNext; pass = cgGetNextPass(pass); } fHasBlending = hasBlending(fTechnique); setProfile(profile); } }
void print_info() { camera->orthographic( 0.0f, static_cast<float>(win_w), 0.0f, static_cast<float>(win_h), -1.0f, 1.0f); glEnable(GL_BLEND); cgSetMatrixParameterfc(cgshaders->get_parameter(std::string("MODELVIEWPROJECTION")), camera->m_projection); CGpass p0 = cgGetFirstPass(cgshaders->get_technique(std::string("bmf"))); cgSetPassState(p0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, bmf_texture); std::stringstream s11; s11 << std::fixed << std::setprecision(0) << mx << std::string(" ") << my << std::string(" ") << tmp0 << std::string(" ") << tmp1 << std::string(" ") << fps_ << std::ends; BMF_DrawStringTextureVBO((unsigned int)s11.str().size(), bmf_font, (char *)s11.str().c_str(), (float)50, (float)50); //cgResetPassState(p0); glDisable(GL_BLEND); glBindTexture(GL_TEXTURE_2D, GL_NONE); }
bool cEffect::SetFirstPass() { if ( mTechnique ) { mCurrentPass = cgGetFirstPass(mTechnique); if ( mCurrentPass ) { cgSetPassState(mCurrentPass); return true; } } return false; }
void CgEffect::Begin() { this->nextPass = cgGetFirstPass(this->activeTechnique); this->currentPass = NULL; }
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); }
// // Scan the technique for passes which use blending // bool cgfxTechnique::hasBlending(CGtechnique technique) { // Assume not blending bool hasBlending = false; // Check for : BlendEnable=true, BlendFunc=something valid on the first pass only. // // We ignore any depth enable and functions for now... // CGpass cgPass = cgGetFirstPass(technique); bool foundBlendEnabled = false; bool foundBlendFunc = false; if (cgPass) { CGstateassignment stateAssignment = cgGetFirstStateAssignment(cgPass); while ( stateAssignment ) { CGstate state = cgGetStateAssignmentState( stateAssignment); const char *stateName = cgGetStateName(state); // Check for blend enabled. if (!foundBlendEnabled && stricmp( stateName, "BlendEnable") == 0) { int numValues = 0; const CGbool *values = cgGetBoolStateAssignmentValues(stateAssignment, &numValues); if (values && numValues) { if (values[0]) { foundBlendEnabled = true; } } } // Check for valid blend function else if (!foundBlendFunc && ( stricmp( stateName, "BlendFunc") == 0 || stricmp( stateName, "BlendFuncSeparate") == 0 )) { int numValues = 0; const int * values = cgGetIntStateAssignmentValues(stateAssignment, &numValues); if (values) { #if defined(CGFX_DEBUG_BLEND_FUNCTIONS) /* #define GL_SRC_COLOR 0x0300 = 768 #define GL_ONE_MINUS_SRC_COLOR 0x0301 = 769 #define GL_SRC_ALPHA 0x0302 = 770 #define GL_ONE_MINUS_SRC_ALPHA 0x0303 = 771 #define GL_DST_ALPHA 0x0304 = 772 #define GL_ONE_MINUS_DST_ALPHA 0x0305 = 773 */ MString blendStringTable[6] = { "GL_SRC_COLOR", // SrcColor "GL_ONE_MINUS_SRC_COLOR", // OneMinusSrcColor "GL_SRC_ALPHA", // SrcAlpha "GL_ONE_MINUS_SRC_ALPHA", // OneMinusSrcAlpha "GL_DST_ALPHA", // DstAlpha "GL_ONE_MINUS_DST_ALPHA" // OneMinusDstAlpha }; #endif for (int i=0; i<numValues; i++) { if ((values[i] >= GL_SRC_COLOR) && (values[i] <= GL_ONE_MINUS_DST_ALPHA)) { #if defined(CGFX_DEBUG_BLEND_FUNCTIONS) printf("Found blend function = %s, %s\n", blendStringTable[ values[0]-GL_SRC_COLOR].asChar(), blendStringTable[ values[1]-GL_SRC_COLOR].asChar()); #endif foundBlendFunc = true; break; } } } } hasBlending = foundBlendEnabled && foundBlendFunc; if (hasBlending) break; stateAssignment = cgGetNextStateAssignment( stateAssignment); } } return hasBlending; }
cCgShaderHelper::cCgShaderHelper(const zsString& shaderPath) { cFileUtil::ReplaceIncludeDirectives(shaderPath, cgFileLines, includedFilesPaths); // Setup context, for creation (Yeah silly, but use OpenGL cg state and params) because equal with dx... CGcontext con = cgCreateContext(); cgGLRegisterStates(con); cgGLSetManageTextureParameters(con, CG_TRUE); // Create cg effect from file (load) assert(shaderPath.size() <= 256); char ansiShaderPath[256]; cStrUtil::ToAnsi(shaderPath, ansiShaderPath, 256); CGeffect effect = cgCreateEffectFromFile(con, ansiShaderPath, nullptr); if (effect == nullptr) { lastErrorMsg = cgGetLastListing(con); // Free up cgDestroyEffect(effect); cgDestroyContext(con); return; } // Tech creation CGtechnique tech = cgGetFirstTechnique(effect); if (tech == nullptr) { lastErrorMsg = L"There is no Technique in shader: " + shaderPath; // Free up cgDestroyEffect(effect); cgDestroyContext(con); return; } // Pass creation CGpass pass = cgGetFirstPass(tech); if (pass == nullptr) { lastErrorMsg = L"There is no pass in shader: " + shaderPath; // Free up cgDestroyEffect(effect); cgDestroyContext(con); return; } CGprogram shaderPrograms[NDOMAINS]; shaderPrograms[VS] = cgGetPassProgram(pass, CGdomain::CG_VERTEX_DOMAIN); shaderPrograms[HS] = cgGetPassProgram(pass, CGdomain::CG_TESSELLATION_CONTROL_DOMAIN); shaderPrograms[DS] = cgGetPassProgram(pass, CGdomain::CG_TESSELLATION_EVALUATION_DOMAIN); shaderPrograms[GS] = cgGetPassProgram(pass, CGdomain::CG_GEOMETRY_DOMAIN); shaderPrograms[PS] = cgGetPassProgram(pass, CGdomain::CG_FRAGMENT_DOMAIN); // Domain infos for (uint8_t i = 0; i < NDOMAINS; i++) { // Domain existence info.domainsExist[i] = shaderPrograms[i] != nullptr; // if exist save entry name if (info.domainsExist[i]) info.domainsEntry[i] = cgGetProgramString(shaderPrograms[i], CGenum::CG_PROGRAM_ENTRY); } // Free up cgDestroyEffect(effect); cgDestroyContext(con); }
void render(double et) { if (!cgshaders||!run||!camera||!context) return; ms_fbo->Activate(); glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); if (v_forward != 0 || v_backward != 0) { if (v_forward == 1) { camera->set_velocity(10.0f); } else { if (v_backward == 1) { camera->set_velocity(-10.0f); } } } else { camera->set_velocity(0.0f); } if (v_right != 0 || v_left != 0) { if (v_right == 1) { camera->set_side_velocity(10.0f); } else { if (v_left == 1) { camera->set_side_velocity(-10.0f); } } } else { camera->set_side_velocity(0.0f); } #ifndef QUERY_POINTER camera->change_heading(DEMO_D2R*heading_degrees); camera->change_pitch (DEMO_D2R*pitch_degrees ); #else camera->set_heading(DEMO_D2R*90.0f*heading_degrees); camera->set_pitch (DEMO_D2R*90.0f*pitch_degrees ); #endif camera->set_viewer_matrix(et); camera->perspective((float)win_w, (float)win_h, camera_fov_radians, camera_znear, camera_zfar); int_params[0] = 1; lights_positions[0] = light0_direction[0]; lights_positions[1] = light0_direction[1]; lights_positions[2] = light0_direction[2]; float_params[0] = camera->m_position_aos.getX(); float_params[1] = camera->m_position_aos.getY(); float_params[2] = camera->m_position_aos.getZ(); cgshaders->set_array_param_3f(std::string("lights_positions"),0, 0, lights_positions); cgshaders->set_array_param_3f(std::string("float_params"), 0, 0, float_params); glViewport(0, 0, GLsizei(win_w), GLsizei(win_h)); /////////////////////////////////////////////////////////// skybox cgshaders->set_effect_matrix_param_fc("PROJECTION", camera->m_projection); cgshaders->set_effect_matrix_param_fc("VIEW_INVERSE", camera->m_inv_view); CGpass p0 = cgGetFirstPass(cgshaders->get_technique(std::string("skybox"))); cgSetPassState(p0); glActiveTexture(GL_TEXTURE5); glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_t); glBindBuffer(GL_ARRAY_BUFFER, sphere0_vbo[0]); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(0); glDrawArrays(GL_TRIANGLES, 0, (faces_size_sphere0/12)*3); glDisableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, GL_NONE); glBindTexture(GL_TEXTURE_CUBE_MAP, GL_NONE); //cgResetPassState(p0); ////////////////////////////////////////////////////////////// float mvp[16]; Matrix4 mvp_aos = camera->m_projection_aos*camera->m_view_aos; copy_matrix4_to_float(mvp_aos,mvp); if (plant_shader_time > 60.0) { plant_shader_time = 0.0; } plant_shader_time += et; cgshaders->set_effect_param_1f(std::string("PLANT_SHADER_TIME"), (float)plant_shader_time); //#if 0 for (unsigned int x = 0; x < objects.size(); x++) { DisplayInterface * di = objects.at(x); for (int k = 0; k < di->submeshes_size(); k++) { SubMesh * sm = di->get_submesh(k); if (!sm) continue; if (!sm->get_data()) continue; //if (pass == L_SHADOW_PASS && sm->get_data()->cast_shadows[0] == 0) // continue; //if (pass == L_2ND_SHADOW_PASS && sm->get_data()->cast_shadows[1] == 0) // continue; if (sm->callback != 0) { mparams[5] = sm->get_data()->shininess; mparams[6] = sm->get_data()->tc_scale_x; mparams[7] = sm->get_data()->tc_scale_y; mparams[8] = (float)win_w; mparams[9] = (float)win_h; sm->callback(sm->get_data(), cgshaders, int_params, mparams, float_params, lights_positions, camera->m_view, camera->m_inv_view, 0, 0, mvp, 0, 0); } } } //#endif // // ARK cgshaders->set_pass_state(cgshaders->get_first_pass(std::string("tbn0"))); // ARK MAIN glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, ark_sp); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, ark_nm); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, ark_t); for (unsigned int x = 0; x < ark.size(); x++) { DisplayInterface * di = ark.at(x); for (int k = 0; k < di->submeshes_size(); k++) { SubMesh * sm = di->get_submesh(k); if (!sm) continue; if (!sm->get_data()) continue; //if (pass == L_SHADOW_PASS && sm->get_data()->cast_shadows[0] == 0) // continue; //if (pass == L_2ND_SHADOW_PASS && sm->get_data()->cast_shadows[1] == 0) // continue; if (sm->callback != 0) { mparams[5] = sm->get_data()->shininess; mparams[6] = sm->get_data()->tc_scale_x; mparams[7] = sm->get_data()->tc_scale_y; mparams[8] = (float)win_w; mparams[9] = (float)win_h; sm->callback(sm->get_data(), cgshaders, int_params, mparams, float_params, lights_positions, camera->m_view, camera->m_inv_view, 0, 0, mvp, 0, 0); } } } glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, GL_NONE); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, GL_NONE); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, GL_NONE); //////// // ARK WHEELS glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, ark_wheels_sp); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, ark_wheels_nm); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, ark_wheels_t); for (unsigned int x = 0; x < ark_wheels.size(); x++) { DisplayInterface * di = ark_wheels.at(x); for (int k = 0; k < di->submeshes_size(); k++) { SubMesh * sm = di->get_submesh(k); if (!sm) continue; if (!sm->get_data()) continue; //if (pass == L_SHADOW_PASS && sm->get_data()->cast_shadows[0] == 0) // continue; //if (pass == L_2ND_SHADOW_PASS && sm->get_data()->cast_shadows[1] == 0) // continue; if (sm->callback != 0) { mparams[5] = sm->get_data()->shininess; mparams[6] = sm->get_data()->tc_scale_x; mparams[7] = sm->get_data()->tc_scale_y; mparams[8] = (float)win_w; mparams[9] = (float)win_h; sm->callback(sm->get_data(), cgshaders, int_params, mparams, float_params, lights_positions, camera->m_view, camera->m_inv_view, 0, 0, mvp, 0, 0); } } } glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, GL_NONE); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, GL_NONE); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, GL_NONE); // // ARK WHEELS glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, ark_tread_sp); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, ark_tread_nm); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, ark_tread_t); for (unsigned int x = 0; x < ark_tread.size(); x++) { DisplayInterface * di = ark_tread.at(x); for (int k = 0; k < di->submeshes_size(); k++) { SubMesh * sm = di->get_submesh(k); if (!sm) continue; if (!sm->get_data()) continue; //if (pass == L_SHADOW_PASS && sm->get_data()->cast_shadows[0] == 0) // continue; //if (pass == L_2ND_SHADOW_PASS && sm->get_data()->cast_shadows[1] == 0) // continue; if (sm->callback != 0) { mparams[5] = sm->get_data()->shininess; mparams[6] = sm->get_data()->tc_scale_x; mparams[7] = sm->get_data()->tc_scale_y; mparams[8] = (float)win_w; mparams[9] = (float)win_h; sm->callback(sm->get_data(), cgshaders, int_params, mparams, float_params, lights_positions, camera->m_view, camera->m_inv_view, 0, 0, mvp, 0, 0); } } } glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, GL_NONE); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, GL_NONE); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, GL_NONE); // glFinish(); glBindFramebuffer(GL_READ_FRAMEBUFFER, ms_fbo->GetFramebuffer()); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, scene_fbo->GetFramebuffer()); glBlitFramebuffer(0, 0, win_w, win_h, 0, 0, win_w, win_h, GL_COLOR_BUFFER_BIT, GL_LINEAR); glBindFramebuffer(GL_FRAMEBUFFER, GL_NONE); cgshaders->set_pass_state(cgshaders->get_first_pass(std::string("fsquad"))); glViewport(0, 0, static_cast<GLsizei>(win_w), static_cast<GLsizei>(win_h) ); glDepthFunc(GL_ALWAYS); glActiveTexture(GL_TEXTURE0); scene_fbo->BindColor(); glBindBuffer(GL_ARRAY_BUFFER, fsquad[0]); glVertexAttribPointer(0, 2, GL_FLOAT,GL_FALSE, 0, 0); glEnableVertexAttribArray(0); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, GL_NONE); glBindTexture(GL_TEXTURE_RECTANGLE, GL_NONE); glDepthFunc(GL_LEQUAL); print_info(); context->swap_buffers(); }
void CGFXShader::SetVertexFormat(VertexElement format[], uint32 nElem) { if(m_pIL != NULL) { m_pIL->Release(); } //DXGI_FORMAT_R32G32B32_FLOAT ID3D11Device* pDevice = cgD3D11GetDevice(m_pCG); D3D11_INPUT_ELEMENT_DESC* layout = new D3D11_INPUT_ELEMENT_DESC[nElem]; for(uint32 i = 0; i < nElem; ++i) { switch(format[i].semantic) { case VertexElement::POSITION: layout[i].SemanticName = "POSITION"; break; case VertexElement::NORMAL: layout[i].SemanticName = "NORMAL"; break; case VertexElement::COLOR: layout[i].SemanticName = "COLOR"; break; case VertexElement::POSITION_T: layout[i].SemanticName = "POSITIONT"; break; case VertexElement::TEXCOORD: layout[i].SemanticName = "TEXCOORD"; break; default: break; } switch(format[i].type) { case VertexElement::VE_FLOAT1: layout[i].Format = DXGI_FORMAT_R32_FLOAT; break; case VertexElement::VE_FLOAT2: layout[i].Format = DXGI_FORMAT_R32G32_FLOAT; break; case VertexElement::VE_FLOAT3: layout[i].Format = DXGI_FORMAT_R32G32B32_FLOAT; break; case VertexElement::VE_FLOAT4: layout[i].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; break; case VertexElement::VE_UINT32: layout[i].Format = DXGI_FORMAT_R32_UINT; break; default: break; } layout[i].SemanticIndex = format[i].element_slot; layout[i].InputSlot = 0; layout[i].AlignedByteOffset = 0; layout[i].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; layout[i].InstanceDataStepRate = 0; } CGpass pass = cgGetFirstPass( m_pTechnique ); ID3D10Blob * pVSBuf = cgD3D11GetIASignatureByPass( pass ); pDevice->CreateInputLayout( layout, nElem, pVSBuf->GetBufferPointer(), pVSBuf->GetBufferSize(), &m_pIL ); }
void Effect::activate() { currentPass = cgGetFirstPass(technique); cgSetPassState(currentPass); }