Remove uniform names from top of GrGLProgram.cpp, builder responsible for "u" and stage num tags.

Also add LF eol-style property to some files in src/gpu/effects.

Review URL: http://codereview.appspot.com/6448049/



git-svn-id: http://skia.googlecode.com/svn/trunk@4770 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
bsalomon@google.com 2012-07-25 20:58:20 +00:00
parent 09640d6bac
commit 777c3aab0a
12 changed files with 196 additions and 207 deletions

View File

@ -361,7 +361,7 @@ private:
class GrGLLight {
public:
virtual ~GrGLLight() {}
virtual void setupVariables(GrGLShaderBuilder* builder, int stage);
virtual void setupVariables(GrGLShaderBuilder* builder);
virtual void emitVS(SkString* out) const {}
virtual void emitFuncs(const GrGLShaderBuilder* builder, SkString* out) const {}
virtual void emitSurfaceToLight(const GrGLShaderBuilder*,
@ -384,7 +384,7 @@ protected:
class GrGLDistantLight : public GrGLLight {
public:
virtual ~GrGLDistantLight() {}
virtual void setupVariables(GrGLShaderBuilder* builder, int stage) SK_OVERRIDE;
virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
virtual void setData(const GrGLUniformManager&, const GrRenderTarget* rt, const SkLight* light) const SK_OVERRIDE;
virtual void emitSurfaceToLight(const GrGLShaderBuilder*,
SkString* out,
@ -399,7 +399,7 @@ private:
class GrGLPointLight : public GrGLLight {
public:
virtual ~GrGLPointLight() {}
virtual void setupVariables(GrGLShaderBuilder* builder, int stage) SK_OVERRIDE;
virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
virtual void setData(const GrGLUniformManager&, const GrRenderTarget* rt, const SkLight* light) const SK_OVERRIDE;
virtual void emitVS(SkString* out) const SK_OVERRIDE;
virtual void emitSurfaceToLight(const GrGLShaderBuilder*,
@ -416,7 +416,7 @@ private:
class GrGLSpotLight : public GrGLLight {
public:
virtual ~GrGLSpotLight() {}
virtual void setupVariables(GrGLShaderBuilder* builder, int stage) SK_OVERRIDE;
virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
virtual void setData(const GrGLUniformManager&, const GrRenderTarget* rt, const SkLight* light) const SK_OVERRIDE;
virtual void emitVS(SkString* out) const SK_OVERRIDE;
virtual void emitFuncs(const GrGLShaderBuilder* builder, SkString* out) const;
@ -869,8 +869,7 @@ public:
const GrCustomStage& stage);
virtual ~GrGLLightingEffect();
virtual void setupVariables(GrGLShaderBuilder* builder,
int stage) SK_OVERRIDE;
virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
virtual void emitVS(GrGLShaderBuilder* builder,
const char* vertexCoords) SK_OVERRIDE;
virtual void emitFS(GrGLShaderBuilder* builder,
@ -901,8 +900,7 @@ class GrGLDiffuseLightingEffect : public GrGLLightingEffect {
public:
GrGLDiffuseLightingEffect(const GrProgramStageFactory& factory,
const GrCustomStage& stage);
virtual void setupVariables(GrGLShaderBuilder* builder,
int stage) SK_OVERRIDE;
virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
virtual void emitLightFunc(const GrGLShaderBuilder*, SkString* funcs) SK_OVERRIDE;
virtual void setData(const GrGLUniformManager&,
const GrCustomStage&,
@ -921,8 +919,7 @@ class GrGLSpecularLightingEffect : public GrGLLightingEffect {
public:
GrGLSpecularLightingEffect(const GrProgramStageFactory& factory,
const GrCustomStage& stage);
virtual void setupVariables(GrGLShaderBuilder* builder,
int stage) SK_OVERRIDE;
virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
virtual void emitLightFunc(const GrGLShaderBuilder*, SkString* funcs) SK_OVERRIDE;
virtual void setData(const GrGLUniformManager&,
const GrCustomStage&,
@ -989,14 +986,14 @@ GrGLLightingEffect::~GrGLLightingEffect() {
delete fLight;
}
void GrGLLightingEffect::setupVariables(GrGLShaderBuilder* builder, int stage) {
void GrGLLightingEffect::setupVariables(GrGLShaderBuilder* builder) {
fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec2f_GrSLType,
"uImageIncrement", stage);
"ImageIncrement");
fSurfaceScaleUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType,
"uSurfaceScale", stage);
fLight->setupVariables(builder, stage);
"SurfaceScale");
fLight->setupVariables(builder);
}
void GrGLLightingEffect::emitVS(GrGLShaderBuilder* builder,
@ -1078,10 +1075,9 @@ GrGLDiffuseLightingEffect::GrGLDiffuseLightingEffect(const GrProgramStageFactory
, fKDUni(kInvalidUniformHandle) {
}
void GrGLDiffuseLightingEffect::setupVariables(GrGLShaderBuilder* builder, int stage) {
INHERITED::setupVariables(builder, stage);
fKDUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, kFloat_GrSLType, "uKD",
stage);
void GrGLDiffuseLightingEffect::setupVariables(GrGLShaderBuilder* builder) {
INHERITED::setupVariables(builder);
fKDUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, kFloat_GrSLType, "KD");
}
void GrGLDiffuseLightingEffect::emitLightFunc(const GrGLShaderBuilder* builder, SkString* funcs) {
@ -1131,12 +1127,12 @@ GrGLSpecularLightingEffect::GrGLSpecularLightingEffect(const GrProgramStageFacto
, fShininessUni(kInvalidUniformHandle) {
}
void GrGLSpecularLightingEffect::setupVariables(GrGLShaderBuilder* builder, int stage) {
INHERITED::setupVariables(builder, stage);
void GrGLSpecularLightingEffect::setupVariables(GrGLShaderBuilder* builder) {
INHERITED::setupVariables(builder);
fKSUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType, "uKS", stage);
kFloat_GrSLType, "KS");
fShininessUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType, "uShininess", stage);
kFloat_GrSLType, "Shininess");
}
void GrGLSpecularLightingEffect::emitLightFunc(const GrGLShaderBuilder* builder, SkString* funcs) {
@ -1169,9 +1165,9 @@ void GrGLLight::emitLightColor(const GrGLShaderBuilder* builder,
out->append(color);
}
void GrGLLight::setupVariables(GrGLShaderBuilder* builder, int stage) {
void GrGLLight::setupVariables(GrGLShaderBuilder* builder) {
fColorUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec3f_GrSLType, "uLightColor", stage);
kVec3f_GrSLType, "LightColor");
}
void GrGLLight::setData(const GrGLUniformManager& uman,
@ -1186,10 +1182,10 @@ GrGLLight* SkDistantLight::createGLLight() const {
///////////////////////////////////////////////////////////////////////////////
void GrGLDistantLight::setupVariables(GrGLShaderBuilder* builder, int stage) {
INHERITED::setupVariables(builder, stage);
void GrGLDistantLight::setupVariables(GrGLShaderBuilder* builder) {
INHERITED::setupVariables(builder);
fDirectionUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, kVec3f_GrSLType,
"uLightDirection", stage);
"LightDirection");
}
void GrGLDistantLight::setData(const GrGLUniformManager& uman,
@ -1210,10 +1206,10 @@ void GrGLDistantLight::emitSurfaceToLight(const GrGLShaderBuilder* builder,
///////////////////////////////////////////////////////////////////////////////
void GrGLPointLight::setupVariables(GrGLShaderBuilder* builder, int stage) {
INHERITED::setupVariables(builder, stage);
void GrGLPointLight::setupVariables(GrGLShaderBuilder* builder) {
INHERITED::setupVariables(builder);
fLocationUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, kVec3f_GrSLType,
"uLightLocation", stage);
"LightLocation");
}
void GrGLPointLight::setData(const GrGLUniformManager& uman,
@ -1237,20 +1233,20 @@ void GrGLPointLight::emitSurfaceToLight(const GrGLShaderBuilder* builder,
///////////////////////////////////////////////////////////////////////////////
void GrGLSpotLight::setupVariables(GrGLShaderBuilder* builder, int stage) {
INHERITED::setupVariables(builder, stage);
void GrGLSpotLight::setupVariables(GrGLShaderBuilder* builder) {
INHERITED::setupVariables(builder);
fLocationUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec3f_GrSLType, "uLightLocation", stage);
kVec3f_GrSLType, "LightLocation");
fExponentUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType, "uExponent", stage);
kFloat_GrSLType, "Exponent");
fCosInnerConeAngleUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType, "uCosInnerConeAngle", stage);
kFloat_GrSLType, "CosInnerConeAngle");
fCosOuterConeAngleUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType, "uCosOuterConeAngle", stage);
kFloat_GrSLType, "CosOuterConeAngle");
fConeScaleUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType, "uConeScale", stage);
kFloat_GrSLType, "ConeScale");
fSUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec3f_GrSLType, "uS", stage);
kVec3f_GrSLType, "S");
}
void GrGLSpotLight::setData(const GrGLUniformManager& uman,

View File

@ -17,8 +17,7 @@ public:
GrGLColorTableEffect(const GrProgramStageFactory& factory,
const GrCustomStage& stage);
virtual void setupVariables(GrGLShaderBuilder* state,
int stage) SK_OVERRIDE {}
virtual void setupVariables(GrGLShaderBuilder* state) SK_OVERRIDE {}
virtual void emitVS(GrGLShaderBuilder* state,
const char* vertexCoords) SK_OVERRIDE {}
virtual void emitFS(GrGLShaderBuilder* state,

View File

@ -20,8 +20,7 @@ public:
GrGLConvolutionEffect(const GrProgramStageFactory& factory,
const GrCustomStage& stage);
virtual void setupVariables(GrGLShaderBuilder* builder,
int stage) SK_OVERRIDE;
virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
virtual void emitVS(GrGLShaderBuilder* builder,
const char* vertexCoords) SK_OVERRIDE;
virtual void emitFS(GrGLShaderBuilder* builder,
@ -56,13 +55,12 @@ GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProgramStageFactory& factor
fRadius = c.radius();
}
void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* builder,
int stage) {
void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* builder) {
fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType |
GrGLShaderBuilder::kVertex_ShaderType,
kVec2f_GrSLType, "uImageIncrement", stage);
fKernelUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType, "uKernel", stage, this->width());
kVec2f_GrSLType, "ImageIncrement");
fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType, "Kernel", this->width());
}
void GrGLConvolutionEffect::emitVS(GrGLShaderBuilder* builder,

View File

@ -214,8 +214,7 @@ public:
const GrCustomStage&);
virtual ~GrGLRadial2Gradient() { }
virtual void setupVariables(GrGLShaderBuilder* builder,
int stage) SK_OVERRIDE;
virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
virtual void emitVS(GrGLShaderBuilder* builder,
const char* vertexCoords) SK_OVERRIDE;
virtual void emitFS(GrGLShaderBuilder* builder,
@ -273,19 +272,19 @@ GrGLRadial2Gradient::GrGLRadial2Gradient(
fIsDegenerate = data.isDegenerate();
}
void GrGLRadial2Gradient::setupVariables(GrGLShaderBuilder* builder, int stage) {
void GrGLRadial2Gradient::setupVariables(GrGLShaderBuilder* builder) {
// 2 copies of uniform array, 1 for each of vertex & fragment shader,
// to work around Xoom bug. Doesn't seem to cause performance decrease
// in test apps, but need to keep an eye on it.
fVSParamUni = builder->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
kFloat_GrSLType, "uRadial2VSParams", stage, 6);
fFSParamUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType, "uRadial2FSParams", stage, 6);
fVSParamUni = builder->addUniformArray(GrGLShaderBuilder::kVertex_ShaderType,
kFloat_GrSLType, "Radial2VSParams", 6);
fFSParamUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType, "Radial2FSParams", 6);
// For radial gradients without perspective we can pass the linear
// part of the quadratic as a varying.
if (builder->fVaryingDims == builder->fCoordDims) {
builder->addVarying(kFloat_GrSLType, "Radial2BCoeff", stage,
builder->addVarying(kFloat_GrSLType, "Radial2BCoeff",
&fVSVaryingName, &fFSVaryingName);
}
}
@ -467,8 +466,7 @@ public:
const GrCustomStage&);
virtual ~GrGLConical2Gradient() { }
virtual void setupVariables(GrGLShaderBuilder* builder,
int stage) SK_OVERRIDE;
virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
virtual void emitVS(GrGLShaderBuilder* builder,
const char* vertexCoords) SK_OVERRIDE;
virtual void emitFS(GrGLShaderBuilder* builder,
@ -528,14 +526,14 @@ GrGLConical2Gradient::GrGLConical2Gradient(
fIsDegenerate = data.isDegenerate();
}
void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* builder, int stage) {
void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* builder) {
// 2 copies of uniform array, 1 for each of vertex & fragment shader,
// to work around Xoom bug. Doesn't seem to cause performance decrease
// in test apps, but need to keep an eye on it.
fVSParamUni = builder->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
kFloat_GrSLType, "uConical2VSParams", stage, 6);
fFSParamUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType, "uConical2FSParams", stage, 6);
fVSParamUni = builder->addUniformArray(GrGLShaderBuilder::kVertex_ShaderType,
kFloat_GrSLType, "Conical2VSParams", 6);
fFSParamUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType, "Conical2FSParams", 6);
fVSParamLocation = GrGLProgramStage::kUseUniform;
fFSParamLocation = GrGLProgramStage::kUseUniform;
@ -543,7 +541,7 @@ void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* builder, int stage)
// For radial gradients without perspective we can pass the linear
// part of the quadratic as a varying.
if (builder->fVaryingDims == builder->fCoordDims) {
builder->addVarying(kFloat_GrSLType, "Conical2BCoeff", stage,
builder->addVarying(kFloat_GrSLType, "Conical2BCoeff",
&fVSVaryingName, &fFSVaryingName);
}
}

View File

@ -18,8 +18,7 @@ public:
GrGLMorphologyEffect (const GrProgramStageFactory& factory,
const GrCustomStage& stage);
virtual void setupVariables(GrGLShaderBuilder* builder,
int stage) SK_OVERRIDE;
virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
virtual void emitVS(GrGLShaderBuilder* state,
const char* vertexCoords) SK_OVERRIDE;
virtual void emitFS(GrGLShaderBuilder* state,
@ -53,10 +52,10 @@ GrGLMorphologyEffect ::GrGLMorphologyEffect(const GrProgramStageFactory& factory
fType = m.type();
}
void GrGLMorphologyEffect::setupVariables(GrGLShaderBuilder* builder, int stage) {
void GrGLMorphologyEffect::setupVariables(GrGLShaderBuilder* builder) {
fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType |
GrGLShaderBuilder::kVertex_ShaderType,
kVec2f_GrSLType, "uImageIncrement", stage);
kVec2f_GrSLType, "ImageIncrement");
}
void GrGLMorphologyEffect::emitVS(GrGLShaderBuilder* builder,

View File

@ -14,8 +14,7 @@ public:
GrGLTextureDomainEffect(const GrProgramStageFactory& factory,
const GrCustomStage& stage);
virtual void setupVariables(GrGLShaderBuilder* builder,
int stage) SK_OVERRIDE;
virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
virtual void emitVS(GrGLShaderBuilder* builder,
const char* vertexCoords) SK_OVERRIDE { }
virtual void emitFS(GrGLShaderBuilder* builder,
@ -42,10 +41,9 @@ GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrProgramStageFactory& fa
, fNameUni(GrGLUniformManager::kInvalidUniformHandle) {
}
void GrGLTextureDomainEffect::setupVariables(GrGLShaderBuilder* builder,
int stage) {
void GrGLTextureDomainEffect::setupVariables(GrGLShaderBuilder* builder) {
fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec4f_GrSLType, "uTexDom", stage);
kVec4f_GrSLType, "TexDom");
};
void GrGLTextureDomainEffect::emitFS(GrGLShaderBuilder* builder,

View File

@ -25,17 +25,10 @@ SK_DEFINE_INST_COUNT(GrGLProgram)
typedef GrGLProgram::Desc::StageDesc StageDesc;
#define VIEW_MATRIX_NAME "uViewM"
#define POS_ATTR_NAME "aPosition"
#define COL_ATTR_NAME "aColor"
#define COV_ATTR_NAME "aCoverage"
#define EDGE_ATTR_NAME "aEdge"
#define COL_UNI_NAME "uColor"
#define COV_UNI_NAME "uCoverage"
#define COL_FILTER_UNI_NAME "uColorFilter"
#define COL_MATRIX_UNI_NAME "uColorMatrix"
#define COL_MATRIX_VEC_UNI_NAME "uColorMatrixVec"
namespace {
inline void tex_attr_name(int coordIdx, SkString* s) {
@ -70,15 +63,6 @@ inline const char* all_zeros_vec(int count) {
inline const char* declared_color_output_name() { return "fsColorOut"; }
inline const char* dual_source_output_name() { return "dualSourceOut"; }
inline void tex_matrix_name(int stage, SkString* s) {
*s = "uTexM";
s->appendS32(stage);
}
inline void sampler_name(int stage, SkString* s) {
*s = "uSampler";
s->appendS32(stage);
}
}
GrGLProgram* GrGLProgram::Create(const GrGLContextInfo& gl,
@ -308,24 +292,14 @@ static void blendTermString(SkString* str, SkXfermode::Coeff coeff,
static void addColorFilter(SkString* fsCode, const char * outputVar,
SkXfermode::Coeff uniformCoeff,
SkXfermode::Coeff colorCoeff,
const char* filterColor,
const char* inColor) {
SkString colorStr, constStr;
blendTermString(&colorStr, colorCoeff, COL_FILTER_UNI_NAME,
inColor, inColor);
blendTermString(&constStr, uniformCoeff, COL_FILTER_UNI_NAME,
inColor, COL_FILTER_UNI_NAME);
blendTermString(&colorStr, colorCoeff, filterColor, inColor, inColor);
blendTermString(&constStr, uniformCoeff, filterColor, inColor, filterColor);
add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode);
}
/**
* Adds code to the fragment shader code which modifies the color by
* the specified color matrix.
*/
static void addColorMatrix(SkString* fsCode, const char * outputVar,
const char* inColor) {
fsCode->appendf("\t%s = %s * vec4(%s.rgb / %s.a, %s.a) + %s;\n", outputVar, COL_MATRIX_UNI_NAME, inColor, inColor, inColor, COL_MATRIX_VEC_UNI_NAME);
fsCode->appendf("\t%s.rgb *= %s.a;\n", outputVar, outputVar);
}
void GrGLProgram::genEdgeCoverage(SkString* coverageVar,
GrGLShaderBuilder* segments) const {
@ -400,11 +374,13 @@ void GrGLProgram::genInputColor(GrGLShaderBuilder* builder, SkString* inColor) {
builder->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName);
*inColor = fsName;
} break;
case GrGLProgram::Desc::kUniform_ColorInput:
case GrGLProgram::Desc::kUniform_ColorInput: {
const char* name;
fUniforms.fColorUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec4f_GrSLType, COL_UNI_NAME);
*inColor = COL_UNI_NAME;
kVec4f_GrSLType, "Color", &name);
*inColor = name;
break;
}
case GrGLProgram::Desc::kTransBlack_ColorInput:
GrAssert(!"needComputedColor should be false.");
break;
@ -417,14 +393,15 @@ void GrGLProgram::genInputColor(GrGLShaderBuilder* builder, SkString* inColor) {
}
void GrGLProgram::genUniformCoverage(GrGLShaderBuilder* builder, SkString* inOutCoverage) {
const char* covUniName;
fUniforms.fCoverageUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec4f_GrSLType, COV_UNI_NAME);
kVec4f_GrSLType, "Coverage", &covUniName);
if (inOutCoverage->size()) {
builder->fFSCode.appendf("\tvec4 uniCoverage = %s * %s;\n",
COV_UNI_NAME, inOutCoverage->c_str());
covUniName, inOutCoverage->c_str());
*inOutCoverage = "uniCoverage";
} else {
*inOutCoverage = COV_UNI_NAME;
*inOutCoverage = covUniName;
}
}
@ -655,16 +632,18 @@ bool GrGLProgram::genProgram(GrCustomStage** customStages) {
builder.fFSOutputs.push_back(colorOutput);
}
const char* viewMName;
fUniforms.fViewMatrixUni = builder.addUniform(GrGLShaderBuilder::kVertex_ShaderType,
kMat33f_GrSLType, VIEW_MATRIX_NAME);
kMat33f_GrSLType, "ViewM", &viewMName);
builder.fVSAttrs.push_back().set(kVec2f_GrSLType,
GrGLShaderVar::kAttribute_TypeModifier,
POS_ATTR_NAME);
builder.fVSCode.append("void main() {\n"
"\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n"
"\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n");
builder.fVSCode.appendf("void main() {\n"
"\tvec3 pos3 = %s * vec3("POS_ATTR_NAME", 1);\n"
"\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n",
viewMName);
// incoming color to current stage being processed.
SkString inColor;
@ -743,9 +722,11 @@ bool GrGLProgram::genProgram(GrCustomStage** customStages) {
&needColorFilterUniform, &bogus);
}
}
const char* colorFilterColorUniName = NULL;
if (needColorFilterUniform) {
fUniforms.fColorFilterUni = builder.addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec4f_GrSLType, COL_FILTER_UNI_NAME);
kVec4f_GrSLType, "FilterColor",
&colorFilterColorUniName);
}
bool wroteFragColorZero = false;
if (SkXfermode::kZero_Coeff == uniformCoeff &&
@ -759,17 +740,23 @@ bool GrGLProgram::genProgram(GrCustomStage** customStages) {
builder.fFSCode.append("\tvec4 filteredColor;\n");
const char* color = adjustInColor(inColor);
addColorFilter(&builder.fFSCode, "filteredColor", uniformCoeff,
colorCoeff, color);
colorCoeff, colorFilterColorUniName, color);
inColor = "filteredColor";
}
if (applyColorMatrix) {
const char* colMatrixName;
const char* colMatrixVecName;
fUniforms.fColorMatrixUni = builder.addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kMat44f_GrSLType, COL_MATRIX_UNI_NAME);
kMat44f_GrSLType, "ColorMatrix",
&colMatrixName);
fUniforms.fColorMatrixVecUni = builder.addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec4f_GrSLType, COL_MATRIX_VEC_UNI_NAME);
builder.fFSCode.append("\tvec4 matrixedColor;\n");
kVec4f_GrSLType, "ColorMatrixVec",
&colMatrixVecName);
const char* color = adjustInColor(inColor);
addColorMatrix(&builder.fFSCode, "matrixedColor", color);
builder.fFSCode.appendf("\tvec4 matrixedColor = %s * vec4(%s.rgb / %s.a, %s.a) + %s;\n",
colMatrixName, color, color, color, colMatrixVecName);
builder.fFSCode.append("\tmatrixedColor.rgb *= matrixedColor.a;\n");
inColor = "matrixedColor";
}
@ -835,6 +822,7 @@ bool GrGLProgram::genProgram(GrCustomStage** customStages) {
}
}
}
if (Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) {
builder.fFSOutputs.push_back().set(kVec4f_GrSLType,
GrGLShaderVar::kOut_TypeModifier,
@ -1004,7 +992,7 @@ void GrGLProgram::genStageCode(int stageNum,
const char* fsInColor, // NULL means no incoming color
const char* fsOutColor,
const char* vsInCoord,
GrGLShaderBuilder* segments) {
GrGLShaderBuilder* builder) {
GrAssert(stageNum >= 0 && stageNum <= GrDrawState::kNumStages);
const GrGLProgram::StageDesc& desc = fDesc.fStages[stageNum];
@ -1013,67 +1001,63 @@ void GrGLProgram::genStageCode(int stageNum,
GrAssert((desc.fInConfigFlags & StageDesc::kInConfigBitMask) == desc.fInConfigFlags);
builder->setCurrentStage(stageNum);
/// Vertex Shader Stuff
// decide whether we need a matrix to transform texture coords and whether the varying needs a
// perspective coord.
const char* matName = NULL;
if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
segments->fVaryingDims = segments->fCoordDims;
builder->fVaryingDims = builder->fCoordDims;
} else {
SkString texMatName;
tex_matrix_name(stageNum, &texMatName);
uniforms.fTextureMatrixUni = segments->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
kMat33f_GrSLType, texMatName.c_str());
const GrGLShaderVar& mat = segments->getUniformVariable(uniforms.fTextureMatrixUni);
// Can't use texMatName.c_str() because it's on the stack!
matName = mat.getName().c_str();
uniforms.fTextureMatrixUni = builder->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
kMat33f_GrSLType, "TexM", &matName);
const GrGLShaderVar& mat = builder->getUniformVariable(uniforms.fTextureMatrixUni);
if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) {
segments->fVaryingDims = segments->fCoordDims;
builder->fVaryingDims = builder->fCoordDims;
} else {
segments->fVaryingDims = segments->fCoordDims + 1;
builder->fVaryingDims = builder->fCoordDims + 1;
}
}
GrAssert(segments->fVaryingDims > 0);
GrAssert(builder->fVaryingDims > 0);
// Must setup variables after computing segments->fVaryingDims
if (NULL != customStage) {
customStage->setupVariables(segments, stageNum);
customStage->setupVariables(builder);
}
SkString samplerName;
sampler_name(stageNum, &samplerName);
uniforms.fSamplerUni = segments->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kSampler2D_GrSLType, samplerName.c_str());
const char* samplerName;
uniforms.fSamplerUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kSampler2D_GrSLType, "Sampler", &samplerName);
const char *varyingVSName, *varyingFSName;
segments->addVarying(GrSLFloatVectorType(segments->fVaryingDims),
"Stage",
stageNum,
&varyingVSName,
&varyingFSName);
builder->addVarying(GrSLFloatVectorType(builder->fVaryingDims),
"Stage",
&varyingVSName,
&varyingFSName);
if (!matName) {
GrAssert(segments->fVaryingDims == segments->fCoordDims);
segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
GrAssert(builder->fVaryingDims == builder->fCoordDims);
builder->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
} else {
// varying = texMatrix * texCoord
segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
builder->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
varyingVSName, matName, vsInCoord,
vector_all_coords(segments->fVaryingDims));
vector_all_coords(builder->fVaryingDims));
}
if (NULL != customStage) {
segments->fVSCode.appendf("\t{ // stage %d %s\n",
stageNum, customStage->name());
customStage->emitVS(segments, varyingVSName);
segments->fVSCode.appendf("\t}\n");
builder->fVSCode.appendf("\t{ // stage %d %s\n",
stageNum, customStage->name());
customStage->emitVS(builder, varyingVSName);
builder->fVSCode.appendf("\t}\n");
}
/// Fragment Shader Stuff
segments->fSampleCoords = varyingFSName;
builder->fSampleCoords = varyingFSName;
GrGLShaderBuilder::SamplerMode sampleMode =
GrGLShaderBuilder::kExplicitDivide_SamplerMode;
@ -1083,10 +1067,10 @@ void GrGLProgram::genStageCode(int stageNum,
} else if (NULL == customStage) {
sampleMode = GrGLShaderBuilder::kProj_SamplerMode;
}
segments->setupTextureAccess(sampleMode, stageNum);
builder->setupTextureAccess(sampleMode, stageNum);
segments->computeSwizzle(desc.fInConfigFlags);
segments->computeModulate(fsInColor);
builder->computeSwizzle(desc.fInConfigFlags);
builder->computeModulate(fsInColor);
static const uint32_t kMulByAlphaMask =
(StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
@ -1101,33 +1085,34 @@ void GrGLProgram::genStageCode(int stageNum,
StageDesc::kSmearAlpha_InConfigFlag));
GrAssert(!(desc.fInConfigFlags &
StageDesc::kSmearRed_InConfigFlag));
segments->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n",
fsOutColor,
segments->fTexFunc.c_str(),
samplerName.c_str(),
segments->fSampleCoords.c_str(),
segments->fSwizzle.c_str());
builder->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n",
fsOutColor,
builder->fTexFunc.c_str(),
samplerName,
builder->fSampleCoords.c_str(),
builder->fSwizzle.c_str());
if (desc.fInConfigFlags &
StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag) {
segments->fFSCode.appendf("\t%s = vec4(ceil(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
fsOutColor, fsOutColor, fsOutColor,
fsOutColor, segments->fModulate.c_str());
builder->fFSCode.appendf("\t%s = vec4(ceil(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
fsOutColor, fsOutColor, fsOutColor,
fsOutColor, builder->fModulate.c_str());
} else {
segments->fFSCode.appendf("\t%s = vec4(floor(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
fsOutColor, fsOutColor, fsOutColor,
fsOutColor, segments->fModulate.c_str());
builder->fFSCode.appendf("\t%s = vec4(floor(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
fsOutColor, fsOutColor, fsOutColor,
fsOutColor, builder->fModulate.c_str());
}
} else {
segments->emitDefaultFetch(fsOutColor, samplerName.c_str());
builder->emitDefaultFetch(fsOutColor, samplerName);
}
}
if (NULL != customStage) {
// Enclose custom code in a block to avoid namespace conflicts
segments->fFSCode.appendf("\t{ // stage %d %s \n",
stageNum, customStage->name());
customStage->emitFS(segments, fsOutColor, fsInColor,
samplerName.c_str());
segments->fFSCode.appendf("\t}\n");
builder->fFSCode.appendf("\t{ // stage %d %s \n",
stageNum, customStage->name());
customStage->emitFS(builder, fsOutColor, fsInColor,
samplerName);
builder->fFSCode.appendf("\t}\n");
}
builder->setNonStage();
}

View File

@ -17,7 +17,7 @@ GrGLProgramStage::~GrGLProgramStage() {
///////////////////////////////////////////////////////////////////////////////
void GrGLProgramStage::setupVariables(GrGLShaderBuilder*, int stage) {
void GrGLProgramStage::setupVariables(GrGLShaderBuilder*) {
}

View File

@ -48,7 +48,7 @@ public:
virtual ~GrGLProgramStage();
/** Create any uniforms or varyings the vertex shader requires. */
virtual void setupVariables(GrGLShaderBuilder* builder, int stage);
virtual void setupVariables(GrGLShaderBuilder* builder);
/** Appends vertex code to the appropriate SkString
on the state.

View File

@ -38,7 +38,8 @@ GrGLShaderBuilder::GrGLShaderBuilder(const GrGLContextInfo& ctx, GrGLUniformMana
, fVaryingDims(0)
, fComplexCoord(false)
, fContext(ctx)
, fUniformManager(uniformManager) {
, fUniformManager(uniformManager)
, fCurrentStage(kNonStageIdx) {
}
void GrGLShaderBuilder::computeSwizzle(uint32_t configFlags) {
@ -119,11 +120,11 @@ void GrGLShaderBuilder::emitDefaultFetch(const char* outColor,
fFSCode.appendf("%s%s;\n", fSwizzle.c_str(), fModulate.c_str());
}
GrGLUniformManager::UniformHandle GrGLShaderBuilder::addUniform(uint32_t visibility,
GrSLType type,
const char* name,
int stageNum,
int count) {
GrGLUniformManager::UniformHandle GrGLShaderBuilder::addUniformArray(uint32_t visibility,
GrSLType type,
const char* name,
int count,
const char** outName) {
GrAssert(name && strlen(name));
static const uint32_t kVisibilityMask = kVertex_ShaderType | kFragment_ShaderType;
GrAssert(0 == (~kVisibilityMask & visibility));
@ -138,9 +139,11 @@ GrGLUniformManager::UniformHandle GrGLShaderBuilder::addUniform(uint32_t visibil
GrAssert(h2 == h);
uni.fVariable.setType(type);
uni.fVariable.setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
uni.fVariable.setName(name);
if (stageNum >= 0) {
uni.fVariable.accessName()->appendS32(stageNum);
SkString* uniName = uni.fVariable.accessName();
if (kNonStageIdx == fCurrentStage) {
uniName->printf("u%s", name);
} else {
uniName->printf("u%s%d", name, fCurrentStage);
}
uni.fVariable.setArrayCount(count);
uni.fVisibility = visibility;
@ -153,6 +156,10 @@ GrGLUniformManager::UniformHandle GrGLShaderBuilder::addUniform(uint32_t visibil
uni.fVariable.setPrecision(kDefaultFragmentPrecision);
}
if (NULL != outName) {
*outName = uni.fVariable.c_str();
}
return h;
}
@ -167,7 +174,11 @@ void GrGLShaderBuilder::addVarying(GrSLType type,
fVSOutputs.push_back();
fVSOutputs.back().setType(type);
fVSOutputs.back().setTypeModifier(GrGLShaderVar::kOut_TypeModifier);
fVSOutputs.back().accessName()->printf("v%s", name);
if (kNonStageIdx == fCurrentStage) {
fVSOutputs.back().accessName()->printf("v%s", name);
} else {
fVSOutputs.back().accessName()->printf("v%s%d", name, fCurrentStage);
}
if (vsOutName) {
*vsOutName = fVSOutputs.back().getName().c_str();
}
@ -198,15 +209,6 @@ void GrGLShaderBuilder::addVarying(GrSLType type,
}
}
void GrGLShaderBuilder::addVarying(GrSLType type,
const char* name,
int stageNum,
const char** vsOutName,
const char** fsInName) {
SkString nameWithStage(name);
nameWithStage.appendS32(stageNum);
this->addVarying(type, nameWithStage.c_str(), vsOutName, fsInName);
}
namespace {

View File

@ -56,16 +56,23 @@ public:
const char* samplerName);
/** Add a uniform variable to the current program, that has visibilty in one or more shaders.
If stageNum is specified, it is appended to the name to guarantee uniqueness; if count is
specified, the uniform is an array. visibility is a bitfield of ShaderType values indicating
from which shaders the uniform should be accessible. At least one bit must be set. Geometry
shader uniforms are not supported at this time.
visibility is a bitfield of ShaderType values indicating from which shaders the uniform
should be accessible. At least one bit must be set. Geometry shader uniforms are not
supported at this time. The actual uniform name will be mangled. If outName is not NULL then
it will refer to the final uniform name after return. Use the addUniformArray variant to add
an array of uniforms.
*/
GrGLUniformManager::UniformHandle addUniform(uint32_t visibility,
GrSLType type,
const char* name,
int stageNum = -1,
int count = GrGLShaderVar::kNonArray);
const char** outName = NULL) {
return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNonArray, outName);
}
GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility,
GrSLType type,
const char* name,
int arrayCount,
const char** outName = NULL);
const GrGLShaderVar& getUniformVariable(GrGLUniformManager::UniformHandle) const;
@ -84,23 +91,21 @@ public:
const char** vsOutName = NULL,
const char** fsInName = NULL);
/** Add a varying variable to the current program to pass values between vertex and fragment
shaders; stageNum is appended to the name to guarantee uniqueness. If the last two
parameters are non-NULL, they are filled in with the name generated. */
void addVarying(GrSLType type,
const char* name,
int stageNum,
const char** vsOutName = NULL,
const char** fsInName = NULL);
/** Called after building is complete to get the final shader string. */
void getShader(ShaderType, SkString*) const;
/**
* TODO: Make this do all the compiling, linking, etc. Hide this from the custom stages
* TODO: Make this do all the compiling, linking, etc. Hide from the custom stages
*/
void finished(GrGLuint programID);
/**
* Sets the current stage (used to make variable names unique).
* TODO: Hide from the custom stages
*/
void setCurrentStage(int stage) { fCurrentStage = stage; }
void setNonStage() { fCurrentStage = kNonStageIdx; }
private:
typedef GrTAllocator<GrGLShaderVar> VarArray;
@ -147,8 +152,13 @@ public:
//@}
private:
enum {
kNonStageIdx = -1,
};
const GrGLContextInfo& fContext;
GrGLUniformManager& fUniformManager;
int fCurrentStage;
};
#endif

View File

@ -10,6 +10,7 @@
#include "gl/GrGLShaderVar.h"
#include "gl/GrGLSL.h"
#include "GrAllocator.h"
#include "SkTArray.h"
@ -50,7 +51,10 @@ public:
GrGLShaderVar fVariable;
uint32_t fVisibility;
};
typedef SkTArray<BuilderUniform, true> BuilderUniformArray;
// This uses an allocator rather than array so that the GrGLShaderVars don't move in memory
// after they are inserted. Users of GrGLShaderBuilder get refs to the vars and ptrs to their
// name strings. Otherwise, we'd have to hand out copies.
typedef GrTAllocator<BuilderUniform> BuilderUniformArray;
/**
* Called by the GrGLShaderBuilder to get GL locations for all uniforms.