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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/gl/builders/GrGLProgramBuilder.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrContext.h" |
| 11 | #include "src/core/SkATrace.h" |
| 12 | #include "src/core/SkAutoMalloc.h" |
| 13 | #include "src/core/SkReader32.h" |
| 14 | #include "src/core/SkTraceEvent.h" |
| 15 | #include "src/core/SkWriter32.h" |
| 16 | #include "src/gpu/GrAutoLocaleSetter.h" |
| 17 | #include "src/gpu/GrContextPriv.h" |
| 18 | #include "src/gpu/GrCoordTransform.h" |
| 19 | #include "src/gpu/GrPersistentCacheUtils.h" |
| 20 | #include "src/gpu/GrProgramDesc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrShaderCaps.h" |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame^] | 22 | #include "src/gpu/GrShaderUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/gpu/GrSwizzle.h" |
| 24 | #include "src/gpu/gl/GrGLGpu.h" |
| 25 | #include "src/gpu/gl/GrGLProgram.h" |
| 26 | #include "src/gpu/gl/builders/GrGLProgramBuilder.h" |
| 27 | #include "src/gpu/gl/builders/GrGLShaderStringBuilder.h" |
| 28 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 29 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 30 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 31 | #include "src/gpu/glsl/GrGLSLXferProcessor.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 32 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 33 | #define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X) |
| 34 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X) |
| 35 | |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 36 | GrGLProgram* GrGLProgramBuilder::CreateProgram(GrRenderTarget* renderTarget, GrSurfaceOrigin origin, |
| 37 | const GrPrimitiveProcessor& primProc, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 38 | const GrTextureProxy* const primProcProxies[], |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 39 | const GrPipeline& pipeline, |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 40 | GrProgramDesc* desc, |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 41 | GrGLGpu* gpu) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 42 | SkASSERT(!pipeline.isBad()); |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 43 | |
Derek Sollenberger | 488f0d6 | 2017-03-03 15:48:33 -0500 | [diff] [blame] | 44 | ATRACE_ANDROID_FRAMEWORK("Shader Compile"); |
bsalomon | 3318ee7 | 2015-03-16 11:56:29 -0700 | [diff] [blame] | 45 | GrAutoLocaleSetter als("C"); |
| 46 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 47 | // create a builder. This will be handed off to effects so they can use it to add |
| 48 | // uniforms, varyings, textures, etc |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 49 | GrGLProgramBuilder builder(gpu, renderTarget, origin, |
| 50 | pipeline, primProc, primProcProxies, desc); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 51 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 52 | auto persistentCache = gpu->getContext()->priv().getPersistentCache(); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 53 | if (persistentCache) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 54 | sk_sp<SkData> key = SkData::MakeWithoutCopy(desc->asKey(), desc->keyLength()); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 55 | builder.fCached = persistentCache->load(*key); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 56 | // the eventual end goal is to completely skip emitAndInstallProcs on a cache hit, but it's |
| 57 | // doing necessary setup in addition to generating the SkSL code. Currently we are only able |
| 58 | // to skip the SkSL->GLSL step on a cache hit. |
| 59 | } |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 60 | if (!builder.emitAndInstallProcs()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 61 | return nullptr; |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 62 | } |
egdaniel | c1e7101 | 2016-01-20 07:53:51 -0800 | [diff] [blame] | 63 | return builder.finalize(); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 64 | } |
| 65 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 66 | ///////////////////////////////////////////////////////////////////////////// |
| 67 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 68 | GrGLProgramBuilder::GrGLProgramBuilder(GrGLGpu* gpu, |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 69 | GrRenderTarget* renderTarget, |
| 70 | GrSurfaceOrigin origin, |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 71 | const GrPipeline& pipeline, |
| 72 | const GrPrimitiveProcessor& primProc, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 73 | const GrTextureProxy* const primProcProxies[], |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 74 | GrProgramDesc* desc) |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 75 | : INHERITED(renderTarget, origin, primProc, primProcProxies, pipeline, desc) |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 76 | , fGpu(gpu) |
| 77 | , fVaryingHandler(this) |
| 78 | , fUniformHandler(this) |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 79 | , fVertexAttributeCnt(0) |
| 80 | , fInstanceAttributeCnt(0) |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 81 | , fVertexStride(0) |
| 82 | , fInstanceStride(0) {} |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 83 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 84 | const GrCaps* GrGLProgramBuilder::caps() const { |
| 85 | return fGpu->caps(); |
| 86 | } |
| 87 | |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame^] | 88 | bool GrGLProgramBuilder::compileAndAttachShaders(const SkSL::String& glsl, |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 89 | GrGLuint programId, |
| 90 | GrGLenum type, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 91 | SkTDArray<GrGLuint>* shaderIds, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 92 | const SkSL::Program::Inputs& inputs) { |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 93 | GrGLGpu* gpu = this->gpu(); |
| 94 | GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(), |
| 95 | programId, |
| 96 | type, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 97 | glsl, |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame^] | 98 | gpu->stats()); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 99 | if (!shaderId) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | *shaderIds->append() = shaderId; |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 104 | if (inputs.fFlipY) { |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 105 | GrProgramDesc* d = this->desc(); |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 106 | d->setSurfaceOriginKey(GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(this->origin())); |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 107 | } |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 112 | void GrGLProgramBuilder::computeCountsAndStrides(GrGLuint programID, |
| 113 | const GrPrimitiveProcessor& primProc, |
| 114 | bool bindAttribLocations) { |
| 115 | fVertexAttributeCnt = primProc.numVertexAttributes(); |
| 116 | fInstanceAttributeCnt = primProc.numInstanceAttributes(); |
| 117 | fAttributes.reset( |
| 118 | new GrGLProgram::Attribute[fVertexAttributeCnt + fInstanceAttributeCnt]); |
| 119 | auto addAttr = [&](int i, const auto& a, size_t* stride) { |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 120 | fAttributes[i].fCPUType = a.cpuType(); |
| 121 | fAttributes[i].fGPUType = a.gpuType(); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 122 | fAttributes[i].fOffset = *stride; |
| 123 | *stride += a.sizeAlign4(); |
| 124 | fAttributes[i].fLocation = i; |
| 125 | if (bindAttribLocations) { |
| 126 | GL_CALL(BindAttribLocation(programID, i, a.name())); |
| 127 | } |
| 128 | }; |
| 129 | fVertexStride = 0; |
| 130 | int i = 0; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 131 | for (const auto& attr : primProc.vertexAttributes()) { |
| 132 | addAttr(i++, attr, &fVertexStride); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 133 | } |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 134 | SkASSERT(fVertexStride == primProc.vertexStride()); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 135 | fInstanceStride = 0; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 136 | for (const auto& attr : primProc.instanceAttributes()) { |
| 137 | addAttr(i++, attr, &fInstanceStride); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 138 | } |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 139 | SkASSERT(fInstanceStride == primProc.instanceStride()); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 142 | void GrGLProgramBuilder::addInputVars(const SkSL::Program::Inputs& inputs) { |
| 143 | if (inputs.fRTWidth) { |
| 144 | this->addRTWidthUniform(SKSL_RTWIDTH_NAME); |
| 145 | } |
| 146 | if (inputs.fRTHeight) { |
| 147 | this->addRTHeightUniform(SKSL_RTHEIGHT_NAME); |
| 148 | } |
| 149 | } |
| 150 | |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 151 | static constexpr SkFourByteTag kSKSL_Tag = SkSetFourByteTag('S', 'K', 'S', 'L'); |
| 152 | static constexpr SkFourByteTag kGLSL_Tag = SkSetFourByteTag('G', 'L', 'S', 'L'); |
| 153 | |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 154 | void GrGLProgramBuilder::storeShaderInCache(const SkSL::Program::Inputs& inputs, GrGLuint programID, |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 155 | const SkSL::String shaders[], bool isSkSL) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 156 | if (!this->gpu()->getContext()->priv().getPersistentCache()) { |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | sk_sp<SkData> key = SkData::MakeWithoutCopy(desc()->asKey(), desc()->keyLength()); |
| 160 | if (fGpu->glCaps().programBinarySupport()) { |
| 161 | // binary cache |
| 162 | GrGLsizei length = 0; |
| 163 | GL_CALL(GetProgramiv(programID, GL_PROGRAM_BINARY_LENGTH, &length)); |
| 164 | if (length > 0) { |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 165 | SkWriter32 writer; |
| 166 | writer.writePad(&inputs, sizeof(inputs)); |
| 167 | writer.write32(length); |
| 168 | |
| 169 | void* binary = writer.reservePad(length); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 170 | GrGLenum binaryFormat; |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 171 | GL_CALL(GetProgramBinary(programID, length, &length, &binaryFormat, binary)); |
| 172 | writer.write32(binaryFormat); |
| 173 | |
| 174 | auto data = writer.snapshotAsData(); |
| 175 | this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 176 | } |
| 177 | } else { |
| 178 | // source cache |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 179 | auto data = GrPersistentCacheUtils::PackCachedShaders(isSkSL ? kSKSL_Tag : kGLSL_Tag, |
| 180 | shaders, &inputs, 1); |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 181 | this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 185 | GrGLProgram* GrGLProgramBuilder::finalize() { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 186 | TRACE_EVENT0("skia", TRACE_FUNC); |
Ryan Macnak | 38a10ad | 2017-07-10 10:36:34 -0700 | [diff] [blame] | 187 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 188 | // verify we can get a program id |
| 189 | GrGLuint programID; |
| 190 | GL_CALL_RET(programID, CreateProgram()); |
| 191 | if (0 == programID) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 192 | return nullptr; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Ethan Nicholas | 06d55fb | 2017-11-08 09:48:50 -0500 | [diff] [blame] | 195 | if (this->gpu()->glCaps().programBinarySupport() && |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 196 | this->gpu()->getContext()->priv().getPersistentCache()) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 197 | GL_CALL(ProgramParameteri(programID, GR_GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GR_GL_TRUE)); |
| 198 | } |
| 199 | |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 200 | this->finalizeShaders(); |
| 201 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 202 | // compile shaders and bind attributes / uniforms |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 203 | const GrPrimitiveProcessor& primProc = this->primitiveProcessor(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 204 | SkSL::Program::Settings settings; |
| 205 | settings.fCaps = this->gpu()->glCaps().shaderCaps(); |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 206 | settings.fFlipY = this->origin() != kTopLeft_GrSurfaceOrigin; |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 207 | settings.fSharpenTextures = |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 208 | this->gpu()->getContext()->priv().options().fSharpenMipmappedTextures; |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 209 | settings.fFragColorIsInOut = this->fragColorIsInOut(); |
| 210 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 211 | SkSL::Program::Inputs inputs; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 212 | SkTDArray<GrGLuint> shadersToDelete; |
Ethan Nicholas | 5a0338c | 2019-01-02 11:48:56 -0500 | [diff] [blame] | 213 | // Calling GetProgramiv is expensive in Chromium. Assume success in release builds. |
| 214 | bool checkLinked = kChromium_GrGLDriver != fGpu->ctxInfo().driver(); |
| 215 | #ifdef SK_DEBUG |
| 216 | checkLinked = true; |
| 217 | #endif |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 218 | bool cached = fCached.get() != nullptr; |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 219 | SkSL::String glsl[kGrShaderTypeCount]; |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 220 | SkSL::String* sksl[kGrShaderTypeCount] = { |
| 221 | &fVS.fCompilerString, |
| 222 | &fGS.fCompilerString, |
| 223 | &fFS.fCompilerString, |
| 224 | }; |
| 225 | #if GR_TEST_UTILS |
| 226 | SkSL::String cached_sksl[kGrShaderTypeCount]; |
| 227 | #endif |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 228 | if (cached) { |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 229 | if (fGpu->glCaps().programBinarySupport()) { |
| 230 | // binary cache hit, just hand the binary to GL |
Brian Osman | e6ef03d | 2019-04-11 14:38:27 -0400 | [diff] [blame] | 231 | SkReader32 reader(fCached->data(), fCached->size()); |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 232 | reader.read(&inputs, sizeof(inputs)); |
| 233 | GrGLsizei length = reader.readInt(); |
| 234 | const void* binary = reader.skip(length); |
| 235 | GrGLenum binaryFormat = reader.readU32(); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 236 | GrGLClearErr(this->gpu()->glInterface()); |
| 237 | GR_GL_CALL_NOERRCHECK(this->gpu()->glInterface(), |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 238 | ProgramBinary(programID, binaryFormat, const_cast<void*>(binary), |
| 239 | length)); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 240 | if (GR_GL_GET_ERROR(this->gpu()->glInterface()) == GR_GL_NO_ERROR) { |
| 241 | if (checkLinked) { |
| 242 | cached = this->checkLinkStatus(programID); |
| 243 | } |
| 244 | if (cached) { |
| 245 | this->addInputVars(inputs); |
| 246 | this->computeCountsAndStrides(programID, primProc, false); |
| 247 | } |
| 248 | } else { |
| 249 | cached = false; |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 250 | } |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 251 | #if GR_TEST_UTILS |
| 252 | } else if (fGpu->getContext()->priv().options().fCacheSKSL) { |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 253 | // Only switch to the stored SkSL if it unpacks correctly |
| 254 | if (kSKSL_Tag == GrPersistentCacheUtils::UnpackCachedShaders(fCached.get(), |
| 255 | cached_sksl, |
| 256 | &inputs, 1)) { |
| 257 | for (int i = 0; i < kGrShaderTypeCount; ++i) { |
| 258 | sksl[i] = &cached_sksl[i]; |
| 259 | } |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 260 | } |
| 261 | #endif |
Ethan Nicholas | 98ad5b7 | 2018-03-13 09:53:02 -0400 | [diff] [blame] | 262 | } else { |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 263 | // source cache hit, we don't need to compile the SkSL->GLSL |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 264 | // It's unlikely, but if we get the wrong kind of shader back, don't use the strings |
| 265 | if (kGLSL_Tag != GrPersistentCacheUtils::UnpackCachedShaders(fCached.get(), |
| 266 | glsl, |
| 267 | &inputs, 1)) { |
| 268 | for (int i = 0; i < kGrShaderTypeCount; ++i) { |
| 269 | glsl[i].clear(); |
| 270 | } |
| 271 | } |
Ethan Nicholas | 98ad5b7 | 2018-03-13 09:53:02 -0400 | [diff] [blame] | 272 | } |
| 273 | } |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 274 | if (!cached || !fGpu->glCaps().programBinarySupport()) { |
| 275 | // either a cache miss, or we can't store binaries in the cache |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 276 | if (glsl[kFragment_GrShaderType].empty()) { |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 277 | // Don't have cached GLSL, need to compile SkSL->GLSL |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 278 | if (fFS.fForceHighPrecision) { |
| 279 | settings.fForceHighPrecision = true; |
| 280 | } |
| 281 | std::unique_ptr<SkSL::Program> fs = GrSkSLtoGLSL(gpu()->glContext(), |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame^] | 282 | SkSL::Program::kFragment_Kind, |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 283 | *sksl[kFragment_GrShaderType], |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 284 | settings, |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 285 | &glsl[kFragment_GrShaderType]); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 286 | if (!fs) { |
| 287 | this->cleanupProgram(programID, shadersToDelete); |
| 288 | return nullptr; |
| 289 | } |
| 290 | inputs = fs->fInputs; |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 291 | this->addInputVars(inputs); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 292 | } else { |
| 293 | // we've pulled GLSL and inputs from the cache, but still need to do some setup |
| 294 | this->addInputVars(inputs); |
| 295 | this->computeCountsAndStrides(programID, primProc, false); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 296 | } |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame^] | 297 | if (!this->compileAndAttachShaders(glsl[kFragment_GrShaderType], programID, |
| 298 | GR_GL_FRAGMENT_SHADER, &shadersToDelete, inputs)) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 299 | this->cleanupProgram(programID, shadersToDelete); |
| 300 | return nullptr; |
| 301 | } |
| 302 | |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 303 | if (glsl[kVertex_GrShaderType].empty()) { |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 304 | // Don't have cached GLSL, need to compile SkSL->GLSL |
| 305 | std::unique_ptr<SkSL::Program> vs = GrSkSLtoGLSL(gpu()->glContext(), |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame^] | 306 | SkSL::Program::kVertex_Kind, |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 307 | *sksl[kVertex_GrShaderType], |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 308 | settings, |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 309 | &glsl[kVertex_GrShaderType]); |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 310 | if (!vs) { |
| 311 | this->cleanupProgram(programID, shadersToDelete); |
| 312 | return nullptr; |
| 313 | } |
| 314 | } |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame^] | 315 | if (!this->compileAndAttachShaders(glsl[kVertex_GrShaderType], programID, |
| 316 | GR_GL_VERTEX_SHADER, &shadersToDelete, inputs)) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 317 | this->cleanupProgram(programID, shadersToDelete); |
| 318 | return nullptr; |
| 319 | } |
| 320 | |
| 321 | // NVPR actually requires a vertex shader to compile |
| 322 | bool useNvpr = primProc.isPathRendering(); |
| 323 | if (!useNvpr) { |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 324 | this->computeCountsAndStrides(programID, primProc, true); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (primProc.willUseGeoShader()) { |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 328 | if (glsl[kGeometry_GrShaderType].empty()) { |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 329 | // Don't have cached GLSL, need to compile SkSL->GLSL |
| 330 | std::unique_ptr<SkSL::Program> gs; |
| 331 | gs = GrSkSLtoGLSL(gpu()->glContext(), |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame^] | 332 | SkSL::Program::kGeometry_Kind, |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 333 | *sksl[kGeometry_GrShaderType], |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 334 | settings, |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 335 | &glsl[kGeometry_GrShaderType]); |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 336 | if (!gs) { |
| 337 | this->cleanupProgram(programID, shadersToDelete); |
| 338 | return nullptr; |
| 339 | } |
| 340 | } |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame^] | 341 | if (!this->compileAndAttachShaders(glsl[kGeometry_GrShaderType], programID, |
| 342 | GR_GL_GEOMETRY_SHADER, &shadersToDelete, inputs)) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 343 | this->cleanupProgram(programID, shadersToDelete); |
| 344 | return nullptr; |
| 345 | } |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 346 | } |
| 347 | this->bindProgramResourceLocations(programID); |
| 348 | |
| 349 | GL_CALL(LinkProgram(programID)); |
Ethan Nicholas | 5a0338c | 2019-01-02 11:48:56 -0500 | [diff] [blame] | 350 | if (checkLinked) { |
| 351 | if (!this->checkLinkStatus(programID)) { |
| 352 | GL_CALL(DeleteProgram(programID)); |
Ethan Nicholas | 5a0338c | 2019-01-02 11:48:56 -0500 | [diff] [blame] | 353 | GrGLPrintShader(fGpu->glContext(), |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame^] | 354 | SkSL::Program::kVertex_Kind, |
Brian Osman | 6c431d5 | 2019-04-15 16:31:54 -0400 | [diff] [blame] | 355 | fVS.fCompilerString, |
Ethan Nicholas | 5a0338c | 2019-01-02 11:48:56 -0500 | [diff] [blame] | 356 | settings); |
| 357 | if (primProc.willUseGeoShader()) { |
Ethan Nicholas | 5a0338c | 2019-01-02 11:48:56 -0500 | [diff] [blame] | 358 | GrGLPrintShader(fGpu->glContext(), |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame^] | 359 | SkSL::Program::kGeometry_Kind, |
Brian Osman | 6c431d5 | 2019-04-15 16:31:54 -0400 | [diff] [blame] | 360 | fGS.fCompilerString, |
| 361 | settings); |
Ethan Nicholas | 5a0338c | 2019-01-02 11:48:56 -0500 | [diff] [blame] | 362 | } |
Ethan Nicholas | 5a0338c | 2019-01-02 11:48:56 -0500 | [diff] [blame] | 363 | GrGLPrintShader(fGpu->glContext(), |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame^] | 364 | SkSL::Program::kFragment_Kind, |
Brian Osman | 6c431d5 | 2019-04-15 16:31:54 -0400 | [diff] [blame] | 365 | fFS.fCompilerString, |
Ethan Nicholas | 5a0338c | 2019-01-02 11:48:56 -0500 | [diff] [blame] | 366 | settings); |
| 367 | return nullptr; |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 368 | } |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 369 | } |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 370 | } |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 371 | this->resolveProgramResourceLocations(programID); |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 372 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 373 | this->cleanupShaders(shadersToDelete); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 374 | if (!cached) { |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 375 | bool isSkSL = false; |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 376 | #if GR_TEST_UTILS |
| 377 | if (fGpu->getContext()->priv().options().fCacheSKSL) { |
| 378 | for (int i = 0; i < kGrShaderTypeCount; ++i) { |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame^] | 379 | glsl[i] = GrShaderUtils::PrettyPrint(*sksl[i]); |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 380 | } |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 381 | isSkSL = true; |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 382 | } |
| 383 | #endif |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 384 | this->storeShaderInCache(inputs, programID, glsl, isSkSL); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 385 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 386 | return this->createProgram(programID); |
| 387 | } |
| 388 | |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 389 | void GrGLProgramBuilder::bindProgramResourceLocations(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 390 | fUniformHandler.bindUniformLocations(programID, fGpu->glCaps()); |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 391 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 392 | const GrGLCaps& caps = this->gpu()->glCaps(); |
| 393 | if (fFS.hasCustomColorOutput() && caps.bindFragDataLocationSupport()) { |
| 394 | GL_CALL(BindFragDataLocation(programID, 0, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 395 | GrGLSLFragmentShaderBuilder::DeclaredColorOutputName())); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 396 | } |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 397 | if (fFS.hasSecondaryOutput() && caps.shaderCaps()->mustDeclareFragmentShaderOutput()) { |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 398 | GL_CALL(BindFragDataLocationIndexed(programID, 0, 1, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 399 | GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName())); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 400 | } |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 401 | |
| 402 | // handle NVPR separable varyings |
| 403 | if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() || |
| 404 | !fGpu->glPathRendering()->shouldBindFragmentInputs()) { |
| 405 | return; |
| 406 | } |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 407 | int count = fVaryingHandler.fPathProcVaryingInfos.count(); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 408 | for (int i = 0; i < count; ++i) { |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 409 | GL_CALL(BindFragmentInputLocation(programID, i, |
| 410 | fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str())); |
| 411 | fVaryingHandler.fPathProcVaryingInfos[i].fLocation = i; |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 412 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | bool GrGLProgramBuilder::checkLinkStatus(GrGLuint programID) { |
| 416 | GrGLint linked = GR_GL_INIT_ZERO; |
| 417 | GL_CALL(GetProgramiv(programID, GR_GL_LINK_STATUS, &linked)); |
| 418 | if (!linked) { |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 419 | SkDebugf("Program linking failed.\n"); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 420 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 421 | GL_CALL(GetProgramiv(programID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 422 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 423 | if (infoLen > 0) { |
| 424 | // retrieve length even though we don't need it to workaround |
| 425 | // bug in chrome cmd buffer param validation. |
| 426 | GrGLsizei length = GR_GL_INIT_ZERO; |
| 427 | GL_CALL(GetProgramInfoLog(programID, |
| 428 | infoLen+1, |
| 429 | &length, |
| 430 | (char*)log.get())); |
kkinnunen | 297aaf9 | 2015-02-19 06:32:12 -0800 | [diff] [blame] | 431 | SkDebugf("%s", (char*)log.get()); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 432 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 433 | } |
| 434 | return SkToBool(linked); |
| 435 | } |
| 436 | |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 437 | void GrGLProgramBuilder::resolveProgramResourceLocations(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 438 | fUniformHandler.getUniformLocations(programID, fGpu->glCaps()); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 439 | |
| 440 | // handle NVPR separable varyings |
| 441 | if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() || |
kkinnunen | 7bdb05d | 2016-01-25 00:47:23 -0800 | [diff] [blame] | 442 | fGpu->glPathRendering()->shouldBindFragmentInputs()) { |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 443 | return; |
| 444 | } |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 445 | int count = fVaryingHandler.fPathProcVaryingInfos.count(); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 446 | for (int i = 0; i < count; ++i) { |
| 447 | GrGLint location; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 448 | GL_CALL_RET(location, GetProgramResourceLocation( |
| 449 | programID, |
| 450 | GR_GL_FRAGMENT_INPUT, |
| 451 | fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str())); |
| 452 | fVaryingHandler.fPathProcVaryingInfos[i].fLocation = location; |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 453 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 454 | } |
| 455 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 456 | void GrGLProgramBuilder::cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs) { |
| 457 | GL_CALL(DeleteProgram(programID)); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 458 | this->cleanupShaders(shaderIDs); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 459 | } |
| 460 | void GrGLProgramBuilder::cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs) { |
| 461 | for (int i = 0; i < shaderIDs.count(); ++i) { |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 462 | GL_CALL(DeleteShader(shaderIDs[i])); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
| 466 | GrGLProgram* GrGLProgramBuilder::createProgram(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 467 | return new GrGLProgram(fGpu, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 468 | fUniformHandles, |
| 469 | programID, |
| 470 | fUniformHandler.fUniforms, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 471 | fUniformHandler.fSamplers, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 472 | fVaryingHandler.fPathProcVaryingInfos, |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 473 | std::move(fGeometryProcessor), |
| 474 | std::move(fXferProcessor), |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 475 | std::move(fFragmentProcessors), |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 476 | fFragmentProcessorCnt, |
| 477 | std::move(fAttributes), |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 478 | fVertexAttributeCnt, |
| 479 | fInstanceAttributeCnt, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 480 | fVertexStride, |
| 481 | fInstanceStride); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 482 | } |