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" |
Brian Salomon | 3ec1f54 | 2019-06-17 17:54:57 +0000 | [diff] [blame] | 23 | #include "src/gpu/GrSwizzle.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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 | |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 36 | static void cleanup_shaders(GrGLGpu* gpu, const SkTDArray<GrGLuint>& shaderIDs) { |
| 37 | for (int i = 0; i < shaderIDs.count(); ++i) { |
| 38 | GR_GL_CALL(gpu->glInterface(), DeleteShader(shaderIDs[i])); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | static void cleanup_program(GrGLGpu* gpu, GrGLuint programID, |
| 43 | const SkTDArray<GrGLuint>& shaderIDs) { |
| 44 | GR_GL_CALL(gpu->glInterface(), DeleteProgram(programID)); |
| 45 | cleanup_shaders(gpu, shaderIDs); |
| 46 | } |
| 47 | |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 48 | GrGLProgram* GrGLProgramBuilder::CreateProgram(GrRenderTarget* renderTarget, GrSurfaceOrigin origin, |
| 49 | const GrPrimitiveProcessor& primProc, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 50 | const GrTextureProxy* const primProcProxies[], |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 51 | const GrPipeline& pipeline, |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 52 | GrProgramDesc* desc, |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 53 | GrGLGpu* gpu, |
| 54 | const GrGLPrecompiledProgram* precompiledProgram) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 55 | SkASSERT(!pipeline.isBad()); |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 56 | |
Derek Sollenberger | 488f0d6 | 2017-03-03 15:48:33 -0500 | [diff] [blame] | 57 | ATRACE_ANDROID_FRAMEWORK("Shader Compile"); |
bsalomon | 3318ee7 | 2015-03-16 11:56:29 -0700 | [diff] [blame] | 58 | GrAutoLocaleSetter als("C"); |
| 59 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 60 | // create a builder. This will be handed off to effects so they can use it to add |
| 61 | // uniforms, varyings, textures, etc |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 62 | GrGLProgramBuilder builder(gpu, renderTarget, origin, |
| 63 | pipeline, primProc, primProcProxies, desc); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 64 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 65 | auto persistentCache = gpu->getContext()->priv().getPersistentCache(); |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 66 | if (persistentCache && !precompiledProgram) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 67 | sk_sp<SkData> key = SkData::MakeWithoutCopy(desc->asKey(), desc->keyLength()); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 68 | builder.fCached = persistentCache->load(*key); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 69 | // the eventual end goal is to completely skip emitAndInstallProcs on a cache hit, but it's |
| 70 | // doing necessary setup in addition to generating the SkSL code. Currently we are only able |
| 71 | // to skip the SkSL->GLSL step on a cache hit. |
| 72 | } |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 73 | if (!builder.emitAndInstallProcs()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | return nullptr; |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 75 | } |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 76 | return builder.finalize(precompiledProgram); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 77 | } |
| 78 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 79 | ///////////////////////////////////////////////////////////////////////////// |
| 80 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 81 | GrGLProgramBuilder::GrGLProgramBuilder(GrGLGpu* gpu, |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 82 | GrRenderTarget* renderTarget, |
| 83 | GrSurfaceOrigin origin, |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 84 | const GrPipeline& pipeline, |
| 85 | const GrPrimitiveProcessor& primProc, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 86 | const GrTextureProxy* const primProcProxies[], |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 87 | GrProgramDesc* desc) |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 88 | : INHERITED(renderTarget, origin, primProc, primProcProxies, pipeline, desc) |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 89 | , fGpu(gpu) |
| 90 | , fVaryingHandler(this) |
| 91 | , fUniformHandler(this) |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 92 | , fVertexAttributeCnt(0) |
| 93 | , fInstanceAttributeCnt(0) |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 94 | , fVertexStride(0) |
| 95 | , fInstanceStride(0) {} |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 96 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 97 | const GrCaps* GrGLProgramBuilder::caps() const { |
| 98 | return fGpu->caps(); |
| 99 | } |
| 100 | |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 101 | bool GrGLProgramBuilder::compileAndAttachShaders(const SkSL::String& glsl, |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 102 | GrGLuint programId, |
| 103 | GrGLenum type, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 104 | SkTDArray<GrGLuint>* shaderIds, |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 105 | GrContextOptions::ShaderErrorHandler* errHandler) { |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 106 | GrGLGpu* gpu = this->gpu(); |
| 107 | GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(), |
| 108 | programId, |
| 109 | type, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 110 | glsl, |
Brian Osman | 8518f2e | 2019-05-01 14:13:41 -0400 | [diff] [blame] | 111 | gpu->stats(), |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 112 | errHandler); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 113 | if (!shaderId) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | *shaderIds->append() = shaderId; |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 121 | void GrGLProgramBuilder::computeCountsAndStrides(GrGLuint programID, |
| 122 | const GrPrimitiveProcessor& primProc, |
| 123 | bool bindAttribLocations) { |
| 124 | fVertexAttributeCnt = primProc.numVertexAttributes(); |
| 125 | fInstanceAttributeCnt = primProc.numInstanceAttributes(); |
| 126 | fAttributes.reset( |
| 127 | new GrGLProgram::Attribute[fVertexAttributeCnt + fInstanceAttributeCnt]); |
| 128 | auto addAttr = [&](int i, const auto& a, size_t* stride) { |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 129 | fAttributes[i].fCPUType = a.cpuType(); |
| 130 | fAttributes[i].fGPUType = a.gpuType(); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 131 | fAttributes[i].fOffset = *stride; |
| 132 | *stride += a.sizeAlign4(); |
| 133 | fAttributes[i].fLocation = i; |
| 134 | if (bindAttribLocations) { |
| 135 | GL_CALL(BindAttribLocation(programID, i, a.name())); |
| 136 | } |
| 137 | }; |
| 138 | fVertexStride = 0; |
| 139 | int i = 0; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 140 | for (const auto& attr : primProc.vertexAttributes()) { |
| 141 | addAttr(i++, attr, &fVertexStride); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 142 | } |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 143 | SkASSERT(fVertexStride == primProc.vertexStride()); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 144 | fInstanceStride = 0; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 145 | for (const auto& attr : primProc.instanceAttributes()) { |
| 146 | addAttr(i++, attr, &fInstanceStride); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 147 | } |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 148 | SkASSERT(fInstanceStride == primProc.instanceStride()); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 149 | } |
| 150 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 151 | void GrGLProgramBuilder::addInputVars(const SkSL::Program::Inputs& inputs) { |
| 152 | if (inputs.fRTWidth) { |
| 153 | this->addRTWidthUniform(SKSL_RTWIDTH_NAME); |
| 154 | } |
| 155 | if (inputs.fRTHeight) { |
| 156 | this->addRTHeightUniform(SKSL_RTHEIGHT_NAME); |
| 157 | } |
| 158 | } |
| 159 | |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 160 | static constexpr SkFourByteTag kSKSL_Tag = SkSetFourByteTag('S', 'K', 'S', 'L'); |
| 161 | static constexpr SkFourByteTag kGLSL_Tag = SkSetFourByteTag('G', 'L', 'S', 'L'); |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 162 | static constexpr SkFourByteTag kGLPB_Tag = SkSetFourByteTag('G', 'L', 'P', 'B'); |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 163 | |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 164 | void GrGLProgramBuilder::storeShaderInCache(const SkSL::Program::Inputs& inputs, GrGLuint programID, |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 165 | const SkSL::String shaders[], bool isSkSL, |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 166 | SkSL::Program::Settings* settings) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 167 | if (!this->gpu()->getContext()->priv().getPersistentCache()) { |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | sk_sp<SkData> key = SkData::MakeWithoutCopy(desc()->asKey(), desc()->keyLength()); |
| 171 | if (fGpu->glCaps().programBinarySupport()) { |
| 172 | // binary cache |
| 173 | GrGLsizei length = 0; |
| 174 | GL_CALL(GetProgramiv(programID, GL_PROGRAM_BINARY_LENGTH, &length)); |
| 175 | if (length > 0) { |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 176 | SkWriter32 writer; |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 177 | writer.write32(kGLPB_Tag); |
| 178 | |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 179 | writer.writePad(&inputs, sizeof(inputs)); |
| 180 | writer.write32(length); |
| 181 | |
| 182 | void* binary = writer.reservePad(length); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 183 | GrGLenum binaryFormat; |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 184 | GL_CALL(GetProgramBinary(programID, length, &length, &binaryFormat, binary)); |
| 185 | writer.write32(binaryFormat); |
| 186 | |
| 187 | auto data = writer.snapshotAsData(); |
| 188 | this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 189 | } |
| 190 | } else { |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 191 | // source cache, plus metadata to allow for a complete precompile |
| 192 | GrPersistentCacheUtils::ShaderMetadata meta; |
| 193 | meta.fSettings = settings; |
| 194 | meta.fHasCustomColorOutput = fFS.hasCustomColorOutput(); |
| 195 | meta.fHasSecondaryColorOutput = fFS.hasSecondaryOutput(); |
| 196 | for (const auto& attr : this->primitiveProcessor().vertexAttributes()) { |
| 197 | meta.fAttributeNames.emplace_back(attr.name()); |
| 198 | } |
| 199 | for (const auto& attr : this->primitiveProcessor().instanceAttributes()) { |
| 200 | meta.fAttributeNames.emplace_back(attr.name()); |
| 201 | } |
| 202 | |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 203 | auto data = GrPersistentCacheUtils::PackCachedShaders(isSkSL ? kSKSL_Tag : kGLSL_Tag, |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 204 | shaders, &inputs, 1, &meta); |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 205 | this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 209 | GrGLProgram* GrGLProgramBuilder::finalize(const GrGLPrecompiledProgram* precompiledProgram) { |
Brian Salomon | 5f39427 | 2019-07-02 14:07:49 -0400 | [diff] [blame] | 210 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Ryan Macnak | 38a10ad | 2017-07-10 10:36:34 -0700 | [diff] [blame] | 211 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 212 | // verify we can get a program id |
| 213 | GrGLuint programID; |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 214 | if (precompiledProgram) { |
| 215 | programID = precompiledProgram->fProgramID; |
| 216 | } else { |
| 217 | GL_CALL_RET(programID, CreateProgram()); |
| 218 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 219 | if (0 == programID) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 220 | return nullptr; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Ethan Nicholas | 06d55fb | 2017-11-08 09:48:50 -0500 | [diff] [blame] | 223 | if (this->gpu()->glCaps().programBinarySupport() && |
Brian Osman | 064729e | 2019-06-18 17:22:59 -0400 | [diff] [blame] | 224 | this->gpu()->glCaps().programParameterSupport() && |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 225 | this->gpu()->getContext()->priv().getPersistentCache() && |
| 226 | !precompiledProgram) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 227 | GL_CALL(ProgramParameteri(programID, GR_GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GR_GL_TRUE)); |
| 228 | } |
| 229 | |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 230 | this->finalizeShaders(); |
| 231 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 232 | // compile shaders and bind attributes / uniforms |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 233 | auto errorHandler = this->gpu()->getContext()->priv().getShaderErrorHandler(); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 234 | const GrPrimitiveProcessor& primProc = this->primitiveProcessor(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 235 | SkSL::Program::Settings settings; |
| 236 | settings.fCaps = this->gpu()->glCaps().shaderCaps(); |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 237 | settings.fFlipY = this->origin() != kTopLeft_GrSurfaceOrigin; |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 238 | settings.fSharpenTextures = |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 239 | this->gpu()->getContext()->priv().options().fSharpenMipmappedTextures; |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 240 | settings.fFragColorIsInOut = this->fragColorIsInOut(); |
| 241 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 242 | SkSL::Program::Inputs inputs; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 243 | SkTDArray<GrGLuint> shadersToDelete; |
Ethan Nicholas | 5a0338c | 2019-01-02 11:48:56 -0500 | [diff] [blame] | 244 | // Calling GetProgramiv is expensive in Chromium. Assume success in release builds. |
| 245 | bool checkLinked = kChromium_GrGLDriver != fGpu->ctxInfo().driver(); |
| 246 | #ifdef SK_DEBUG |
| 247 | checkLinked = true; |
| 248 | #endif |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 249 | bool cached = fCached.get() != nullptr; |
Brian Osman | 2c60a38 | 2019-06-26 15:19:30 -0400 | [diff] [blame] | 250 | bool usedProgramBinaries = false; |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 251 | SkSL::String glsl[kGrShaderTypeCount]; |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 252 | SkSL::String* sksl[kGrShaderTypeCount] = { |
| 253 | &fVS.fCompilerString, |
| 254 | &fGS.fCompilerString, |
| 255 | &fFS.fCompilerString, |
| 256 | }; |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 257 | SkSL::String cached_sksl[kGrShaderTypeCount]; |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 258 | if (precompiledProgram) { |
| 259 | // This is very similar to when we get program binaries. We even set that flag, as it's |
| 260 | // used to prevent other compile work later, and to force re-querying uniform locations. |
| 261 | this->addInputVars(precompiledProgram->fInputs); |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 262 | this->computeCountsAndStrides(programID, primProc, false); |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 263 | usedProgramBinaries = true; |
| 264 | } else if (cached) { |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 265 | SkReader32 reader(fCached->data(), fCached->size()); |
| 266 | SkFourByteTag shaderType = reader.readU32(); |
| 267 | |
| 268 | switch (shaderType) { |
| 269 | case kGLPB_Tag: { |
| 270 | // Program binary cache hit. We may opt not to use this if we don't trust program |
| 271 | // binaries on this driver |
| 272 | if (!fGpu->glCaps().programBinarySupport()) { |
| 273 | cached = false; |
| 274 | break; |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 275 | } |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 276 | reader.read(&inputs, sizeof(inputs)); |
| 277 | GrGLsizei length = reader.readInt(); |
| 278 | const void* binary = reader.skip(length); |
| 279 | GrGLenum binaryFormat = reader.readU32(); |
| 280 | GrGLClearErr(this->gpu()->glInterface()); |
| 281 | GR_GL_CALL_NOERRCHECK(this->gpu()->glInterface(), |
| 282 | ProgramBinary(programID, binaryFormat, |
| 283 | const_cast<void*>(binary), length)); |
| 284 | if (GR_GL_GET_ERROR(this->gpu()->glInterface()) == GR_GL_NO_ERROR) { |
| 285 | if (checkLinked) { |
| 286 | cached = this->checkLinkStatus(programID, errorHandler, nullptr, nullptr); |
| 287 | } |
| 288 | if (cached) { |
| 289 | this->addInputVars(inputs); |
| 290 | this->computeCountsAndStrides(programID, primProc, false); |
| 291 | } |
| 292 | } else { |
| 293 | cached = false; |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 294 | } |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 295 | usedProgramBinaries = cached; |
| 296 | break; |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 297 | } |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 298 | |
| 299 | case kGLSL_Tag: |
| 300 | // Source cache hit, we don't need to compile the SkSL->GLSL |
| 301 | GrPersistentCacheUtils::UnpackCachedShaders(&reader, glsl, &inputs, 1); |
| 302 | break; |
| 303 | |
| 304 | case kSKSL_Tag: |
| 305 | // SkSL cache hit, this should only happen in tools overriding the generated SkSL |
| 306 | GrPersistentCacheUtils::UnpackCachedShaders(&reader, cached_sksl, &inputs, 1); |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 307 | for (int i = 0; i < kGrShaderTypeCount; ++i) { |
| 308 | sksl[i] = &cached_sksl[i]; |
| 309 | } |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 310 | break; |
Ethan Nicholas | 98ad5b7 | 2018-03-13 09:53:02 -0400 | [diff] [blame] | 311 | } |
| 312 | } |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 313 | if (!usedProgramBinaries) { |
Brian Osman | f792445 | 2019-09-24 10:44:37 -0400 | [diff] [blame] | 314 | // Either a cache miss, or we got something other than binaries from the cache |
| 315 | |
| 316 | /* |
| 317 | Fragment Shader |
| 318 | */ |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 319 | if (glsl[kFragment_GrShaderType].empty()) { |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 320 | // Don't have cached GLSL, need to compile SkSL->GLSL |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 321 | if (fFS.fForceHighPrecision) { |
| 322 | settings.fForceHighPrecision = true; |
| 323 | } |
| 324 | std::unique_ptr<SkSL::Program> fs = GrSkSLtoGLSL(gpu()->glContext(), |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 325 | SkSL::Program::kFragment_Kind, |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 326 | *sksl[kFragment_GrShaderType], |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 327 | settings, |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 328 | &glsl[kFragment_GrShaderType], |
| 329 | errorHandler); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 330 | if (!fs) { |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 331 | cleanup_program(fGpu, programID, shadersToDelete); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 332 | return nullptr; |
| 333 | } |
| 334 | inputs = fs->fInputs; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 335 | } |
Brian Osman | f792445 | 2019-09-24 10:44:37 -0400 | [diff] [blame] | 336 | |
| 337 | this->addInputVars(inputs); |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 338 | if (!this->compileAndAttachShaders(glsl[kFragment_GrShaderType], programID, |
Brian Osman | e11dfd3 | 2019-07-23 10:29:41 -0400 | [diff] [blame] | 339 | GR_GL_FRAGMENT_SHADER, &shadersToDelete, errorHandler)) { |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 340 | cleanup_program(fGpu, programID, shadersToDelete); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 341 | return nullptr; |
| 342 | } |
| 343 | |
Brian Osman | f792445 | 2019-09-24 10:44:37 -0400 | [diff] [blame] | 344 | /* |
| 345 | Vertex Shader |
| 346 | */ |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 347 | if (glsl[kVertex_GrShaderType].empty()) { |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 348 | // Don't have cached GLSL, need to compile SkSL->GLSL |
| 349 | std::unique_ptr<SkSL::Program> vs = GrSkSLtoGLSL(gpu()->glContext(), |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 350 | SkSL::Program::kVertex_Kind, |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 351 | *sksl[kVertex_GrShaderType], |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 352 | settings, |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 353 | &glsl[kVertex_GrShaderType], |
| 354 | errorHandler); |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 355 | if (!vs) { |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 356 | cleanup_program(fGpu, programID, shadersToDelete); |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 357 | return nullptr; |
| 358 | } |
| 359 | } |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 360 | if (!this->compileAndAttachShaders(glsl[kVertex_GrShaderType], programID, |
Brian Osman | e11dfd3 | 2019-07-23 10:29:41 -0400 | [diff] [blame] | 361 | GR_GL_VERTEX_SHADER, &shadersToDelete, errorHandler)) { |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 362 | cleanup_program(fGpu, programID, shadersToDelete); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 363 | return nullptr; |
| 364 | } |
| 365 | |
Brian Osman | f792445 | 2019-09-24 10:44:37 -0400 | [diff] [blame] | 366 | // This also binds vertex attribute locations. NVPR doesn't really use vertices, |
| 367 | // even though it requires a vertex shader in the program. |
| 368 | if (!primProc.isPathRendering()) { |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 369 | this->computeCountsAndStrides(programID, primProc, true); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 370 | } |
| 371 | |
Brian Osman | f792445 | 2019-09-24 10:44:37 -0400 | [diff] [blame] | 372 | /* |
| 373 | Geometry Shader |
| 374 | */ |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 375 | if (primProc.willUseGeoShader()) { |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 376 | if (glsl[kGeometry_GrShaderType].empty()) { |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 377 | // Don't have cached GLSL, need to compile SkSL->GLSL |
| 378 | std::unique_ptr<SkSL::Program> gs; |
| 379 | gs = GrSkSLtoGLSL(gpu()->glContext(), |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 380 | SkSL::Program::kGeometry_Kind, |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 381 | *sksl[kGeometry_GrShaderType], |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 382 | settings, |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 383 | &glsl[kGeometry_GrShaderType], |
| 384 | errorHandler); |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 385 | if (!gs) { |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 386 | cleanup_program(fGpu, programID, shadersToDelete); |
Brian Osman | f71b070 | 2019-04-03 13:04:16 -0400 | [diff] [blame] | 387 | return nullptr; |
| 388 | } |
| 389 | } |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 390 | if (!this->compileAndAttachShaders(glsl[kGeometry_GrShaderType], programID, |
Brian Osman | e11dfd3 | 2019-07-23 10:29:41 -0400 | [diff] [blame] | 391 | GR_GL_GEOMETRY_SHADER, &shadersToDelete, |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 392 | errorHandler)) { |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 393 | cleanup_program(fGpu, programID, shadersToDelete); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 394 | return nullptr; |
| 395 | } |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 396 | } |
| 397 | this->bindProgramResourceLocations(programID); |
| 398 | |
| 399 | GL_CALL(LinkProgram(programID)); |
Ethan Nicholas | 5a0338c | 2019-01-02 11:48:56 -0500 | [diff] [blame] | 400 | if (checkLinked) { |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 401 | if (!this->checkLinkStatus(programID, errorHandler, sksl, glsl)) { |
Brian Osman | 2ab2ac0 | 2019-09-09 13:54:54 -0400 | [diff] [blame] | 402 | cleanup_program(fGpu, programID, shadersToDelete); |
Ethan Nicholas | 5a0338c | 2019-01-02 11:48:56 -0500 | [diff] [blame] | 403 | return nullptr; |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 404 | } |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 405 | } |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 406 | } |
Brian Osman | 2c60a38 | 2019-06-26 15:19:30 -0400 | [diff] [blame] | 407 | this->resolveProgramResourceLocations(programID, usedProgramBinaries); |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 408 | |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 409 | cleanup_shaders(fGpu, shadersToDelete); |
Brian Osman | e4c88bb | 2019-06-27 16:15:11 -0400 | [diff] [blame] | 410 | |
| 411 | // With ANGLE, we can't cache path-rendering programs. We use ProgramPathFragmentInputGen, |
| 412 | // and ANGLE's deserialized program state doesn't restore enough state to handle that. |
| 413 | // The native NVIDIA drivers do, but this is such an edge case that it's easier to just |
| 414 | // black-list caching these programs in all cases. See: anglebug.com/3619 |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 415 | // We also can't cache SkSL or GLSL if we were given a precompiled program, but there's not |
| 416 | // much point in doing so. |
| 417 | if (!cached && !primProc.isPathRendering() && !precompiledProgram) { |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 418 | bool isSkSL = false; |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 419 | if (fGpu->getContext()->priv().options().fShaderCacheStrategy == |
| 420 | GrContextOptions::ShaderCacheStrategy::kSkSL) { |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 421 | for (int i = 0; i < kGrShaderTypeCount; ++i) { |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 422 | glsl[i] = GrShaderUtils::PrettyPrint(*sksl[i]); |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 423 | } |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 424 | isSkSL = true; |
Brian Osman | cbc33b8 | 2019-04-19 14:16:19 -0400 | [diff] [blame] | 425 | } |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 426 | this->storeShaderInCache(inputs, programID, glsl, isSkSL, &settings); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 427 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 428 | return this->createProgram(programID); |
| 429 | } |
| 430 | |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 431 | void GrGLProgramBuilder::bindProgramResourceLocations(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 432 | fUniformHandler.bindUniformLocations(programID, fGpu->glCaps()); |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 433 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 434 | const GrGLCaps& caps = this->gpu()->glCaps(); |
| 435 | if (fFS.hasCustomColorOutput() && caps.bindFragDataLocationSupport()) { |
| 436 | GL_CALL(BindFragDataLocation(programID, 0, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 437 | GrGLSLFragmentShaderBuilder::DeclaredColorOutputName())); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 438 | } |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 439 | if (fFS.hasSecondaryOutput() && caps.shaderCaps()->mustDeclareFragmentShaderOutput()) { |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 440 | GL_CALL(BindFragDataLocationIndexed(programID, 0, 1, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 441 | GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName())); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 442 | } |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 443 | |
| 444 | // handle NVPR separable varyings |
| 445 | if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() || |
| 446 | !fGpu->glPathRendering()->shouldBindFragmentInputs()) { |
| 447 | return; |
| 448 | } |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 449 | int count = fVaryingHandler.fPathProcVaryingInfos.count(); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 450 | for (int i = 0; i < count; ++i) { |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 451 | GL_CALL(BindFragmentInputLocation(programID, i, |
| 452 | fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str())); |
| 453 | fVaryingHandler.fPathProcVaryingInfos[i].fLocation = i; |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 454 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 457 | bool GrGLProgramBuilder::checkLinkStatus(GrGLuint programID, |
| 458 | GrContextOptions::ShaderErrorHandler* errorHandler, |
| 459 | SkSL::String* sksl[], const SkSL::String glsl[]) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 460 | GrGLint linked = GR_GL_INIT_ZERO; |
| 461 | GL_CALL(GetProgramiv(programID, GR_GL_LINK_STATUS, &linked)); |
| 462 | if (!linked) { |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 463 | SkSL::String allShaders; |
| 464 | if (sksl) { |
| 465 | allShaders.appendf("// Vertex SKSL\n%s\n", sksl[kVertex_GrShaderType]->c_str()); |
| 466 | if (!sksl[kGeometry_GrShaderType]->empty()) { |
| 467 | allShaders.appendf("// Geometry SKSL\n%s\n", sksl[kGeometry_GrShaderType]->c_str()); |
| 468 | } |
| 469 | allShaders.appendf("// Fragment SKSL\n%s\n", sksl[kFragment_GrShaderType]->c_str()); |
| 470 | } |
| 471 | if (glsl) { |
| 472 | allShaders.appendf("// Vertex GLSL\n%s\n", glsl[kVertex_GrShaderType].c_str()); |
| 473 | if (!glsl[kGeometry_GrShaderType].empty()) { |
| 474 | allShaders.appendf("// Geometry GLSL\n%s\n", glsl[kGeometry_GrShaderType].c_str()); |
| 475 | } |
| 476 | allShaders.appendf("// Fragment GLSL\n%s\n", glsl[kFragment_GrShaderType].c_str()); |
| 477 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 478 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 479 | GL_CALL(GetProgramiv(programID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 480 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 481 | if (infoLen > 0) { |
| 482 | // retrieve length even though we don't need it to workaround |
| 483 | // bug in chrome cmd buffer param validation. |
| 484 | GrGLsizei length = GR_GL_INIT_ZERO; |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 485 | GL_CALL(GetProgramInfoLog(programID, infoLen+1, &length, (char*)log.get())); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 486 | } |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 487 | errorHandler->compileError(allShaders.c_str(), infoLen > 0 ? (const char*)log.get() : ""); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 488 | } |
| 489 | return SkToBool(linked); |
| 490 | } |
| 491 | |
Brian Osman | 2c60a38 | 2019-06-26 15:19:30 -0400 | [diff] [blame] | 492 | void GrGLProgramBuilder::resolveProgramResourceLocations(GrGLuint programID, bool force) { |
| 493 | fUniformHandler.getUniformLocations(programID, fGpu->glCaps(), force); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 494 | |
| 495 | // handle NVPR separable varyings |
| 496 | if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() || |
kkinnunen | 7bdb05d | 2016-01-25 00:47:23 -0800 | [diff] [blame] | 497 | fGpu->glPathRendering()->shouldBindFragmentInputs()) { |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 498 | return; |
| 499 | } |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 500 | int count = fVaryingHandler.fPathProcVaryingInfos.count(); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 501 | for (int i = 0; i < count; ++i) { |
| 502 | GrGLint location; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 503 | GL_CALL_RET(location, GetProgramResourceLocation( |
| 504 | programID, |
| 505 | GR_GL_FRAGMENT_INPUT, |
| 506 | fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str())); |
| 507 | fVaryingHandler.fPathProcVaryingInfos[i].fLocation = location; |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 508 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 509 | } |
| 510 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 511 | GrGLProgram* GrGLProgramBuilder::createProgram(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 512 | return new GrGLProgram(fGpu, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 513 | fUniformHandles, |
| 514 | programID, |
| 515 | fUniformHandler.fUniforms, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 516 | fUniformHandler.fSamplers, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 517 | fVaryingHandler.fPathProcVaryingInfos, |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 518 | std::move(fGeometryProcessor), |
| 519 | std::move(fXferProcessor), |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 520 | std::move(fFragmentProcessors), |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 521 | fFragmentProcessorCnt, |
| 522 | std::move(fAttributes), |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 523 | fVertexAttributeCnt, |
| 524 | fInstanceAttributeCnt, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 525 | fVertexStride, |
| 526 | fInstanceStride); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 527 | } |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 528 | |
| 529 | bool GrGLProgramBuilder::PrecompileProgram(GrGLPrecompiledProgram* precompiledProgram, |
| 530 | GrGLGpu* gpu, |
| 531 | const SkData& cachedData) { |
| 532 | SkReader32 reader(cachedData.data(), cachedData.size()); |
| 533 | SkFourByteTag shaderType = reader.readU32(); |
| 534 | if (shaderType != kSKSL_Tag) { |
| 535 | // TODO: Support GLSL, and maybe even program binaries, too? |
| 536 | return false; |
| 537 | } |
| 538 | |
| 539 | const GrGLInterface* gl = gpu->glInterface(); |
| 540 | auto errorHandler = gpu->getContext()->priv().getShaderErrorHandler(); |
| 541 | GrGLuint programID; |
| 542 | GR_GL_CALL_RET(gl, programID, CreateProgram()); |
| 543 | if (0 == programID) { |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | SkTDArray<GrGLuint> shadersToDelete; |
| 548 | |
| 549 | SkSL::Program::Settings settings; |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 550 | const GrGLCaps& caps = gpu->glCaps(); |
| 551 | settings.fCaps = caps.shaderCaps(); |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 552 | settings.fSharpenTextures = gpu->getContext()->priv().options().fSharpenMipmappedTextures; |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 553 | GrPersistentCacheUtils::ShaderMetadata meta; |
| 554 | meta.fSettings = &settings; |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 555 | |
| 556 | SkSL::String shaders[kGrShaderTypeCount]; |
| 557 | SkSL::Program::Inputs inputs; |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 558 | GrPersistentCacheUtils::UnpackCachedShaders(&reader, shaders, &inputs, 1, &meta); |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 559 | |
| 560 | auto compileShader = [&](SkSL::Program::Kind kind, const SkSL::String& sksl, GrGLenum type) { |
| 561 | SkSL::String glsl; |
| 562 | auto program = GrSkSLtoGLSL(gpu->glContext(), kind, sksl, settings, &glsl, errorHandler); |
| 563 | if (!program) { |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | if (GrGLuint shaderID = GrGLCompileAndAttachShader(gpu->glContext(), programID, type, glsl, |
| 568 | gpu->stats(), errorHandler)) { |
| 569 | shadersToDelete.push_back(shaderID); |
| 570 | return true; |
| 571 | } else { |
| 572 | return false; |
| 573 | } |
| 574 | }; |
| 575 | |
| 576 | if (!compileShader(SkSL::Program::kFragment_Kind, |
| 577 | shaders[kFragment_GrShaderType], |
| 578 | GR_GL_FRAGMENT_SHADER) || |
| 579 | !compileShader(SkSL::Program::kVertex_Kind, |
| 580 | shaders[kVertex_GrShaderType], |
| 581 | GR_GL_VERTEX_SHADER) || |
| 582 | (!shaders[kGeometry_GrShaderType].empty() && |
| 583 | !compileShader(SkSL::Program::kGeometry_Kind, |
| 584 | shaders[kGeometry_GrShaderType], |
| 585 | GR_GL_GEOMETRY_SHADER))) { |
| 586 | cleanup_program(gpu, programID, shadersToDelete); |
| 587 | return false; |
| 588 | } |
| 589 | |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 590 | for (int i = 0; i < meta.fAttributeNames.count(); ++i) { |
| 591 | GR_GL_CALL(gpu->glInterface(), BindAttribLocation(programID, i, |
| 592 | meta.fAttributeNames[i].c_str())); |
| 593 | } |
| 594 | |
| 595 | if (meta.fHasCustomColorOutput && caps.bindFragDataLocationSupport()) { |
| 596 | GR_GL_CALL(gpu->glInterface(), BindFragDataLocation(programID, 0, |
| 597 | GrGLSLFragmentShaderBuilder::DeclaredColorOutputName())); |
| 598 | } |
| 599 | if (meta.fHasSecondaryColorOutput && caps.shaderCaps()->mustDeclareFragmentShaderOutput()) { |
| 600 | GR_GL_CALL(gpu->glInterface(), BindFragDataLocationIndexed(programID, 0, 1, |
| 601 | GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName())); |
| 602 | } |
| 603 | |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 604 | GR_GL_CALL(gpu->glInterface(), LinkProgram(programID)); |
| 605 | GrGLint linked = GR_GL_INIT_ZERO; |
| 606 | GR_GL_CALL(gpu->glInterface(), GetProgramiv(programID, GR_GL_LINK_STATUS, &linked)); |
| 607 | if (!linked) { |
| 608 | cleanup_program(gpu, programID, shadersToDelete); |
| 609 | return false; |
| 610 | } |
| 611 | |
| 612 | cleanup_shaders(gpu, shadersToDelete); |
| 613 | |
| 614 | precompiledProgram->fProgramID = programID; |
| 615 | precompiledProgram->fInputs = inputs; |
| 616 | return true; |
| 617 | } |