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" |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 11 | #include "GrContext.h" |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 13 | #include "GrCoordTransform.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 14 | #include "GrGLProgramBuilder.h" |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 15 | #include "GrProgramDesc.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 16 | #include "GrShaderCaps.h" |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 17 | #include "GrSwizzle.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 18 | #include "SkAutoMalloc.h" |
Derek Sollenberger | 488f0d6 | 2017-03-03 15:48:33 -0500 | [diff] [blame] | 19 | #include "SkATrace.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 20 | #include "SkTraceEvent.h" |
joshualitt | e7afc2d | 2015-09-11 10:44:13 -0700 | [diff] [blame] | 21 | #include "gl/GrGLGpu.h" |
joshualitt | e7afc2d | 2015-09-11 10:44:13 -0700 | [diff] [blame] | 22 | #include "gl/GrGLProgram.h" |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 23 | #include "gl/builders/GrGLShaderStringBuilder.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 24 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 25 | #include "glsl/GrGLSLGeometryProcessor.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 26 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 27 | #include "glsl/GrGLSLXferProcessor.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 28 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 29 | #define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X) |
| 30 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X) |
| 31 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 32 | GrGLProgram* GrGLProgramBuilder::CreateProgram(const GrPipeline& pipeline, |
| 33 | const GrPrimitiveProcessor& primProc, |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 34 | GrProgramDesc* desc, |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 35 | GrGLGpu* gpu) { |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 36 | #ifdef SK_DEBUG |
| 37 | GrResourceProvider* resourceProvider = gpu->getContext()->contextPriv().resourceProvider(); |
| 38 | |
| 39 | SkASSERT(!pipeline.isBad() && primProc.instantiate(resourceProvider)); |
| 40 | #endif |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 41 | |
Derek Sollenberger | 488f0d6 | 2017-03-03 15:48:33 -0500 | [diff] [blame] | 42 | ATRACE_ANDROID_FRAMEWORK("Shader Compile"); |
bsalomon | 3318ee7 | 2015-03-16 11:56:29 -0700 | [diff] [blame] | 43 | GrAutoLocaleSetter als("C"); |
| 44 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 45 | // create a builder. This will be handed off to effects so they can use it to add |
| 46 | // uniforms, varyings, textures, etc |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 47 | GrGLProgramBuilder builder(gpu, pipeline, primProc, desc); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 48 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 49 | auto persistentCache = gpu->getContext()->contextPriv().getPersistentCache(); |
| 50 | if (persistentCache && gpu->glCaps().programBinarySupport()) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 51 | sk_sp<SkData> key = SkData::MakeWithoutCopy(desc->asKey(), desc->keyLength()); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 52 | builder.fCached = persistentCache->load(*key); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 53 | // the eventual end goal is to completely skip emitAndInstallProcs on a cache hit, but it's |
| 54 | // doing necessary setup in addition to generating the SkSL code. Currently we are only able |
| 55 | // to skip the SkSL->GLSL step on a cache hit. |
| 56 | } |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 57 | if (!builder.emitAndInstallProcs()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 58 | return nullptr; |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 59 | } |
egdaniel | c1e7101 | 2016-01-20 07:53:51 -0800 | [diff] [blame] | 60 | return builder.finalize(); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 61 | } |
| 62 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 63 | ///////////////////////////////////////////////////////////////////////////// |
| 64 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 65 | GrGLProgramBuilder::GrGLProgramBuilder(GrGLGpu* gpu, |
| 66 | const GrPipeline& pipeline, |
| 67 | const GrPrimitiveProcessor& primProc, |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 68 | GrProgramDesc* desc) |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 69 | : INHERITED(pipeline, primProc, desc) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 70 | , fGpu(gpu) |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 71 | , fVaryingHandler(this) |
| 72 | , fUniformHandler(this) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 73 | } |
| 74 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 75 | const GrCaps* GrGLProgramBuilder::caps() const { |
| 76 | return fGpu->caps(); |
| 77 | } |
| 78 | |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 79 | bool GrGLProgramBuilder::compileAndAttachShaders(const char* glsl, |
| 80 | int length, |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 81 | GrGLuint programId, |
| 82 | GrGLenum type, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 83 | SkTDArray<GrGLuint>* shaderIds, |
| 84 | const SkSL::Program::Settings& settings, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 85 | const SkSL::Program::Inputs& inputs) { |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 86 | GrGLGpu* gpu = this->gpu(); |
| 87 | GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(), |
| 88 | programId, |
| 89 | type, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 90 | glsl, |
| 91 | length, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 92 | gpu->stats(), |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 93 | settings); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 94 | if (!shaderId) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | *shaderIds->append() = shaderId; |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 99 | if (inputs.fFlipY) { |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 100 | GrProgramDesc* d = this->desc(); |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 101 | d->setSurfaceOriginKey(GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin( |
| 102 | this->pipeline().proxy()->origin())); |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 103 | d->finalize(); |
| 104 | } |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 109 | bool GrGLProgramBuilder::compileAndAttachShaders(GrGLSLShaderBuilder& shader, |
| 110 | GrGLuint programId, |
| 111 | GrGLenum type, |
| 112 | SkTDArray<GrGLuint>* shaderIds, |
| 113 | const SkSL::Program::Settings& settings, |
| 114 | SkSL::Program::Inputs* outInputs) { |
| 115 | SkSL::String glsl; |
| 116 | std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(gpu()->glContext(), type, |
| 117 | shader.fCompilerStrings.begin(), |
| 118 | shader.fCompilerStringLengths.begin(), |
| 119 | shader.fCompilerStrings.count(), |
| 120 | settings, |
| 121 | &glsl); |
| 122 | *outInputs = program->fInputs; |
| 123 | return this->compileAndAttachShaders(glsl.c_str(), |
| 124 | glsl.size(), |
| 125 | programId, |
| 126 | type, |
| 127 | shaderIds, |
| 128 | settings, |
| 129 | *outInputs); |
| 130 | } |
| 131 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 132 | GrGLProgram* GrGLProgramBuilder::finalize() { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 133 | TRACE_EVENT0("skia", TRACE_FUNC); |
Ryan Macnak | 38a10ad | 2017-07-10 10:36:34 -0700 | [diff] [blame] | 134 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 135 | // verify we can get a program id |
| 136 | GrGLuint programID; |
| 137 | GL_CALL_RET(programID, CreateProgram()); |
| 138 | if (0 == programID) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 139 | return nullptr; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Ethan Nicholas | 06d55fb | 2017-11-08 09:48:50 -0500 | [diff] [blame] | 142 | if (this->gpu()->glCaps().programBinarySupport() && |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 143 | this->gpu()->getContext()->contextPriv().getPersistentCache()) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 144 | GL_CALL(ProgramParameteri(programID, GR_GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GR_GL_TRUE)); |
| 145 | } |
| 146 | |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 147 | this->finalizeShaders(); |
| 148 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 149 | // compile shaders and bind attributes / uniforms |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 150 | const GrPrimitiveProcessor& primProc = this->primitiveProcessor(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 151 | SkSL::Program::Settings settings; |
| 152 | settings.fCaps = this->gpu()->glCaps().shaderCaps(); |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 153 | settings.fFlipY = this->pipeline().proxy()->origin() != kTopLeft_GrSurfaceOrigin; |
Brian Osman | 8a83ca4 | 2018-02-12 14:32:17 -0500 | [diff] [blame] | 154 | settings.fSharpenTextures = this->gpu()->getContext()->contextPriv().sharpenMipmappedTextures(); |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 155 | settings.fFragColorIsInOut = this->fragColorIsInOut(); |
| 156 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 157 | SkSL::Program::Inputs inputs; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 158 | SkTDArray<GrGLuint> shadersToDelete; |
Ethan Nicholas | 98ad5b7 | 2018-03-13 09:53:02 -0400 | [diff] [blame] | 159 | bool cached = fGpu->glCaps().programBinarySupport() && nullptr != fCached.get(); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 160 | if (cached) { |
Ethan Nicholas | 907204f | 2017-11-14 14:35:57 -0500 | [diff] [blame] | 161 | this->bindProgramResourceLocations(programID); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 162 | // cache hit, just hand the binary to GL |
| 163 | const uint8_t* bytes = fCached->bytes(); |
| 164 | size_t offset = 0; |
| 165 | memcpy(&inputs, bytes + offset, sizeof(inputs)); |
| 166 | offset += sizeof(inputs); |
| 167 | int binaryFormat; |
| 168 | memcpy(&binaryFormat, bytes + offset, sizeof(binaryFormat)); |
| 169 | offset += sizeof(binaryFormat); |
Ethan Nicholas | 98ad5b7 | 2018-03-13 09:53:02 -0400 | [diff] [blame] | 170 | GrGLClearErr(this->gpu()->glInterface()); |
| 171 | GR_GL_CALL_NOERRCHECK(this->gpu()->glInterface(), |
| 172 | ProgramBinary(programID, binaryFormat, (void*) (bytes + offset), |
| 173 | fCached->size() - offset)); |
| 174 | if (GR_GL_GET_ERROR(this->gpu()->glInterface()) == GR_GL_NO_ERROR) { |
| 175 | if (inputs.fRTHeight) { |
| 176 | this->addRTHeightUniform(SKSL_RTHEIGHT_NAME); |
| 177 | } |
| 178 | cached = this->checkLinkStatus(programID); |
| 179 | } else { |
| 180 | cached = false; |
| 181 | } |
| 182 | } |
| 183 | if (!cached) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 184 | // cache miss, compile shaders |
| 185 | if (fFS.fForceHighPrecision) { |
| 186 | settings.fForceHighPrecision = true; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 187 | } |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 188 | SkSL::String glsl; |
| 189 | std::unique_ptr<SkSL::Program> fs = GrSkSLtoGLSL(gpu()->glContext(), |
| 190 | GR_GL_FRAGMENT_SHADER, |
| 191 | fFS.fCompilerStrings.begin(), |
| 192 | fFS.fCompilerStringLengths.begin(), |
| 193 | fFS.fCompilerStrings.count(), |
| 194 | settings, |
| 195 | &glsl); |
Brian Salomon | 7c93472 | 2018-06-01 14:42:53 -0400 | [diff] [blame] | 196 | if (!fs) { |
| 197 | this->cleanupProgram(programID, shadersToDelete); |
| 198 | return nullptr; |
| 199 | } |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 200 | inputs = fs->fInputs; |
| 201 | if (inputs.fRTHeight) { |
| 202 | this->addRTHeightUniform(SKSL_RTHEIGHT_NAME); |
| 203 | } |
| 204 | if (!this->compileAndAttachShaders(glsl.c_str(), glsl.size(), programID, |
| 205 | GR_GL_FRAGMENT_SHADER, &shadersToDelete, settings, |
| 206 | inputs)) { |
| 207 | this->cleanupProgram(programID, shadersToDelete); |
| 208 | return nullptr; |
| 209 | } |
| 210 | |
| 211 | std::unique_ptr<SkSL::Program> vs = GrSkSLtoGLSL(gpu()->glContext(), |
| 212 | GR_GL_VERTEX_SHADER, |
| 213 | fVS.fCompilerStrings.begin(), |
| 214 | fVS.fCompilerStringLengths.begin(), |
| 215 | fVS.fCompilerStrings.count(), |
| 216 | settings, |
| 217 | &glsl); |
Brian Salomon | 7c93472 | 2018-06-01 14:42:53 -0400 | [diff] [blame] | 218 | if (!vs || !this->compileAndAttachShaders(glsl.c_str(), glsl.size(), programID, |
| 219 | GR_GL_VERTEX_SHADER, &shadersToDelete, settings, |
| 220 | inputs)) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 221 | this->cleanupProgram(programID, shadersToDelete); |
| 222 | return nullptr; |
| 223 | } |
| 224 | |
| 225 | // NVPR actually requires a vertex shader to compile |
| 226 | bool useNvpr = primProc.isPathRendering(); |
| 227 | if (!useNvpr) { |
| 228 | int vaCount = primProc.numAttribs(); |
| 229 | for (int i = 0; i < vaCount; i++) { |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 230 | GL_CALL(BindAttribLocation(programID, i, primProc.getAttrib(i).name())); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | if (primProc.willUseGeoShader()) { |
| 235 | std::unique_ptr<SkSL::Program> gs; |
| 236 | gs = GrSkSLtoGLSL(gpu()->glContext(), |
| 237 | GR_GL_GEOMETRY_SHADER, |
| 238 | fGS.fCompilerStrings.begin(), |
| 239 | fGS.fCompilerStringLengths.begin(), |
| 240 | fGS.fCompilerStrings.count(), |
| 241 | settings, |
| 242 | &glsl); |
Brian Salomon | 7c93472 | 2018-06-01 14:42:53 -0400 | [diff] [blame] | 243 | if (!gs || !this->compileAndAttachShaders(glsl.c_str(), glsl.size(), programID, |
| 244 | GR_GL_GEOMETRY_SHADER, &shadersToDelete, |
| 245 | settings, inputs)) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 246 | this->cleanupProgram(programID, shadersToDelete); |
| 247 | return nullptr; |
| 248 | } |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 249 | } |
| 250 | this->bindProgramResourceLocations(programID); |
| 251 | |
| 252 | GL_CALL(LinkProgram(programID)); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 253 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 254 | // Calling GetProgramiv is expensive in Chromium. Assume success in release builds. |
cdalton | 1acea86 | 2015-06-02 13:05:52 -0700 | [diff] [blame] | 255 | bool checkLinked = kChromium_GrGLDriver != fGpu->ctxInfo().driver(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 256 | #ifdef SK_DEBUG |
| 257 | checkLinked = true; |
| 258 | #endif |
| 259 | if (checkLinked) { |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 260 | if (!this->checkLinkStatus(programID)) { |
| 261 | SkDebugf("VS:\n"); |
| 262 | GrGLPrintShader(fGpu->glContext(), GR_GL_VERTEX_SHADER, fVS.fCompilerStrings.begin(), |
| 263 | fVS.fCompilerStringLengths.begin(), fVS.fCompilerStrings.count(), |
| 264 | settings); |
| 265 | if (primProc.willUseGeoShader()) { |
| 266 | SkDebugf("\nGS:\n"); |
| 267 | GrGLPrintShader(fGpu->glContext(), GR_GL_GEOMETRY_SHADER, |
| 268 | fGS.fCompilerStrings.begin(), fGS.fCompilerStringLengths.begin(), |
| 269 | fGS.fCompilerStrings.count(), settings); |
| 270 | } |
| 271 | SkDebugf("\nFS:\n"); |
| 272 | GrGLPrintShader(fGpu->glContext(), GR_GL_FRAGMENT_SHADER, fFS.fCompilerStrings.begin(), |
| 273 | fFS.fCompilerStringLengths.begin(), fFS.fCompilerStrings.count(), |
| 274 | settings); |
| 275 | SkDEBUGFAIL(""); |
| 276 | return nullptr; |
| 277 | } |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 278 | } |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 279 | this->resolveProgramResourceLocations(programID); |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 280 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 281 | this->cleanupShaders(shadersToDelete); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 282 | if (!cached && this->gpu()->getContext()->contextPriv().getPersistentCache() && |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 283 | fGpu->glCaps().programBinarySupport()) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 284 | GrGLsizei length = 0; |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 285 | GL_CALL(GetProgramiv(programID, GL_PROGRAM_BINARY_LENGTH, &length)); |
Ethan Nicholas | 12b69ee | 2017-11-20 12:12:44 -0500 | [diff] [blame] | 286 | if (length > 0) { |
| 287 | // store shader in cache |
| 288 | sk_sp<SkData> key = SkData::MakeWithoutCopy(desc()->asKey(), desc()->keyLength()); |
| 289 | GrGLenum binaryFormat; |
Mike Klein | 1d74620 | 2018-01-25 17:32:51 -0500 | [diff] [blame] | 290 | std::unique_ptr<char[]> binary(new char[length]); |
Ethan Nicholas | 12b69ee | 2017-11-20 12:12:44 -0500 | [diff] [blame] | 291 | GL_CALL(GetProgramBinary(programID, length, &length, &binaryFormat, binary.get())); |
| 292 | size_t dataLength = sizeof(inputs) + sizeof(binaryFormat) + length; |
Mike Klein | 1d74620 | 2018-01-25 17:32:51 -0500 | [diff] [blame] | 293 | std::unique_ptr<uint8_t[]> data(new uint8_t[dataLength]); |
Ethan Nicholas | 12b69ee | 2017-11-20 12:12:44 -0500 | [diff] [blame] | 294 | size_t offset = 0; |
| 295 | memcpy(data.get() + offset, &inputs, sizeof(inputs)); |
| 296 | offset += sizeof(inputs); |
| 297 | memcpy(data.get() + offset, &binaryFormat, sizeof(binaryFormat)); |
| 298 | offset += sizeof(binaryFormat); |
| 299 | memcpy(data.get() + offset, binary.get(), length); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 300 | this->gpu()->getContext()->contextPriv().getPersistentCache()->store( |
| 301 | *key, *SkData::MakeWithoutCopy(data.get(), dataLength)); |
Ethan Nicholas | 12b69ee | 2017-11-20 12:12:44 -0500 | [diff] [blame] | 302 | } |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 303 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 304 | return this->createProgram(programID); |
| 305 | } |
| 306 | |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 307 | void GrGLProgramBuilder::bindProgramResourceLocations(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 308 | fUniformHandler.bindUniformLocations(programID, fGpu->glCaps()); |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 309 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 310 | const GrGLCaps& caps = this->gpu()->glCaps(); |
| 311 | if (fFS.hasCustomColorOutput() && caps.bindFragDataLocationSupport()) { |
| 312 | GL_CALL(BindFragDataLocation(programID, 0, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 313 | GrGLSLFragmentShaderBuilder::DeclaredColorOutputName())); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 314 | } |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 315 | if (fFS.hasSecondaryOutput() && caps.shaderCaps()->mustDeclareFragmentShaderOutput()) { |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 316 | GL_CALL(BindFragDataLocationIndexed(programID, 0, 1, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 317 | GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName())); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 318 | } |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 319 | |
| 320 | // handle NVPR separable varyings |
| 321 | if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() || |
| 322 | !fGpu->glPathRendering()->shouldBindFragmentInputs()) { |
| 323 | return; |
| 324 | } |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 325 | int count = fVaryingHandler.fPathProcVaryingInfos.count(); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 326 | for (int i = 0; i < count; ++i) { |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 327 | GL_CALL(BindFragmentInputLocation(programID, i, |
| 328 | fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str())); |
| 329 | fVaryingHandler.fPathProcVaryingInfos[i].fLocation = i; |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 330 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | bool GrGLProgramBuilder::checkLinkStatus(GrGLuint programID) { |
| 334 | GrGLint linked = GR_GL_INIT_ZERO; |
| 335 | GL_CALL(GetProgramiv(programID, GR_GL_LINK_STATUS, &linked)); |
| 336 | if (!linked) { |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 337 | SkDebugf("Program linking failed.\n"); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 338 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 339 | GL_CALL(GetProgramiv(programID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 340 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 341 | if (infoLen > 0) { |
| 342 | // retrieve length even though we don't need it to workaround |
| 343 | // bug in chrome cmd buffer param validation. |
| 344 | GrGLsizei length = GR_GL_INIT_ZERO; |
| 345 | GL_CALL(GetProgramInfoLog(programID, |
| 346 | infoLen+1, |
| 347 | &length, |
| 348 | (char*)log.get())); |
kkinnunen | 297aaf9 | 2015-02-19 06:32:12 -0800 | [diff] [blame] | 349 | SkDebugf("%s", (char*)log.get()); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 350 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 351 | GL_CALL(DeleteProgram(programID)); |
| 352 | programID = 0; |
| 353 | } |
| 354 | return SkToBool(linked); |
| 355 | } |
| 356 | |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 357 | void GrGLProgramBuilder::resolveProgramResourceLocations(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 358 | fUniformHandler.getUniformLocations(programID, fGpu->glCaps()); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 359 | |
| 360 | // handle NVPR separable varyings |
| 361 | if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() || |
kkinnunen | 7bdb05d | 2016-01-25 00:47:23 -0800 | [diff] [blame] | 362 | fGpu->glPathRendering()->shouldBindFragmentInputs()) { |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 363 | return; |
| 364 | } |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 365 | int count = fVaryingHandler.fPathProcVaryingInfos.count(); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 366 | for (int i = 0; i < count; ++i) { |
| 367 | GrGLint location; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 368 | GL_CALL_RET(location, GetProgramResourceLocation( |
| 369 | programID, |
| 370 | GR_GL_FRAGMENT_INPUT, |
| 371 | fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str())); |
| 372 | fVaryingHandler.fPathProcVaryingInfos[i].fLocation = location; |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 373 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 374 | } |
| 375 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 376 | void GrGLProgramBuilder::cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs) { |
| 377 | GL_CALL(DeleteProgram(programID)); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 378 | this->cleanupShaders(shaderIDs); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 379 | } |
| 380 | void GrGLProgramBuilder::cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs) { |
| 381 | for (int i = 0; i < shaderIDs.count(); ++i) { |
| 382 | GL_CALL(DeleteShader(shaderIDs[i])); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | GrGLProgram* GrGLProgramBuilder::createProgram(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 387 | return new GrGLProgram(fGpu, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 388 | fUniformHandles, |
| 389 | programID, |
| 390 | fUniformHandler.fUniforms, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 391 | fUniformHandler.fSamplers, |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 392 | fUniformHandler.fTexelBuffers, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 393 | fVaryingHandler.fPathProcVaryingInfos, |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 394 | std::move(fGeometryProcessor), |
| 395 | std::move(fXferProcessor), |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 396 | std::move(fFragmentProcessors), |
| 397 | fFragmentProcessorCnt); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 398 | } |