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