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; |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 465 | builder.fHeader.append(GrGetGLSLVersionDecl(fContext.info().binding(), |
| 466 | fContext.info().glslGeneration())); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 467 | |
| 468 | GrGLShaderVar colorOutput; |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 469 | bool isColorDeclared = GrGLSLSetupFSColorOuput(fContext.info().glslGeneration(), |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 470 | declared_color_output_name(), |
| 471 | &colorOutput); |
| 472 | if (isColorDeclared) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 473 | builder.fFSOutputs.push_back(colorOutput); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 474 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 475 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 476 | const char* viewMName; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 477 | fUniformHandles.fViewMatrixUni = builder.addUniform(GrGLShaderBuilder::kVertex_ShaderType, |
| 478 | kMat33f_GrSLType, "ViewM", &viewMName); |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 479 | |
skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 480 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 481 | builder.vsCodeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n" |
| 482 | "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n", |
| 483 | viewMName, builder.positionAttribute().getName().c_str()); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 484 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 485 | // incoming color to current stage being processed. |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 486 | SkString inColor; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 487 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 488 | if (needComputedColor) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 489 | this->genInputColor(&builder, &inColor); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 490 | } |
| 491 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 492 | // we output point size in the GS if present |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame^] | 493 | if (fDesc.fEmitsPointSize |
| 494 | #if GR_GL_EXPERIMENTAL_GS |
| 495 | && !fDesc.fExperimentalGS |
| 496 | #endif |
| 497 | ) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 498 | builder.vsCodeAppend("\tgl_PointSize = 1.0;\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 499 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 500 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 501 | /////////////////////////////////////////////////////////////////////////// |
| 502 | // compute the final color |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 503 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 504 | // if we have color stages string them together, feeding the output color |
| 505 | // of each to the next and generating code for each stage. |
| 506 | if (needComputedColor) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 507 | SkString outColor; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 508 | for (int s = 0; s < fDesc.fFirstCoverageStage; ++s) { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 509 | if (GrGLEffect::kNoEffectKey != fDesc.fEffectKeys[s]) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 510 | // create var to hold stage result |
| 511 | outColor = "color"; |
| 512 | outColor.appendS32(s); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 513 | builder.fsCodeAppendf("\tvec4 %s;\n", outColor.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 514 | |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 515 | builder.setCurrentStage(s); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 516 | fEffects[s] = builder.createAndEmitGLEffect(*stages[s], |
| 517 | fDesc.fEffectKeys[s], |
| 518 | inColor.size() ? inColor.c_str() : NULL, |
| 519 | outColor.c_str(), |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame^] | 520 | &fUniformHandles.fEffectSamplerUnis[s]); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 521 | builder.setNonStage(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 522 | inColor = outColor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 525 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 526 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 527 | // if have all ones or zeros for the "dst" input to the color filter then we |
| 528 | // may be able to make additional optimizations. |
| 529 | if (needColorFilterUniform && needComputedColor && !inColor.size()) { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 530 | GrAssert(GrGLProgramDesc::kSolidWhite_ColorInput == fDesc.fColorInput); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 531 | bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff || |
| 532 | SkXfermode::kIDA_Coeff == uniformCoeff; |
| 533 | if (uniformCoeffIsZero) { |
| 534 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 535 | bool bogus; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 536 | need_blend_inputs(SkXfermode::kZero_Coeff, colorCoeff, |
| 537 | &needColorFilterUniform, &bogus); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 538 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 539 | } |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 540 | const char* colorFilterColorUniName = NULL; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 541 | if (needColorFilterUniform) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 542 | fUniformHandles.fColorFilterUni = builder.addUniform( |
| 543 | GrGLShaderBuilder::kFragment_ShaderType, |
| 544 | kVec4f_GrSLType, "FilterColor", |
| 545 | &colorFilterColorUniName); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 546 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 547 | bool wroteFragColorZero = false; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 548 | if (SkXfermode::kZero_Coeff == uniformCoeff && |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 549 | SkXfermode::kZero_Coeff == colorCoeff) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 550 | 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] | 551 | wroteFragColorZero = true; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 552 | } else if (SkXfermode::kDst_Mode != fDesc.fColorFilterXfermode) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 553 | builder.fsCodeAppend("\tvec4 filteredColor;\n"); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 554 | const char* color = adjustInColor(inColor); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 555 | add_color_filter(&builder, "filteredColor", uniformCoeff, |
| 556 | colorCoeff, colorFilterColorUniName, color); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 557 | inColor = "filteredColor"; |
| 558 | } |
| 559 | |
| 560 | /////////////////////////////////////////////////////////////////////////// |
| 561 | // compute the partial coverage (coverage stages and edge aa) |
| 562 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 563 | SkString inCoverage; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 564 | bool coverageIsZero = GrGLProgramDesc::kTransBlack_ColorInput == fDesc.fCoverageInput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 565 | // we don't need to compute coverage at all if we know the final shader |
| 566 | // 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] | 567 | if (!wroteFragColorZero || GrGLProgramDesc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) { |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 568 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 569 | if (!coverageIsZero) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 570 | switch (fDesc.fCoverageInput) { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 571 | case GrGLProgramDesc::kSolidWhite_ColorInput: |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 572 | // empty string implies solid white |
| 573 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 574 | case GrGLProgramDesc::kAttribute_ColorInput: |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 575 | gen_attribute_coverage(&builder, &inCoverage); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 576 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 577 | case GrGLProgramDesc::kUniform_ColorInput: |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 578 | this->genUniformCoverage(&builder, &inCoverage); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 579 | break; |
| 580 | default: |
| 581 | GrCrash("Unexpected input coverage."); |
| 582 | } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 583 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 584 | SkString outCoverage; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 585 | const int& startStage = fDesc.fFirstCoverageStage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 586 | for (int s = startStage; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 587 | if (fDesc.fEffectKeys[s]) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 588 | // create var to hold stage output |
| 589 | outCoverage = "coverage"; |
| 590 | outCoverage.appendS32(s); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 591 | builder.fsCodeAppendf("\tvec4 %s;\n", outCoverage.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 592 | |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 593 | builder.setCurrentStage(s); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 594 | fEffects[s] = builder.createAndEmitGLEffect( |
| 595 | *stages[s], |
| 596 | fDesc.fEffectKeys[s], |
| 597 | inCoverage.size() ? inCoverage.c_str() : NULL, |
| 598 | outCoverage.c_str(), |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame^] | 599 | &fUniformHandles.fEffectSamplerUnis[s]); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 600 | builder.setNonStage(); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 601 | inCoverage = outCoverage; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 602 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 603 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 604 | |
| 605 | // discard if coverage is zero |
bsalomon@google.com | e9144c6 | 2013-03-26 16:42:17 +0000 | [diff] [blame] | 606 | if (fDesc.fDiscardIfZeroCoverage && !outCoverage.isEmpty()) { |
jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 607 | builder.fsCodeAppendf( |
| 608 | "\tif (all(lessThanEqual(%s, vec4(0.0)))) {\n\t\tdiscard;\n\t}\n", |
| 609 | outCoverage.c_str()); |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 610 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 611 | } |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 612 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 613 | if (GrGLProgramDesc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 614 | builder.fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 615 | GrGLShaderVar::kOut_TypeModifier, |
| 616 | dual_source_output_name()); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 617 | bool outputIsZero = coverageIsZero; |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 618 | SkString coeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 619 | if (!outputIsZero && |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 620 | GrGLProgramDesc::kCoverage_DualSrcOutput != fDesc.fDualSrcOutput && !wroteFragColorZero) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 621 | if (!inColor.size()) { |
| 622 | outputIsZero = true; |
| 623 | } else { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 624 | if (GrGLProgramDesc::kCoverageISA_DualSrcOutput == fDesc.fDualSrcOutput) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 625 | coeff.printf("(1 - %s.a)", inColor.c_str()); |
| 626 | } else { |
| 627 | coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str()); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | if (outputIsZero) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 632 | 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] | 633 | } else { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 634 | SkString modulate; |
| 635 | GrGLSLModulate4f(&modulate, coeff.c_str(), inCoverage.c_str()); |
| 636 | 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] | 637 | } |
| 638 | dualSourceOutputWritten = true; |
| 639 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 640 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 641 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 642 | /////////////////////////////////////////////////////////////////////////// |
| 643 | // combine color and coverage as frag color |
| 644 | |
| 645 | if (!wroteFragColorZero) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 646 | if (coverageIsZero) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 647 | 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] | 648 | } else { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 649 | SkString modulate; |
| 650 | GrGLSLModulate4f(&modulate, inColor.c_str(), inCoverage.c_str()); |
| 651 | 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] | 652 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 653 | } |
| 654 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 655 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 656 | // insert GS |
| 657 | #if GR_DEBUG |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 658 | this->genGeometryShader(&builder); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 659 | #endif |
| 660 | |
| 661 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 662 | // compile and setup attribs and unis |
| 663 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 664 | if (!this->compileShaders(builder)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 665 | return false; |
| 666 | } |
| 667 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 668 | if (!this->bindOutputsAttribsAndLinkProgram(builder, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 669 | isColorDeclared, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 670 | dualSourceOutputWritten)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 671 | return false; |
| 672 | } |
| 673 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 674 | builder.finished(fProgramID); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 675 | fUniformHandles.fRTHeightUni = builder.getRTHeightUniform(); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame^] | 676 | fUniformHandles.fDstCopyTopLeftUni = builder.getDstCopyTopLeftUniform(); |
| 677 | fUniformHandles.fDstCopyScaleUni = builder.getDstCopyScaleUniform(); |
| 678 | fUniformHandles.fDstCopySamplerUni = builder.getDstCopySamplerUniform(); |
| 679 | // This must be called after we set fDstCopySamplerUni above. |
| 680 | this->initSamplerUniforms(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 681 | |
| 682 | return true; |
| 683 | } |
| 684 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 685 | bool GrGLProgram::bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 686 | bool bindColorOut, |
| 687 | bool bindDualSrcOut) { |
| 688 | GL_CALL_RET(fProgramID, CreateProgram()); |
| 689 | if (!fProgramID) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 690 | return false; |
| 691 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 692 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 693 | GL_CALL(AttachShader(fProgramID, fVShaderID)); |
| 694 | if (fGShaderID) { |
| 695 | GL_CALL(AttachShader(fProgramID, fGShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 696 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 697 | GL_CALL(AttachShader(fProgramID, fFShaderID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 698 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 699 | if (bindColorOut) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 700 | GL_CALL(BindFragDataLocation(fProgramID, 0, declared_color_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 701 | } |
| 702 | if (bindDualSrcOut) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 703 | GL_CALL(BindFragDataLocationIndexed(fProgramID, 0, 1, dual_source_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 704 | } |
| 705 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 706 | // Bind the attrib locations to same values for all shaders |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 707 | GL_CALL(BindAttribLocation(fProgramID, |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 708 | fDesc.fPositionAttributeIndex, |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 709 | builder.positionAttribute().c_str())); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 710 | GL_CALL(BindAttribLocation(fProgramID, fDesc.fColorAttributeIndex, COL_ATTR_NAME)); |
| 711 | GL_CALL(BindAttribLocation(fProgramID, fDesc.fCoverageAttributeIndex, COV_ATTR_NAME)); |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 712 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 713 | if (fDesc.fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit) { |
| 714 | GL_CALL(BindAttribLocation(fProgramID, |
| 715 | fDesc.fLocalCoordsAttributeIndex, |
| 716 | builder.localCoordsAttribute().c_str())); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 717 | } |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 718 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 719 | const GrGLShaderBuilder::AttributePair* attribEnd = builder.getEffectAttributes().end(); |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 720 | for (const GrGLShaderBuilder::AttributePair* attrib = builder.getEffectAttributes().begin(); |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 721 | attrib != attribEnd; |
| 722 | ++attrib) { |
| 723 | GL_CALL(BindAttribLocation(fProgramID, attrib->fIndex, attrib->fName.c_str())); |
| 724 | } |
| 725 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 726 | GL_CALL(LinkProgram(fProgramID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 727 | |
| 728 | GrGLint linked = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 729 | GL_CALL(GetProgramiv(fProgramID, GR_GL_LINK_STATUS, &linked)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 730 | if (!linked) { |
| 731 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 732 | GL_CALL(GetProgramiv(fProgramID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 733 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 734 | if (infoLen > 0) { |
bsalomon@google.com | 79afcaa | 2011-09-14 14:29:18 +0000 | [diff] [blame] | 735 | // retrieve length even though we don't need it to workaround |
| 736 | // bug in chrome cmd buffer param validation. |
| 737 | GrGLsizei length = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 738 | GL_CALL(GetProgramInfoLog(fProgramID, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 739 | infoLen+1, |
| 740 | &length, |
| 741 | (char*)log.get())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 742 | GrPrintf((char*)log.get()); |
| 743 | } |
| 744 | GrAssert(!"Error linking program"); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 745 | GL_CALL(DeleteProgram(fProgramID)); |
| 746 | fProgramID = 0; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 747 | return false; |
| 748 | } |
| 749 | return true; |
| 750 | } |
| 751 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 752 | void GrGLProgram::initSamplerUniforms() { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 753 | GL_CALL(UseProgram(fProgramID)); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame^] | 754 | // We simply bind the uniforms to successive texture units beginning at 0. setData() assumes |
| 755 | // this behavior. |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 756 | GrGLint texUnitIdx = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame^] | 757 | if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fDstCopySamplerUni) { |
| 758 | fUniformManager.setSampler(fUniformHandles.fDstCopySamplerUni, texUnitIdx); |
| 759 | ++texUnitIdx; |
| 760 | } |
| 761 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 762 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame^] | 763 | int numSamplers = fUniformHandles.fEffectSamplerUnis[s].count(); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 764 | for (int u = 0; u < numSamplers; ++u) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame^] | 765 | UniformHandle handle = fUniformHandles.fEffectSamplerUnis[s][u]; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 766 | if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
| 767 | fUniformManager.setSampler(handle, texUnitIdx); |
| 768 | ++texUnitIdx; |
bsalomon@google.com | 0982d35 | 2012-07-31 15:33:25 +0000 | [diff] [blame] | 769 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 770 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 771 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 772 | } |
| 773 | |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 774 | /////////////////////////////////////////////////////////////////////////////// |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 775 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 776 | void GrGLProgram::setData(GrGpuGL* gpu, |
| 777 | GrColor color, |
| 778 | GrColor coverage, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame^] | 779 | const GrDeviceCoordTexture* dstCopy, |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 780 | SharedGLState* sharedState) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 781 | const GrDrawState& drawState = gpu->getDrawState(); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 782 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 783 | this->setColor(drawState, color, sharedState); |
| 784 | this->setCoverage(drawState, coverage, sharedState); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 785 | this->setMatrixAndRenderTargetHeight(drawState); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 786 | |
| 787 | // Setup the SkXfermode::Mode-based colorfilter uniform if necessary |
| 788 | if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fColorFilterUni && |
| 789 | fColorFilterColor != drawState.getColorFilterColor()) { |
| 790 | GrGLfloat c[4]; |
| 791 | GrColorToRGBAFloat(drawState.getColorFilterColor(), c); |
| 792 | fUniformManager.set4fv(fUniformHandles.fColorFilterUni, 0, 1, c); |
| 793 | fColorFilterColor = drawState.getColorFilterColor(); |
| 794 | } |
| 795 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 796 | GrGLint texUnitIdx = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame^] | 797 | if (NULL != dstCopy) { |
| 798 | if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fDstCopyTopLeftUni) { |
| 799 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fDstCopyScaleUni); |
| 800 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != |
| 801 | fUniformHandles.fDstCopySamplerUni); |
| 802 | fUniformManager.set2f(fUniformHandles.fDstCopyTopLeftUni, |
| 803 | static_cast<GrGLfloat>(dstCopy->offset().fX), |
| 804 | static_cast<GrGLfloat>(dstCopy->offset().fY)); |
| 805 | fUniformManager.set2f(fUniformHandles.fDstCopyScaleUni, |
| 806 | 1.f / dstCopy->texture()->width(), |
| 807 | 1.f / dstCopy->texture()->height()); |
| 808 | GrGLTexture* texture = static_cast<GrGLTexture*>(dstCopy->texture()); |
| 809 | static GrTextureParams kParams; // the default is clamp, nearest filtering. |
| 810 | gpu->bindTexture(texUnitIdx, kParams, texture); |
| 811 | ++texUnitIdx; |
| 812 | } else { |
| 813 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == |
| 814 | fUniformHandles.fDstCopyScaleUni); |
| 815 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == |
| 816 | fUniformHandles.fDstCopySamplerUni); |
| 817 | } |
| 818 | } else { |
| 819 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == |
| 820 | fUniformHandles.fDstCopyTopLeftUni); |
| 821 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == |
| 822 | fUniformHandles.fDstCopyScaleUni); |
| 823 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == |
| 824 | fUniformHandles.fDstCopySamplerUni); |
| 825 | } |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 826 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 827 | if (NULL != fEffects[s]) { |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 828 | const GrEffectStage& stage = drawState.getStage(s); |
| 829 | GrAssert(NULL != stage.getEffect()); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 830 | |
| 831 | bool explicitLocalCoords = |
| 832 | (fDesc.fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit); |
| 833 | GrDrawEffect drawEffect(stage, explicitLocalCoords); |
| 834 | fEffects[s]->setData(fUniformManager, drawEffect); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame^] | 835 | int numSamplers = fUniformHandles.fEffectSamplerUnis[s].count(); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 836 | for (int u = 0; u < numSamplers; ++u) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame^] | 837 | UniformHandle handle = fUniformHandles.fEffectSamplerUnis[s][u]; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 838 | if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 839 | const GrTextureAccess& access = (*stage.getEffect())->textureAccess(u); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 840 | GrGLTexture* texture = static_cast<GrGLTexture*>(access.getTexture()); |
| 841 | gpu->bindTexture(texUnitIdx, access.getParams(), texture); |
| 842 | ++texUnitIdx; |
| 843 | } |
| 844 | } |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 845 | } |
| 846 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 847 | } |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 848 | |
| 849 | void GrGLProgram::setColor(const GrDrawState& drawState, |
| 850 | GrColor color, |
| 851 | SharedGLState* sharedState) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 852 | if (!(drawState.getAttribBindings() & GrDrawState::kColor_AttribBindingsBit)) { |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 853 | switch (fDesc.fColorInput) { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 854 | case GrGLProgramDesc::kAttribute_ColorInput: |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 855 | if (sharedState->fConstAttribColor != color) { |
| 856 | // OpenGL ES only supports the float varieties of glVertexAttrib |
| 857 | GrGLfloat c[4]; |
| 858 | GrColorToRGBAFloat(color, c); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 859 | GL_CALL(VertexAttrib4fv(fDesc.fColorAttributeIndex, c)); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 860 | sharedState->fConstAttribColor = color; |
| 861 | } |
| 862 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 863 | case GrGLProgramDesc::kUniform_ColorInput: |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 864 | if (fColor != color) { |
| 865 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 866 | GrGLfloat c[4]; |
| 867 | GrColorToRGBAFloat(color, c); |
| 868 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != |
| 869 | fUniformHandles.fColorUni); |
| 870 | fUniformManager.set4fv(fUniformHandles.fColorUni, 0, 1, c); |
| 871 | fColor = color; |
| 872 | } |
| 873 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 874 | case GrGLProgramDesc::kSolidWhite_ColorInput: |
| 875 | case GrGLProgramDesc::kTransBlack_ColorInput: |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 876 | break; |
| 877 | default: |
| 878 | GrCrash("Unknown color type."); |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | void GrGLProgram::setCoverage(const GrDrawState& drawState, |
| 884 | GrColor coverage, |
| 885 | SharedGLState* sharedState) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 886 | if (!(drawState.getAttribBindings() & GrDrawState::kCoverage_AttribBindingsBit)) { |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 887 | switch (fDesc.fCoverageInput) { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 888 | case GrGLProgramDesc::kAttribute_ColorInput: |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 889 | if (sharedState->fConstAttribCoverage != coverage) { |
| 890 | // OpenGL ES only supports the float varieties of glVertexAttrib |
| 891 | GrGLfloat c[4]; |
| 892 | GrColorToRGBAFloat(coverage, c); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 893 | GL_CALL(VertexAttrib4fv(fDesc.fCoverageAttributeIndex, c)); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 894 | sharedState->fConstAttribCoverage = coverage; |
| 895 | } |
| 896 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 897 | case GrGLProgramDesc::kUniform_ColorInput: |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 898 | if (fCoverage != coverage) { |
| 899 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 900 | GrGLfloat c[4]; |
| 901 | GrColorToRGBAFloat(coverage, c); |
| 902 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != |
| 903 | fUniformHandles.fCoverageUni); |
| 904 | fUniformManager.set4fv(fUniformHandles.fCoverageUni, 0, 1, c); |
| 905 | fCoverage = coverage; |
| 906 | } |
| 907 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 908 | case GrGLProgramDesc::kSolidWhite_ColorInput: |
| 909 | case GrGLProgramDesc::kTransBlack_ColorInput: |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 910 | break; |
| 911 | default: |
| 912 | GrCrash("Unknown coverage type."); |
| 913 | } |
| 914 | } |
| 915 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 916 | |
| 917 | void GrGLProgram::setMatrixAndRenderTargetHeight(const GrDrawState& drawState) { |
| 918 | const GrRenderTarget* rt = drawState.getRenderTarget(); |
| 919 | SkISize size; |
| 920 | size.set(rt->width(), rt->height()); |
| 921 | |
| 922 | // Load the RT height uniform if it is needed to y-flip gl_FragCoord. |
| 923 | if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fRTHeightUni && |
| 924 | fMatrixState.fRenderTargetSize.fHeight != size.fHeight) { |
| 925 | fUniformManager.set1f(fUniformHandles.fRTHeightUni, SkIntToScalar(size.fHeight)); |
| 926 | } |
| 927 | |
| 928 | if (fMatrixState.fRenderTargetOrigin != rt->origin() || |
| 929 | !fMatrixState.fViewMatrix.cheapEqualTo(drawState.getViewMatrix()) || |
| 930 | fMatrixState.fRenderTargetSize != size) { |
| 931 | SkMatrix m; |
| 932 | if (kBottomLeft_GrSurfaceOrigin == rt->origin()) { |
| 933 | m.setAll( |
| 934 | SkIntToScalar(2) / size.fWidth, 0, -SK_Scalar1, |
| 935 | 0,-SkIntToScalar(2) / size.fHeight, SK_Scalar1, |
| 936 | 0, 0, SkMatrix::I()[8]); |
| 937 | } else { |
| 938 | m.setAll( |
| 939 | SkIntToScalar(2) / size.fWidth, 0, -SK_Scalar1, |
| 940 | 0, SkIntToScalar(2) / size.fHeight,-SK_Scalar1, |
| 941 | 0, 0, SkMatrix::I()[8]); |
| 942 | } |
| 943 | m.setConcat(m, drawState.getViewMatrix()); |
| 944 | |
| 945 | // ES doesn't allow you to pass true to the transpose param so we do our own transpose. |
| 946 | GrGLfloat mt[] = { |
| 947 | SkScalarToFloat(m[SkMatrix::kMScaleX]), |
| 948 | SkScalarToFloat(m[SkMatrix::kMSkewY]), |
| 949 | SkScalarToFloat(m[SkMatrix::kMPersp0]), |
| 950 | SkScalarToFloat(m[SkMatrix::kMSkewX]), |
| 951 | SkScalarToFloat(m[SkMatrix::kMScaleY]), |
| 952 | SkScalarToFloat(m[SkMatrix::kMPersp1]), |
| 953 | SkScalarToFloat(m[SkMatrix::kMTransX]), |
| 954 | SkScalarToFloat(m[SkMatrix::kMTransY]), |
| 955 | SkScalarToFloat(m[SkMatrix::kMPersp2]) |
| 956 | }; |
| 957 | fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, mt); |
| 958 | fMatrixState.fViewMatrix = drawState.getViewMatrix(); |
| 959 | fMatrixState.fRenderTargetSize = size; |
| 960 | fMatrixState.fRenderTargetOrigin = rt->origin(); |
| 961 | } |
| 962 | } |