SK_DEFINE_INST_COUNT(GrGLProgram)
+#define GL_CALL(X) GR_GL_CALL(fContextInfo.interface(), X)
+#define GL_CALL_RET(R, X) GR_GL_CALL_RET(fContextInfo.interface(), R, X)
+
namespace {
enum {
}
}
-GrGLProgram::GrGLProgram() {
+GrGLProgram* GrGLProgram::Create(const GrGLContextInfo& gl,
+ const Desc& desc,
+ GrCustomStage** customStages) {
+ GrGLProgram* program = SkNEW_ARGS(GrGLProgram, (gl, desc, customStages));
+ if (!program->succeeded()) {
+ delete program;
+ program = NULL;
+ }
+ return program;
+}
+
+GrGLProgram::GrGLProgram(const GrGLContextInfo& gl,
+ const Desc& desc,
+ GrCustomStage** customStages) : fContextInfo(gl) {
+ fDesc = desc;
+ fVShaderID = 0;
+ fGShaderID = 0;
+ fFShaderID = 0;
+ fProgramID = 0;
+ for (int s = 0; s < GrDrawState::kNumStages; ++s) {
+ fProgramStage[s] = NULL;
+ }
+ this->genProgram(customStages);
}
GrGLProgram::~GrGLProgram() {
+ if (fVShaderID) {
+ GL_CALL(DeleteShader(fVShaderID));
+ }
+ if (fGShaderID) {
+ GL_CALL(DeleteShader(fGShaderID));
+ }
+ if (fFShaderID) {
+ GL_CALL(DeleteShader(fFShaderID));
+ }
+ if (fProgramID) {
+ GL_CALL(DeleteProgram(fProgramID));
+ }
+
for (int i = 0; i < GrDrawState::kNumStages; ++i) {
delete fProgramStage[i];
}
}
+void GrGLProgram::abandon() {
+ fVShaderID = 0;
+ fGShaderID = 0;
+ fFShaderID = 0;
+ fProgramID = 0;
+}
+
void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff,
GrBlendCoeff* dstCoeff) const {
switch (fDesc.fDualSrcOutput) {
fsCode->appendf("\t%s.rgb *= %s.a;\n", outputVar, outputVar);
}
-void GrGLProgram::genEdgeCoverage(const GrGLContextInfo& gl,
- SkString* coverageVar,
+void GrGLProgram::genEdgeCoverage(SkString* coverageVar,
GrGLShaderBuilder* segments) const {
if (fDesc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit) {
const char *vsName, *fsName;
segments->fFSCode.appendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
segments->fFSCode.append("\t\tedgeAlpha = clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n"
"\t}\n");
- if (kES2_GrGLBinding == gl.binding()) {
+ if (kES2_GrGLBinding == fContextInfo.binding()) {
segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
}
break;
segments->fFSCode.appendf("\tfloat edgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
segments->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n");
segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
- if (kES2_GrGLBinding == gl.binding()) {
+ if (kES2_GrGLBinding == fContextInfo.binding()) {
segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
}
break;
}
}
-void GrGLProgram::genGeometryShader(const GrGLContextInfo& gl,
- GrGLShaderBuilder* segments) const {
+void GrGLProgram::genGeometryShader(GrGLShaderBuilder* segments) const {
#if GR_GL_EXPERIMENTAL_GS
if (fDesc.fExperimentalGS) {
- GrAssert(gl.glslGeneration() >= k150_GrGLSLGeneration);
+ GrAssert(fContextInfo.glslGeneration() >= k150_GrGLSLGeneration);
segments->fGSHeader.append("layout(triangles) in;\n"
"layout(triangle_strip, max_vertices = 6) out;\n");
segments->fGSCode.append("void main() {\n"
}
namespace {
-#define GL_CALL(X) GR_GL_CALL(gl.interface(), X)
-#define GL_CALL_RET(R, X) GR_GL_CALL_RET(gl.interface(), R, X)
-
// prints a shader using params similar to glShaderSource
void print_shader(GrGLint stringCnt,
const GrGLchar** strings,
"stringCount", SkStringPrintf("%i", stringCnt).c_str());
GrGLuint shader;
- GL_CALL_RET(shader, CreateShader(type));
+ GR_GL_CALL_RET(gl.interface(), shader, CreateShader(type));
if (0 == shader) {
return 0;
}
+ const GrGLInterface* gli = gl.interface();
GrGLint compiled = GR_GL_INIT_ZERO;
- GL_CALL(ShaderSource(shader, stringCnt, strings, stringLengths));
- GL_CALL(CompileShader(shader));
- GL_CALL(GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
+ GR_GL_CALL(gli, ShaderSource(shader, stringCnt, strings, stringLengths));
+ GR_GL_CALL(gli, CompileShader(shader));
+ GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
if (!compiled) {
GrGLint infoLen = GR_GL_INIT_ZERO;
- GL_CALL(GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
+ GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
if (infoLen > 0) {
// retrieve length even though we don't need it to workaround bug in chrome cmd buffer
// param validation.
GrGLsizei length = GR_GL_INIT_ZERO;
- GL_CALL(GetShaderInfoLog(shader, infoLen+1,
- &length, (char*)log.get()));
+ GR_GL_CALL(gli, GetShaderInfoLog(shader, infoLen+1,
+ &length, (char*)log.get()));
print_shader(stringCnt, strings, stringLengths);
GrPrintf("\n%s", log.get());
}
GrAssert(!"Shader compilation failed!");
- GL_CALL(DeleteShader(shader));
+ GR_GL_CALL(gli, DeleteShader(shader));
return 0;
}
return shader;
}
// compiles all the shaders from builder and stores the shader IDs
-bool GrGLProgram::compileShaders(const GrGLContextInfo& gl,
- const GrGLShaderBuilder& builder) {
+bool GrGLProgram::compileShaders(const GrGLShaderBuilder& builder) {
SkString shader;
GrPrintf(shader.c_str());
GrPrintf("\n");
#endif
- if (!(fVShaderID = compile_shader(gl, GR_GL_VERTEX_SHADER, shader))) {
+ if (!(fVShaderID = compile_shader(fContextInfo, GR_GL_VERTEX_SHADER, shader))) {
return false;
}
GrPrintf(shader.c_str());
GrPrintf("\n");
#endif
- if (!(fGShaderID = compile_shader(gl, GR_GL_GEOMETRY_SHADER, shader))) {
+ if (!(fGShaderID = compile_shader(fContextInfo, GR_GL_GEOMETRY_SHADER, shader))) {
return false;
}
} else {
GrPrintf(shader.c_str());
GrPrintf("\n");
#endif
- if (!(fFShaderID = compile_shader(gl, GR_GL_FRAGMENT_SHADER, shader))) {
+ if (!(fFShaderID = compile_shader(fContextInfo, GR_GL_FRAGMENT_SHADER, shader))) {
return false;
}
}
-bool GrGLProgram::genProgram(const GrGLContextInfo& gl,
- const Desc& desc,
- GrCustomStage** customStages) {
- fDesc = desc;
- GrGLShaderBuilder builder(gl);
+bool GrGLProgram::genProgram(GrCustomStage** customStages) {
+ GrAssert(0 == fProgramID);
+
+ GrGLShaderBuilder builder(fContextInfo);
const uint32_t& layout = fDesc.fVertexLayout;
fUniLocations.reset();
// the dual source output has no canonical var name, have to
// declare an output, which is incompatible with gl_FragColor/gl_FragData.
bool dualSourceOutputWritten = false;
- builder.fHeader.append(GrGetGLSLVersionDecl(gl.binding(),
- gl.glslGeneration()));
+ builder.fHeader.append(GrGetGLSLVersionDecl(fContextInfo.binding(),
+ fContextInfo.glslGeneration()));
GrGLShaderVar colorOutput;
- bool isColorDeclared = GrGLSLSetupFSColorOuput(gl.glslGeneration(),
+ bool isColorDeclared = GrGLSLSetupFSColorOuput(fContextInfo.glslGeneration(),
declared_color_output_name(),
&colorOutput);
if (isColorDeclared) {
}
}
- ///////////////////////////////////////////////////////////////////////////
- // We need to convert generic effect representations to GL-specific
- // backends so they can be accesseed in genStageCode() and in subsequent,
- // uses of programData, but it's safest to do so below when we're *sure*
- // we need them.
- for (int s = 0; s < GrDrawState::kNumStages; ++s) {
- fProgramStage[s] = NULL;
- }
-
///////////////////////////////////////////////////////////////////////////
// compute the final color
const GrProgramStageFactory& factory = customStages[s]->getFactory();
fProgramStage[s] = factory.createGLInstance(*customStages[s]);
}
- this->genStageCode(gl,
- s,
+ this->genStageCode(s,
inColor.size() ? inColor.c_str() : NULL,
outColor.c_str(),
inCoords,
if (!wroteFragColorZero || Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) {
if (!coverageIsZero) {
- this->genEdgeCoverage(gl, &inCoverage, &builder);
+ this->genEdgeCoverage(&inCoverage, &builder);
switch (fDesc.fCoverageInput) {
case Desc::kSolidWhite_ColorInput:
const GrProgramStageFactory& factory = customStages[s]->getFactory();
fProgramStage[s] = factory.createGLInstance(*customStages[s]);
}
- this->genStageCode(gl,
- s,
+ this->genStageCode(s,
inCoverage.size() ? inCoverage.c_str() : NULL,
outCoverage.c_str(),
inCoords,
///////////////////////////////////////////////////////////////////////////
// insert GS
#if GR_DEBUG
- this->genGeometryShader(gl, &builder);
+ this->genGeometryShader(&builder);
#endif
///////////////////////////////////////////////////////////////////////////
// compile and setup attribs and unis
- if (!this->compileShaders(gl, builder)) {
+ if (!this->compileShaders(builder)) {
return false;
}
- if (!this->bindOutputsAttribsAndLinkProgram(gl,
- texCoordAttrs,
+ if (!this->bindOutputsAttribsAndLinkProgram(texCoordAttrs,
isColorDeclared,
dualSourceOutputWritten)) {
return false;
}
- this->getUniformLocationsAndInitCache(gl, builder);
+ this->getUniformLocationsAndInitCache(builder);
return true;
}
-bool GrGLProgram::bindOutputsAttribsAndLinkProgram(const GrGLContextInfo& gl,
- SkString texCoordAttrNames[],
+bool GrGLProgram::bindOutputsAttribsAndLinkProgram(SkString texCoordAttrNames[],
bool bindColorOut,
bool bindDualSrcOut) {
GL_CALL_RET(fProgramID, CreateProgram());
return true;
}
-void GrGLProgram::getUniformLocationsAndInitCache(const GrGLContextInfo& gl,
- const GrGLShaderBuilder& builder) {
+void GrGLProgram::getUniformLocationsAndInitCache(const GrGLShaderBuilder& builder) {
if (kUseUniform == fUniLocations.fViewMatrixUni) {
GL_CALL_RET(fUniLocations.fViewMatrixUni, GetUniformLocation(fProgramID, VIEW_MATRIX_NAME));
}
if (NULL != fProgramStage[s]) {
- fProgramStage[s]->initUniforms(&builder, gl.interface(), fProgramID);
+ fProgramStage[s]->initUniforms(&builder, fContextInfo.interface(), fProgramID);
}
}
}
///////////////////////////////////////////////////////////////////////////////
// Stage code generation
-void GrGLProgram::genStageCode(const GrGLContextInfo& gl,
- int stageNum,
+void GrGLProgram::genStageCode(int stageNum,
const char* fsInColor, // NULL means no incoming color
const char* fsOutColor,
const char* vsInCoord,
struct Desc;
- GrGLProgram();
+ static GrGLProgram* Create(const GrGLContextInfo& gl,
+ const Desc& desc,
+ GrCustomStage** customStages);
+
virtual ~GrGLProgram();
+ /** Call to abandon GL objects owned by this program */
+ void abandon();
+
/**
- * This is the heavy initilization routine for building a GLProgram.
+ * The shader may modify the blend coeffecients. Params are in/out
*/
- bool genProgram(const GrGLContextInfo& gl,
- const Desc& desc,
- GrCustomStage** customStages);
+ void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
- /**
- * The shader may modify the blend coeffecients. Params are in/out
- */
- void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
-
- const Desc& getDesc() { return fDesc; }
+ const Desc& getDesc() { return fDesc; }
/**
* Attribute indices. These should not overlap. Matrices consume 3 slots.
typedef Desc::StageDesc StageDesc;
private:
+ GrGLProgram(const GrGLContextInfo& gl,
+ const Desc& desc,
+ GrCustomStage** customStages);
+
+ bool succeeded() const { return 0 != fProgramID; }
+
+ /**
+ * This is the heavy initilization routine for building a GLProgram.
+ */
+ bool genProgram(GrCustomStage** customStages);
+
void genInputColor(GrGLShaderBuilder* builder, SkString* inColor);
// Determines which uniforms will need to be bound.
- void genStageCode(const GrGLContextInfo& gl,
- int stageNum,
+ void genStageCode(int stageNum,
const char* fsInColor, // NULL means no incoming color
const char* fsOutColor,
const char* vsInCoord,
GrGLShaderBuilder* builder);
- void genGeometryShader(const GrGLContextInfo& gl, GrGLShaderBuilder* segments) const;
+ void genGeometryShader(GrGLShaderBuilder* segments) const;
void genUniformCoverage(GrGLShaderBuilder* segments, SkString* inOutCoverage);
// generates code to compute coverage based on edge AA.
- void genEdgeCoverage(const GrGLContextInfo& gl,
- SkString* coverageVar,
- GrGLShaderBuilder* builder) const;
+ void genEdgeCoverage(SkString* coverageVar, GrGLShaderBuilder* builder) const;
// Creates a GL program ID, binds shader attributes to GL vertex attrs, and links the program
- bool bindOutputsAttribsAndLinkProgram(const GrGLContextInfo& gl,
- SkString texCoordAttrNames[GrDrawState::kMaxTexCoords],
+ bool bindOutputsAttribsAndLinkProgram(SkString texCoordAttrNames[GrDrawState::kMaxTexCoords],
bool bindColorOut,
bool bindDualSrcOut);
// Binds uniforms; initializes cache to invalid values.
- void getUniformLocationsAndInitCache(const GrGLContextInfo& gl,
- const GrGLShaderBuilder& builder);
+ void getUniformLocationsAndInitCache(const GrGLShaderBuilder& builder);
- bool compileShaders(const GrGLContextInfo& gl, const GrGLShaderBuilder& builder);
+ bool compileShaders(const GrGLShaderBuilder& builder);
const char* adjustInColor(const SkString& inColor) const;
GrGLProgramStage* fProgramStage[GrDrawState::kNumStages];
Desc fDesc;
+ const GrGLContextInfo& fContextInfo;
friend class GrGpuGL; // TODO: remove this by adding getters and moving functionality.