void Graphics::renderGlowTexture() { glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(-1,1,-1,1,0.5,5); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); gluLookAt(0.0,0.0,1.0, 0.0,0.0,0.0, 0.0,1.0,0.0); if(glowShader == NULL) { World &world = World::getInstance(); glowShader = InitProgram( (world.assetsDir + "shaders/glow.vert").c_str(), (world.assetsDir + "shaders/glow.frag").c_str() ); } glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, glowTexId); int glowLoc = UniformLocation(glowShader,"glowTexture"); glUniform1i(glowLoc,0); glClear(GL_DEPTH_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE); glDisable(GL_LIGHTING); glColor3f(1.0,1.0,1.0); UseProgram(glowShader); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex3f(-1,-1,0); glTexCoord2f(1,0); glVertex3f(1,-1,0); glTexCoord2f(1,1); glVertex3f(1,1,0); glTexCoord2f(0,1); glVertex3f(-1,1,0); glEnd(); UseProgram(0); glDisable(GL_BLEND); glEnable(GL_LIGHTING); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); }
int rb_uniform_data(struct rb_uniform* uniform, int nb, const void* data) { if(!uniform || !data) return -1; if(nb <= 0) return -1; assert(uniform->set != NULL); OGL(UseProgram(uniform->program->name)); uniform->set(uniform->location, nb, data); OGL(UseProgram(uniform->ctxt->state_cache.current_program)); return 0; }
GrGLProgram::GrGLProgram(GrGLGpu* gpu, const GrProgramDesc& desc, const BuiltinUniformHandles& builtinUniforms, GrGLuint programID, const UniformInfoArray& uniforms, const UniformInfoArray& textureSamplers, const UniformInfoArray& texelBuffers, const VaryingInfoArray& pathProcVaryings, std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor, std::unique_ptr<GrGLSLXferProcessor> xferProcessor, const GrGLSLFragProcs& fragmentProcessors) : fBuiltinUniformHandles(builtinUniforms) , fProgramID(programID) , fGeometryProcessor(std::move(geometryProcessor)) , fXferProcessor(std::move(xferProcessor)) , fFragmentProcessors(fragmentProcessors) , fDesc(desc) , fGpu(gpu) , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings) , fNumTextureSamplers(textureSamplers.count()) , fNumTexelBuffers(texelBuffers.count()) { // Assign texture units to sampler uniforms one time up front. GL_CALL(UseProgram(fProgramID)); fProgramDataManager.setSamplerUniforms(textureSamplers, 0); fProgramDataManager.setSamplerUniforms(texelBuffers, fNumTextureSamplers); }
void COGL_FragmentProgramCombiner::GenerateCombinerSetting(int index) { GLuint ID = m_vCompiledShaders[index].programID; UseProgram(ID); glEnableVertexAttribArray(VS_POSITION); OPENGL_CHECK_ERRORS; glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0])); OPENGL_CHECK_ERRORS; glEnableVertexAttribArray(VS_TEXCOORD0); OPENGL_CHECK_ERRORS; glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u)); OPENGL_CHECK_ERRORS; glEnableVertexAttribArray(VS_TEXCOORD1); OPENGL_CHECK_ERRORS; glVertexAttribPointer(VS_TEXCOORD1,2,GL_FLOAT,GL_FALSE, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[1].u)); OPENGL_CHECK_ERRORS; glEnableVertexAttribArray(VS_COLOR); OPENGL_CHECK_ERRORS; glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) ); OPENGL_CHECK_ERRORS; glEnableVertexAttribArray(VS_FOG); OPENGL_CHECK_ERRORS; glVertexAttribPointer(VS_FOG,1,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][4])); OPENGL_CHECK_ERRORS; }
GrGLProgram::GrGLProgram(GrGLGpu* gpu, const GrProgramDesc& desc, const BuiltinUniformHandles& builtinUniforms, GrGLuint programID, const UniformInfoArray& uniforms, const VaryingInfoArray& pathProcVaryings, GrGLSLPrimitiveProcessor* geometryProcessor, GrGLSLXferProcessor* xferProcessor, const GrGLSLFragProcs& fragmentProcessors, SkTArray<UniformHandle>* passSamplerUniforms) : fBuiltinUniformHandles(builtinUniforms) , fProgramID(programID) , fGeometryProcessor(geometryProcessor) , fXferProcessor(xferProcessor) , fFragmentProcessors(fragmentProcessors) , fDesc(desc) , fGpu(gpu) , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings) { fSamplerUniforms.swap(passSamplerUniforms); // Assign texture units to sampler uniforms one time up front. GL_CALL(UseProgram(fProgramID)); for (int i = 0; i < fSamplerUniforms.count(); i++) { fProgramDataManager.setSampler(fSamplerUniforms[i], i); } }
CRenderingContext::CRenderingContext(CRenderer* pRenderer, bool bInherit) { m_pRenderer = pRenderer; m_clrRender = ::Color(255, 255, 255, 255); s_aContexts.push_back(); if (bInherit && s_aContexts.size() > 1) { CRenderContext& oLastContext = s_aContexts[s_aContexts.size()-2]; CRenderContext& oThisContext = GetContext(); oThisContext.m_mProjection = oLastContext.m_mProjection; oThisContext.m_mView = oLastContext.m_mView; oThisContext.m_mTransformations = oLastContext.m_mTransformations; oThisContext.m_hMaterial = oLastContext.m_hMaterial; oThisContext.m_pFrameBuffer = oLastContext.m_pFrameBuffer; tstrncpy(oThisContext.m_szProgram, PROGRAM_LEN, oLastContext.m_szProgram, PROGRAM_LEN); oThisContext.m_pShader = oLastContext.m_pShader; oThisContext.m_rViewport = oLastContext.m_rViewport; oThisContext.m_eBlend = oLastContext.m_eBlend; oThisContext.m_flAlpha = oLastContext.m_flAlpha; oThisContext.m_bDepthMask = oLastContext.m_bDepthMask; oThisContext.m_bDepthTest = oLastContext.m_bDepthTest; oThisContext.m_eDepthFunction = oLastContext.m_eDepthFunction; oThisContext.m_bCull = oLastContext.m_bCull; oThisContext.m_bWinding = oLastContext.m_bWinding; m_pShader = oThisContext.m_pShader; if (m_pShader) m_iProgram = m_pShader->m_iProgram; else m_iProgram = 0; } else { m_pShader = NULL; BindTexture(0); UseMaterial(CMaterialHandle()); UseFrameBuffer(NULL); UseProgram(""); SetViewport(Rect(0, 0, Application()->GetWindowWidth(), Application()->GetWindowHeight())); SetBlend(BLEND_NONE); SetAlpha(1); SetDepthMask(true); SetDepthTest(true); SetDepthFunction(DF_LESS); SetBackCulling(true); SetWinding(true); } }
void GLShader::Uniform4f(const GLchar* var_name, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) { GLint Location = glGetUniformLocation(_shader_prog, var_name); UseProgram(); glUniform4f(Location, v0, v1, v2, v3); }
GrGpuGLShaders::~GrGpuGLShaders() { if (fProgramData && 0 != fHWProgramID) { // detach the current program so there is no confusion on OpenGL's part // that we want it to be deleted SkASSERT(fHWProgramID == fProgramData->fProgramID); GL_CALL(UseProgram(0)); } delete fProgramCache; }
void GrGLProgram::initSamplerUniforms() { GL_CALL(UseProgram(fBuilderOutput.fProgramID)); GrGLint texUnitIdx = 0; if (fBuilderOutput.fUniformHandles.fDstCopySamplerUni.isValid()) { fUniformManager->setSampler(fBuilderOutput.fUniformHandles.fDstCopySamplerUni, texUnitIdx); fDstCopyTexUnit = texUnitIdx++; } fBuilderOutput.fColorEffects->initSamplers(*fUniformManager, &texUnitIdx); fBuilderOutput.fCoverageEffects->initSamplers(*fUniformManager, &texUnitIdx); }
void CRenderingContext::UseMaterial(const CMaterialHandle& hMaterial) { if (!hMaterial.IsValid()) return; GetContext().m_hMaterial = hMaterial; UseProgram(hMaterial->m_pShader); SetupMaterial(); }
void CRenderingContext::UseProgram(const tchar* pszProgram) { tstrncpy(GetContext().m_szProgram, PROGRAM_LEN, pszProgram, PROGRAM_LEN); GetContext().m_pShader = m_pShader = CShaderLibrary::GetShader(pszProgram); if (*pszProgram) TAssert(m_pShader); UseProgram(m_pShader); }
void GL::EnableShading(GLuint aShaderProgram) { MOZ_ASSERT(IsCurrent()); if (mShaderProgram == aShaderProgram) { return; } UseProgram(aShaderProgram); mShaderProgram = aShaderProgram; }
CRenderingContext::~CRenderingContext() { TAssert(s_aContexts.size()); s_aContexts.pop_back(); if (s_aContexts.size()) { CRenderContext& oContext = GetContext(); UseMaterial(oContext.m_hMaterial); UseFrameBuffer(oContext.m_pFrameBuffer); UseProgram(oContext.m_pShader); if (*oContext.m_szProgram) { oContext.m_bProjectionUpdated = false; oContext.m_bViewUpdated = false; oContext.m_bTransformUpdated = false; } SetViewport(oContext.m_rViewport); SetBlend(oContext.m_eBlend); SetAlpha(oContext.m_flAlpha); SetDepthMask(oContext.m_bDepthMask); SetDepthTest(oContext.m_bDepthTest); SetDepthFunction(oContext.m_eDepthFunction); SetBackCulling(oContext.m_bCull); SetWinding(oContext.m_bWinding); } else { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); if (m_pRenderer) glViewport(0, 0, (GLsizei)m_pRenderer->m_iWidth, (GLsizei)m_pRenderer->m_iHeight); else glViewport(0, 0, (GLsizei)Application()->GetWindowWidth(), (GLsizei)Application()->GetWindowHeight()); glUseProgram(0); glDisablei(GL_BLEND, 0); glDepthMask(true); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glDepthFunc(GL_LESS); glFrontFace(GL_CCW); } }
void GrGLProgram::initSamplerUniforms() { GL_CALL(UseProgram(fProgramID)); GrGLint texUnitIdx = 0; this->initSamplers(fGeometryProcessor.get(), &texUnitIdx); if (fXferProcessor.get()) { this->initSamplers(fXferProcessor.get(), &texUnitIdx); } int numProcs = fFragmentProcessors->fProcs.count(); for (int i = 0; i < numProcs; i++) { this->initSamplers(fFragmentProcessors->fProcs[i], &texUnitIdx); } }
void GrGLProgram::initSamplerUniforms() { GL_CALL(UseProgram(fProgramID)); GrGLint texUnitIdx = 0; if (fBuiltinUniformHandles.fDstCopySamplerUni.isValid()) { fProgramDataManager.setSampler(fBuiltinUniformHandles.fDstCopySamplerUni, texUnitIdx); fDstCopyTexUnit = texUnitIdx++; } if (fGeometryProcessor.get()) { this->initSamplers(fGeometryProcessor.get(), &texUnitIdx); } int numProcs = fFragmentProcessors->fProcs.count(); for (int i = 0; i < numProcs; i++) { this->initSamplers(fFragmentProcessors->fProcs[i], &texUnitIdx); } }
void CRenderingContext::EndRender() { TAssert(m_pShader); if (!m_pShader) { UseProgram("model"); if (!m_pShader) return; } SetUniform("mProjection", GetContext().m_mProjection); SetUniform("mView", GetContext().m_mView); SetUniform("mGlobal", GetContext().m_mTransformations); if (m_bTexCoord && m_pShader->m_iTexCoordAttribute != ~0) { glEnableVertexAttribArray(m_pShader->m_iTexCoordAttribute); glVertexAttribPointer(m_pShader->m_iTexCoordAttribute, 2, GL_FLOAT, false, 0, m_aavecTexCoords[0].data()); } if (m_bNormal && m_pShader->m_iNormalAttribute != ~0) { glEnableVertexAttribArray(m_pShader->m_iNormalAttribute); glVertexAttribPointer(m_pShader->m_iNormalAttribute, 3, GL_FLOAT, false, 0, m_avecNormals.data()); } if (m_bColor && m_pShader->m_iColorAttribute != ~0) { glEnableVertexAttribArray(m_pShader->m_iColorAttribute); glVertexAttribPointer(m_pShader->m_iColorAttribute, 3, GL_UNSIGNED_BYTE, true, sizeof(::Color), m_aclrColors.data()); } TAssert(m_pShader->m_iPositionAttribute != ~0); glEnableVertexAttribArray(m_pShader->m_iPositionAttribute); glVertexAttribPointer(m_pShader->m_iPositionAttribute, 3, GL_FLOAT, false, 0, m_avecVertices.data()); glDrawArrays(m_iDrawMode, 0, m_avecVertices.size()); glDisableVertexAttribArray(m_pShader->m_iPositionAttribute); if (m_pShader->m_iTexCoordAttribute != ~0) glDisableVertexAttribArray(m_pShader->m_iTexCoordAttribute); if (m_pShader->m_iNormalAttribute != ~0) glDisableVertexAttribArray(m_pShader->m_iNormalAttribute); if (m_pShader->m_iColorAttribute != ~0) glDisableVertexAttribArray(m_pShader->m_iColorAttribute); }
void GLVec4ScalarBench::setup(const GrGLContext* ctx) { const GrGLInterface* gl = ctx->interface(); if (!gl) { SkFAIL("GL interface is nullptr in setup()!\n"); } fFboTextureId = SetupFramebuffer(gl, kScreenWidth, kScreenHeight); fProgram = this->setupShader(ctx); int index = 0; SkMatrix viewMatrices[kNumTriPerDraw]; setup_matrices(kNumTriPerDraw, [&index, &viewMatrices](const SkMatrix& m) { viewMatrices[index++] = m; }); this->setupSingleVbo(gl, viewMatrices); GR_GL_CALL(gl, UseProgram(fProgram)); }
void GLCpuPosInstancedArraysBench::setup(const GrGLContext* ctx) { const GrGLInterface* gl = ctx->interface(); fTexture = SetupFramebuffer(gl, kScreenWidth, kScreenHeight); fProgram = this->setupShader(ctx); // setup matrices int index = 0; SkMatrix viewMatrices[kNumTri]; setup_matrices(kNumTri, [&index, &viewMatrices](const SkMatrix& m) { viewMatrices[index++] = m; }); // setup VAO GR_GL_CALL(gl, GenVertexArrays(1, &fVAO)); GR_GL_CALL(gl, BindVertexArray(fVAO)); switch (fVboSetup) { case kUseOne_VboSetup: this->setupSingleVbo(gl, viewMatrices); break; case kUseTwo_VboSetup: this->setupDoubleVbo(gl, viewMatrices); break; case kUseInstance_VboSetup: this->setupInstanceVbo(gl, viewMatrices); break; } // clear screen GR_GL_CALL(gl, ClearColor(0.03f, 0.03f, 0.03f, 1.0f)); GR_GL_CALL(gl, Clear(GR_GL_COLOR_BUFFER_BIT)); // set us up to draw GR_GL_CALL(gl, UseProgram(fProgram)); GR_GL_CALL(gl, BindVertexArray(fVAO)); }
void ShaderPosBase::DrawGrid(const ShaderState &ss_, double width, double3 first, double3 last) const { ShaderState ss = ss_; ss.translateZ(first.z); uint xCount = ceil_int((last.x - first.x) / width); uint yCount = ceil_int((last.y - first.y) / width); float2 *v = new float2[2 * (xCount + yCount)]; float2 *pv = v; for (uint x=0; x<xCount; x++) { pv->x = first.x + x * width; pv->y = first.y; pv++; pv->x = first.x + x * width; pv->y = last.y; pv++; } for (uint y=0; y<yCount; y++) { pv->x = first.x; pv->y = first.y + y * width; pv++; pv->x = last.x; pv->y = first.y + y * width; pv++; } UseProgram(ss, v); ss.DrawArrays(GL_LINES, 2 * (xCount + yCount)); UnuseProgram(); delete[] v; }
GrGLProgram::GrGLProgram(GrGLGpu* gpu, const GrProgramDesc& desc, const BuiltinUniformHandles& builtinUniforms, GrGLuint programID, const UniformInfoArray& uniforms, const UniformInfoArray& samplers, const UniformInfoArray& imageStorages, const VaryingInfoArray& pathProcVaryings, GrGLSLPrimitiveProcessor* geometryProcessor, GrGLSLXferProcessor* xferProcessor, const GrGLSLFragProcs& fragmentProcessors) : fBuiltinUniformHandles(builtinUniforms) , fProgramID(programID) , fGeometryProcessor(geometryProcessor) , fXferProcessor(xferProcessor) , fFragmentProcessors(fragmentProcessors) , fDesc(desc) , fGpu(gpu) , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings) { // Assign texture units to sampler uniforms one time up front. GL_CALL(UseProgram(fProgramID)); fProgramDataManager.setSamplers(samplers); fProgramDataManager.setImageStorages(imageStorages); }
void COGL_FragmentProgramCombiner::InitCombinerCycleCopy(void) { m_pOGLRender->DisableMultiTexture(); m_pOGLRender->EnableTexUnit(0,TRUE); UseProgram(copyProgram); glUniform1f(copyAlphaLocation,m_AlphaRef); OPENGL_CHECK_ERRORS; glEnableVertexAttribArray(VS_POSITION); OPENGL_CHECK_ERRORS; glEnableVertexAttribArray(VS_TEXCOORD0); OPENGL_CHECK_ERRORS; glDisableVertexAttribArray(VS_COLOR); OPENGL_CHECK_ERRORS; glDisableVertexAttribArray(VS_TEXCOORD1); OPENGL_CHECK_ERRORS; glDisableVertexAttribArray(VS_FOG); OPENGL_CHECK_ERRORS; COGLTexture* pTexture = g_textures[gRSP.curTile].m_pCOGLTexture; if( pTexture ) { m_pOGLRender->BindTexture(pTexture->m_dwTextureName, 0); m_pOGLRender->SetTexelRepeatFlags(gRSP.curTile); } }
bool GrGpuGL::flushGraphicsState(DrawType type) { const GrDrawState& drawState = this->getDrawState(); // GrGpu::setupClipAndFlushState should have already checked this // and bailed if not true. GrAssert(NULL != drawState.getRenderTarget()); if (kStencilPath_DrawType != type) { this->flushMiscFixedFunctionState(); GrBlendCoeff srcCoeff; GrBlendCoeff dstCoeff; BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff); if (kSkipDraw_BlendOptFlag & blendOpts) { return false; } const GrEffectStage* stages[GrDrawState::kNumStages]; for (int i = 0; i < GrDrawState::kNumStages; ++i) { stages[i] = drawState.isStageEnabled(i) ? &drawState.getStage(i) : NULL; } GrGLProgram::Desc desc; this->buildProgram(kDrawPoints_DrawType == type, blendOpts, dstCoeff, &desc); fCurrentProgram.reset(fProgramCache->getProgram(desc, stages)); if (NULL == fCurrentProgram.get()) { GrAssert(!"Failed to create program!"); return false; } fCurrentProgram.get()->ref(); if (fHWProgramID != fCurrentProgram->fProgramID) { GL_CALL(UseProgram(fCurrentProgram->fProgramID)); fHWProgramID = fCurrentProgram->fProgramID; } fCurrentProgram->overrideBlend(&srcCoeff, &dstCoeff); this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff); GrColor color; GrColor coverage; if (blendOpts & kEmitTransBlack_BlendOptFlag) { color = 0; coverage = 0; } else if (blendOpts & kEmitCoverage_BlendOptFlag) { color = 0xffffffff; coverage = drawState.getCoverage(); } else { color = drawState.getColor(); coverage = drawState.getCoverage(); } this->flushColor(color); this->flushCoverage(coverage); fCurrentProgram->setData(this); } this->flushStencil(type); this->flushViewMatrix(type); this->flushScissor(); this->flushAAState(type); GrIRect* devRect = NULL; GrIRect devClipBounds; if (drawState.isClipState()) { this->getClip()->getConservativeBounds(drawState.getRenderTarget(), &devClipBounds); devRect = &devClipBounds; } // This must come after textures are flushed because a texture may need // to be msaa-resolved (which will modify bound FBO state). this->flushRenderTarget(devRect); return true; }
void GrGLProgram::genProgram(GrGLProgram::CachedData* programData, const GrDrawTarget* target) const { ShaderCodeSegments segments; const uint32_t& layout = fProgramDesc.fVertexLayout; memset(&programData->fUniLocations, 0, sizeof(UniLocations)); bool haveColor = !(ProgramDesc::kVertexColorAllOnes_OptFlagBit & fProgramDesc.fOptFlags); #if ATTRIBUTE_MATRIX segments.fVSAttrs = "attribute mat3 " VIEW_MATRIX_NAME ";\n"; #else segments.fVSUnis = "uniform mat3 " VIEW_MATRIX_NAME ";\n"; segments.fVSAttrs = ""; #endif segments.fVSAttrs += "attribute vec2 " POS_ATTR_NAME ";\n"; if (haveColor) { segments.fVSAttrs += "attribute vec4 " COL_ATTR_NAME ";\n"; segments.fVaryings = "varying vec4 vColor;\n"; } else { segments.fVaryings = ""; } segments.fVSCode = "void main() {\n" "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3(" POS_ATTR_NAME ", 1);\n" "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n"; if (haveColor) { segments.fVSCode += "\tvColor = " COL_ATTR_NAME ";\n"; } if (!(fProgramDesc.fOptFlags & ProgramDesc::kNotPoints_OptFlagBit)) { segments.fVSCode += "\tgl_PointSize = 1.0;\n"; } segments.fFSCode = "void main() {\n"; // add texture coordinates that are used to the list of vertex attr decls GrTokenString texCoordAttrs[GrDrawTarget::kMaxTexCoords]; for (int t = 0; t < GrDrawTarget::kMaxTexCoords; ++t) { if (target->VertexUsesTexCoordIdx(t, layout)) { tex_attr_name(t, texCoordAttrs + t); segments.fVSAttrs += "attribute vec2 "; segments.fVSAttrs += texCoordAttrs[t]; segments.fVSAttrs += ";\n"; } } // for each enabled stage figure out what the input coordinates are // and count the number of stages in use. const char* stageInCoords[GrDrawTarget::kNumStages]; int numActiveStages = 0; for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { if (fProgramDesc.fStages[s].fEnabled) { if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) & layout) { stageInCoords[s] = POS_ATTR_NAME; } else { int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout); // we better have input tex coordinates if stage is enabled. GrAssert(tcIdx >= 0); GrAssert(texCoordAttrs[tcIdx].length()); stageInCoords[s] = texCoordAttrs[tcIdx].cstr(); } ++numActiveStages; } } GrTokenString inColor = "vColor"; // if we have active stages string them together, feeding the output color // of each to the next and generating code for each stage. if (numActiveStages) { int currActiveStage = 0; for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { if (fProgramDesc.fStages[s].fEnabled) { GrTokenString outColor; if (currActiveStage < (numActiveStages - 1)) { outColor = "color"; outColor.appendInt(currActiveStage); segments.fFSCode += "\tvec4 "; segments.fFSCode += outColor; segments.fFSCode += ";\n"; } else { outColor = "gl_FragColor"; } genStageCode(s, fProgramDesc.fStages[s], haveColor ? inColor.cstr() : NULL, outColor.cstr(), stageInCoords[s], &segments, &programData->fUniLocations.fStages[s]); ++currActiveStage; inColor = outColor; haveColor = true; } } } else { segments.fFSCode += "\tgl_FragColor = "; if (haveColor) { segments.fFSCode += inColor; } else { segments.fFSCode += "vec4(1,1,1,1)"; } segments.fFSCode += ";\n"; } segments.fFSCode += "}\n"; segments.fVSCode += "}\n"; const char* strings[4]; int lengths[4]; int stringCnt = 0; if (segments.fVSUnis.length()) { strings[stringCnt] = segments.fVSUnis.cstr(); lengths[stringCnt] = segments.fVSUnis.length(); ++stringCnt; } if (segments.fVSAttrs.length()) { strings[stringCnt] = segments.fVSAttrs.cstr(); lengths[stringCnt] = segments.fVSAttrs.length(); ++stringCnt; } if (segments.fVaryings.length()) { strings[stringCnt] = segments.fVaryings.cstr(); lengths[stringCnt] = segments.fVaryings.length(); ++stringCnt; } GrAssert(segments.fVSCode.length()); strings[stringCnt] = segments.fVSCode.cstr(); lengths[stringCnt] = segments.fVSCode.length(); ++stringCnt; #if PRINT_SHADERS GrPrintf("%s%s%s%s\n", segments.fVSUnis.cstr(), segments.fVSAttrs.cstr(), segments.fVaryings.cstr(), segments.fVSCode.cstr()); #endif programData->fVShaderID = CompileShader(GR_GL_VERTEX_SHADER, stringCnt, strings, lengths); stringCnt = 0; if (strlen(GrShaderPrecision()) > 1) { strings[stringCnt] = GrShaderPrecision(); lengths[stringCnt] = strlen(GrShaderPrecision()); ++stringCnt; } if (segments.fFSUnis.length()) { strings[stringCnt] = segments.fFSUnis.cstr(); lengths[stringCnt] = segments.fFSUnis.length(); ++stringCnt; } if (segments.fVaryings.length()) { strings[stringCnt] = segments.fVaryings.cstr(); lengths[stringCnt] = segments.fVaryings.length(); ++stringCnt; } GrAssert(segments.fFSCode.length()); strings[stringCnt] = segments.fFSCode.cstr(); lengths[stringCnt] = segments.fFSCode.length(); ++stringCnt; #if PRINT_SHADERS GrPrintf("%s%s%s%s\n", GR_SHADER_PRECISION, segments.fFSUnis.cstr(), segments.fVaryings.cstr(), segments.fFSCode.cstr()); #endif programData->fFShaderID = CompileShader(GR_GL_FRAGMENT_SHADER, stringCnt, strings, lengths); programData->fProgramID = GR_GL(CreateProgram()); const GrGLint& progID = programData->fProgramID; GR_GL(AttachShader(progID, programData->fVShaderID)); GR_GL(AttachShader(progID, programData->fFShaderID)); // Bind the attrib locations to same values for all shaders GR_GL(BindAttribLocation(progID, POS_ATTR_LOCATION, POS_ATTR_NAME)); for (int t = 0; t < GrDrawTarget::kMaxTexCoords; ++t) { if (texCoordAttrs[t].length()) { GR_GL(BindAttribLocation(progID, TEX_ATTR_LOCATION(t), texCoordAttrs[t].cstr())); } } #if ATTRIBUTE_MATRIX // set unis to a bogus value so that checks against -1 before // flushing will pass. GR_GL(BindAttribLocation(progID, VIEWMAT_ATTR_LOCATION, VIEW_MATRIX_NAME)); program->fUniLocations.fViewMatrixUni = BOGUS_MATRIX_UNI_LOCATION; for (int s = 0; s < kNumStages; ++s) { if (fProgramDesc.fStages[s].fEnabled) { GrStringBuilder matName; tex_matrix_name(s, &matName); GR_GL(BindAttribLocation(progID, TEXMAT_ATTR_LOCATION(s), matName.cstr())); program->fUniLocations.fStages[s].fTextureMatrixUni = BOGUS_MATRIX_UNI_LOCATION; } } #endif GR_GL(BindAttribLocation(progID, COL_ATTR_LOCATION, COL_ATTR_NAME)); GR_GL(LinkProgram(progID)); GrGLint linked = GR_GL_INIT_ZERO; GR_GL(GetProgramiv(progID, GR_GL_LINK_STATUS, &linked)); if (!linked) { GrGLint infoLen = GR_GL_INIT_ZERO; GR_GL(GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen)); GrAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger if (infoLen > 0) { GR_GL(GetProgramInfoLog(progID, infoLen+1, NULL, (char*)log.get())); GrPrintf((char*)log.get()); } GrAssert(!"Error linking program"); GR_GL(DeleteProgram(progID)); programData->fProgramID = 0; return; } // Get uniform locations #if !ATTRIBUTE_MATRIX programData->fUniLocations.fViewMatrixUni = GR_GL(GetUniformLocation(progID, VIEW_MATRIX_NAME)); GrAssert(-1 != programData->fUniLocations.fViewMatrixUni); #endif for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { StageUniLocations& locations = programData->fUniLocations.fStages[s]; if (fProgramDesc.fStages[s].fEnabled) { #if !ATTRIBUTE_MATRIX if (locations.fTextureMatrixUni) { GrTokenString texMName; tex_matrix_name(s, &texMName); locations.fTextureMatrixUni = GR_GL(GetUniformLocation( progID, texMName.cstr())); GrAssert(-1 != locations.fTextureMatrixUni); } else { locations.fTextureMatrixUni = -1; } #endif if (locations.fSamplerUni) { GrTokenString samplerName; sampler_name(s, &samplerName); locations.fSamplerUni = GR_GL(GetUniformLocation( progID, samplerName.cstr())); GrAssert(-1 != locations.fSamplerUni); } else { locations.fSamplerUni = -1; } if (locations.fRadial2Uni) { GrTokenString radial2ParamName; radial2_param_name(s, &radial2ParamName); locations.fRadial2Uni = GR_GL(GetUniformLocation( progID, radial2ParamName.cstr())); GrAssert(-1 != locations.fRadial2Uni); } else { locations.fRadial2Uni = -1; } } else { locations.fSamplerUni = -1; locations.fRadial2Uni = -1; locations.fTextureMatrixUni = -1; } } GR_GL(UseProgram(progID)); // init sampler unis and set bogus values for state tracking for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { if (-1 != programData->fUniLocations.fStages[s].fSamplerUni) { GR_GL(Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s)); } programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix(); programData->fRadial2CenterX1[s] = GR_ScalarMax; programData->fRadial2Radius0[s] = -GR_ScalarMax; } programData->fViewMatrix = GrMatrix::InvalidMatrix(); }
void COGL_FragmentProgramCombiner::GenerateCombinerSettingConstants(int index) { OGLShaderCombinerSaveType &prog = m_vCompiledShaders[index]; UseProgram(prog.programID); float *pf; if(prog.EnvColorLocation != -1) { pf = GetEnvColorfv(); if (memcmp(pf, prog.EnvColors, sizeof(prog.EnvColors))) { memcpy(prog.EnvColors, pf, sizeof(prog.EnvColors)); glUniform4fv(prog.EnvColorLocation, 1, pf); OPENGL_CHECK_ERRORS; } } if(prog.PrimColorLocation != -1) { pf = GetPrimitiveColorfv(); if (memcmp(pf, prog.PrimColors, sizeof(prog.PrimColors))) { memcpy(prog.PrimColors, pf, sizeof(prog.PrimColors)); glUniform4fv(prog.PrimColorLocation, 1, pf); OPENGL_CHECK_ERRORS; } } if(prog.EnvFracLocation != -1) { // avoid slow float compare.. if( *(int *)&gRDP.LODFrac != *(int *)&prog.EnvLODFrac ) { prog.EnvLODFrac = gRDP.LODFrac; float frac = gRDP.LODFrac / 255.0f; float tempf[4] = {frac,frac,frac,frac}; glUniform4fv(prog.EnvFracLocation, 1, tempf); OPENGL_CHECK_ERRORS; } } if(prog.PrimFracLocation != -1) { if( *(int *)&gRDP.primLODFrac != *(int *)&prog.PrimLODFrac ) { prog.PrimLODFrac = gRDP.primLODFrac; float frac2 = gRDP.primLODFrac / 255.0f; float tempf2[4] = {frac2,frac2,frac2,frac2}; glUniform4fv(prog.PrimFracLocation, 1, tempf2); OPENGL_CHECK_ERRORS; } } if(prog.FogColorLocation != -1) { pf = &gRDP.fvFogColor[0]; if (memcmp(pf, prog.FogColors, sizeof(prog.FogColors))) { memcpy(prog.FogColors, pf, sizeof(prog.FogColors)); glUniform4fv(prog.FogColorLocation, 1, pf); OPENGL_CHECK_ERRORS; } } if(prog.FogMinMaxLocation != -1) { if( gRSPfFogMin != prog.FogMin || gRSPfFogMax != prog.FogMax ) { prog.FogMin = gRSPfFogMin; prog.FogMax = gRSPfFogMax; glUniform2f(prog.FogMinMaxLocation,gRSPfFogMin,gRSPfFogMax); OPENGL_CHECK_ERRORS; } } if(prog.AlphaRefLocation != -1) { if( m_AlphaRef != prog.AlphaRef ) { prog.AlphaRef = m_AlphaRef; glUniform1f(prog.AlphaRefLocation, m_AlphaRef); OPENGL_CHECK_ERRORS; } } }
GL_APICALL void GL_APIENTRY glUseProgram (GLuint program) { CONTEXT_EXEC(UseProgram(program)); }
int COGL_FragmentProgramCombiner::ParseDecodedMux() { if( !m_bFragmentProgramIsSupported ) return COGLColorCombiner4::ParseDecodedMux(); OGLShaderCombinerSaveType res; GLint success; if(vertexProgram == 9999) { vertexProgram = res.vertexShaderID = glCreateShader(GL_VERTEX_SHADER); glShaderSource(res.vertexShaderID, 1, &vertexShader,NULL); OPENGL_CHECK_ERRORS; glCompileShader(res.vertexShaderID); OPENGL_CHECK_ERRORS; } else { res.vertexShaderID = vertexProgram; } //Create 4 shaders, with and without alphatest + with and without fog GenerateProgramStr(); for(int alphaTest = 0;alphaTest < 2;alphaTest++) { for(int fog = 0;fog < 2;fog++) { res.fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); char* tmpShader = (char*)malloc(sizeof(char) * 4096); strcpy(tmpShader,"#version " GLSL_VERSION "\n"); if(alphaTest == 1) { strcat(tmpShader,"#define ALPHA_TEST\n"); } if(fog == 1) { strcat(tmpShader,"#define FOG\n"); } res.fogIsUsed = fog == 1; res.alphaTest = alphaTest == 1; strcat(tmpShader,oglNewFP); glShaderSource(res.fragmentShaderID, 1,(const char**) &tmpShader,NULL); free(tmpShader); OPENGL_CHECK_ERRORS; glCompileShader(res.fragmentShaderID); glGetShaderiv(res.fragmentShaderID, GL_COMPILE_STATUS, &success); if (!success) { char Log[1024]; GLint nLength; glGetShaderInfoLog(res.fragmentShaderID, 1024, &nLength, Log); printf("Error compiling shader!\n %s",oglNewFP); printf("%s", Log); } res.programID = glCreateProgram(); glAttachShader(res.programID,res.vertexShaderID); glAttachShader(res.programID,res.fragmentShaderID); //Bind Attributes glBindAttribLocation(res.programID,VS_COLOR,"aColor"); OPENGL_CHECK_ERRORS; glBindAttribLocation(res.programID,VS_TEXCOORD0,"aTexCoord0"); OPENGL_CHECK_ERRORS; glBindAttribLocation(res.programID,VS_TEXCOORD1,"aTexCoord1"); OPENGL_CHECK_ERRORS; glBindAttribLocation(res.programID,VS_POSITION,"aPosition"); OPENGL_CHECK_ERRORS; glBindAttribLocation(res.programID,VS_FOG,"aFogCoord"); OPENGL_CHECK_ERRORS; glLinkProgram(res.programID); OPENGL_CHECK_ERRORS; glGetProgramiv(res.programID, GL_LINK_STATUS, &success); if (!success) { char Log[1024]; GLint nLength; glGetShaderInfoLog(res.fragmentShaderID, 1024, &nLength, Log); printf("Error linking program!\n"); printf("%s\n",Log); } UseProgram(res.programID); OPENGL_CHECK_ERRORS; //Bind texture samplers GLint tex0 = glGetUniformLocation(res.programID,"uTex0"); GLint tex1 = glGetUniformLocation(res.programID,"uTex1"); if(tex0 != -1) glUniform1i(tex0,0); if(tex1 != -1) glUniform1i(tex1,1); //Bind Uniforms res.PrimColorLocation = glGetUniformLocation(res.programID,"PrimColor"); OPENGL_CHECK_ERRORS; res.EnvColorLocation = glGetUniformLocation(res.programID,"EnvColor"); OPENGL_CHECK_ERRORS; res.PrimFracLocation = glGetUniformLocation(res.programID,"PrimFrac"); OPENGL_CHECK_ERRORS; res.EnvFracLocation = glGetUniformLocation(res.programID,"EnvFrac"); OPENGL_CHECK_ERRORS; res.AlphaRefLocation = glGetUniformLocation(res.programID,"AlphaRef"); OPENGL_CHECK_ERRORS; res.FogColorLocation = glGetUniformLocation(res.programID,"FogColor"); OPENGL_CHECK_ERRORS; res.FogMinMaxLocation = glGetUniformLocation(res.programID,"FogMinMax"); OPENGL_CHECK_ERRORS; res.dwMux0 = m_pDecodedMux->m_dwMux0; res.dwMux1 = m_pDecodedMux->m_dwMux1; m_vCompiledShaders.push_back(res); } } m_lastIndex = m_vCompiledShaders.size()-4; return m_lastIndex; }
void COGL_FragmentProgramCombiner::InitCombinerCycleFill(void) { UseProgram(fillProgram); glUniform4f(fillColorLocation,((gRDP.fillColor>>16)&0xFF)/255.0f,((gRDP.fillColor>>8)&0xFF)/255.0f,((gRDP.fillColor)&0xFF)/255.0f,((gRDP.fillColor>>24)&0xFF)/255.0f); OPENGL_CHECK_ERRORS; }
bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) { if (!flushGLStateCommon(type)) { return false; } if (fDirtyFlags.fRenderTargetChanged) { // our coords are in pixel space and the GL matrices map to NDC // so if the viewport changed, our matrix is now wrong. #if ATTRIBUTE_MATRIX fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix(); #else // we assume all shader matrices may be wrong after viewport changes fProgramCache->invalidateViewMatrices(); #endif } if (fGeometrySrc.fVertexLayout & kColor_VertexLayoutBit) { // invalidate the immediate mode color fHWDrawState.fColor = GrColor_ILLEGAL; } else { if (fHWDrawState.fColor != fCurrDrawState.fColor) { // OpenGL ES only supports the float varities of glVertexAttrib float c[] = { GrColorUnpackR(fCurrDrawState.fColor) / 255.f, GrColorUnpackG(fCurrDrawState.fColor) / 255.f, GrColorUnpackB(fCurrDrawState.fColor) / 255.f, GrColorUnpackA(fCurrDrawState.fColor) / 255.f }; GR_GL(VertexAttrib4fv(COL_ATTR_LOCATION, c)); fHWDrawState.fColor = fCurrDrawState.fColor; } } buildProgram(type); fProgramData = fProgramCache->getProgramData(fCurrentProgram, this); if (fHWProgramID != fProgramData->fProgramID) { GR_GL(UseProgram(fProgramData->fProgramID)); fHWProgramID = fProgramData->fProgramID; } if (!fCurrentProgram.doGLSetup(type, fProgramData)) { return false; } #if ATTRIBUTE_MATRIX GrMatrix& currViewMatrix = fHWDrawState.fViewMatrix; #else GrMatrix& currViewMatrix = fProgramData->fViewMatrix; #endif if (currViewMatrix != fCurrDrawState.fViewMatrix) { flushViewMatrix(); currViewMatrix = fCurrDrawState.fViewMatrix; } for (int s = 0; s < kNumStages; ++s) { GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; if (NULL != texture) { if (-1 != fProgramData->fUniLocations.fStages[s].fTextureMatrixUni && (((1 << s) & fDirtyFlags.fTextureChangedMask) || getHWSamplerMatrix(s) != getSamplerMatrix(s))) { flushTextureMatrix(s); recordHWSamplerMatrix(s, getSamplerMatrix(s)); } } const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s]; if (-1 != fProgramData->fUniLocations.fStages[s].fRadial2Uni && (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() || fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() || fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) { flushRadial2(s); fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1(); fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0(); fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot(); } } resetDirtyFlags(); return true; }
void GLGpuPosInstancedArraysBench::setup(const GrGLInterface* gl) { setup_framebuffer(gl, kScreenWidth, kScreenHeight); // compile and use shaders GrGLint shaderProgram = compile_shader(gl, gpu_vertex_shader, fragment_shader); // translations int index = 0; GrGLfloat viewMatrices[fNumQuads * fSkMatrixNumCells]; setup_matrices(fNumQuads, [&index, &viewMatrices](const SkMatrix& m) { GrGLGetMatrix<3>(&viewMatrices[index], m); index += fSkMatrixNumCells; }); // Constants for our various shader programs GrGLfloat quad_vertices[] = { // Positions // Colors -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f }; // update vertex data GrGLuint quadVAO, quadVBO; GR_GL_CALL(gl, GenVertexArrays(1, &quadVAO)); GR_GL_CALL(gl, GenBuffers(1, &quadVBO)); GR_GL_CALL(gl, BindVertexArray(quadVAO)); GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, quadVBO)); GR_GL_CALL(gl, EnableVertexAttribArray(0)); GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, 5 * sizeof(GrGLfloat), (GrGLvoid*)0)); GR_GL_CALL(gl, EnableVertexAttribArray(1)); GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, 5 * sizeof(GrGLfloat), (GrGLvoid*)(2 * sizeof(GrGLfloat)))); GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(quad_vertices), quad_vertices, GR_GL_STATIC_DRAW)); // Also set instance data GrGLuint instanceVBO; GR_GL_CALL(gl, GenBuffers(1, &instanceVBO)); GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, instanceVBO)); GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(GrGLfloat) * fSkMatrixNumCells * fNumQuads, &viewMatrices[0], GR_GL_STATIC_DRAW)); GR_GL_CALL(gl, EnableVertexAttribArray(2)); GR_GL_CALL(gl, EnableVertexAttribArray(3)); GR_GL_CALL(gl, EnableVertexAttribArray(4)); GR_GL_CALL(gl, VertexAttribPointer(2, 3, GR_GL_FLOAT, GR_GL_FALSE, 9 * sizeof(GrGLfloat), (GrGLvoid*)0)); GR_GL_CALL(gl, VertexAttribPointer(3, 3, GR_GL_FLOAT, GR_GL_FALSE, 9 * sizeof(GrGLfloat), (GrGLvoid*)(3 * sizeof(GrGLfloat)))); GR_GL_CALL(gl, VertexAttribPointer(4, 3, GR_GL_FLOAT, GR_GL_FALSE, 9 * sizeof(GrGLfloat), (GrGLvoid*)(6 * sizeof(GrGLfloat)))); GR_GL_CALL(gl, VertexAttribDivisor(2, 1)); GR_GL_CALL(gl, VertexAttribDivisor(3, 1)); GR_GL_CALL(gl, VertexAttribDivisor(4, 1)); // draw GR_GL_CALL(gl, ClearColor(0.03f, 0.03f, 0.03f, 1.0f)); GR_GL_CALL(gl, Clear(GR_GL_COLOR_BUFFER_BIT)); // set us up to draw GR_GL_CALL(gl, UseProgram(shaderProgram)); GR_GL_CALL(gl, BindVertexArray(quadVAO)); }
void GLShader::Uniform1f(const GLchar* var_name, GLfloat v) { GLint Location = glGetUniformLocation(_shader_prog, var_name); UseProgram(); glUniform1f(Location, v); }