blob: 507d210cad14b79f1d9ec34506745b364384451f [file] [log] [blame]
joshualitt30ba4362014-08-21 20:18:45 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/gl/builders/GrGLProgramBuilder.h"
joshualitt8072caa2015-02-12 14:20:52 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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 Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrShaderCaps.h"
Brian Osmanac9be9d2019-05-01 10:29:34 -040022#include "src/gpu/GrShaderUtils.h"
Brian Salomon3ec1f542019-06-17 17:54:57 +000023#include "src/gpu/GrSwizzle.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#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"
joshualitt30ba4362014-08-21 20:18:45 -070032
joshualitt30ba4362014-08-21 20:18:45 -070033#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 Osmaned58e002019-09-06 14:42:43 -040036static 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
42static 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 Phillips65a77752019-10-02 15:22:24 -040048GrGLProgram* GrGLProgramBuilder::CreateProgram(GrRenderTarget* renderTarget,
49 int numSamples,
50 GrSurfaceOrigin origin,
Robert Phillipsd0fe8752019-01-31 14:13:59 -050051 const GrPrimitiveProcessor& primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -050052 const GrTextureProxy* const primProcProxies[],
Brian Salomonff168d92018-06-23 15:17:27 -040053 const GrPipeline& pipeline,
Ethan Nicholas38657112017-02-09 17:01:22 -050054 GrProgramDesc* desc,
Brian Osmaned58e002019-09-06 14:42:43 -040055 GrGLGpu* gpu,
56 const GrGLPrecompiledProgram* precompiledProgram) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000057 SkASSERT(!pipeline.isBad());
Robert Phillipsa91e0b72017-05-01 13:12:20 -040058
Derek Sollenberger488f0d62017-03-03 15:48:33 -050059 ATRACE_ANDROID_FRAMEWORK("Shader Compile");
bsalomon3318ee72015-03-16 11:56:29 -070060 GrAutoLocaleSetter als("C");
61
joshualitt47bb3822014-10-07 16:43:25 -070062 // create a builder. This will be handed off to effects so they can use it to add
63 // uniforms, varyings, textures, etc
Robert Phillips65a77752019-10-02 15:22:24 -040064 GrGLProgramBuilder builder(gpu, renderTarget, numSamples, origin,
Robert Phillipsd0fe8752019-01-31 14:13:59 -050065 pipeline, primProc, primProcProxies, desc);
joshualitt47bb3822014-10-07 16:43:25 -070066
Robert Phillips9da87e02019-02-04 13:26:26 -050067 auto persistentCache = gpu->getContext()->priv().getPersistentCache();
Brian Osmaned58e002019-09-06 14:42:43 -040068 if (persistentCache && !precompiledProgram) {
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040069 sk_sp<SkData> key = SkData::MakeWithoutCopy(desc->asKey(), desc->keyLength());
Robert Phillips0c4b7b12018-03-06 08:20:37 -050070 builder.fCached = persistentCache->load(*key);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040071 // the eventual end goal is to completely skip emitAndInstallProcs on a cache hit, but it's
72 // doing necessary setup in addition to generating the SkSL code. Currently we are only able
73 // to skip the SkSL->GLSL step on a cache hit.
74 }
Ethan Nicholas2983f402017-05-08 09:36:08 -040075 if (!builder.emitAndInstallProcs()) {
halcanary96fcdcc2015-08-27 07:41:13 -070076 return nullptr;
joshualitt6c891102015-05-13 08:51:49 -070077 }
Brian Osmaned58e002019-09-06 14:42:43 -040078 return builder.finalize(precompiledProgram);
joshualitt47bb3822014-10-07 16:43:25 -070079}
80
joshualitt47bb3822014-10-07 16:43:25 -070081/////////////////////////////////////////////////////////////////////////////
82
egdaniel0e1853c2016-03-17 11:35:45 -070083GrGLProgramBuilder::GrGLProgramBuilder(GrGLGpu* gpu,
Robert Phillipsd0fe8752019-01-31 14:13:59 -050084 GrRenderTarget* renderTarget,
Robert Phillips65a77752019-10-02 15:22:24 -040085 int numSamples,
Robert Phillipsd0fe8752019-01-31 14:13:59 -050086 GrSurfaceOrigin origin,
egdaniel0e1853c2016-03-17 11:35:45 -070087 const GrPipeline& pipeline,
88 const GrPrimitiveProcessor& primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -050089 const GrTextureProxy* const primProcProxies[],
Ethan Nicholas38657112017-02-09 17:01:22 -050090 GrProgramDesc* desc)
Robert Phillips65a77752019-10-02 15:22:24 -040091 : INHERITED(renderTarget, numSamples, origin, primProc, primProcProxies, pipeline, desc)
Brian Salomon802cb312018-06-08 18:05:20 -040092 , fGpu(gpu)
93 , fVaryingHandler(this)
94 , fUniformHandler(this)
Brian Salomon92be2f72018-06-19 14:33:47 -040095 , fVertexAttributeCnt(0)
96 , fInstanceAttributeCnt(0)
Brian Salomon802cb312018-06-08 18:05:20 -040097 , fVertexStride(0)
98 , fInstanceStride(0) {}
joshualitt30ba4362014-08-21 20:18:45 -070099
egdanielfa896322016-01-13 12:19:30 -0800100const GrCaps* GrGLProgramBuilder::caps() const {
101 return fGpu->caps();
102}
103
Brian Osmanac9be9d2019-05-01 10:29:34 -0400104bool GrGLProgramBuilder::compileAndAttachShaders(const SkSL::String& glsl,
egdaniel574a4c12015-11-02 06:22:44 -0800105 GrGLuint programId,
106 GrGLenum type,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500107 SkTDArray<GrGLuint>* shaderIds,
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400108 GrContextOptions::ShaderErrorHandler* errHandler) {
egdaniel574a4c12015-11-02 06:22:44 -0800109 GrGLGpu* gpu = this->gpu();
110 GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(),
111 programId,
112 type,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400113 glsl,
Brian Osman8518f2e2019-05-01 14:13:41 -0400114 gpu->stats(),
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400115 errHandler);
egdaniel574a4c12015-11-02 06:22:44 -0800116 if (!shaderId) {
117 return false;
118 }
119
120 *shaderIds->append() = shaderId;
egdaniel574a4c12015-11-02 06:22:44 -0800121 return true;
122}
123
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400124void GrGLProgramBuilder::computeCountsAndStrides(GrGLuint programID,
125 const GrPrimitiveProcessor& primProc,
126 bool bindAttribLocations) {
127 fVertexAttributeCnt = primProc.numVertexAttributes();
128 fInstanceAttributeCnt = primProc.numInstanceAttributes();
129 fAttributes.reset(
130 new GrGLProgram::Attribute[fVertexAttributeCnt + fInstanceAttributeCnt]);
131 auto addAttr = [&](int i, const auto& a, size_t* stride) {
Brian Osman4a3f5c82018-09-18 16:16:38 -0400132 fAttributes[i].fCPUType = a.cpuType();
133 fAttributes[i].fGPUType = a.gpuType();
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400134 fAttributes[i].fOffset = *stride;
135 *stride += a.sizeAlign4();
136 fAttributes[i].fLocation = i;
137 if (bindAttribLocations) {
138 GL_CALL(BindAttribLocation(programID, i, a.name()));
139 }
140 };
141 fVertexStride = 0;
142 int i = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500143 for (const auto& attr : primProc.vertexAttributes()) {
144 addAttr(i++, attr, &fVertexStride);
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400145 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500146 SkASSERT(fVertexStride == primProc.vertexStride());
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400147 fInstanceStride = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500148 for (const auto& attr : primProc.instanceAttributes()) {
149 addAttr(i++, attr, &fInstanceStride);
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400150 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500151 SkASSERT(fInstanceStride == primProc.instanceStride());
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400152}
153
Ethan Nicholascd700e92018-08-24 16:43:57 -0400154void GrGLProgramBuilder::addInputVars(const SkSL::Program::Inputs& inputs) {
155 if (inputs.fRTWidth) {
156 this->addRTWidthUniform(SKSL_RTWIDTH_NAME);
157 }
158 if (inputs.fRTHeight) {
159 this->addRTHeightUniform(SKSL_RTHEIGHT_NAME);
160 }
161}
162
Brian Osmana085a412019-04-25 09:44:43 -0400163static constexpr SkFourByteTag kSKSL_Tag = SkSetFourByteTag('S', 'K', 'S', 'L');
164static constexpr SkFourByteTag kGLSL_Tag = SkSetFourByteTag('G', 'L', 'S', 'L');
Brian Osmana66081d2019-09-03 14:59:26 -0400165static constexpr SkFourByteTag kGLPB_Tag = SkSetFourByteTag('G', 'L', 'P', 'B');
Brian Osmana085a412019-04-25 09:44:43 -0400166
Ethan Nicholas8d058832019-01-07 10:49:58 -0500167void GrGLProgramBuilder::storeShaderInCache(const SkSL::Program::Inputs& inputs, GrGLuint programID,
Brian Osmaned58e002019-09-06 14:42:43 -0400168 const SkSL::String shaders[], bool isSkSL,
Brian Osman4524e842019-09-24 16:03:41 -0400169 SkSL::Program::Settings* settings) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500170 if (!this->gpu()->getContext()->priv().getPersistentCache()) {
Ethan Nicholas8d058832019-01-07 10:49:58 -0500171 return;
172 }
173 sk_sp<SkData> key = SkData::MakeWithoutCopy(desc()->asKey(), desc()->keyLength());
174 if (fGpu->glCaps().programBinarySupport()) {
175 // binary cache
176 GrGLsizei length = 0;
177 GL_CALL(GetProgramiv(programID, GL_PROGRAM_BINARY_LENGTH, &length));
178 if (length > 0) {
Brian Osman6b797fe2019-04-08 13:56:36 -0400179 SkWriter32 writer;
Brian Osmana66081d2019-09-03 14:59:26 -0400180 writer.write32(kGLPB_Tag);
181
Brian Osman6b797fe2019-04-08 13:56:36 -0400182 writer.writePad(&inputs, sizeof(inputs));
183 writer.write32(length);
184
185 void* binary = writer.reservePad(length);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500186 GrGLenum binaryFormat;
Brian Osman6b797fe2019-04-08 13:56:36 -0400187 GL_CALL(GetProgramBinary(programID, length, &length, &binaryFormat, binary));
188 writer.write32(binaryFormat);
189
190 auto data = writer.snapshotAsData();
191 this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500192 }
193 } else {
Brian Osman4524e842019-09-24 16:03:41 -0400194 // source cache, plus metadata to allow for a complete precompile
195 GrPersistentCacheUtils::ShaderMetadata meta;
196 meta.fSettings = settings;
197 meta.fHasCustomColorOutput = fFS.hasCustomColorOutput();
198 meta.fHasSecondaryColorOutput = fFS.hasSecondaryOutput();
199 for (const auto& attr : this->primitiveProcessor().vertexAttributes()) {
200 meta.fAttributeNames.emplace_back(attr.name());
201 }
202 for (const auto& attr : this->primitiveProcessor().instanceAttributes()) {
203 meta.fAttributeNames.emplace_back(attr.name());
204 }
205
Brian Osmana085a412019-04-25 09:44:43 -0400206 auto data = GrPersistentCacheUtils::PackCachedShaders(isSkSL ? kSKSL_Tag : kGLSL_Tag,
Brian Osman4524e842019-09-24 16:03:41 -0400207 shaders, &inputs, 1, &meta);
Brian Osman6b797fe2019-04-08 13:56:36 -0400208 this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500209 }
210}
211
Brian Osmaned58e002019-09-06 14:42:43 -0400212GrGLProgram* GrGLProgramBuilder::finalize(const GrGLPrecompiledProgram* precompiledProgram) {
Brian Salomon5f394272019-07-02 14:07:49 -0400213 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -0700214
joshualitt47bb3822014-10-07 16:43:25 -0700215 // verify we can get a program id
216 GrGLuint programID;
Brian Osmaned58e002019-09-06 14:42:43 -0400217 if (precompiledProgram) {
218 programID = precompiledProgram->fProgramID;
219 } else {
220 GL_CALL_RET(programID, CreateProgram());
221 }
joshualitt47bb3822014-10-07 16:43:25 -0700222 if (0 == programID) {
halcanary96fcdcc2015-08-27 07:41:13 -0700223 return nullptr;
joshualitt30ba4362014-08-21 20:18:45 -0700224 }
225
Ethan Nicholas06d55fb2017-11-08 09:48:50 -0500226 if (this->gpu()->glCaps().programBinarySupport() &&
Brian Osman064729e2019-06-18 17:22:59 -0400227 this->gpu()->glCaps().programParameterSupport() &&
Brian Osmaned58e002019-09-06 14:42:43 -0400228 this->gpu()->getContext()->priv().getPersistentCache() &&
229 !precompiledProgram) {
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400230 GL_CALL(ProgramParameteri(programID, GR_GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GR_GL_TRUE));
231 }
232
egdaniel9f1d4152016-02-10 09:50:38 -0800233 this->finalizeShaders();
234
joshualitt47bb3822014-10-07 16:43:25 -0700235 // compile shaders and bind attributes / uniforms
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400236 auto errorHandler = this->gpu()->getContext()->priv().getShaderErrorHandler();
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400237 const GrPrimitiveProcessor& primProc = this->primitiveProcessor();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500238 SkSL::Program::Settings settings;
239 settings.fCaps = this->gpu()->glCaps().shaderCaps();
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500240 settings.fFlipY = this->origin() != kTopLeft_GrSurfaceOrigin;
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500241 settings.fSharpenTextures =
Robert Phillips9da87e02019-02-04 13:26:26 -0500242 this->gpu()->getContext()->priv().options().fSharpenMipmappedTextures;
Brian Salomondc092132018-04-04 10:14:16 -0400243 settings.fFragColorIsInOut = this->fragColorIsInOut();
244
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500245 SkSL::Program::Inputs inputs;
joshualitt30ba4362014-08-21 20:18:45 -0700246 SkTDArray<GrGLuint> shadersToDelete;
Ethan Nicholas5a0338c2019-01-02 11:48:56 -0500247 // Calling GetProgramiv is expensive in Chromium. Assume success in release builds.
248 bool checkLinked = kChromium_GrGLDriver != fGpu->ctxInfo().driver();
249#ifdef SK_DEBUG
250 checkLinked = true;
251#endif
Ethan Nicholas8d058832019-01-07 10:49:58 -0500252 bool cached = fCached.get() != nullptr;
Brian Osman2c60a382019-06-26 15:19:30 -0400253 bool usedProgramBinaries = false;
Brian Osman6b797fe2019-04-08 13:56:36 -0400254 SkSL::String glsl[kGrShaderTypeCount];
Brian Osmancbc33b82019-04-19 14:16:19 -0400255 SkSL::String* sksl[kGrShaderTypeCount] = {
256 &fVS.fCompilerString,
257 &fGS.fCompilerString,
258 &fFS.fCompilerString,
259 };
Brian Osmancbc33b82019-04-19 14:16:19 -0400260 SkSL::String cached_sksl[kGrShaderTypeCount];
Brian Osmaned58e002019-09-06 14:42:43 -0400261 if (precompiledProgram) {
262 // This is very similar to when we get program binaries. We even set that flag, as it's
263 // used to prevent other compile work later, and to force re-querying uniform locations.
264 this->addInputVars(precompiledProgram->fInputs);
Brian Osman4524e842019-09-24 16:03:41 -0400265 this->computeCountsAndStrides(programID, primProc, false);
Brian Osmaned58e002019-09-06 14:42:43 -0400266 usedProgramBinaries = true;
267 } else if (cached) {
Brian Osmana66081d2019-09-03 14:59:26 -0400268 SkReader32 reader(fCached->data(), fCached->size());
269 SkFourByteTag shaderType = reader.readU32();
270
271 switch (shaderType) {
272 case kGLPB_Tag: {
273 // Program binary cache hit. We may opt not to use this if we don't trust program
274 // binaries on this driver
275 if (!fGpu->glCaps().programBinarySupport()) {
276 cached = false;
277 break;
Ethan Nicholas8d058832019-01-07 10:49:58 -0500278 }
Brian Osmana66081d2019-09-03 14:59:26 -0400279 reader.read(&inputs, sizeof(inputs));
280 GrGLsizei length = reader.readInt();
281 const void* binary = reader.skip(length);
282 GrGLenum binaryFormat = reader.readU32();
283 GrGLClearErr(this->gpu()->glInterface());
284 GR_GL_CALL_NOERRCHECK(this->gpu()->glInterface(),
285 ProgramBinary(programID, binaryFormat,
286 const_cast<void*>(binary), length));
287 if (GR_GL_GET_ERROR(this->gpu()->glInterface()) == GR_GL_NO_ERROR) {
288 if (checkLinked) {
289 cached = this->checkLinkStatus(programID, errorHandler, nullptr, nullptr);
290 }
291 if (cached) {
292 this->addInputVars(inputs);
293 this->computeCountsAndStrides(programID, primProc, false);
294 }
295 } else {
296 cached = false;
Ethan Nicholas8d058832019-01-07 10:49:58 -0500297 }
Brian Osmana66081d2019-09-03 14:59:26 -0400298 usedProgramBinaries = cached;
299 break;
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400300 }
Brian Osmana66081d2019-09-03 14:59:26 -0400301
302 case kGLSL_Tag:
303 // Source cache hit, we don't need to compile the SkSL->GLSL
304 GrPersistentCacheUtils::UnpackCachedShaders(&reader, glsl, &inputs, 1);
305 break;
306
307 case kSKSL_Tag:
308 // SkSL cache hit, this should only happen in tools overriding the generated SkSL
309 GrPersistentCacheUtils::UnpackCachedShaders(&reader, cached_sksl, &inputs, 1);
Brian Osmana085a412019-04-25 09:44:43 -0400310 for (int i = 0; i < kGrShaderTypeCount; ++i) {
311 sksl[i] = &cached_sksl[i];
312 }
Brian Osmana66081d2019-09-03 14:59:26 -0400313 break;
Ethan Nicholas98ad5b72018-03-13 09:53:02 -0400314 }
315 }
Brian Osmana66081d2019-09-03 14:59:26 -0400316 if (!usedProgramBinaries) {
Brian Osmanf7924452019-09-24 10:44:37 -0400317 // Either a cache miss, or we got something other than binaries from the cache
318
319 /*
320 Fragment Shader
321 */
Brian Osman6b797fe2019-04-08 13:56:36 -0400322 if (glsl[kFragment_GrShaderType].empty()) {
Brian Osmanf71b0702019-04-03 13:04:16 -0400323 // Don't have cached GLSL, need to compile SkSL->GLSL
Ethan Nicholas8d058832019-01-07 10:49:58 -0500324 if (fFS.fForceHighPrecision) {
325 settings.fForceHighPrecision = true;
326 }
327 std::unique_ptr<SkSL::Program> fs = GrSkSLtoGLSL(gpu()->glContext(),
Brian Osmanac9be9d2019-05-01 10:29:34 -0400328 SkSL::Program::kFragment_Kind,
Brian Osmancbc33b82019-04-19 14:16:19 -0400329 *sksl[kFragment_GrShaderType],
Ethan Nicholas8d058832019-01-07 10:49:58 -0500330 settings,
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400331 &glsl[kFragment_GrShaderType],
332 errorHandler);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500333 if (!fs) {
Brian Osmaned58e002019-09-06 14:42:43 -0400334 cleanup_program(fGpu, programID, shadersToDelete);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500335 return nullptr;
336 }
337 inputs = fs->fInputs;
egdaniel8dcdedc2015-11-11 06:27:20 -0800338 }
Brian Osmanf7924452019-09-24 10:44:37 -0400339
340 this->addInputVars(inputs);
Brian Osmanac9be9d2019-05-01 10:29:34 -0400341 if (!this->compileAndAttachShaders(glsl[kFragment_GrShaderType], programID,
Brian Osmane11dfd32019-07-23 10:29:41 -0400342 GR_GL_FRAGMENT_SHADER, &shadersToDelete, errorHandler)) {
Brian Osmaned58e002019-09-06 14:42:43 -0400343 cleanup_program(fGpu, programID, shadersToDelete);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400344 return nullptr;
345 }
346
Brian Osmanf7924452019-09-24 10:44:37 -0400347 /*
348 Vertex Shader
349 */
Brian Osman6b797fe2019-04-08 13:56:36 -0400350 if (glsl[kVertex_GrShaderType].empty()) {
Brian Osmanf71b0702019-04-03 13:04:16 -0400351 // Don't have cached GLSL, need to compile SkSL->GLSL
352 std::unique_ptr<SkSL::Program> vs = GrSkSLtoGLSL(gpu()->glContext(),
Brian Osmanac9be9d2019-05-01 10:29:34 -0400353 SkSL::Program::kVertex_Kind,
Brian Osmancbc33b82019-04-19 14:16:19 -0400354 *sksl[kVertex_GrShaderType],
Brian Osmanf71b0702019-04-03 13:04:16 -0400355 settings,
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400356 &glsl[kVertex_GrShaderType],
357 errorHandler);
Brian Osmanf71b0702019-04-03 13:04:16 -0400358 if (!vs) {
Brian Osmaned58e002019-09-06 14:42:43 -0400359 cleanup_program(fGpu, programID, shadersToDelete);
Brian Osmanf71b0702019-04-03 13:04:16 -0400360 return nullptr;
361 }
362 }
Brian Osmanac9be9d2019-05-01 10:29:34 -0400363 if (!this->compileAndAttachShaders(glsl[kVertex_GrShaderType], programID,
Brian Osmane11dfd32019-07-23 10:29:41 -0400364 GR_GL_VERTEX_SHADER, &shadersToDelete, errorHandler)) {
Brian Osmaned58e002019-09-06 14:42:43 -0400365 cleanup_program(fGpu, programID, shadersToDelete);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400366 return nullptr;
367 }
368
Brian Osmanf7924452019-09-24 10:44:37 -0400369 // This also binds vertex attribute locations. NVPR doesn't really use vertices,
370 // even though it requires a vertex shader in the program.
371 if (!primProc.isPathRendering()) {
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400372 this->computeCountsAndStrides(programID, primProc, true);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400373 }
374
Brian Osmanf7924452019-09-24 10:44:37 -0400375 /*
376 Geometry Shader
377 */
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400378 if (primProc.willUseGeoShader()) {
Brian Osman6b797fe2019-04-08 13:56:36 -0400379 if (glsl[kGeometry_GrShaderType].empty()) {
Brian Osmanf71b0702019-04-03 13:04:16 -0400380 // Don't have cached GLSL, need to compile SkSL->GLSL
381 std::unique_ptr<SkSL::Program> gs;
382 gs = GrSkSLtoGLSL(gpu()->glContext(),
Brian Osmanac9be9d2019-05-01 10:29:34 -0400383 SkSL::Program::kGeometry_Kind,
Brian Osmancbc33b82019-04-19 14:16:19 -0400384 *sksl[kGeometry_GrShaderType],
Brian Osmanf71b0702019-04-03 13:04:16 -0400385 settings,
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400386 &glsl[kGeometry_GrShaderType],
387 errorHandler);
Brian Osmanf71b0702019-04-03 13:04:16 -0400388 if (!gs) {
Brian Osmaned58e002019-09-06 14:42:43 -0400389 cleanup_program(fGpu, programID, shadersToDelete);
Brian Osmanf71b0702019-04-03 13:04:16 -0400390 return nullptr;
391 }
392 }
Brian Osmanac9be9d2019-05-01 10:29:34 -0400393 if (!this->compileAndAttachShaders(glsl[kGeometry_GrShaderType], programID,
Brian Osmane11dfd32019-07-23 10:29:41 -0400394 GR_GL_GEOMETRY_SHADER, &shadersToDelete,
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400395 errorHandler)) {
Brian Osmaned58e002019-09-06 14:42:43 -0400396 cleanup_program(fGpu, programID, shadersToDelete);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400397 return nullptr;
398 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400399 }
400 this->bindProgramResourceLocations(programID);
401
402 GL_CALL(LinkProgram(programID));
Ethan Nicholas5a0338c2019-01-02 11:48:56 -0500403 if (checkLinked) {
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400404 if (!this->checkLinkStatus(programID, errorHandler, sksl, glsl)) {
Brian Osman2ab2ac02019-09-09 13:54:54 -0400405 cleanup_program(fGpu, programID, shadersToDelete);
Ethan Nicholas5a0338c2019-01-02 11:48:56 -0500406 return nullptr;
Brian Salomone334c592017-05-15 11:00:58 -0400407 }
Brian Salomone334c592017-05-15 11:00:58 -0400408 }
joshualittfe1233c2014-10-07 12:16:35 -0700409 }
Brian Osman2c60a382019-06-26 15:19:30 -0400410 this->resolveProgramResourceLocations(programID, usedProgramBinaries);
joshualittdb0d3ca2014-10-07 12:42:26 -0700411
Brian Osmaned58e002019-09-06 14:42:43 -0400412 cleanup_shaders(fGpu, shadersToDelete);
Brian Osmane4c88bb2019-06-27 16:15:11 -0400413
414 // With ANGLE, we can't cache path-rendering programs. We use ProgramPathFragmentInputGen,
415 // and ANGLE's deserialized program state doesn't restore enough state to handle that.
416 // The native NVIDIA drivers do, but this is such an edge case that it's easier to just
417 // black-list caching these programs in all cases. See: anglebug.com/3619
Brian Osmaned58e002019-09-06 14:42:43 -0400418 // We also can't cache SkSL or GLSL if we were given a precompiled program, but there's not
419 // much point in doing so.
420 if (!cached && !primProc.isPathRendering() && !precompiledProgram) {
Brian Osmana085a412019-04-25 09:44:43 -0400421 bool isSkSL = false;
Brian Osmana66081d2019-09-03 14:59:26 -0400422 if (fGpu->getContext()->priv().options().fShaderCacheStrategy ==
423 GrContextOptions::ShaderCacheStrategy::kSkSL) {
Brian Osmancbc33b82019-04-19 14:16:19 -0400424 for (int i = 0; i < kGrShaderTypeCount; ++i) {
Brian Osmanac9be9d2019-05-01 10:29:34 -0400425 glsl[i] = GrShaderUtils::PrettyPrint(*sksl[i]);
Brian Osmancbc33b82019-04-19 14:16:19 -0400426 }
Brian Osmana085a412019-04-25 09:44:43 -0400427 isSkSL = true;
Brian Osmancbc33b82019-04-19 14:16:19 -0400428 }
Brian Osman4524e842019-09-24 16:03:41 -0400429 this->storeShaderInCache(inputs, programID, glsl, isSkSL, &settings);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400430 }
joshualitt47bb3822014-10-07 16:43:25 -0700431 return this->createProgram(programID);
432}
433
kkinnunen7aedda52015-06-29 23:01:28 -0700434void GrGLProgramBuilder::bindProgramResourceLocations(GrGLuint programID) {
egdaniel7ea439b2015-12-03 09:20:44 -0800435 fUniformHandler.bindUniformLocations(programID, fGpu->glCaps());
kkinnunen7aedda52015-06-29 23:01:28 -0700436
egdaniel8dcdedc2015-11-11 06:27:20 -0800437 const GrGLCaps& caps = this->gpu()->glCaps();
438 if (fFS.hasCustomColorOutput() && caps.bindFragDataLocationSupport()) {
439 GL_CALL(BindFragDataLocation(programID, 0,
egdaniel2d721d32015-11-11 13:06:05 -0800440 GrGLSLFragmentShaderBuilder::DeclaredColorOutputName()));
egdaniel8dcdedc2015-11-11 06:27:20 -0800441 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500442 if (fFS.hasSecondaryOutput() && caps.shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdaniel8dcdedc2015-11-11 06:27:20 -0800443 GL_CALL(BindFragDataLocationIndexed(programID, 0, 1,
egdaniel2d721d32015-11-11 13:06:05 -0800444 GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName()));
egdaniel8dcdedc2015-11-11 06:27:20 -0800445 }
joshualittd8dd47b2015-09-11 11:45:01 -0700446
447 // handle NVPR separable varyings
448 if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() ||
449 !fGpu->glPathRendering()->shouldBindFragmentInputs()) {
450 return;
451 }
egdaniel0eafe792015-11-20 14:01:22 -0800452 int count = fVaryingHandler.fPathProcVaryingInfos.count();
joshualittd8dd47b2015-09-11 11:45:01 -0700453 for (int i = 0; i < count; ++i) {
egdaniel0eafe792015-11-20 14:01:22 -0800454 GL_CALL(BindFragmentInputLocation(programID, i,
455 fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str()));
456 fVaryingHandler.fPathProcVaryingInfos[i].fLocation = i;
joshualittd8dd47b2015-09-11 11:45:01 -0700457 }
joshualitt47bb3822014-10-07 16:43:25 -0700458}
459
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400460bool GrGLProgramBuilder::checkLinkStatus(GrGLuint programID,
461 GrContextOptions::ShaderErrorHandler* errorHandler,
462 SkSL::String* sksl[], const SkSL::String glsl[]) {
joshualitt47bb3822014-10-07 16:43:25 -0700463 GrGLint linked = GR_GL_INIT_ZERO;
464 GL_CALL(GetProgramiv(programID, GR_GL_LINK_STATUS, &linked));
465 if (!linked) {
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400466 SkSL::String allShaders;
467 if (sksl) {
468 allShaders.appendf("// Vertex SKSL\n%s\n", sksl[kVertex_GrShaderType]->c_str());
469 if (!sksl[kGeometry_GrShaderType]->empty()) {
470 allShaders.appendf("// Geometry SKSL\n%s\n", sksl[kGeometry_GrShaderType]->c_str());
471 }
472 allShaders.appendf("// Fragment SKSL\n%s\n", sksl[kFragment_GrShaderType]->c_str());
473 }
474 if (glsl) {
475 allShaders.appendf("// Vertex GLSL\n%s\n", glsl[kVertex_GrShaderType].c_str());
476 if (!glsl[kGeometry_GrShaderType].empty()) {
477 allShaders.appendf("// Geometry GLSL\n%s\n", glsl[kGeometry_GrShaderType].c_str());
478 }
479 allShaders.appendf("// Fragment GLSL\n%s\n", glsl[kFragment_GrShaderType].c_str());
480 }
joshualitt47bb3822014-10-07 16:43:25 -0700481 GrGLint infoLen = GR_GL_INIT_ZERO;
482 GL_CALL(GetProgramiv(programID, GR_GL_INFO_LOG_LENGTH, &infoLen));
483 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
484 if (infoLen > 0) {
485 // retrieve length even though we don't need it to workaround
486 // bug in chrome cmd buffer param validation.
487 GrGLsizei length = GR_GL_INIT_ZERO;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400488 GL_CALL(GetProgramInfoLog(programID, infoLen+1, &length, (char*)log.get()));
joshualitt47bb3822014-10-07 16:43:25 -0700489 }
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400490 errorHandler->compileError(allShaders.c_str(), infoLen > 0 ? (const char*)log.get() : "");
joshualitt47bb3822014-10-07 16:43:25 -0700491 }
492 return SkToBool(linked);
493}
494
Brian Osman2c60a382019-06-26 15:19:30 -0400495void GrGLProgramBuilder::resolveProgramResourceLocations(GrGLuint programID, bool force) {
496 fUniformHandler.getUniformLocations(programID, fGpu->glCaps(), force);
joshualittd8dd47b2015-09-11 11:45:01 -0700497
498 // handle NVPR separable varyings
499 if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() ||
kkinnunen7bdb05d2016-01-25 00:47:23 -0800500 fGpu->glPathRendering()->shouldBindFragmentInputs()) {
joshualittd8dd47b2015-09-11 11:45:01 -0700501 return;
502 }
egdaniel0eafe792015-11-20 14:01:22 -0800503 int count = fVaryingHandler.fPathProcVaryingInfos.count();
joshualittd8dd47b2015-09-11 11:45:01 -0700504 for (int i = 0; i < count; ++i) {
505 GrGLint location;
egdaniel0eafe792015-11-20 14:01:22 -0800506 GL_CALL_RET(location, GetProgramResourceLocation(
507 programID,
508 GR_GL_FRAGMENT_INPUT,
509 fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str()));
510 fVaryingHandler.fPathProcVaryingInfos[i].fLocation = location;
joshualittd8dd47b2015-09-11 11:45:01 -0700511 }
joshualitt30ba4362014-08-21 20:18:45 -0700512}
513
joshualitt47bb3822014-10-07 16:43:25 -0700514GrGLProgram* GrGLProgramBuilder::createProgram(GrGLuint programID) {
egdaniel7ea439b2015-12-03 09:20:44 -0800515 return new GrGLProgram(fGpu,
egdaniel7ea439b2015-12-03 09:20:44 -0800516 fUniformHandles,
517 programID,
518 fUniformHandler.fUniforms,
egdaniel09aa1fc2016-04-20 07:09:46 -0700519 fUniformHandler.fSamplers,
egdaniel0eafe792015-11-20 14:01:22 -0800520 fVaryingHandler.fPathProcVaryingInfos,
Robert Phillips369e8b72017-08-01 16:13:04 -0400521 std::move(fGeometryProcessor),
522 std::move(fXferProcessor),
Brian Salomon4d3f5172018-06-07 14:42:52 -0400523 std::move(fFragmentProcessors),
Brian Salomon802cb312018-06-08 18:05:20 -0400524 fFragmentProcessorCnt,
525 std::move(fAttributes),
Brian Salomon92be2f72018-06-19 14:33:47 -0400526 fVertexAttributeCnt,
527 fInstanceAttributeCnt,
Brian Salomon802cb312018-06-08 18:05:20 -0400528 fVertexStride,
529 fInstanceStride);
joshualitt47bb3822014-10-07 16:43:25 -0700530}
Brian Osmaned58e002019-09-06 14:42:43 -0400531
532bool GrGLProgramBuilder::PrecompileProgram(GrGLPrecompiledProgram* precompiledProgram,
533 GrGLGpu* gpu,
534 const SkData& cachedData) {
535 SkReader32 reader(cachedData.data(), cachedData.size());
536 SkFourByteTag shaderType = reader.readU32();
537 if (shaderType != kSKSL_Tag) {
538 // TODO: Support GLSL, and maybe even program binaries, too?
539 return false;
540 }
541
542 const GrGLInterface* gl = gpu->glInterface();
543 auto errorHandler = gpu->getContext()->priv().getShaderErrorHandler();
544 GrGLuint programID;
545 GR_GL_CALL_RET(gl, programID, CreateProgram());
546 if (0 == programID) {
547 return false;
548 }
549
550 SkTDArray<GrGLuint> shadersToDelete;
551
552 SkSL::Program::Settings settings;
Brian Osman4524e842019-09-24 16:03:41 -0400553 const GrGLCaps& caps = gpu->glCaps();
554 settings.fCaps = caps.shaderCaps();
Brian Osmaned58e002019-09-06 14:42:43 -0400555 settings.fSharpenTextures = gpu->getContext()->priv().options().fSharpenMipmappedTextures;
Brian Osman4524e842019-09-24 16:03:41 -0400556 GrPersistentCacheUtils::ShaderMetadata meta;
557 meta.fSettings = &settings;
Brian Osmaned58e002019-09-06 14:42:43 -0400558
559 SkSL::String shaders[kGrShaderTypeCount];
560 SkSL::Program::Inputs inputs;
Brian Osman4524e842019-09-24 16:03:41 -0400561 GrPersistentCacheUtils::UnpackCachedShaders(&reader, shaders, &inputs, 1, &meta);
Brian Osmaned58e002019-09-06 14:42:43 -0400562
563 auto compileShader = [&](SkSL::Program::Kind kind, const SkSL::String& sksl, GrGLenum type) {
564 SkSL::String glsl;
565 auto program = GrSkSLtoGLSL(gpu->glContext(), kind, sksl, settings, &glsl, errorHandler);
566 if (!program) {
567 return false;
568 }
569
570 if (GrGLuint shaderID = GrGLCompileAndAttachShader(gpu->glContext(), programID, type, glsl,
571 gpu->stats(), errorHandler)) {
572 shadersToDelete.push_back(shaderID);
573 return true;
574 } else {
575 return false;
576 }
577 };
578
579 if (!compileShader(SkSL::Program::kFragment_Kind,
580 shaders[kFragment_GrShaderType],
581 GR_GL_FRAGMENT_SHADER) ||
582 !compileShader(SkSL::Program::kVertex_Kind,
583 shaders[kVertex_GrShaderType],
584 GR_GL_VERTEX_SHADER) ||
585 (!shaders[kGeometry_GrShaderType].empty() &&
586 !compileShader(SkSL::Program::kGeometry_Kind,
587 shaders[kGeometry_GrShaderType],
588 GR_GL_GEOMETRY_SHADER))) {
589 cleanup_program(gpu, programID, shadersToDelete);
590 return false;
591 }
592
Brian Osman4524e842019-09-24 16:03:41 -0400593 for (int i = 0; i < meta.fAttributeNames.count(); ++i) {
594 GR_GL_CALL(gpu->glInterface(), BindAttribLocation(programID, i,
595 meta.fAttributeNames[i].c_str()));
596 }
597
598 if (meta.fHasCustomColorOutput && caps.bindFragDataLocationSupport()) {
599 GR_GL_CALL(gpu->glInterface(), BindFragDataLocation(programID, 0,
600 GrGLSLFragmentShaderBuilder::DeclaredColorOutputName()));
601 }
602 if (meta.fHasSecondaryColorOutput && caps.shaderCaps()->mustDeclareFragmentShaderOutput()) {
603 GR_GL_CALL(gpu->glInterface(), BindFragDataLocationIndexed(programID, 0, 1,
604 GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName()));
605 }
606
Brian Osmaned58e002019-09-06 14:42:43 -0400607 GR_GL_CALL(gpu->glInterface(), LinkProgram(programID));
608 GrGLint linked = GR_GL_INIT_ZERO;
609 GR_GL_CALL(gpu->glInterface(), GetProgramiv(programID, GR_GL_LINK_STATUS, &linked));
610 if (!linked) {
611 cleanup_program(gpu, programID, shadersToDelete);
612 return false;
613 }
614
615 cleanup_shaders(gpu, shadersToDelete);
616
617 precompiledProgram->fProgramID = programID;
618 precompiledProgram->fInputs = inputs;
619 return true;
620}