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