joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 8 | #include "GrGLProgramBuilder.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 9 | |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 10 | #include "GrAutoLocaleSetter.h" |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 11 | #include "GrCoordTransform.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 12 | #include "GrGLProgramBuilder.h" |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 13 | #include "GrSwizzle.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 14 | #include "GrTexture.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 15 | #include "SkRTConf.h" |
| 16 | #include "SkTraceEvent.h" |
joshualitt | e7afc2d | 2015-09-11 10:44:13 -0700 | [diff] [blame] | 17 | #include "gl/GrGLGpu.h" |
joshualitt | e7afc2d | 2015-09-11 10:44:13 -0700 | [diff] [blame] | 18 | #include "gl/GrGLProgram.h" |
| 19 | #include "gl/GrGLSLPrettyPrint.h" |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 20 | #include "gl/builders/GrGLShaderStringBuilder.h" |
joshualitt | e7afc2d | 2015-09-11 10:44:13 -0700 | [diff] [blame] | 21 | #include "glsl/GrGLSLCaps.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 22 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 23 | #include "glsl/GrGLSLGeometryProcessor.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 24 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 25 | #include "glsl/GrGLSLTextureSampler.h" |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 26 | #include "glsl/GrGLSLXferProcessor.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 27 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 28 | #define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X) |
| 29 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X) |
| 30 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 31 | GrGLProgram* GrGLProgramBuilder::CreateProgram(const DrawArgs& args, GrGLGpu* gpu) { |
bsalomon | 3318ee7 | 2015-03-16 11:56:29 -0700 | [diff] [blame] | 32 | GrAutoLocaleSetter als("C"); |
| 33 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 34 | // create a builder. This will be handed off to effects so they can use it to add |
| 35 | // uniforms, varyings, textures, etc |
egdaniel | c1e7101 | 2016-01-20 07:53:51 -0800 | [diff] [blame] | 36 | GrGLProgramBuilder builder(gpu, args); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 37 | |
egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 38 | // TODO: Once all stages can handle taking a float or vec4 and correctly handling them we can |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 39 | // seed correctly here |
| 40 | GrGLSLExpr4 inputColor; |
| 41 | GrGLSLExpr4 inputCoverage; |
egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 42 | |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame^] | 43 | if (!builder.emitAndInstallProcs(&inputColor, &inputCoverage)) { |
egdaniel | c1e7101 | 2016-01-20 07:53:51 -0800 | [diff] [blame] | 44 | builder.cleanupFragmentProcessors(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 45 | return nullptr; |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 46 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 47 | |
egdaniel | c1e7101 | 2016-01-20 07:53:51 -0800 | [diff] [blame] | 48 | return builder.finalize(); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 49 | } |
| 50 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 51 | ///////////////////////////////////////////////////////////////////////////// |
| 52 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 53 | GrGLProgramBuilder::GrGLProgramBuilder(GrGLGpu* gpu, const DrawArgs& args) |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 54 | : INHERITED(args) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 55 | , fGpu(gpu) |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 56 | , fVaryingHandler(this) |
| 57 | , fUniformHandler(this) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 58 | } |
| 59 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 60 | const GrCaps* GrGLProgramBuilder::caps() const { |
| 61 | return fGpu->caps(); |
| 62 | } |
| 63 | |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 64 | const GrGLSLCaps* GrGLProgramBuilder::glslCaps() const { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 65 | return fGpu->ctxInfo().caps()->glslCaps(); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 66 | } |
| 67 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 68 | bool GrGLProgramBuilder::compileAndAttachShaders(GrGLSLShaderBuilder& shader, |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 69 | GrGLuint programId, |
| 70 | GrGLenum type, |
| 71 | SkTDArray<GrGLuint>* shaderIds) { |
| 72 | GrGLGpu* gpu = this->gpu(); |
| 73 | GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(), |
| 74 | programId, |
| 75 | type, |
| 76 | shader.fCompilerStrings.begin(), |
| 77 | shader.fCompilerStringLengths.begin(), |
| 78 | shader.fCompilerStrings.count(), |
| 79 | gpu->stats()); |
| 80 | |
| 81 | if (!shaderId) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | *shaderIds->append() = shaderId; |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 90 | GrGLProgram* GrGLProgramBuilder::finalize() { |
| 91 | // verify we can get a program id |
| 92 | GrGLuint programID; |
| 93 | GL_CALL_RET(programID, CreateProgram()); |
| 94 | if (0 == programID) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 95 | this->cleanupFragmentProcessors(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 96 | return nullptr; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 97 | } |
| 98 | |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 99 | this->finalizeShaders(); |
| 100 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 101 | // compile shaders and bind attributes / uniforms |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 102 | SkTDArray<GrGLuint> shadersToDelete; |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 103 | if (!this->compileAndAttachShaders(fVS, programID, GR_GL_VERTEX_SHADER, &shadersToDelete)) { |
jvanverth | 5053063 | 2015-04-27 10:36:27 -0700 | [diff] [blame] | 104 | this->cleanupProgram(programID, shadersToDelete); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 105 | return nullptr; |
jvanverth | 5053063 | 2015-04-27 10:36:27 -0700 | [diff] [blame] | 106 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 107 | |
jvanverth | 5053063 | 2015-04-27 10:36:27 -0700 | [diff] [blame] | 108 | // NVPR actually requires a vertex shader to compile |
| 109 | bool useNvpr = primitiveProcessor().isPathRendering(); |
| 110 | if (!useNvpr) { |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 111 | const GrPrimitiveProcessor& primProc = this->primitiveProcessor(); |
| 112 | |
| 113 | int vaCount = primProc.numAttribs(); |
| 114 | for (int i = 0; i < vaCount; i++) { |
| 115 | GL_CALL(BindAttribLocation(programID, i, primProc.getAttrib(i).fName)); |
| 116 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 117 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 118 | |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 119 | if (!this->compileAndAttachShaders(fFS, programID, GR_GL_FRAGMENT_SHADER, &shadersToDelete)) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 120 | this->cleanupProgram(programID, shadersToDelete); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 121 | return nullptr; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 122 | } |
| 123 | |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 124 | this->bindProgramResourceLocations(programID); |
| 125 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 126 | GL_CALL(LinkProgram(programID)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 127 | |
| 128 | // Calling GetProgramiv is expensive in Chromium. Assume success in release builds. |
cdalton | 1acea86 | 2015-06-02 13:05:52 -0700 | [diff] [blame] | 129 | bool checkLinked = kChromium_GrGLDriver != fGpu->ctxInfo().driver(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 130 | #ifdef SK_DEBUG |
| 131 | checkLinked = true; |
| 132 | #endif |
| 133 | if (checkLinked) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 134 | checkLinkStatus(programID); |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 135 | } |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 136 | this->resolveProgramResourceLocations(programID); |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 137 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 138 | this->cleanupShaders(shadersToDelete); |
| 139 | |
| 140 | return this->createProgram(programID); |
| 141 | } |
| 142 | |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 143 | void GrGLProgramBuilder::bindProgramResourceLocations(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 144 | fUniformHandler.bindUniformLocations(programID, fGpu->glCaps()); |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 145 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 146 | const GrGLCaps& caps = this->gpu()->glCaps(); |
| 147 | if (fFS.hasCustomColorOutput() && caps.bindFragDataLocationSupport()) { |
| 148 | GL_CALL(BindFragDataLocation(programID, 0, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 149 | GrGLSLFragmentShaderBuilder::DeclaredColorOutputName())); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 150 | } |
| 151 | if (fFS.hasSecondaryOutput() && caps.glslCaps()->mustDeclareFragmentShaderOutput()) { |
| 152 | GL_CALL(BindFragDataLocationIndexed(programID, 0, 1, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 153 | GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName())); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 154 | } |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 155 | |
| 156 | // handle NVPR separable varyings |
| 157 | if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() || |
| 158 | !fGpu->glPathRendering()->shouldBindFragmentInputs()) { |
| 159 | return; |
| 160 | } |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 161 | int count = fVaryingHandler.fPathProcVaryingInfos.count(); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 162 | for (int i = 0; i < count; ++i) { |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 163 | GL_CALL(BindFragmentInputLocation(programID, i, |
| 164 | fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str())); |
| 165 | fVaryingHandler.fPathProcVaryingInfos[i].fLocation = i; |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 166 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | bool GrGLProgramBuilder::checkLinkStatus(GrGLuint programID) { |
| 170 | GrGLint linked = GR_GL_INIT_ZERO; |
| 171 | GL_CALL(GetProgramiv(programID, GR_GL_LINK_STATUS, &linked)); |
| 172 | if (!linked) { |
| 173 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 174 | GL_CALL(GetProgramiv(programID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 175 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 176 | if (infoLen > 0) { |
| 177 | // retrieve length even though we don't need it to workaround |
| 178 | // bug in chrome cmd buffer param validation. |
| 179 | GrGLsizei length = GR_GL_INIT_ZERO; |
| 180 | GL_CALL(GetProgramInfoLog(programID, |
| 181 | infoLen+1, |
| 182 | &length, |
| 183 | (char*)log.get())); |
kkinnunen | 297aaf9 | 2015-02-19 06:32:12 -0800 | [diff] [blame] | 184 | SkDebugf("%s", (char*)log.get()); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 185 | } |
| 186 | SkDEBUGFAIL("Error linking program"); |
| 187 | GL_CALL(DeleteProgram(programID)); |
| 188 | programID = 0; |
| 189 | } |
| 190 | return SkToBool(linked); |
| 191 | } |
| 192 | |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 193 | void GrGLProgramBuilder::resolveProgramResourceLocations(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 194 | fUniformHandler.getUniformLocations(programID, fGpu->glCaps()); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 195 | |
| 196 | // handle NVPR separable varyings |
| 197 | if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() || |
kkinnunen | 7bdb05d | 2016-01-25 00:47:23 -0800 | [diff] [blame] | 198 | fGpu->glPathRendering()->shouldBindFragmentInputs()) { |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 199 | return; |
| 200 | } |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 201 | int count = fVaryingHandler.fPathProcVaryingInfos.count(); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 202 | for (int i = 0; i < count; ++i) { |
| 203 | GrGLint location; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 204 | GL_CALL_RET(location, GetProgramResourceLocation( |
| 205 | programID, |
| 206 | GR_GL_FRAGMENT_INPUT, |
| 207 | fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str())); |
| 208 | fVaryingHandler.fPathProcVaryingInfos[i].fLocation = location; |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 209 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 210 | } |
| 211 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 212 | void GrGLProgramBuilder::cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs) { |
| 213 | GL_CALL(DeleteProgram(programID)); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 214 | this->cleanupShaders(shaderIDs); |
| 215 | this->cleanupFragmentProcessors(); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 216 | } |
| 217 | void GrGLProgramBuilder::cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs) { |
| 218 | for (int i = 0; i < shaderIDs.count(); ++i) { |
| 219 | GL_CALL(DeleteShader(shaderIDs[i])); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | GrGLProgram* GrGLProgramBuilder::createProgram(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 224 | return new GrGLProgram(fGpu, |
| 225 | this->desc(), |
| 226 | fUniformHandles, |
| 227 | programID, |
| 228 | fUniformHandler.fUniforms, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 229 | fVaryingHandler.fPathProcVaryingInfos, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 230 | fGeometryProcessor, |
| 231 | fXferProcessor, |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 232 | fFragmentProcessors, |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 233 | &fSamplerUniforms); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 234 | } |
| 235 | |