tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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. |
| 6 | */ |
| 7 | |
| 8 | #include "gl/GrGLShaderBuilder.h" |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 9 | #include "gl/GrGLProgram.h" |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 10 | #include "gl/GrGLUniformHandle.h" |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 11 | #include "GrCoordTransform.h" |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 12 | #include "GrDrawEffect.h" |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 13 | #include "GrGpuGL.h" |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 14 | #include "GrTexture.h" |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 15 | #include "SkRTConf.h" |
| 16 | #include "SkTrace.h" |
| 17 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 18 | #define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X) |
| 19 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X) |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 20 | |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 21 | // number of each input/output type in a single allocation block |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 22 | static const int kVarsPerBlock = 8; |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 23 | |
| 24 | // except FS outputs where we expect 2 at most. |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 25 | static const int kMaxFSOutputs = 2; |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 26 | |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 27 | // ES2 FS only guarantees mediump and lowp support |
| 28 | static const GrGLShaderVar::Precision kDefaultFragmentPrecision = GrGLShaderVar::kMedium_Precision; |
| 29 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 30 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 31 | |
| 32 | SK_CONF_DECLARE(bool, c_PrintShaders, "gpu.printShaders", false, |
| 33 | "Print the source code for all shaders generated."); |
| 34 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 37 | namespace { |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 38 | |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 39 | inline const char* color_attribute_name() { return "aColor"; } |
| 40 | inline const char* coverage_attribute_name() { return "aCoverage"; } |
| 41 | inline const char* declared_color_output_name() { return "fsColorOut"; } |
| 42 | inline const char* dual_source_output_name() { return "dualSourceOut"; } |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 43 | inline const char* sample_function_name(GrSLType type, GrGLSLGeneration glslGen) { |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 44 | if (kVec2f_GrSLType == type) { |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 45 | return glslGen >= k130_GrGLSLGeneration ? "texture" : "texture2D"; |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 46 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 47 | SkASSERT(kVec3f_GrSLType == type); |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 48 | return glslGen >= k130_GrGLSLGeneration ? "textureProj" : "texture2DProj"; |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 49 | } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 50 | } |
| 51 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 52 | void append_texture_lookup(SkString* out, |
| 53 | GrGpuGL* gpu, |
| 54 | const char* samplerName, |
| 55 | const char* coordName, |
| 56 | uint32_t configComponentMask, |
| 57 | const char* swizzle, |
| 58 | GrSLType varyingType = kVec2f_GrSLType) { |
| 59 | SkASSERT(NULL != coordName); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 60 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 61 | out->appendf("%s(%s, %s)", |
| 62 | sample_function_name(varyingType, gpu->glslGeneration()), |
| 63 | samplerName, |
| 64 | coordName); |
| 65 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 66 | char mangledSwizzle[5]; |
| 67 | |
| 68 | // The swizzling occurs using texture params instead of shader-mangling if ARB_texture_swizzle |
| 69 | // is available. |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 70 | if (!gpu->glCaps().textureSwizzleSupport() && |
| 71 | (kA_GrColorComponentFlag == configComponentMask)) { |
| 72 | char alphaChar = gpu->glCaps().textureRedSupport() ? 'r' : 'a'; |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 73 | int i; |
| 74 | for (i = 0; '\0' != swizzle[i]; ++i) { |
| 75 | mangledSwizzle[i] = alphaChar; |
| 76 | } |
| 77 | mangledSwizzle[i] ='\0'; |
| 78 | swizzle = mangledSwizzle; |
| 79 | } |
bsalomon@google.com | 73d5b2f | 2012-10-04 13:26:32 +0000 | [diff] [blame] | 80 | // For shader prettiness we omit the swizzle rather than appending ".rgba". |
| 81 | if (memcmp(swizzle, "rgba", 4)) { |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 82 | out->appendf(".%s", swizzle); |
bsalomon@google.com | 73d5b2f | 2012-10-04 13:26:32 +0000 | [diff] [blame] | 83 | } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 84 | } |
| 85 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 86 | } |
| 87 | |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 88 | static const char kDstCopyColorName[] = "_dstColor"; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 89 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 90 | /////////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 91 | |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 92 | GrGLShaderBuilder::GrGLShaderBuilder(GrGpuGL* gpu, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 93 | GrGLUniformManager& uniformManager, |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 94 | const GrGLProgramDesc& desc) |
| 95 | : fGpu(gpu) |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 96 | , fUniformManager(uniformManager) |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 97 | , fFSFeaturesAddedMask(0) |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 98 | , fFSInputs(kVarsPerBlock) |
| 99 | , fFSOutputs(kMaxFSOutputs) |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 100 | , fUniforms(kVarsPerBlock) |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 101 | , fSetupFragPosition(false) |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 102 | , fHasCustomColorOutput(false) |
| 103 | , fHasSecondaryOutput(false) |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 104 | , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == desc.getHeader().fFragPosKey) { |
| 105 | |
| 106 | const GrGLProgramDesc::KeyHeader& header = desc.getHeader(); |
skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 107 | |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 108 | // Emit code to read the dst copy textue if necessary. |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 109 | if (kNoDstRead_DstReadKey != header.fDstReadKey && |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 110 | GrGLCaps::kNone_FBFetchType == fGpu->glCaps().fbFetchType()) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 111 | bool topDown = SkToBool(kTopLeftOrigin_DstReadKeyBit & header.fDstReadKey); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 112 | const char* dstCopyTopLeftName; |
| 113 | const char* dstCopyCoordScaleName; |
| 114 | uint32_t configMask; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 115 | if (SkToBool(kUseAlphaConfig_DstReadKeyBit & header.fDstReadKey)) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 116 | configMask = kA_GrColorComponentFlag; |
| 117 | } else { |
| 118 | configMask = kRGBA_GrColorComponentFlags; |
| 119 | } |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 120 | fDstCopySamplerUniform = this->addUniform(kFragment_Visibility, |
| 121 | kSampler2D_GrSLType, |
| 122 | "DstCopySampler"); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 123 | fDstCopyTopLeftUniform = this->addUniform(kFragment_Visibility, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 124 | kVec2f_GrSLType, |
| 125 | "DstCopyUpperLeft", |
| 126 | &dstCopyTopLeftName); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 127 | fDstCopyScaleUniform = this->addUniform(kFragment_Visibility, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 128 | kVec2f_GrSLType, |
| 129 | "DstCopyCoordScale", |
| 130 | &dstCopyCoordScaleName); |
| 131 | const char* fragPos = this->fragmentPosition(); |
| 132 | this->fsCodeAppend("\t// Read color from copy of the destination.\n"); |
| 133 | this->fsCodeAppendf("\tvec2 _dstTexCoord = (%s.xy - %s) * %s;\n", |
| 134 | fragPos, dstCopyTopLeftName, dstCopyCoordScaleName); |
| 135 | if (!topDown) { |
| 136 | this->fsCodeAppend("\t_dstTexCoord.y = 1.0 - _dstTexCoord.y;\n"); |
| 137 | } |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 138 | this->fsCodeAppendf("\tvec4 %s = ", kDstCopyColorName); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 139 | append_texture_lookup(&fFSCode, |
| 140 | fGpu, |
| 141 | this->getUniformCStr(fDstCopySamplerUniform), |
| 142 | "_dstTexCoord", |
| 143 | configMask, |
| 144 | "rgba"); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 145 | this->fsCodeAppend(";\n\n"); |
| 146 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 147 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 148 | if (GrGLProgramDesc::kUniform_ColorInput == header.fColorInput) { |
| 149 | const char* name; |
| 150 | fColorUniform = this->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 151 | kVec4f_GrSLType, "Color", &name); |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 152 | fInputColor = GrGLSLExpr4(name); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 153 | } else if (GrGLProgramDesc::kSolidWhite_ColorInput == header.fColorInput) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 154 | fInputColor = GrGLSLExpr4(1); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 155 | } else if (GrGLProgramDesc::kTransBlack_ColorInput == header.fColorInput) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 156 | fInputColor = GrGLSLExpr4(0); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 157 | } |
| 158 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 159 | if (GrGLProgramDesc::kUniform_ColorInput == header.fCoverageInput) { |
| 160 | const char* name; |
| 161 | fCoverageUniform = this->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 162 | kVec4f_GrSLType, "Coverage", &name); |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 163 | fInputCoverage = GrGLSLExpr4(name); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 164 | } else if (GrGLProgramDesc::kSolidWhite_ColorInput == header.fCoverageInput) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 165 | fInputCoverage = GrGLSLExpr4(1); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 166 | } else if (GrGLProgramDesc::kTransBlack_ColorInput == header.fCoverageInput) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 167 | fInputCoverage = GrGLSLExpr4(0); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | if (k110_GrGLSLGeneration != fGpu->glslGeneration()) { |
| 171 | fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 172 | GrGLShaderVar::kOut_TypeModifier, |
| 173 | declared_color_output_name()); |
| 174 | fHasCustomColorOutput = true; |
| 175 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 176 | } |
| 177 | |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 178 | bool GrGLShaderBuilder::enableFeature(GLSLFeature feature) { |
| 179 | switch (feature) { |
| 180 | case kStandardDerivatives_GLSLFeature: |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 181 | if (!fGpu->glCaps().shaderDerivativeSupport()) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 182 | return false; |
| 183 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 184 | if (kES_GrGLBinding == fGpu->glBinding()) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 185 | this->addFSFeature(1 << kStandardDerivatives_GLSLFeature, |
| 186 | "GL_OES_standard_derivatives"); |
| 187 | } |
| 188 | return true; |
| 189 | default: |
| 190 | GrCrash("Unexpected GLSLFeature requested."); |
| 191 | return false; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | bool GrGLShaderBuilder::enablePrivateFeature(GLSLPrivateFeature feature) { |
| 196 | switch (feature) { |
| 197 | case kFragCoordConventions_GLSLPrivateFeature: |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 198 | if (!fGpu->glCaps().fragCoordConventionsSupport()) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 199 | return false; |
| 200 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 201 | if (fGpu->glslGeneration() < k150_GrGLSLGeneration) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 202 | this->addFSFeature(1 << kFragCoordConventions_GLSLPrivateFeature, |
| 203 | "GL_ARB_fragment_coord_conventions"); |
| 204 | } |
| 205 | return true; |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 206 | case kEXTShaderFramebufferFetch_GLSLPrivateFeature: |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 207 | if (GrGLCaps::kEXT_FBFetchType != fGpu->glCaps().fbFetchType()) { |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | this->addFSFeature(1 << kEXTShaderFramebufferFetch_GLSLPrivateFeature, |
| 211 | "GL_EXT_shader_framebuffer_fetch"); |
| 212 | return true; |
| 213 | case kNVShaderFramebufferFetch_GLSLPrivateFeature: |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 214 | if (GrGLCaps::kNV_FBFetchType != fGpu->glCaps().fbFetchType()) { |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | this->addFSFeature(1 << kNVShaderFramebufferFetch_GLSLPrivateFeature, |
| 218 | "GL_NV_shader_framebuffer_fetch"); |
| 219 | return true; |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 220 | default: |
| 221 | GrCrash("Unexpected GLSLPrivateFeature requested."); |
| 222 | return false; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void GrGLShaderBuilder::addFSFeature(uint32_t featureBit, const char* extensionName) { |
| 227 | if (!(featureBit & fFSFeaturesAddedMask)) { |
| 228 | fFSExtensions.appendf("#extension %s: require\n", extensionName); |
| 229 | fFSFeaturesAddedMask |= featureBit; |
| 230 | } |
| 231 | } |
| 232 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 233 | void GrGLShaderBuilder::nameVariable(SkString* out, char prefix, const char* name) { |
| 234 | if ('\0' == prefix) { |
| 235 | *out = name; |
| 236 | } else { |
| 237 | out->printf("%c%s", prefix, name); |
| 238 | } |
| 239 | if (fCodeStage.inStageCode()) { |
| 240 | if (out->endsWith('_')) { |
| 241 | // Names containing "__" are reserved. |
| 242 | out->append("x"); |
| 243 | } |
| 244 | out->appendf("_Stage%d", fCodeStage.stageIndex()); |
| 245 | } |
| 246 | } |
| 247 | |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 248 | const char* GrGLShaderBuilder::dstColor() { |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 249 | if (fCodeStage.inStageCode()) { |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 250 | const GrEffectRef& effect = *fCodeStage.effectStage()->getEffect(); |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 251 | if (!effect->willReadDstColor()) { |
| 252 | GrDebugCrash("GrGLEffect asked for dst color but its generating GrEffect " |
| 253 | "did not request access."); |
| 254 | return ""; |
| 255 | } |
| 256 | } |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 257 | static const char kFBFetchColorName[] = "gl_LastFragData[0]"; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 258 | GrGLCaps::FBFetchType fetchType = fGpu->glCaps().fbFetchType(); |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 259 | if (GrGLCaps::kEXT_FBFetchType == fetchType) { |
| 260 | SkAssertResult(this->enablePrivateFeature(kEXTShaderFramebufferFetch_GLSLPrivateFeature)); |
| 261 | return kFBFetchColorName; |
| 262 | } else if (GrGLCaps::kNV_FBFetchType == fetchType) { |
| 263 | SkAssertResult(this->enablePrivateFeature(kNVShaderFramebufferFetch_GLSLPrivateFeature)); |
| 264 | return kFBFetchColorName; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 265 | } else if (fDstCopySamplerUniform.isValid()) { |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 266 | return kDstCopyColorName; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 267 | } else { |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 268 | return ""; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 269 | } |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 270 | } |
| 271 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 272 | void GrGLShaderBuilder::appendTextureLookup(SkString* out, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 273 | const GrGLShaderBuilder::TextureSampler& sampler, |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 274 | const char* coordName, |
| 275 | GrSLType varyingType) const { |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 276 | append_texture_lookup(out, |
| 277 | fGpu, |
| 278 | this->getUniformCStr(sampler.samplerUniform()), |
| 279 | coordName, |
| 280 | sampler.configComponentMask(), |
| 281 | sampler.swizzle(), |
| 282 | varyingType); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 283 | } |
| 284 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 285 | void GrGLShaderBuilder::fsAppendTextureLookup(const GrGLShaderBuilder::TextureSampler& sampler, |
| 286 | const char* coordName, |
| 287 | GrSLType varyingType) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 288 | this->appendTextureLookup(&fFSCode, sampler, coordName, varyingType); |
| 289 | } |
| 290 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 291 | void GrGLShaderBuilder::fsAppendTextureLookupAndModulate( |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 292 | const char* modulation, |
| 293 | const GrGLShaderBuilder::TextureSampler& sampler, |
| 294 | const char* coordName, |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 295 | GrSLType varyingType) { |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 296 | SkString lookup; |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 297 | this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 298 | fFSCode.append((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 299 | } |
| 300 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 301 | GrGLShaderBuilder::DstReadKey GrGLShaderBuilder::KeyForDstRead(const GrTexture* dstCopy, |
| 302 | const GrGLCaps& caps) { |
| 303 | uint32_t key = kYesDstRead_DstReadKeyBit; |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 304 | if (GrGLCaps::kNone_FBFetchType != caps.fbFetchType()) { |
| 305 | return key; |
| 306 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 307 | SkASSERT(NULL != dstCopy); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 308 | if (!caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(dstCopy->config())) { |
| 309 | // The fact that the config is alpha-only must be considered when generating code. |
| 310 | key |= kUseAlphaConfig_DstReadKeyBit; |
| 311 | } |
| 312 | if (kTopLeft_GrSurfaceOrigin == dstCopy->origin()) { |
| 313 | key |= kTopLeftOrigin_DstReadKeyBit; |
| 314 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 315 | SkASSERT(static_cast<DstReadKey>(key) == key); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 316 | return static_cast<DstReadKey>(key); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 317 | } |
| 318 | |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 319 | GrGLShaderBuilder::FragPosKey GrGLShaderBuilder::KeyForFragmentPosition(const GrRenderTarget* dst, |
| 320 | const GrGLCaps&) { |
| 321 | if (kTopLeft_GrSurfaceOrigin == dst->origin()) { |
| 322 | return kTopLeftFragPosRead_FragPosKey; |
| 323 | } else { |
| 324 | return kBottomLeftFragPosRead_FragPosKey; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 329 | const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps) { |
| 330 | if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) { |
| 331 | if (caps.textureRedSupport()) { |
| 332 | static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RED, GR_GL_RED }; |
| 333 | return gRedSmear; |
| 334 | } else { |
| 335 | static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 336 | GR_GL_ALPHA, GR_GL_ALPHA }; |
| 337 | return gAlphaSmear; |
| 338 | } |
| 339 | } else { |
| 340 | static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE, GR_GL_ALPHA }; |
| 341 | return gStraight; |
| 342 | } |
| 343 | } |
| 344 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 345 | GrGLUniformManager::UniformHandle GrGLShaderBuilder::addUniformArray(uint32_t visibility, |
| 346 | GrSLType type, |
| 347 | const char* name, |
| 348 | int count, |
| 349 | const char** outName) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 350 | SkASSERT(name && strlen(name)); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 351 | SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_Visibility | kFragment_Visibility); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 352 | SkASSERT(0 == (~kVisibilityMask & visibility)); |
| 353 | SkASSERT(0 != visibility); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 354 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 355 | BuilderUniform& uni = fUniforms.push_back(); |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 356 | UniformHandle h = GrGLUniformManager::UniformHandle::CreateFromUniformIndex(fUniforms.count() - 1); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 357 | SkDEBUGCODE(UniformHandle h2 =) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 358 | fUniformManager.appendUniform(type, count); |
| 359 | // We expect the uniform manager to initially have no uniforms and that all uniforms are added |
| 360 | // by this function. Therefore, the handles should match. |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 361 | SkASSERT(h2 == h); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 362 | uni.fVariable.setType(type); |
| 363 | uni.fVariable.setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 364 | this->nameVariable(uni.fVariable.accessName(), 'u', name); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 365 | uni.fVariable.setArrayCount(count); |
| 366 | uni.fVisibility = visibility; |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 367 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 368 | // If it is visible in both the VS and FS, the precision must match. |
| 369 | // We declare a default FS precision, but not a default VS. So set the var |
| 370 | // to use the default FS precision. |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 371 | if ((kVertex_Visibility | kFragment_Visibility) == visibility) { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 372 | // the fragment and vertex precisions must match |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 373 | uni.fVariable.setPrecision(kDefaultFragmentPrecision); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 374 | } |
| 375 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 376 | if (NULL != outName) { |
| 377 | *outName = uni.fVariable.c_str(); |
| 378 | } |
| 379 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 380 | return h; |
| 381 | } |
| 382 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 383 | SkString GrGLShaderBuilder::ensureFSCoords2D(const TransformedCoordsArray& coords, int index) { |
| 384 | if (kVec3f_GrSLType != coords[index].type()) { |
| 385 | SkASSERT(kVec2f_GrSLType == coords[index].type()); |
| 386 | return coords[index].getName(); |
| 387 | } |
| 388 | |
| 389 | SkString coords2D("coords2D"); |
| 390 | if (0 != index) { |
| 391 | coords2D.appendf("_%i", index); |
| 392 | } |
| 393 | this->fsCodeAppendf("\tvec2 %s = %s.xy / %s.z;", |
| 394 | coords2D.c_str(), coords[index].c_str(), coords[index].c_str()); |
| 395 | return coords2D; |
| 396 | } |
| 397 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 398 | const char* GrGLShaderBuilder::fragmentPosition() { |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 399 | if (fCodeStage.inStageCode()) { |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 400 | const GrEffectRef& effect = *fCodeStage.effectStage()->getEffect(); |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 401 | if (!effect->willReadFragmentPosition()) { |
| 402 | GrDebugCrash("GrGLEffect asked for frag position but its generating GrEffect " |
| 403 | "did not request access."); |
| 404 | return ""; |
| 405 | } |
| 406 | } |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 407 | if (fTopLeftFragPosRead) { |
| 408 | if (!fSetupFragPosition) { |
| 409 | fFSInputs.push_back().set(kVec4f_GrSLType, |
| 410 | GrGLShaderVar::kIn_TypeModifier, |
| 411 | "gl_FragCoord", |
| 412 | GrGLShaderVar::kDefault_Precision); |
| 413 | fSetupFragPosition = true; |
| 414 | } |
| 415 | return "gl_FragCoord"; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 416 | } else if (fGpu->glCaps().fragCoordConventionsSupport()) { |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 417 | if (!fSetupFragPosition) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 418 | SkAssertResult(this->enablePrivateFeature(kFragCoordConventions_GLSLPrivateFeature)); |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 419 | fFSInputs.push_back().set(kVec4f_GrSLType, |
| 420 | GrGLShaderVar::kIn_TypeModifier, |
| 421 | "gl_FragCoord", |
| 422 | GrGLShaderVar::kDefault_Precision, |
| 423 | GrGLShaderVar::kUpperLeft_Origin); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 424 | fSetupFragPosition = true; |
| 425 | } |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 426 | return "gl_FragCoord"; |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 427 | } else { |
| 428 | static const char* kCoordName = "fragCoordYDown"; |
| 429 | if (!fSetupFragPosition) { |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 430 | // temporarily change the stage index because we're inserting non-stage code. |
| 431 | CodeStage::AutoStageRestore csar(&fCodeStage, NULL); |
| 432 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 433 | SkASSERT(!fRTHeightUniform.isValid()); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 434 | const char* rtHeightName; |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 435 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 436 | fRTHeightUniform = this->addUniform(kFragment_Visibility, |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 437 | kFloat_GrSLType, |
| 438 | "RTHeight", |
| 439 | &rtHeightName); |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 440 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 441 | this->fFSCode.prependf("\tvec4 %s = vec4(gl_FragCoord.x, %s - gl_FragCoord.y, gl_FragCoord.zw);\n", |
| 442 | kCoordName, rtHeightName); |
| 443 | fSetupFragPosition = true; |
| 444 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 445 | SkASSERT(fRTHeightUniform.isValid()); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 446 | return kCoordName; |
| 447 | } |
| 448 | } |
| 449 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 450 | void GrGLShaderBuilder::fsEmitFunction(GrSLType returnType, |
| 451 | const char* name, |
| 452 | int argCnt, |
| 453 | const GrGLShaderVar* args, |
| 454 | const char* body, |
| 455 | SkString* outName) { |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 456 | fFSFunctions.append(GrGLSLTypeString(returnType)); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 457 | this->nameVariable(outName, '\0', name); |
bsalomon@google.com | 77cf460 | 2013-04-22 21:05:48 +0000 | [diff] [blame] | 458 | fFSFunctions.appendf(" %s", outName->c_str()); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 459 | fFSFunctions.append("("); |
| 460 | for (int i = 0; i < argCnt; ++i) { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 461 | args[i].appendDecl(this->ctxInfo(), &fFSFunctions); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 462 | if (i < argCnt - 1) { |
| 463 | fFSFunctions.append(", "); |
| 464 | } |
| 465 | } |
| 466 | fFSFunctions.append(") {\n"); |
| 467 | fFSFunctions.append(body); |
| 468 | fFSFunctions.append("}\n\n"); |
| 469 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 470 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 471 | namespace { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 472 | |
| 473 | inline void append_default_precision_qualifier(GrGLShaderVar::Precision p, |
| 474 | GrGLBinding binding, |
| 475 | SkString* str) { |
| 476 | // Desktop GLSL has added precision qualifiers but they don't do anything. |
bsalomon@google.com | 791816a | 2013-08-15 18:54:39 +0000 | [diff] [blame] | 477 | if (kES_GrGLBinding == binding) { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 478 | switch (p) { |
| 479 | case GrGLShaderVar::kHigh_Precision: |
| 480 | str->append("precision highp float;\n"); |
| 481 | break; |
| 482 | case GrGLShaderVar::kMedium_Precision: |
| 483 | str->append("precision mediump float;\n"); |
| 484 | break; |
| 485 | case GrGLShaderVar::kLow_Precision: |
| 486 | str->append("precision lowp float;\n"); |
| 487 | break; |
| 488 | case GrGLShaderVar::kDefault_Precision: |
| 489 | GrCrash("Default precision now allowed."); |
| 490 | default: |
| 491 | GrCrash("Unknown precision value."); |
| 492 | } |
| 493 | } |
| 494 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 495 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 496 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 497 | void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 498 | for (int i = 0; i < vars.count(); ++i) { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 499 | vars[i].appendDecl(this->ctxInfo(), out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 500 | out->append(";\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 503 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 504 | void GrGLShaderBuilder::appendUniformDecls(ShaderVisibility visibility, |
| 505 | SkString* out) const { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 506 | for (int i = 0; i < fUniforms.count(); ++i) { |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 507 | if (fUniforms[i].fVisibility & visibility) { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 508 | fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 509 | out->append(";\n"); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 512 | } |
| 513 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 514 | void GrGLShaderBuilder::createAndEmitEffects(GrGLProgramEffectsBuilder* programEffectsBuilder, |
| 515 | const GrEffectStage* effectStages[], |
| 516 | const EffectKey effectKeys[], |
| 517 | int effectCnt, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 518 | GrGLSLExpr4* fsInOutColor) { |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 519 | bool effectEmitted = false; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 520 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 521 | GrGLSLExpr4 inColor = *fsInOutColor; |
| 522 | GrGLSLExpr4 outColor; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 523 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 524 | for (int e = 0; e < effectCnt; ++e) { |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 525 | SkASSERT(NULL != effectStages[e] && NULL != effectStages[e]->getEffect()); |
| 526 | const GrEffectStage& stage = *effectStages[e]; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 527 | |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 528 | CodeStage::AutoStageRestore csar(&fCodeStage, &stage); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 529 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 530 | if (inColor.isZeros()) { |
| 531 | SkString inColorName; |
| 532 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 533 | // Effects have no way to communicate zeros, they treat an empty string as ones. |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 534 | this->nameVariable(&inColorName, '\0', "input"); |
| 535 | this->fsCodeAppendf("\tvec4 %s = %s;\n", inColorName.c_str(), inColor.c_str()); |
| 536 | inColor = inColorName; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | // create var to hold stage result |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 540 | SkString outColorName; |
| 541 | this->nameVariable(&outColorName, '\0', "output"); |
| 542 | this->fsCodeAppendf("\tvec4 %s;\n", outColorName.c_str()); |
| 543 | outColor = outColorName; |
| 544 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 545 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 546 | programEffectsBuilder->emitEffect(stage, |
| 547 | effectKeys[e], |
| 548 | outColor.c_str(), |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 549 | inColor.isOnes() ? NULL : inColor.c_str(), |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 550 | fCodeStage.stageIndex()); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 551 | |
| 552 | inColor = outColor; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 553 | effectEmitted = true; |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 554 | } |
| 555 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 556 | if (effectEmitted) { |
| 557 | *fsInOutColor = outColor; |
| 558 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 559 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 560 | |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 561 | const char* GrGLShaderBuilder::getColorOutputName() const { |
| 562 | return fHasCustomColorOutput ? declared_color_output_name() : "gl_FragColor"; |
| 563 | } |
| 564 | |
| 565 | const char* GrGLShaderBuilder::enableSecondaryOutput() { |
| 566 | if (!fHasSecondaryOutput) { |
| 567 | fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 568 | GrGLShaderVar::kOut_TypeModifier, |
| 569 | dual_source_output_name()); |
| 570 | fHasSecondaryOutput = true; |
| 571 | } |
| 572 | return dual_source_output_name(); |
| 573 | } |
| 574 | |
| 575 | |
| 576 | bool GrGLShaderBuilder::finish(GrGLuint* outProgramId) { |
| 577 | SK_TRACE_EVENT0("GrGLShaderBuilder::finish"); |
| 578 | |
| 579 | GrGLuint programId = 0; |
| 580 | GL_CALL_RET(programId, CreateProgram()); |
| 581 | if (!programId) { |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | if (!this->compileAndAttachShaders(programId)) { |
| 586 | GL_CALL(DeleteProgram(programId)); |
| 587 | return false; |
| 588 | } |
| 589 | |
| 590 | this->bindProgramLocations(programId); |
| 591 | |
| 592 | GL_CALL(LinkProgram(programId)); |
| 593 | GrGLint linked = GR_GL_INIT_ZERO; |
| 594 | GL_CALL(GetProgramiv(programId, GR_GL_LINK_STATUS, &linked)); |
| 595 | if (!linked) { |
| 596 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 597 | GL_CALL(GetProgramiv(programId, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 598 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 599 | if (infoLen > 0) { |
| 600 | // retrieve length even though we don't need it to workaround |
| 601 | // bug in chrome cmd buffer param validation. |
| 602 | GrGLsizei length = GR_GL_INIT_ZERO; |
| 603 | GL_CALL(GetProgramInfoLog(programId, |
| 604 | infoLen+1, |
| 605 | &length, |
| 606 | (char*)log.get())); |
| 607 | GrPrintf((char*)log.get()); |
| 608 | } |
| 609 | SkDEBUGFAIL("Error linking program"); |
| 610 | GL_CALL(DeleteProgram(programId)); |
| 611 | return false; |
| 612 | } |
| 613 | |
| 614 | fUniformManager.getUniformLocations(programId, fUniforms); |
| 615 | *outProgramId = programId; |
| 616 | return true; |
| 617 | } |
| 618 | |
| 619 | namespace { |
| 620 | // Compiles a GL shader, attaches it to a program, and releases the shader's reference. |
| 621 | // (That way there's no need to hang on to the GL shader id and delete it later.) |
| 622 | bool attach_shader(const GrGLInterface* gli, |
| 623 | GrGLuint programId, |
| 624 | GrGLenum type, |
| 625 | const SkString& shaderSrc) { |
| 626 | GrGLuint shaderId; |
| 627 | GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); |
| 628 | if (0 == shaderId) { |
| 629 | return false; |
| 630 | } |
| 631 | |
| 632 | const GrGLchar* sourceStr = shaderSrc.c_str(); |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 633 | GrGLint sourceLength = static_cast<GrGLint>(shaderSrc.size()); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 634 | GR_GL_CALL(gli, ShaderSource(shaderId, 1, &sourceStr, &sourceLength)); |
| 635 | |
| 636 | GrGLint compiled = GR_GL_INIT_ZERO; |
| 637 | GR_GL_CALL(gli, CompileShader(shaderId)); |
| 638 | GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_COMPILE_STATUS, &compiled)); |
| 639 | |
| 640 | if (!compiled) { |
| 641 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 642 | GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 643 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 644 | if (infoLen > 0) { |
| 645 | // retrieve length even though we don't need it to workaround bug in chrome cmd buffer |
| 646 | // param validation. |
| 647 | GrGLsizei length = GR_GL_INIT_ZERO; |
| 648 | GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, |
| 649 | &length, (char*)log.get())); |
| 650 | GrPrintf(shaderSrc.c_str()); |
| 651 | GrPrintf("\n%s", log.get()); |
| 652 | } |
| 653 | SkDEBUGFAIL("Shader compilation failed!"); |
| 654 | GR_GL_CALL(gli, DeleteShader(shaderId)); |
| 655 | return false; |
| 656 | } else if (c_PrintShaders) { |
| 657 | GrPrintf(shaderSrc.c_str()); |
| 658 | GrPrintf("\n"); |
| 659 | } |
| 660 | |
| 661 | GR_GL_CALL(gli, AttachShader(programId, shaderId)); |
| 662 | GR_GL_CALL(gli, DeleteShader(shaderId)); |
| 663 | return true; |
| 664 | } |
| 665 | |
| 666 | } |
| 667 | |
| 668 | bool GrGLShaderBuilder::compileAndAttachShaders(GrGLuint programId) const { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 669 | SkString fragShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); |
| 670 | fragShaderSrc.append(fFSExtensions); |
| 671 | append_default_precision_qualifier(kDefaultFragmentPrecision, |
| 672 | fGpu->glBinding(), |
| 673 | &fragShaderSrc); |
| 674 | this->appendUniformDecls(kFragment_Visibility, &fragShaderSrc); |
| 675 | this->appendDecls(fFSInputs, &fragShaderSrc); |
| 676 | // We shouldn't have declared outputs on 1.10 |
| 677 | SkASSERT(k110_GrGLSLGeneration != fGpu->glslGeneration() || fFSOutputs.empty()); |
| 678 | this->appendDecls(fFSOutputs, &fragShaderSrc); |
| 679 | fragShaderSrc.append(fFSFunctions); |
| 680 | fragShaderSrc.append("void main() {\n"); |
| 681 | fragShaderSrc.append(fFSCode); |
| 682 | fragShaderSrc.append("}\n"); |
| 683 | if (!attach_shader(fGpu->glInterface(), programId, GR_GL_FRAGMENT_SHADER, fragShaderSrc)) { |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | void GrGLShaderBuilder::bindProgramLocations(GrGLuint programId) const { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 691 | if (fHasCustomColorOutput) { |
| 692 | GL_CALL(BindFragDataLocation(programId, 0, declared_color_output_name())); |
| 693 | } |
| 694 | if (fHasSecondaryOutput) { |
| 695 | GL_CALL(BindFragDataLocationIndexed(programId, 0, 1, dual_source_output_name())); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | const GrGLContextInfo& GrGLShaderBuilder::ctxInfo() const { |
| 700 | return fGpu->ctxInfo(); |
| 701 | } |
| 702 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 703 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 704 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 705 | GrGLFullShaderBuilder::GrGLFullShaderBuilder(GrGpuGL* gpu, |
| 706 | GrGLUniformManager& uniformManager, |
| 707 | const GrGLProgramDesc& desc) |
| 708 | : INHERITED(gpu, uniformManager, desc) |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 709 | , fDesc(desc) |
| 710 | , fVSAttrs(kVarsPerBlock) |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 711 | , fVSOutputs(kVarsPerBlock) |
| 712 | , fGSInputs(kVarsPerBlock) |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 713 | , fGSOutputs(kVarsPerBlock) { |
| 714 | |
| 715 | const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 716 | |
| 717 | fPositionVar = &fVSAttrs.push_back(); |
| 718 | fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "aPosition"); |
| 719 | if (-1 != header.fLocalCoordAttributeIndex) { |
| 720 | fLocalCoordsVar = &fVSAttrs.push_back(); |
| 721 | fLocalCoordsVar->set(kVec2f_GrSLType, |
| 722 | GrGLShaderVar::kAttribute_TypeModifier, |
| 723 | "aLocalCoords"); |
| 724 | } else { |
| 725 | fLocalCoordsVar = fPositionVar; |
| 726 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 727 | |
| 728 | const char* viewMName; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 729 | fViewMatrixUniform = this->addUniform(GrGLShaderBuilder::kVertex_Visibility, |
| 730 | kMat33f_GrSLType, "ViewM", &viewMName); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 731 | |
| 732 | this->vsCodeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n" |
| 733 | "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n", |
| 734 | viewMName, fPositionVar->c_str()); |
| 735 | |
| 736 | // we output point size in the GS if present |
| 737 | if (header.fEmitsPointSize |
| 738 | #if GR_GL_EXPERIMENTAL_GS |
| 739 | && !header.fExperimentalGS |
| 740 | #endif |
| 741 | ) { |
| 742 | this->vsCodeAppend("\tgl_PointSize = 1.0;\n"); |
| 743 | } |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 744 | |
| 745 | if (GrGLProgramDesc::kAttribute_ColorInput == header.fColorInput) { |
| 746 | this->addAttribute(kVec4f_GrSLType, color_attribute_name()); |
| 747 | const char *vsName, *fsName; |
| 748 | this->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName); |
| 749 | this->vsCodeAppendf("\t%s = %s;\n", vsName, color_attribute_name()); |
| 750 | this->setInputColor(fsName); |
| 751 | } |
| 752 | |
| 753 | if (GrGLProgramDesc::kAttribute_ColorInput == header.fCoverageInput) { |
| 754 | this->addAttribute(kVec4f_GrSLType, coverage_attribute_name()); |
| 755 | const char *vsName, *fsName; |
| 756 | this->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName); |
| 757 | this->vsCodeAppendf("\t%s = %s;\n", vsName, coverage_attribute_name()); |
| 758 | this->setInputCoverage(fsName); |
| 759 | } |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 760 | } |
| 761 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 762 | bool GrGLFullShaderBuilder::addAttribute(GrSLType type, const char* name) { |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 763 | for (int i = 0; i < fVSAttrs.count(); ++i) { |
| 764 | const GrGLShaderVar& attr = fVSAttrs[i]; |
| 765 | // if attribute already added, don't add it again |
| 766 | if (attr.getName().equals(name)) { |
| 767 | SkASSERT(attr.getType() == type); |
| 768 | return false; |
| 769 | } |
| 770 | } |
| 771 | fVSAttrs.push_back().set(type, |
| 772 | GrGLShaderVar::kAttribute_TypeModifier, |
| 773 | name); |
| 774 | return true; |
| 775 | } |
| 776 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 777 | bool GrGLFullShaderBuilder::addEffectAttribute(int attributeIndex, |
| 778 | GrSLType type, |
| 779 | const SkString& name) { |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 780 | if (!this->addAttribute(type, name.c_str())) { |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | fEffectAttributes.push_back().set(attributeIndex, name); |
| 785 | return true; |
| 786 | } |
| 787 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 788 | void GrGLFullShaderBuilder::addVarying(GrSLType type, |
| 789 | const char* name, |
| 790 | const char** vsOutName, |
| 791 | const char** fsInName) { |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 792 | fVSOutputs.push_back(); |
| 793 | fVSOutputs.back().setType(type); |
| 794 | fVSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 795 | this->nameVariable(fVSOutputs.back().accessName(), 'v', name); |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 796 | |
| 797 | if (vsOutName) { |
| 798 | *vsOutName = fVSOutputs.back().getName().c_str(); |
| 799 | } |
| 800 | // input to FS comes either from VS or GS |
| 801 | const SkString* fsName; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 802 | #if GR_GL_EXPERIMENTAL_GS |
| 803 | if (fDesc.getHeader().fExperimentalGS) { |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 804 | // if we have a GS take each varying in as an array |
| 805 | // and output as non-array. |
| 806 | fGSInputs.push_back(); |
| 807 | fGSInputs.back().setType(type); |
| 808 | fGSInputs.back().setTypeModifier(GrGLShaderVar::kVaryingIn_TypeModifier); |
| 809 | fGSInputs.back().setUnsizedArray(); |
| 810 | *fGSInputs.back().accessName() = fVSOutputs.back().getName(); |
| 811 | fGSOutputs.push_back(); |
| 812 | fGSOutputs.back().setType(type); |
| 813 | fGSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 814 | this->nameVariable(fGSOutputs.back().accessName(), 'g', name); |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 815 | fsName = fGSOutputs.back().accessName(); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 816 | } else |
| 817 | #endif |
| 818 | { |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 819 | fsName = fVSOutputs.back().accessName(); |
| 820 | } |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 821 | this->fsInputAppend().set(type, GrGLShaderVar::kVaryingIn_TypeModifier, *fsName); |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 822 | if (fsInName) { |
| 823 | *fsInName = fsName->c_str(); |
| 824 | } |
| 825 | } |
| 826 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 827 | const SkString* GrGLFullShaderBuilder::getEffectAttributeName(int attributeIndex) const { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 828 | const AttributePair* attribEnd = fEffectAttributes.end(); |
| 829 | for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attribEnd; ++attrib) { |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 830 | if (attrib->fIndex == attributeIndex) { |
| 831 | return &attrib->fName; |
| 832 | } |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 833 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 834 | |
| 835 | return NULL; |
| 836 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 837 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 838 | GrGLProgramEffects* GrGLFullShaderBuilder::createAndEmitEffects( |
| 839 | const GrEffectStage* effectStages[], |
| 840 | const EffectKey effectKeys[], |
| 841 | int effectCnt, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 842 | GrGLSLExpr4* inOutFSColor) { |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 843 | |
| 844 | GrGLVertexProgramEffectsBuilder programEffectsBuilder(this, effectCnt); |
| 845 | this->INHERITED::createAndEmitEffects(&programEffectsBuilder, |
| 846 | effectStages, |
| 847 | effectKeys, |
| 848 | effectCnt, |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 849 | inOutFSColor); |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 850 | return programEffectsBuilder.finish(); |
| 851 | } |
| 852 | |
| 853 | bool GrGLFullShaderBuilder::compileAndAttachShaders(GrGLuint programId) const { |
| 854 | const GrGLInterface* glInterface = this->gpu()->glInterface(); |
| 855 | SkString vertShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); |
| 856 | this->appendUniformDecls(kVertex_Visibility, &vertShaderSrc); |
| 857 | this->appendDecls(fVSAttrs, &vertShaderSrc); |
| 858 | this->appendDecls(fVSOutputs, &vertShaderSrc); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 859 | vertShaderSrc.append("void main() {\n"); |
| 860 | vertShaderSrc.append(fVSCode); |
| 861 | vertShaderSrc.append("}\n"); |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 862 | if (!attach_shader(glInterface, programId, GR_GL_VERTEX_SHADER, vertShaderSrc)) { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 863 | return false; |
| 864 | } |
| 865 | |
| 866 | #if GR_GL_EXPERIMENTAL_GS |
| 867 | if (fDesc.getHeader().fExperimentalGS) { |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 868 | SkASSERT(this->ctxInfo().glslGeneration() >= k150_GrGLSLGeneration); |
| 869 | SkString geomShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 870 | geomShaderSrc.append("layout(triangles) in;\n" |
| 871 | "layout(triangle_strip, max_vertices = 6) out;\n"); |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 872 | this->appendDecls(fGSInputs, &geomShaderSrc); |
| 873 | this->appendDecls(fGSOutputs, &geomShaderSrc); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 874 | geomShaderSrc.append("void main() {\n"); |
| 875 | geomShaderSrc.append("\tfor (int i = 0; i < 3; ++i) {\n" |
| 876 | "\t\tgl_Position = gl_in[i].gl_Position;\n"); |
| 877 | if (fDesc.getHeader().fEmitsPointSize) { |
| 878 | geomShaderSrc.append("\t\tgl_PointSize = 1.0;\n"); |
| 879 | } |
| 880 | SkASSERT(fGSInputs.count() == fGSOutputs.count()); |
| 881 | for (int i = 0; i < fGSInputs.count(); ++i) { |
| 882 | geomShaderSrc.appendf("\t\t%s = %s[i];\n", |
| 883 | fGSOutputs[i].getName().c_str(), |
| 884 | fGSInputs[i].getName().c_str()); |
| 885 | } |
| 886 | geomShaderSrc.append("\t\tEmitVertex();\n" |
| 887 | "\t}\n" |
| 888 | "\tEndPrimitive();\n"); |
| 889 | geomShaderSrc.append("}\n"); |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 890 | if (!attach_shader(glInterface, programId, GR_GL_GEOMETRY_SHADER, geomShaderSrc)) { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 891 | return false; |
| 892 | } |
| 893 | } |
| 894 | #endif |
| 895 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 896 | return this->INHERITED::compileAndAttachShaders(programId); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 897 | } |
| 898 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 899 | void GrGLFullShaderBuilder::bindProgramLocations(GrGLuint programId) const { |
| 900 | this->INHERITED::bindProgramLocations(programId); |
| 901 | |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 902 | const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
| 903 | |
| 904 | // Bind the attrib locations to same values for all shaders |
| 905 | SkASSERT(-1 != header.fPositionAttributeIndex); |
| 906 | GL_CALL(BindAttribLocation(programId, |
| 907 | header.fPositionAttributeIndex, |
| 908 | fPositionVar->c_str())); |
| 909 | if (-1 != header.fLocalCoordAttributeIndex) { |
| 910 | GL_CALL(BindAttribLocation(programId, |
| 911 | header.fLocalCoordAttributeIndex, |
| 912 | fLocalCoordsVar->c_str())); |
| 913 | } |
| 914 | if (-1 != header.fColorAttributeIndex) { |
| 915 | GL_CALL(BindAttribLocation(programId, |
| 916 | header.fColorAttributeIndex, |
| 917 | color_attribute_name())); |
| 918 | } |
| 919 | if (-1 != header.fCoverageAttributeIndex) { |
| 920 | GL_CALL(BindAttribLocation(programId, |
| 921 | header.fCoverageAttributeIndex, |
| 922 | coverage_attribute_name())); |
| 923 | } |
| 924 | |
| 925 | const AttributePair* attribEnd = fEffectAttributes.end(); |
| 926 | for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attribEnd; ++attrib) { |
| 927 | GL_CALL(BindAttribLocation(programId, attrib->fIndex, attrib->fName.c_str())); |
| 928 | } |
| 929 | } |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 930 | |
| 931 | //////////////////////////////////////////////////////////////////////////////// |
| 932 | |
| 933 | GrGLFragmentOnlyShaderBuilder::GrGLFragmentOnlyShaderBuilder(GrGpuGL* gpu, |
| 934 | GrGLUniformManager& uniformManager, |
| 935 | const GrGLProgramDesc& desc) |
| 936 | : INHERITED(gpu, uniformManager, desc) |
| 937 | , fNumTexCoordSets(0) { |
| 938 | |
| 939 | SkASSERT(!desc.getHeader().fHasVertexCode); |
| 940 | SkASSERT(gpu->glCaps().fixedFunctionSupport()); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 941 | SkASSERT(gpu->glCaps().pathRenderingSupport()); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 942 | SkASSERT(GrGLProgramDesc::kAttribute_ColorInput != desc.getHeader().fColorInput); |
| 943 | SkASSERT(GrGLProgramDesc::kAttribute_ColorInput != desc.getHeader().fCoverageInput); |
| 944 | } |
| 945 | |
commit-bot@chromium.org | 8e919ad | 2013-10-21 14:48:23 +0000 | [diff] [blame] | 946 | int GrGLFragmentOnlyShaderBuilder::addTexCoordSets(int count) { |
| 947 | int firstFreeCoordSet = fNumTexCoordSets; |
| 948 | fNumTexCoordSets += count; |
| 949 | SkASSERT(gpu()->glCaps().maxFixedFunctionTextureCoords() >= fNumTexCoordSets); |
| 950 | return firstFreeCoordSet; |
| 951 | } |
| 952 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 953 | GrGLProgramEffects* GrGLFragmentOnlyShaderBuilder::createAndEmitEffects( |
| 954 | const GrEffectStage* effectStages[], |
| 955 | const EffectKey effectKeys[], |
| 956 | int effectCnt, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 957 | GrGLSLExpr4* inOutFSColor) { |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 958 | |
| 959 | GrGLTexGenProgramEffectsBuilder texGenEffectsBuilder(this, effectCnt); |
| 960 | this->INHERITED::createAndEmitEffects(&texGenEffectsBuilder, |
| 961 | effectStages, |
| 962 | effectKeys, |
| 963 | effectCnt, |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 964 | inOutFSColor); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 965 | return texGenEffectsBuilder.finish(); |
| 966 | } |