junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "GrGLProgram.h" |
| 9 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 10 | #include "GrAllocator.h" |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 11 | #include "GrEffect.h" |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 12 | #include "GrDrawEffect.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 13 | #include "GrGLEffect.h" |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 14 | #include "GrGpuGL.h" |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 15 | #include "GrGLShaderVar.h" |
tomhudson@google.com | 0c8d93a | 2011-07-01 17:08:26 +0000 | [diff] [blame] | 16 | #include "SkTrace.h" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 17 | #include "SkXfermode.h" |
| 18 | |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 19 | #include "SkRTConf.h" |
| 20 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 21 | SK_DEFINE_INST_COUNT(GrGLProgram) |
| 22 | |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 23 | #define GL_CALL(X) GR_GL_CALL(fContext.interface(), X) |
| 24 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fContext.interface(), R, X) |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 25 | |
jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 26 | SK_CONF_DECLARE(bool, c_PrintShaders, "gpu.printShaders", false, |
| 27 | "Print the source code for all shaders generated."); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 28 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 29 | #define COL_ATTR_NAME "aColor" |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 30 | #define COV_ATTR_NAME "aCoverage" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 31 | #define EDGE_ATTR_NAME "aEdge" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 32 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 33 | namespace { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 34 | inline const char* declared_color_output_name() { return "fsColorOut"; } |
| 35 | inline const char* dual_source_output_name() { return "dualSourceOut"; } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 36 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 37 | |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 38 | GrGLProgram* GrGLProgram::Create(const GrGLContext& gl, |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 39 | const GrGLProgramDesc& desc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 40 | const GrEffectStage* stages[]) { |
| 41 | GrGLProgram* program = SkNEW_ARGS(GrGLProgram, (gl, desc, stages)); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 42 | if (!program->succeeded()) { |
| 43 | delete program; |
| 44 | program = NULL; |
| 45 | } |
| 46 | return program; |
| 47 | } |
| 48 | |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 49 | GrGLProgram::GrGLProgram(const GrGLContext& gl, |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 50 | const GrGLProgramDesc& desc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 51 | const GrEffectStage* stages[]) |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 52 | : fContext(gl) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 53 | , fUniformManager(gl) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 54 | fDesc = desc; |
| 55 | fVShaderID = 0; |
| 56 | fGShaderID = 0; |
| 57 | fFShaderID = 0; |
| 58 | fProgramID = 0; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 59 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 60 | fColor = GrColor_ILLEGAL; |
| 61 | fColorFilterColor = GrColor_ILLEGAL; |
| 62 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 63 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 64 | fEffects[s] = NULL; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 65 | } |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 66 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 67 | this->genProgram(stages); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | GrGLProgram::~GrGLProgram() { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 71 | if (fVShaderID) { |
| 72 | GL_CALL(DeleteShader(fVShaderID)); |
| 73 | } |
| 74 | if (fGShaderID) { |
| 75 | GL_CALL(DeleteShader(fGShaderID)); |
| 76 | } |
| 77 | if (fFShaderID) { |
| 78 | GL_CALL(DeleteShader(fFShaderID)); |
| 79 | } |
| 80 | if (fProgramID) { |
| 81 | GL_CALL(DeleteProgram(fProgramID)); |
| 82 | } |
| 83 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 84 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 85 | delete fEffects[i]; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 86 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 87 | } |
| 88 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 89 | void GrGLProgram::abandon() { |
| 90 | fVShaderID = 0; |
| 91 | fGShaderID = 0; |
| 92 | fFShaderID = 0; |
| 93 | fProgramID = 0; |
| 94 | } |
| 95 | |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 96 | void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 97 | GrBlendCoeff* dstCoeff) const { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 98 | switch (fDesc.fDualSrcOutput) { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 99 | case GrGLProgramDesc::kNone_DualSrcOutput: |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 100 | break; |
| 101 | // the prog will write a coverage value to the secondary |
| 102 | // output and the dst is blended by one minus that value. |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 103 | case GrGLProgramDesc::kCoverage_DualSrcOutput: |
| 104 | case GrGLProgramDesc::kCoverageISA_DualSrcOutput: |
| 105 | case GrGLProgramDesc::kCoverageISC_DualSrcOutput: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 106 | *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 107 | break; |
| 108 | default: |
| 109 | GrCrash("Unexpected dual source blend output"); |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 114 | namespace { |
| 115 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 116 | // given two blend coeffecients determine whether the src |
| 117 | // and/or dst computation can be omitted. |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 118 | inline void need_blend_inputs(SkXfermode::Coeff srcCoeff, |
| 119 | SkXfermode::Coeff dstCoeff, |
| 120 | bool* needSrcValue, |
| 121 | bool* needDstValue) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 122 | if (SkXfermode::kZero_Coeff == srcCoeff) { |
| 123 | switch (dstCoeff) { |
| 124 | // these all read the src |
| 125 | case SkXfermode::kSC_Coeff: |
| 126 | case SkXfermode::kISC_Coeff: |
| 127 | case SkXfermode::kSA_Coeff: |
| 128 | case SkXfermode::kISA_Coeff: |
| 129 | *needSrcValue = true; |
| 130 | break; |
| 131 | default: |
| 132 | *needSrcValue = false; |
| 133 | break; |
| 134 | } |
| 135 | } else { |
| 136 | *needSrcValue = true; |
| 137 | } |
| 138 | if (SkXfermode::kZero_Coeff == dstCoeff) { |
| 139 | switch (srcCoeff) { |
| 140 | // these all read the dst |
| 141 | case SkXfermode::kDC_Coeff: |
| 142 | case SkXfermode::kIDC_Coeff: |
| 143 | case SkXfermode::kDA_Coeff: |
| 144 | case SkXfermode::kIDA_Coeff: |
| 145 | *needDstValue = true; |
| 146 | break; |
| 147 | default: |
| 148 | *needDstValue = false; |
| 149 | break; |
| 150 | } |
| 151 | } else { |
| 152 | *needDstValue = true; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 157 | * Create a blend_coeff * value string to be used in shader code. Sets empty |
| 158 | * string if result is trivially zero. |
| 159 | */ |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 160 | inline void blend_term_string(SkString* str, SkXfermode::Coeff coeff, |
| 161 | const char* src, const char* dst, |
| 162 | const char* value) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 163 | switch (coeff) { |
| 164 | case SkXfermode::kZero_Coeff: /** 0 */ |
| 165 | *str = ""; |
| 166 | break; |
| 167 | case SkXfermode::kOne_Coeff: /** 1 */ |
| 168 | *str = value; |
| 169 | break; |
| 170 | case SkXfermode::kSC_Coeff: |
| 171 | str->printf("(%s * %s)", src, value); |
| 172 | break; |
| 173 | case SkXfermode::kISC_Coeff: |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 174 | str->printf("((%s - %s) * %s)", GrGLSLOnesVecf(4), src, value); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 175 | break; |
| 176 | case SkXfermode::kDC_Coeff: |
| 177 | str->printf("(%s * %s)", dst, value); |
| 178 | break; |
| 179 | case SkXfermode::kIDC_Coeff: |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 180 | str->printf("((%s - %s) * %s)", GrGLSLOnesVecf(4), dst, value); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 181 | break; |
| 182 | case SkXfermode::kSA_Coeff: /** src alpha */ |
| 183 | str->printf("(%s.a * %s)", src, value); |
| 184 | break; |
| 185 | case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */ |
| 186 | str->printf("((1.0 - %s.a) * %s)", src, value); |
| 187 | break; |
| 188 | case SkXfermode::kDA_Coeff: /** dst alpha */ |
| 189 | str->printf("(%s.a * %s)", dst, value); |
| 190 | break; |
| 191 | case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */ |
| 192 | str->printf("((1.0 - %s.a) * %s)", dst, value); |
| 193 | break; |
| 194 | default: |
| 195 | GrCrash("Unexpected xfer coeff."); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | /** |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 200 | * Adds a line to the fragment shader code which modifies the color by |
| 201 | * the specified color filter. |
| 202 | */ |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 203 | void add_color_filter(GrGLShaderBuilder* builder, |
| 204 | const char * outputVar, |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 205 | SkXfermode::Coeff uniformCoeff, |
| 206 | SkXfermode::Coeff colorCoeff, |
| 207 | const char* filterColor, |
| 208 | const char* inColor) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 209 | SkString colorStr, constStr; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 210 | blend_term_string(&colorStr, colorCoeff, filterColor, inColor, inColor); |
| 211 | blend_term_string(&constStr, uniformCoeff, filterColor, inColor, filterColor); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 212 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 213 | SkString sum; |
| 214 | GrGLSLAdd4f(&sum, colorStr.c_str(), constStr.c_str()); |
| 215 | builder->fsCodeAppendf("\t%s = %s;\n", outputVar, sum.c_str()); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 216 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 217 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 218 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 219 | void GrGLProgram::genInputColor(GrGLShaderBuilder* builder, SkString* inColor) { |
| 220 | switch (fDesc.fColorInput) { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 221 | case GrGLProgramDesc::kAttribute_ColorInput: { |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 222 | builder->addAttribute(kVec4f_GrSLType, COL_ATTR_NAME); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 223 | const char *vsName, *fsName; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 224 | builder->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 225 | builder->vsCodeAppendf("\t%s = " COL_ATTR_NAME ";\n", vsName); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 226 | *inColor = fsName; |
| 227 | } break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 228 | case GrGLProgramDesc::kUniform_ColorInput: { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 229 | const char* name; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 230 | fUniformHandles.fColorUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 231 | kVec4f_GrSLType, "Color", &name); |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 232 | *inColor = name; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 233 | break; |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 234 | } |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 235 | case GrGLProgramDesc::kTransBlack_ColorInput: |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 236 | GrAssert(!"needComputedColor should be false."); |
| 237 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 238 | case GrGLProgramDesc::kSolidWhite_ColorInput: |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 239 | break; |
| 240 | default: |
| 241 | GrCrash("Unknown color type."); |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 246 | void GrGLProgram::genUniformCoverage(GrGLShaderBuilder* builder, SkString* inOutCoverage) { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 247 | const char* covUniName; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 248 | fUniformHandles.fCoverageUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 249 | kVec4f_GrSLType, "Coverage", &covUniName); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 250 | if (inOutCoverage->size()) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 251 | builder->fsCodeAppendf("\tvec4 uniCoverage = %s * %s;\n", |
| 252 | covUniName, inOutCoverage->c_str()); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 253 | *inOutCoverage = "uniCoverage"; |
| 254 | } else { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 255 | *inOutCoverage = covUniName; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | namespace { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 260 | void gen_attribute_coverage(GrGLShaderBuilder* builder, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 261 | SkString* inOutCoverage) { |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 262 | builder->addAttribute(kVec4f_GrSLType, COV_ATTR_NAME); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 263 | const char *vsName, *fsName; |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 264 | builder->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName); |
| 265 | builder->vsCodeAppendf("\t%s = " COV_ATTR_NAME ";\n", vsName); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 266 | if (inOutCoverage->size()) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 267 | builder->fsCodeAppendf("\tvec4 attrCoverage = %s * %s;\n", fsName, inOutCoverage->c_str()); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 268 | *inOutCoverage = "attrCoverage"; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 269 | } else { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 270 | *inOutCoverage = fsName; |
| 271 | } |
| 272 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 273 | } |
| 274 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 275 | void GrGLProgram::genGeometryShader(GrGLShaderBuilder* builder) const { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 276 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 277 | // TODO: The builder should add all this glue code. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 278 | if (fDesc.fExperimentalGS) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 279 | GrAssert(fContext.info().glslGeneration() >= k150_GrGLSLGeneration); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 280 | builder->fGSHeader.append("layout(triangles) in;\n" |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 281 | "layout(triangle_strip, max_vertices = 6) out;\n"); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 282 | builder->gsCodeAppend("\tfor (int i = 0; i < 3; ++i) {\n" |
| 283 | "\t\tgl_Position = gl_in[i].gl_Position;\n"); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 284 | if (fDesc.fEmitsPointSize) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 285 | builder->gsCodeAppend("\t\tgl_PointSize = 1.0;\n"); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 286 | } |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 287 | GrAssert(builder->fGSInputs.count() == builder->fGSOutputs.count()); |
| 288 | int count = builder->fGSInputs.count(); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 289 | for (int i = 0; i < count; ++i) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 290 | builder->gsCodeAppendf("\t\t%s = %s[i];\n", |
| 291 | builder->fGSOutputs[i].getName().c_str(), |
| 292 | builder->fGSInputs[i].getName().c_str()); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 293 | } |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 294 | builder->gsCodeAppend("\t\tEmitVertex();\n" |
| 295 | "\t}\n" |
| 296 | "\tEndPrimitive();\n"); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 297 | } |
| 298 | #endif |
| 299 | } |
| 300 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 301 | const char* GrGLProgram::adjustInColor(const SkString& inColor) const { |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 302 | if (inColor.size()) { |
| 303 | return inColor.c_str(); |
| 304 | } else { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 305 | if (GrGLProgramDesc::kSolidWhite_ColorInput == fDesc.fColorInput) { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 306 | return GrGLSLOnesVecf(4); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 307 | } else { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 308 | return GrGLSLZerosVecf(4); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 313 | namespace { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 314 | // prints a shader using params similar to glShaderSource |
| 315 | void print_shader(GrGLint stringCnt, |
| 316 | const GrGLchar** strings, |
| 317 | GrGLint* stringLengths) { |
| 318 | for (int i = 0; i < stringCnt; ++i) { |
| 319 | if (NULL == stringLengths || stringLengths[i] < 0) { |
| 320 | GrPrintf(strings[i]); |
| 321 | } else { |
| 322 | GrPrintf("%.*s", stringLengths[i], strings[i]); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | // Compiles a GL shader, returns shader ID or 0 if failed params have same meaning as glShaderSource |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 328 | GrGLuint compile_shader(const GrGLContext& gl, |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 329 | GrGLenum type, |
| 330 | int stringCnt, |
| 331 | const char** strings, |
| 332 | int* stringLengths) { |
| 333 | SK_TRACE_EVENT1("GrGLProgram::CompileShader", |
| 334 | "stringCount", SkStringPrintf("%i", stringCnt).c_str()); |
| 335 | |
| 336 | GrGLuint shader; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 337 | GR_GL_CALL_RET(gl.interface(), shader, CreateShader(type)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 338 | if (0 == shader) { |
| 339 | return 0; |
| 340 | } |
| 341 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 342 | const GrGLInterface* gli = gl.interface(); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 343 | GrGLint compiled = GR_GL_INIT_ZERO; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 344 | GR_GL_CALL(gli, ShaderSource(shader, stringCnt, strings, stringLengths)); |
| 345 | GR_GL_CALL(gli, CompileShader(shader)); |
| 346 | GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 347 | |
| 348 | if (!compiled) { |
| 349 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 350 | GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 351 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 352 | if (infoLen > 0) { |
| 353 | // retrieve length even though we don't need it to workaround bug in chrome cmd buffer |
| 354 | // param validation. |
| 355 | GrGLsizei length = GR_GL_INIT_ZERO; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 356 | GR_GL_CALL(gli, GetShaderInfoLog(shader, infoLen+1, |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 357 | &length, (char*)log.get())); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 358 | print_shader(stringCnt, strings, stringLengths); |
| 359 | GrPrintf("\n%s", log.get()); |
| 360 | } |
| 361 | GrAssert(!"Shader compilation failed!"); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 362 | GR_GL_CALL(gli, DeleteShader(shader)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | return shader; |
| 366 | } |
| 367 | |
| 368 | // helper version of above for when shader is already flattened into a single SkString |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 369 | GrGLuint compile_shader(const GrGLContext& gl, GrGLenum type, const SkString& shader) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 370 | const GrGLchar* str = shader.c_str(); |
| 371 | int length = shader.size(); |
| 372 | return compile_shader(gl, type, 1, &str, &length); |
| 373 | } |
| 374 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // compiles all the shaders from builder and stores the shader IDs |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 378 | bool GrGLProgram::compileShaders(const GrGLShaderBuilder& builder) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 379 | |
| 380 | SkString shader; |
| 381 | |
| 382 | builder.getShader(GrGLShaderBuilder::kVertex_ShaderType, &shader); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 383 | if (c_PrintShaders) { |
| 384 | GrPrintf(shader.c_str()); |
| 385 | GrPrintf("\n"); |
| 386 | } |
skia.committer@gmail.com | 7fc0e0a | 2013-01-15 02:01:40 +0000 | [diff] [blame] | 387 | |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 388 | if (!(fVShaderID = compile_shader(fContext, GR_GL_VERTEX_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 389 | return false; |
| 390 | } |
| 391 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 392 | fGShaderID = 0; |
| 393 | #if GR_GL_EXPERIMENTAL_GS |
| 394 | if (fDesc.fExperimentalGS) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 395 | builder.getShader(GrGLShaderBuilder::kGeometry_ShaderType, &shader); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 396 | if (c_PrintShaders) { |
| 397 | GrPrintf(shader.c_str()); |
| 398 | GrPrintf("\n"); |
| 399 | } |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 400 | if (!(fGShaderID = compile_shader(fContext, GR_GL_GEOMETRY_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 401 | return false; |
| 402 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 403 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 404 | #endif |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 405 | |
| 406 | builder.getShader(GrGLShaderBuilder::kFragment_ShaderType, &shader); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 407 | if (c_PrintShaders) { |
| 408 | GrPrintf(shader.c_str()); |
| 409 | GrPrintf("\n"); |
| 410 | } |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 411 | if (!(fFShaderID = compile_shader(fContext, GR_GL_FRAGMENT_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 412 | return false; |
| 413 | } |
| 414 | |
| 415 | return true; |
| 416 | } |
| 417 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 418 | bool GrGLProgram::genProgram(const GrEffectStage* stages[]) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 419 | GrAssert(0 == fProgramID); |
| 420 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 421 | GrGLShaderBuilder builder(fContext.info(), fUniformManager, fDesc); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 422 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 423 | SkXfermode::Coeff colorCoeff, uniformCoeff; |
| 424 | // The rest of transfer mode color filters have not been implemented |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 425 | if (fDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 426 | GR_DEBUGCODE(bool success =) |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 427 | SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode> |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 428 | (fDesc.fColorFilterXfermode), |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 429 | &uniformCoeff, &colorCoeff); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 430 | GR_DEBUGASSERT(success); |
| 431 | } else { |
| 432 | colorCoeff = SkXfermode::kOne_Coeff; |
| 433 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 434 | } |
| 435 | |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 436 | // no need to do the color filter if coverage is 0. The output color is scaled by the coverage. |
| 437 | // All the dual source outputs are scaled by the coverage as well. |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 438 | if (GrGLProgramDesc::kTransBlack_ColorInput == fDesc.fCoverageInput) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 439 | colorCoeff = SkXfermode::kZero_Coeff; |
| 440 | uniformCoeff = SkXfermode::kZero_Coeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 441 | } |
| 442 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 443 | // If we know the final color is going to be all zeros then we can |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 444 | // simplify the color filter coefficients. needComputedColor will then |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 445 | // come out false below. |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 446 | if (GrGLProgramDesc::kTransBlack_ColorInput == fDesc.fColorInput) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 447 | colorCoeff = SkXfermode::kZero_Coeff; |
| 448 | if (SkXfermode::kDC_Coeff == uniformCoeff || |
| 449 | SkXfermode::kDA_Coeff == uniformCoeff) { |
| 450 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 451 | } else if (SkXfermode::kIDC_Coeff == uniformCoeff || |
| 452 | SkXfermode::kIDA_Coeff == uniformCoeff) { |
| 453 | uniformCoeff = SkXfermode::kOne_Coeff; |
| 454 | } |
| 455 | } |
| 456 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 457 | bool needColorFilterUniform; |
| 458 | bool needComputedColor; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 459 | need_blend_inputs(uniformCoeff, colorCoeff, |
| 460 | &needColorFilterUniform, &needComputedColor); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 461 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 462 | // the dual source output has no canonical var name, have to |
| 463 | // declare an output, which is incompatible with gl_FragColor/gl_FragData. |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 464 | bool dualSourceOutputWritten = false; |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 465 | |
| 466 | GrGLShaderVar colorOutput; |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 467 | bool isColorDeclared = GrGLSLSetupFSColorOuput(fContext.info().glslGeneration(), |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 468 | declared_color_output_name(), |
| 469 | &colorOutput); |
| 470 | if (isColorDeclared) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 471 | builder.fFSOutputs.push_back(colorOutput); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 472 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 473 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 474 | const char* viewMName; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 475 | fUniformHandles.fViewMatrixUni = builder.addUniform(GrGLShaderBuilder::kVertex_ShaderType, |
| 476 | kMat33f_GrSLType, "ViewM", &viewMName); |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 477 | |
skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 478 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 479 | builder.vsCodeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n" |
| 480 | "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n", |
| 481 | viewMName, builder.positionAttribute().getName().c_str()); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 482 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 483 | // incoming color to current stage being processed. |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 484 | SkString inColor; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 485 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 486 | if (needComputedColor) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 487 | this->genInputColor(&builder, &inColor); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 488 | } |
| 489 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 490 | // we output point size in the GS if present |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 491 | if (fDesc.fEmitsPointSize |
| 492 | #if GR_GL_EXPERIMENTAL_GS |
| 493 | && !fDesc.fExperimentalGS |
| 494 | #endif |
| 495 | ) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 496 | builder.vsCodeAppend("\tgl_PointSize = 1.0;\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 497 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 498 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 499 | /////////////////////////////////////////////////////////////////////////// |
| 500 | // compute the final color |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 501 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 502 | // if we have color stages string them together, feeding the output color |
| 503 | // of each to the next and generating code for each stage. |
| 504 | if (needComputedColor) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 505 | SkString outColor; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 506 | for (int s = 0; s < fDesc.fFirstCoverageStage; ++s) { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 507 | if (GrGLEffect::kNoEffectKey != fDesc.fEffectKeys[s]) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 508 | // create var to hold stage result |
| 509 | outColor = "color"; |
| 510 | outColor.appendS32(s); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 511 | builder.fsCodeAppendf("\tvec4 %s;\n", outColor.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 512 | |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 513 | builder.setCurrentStage(s); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 514 | fEffects[s] = builder.createAndEmitGLEffect(*stages[s], |
| 515 | fDesc.fEffectKeys[s], |
| 516 | inColor.size() ? inColor.c_str() : NULL, |
| 517 | outColor.c_str(), |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 518 | &fUniformHandles.fEffectSamplerUnis[s]); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 519 | builder.setNonStage(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 520 | inColor = outColor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 523 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 524 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 525 | // if have all ones or zeros for the "dst" input to the color filter then we |
| 526 | // may be able to make additional optimizations. |
| 527 | if (needColorFilterUniform && needComputedColor && !inColor.size()) { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 528 | GrAssert(GrGLProgramDesc::kSolidWhite_ColorInput == fDesc.fColorInput); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 529 | bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff || |
| 530 | SkXfermode::kIDA_Coeff == uniformCoeff; |
| 531 | if (uniformCoeffIsZero) { |
| 532 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 533 | bool bogus; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 534 | need_blend_inputs(SkXfermode::kZero_Coeff, colorCoeff, |
| 535 | &needColorFilterUniform, &bogus); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 536 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 537 | } |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 538 | const char* colorFilterColorUniName = NULL; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 539 | if (needColorFilterUniform) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 540 | fUniformHandles.fColorFilterUni = builder.addUniform( |
| 541 | GrGLShaderBuilder::kFragment_ShaderType, |
| 542 | kVec4f_GrSLType, "FilterColor", |
| 543 | &colorFilterColorUniName); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 544 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 545 | bool wroteFragColorZero = false; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 546 | if (SkXfermode::kZero_Coeff == uniformCoeff && |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 547 | SkXfermode::kZero_Coeff == colorCoeff) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 548 | builder.fsCodeAppendf("\t%s = %s;\n", colorOutput.getName().c_str(), GrGLSLZerosVecf(4)); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 549 | wroteFragColorZero = true; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 550 | } else if (SkXfermode::kDst_Mode != fDesc.fColorFilterXfermode) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 551 | builder.fsCodeAppend("\tvec4 filteredColor;\n"); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 552 | const char* color = adjustInColor(inColor); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 553 | add_color_filter(&builder, "filteredColor", uniformCoeff, |
| 554 | colorCoeff, colorFilterColorUniName, color); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 555 | inColor = "filteredColor"; |
| 556 | } |
| 557 | |
| 558 | /////////////////////////////////////////////////////////////////////////// |
| 559 | // compute the partial coverage (coverage stages and edge aa) |
| 560 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 561 | SkString inCoverage; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 562 | bool coverageIsZero = GrGLProgramDesc::kTransBlack_ColorInput == fDesc.fCoverageInput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 563 | // we don't need to compute coverage at all if we know the final shader |
| 564 | // output will be zero and we don't have a dual src blend output. |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 565 | if (!wroteFragColorZero || GrGLProgramDesc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) { |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 566 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 567 | if (!coverageIsZero) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 568 | switch (fDesc.fCoverageInput) { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 569 | case GrGLProgramDesc::kSolidWhite_ColorInput: |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 570 | // empty string implies solid white |
| 571 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 572 | case GrGLProgramDesc::kAttribute_ColorInput: |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 573 | gen_attribute_coverage(&builder, &inCoverage); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 574 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 575 | case GrGLProgramDesc::kUniform_ColorInput: |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 576 | this->genUniformCoverage(&builder, &inCoverage); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 577 | break; |
| 578 | default: |
| 579 | GrCrash("Unexpected input coverage."); |
| 580 | } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 581 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 582 | SkString outCoverage; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 583 | const int& startStage = fDesc.fFirstCoverageStage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 584 | for (int s = startStage; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 585 | if (fDesc.fEffectKeys[s]) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 586 | // create var to hold stage output |
| 587 | outCoverage = "coverage"; |
| 588 | outCoverage.appendS32(s); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 589 | builder.fsCodeAppendf("\tvec4 %s;\n", outCoverage.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 590 | |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 591 | builder.setCurrentStage(s); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 592 | fEffects[s] = builder.createAndEmitGLEffect( |
| 593 | *stages[s], |
| 594 | fDesc.fEffectKeys[s], |
| 595 | inCoverage.size() ? inCoverage.c_str() : NULL, |
| 596 | outCoverage.c_str(), |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 597 | &fUniformHandles.fEffectSamplerUnis[s]); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 598 | builder.setNonStage(); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 599 | inCoverage = outCoverage; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 600 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 601 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 602 | |
| 603 | // discard if coverage is zero |
bsalomon@google.com | e9144c6 | 2013-03-26 16:42:17 +0000 | [diff] [blame] | 604 | if (fDesc.fDiscardIfZeroCoverage && !outCoverage.isEmpty()) { |
jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 605 | builder.fsCodeAppendf( |
| 606 | "\tif (all(lessThanEqual(%s, vec4(0.0)))) {\n\t\tdiscard;\n\t}\n", |
| 607 | outCoverage.c_str()); |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 608 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 609 | } |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 610 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 611 | if (GrGLProgramDesc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 612 | builder.fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 613 | GrGLShaderVar::kOut_TypeModifier, |
| 614 | dual_source_output_name()); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 615 | bool outputIsZero = coverageIsZero; |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 616 | SkString coeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 617 | if (!outputIsZero && |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 618 | GrGLProgramDesc::kCoverage_DualSrcOutput != fDesc.fDualSrcOutput && !wroteFragColorZero) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 619 | if (!inColor.size()) { |
| 620 | outputIsZero = true; |
| 621 | } else { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 622 | if (GrGLProgramDesc::kCoverageISA_DualSrcOutput == fDesc.fDualSrcOutput) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 623 | coeff.printf("(1 - %s.a)", inColor.c_str()); |
| 624 | } else { |
| 625 | coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str()); |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | if (outputIsZero) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 630 | builder.fsCodeAppendf("\t%s = %s;\n", dual_source_output_name(), GrGLSLZerosVecf(4)); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 631 | } else { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 632 | SkString modulate; |
| 633 | GrGLSLModulate4f(&modulate, coeff.c_str(), inCoverage.c_str()); |
| 634 | builder.fsCodeAppendf("\t%s = %s;\n", dual_source_output_name(), modulate.c_str()); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 635 | } |
| 636 | dualSourceOutputWritten = true; |
| 637 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 638 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 639 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 640 | /////////////////////////////////////////////////////////////////////////// |
| 641 | // combine color and coverage as frag color |
| 642 | |
| 643 | if (!wroteFragColorZero) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 644 | if (coverageIsZero) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 645 | builder.fsCodeAppendf("\t%s = %s;\n", colorOutput.getName().c_str(), GrGLSLZerosVecf(4)); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 646 | } else { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 647 | SkString modulate; |
| 648 | GrGLSLModulate4f(&modulate, inColor.c_str(), inCoverage.c_str()); |
| 649 | builder.fsCodeAppendf("\t%s = %s;\n", colorOutput.getName().c_str(), modulate.c_str()); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 650 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 651 | } |
| 652 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 653 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 654 | // insert GS |
| 655 | #if GR_DEBUG |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 656 | this->genGeometryShader(&builder); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 657 | #endif |
| 658 | |
| 659 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 660 | // compile and setup attribs and unis |
| 661 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 662 | if (!this->compileShaders(builder)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 663 | return false; |
| 664 | } |
| 665 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 666 | if (!this->bindOutputsAttribsAndLinkProgram(builder, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 667 | isColorDeclared, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 668 | dualSourceOutputWritten)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 669 | return false; |
| 670 | } |
| 671 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 672 | builder.finished(fProgramID); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 673 | fUniformHandles.fRTHeightUni = builder.getRTHeightUniform(); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 674 | fUniformHandles.fDstCopyTopLeftUni = builder.getDstCopyTopLeftUniform(); |
| 675 | fUniformHandles.fDstCopyScaleUni = builder.getDstCopyScaleUniform(); |
| 676 | fUniformHandles.fDstCopySamplerUni = builder.getDstCopySamplerUniform(); |
| 677 | // This must be called after we set fDstCopySamplerUni above. |
| 678 | this->initSamplerUniforms(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 679 | |
| 680 | return true; |
| 681 | } |
| 682 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 683 | bool GrGLProgram::bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 684 | bool bindColorOut, |
| 685 | bool bindDualSrcOut) { |
| 686 | GL_CALL_RET(fProgramID, CreateProgram()); |
| 687 | if (!fProgramID) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 688 | return false; |
| 689 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 690 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 691 | GL_CALL(AttachShader(fProgramID, fVShaderID)); |
| 692 | if (fGShaderID) { |
| 693 | GL_CALL(AttachShader(fProgramID, fGShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 694 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 695 | GL_CALL(AttachShader(fProgramID, fFShaderID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 696 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 697 | if (bindColorOut) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 698 | GL_CALL(BindFragDataLocation(fProgramID, 0, declared_color_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 699 | } |
| 700 | if (bindDualSrcOut) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 701 | GL_CALL(BindFragDataLocationIndexed(fProgramID, 0, 1, dual_source_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 702 | } |
| 703 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 704 | // Bind the attrib locations to same values for all shaders |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 705 | GL_CALL(BindAttribLocation(fProgramID, |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 706 | fDesc.fPositionAttributeIndex, |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 707 | builder.positionAttribute().c_str())); |
jvanverth@google.com | c7bf296 | 2013-04-01 19:29:32 +0000 | [diff] [blame^] | 708 | GL_CALL(BindAttribLocation(fProgramID, fDesc.fColorAttributeIndex, COL_ATTR_NAME)); |
| 709 | GL_CALL(BindAttribLocation(fProgramID, fDesc.fCoverageAttributeIndex, COV_ATTR_NAME)); |
| 710 | |
| 711 | if (fDesc.fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit) { |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 712 | GL_CALL(BindAttribLocation(fProgramID, |
jvanverth@google.com | c7bf296 | 2013-04-01 19:29:32 +0000 | [diff] [blame^] | 713 | fDesc.fLocalCoordsAttributeIndex, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 714 | builder.localCoordsAttribute().c_str())); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 715 | } |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 716 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 717 | const GrGLShaderBuilder::AttributePair* attribEnd = builder.getEffectAttributes().end(); |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 718 | for (const GrGLShaderBuilder::AttributePair* attrib = builder.getEffectAttributes().begin(); |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 719 | attrib != attribEnd; |
| 720 | ++attrib) { |
| 721 | GL_CALL(BindAttribLocation(fProgramID, attrib->fIndex, attrib->fName.c_str())); |
| 722 | } |
| 723 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 724 | GL_CALL(LinkProgram(fProgramID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 725 | |
| 726 | GrGLint linked = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 727 | GL_CALL(GetProgramiv(fProgramID, GR_GL_LINK_STATUS, &linked)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 728 | if (!linked) { |
| 729 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 730 | GL_CALL(GetProgramiv(fProgramID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 731 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 732 | if (infoLen > 0) { |
bsalomon@google.com | 79afcaa | 2011-09-14 14:29:18 +0000 | [diff] [blame] | 733 | // retrieve length even though we don't need it to workaround |
| 734 | // bug in chrome cmd buffer param validation. |
| 735 | GrGLsizei length = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 736 | GL_CALL(GetProgramInfoLog(fProgramID, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 737 | infoLen+1, |
| 738 | &length, |
| 739 | (char*)log.get())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 740 | GrPrintf((char*)log.get()); |
| 741 | } |
| 742 | GrAssert(!"Error linking program"); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 743 | GL_CALL(DeleteProgram(fProgramID)); |
| 744 | fProgramID = 0; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 745 | return false; |
| 746 | } |
| 747 | return true; |
| 748 | } |
| 749 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 750 | void GrGLProgram::initSamplerUniforms() { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 751 | GL_CALL(UseProgram(fProgramID)); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 752 | // We simply bind the uniforms to successive texture units beginning at 0. setData() assumes |
| 753 | // this behavior. |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 754 | GrGLint texUnitIdx = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 755 | if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fDstCopySamplerUni) { |
| 756 | fUniformManager.setSampler(fUniformHandles.fDstCopySamplerUni, texUnitIdx); |
| 757 | ++texUnitIdx; |
| 758 | } |
| 759 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 760 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 761 | int numSamplers = fUniformHandles.fEffectSamplerUnis[s].count(); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 762 | for (int u = 0; u < numSamplers; ++u) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 763 | UniformHandle handle = fUniformHandles.fEffectSamplerUnis[s][u]; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 764 | if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
| 765 | fUniformManager.setSampler(handle, texUnitIdx); |
| 766 | ++texUnitIdx; |
bsalomon@google.com | 0982d35 | 2012-07-31 15:33:25 +0000 | [diff] [blame] | 767 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 768 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 769 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 770 | } |
| 771 | |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 772 | /////////////////////////////////////////////////////////////////////////////// |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 773 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 774 | void GrGLProgram::setData(GrGpuGL* gpu, |
| 775 | GrColor color, |
| 776 | GrColor coverage, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 777 | const GrDeviceCoordTexture* dstCopy, |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 778 | SharedGLState* sharedState) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 779 | const GrDrawState& drawState = gpu->getDrawState(); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 780 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 781 | this->setColor(drawState, color, sharedState); |
| 782 | this->setCoverage(drawState, coverage, sharedState); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 783 | this->setMatrixAndRenderTargetHeight(drawState); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 784 | |
| 785 | // Setup the SkXfermode::Mode-based colorfilter uniform if necessary |
| 786 | if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fColorFilterUni && |
| 787 | fColorFilterColor != drawState.getColorFilterColor()) { |
| 788 | GrGLfloat c[4]; |
| 789 | GrColorToRGBAFloat(drawState.getColorFilterColor(), c); |
| 790 | fUniformManager.set4fv(fUniformHandles.fColorFilterUni, 0, 1, c); |
| 791 | fColorFilterColor = drawState.getColorFilterColor(); |
| 792 | } |
| 793 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 794 | GrGLint texUnitIdx = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 795 | if (NULL != dstCopy) { |
| 796 | if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fDstCopyTopLeftUni) { |
| 797 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fDstCopyScaleUni); |
| 798 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != |
| 799 | fUniformHandles.fDstCopySamplerUni); |
| 800 | fUniformManager.set2f(fUniformHandles.fDstCopyTopLeftUni, |
| 801 | static_cast<GrGLfloat>(dstCopy->offset().fX), |
| 802 | static_cast<GrGLfloat>(dstCopy->offset().fY)); |
| 803 | fUniformManager.set2f(fUniformHandles.fDstCopyScaleUni, |
| 804 | 1.f / dstCopy->texture()->width(), |
| 805 | 1.f / dstCopy->texture()->height()); |
| 806 | GrGLTexture* texture = static_cast<GrGLTexture*>(dstCopy->texture()); |
| 807 | static GrTextureParams kParams; // the default is clamp, nearest filtering. |
| 808 | gpu->bindTexture(texUnitIdx, kParams, texture); |
| 809 | ++texUnitIdx; |
| 810 | } else { |
| 811 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == |
| 812 | fUniformHandles.fDstCopyScaleUni); |
| 813 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == |
| 814 | fUniformHandles.fDstCopySamplerUni); |
| 815 | } |
| 816 | } else { |
| 817 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == |
| 818 | fUniformHandles.fDstCopyTopLeftUni); |
| 819 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == |
| 820 | fUniformHandles.fDstCopyScaleUni); |
| 821 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == |
| 822 | fUniformHandles.fDstCopySamplerUni); |
| 823 | } |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 824 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 825 | if (NULL != fEffects[s]) { |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 826 | const GrEffectStage& stage = drawState.getStage(s); |
| 827 | GrAssert(NULL != stage.getEffect()); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 828 | |
jvanverth@google.com | c7bf296 | 2013-04-01 19:29:32 +0000 | [diff] [blame^] | 829 | bool explicitLocalCoords = |
| 830 | (fDesc.fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 831 | GrDrawEffect drawEffect(stage, explicitLocalCoords); |
| 832 | fEffects[s]->setData(fUniformManager, drawEffect); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 833 | int numSamplers = fUniformHandles.fEffectSamplerUnis[s].count(); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 834 | for (int u = 0; u < numSamplers; ++u) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 835 | UniformHandle handle = fUniformHandles.fEffectSamplerUnis[s][u]; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 836 | if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 837 | const GrTextureAccess& access = (*stage.getEffect())->textureAccess(u); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 838 | GrGLTexture* texture = static_cast<GrGLTexture*>(access.getTexture()); |
| 839 | gpu->bindTexture(texUnitIdx, access.getParams(), texture); |
| 840 | ++texUnitIdx; |
| 841 | } |
| 842 | } |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 843 | } |
| 844 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 845 | } |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 846 | |
| 847 | void GrGLProgram::setColor(const GrDrawState& drawState, |
| 848 | GrColor color, |
| 849 | SharedGLState* sharedState) { |
jvanverth@google.com | c7bf296 | 2013-04-01 19:29:32 +0000 | [diff] [blame^] | 850 | if (!(drawState.getAttribBindings() & GrDrawState::kColor_AttribBindingsBit)) { |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 851 | switch (fDesc.fColorInput) { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 852 | case GrGLProgramDesc::kAttribute_ColorInput: |
jvanverth@google.com | c7bf296 | 2013-04-01 19:29:32 +0000 | [diff] [blame^] | 853 | if (sharedState->fConstAttribColor != color) { |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 854 | // OpenGL ES only supports the float varieties of glVertexAttrib |
| 855 | GrGLfloat c[4]; |
| 856 | GrColorToRGBAFloat(color, c); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 857 | GL_CALL(VertexAttrib4fv(fDesc.fColorAttributeIndex, c)); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 858 | sharedState->fConstAttribColor = color; |
| 859 | } |
| 860 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 861 | case GrGLProgramDesc::kUniform_ColorInput: |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 862 | if (fColor != color) { |
| 863 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 864 | GrGLfloat c[4]; |
| 865 | GrColorToRGBAFloat(color, c); |
| 866 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != |
| 867 | fUniformHandles.fColorUni); |
| 868 | fUniformManager.set4fv(fUniformHandles.fColorUni, 0, 1, c); |
| 869 | fColor = color; |
| 870 | } |
| 871 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 872 | case GrGLProgramDesc::kSolidWhite_ColorInput: |
| 873 | case GrGLProgramDesc::kTransBlack_ColorInput: |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 874 | break; |
| 875 | default: |
| 876 | GrCrash("Unknown color type."); |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | void GrGLProgram::setCoverage(const GrDrawState& drawState, |
| 882 | GrColor coverage, |
| 883 | SharedGLState* sharedState) { |
jvanverth@google.com | c7bf296 | 2013-04-01 19:29:32 +0000 | [diff] [blame^] | 884 | if (!(drawState.getAttribBindings() & GrDrawState::kCoverage_AttribBindingsBit)) { |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 885 | switch (fDesc.fCoverageInput) { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 886 | case GrGLProgramDesc::kAttribute_ColorInput: |
jvanverth@google.com | c7bf296 | 2013-04-01 19:29:32 +0000 | [diff] [blame^] | 887 | if (sharedState->fConstAttribCoverage != coverage) { |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 888 | // OpenGL ES only supports the float varieties of glVertexAttrib |
| 889 | GrGLfloat c[4]; |
| 890 | GrColorToRGBAFloat(coverage, c); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 891 | GL_CALL(VertexAttrib4fv(fDesc.fCoverageAttributeIndex, c)); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 892 | sharedState->fConstAttribCoverage = coverage; |
| 893 | } |
| 894 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 895 | case GrGLProgramDesc::kUniform_ColorInput: |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 896 | if (fCoverage != coverage) { |
| 897 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 898 | GrGLfloat c[4]; |
| 899 | GrColorToRGBAFloat(coverage, c); |
| 900 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != |
| 901 | fUniformHandles.fCoverageUni); |
| 902 | fUniformManager.set4fv(fUniformHandles.fCoverageUni, 0, 1, c); |
| 903 | fCoverage = coverage; |
| 904 | } |
| 905 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 906 | case GrGLProgramDesc::kSolidWhite_ColorInput: |
| 907 | case GrGLProgramDesc::kTransBlack_ColorInput: |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 908 | break; |
| 909 | default: |
| 910 | GrCrash("Unknown coverage type."); |
| 911 | } |
| 912 | } |
| 913 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 914 | |
| 915 | void GrGLProgram::setMatrixAndRenderTargetHeight(const GrDrawState& drawState) { |
| 916 | const GrRenderTarget* rt = drawState.getRenderTarget(); |
| 917 | SkISize size; |
| 918 | size.set(rt->width(), rt->height()); |
| 919 | |
| 920 | // Load the RT height uniform if it is needed to y-flip gl_FragCoord. |
| 921 | if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fRTHeightUni && |
| 922 | fMatrixState.fRenderTargetSize.fHeight != size.fHeight) { |
| 923 | fUniformManager.set1f(fUniformHandles.fRTHeightUni, SkIntToScalar(size.fHeight)); |
| 924 | } |
| 925 | |
| 926 | if (fMatrixState.fRenderTargetOrigin != rt->origin() || |
| 927 | !fMatrixState.fViewMatrix.cheapEqualTo(drawState.getViewMatrix()) || |
| 928 | fMatrixState.fRenderTargetSize != size) { |
| 929 | SkMatrix m; |
| 930 | if (kBottomLeft_GrSurfaceOrigin == rt->origin()) { |
| 931 | m.setAll( |
| 932 | SkIntToScalar(2) / size.fWidth, 0, -SK_Scalar1, |
| 933 | 0,-SkIntToScalar(2) / size.fHeight, SK_Scalar1, |
| 934 | 0, 0, SkMatrix::I()[8]); |
| 935 | } else { |
| 936 | m.setAll( |
| 937 | SkIntToScalar(2) / size.fWidth, 0, -SK_Scalar1, |
| 938 | 0, SkIntToScalar(2) / size.fHeight,-SK_Scalar1, |
| 939 | 0, 0, SkMatrix::I()[8]); |
| 940 | } |
| 941 | m.setConcat(m, drawState.getViewMatrix()); |
| 942 | |
| 943 | // ES doesn't allow you to pass true to the transpose param so we do our own transpose. |
| 944 | GrGLfloat mt[] = { |
| 945 | SkScalarToFloat(m[SkMatrix::kMScaleX]), |
| 946 | SkScalarToFloat(m[SkMatrix::kMSkewY]), |
| 947 | SkScalarToFloat(m[SkMatrix::kMPersp0]), |
| 948 | SkScalarToFloat(m[SkMatrix::kMSkewX]), |
| 949 | SkScalarToFloat(m[SkMatrix::kMScaleY]), |
| 950 | SkScalarToFloat(m[SkMatrix::kMPersp1]), |
| 951 | SkScalarToFloat(m[SkMatrix::kMTransX]), |
| 952 | SkScalarToFloat(m[SkMatrix::kMTransY]), |
| 953 | SkScalarToFloat(m[SkMatrix::kMPersp2]) |
| 954 | }; |
| 955 | fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, mt); |
| 956 | fMatrixState.fViewMatrix = drawState.getViewMatrix(); |
| 957 | fMatrixState.fRenderTargetSize = size; |
| 958 | fMatrixState.fRenderTargetOrigin = rt->origin(); |
| 959 | } |
| 960 | } |