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