blob: 0fb915d0aed46a53bda3beb35dbdd19db1bfc64a [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,
Robert Phillips901aff02019-10-08 12:32:56 -040049 const GrProgramInfo& programInfo,
Ethan Nicholas38657112017-02-09 17:01:22 -050050 GrProgramDesc* desc,
Brian Osmaned58e002019-09-06 14:42:43 -040051 GrGLGpu* gpu,
52 const GrGLPrecompiledProgram* precompiledProgram) {
Derek Sollenberger488f0d62017-03-03 15:48:33 -050053 ATRACE_ANDROID_FRAMEWORK("Shader Compile");
bsalomon3318ee72015-03-16 11:56:29 -070054 GrAutoLocaleSetter als("C");
55
joshualitt47bb3822014-10-07 16:43:25 -070056 // create a builder. This will be handed off to effects so they can use it to add
57 // uniforms, varyings, textures, etc
Robert Phillips901aff02019-10-08 12:32:56 -040058 GrGLProgramBuilder builder(gpu, renderTarget, programInfo, desc);
joshualitt47bb3822014-10-07 16:43:25 -070059
Robert Phillips9da87e02019-02-04 13:26:26 -050060 auto persistentCache = gpu->getContext()->priv().getPersistentCache();
Brian Osmaned58e002019-09-06 14:42:43 -040061 if (persistentCache && !precompiledProgram) {
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040062 sk_sp<SkData> key = SkData::MakeWithoutCopy(desc->asKey(), desc->keyLength());
Robert Phillips0c4b7b12018-03-06 08:20:37 -050063 builder.fCached = persistentCache->load(*key);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040064 // the eventual end goal is to completely skip emitAndInstallProcs on a cache hit, but it's
65 // doing necessary setup in addition to generating the SkSL code. Currently we are only able
66 // to skip the SkSL->GLSL step on a cache hit.
67 }
Ethan Nicholas2983f402017-05-08 09:36:08 -040068 if (!builder.emitAndInstallProcs()) {
halcanary96fcdcc2015-08-27 07:41:13 -070069 return nullptr;
joshualitt6c891102015-05-13 08:51:49 -070070 }
Brian Osmaned58e002019-09-06 14:42:43 -040071 return builder.finalize(precompiledProgram);
joshualitt47bb3822014-10-07 16:43:25 -070072}
73
joshualitt47bb3822014-10-07 16:43:25 -070074/////////////////////////////////////////////////////////////////////////////
75
egdaniel0e1853c2016-03-17 11:35:45 -070076GrGLProgramBuilder::GrGLProgramBuilder(GrGLGpu* gpu,
Robert Phillipsd0fe8752019-01-31 14:13:59 -050077 GrRenderTarget* renderTarget,
Robert Phillips901aff02019-10-08 12:32:56 -040078 const GrProgramInfo& programInfo,
Ethan Nicholas38657112017-02-09 17:01:22 -050079 GrProgramDesc* desc)
Robert Phillips901aff02019-10-08 12:32:56 -040080 : INHERITED(renderTarget, programInfo, desc)
Brian Salomon802cb312018-06-08 18:05:20 -040081 , fGpu(gpu)
82 , fVaryingHandler(this)
83 , fUniformHandler(this)
Brian Salomon92be2f72018-06-19 14:33:47 -040084 , fVertexAttributeCnt(0)
85 , fInstanceAttributeCnt(0)
Brian Salomon802cb312018-06-08 18:05:20 -040086 , fVertexStride(0)
87 , fInstanceStride(0) {}
joshualitt30ba4362014-08-21 20:18:45 -070088
egdanielfa896322016-01-13 12:19:30 -080089const GrCaps* GrGLProgramBuilder::caps() const {
90 return fGpu->caps();
91}
92
Brian Osmanac9be9d2019-05-01 10:29:34 -040093bool GrGLProgramBuilder::compileAndAttachShaders(const SkSL::String& glsl,
egdaniel574a4c12015-11-02 06:22:44 -080094 GrGLuint programId,
95 GrGLenum type,
Ethan Nicholas941e7e22016-12-12 15:33:30 -050096 SkTDArray<GrGLuint>* shaderIds,
Brian Osman5e7fbfd2019-05-03 13:13:35 -040097 GrContextOptions::ShaderErrorHandler* errHandler) {
egdaniel574a4c12015-11-02 06:22:44 -080098 GrGLGpu* gpu = this->gpu();
99 GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(),
100 programId,
101 type,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400102 glsl,
Brian Osman8518f2e2019-05-01 14:13:41 -0400103 gpu->stats(),
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400104 errHandler);
egdaniel574a4c12015-11-02 06:22:44 -0800105 if (!shaderId) {
106 return false;
107 }
108
109 *shaderIds->append() = shaderId;
egdaniel574a4c12015-11-02 06:22:44 -0800110 return true;
111}
112
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400113void GrGLProgramBuilder::computeCountsAndStrides(GrGLuint programID,
114 const GrPrimitiveProcessor& primProc,
115 bool bindAttribLocations) {
116 fVertexAttributeCnt = primProc.numVertexAttributes();
117 fInstanceAttributeCnt = primProc.numInstanceAttributes();
118 fAttributes.reset(
119 new GrGLProgram::Attribute[fVertexAttributeCnt + fInstanceAttributeCnt]);
120 auto addAttr = [&](int i, const auto& a, size_t* stride) {
Brian Osman4a3f5c82018-09-18 16:16:38 -0400121 fAttributes[i].fCPUType = a.cpuType();
122 fAttributes[i].fGPUType = a.gpuType();
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400123 fAttributes[i].fOffset = *stride;
124 *stride += a.sizeAlign4();
125 fAttributes[i].fLocation = i;
126 if (bindAttribLocations) {
127 GL_CALL(BindAttribLocation(programID, i, a.name()));
128 }
129 };
130 fVertexStride = 0;
131 int i = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500132 for (const auto& attr : primProc.vertexAttributes()) {
133 addAttr(i++, attr, &fVertexStride);
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400134 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500135 SkASSERT(fVertexStride == primProc.vertexStride());
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400136 fInstanceStride = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500137 for (const auto& attr : primProc.instanceAttributes()) {
138 addAttr(i++, attr, &fInstanceStride);
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400139 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500140 SkASSERT(fInstanceStride == primProc.instanceStride());
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400141}
142
Ethan Nicholascd700e92018-08-24 16:43:57 -0400143void GrGLProgramBuilder::addInputVars(const SkSL::Program::Inputs& inputs) {
144 if (inputs.fRTWidth) {
145 this->addRTWidthUniform(SKSL_RTWIDTH_NAME);
146 }
147 if (inputs.fRTHeight) {
148 this->addRTHeightUniform(SKSL_RTHEIGHT_NAME);
149 }
150}
151
Brian Osmana085a412019-04-25 09:44:43 -0400152static constexpr SkFourByteTag kSKSL_Tag = SkSetFourByteTag('S', 'K', 'S', 'L');
153static constexpr SkFourByteTag kGLSL_Tag = SkSetFourByteTag('G', 'L', 'S', 'L');
Brian Osmana66081d2019-09-03 14:59:26 -0400154static constexpr SkFourByteTag kGLPB_Tag = SkSetFourByteTag('G', 'L', 'P', 'B');
Brian Osmana085a412019-04-25 09:44:43 -0400155
Ethan Nicholas8d058832019-01-07 10:49:58 -0500156void GrGLProgramBuilder::storeShaderInCache(const SkSL::Program::Inputs& inputs, GrGLuint programID,
Brian Osmaned58e002019-09-06 14:42:43 -0400157 const SkSL::String shaders[], bool isSkSL,
Brian Osman4524e842019-09-24 16:03:41 -0400158 SkSL::Program::Settings* settings) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500159 if (!this->gpu()->getContext()->priv().getPersistentCache()) {
Ethan Nicholas8d058832019-01-07 10:49:58 -0500160 return;
161 }
Robert Phillips901aff02019-10-08 12:32:56 -0400162 sk_sp<SkData> key = SkData::MakeWithoutCopy(this->desc()->asKey(), this->desc()->keyLength());
Ethan Nicholas8d058832019-01-07 10:49:58 -0500163 if (fGpu->glCaps().programBinarySupport()) {
164 // binary cache
165 GrGLsizei length = 0;
166 GL_CALL(GetProgramiv(programID, GL_PROGRAM_BINARY_LENGTH, &length));
167 if (length > 0) {
Brian Osman6b797fe2019-04-08 13:56:36 -0400168 SkWriter32 writer;
Brian Osmana66081d2019-09-03 14:59:26 -0400169 writer.write32(kGLPB_Tag);
170
Brian Osman6b797fe2019-04-08 13:56:36 -0400171 writer.writePad(&inputs, sizeof(inputs));
172 writer.write32(length);
173
174 void* binary = writer.reservePad(length);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500175 GrGLenum binaryFormat;
Brian Osman6b797fe2019-04-08 13:56:36 -0400176 GL_CALL(GetProgramBinary(programID, length, &length, &binaryFormat, binary));
177 writer.write32(binaryFormat);
178
179 auto data = writer.snapshotAsData();
180 this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500181 }
182 } else {
Brian Osman4524e842019-09-24 16:03:41 -0400183 // source cache, plus metadata to allow for a complete precompile
184 GrPersistentCacheUtils::ShaderMetadata meta;
185 meta.fSettings = settings;
186 meta.fHasCustomColorOutput = fFS.hasCustomColorOutput();
187 meta.fHasSecondaryColorOutput = fFS.hasSecondaryOutput();
188 for (const auto& attr : this->primitiveProcessor().vertexAttributes()) {
189 meta.fAttributeNames.emplace_back(attr.name());
190 }
191 for (const auto& attr : this->primitiveProcessor().instanceAttributes()) {
192 meta.fAttributeNames.emplace_back(attr.name());
193 }
194
Brian Osmana085a412019-04-25 09:44:43 -0400195 auto data = GrPersistentCacheUtils::PackCachedShaders(isSkSL ? kSKSL_Tag : kGLSL_Tag,
Brian Osman4524e842019-09-24 16:03:41 -0400196 shaders, &inputs, 1, &meta);
Brian Osman6b797fe2019-04-08 13:56:36 -0400197 this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500198 }
199}
200
Brian Osmaned58e002019-09-06 14:42:43 -0400201GrGLProgram* GrGLProgramBuilder::finalize(const GrGLPrecompiledProgram* precompiledProgram) {
Brian Salomon5f394272019-07-02 14:07:49 -0400202 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -0700203
joshualitt47bb3822014-10-07 16:43:25 -0700204 // verify we can get a program id
205 GrGLuint programID;
Brian Osmaned58e002019-09-06 14:42:43 -0400206 if (precompiledProgram) {
207 programID = precompiledProgram->fProgramID;
208 } else {
209 GL_CALL_RET(programID, CreateProgram());
210 }
joshualitt47bb3822014-10-07 16:43:25 -0700211 if (0 == programID) {
halcanary96fcdcc2015-08-27 07:41:13 -0700212 return nullptr;
joshualitt30ba4362014-08-21 20:18:45 -0700213 }
214
Ethan Nicholas06d55fb2017-11-08 09:48:50 -0500215 if (this->gpu()->glCaps().programBinarySupport() &&
Brian Osman064729e2019-06-18 17:22:59 -0400216 this->gpu()->glCaps().programParameterSupport() &&
Brian Osmaned58e002019-09-06 14:42:43 -0400217 this->gpu()->getContext()->priv().getPersistentCache() &&
218 !precompiledProgram) {
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400219 GL_CALL(ProgramParameteri(programID, GR_GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GR_GL_TRUE));
220 }
221
egdaniel9f1d4152016-02-10 09:50:38 -0800222 this->finalizeShaders();
223
joshualitt47bb3822014-10-07 16:43:25 -0700224 // compile shaders and bind attributes / uniforms
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400225 auto errorHandler = this->gpu()->getContext()->priv().getShaderErrorHandler();
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400226 const GrPrimitiveProcessor& primProc = this->primitiveProcessor();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500227 SkSL::Program::Settings settings;
228 settings.fCaps = this->gpu()->glCaps().shaderCaps();
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500229 settings.fFlipY = this->origin() != kTopLeft_GrSurfaceOrigin;
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500230 settings.fSharpenTextures =
Robert Phillips9da87e02019-02-04 13:26:26 -0500231 this->gpu()->getContext()->priv().options().fSharpenMipmappedTextures;
Brian Salomondc092132018-04-04 10:14:16 -0400232 settings.fFragColorIsInOut = this->fragColorIsInOut();
233
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500234 SkSL::Program::Inputs inputs;
joshualitt30ba4362014-08-21 20:18:45 -0700235 SkTDArray<GrGLuint> shadersToDelete;
Ethan Nicholas5a0338c2019-01-02 11:48:56 -0500236 // Calling GetProgramiv is expensive in Chromium. Assume success in release builds.
237 bool checkLinked = kChromium_GrGLDriver != fGpu->ctxInfo().driver();
238#ifdef SK_DEBUG
239 checkLinked = true;
240#endif
Ethan Nicholas8d058832019-01-07 10:49:58 -0500241 bool cached = fCached.get() != nullptr;
Brian Osman2c60a382019-06-26 15:19:30 -0400242 bool usedProgramBinaries = false;
Brian Osman6b797fe2019-04-08 13:56:36 -0400243 SkSL::String glsl[kGrShaderTypeCount];
Brian Osmancbc33b82019-04-19 14:16:19 -0400244 SkSL::String* sksl[kGrShaderTypeCount] = {
245 &fVS.fCompilerString,
246 &fGS.fCompilerString,
247 &fFS.fCompilerString,
248 };
Brian Osmancbc33b82019-04-19 14:16:19 -0400249 SkSL::String cached_sksl[kGrShaderTypeCount];
Brian Osmaned58e002019-09-06 14:42:43 -0400250 if (precompiledProgram) {
251 // This is very similar to when we get program binaries. We even set that flag, as it's
252 // used to prevent other compile work later, and to force re-querying uniform locations.
253 this->addInputVars(precompiledProgram->fInputs);
Brian Osman4524e842019-09-24 16:03:41 -0400254 this->computeCountsAndStrides(programID, primProc, false);
Brian Osmaned58e002019-09-06 14:42:43 -0400255 usedProgramBinaries = true;
256 } else if (cached) {
Brian Osmana66081d2019-09-03 14:59:26 -0400257 SkReader32 reader(fCached->data(), fCached->size());
258 SkFourByteTag shaderType = reader.readU32();
259
260 switch (shaderType) {
261 case kGLPB_Tag: {
262 // Program binary cache hit. We may opt not to use this if we don't trust program
263 // binaries on this driver
264 if (!fGpu->glCaps().programBinarySupport()) {
265 cached = false;
266 break;
Ethan Nicholas8d058832019-01-07 10:49:58 -0500267 }
Brian Osmana66081d2019-09-03 14:59:26 -0400268 reader.read(&inputs, sizeof(inputs));
269 GrGLsizei length = reader.readInt();
270 const void* binary = reader.skip(length);
271 GrGLenum binaryFormat = reader.readU32();
272 GrGLClearErr(this->gpu()->glInterface());
273 GR_GL_CALL_NOERRCHECK(this->gpu()->glInterface(),
274 ProgramBinary(programID, binaryFormat,
275 const_cast<void*>(binary), length));
276 if (GR_GL_GET_ERROR(this->gpu()->glInterface()) == GR_GL_NO_ERROR) {
277 if (checkLinked) {
278 cached = this->checkLinkStatus(programID, errorHandler, nullptr, nullptr);
279 }
280 if (cached) {
281 this->addInputVars(inputs);
282 this->computeCountsAndStrides(programID, primProc, false);
283 }
284 } else {
285 cached = false;
Ethan Nicholas8d058832019-01-07 10:49:58 -0500286 }
Brian Osmana66081d2019-09-03 14:59:26 -0400287 usedProgramBinaries = cached;
288 break;
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400289 }
Brian Osmana66081d2019-09-03 14:59:26 -0400290
291 case kGLSL_Tag:
292 // Source cache hit, we don't need to compile the SkSL->GLSL
293 GrPersistentCacheUtils::UnpackCachedShaders(&reader, glsl, &inputs, 1);
294 break;
295
296 case kSKSL_Tag:
297 // SkSL cache hit, this should only happen in tools overriding the generated SkSL
298 GrPersistentCacheUtils::UnpackCachedShaders(&reader, cached_sksl, &inputs, 1);
Brian Osmana085a412019-04-25 09:44:43 -0400299 for (int i = 0; i < kGrShaderTypeCount; ++i) {
300 sksl[i] = &cached_sksl[i];
301 }
Brian Osmana66081d2019-09-03 14:59:26 -0400302 break;
Ethan Nicholas98ad5b72018-03-13 09:53:02 -0400303 }
304 }
Brian Osmana66081d2019-09-03 14:59:26 -0400305 if (!usedProgramBinaries) {
Brian Osmanf7924452019-09-24 10:44:37 -0400306 // Either a cache miss, or we got something other than binaries from the cache
307
308 /*
309 Fragment Shader
310 */
Brian Osman6b797fe2019-04-08 13:56:36 -0400311 if (glsl[kFragment_GrShaderType].empty()) {
Brian Osmanf71b0702019-04-03 13:04:16 -0400312 // Don't have cached GLSL, need to compile SkSL->GLSL
Ethan Nicholas8d058832019-01-07 10:49:58 -0500313 if (fFS.fForceHighPrecision) {
314 settings.fForceHighPrecision = true;
315 }
316 std::unique_ptr<SkSL::Program> fs = GrSkSLtoGLSL(gpu()->glContext(),
Brian Osmanac9be9d2019-05-01 10:29:34 -0400317 SkSL::Program::kFragment_Kind,
Brian Osmancbc33b82019-04-19 14:16:19 -0400318 *sksl[kFragment_GrShaderType],
Ethan Nicholas8d058832019-01-07 10:49:58 -0500319 settings,
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400320 &glsl[kFragment_GrShaderType],
321 errorHandler);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500322 if (!fs) {
Brian Osmaned58e002019-09-06 14:42:43 -0400323 cleanup_program(fGpu, programID, shadersToDelete);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500324 return nullptr;
325 }
326 inputs = fs->fInputs;
egdaniel8dcdedc2015-11-11 06:27:20 -0800327 }
Brian Osmanf7924452019-09-24 10:44:37 -0400328
329 this->addInputVars(inputs);
Brian Osmanac9be9d2019-05-01 10:29:34 -0400330 if (!this->compileAndAttachShaders(glsl[kFragment_GrShaderType], programID,
Brian Osmane11dfd32019-07-23 10:29:41 -0400331 GR_GL_FRAGMENT_SHADER, &shadersToDelete, errorHandler)) {
Brian Osmaned58e002019-09-06 14:42:43 -0400332 cleanup_program(fGpu, programID, shadersToDelete);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400333 return nullptr;
334 }
335
Brian Osmanf7924452019-09-24 10:44:37 -0400336 /*
337 Vertex Shader
338 */
Brian Osman6b797fe2019-04-08 13:56:36 -0400339 if (glsl[kVertex_GrShaderType].empty()) {
Brian Osmanf71b0702019-04-03 13:04:16 -0400340 // Don't have cached GLSL, need to compile SkSL->GLSL
341 std::unique_ptr<SkSL::Program> vs = GrSkSLtoGLSL(gpu()->glContext(),
Brian Osmanac9be9d2019-05-01 10:29:34 -0400342 SkSL::Program::kVertex_Kind,
Brian Osmancbc33b82019-04-19 14:16:19 -0400343 *sksl[kVertex_GrShaderType],
Brian Osmanf71b0702019-04-03 13:04:16 -0400344 settings,
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400345 &glsl[kVertex_GrShaderType],
346 errorHandler);
Brian Osmanf71b0702019-04-03 13:04:16 -0400347 if (!vs) {
Brian Osmaned58e002019-09-06 14:42:43 -0400348 cleanup_program(fGpu, programID, shadersToDelete);
Brian Osmanf71b0702019-04-03 13:04:16 -0400349 return nullptr;
350 }
351 }
Brian Osmanac9be9d2019-05-01 10:29:34 -0400352 if (!this->compileAndAttachShaders(glsl[kVertex_GrShaderType], programID,
Brian Osmane11dfd32019-07-23 10:29:41 -0400353 GR_GL_VERTEX_SHADER, &shadersToDelete, errorHandler)) {
Brian Osmaned58e002019-09-06 14:42:43 -0400354 cleanup_program(fGpu, programID, shadersToDelete);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400355 return nullptr;
356 }
357
Brian Osmanf7924452019-09-24 10:44:37 -0400358 // This also binds vertex attribute locations. NVPR doesn't really use vertices,
359 // even though it requires a vertex shader in the program.
360 if (!primProc.isPathRendering()) {
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400361 this->computeCountsAndStrides(programID, primProc, true);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400362 }
363
Brian Osmanf7924452019-09-24 10:44:37 -0400364 /*
Chris Dalton5a2f9622019-12-27 14:56:38 -0700365 Tessellation Shaders
366 */
367 if (fProgramInfo.primProc().willUseTessellationShaders()) {
368 // Tessellation shaders are not currently supported by SkSL. So here, we temporarily
369 // generate GLSL strings directly using back door methods on GrPrimitiveProcessor, and
370 // pass those raw strings on to the driver.
371 SkString versionAndExtensionDecls;
372 versionAndExtensionDecls.appendf("%s\n", this->shaderCaps()->versionDeclString());
373 if (const char* extensionString = this->shaderCaps()->tessellationExtensionString()) {
374 versionAndExtensionDecls.appendf("#extension %s : require\n", extensionString);
375 }
376
377 SkString tessControlShader = primProc.getTessControlShaderGLSL(
378 versionAndExtensionDecls.c_str(), *this->shaderCaps());
379 if (!this->compileAndAttachShaders(tessControlShader.c_str(), programID,
380 GR_GL_TESS_CONTROL_SHADER, &shadersToDelete,
381 errorHandler)) {
382 cleanup_program(fGpu, programID, shadersToDelete);
383 return nullptr;
384 }
385
386 SkString tessEvaluationShader = primProc.getTessEvaluationShaderGLSL(
387 versionAndExtensionDecls.c_str(), *this->shaderCaps());
388 if (!this->compileAndAttachShaders(tessEvaluationShader.c_str(), programID,
389 GR_GL_TESS_EVALUATION_SHADER, &shadersToDelete,
390 errorHandler)) {
391 cleanup_program(fGpu, programID, shadersToDelete);
392 return nullptr;
393 }
394 }
395
396 /*
Brian Osmanf7924452019-09-24 10:44:37 -0400397 Geometry Shader
398 */
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400399 if (primProc.willUseGeoShader()) {
Brian Osman6b797fe2019-04-08 13:56:36 -0400400 if (glsl[kGeometry_GrShaderType].empty()) {
Brian Osmanf71b0702019-04-03 13:04:16 -0400401 // Don't have cached GLSL, need to compile SkSL->GLSL
402 std::unique_ptr<SkSL::Program> gs;
403 gs = GrSkSLtoGLSL(gpu()->glContext(),
Brian Osmanac9be9d2019-05-01 10:29:34 -0400404 SkSL::Program::kGeometry_Kind,
Brian Osmancbc33b82019-04-19 14:16:19 -0400405 *sksl[kGeometry_GrShaderType],
Brian Osmanf71b0702019-04-03 13:04:16 -0400406 settings,
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400407 &glsl[kGeometry_GrShaderType],
408 errorHandler);
Brian Osmanf71b0702019-04-03 13:04:16 -0400409 if (!gs) {
Brian Osmaned58e002019-09-06 14:42:43 -0400410 cleanup_program(fGpu, programID, shadersToDelete);
Brian Osmanf71b0702019-04-03 13:04:16 -0400411 return nullptr;
412 }
413 }
Brian Osmanac9be9d2019-05-01 10:29:34 -0400414 if (!this->compileAndAttachShaders(glsl[kGeometry_GrShaderType], programID,
Brian Osmane11dfd32019-07-23 10:29:41 -0400415 GR_GL_GEOMETRY_SHADER, &shadersToDelete,
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400416 errorHandler)) {
Brian Osmaned58e002019-09-06 14:42:43 -0400417 cleanup_program(fGpu, programID, shadersToDelete);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400418 return nullptr;
419 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400420 }
421 this->bindProgramResourceLocations(programID);
422
423 GL_CALL(LinkProgram(programID));
Ethan Nicholas5a0338c2019-01-02 11:48:56 -0500424 if (checkLinked) {
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400425 if (!this->checkLinkStatus(programID, errorHandler, sksl, glsl)) {
Brian Osman2ab2ac02019-09-09 13:54:54 -0400426 cleanup_program(fGpu, programID, shadersToDelete);
Ethan Nicholas5a0338c2019-01-02 11:48:56 -0500427 return nullptr;
Brian Salomone334c592017-05-15 11:00:58 -0400428 }
Brian Salomone334c592017-05-15 11:00:58 -0400429 }
joshualittfe1233c2014-10-07 12:16:35 -0700430 }
Brian Osman2c60a382019-06-26 15:19:30 -0400431 this->resolveProgramResourceLocations(programID, usedProgramBinaries);
joshualittdb0d3ca2014-10-07 12:42:26 -0700432
Brian Osmaned58e002019-09-06 14:42:43 -0400433 cleanup_shaders(fGpu, shadersToDelete);
Brian Osmane4c88bb2019-06-27 16:15:11 -0400434
435 // With ANGLE, we can't cache path-rendering programs. We use ProgramPathFragmentInputGen,
436 // and ANGLE's deserialized program state doesn't restore enough state to handle that.
437 // The native NVIDIA drivers do, but this is such an edge case that it's easier to just
438 // black-list caching these programs in all cases. See: anglebug.com/3619
Chris Dalton5a2f9622019-12-27 14:56:38 -0700439 //
440 // We temporarily can't cache tessellation shaders while using back door GLSL.
441 //
Brian Osmaned58e002019-09-06 14:42:43 -0400442 // We also can't cache SkSL or GLSL if we were given a precompiled program, but there's not
443 // much point in doing so.
Chris Dalton5a2f9622019-12-27 14:56:38 -0700444 if (!cached && !primProc.isPathRendering() && !primProc.willUseTessellationShaders() &&
445 !precompiledProgram) {
446 static_assert(&GrPrimitiveProcessor::getTessControlShaderGLSL,
447 "FIXME: Remove the check for tessellation shaders in the above 'if' once the "
448 "back door GLSL mechanism is removed.");
Brian Osmana085a412019-04-25 09:44:43 -0400449 bool isSkSL = false;
Brian Osmana66081d2019-09-03 14:59:26 -0400450 if (fGpu->getContext()->priv().options().fShaderCacheStrategy ==
451 GrContextOptions::ShaderCacheStrategy::kSkSL) {
Brian Osmancbc33b82019-04-19 14:16:19 -0400452 for (int i = 0; i < kGrShaderTypeCount; ++i) {
Brian Osmanac9be9d2019-05-01 10:29:34 -0400453 glsl[i] = GrShaderUtils::PrettyPrint(*sksl[i]);
Brian Osmancbc33b82019-04-19 14:16:19 -0400454 }
Brian Osmana085a412019-04-25 09:44:43 -0400455 isSkSL = true;
Brian Osmancbc33b82019-04-19 14:16:19 -0400456 }
Brian Osman4524e842019-09-24 16:03:41 -0400457 this->storeShaderInCache(inputs, programID, glsl, isSkSL, &settings);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400458 }
joshualitt47bb3822014-10-07 16:43:25 -0700459 return this->createProgram(programID);
460}
461
kkinnunen7aedda52015-06-29 23:01:28 -0700462void GrGLProgramBuilder::bindProgramResourceLocations(GrGLuint programID) {
egdaniel7ea439b2015-12-03 09:20:44 -0800463 fUniformHandler.bindUniformLocations(programID, fGpu->glCaps());
kkinnunen7aedda52015-06-29 23:01:28 -0700464
egdaniel8dcdedc2015-11-11 06:27:20 -0800465 const GrGLCaps& caps = this->gpu()->glCaps();
466 if (fFS.hasCustomColorOutput() && caps.bindFragDataLocationSupport()) {
467 GL_CALL(BindFragDataLocation(programID, 0,
egdaniel2d721d32015-11-11 13:06:05 -0800468 GrGLSLFragmentShaderBuilder::DeclaredColorOutputName()));
egdaniel8dcdedc2015-11-11 06:27:20 -0800469 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500470 if (fFS.hasSecondaryOutput() && caps.shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdaniel8dcdedc2015-11-11 06:27:20 -0800471 GL_CALL(BindFragDataLocationIndexed(programID, 0, 1,
egdaniel2d721d32015-11-11 13:06:05 -0800472 GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName()));
egdaniel8dcdedc2015-11-11 06:27:20 -0800473 }
joshualittd8dd47b2015-09-11 11:45:01 -0700474
475 // handle NVPR separable varyings
476 if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() ||
477 !fGpu->glPathRendering()->shouldBindFragmentInputs()) {
478 return;
479 }
egdaniel0eafe792015-11-20 14:01:22 -0800480 int count = fVaryingHandler.fPathProcVaryingInfos.count();
joshualittd8dd47b2015-09-11 11:45:01 -0700481 for (int i = 0; i < count; ++i) {
egdaniel0eafe792015-11-20 14:01:22 -0800482 GL_CALL(BindFragmentInputLocation(programID, i,
483 fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str()));
484 fVaryingHandler.fPathProcVaryingInfos[i].fLocation = i;
joshualittd8dd47b2015-09-11 11:45:01 -0700485 }
joshualitt47bb3822014-10-07 16:43:25 -0700486}
487
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400488bool GrGLProgramBuilder::checkLinkStatus(GrGLuint programID,
489 GrContextOptions::ShaderErrorHandler* errorHandler,
490 SkSL::String* sksl[], const SkSL::String glsl[]) {
joshualitt47bb3822014-10-07 16:43:25 -0700491 GrGLint linked = GR_GL_INIT_ZERO;
492 GL_CALL(GetProgramiv(programID, GR_GL_LINK_STATUS, &linked));
493 if (!linked) {
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400494 SkSL::String allShaders;
495 if (sksl) {
496 allShaders.appendf("// Vertex SKSL\n%s\n", sksl[kVertex_GrShaderType]->c_str());
497 if (!sksl[kGeometry_GrShaderType]->empty()) {
498 allShaders.appendf("// Geometry SKSL\n%s\n", sksl[kGeometry_GrShaderType]->c_str());
499 }
500 allShaders.appendf("// Fragment SKSL\n%s\n", sksl[kFragment_GrShaderType]->c_str());
501 }
502 if (glsl) {
503 allShaders.appendf("// Vertex GLSL\n%s\n", glsl[kVertex_GrShaderType].c_str());
504 if (!glsl[kGeometry_GrShaderType].empty()) {
505 allShaders.appendf("// Geometry GLSL\n%s\n", glsl[kGeometry_GrShaderType].c_str());
506 }
507 allShaders.appendf("// Fragment GLSL\n%s\n", glsl[kFragment_GrShaderType].c_str());
508 }
joshualitt47bb3822014-10-07 16:43:25 -0700509 GrGLint infoLen = GR_GL_INIT_ZERO;
510 GL_CALL(GetProgramiv(programID, GR_GL_INFO_LOG_LENGTH, &infoLen));
511 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
512 if (infoLen > 0) {
513 // retrieve length even though we don't need it to workaround
514 // bug in chrome cmd buffer param validation.
515 GrGLsizei length = GR_GL_INIT_ZERO;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400516 GL_CALL(GetProgramInfoLog(programID, infoLen+1, &length, (char*)log.get()));
joshualitt47bb3822014-10-07 16:43:25 -0700517 }
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400518 errorHandler->compileError(allShaders.c_str(), infoLen > 0 ? (const char*)log.get() : "");
joshualitt47bb3822014-10-07 16:43:25 -0700519 }
520 return SkToBool(linked);
521}
522
Brian Osman2c60a382019-06-26 15:19:30 -0400523void GrGLProgramBuilder::resolveProgramResourceLocations(GrGLuint programID, bool force) {
524 fUniformHandler.getUniformLocations(programID, fGpu->glCaps(), force);
joshualittd8dd47b2015-09-11 11:45:01 -0700525
526 // handle NVPR separable varyings
527 if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() ||
kkinnunen7bdb05d2016-01-25 00:47:23 -0800528 fGpu->glPathRendering()->shouldBindFragmentInputs()) {
joshualittd8dd47b2015-09-11 11:45:01 -0700529 return;
530 }
egdaniel0eafe792015-11-20 14:01:22 -0800531 int count = fVaryingHandler.fPathProcVaryingInfos.count();
joshualittd8dd47b2015-09-11 11:45:01 -0700532 for (int i = 0; i < count; ++i) {
533 GrGLint location;
egdaniel0eafe792015-11-20 14:01:22 -0800534 GL_CALL_RET(location, GetProgramResourceLocation(
535 programID,
536 GR_GL_FRAGMENT_INPUT,
537 fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str()));
538 fVaryingHandler.fPathProcVaryingInfos[i].fLocation = location;
joshualittd8dd47b2015-09-11 11:45:01 -0700539 }
joshualitt30ba4362014-08-21 20:18:45 -0700540}
541
joshualitt47bb3822014-10-07 16:43:25 -0700542GrGLProgram* GrGLProgramBuilder::createProgram(GrGLuint programID) {
egdaniel7ea439b2015-12-03 09:20:44 -0800543 return new GrGLProgram(fGpu,
egdaniel7ea439b2015-12-03 09:20:44 -0800544 fUniformHandles,
545 programID,
546 fUniformHandler.fUniforms,
egdaniel09aa1fc2016-04-20 07:09:46 -0700547 fUniformHandler.fSamplers,
egdaniel0eafe792015-11-20 14:01:22 -0800548 fVaryingHandler.fPathProcVaryingInfos,
Robert Phillips369e8b72017-08-01 16:13:04 -0400549 std::move(fGeometryProcessor),
550 std::move(fXferProcessor),
Brian Salomon4d3f5172018-06-07 14:42:52 -0400551 std::move(fFragmentProcessors),
Brian Salomon802cb312018-06-08 18:05:20 -0400552 fFragmentProcessorCnt,
553 std::move(fAttributes),
Brian Salomon92be2f72018-06-19 14:33:47 -0400554 fVertexAttributeCnt,
555 fInstanceAttributeCnt,
Brian Salomon802cb312018-06-08 18:05:20 -0400556 fVertexStride,
557 fInstanceStride);
joshualitt47bb3822014-10-07 16:43:25 -0700558}
Brian Osmaned58e002019-09-06 14:42:43 -0400559
560bool GrGLProgramBuilder::PrecompileProgram(GrGLPrecompiledProgram* precompiledProgram,
561 GrGLGpu* gpu,
562 const SkData& cachedData) {
563 SkReader32 reader(cachedData.data(), cachedData.size());
564 SkFourByteTag shaderType = reader.readU32();
565 if (shaderType != kSKSL_Tag) {
566 // TODO: Support GLSL, and maybe even program binaries, too?
567 return false;
568 }
569
570 const GrGLInterface* gl = gpu->glInterface();
571 auto errorHandler = gpu->getContext()->priv().getShaderErrorHandler();
572 GrGLuint programID;
573 GR_GL_CALL_RET(gl, programID, CreateProgram());
574 if (0 == programID) {
575 return false;
576 }
577
578 SkTDArray<GrGLuint> shadersToDelete;
579
580 SkSL::Program::Settings settings;
Brian Osman4524e842019-09-24 16:03:41 -0400581 const GrGLCaps& caps = gpu->glCaps();
582 settings.fCaps = caps.shaderCaps();
Brian Osmaned58e002019-09-06 14:42:43 -0400583 settings.fSharpenTextures = gpu->getContext()->priv().options().fSharpenMipmappedTextures;
Brian Osman4524e842019-09-24 16:03:41 -0400584 GrPersistentCacheUtils::ShaderMetadata meta;
585 meta.fSettings = &settings;
Brian Osmaned58e002019-09-06 14:42:43 -0400586
587 SkSL::String shaders[kGrShaderTypeCount];
588 SkSL::Program::Inputs inputs;
Brian Osman4524e842019-09-24 16:03:41 -0400589 GrPersistentCacheUtils::UnpackCachedShaders(&reader, shaders, &inputs, 1, &meta);
Brian Osmaned58e002019-09-06 14:42:43 -0400590
591 auto compileShader = [&](SkSL::Program::Kind kind, const SkSL::String& sksl, GrGLenum type) {
592 SkSL::String glsl;
593 auto program = GrSkSLtoGLSL(gpu->glContext(), kind, sksl, settings, &glsl, errorHandler);
594 if (!program) {
595 return false;
596 }
597
598 if (GrGLuint shaderID = GrGLCompileAndAttachShader(gpu->glContext(), programID, type, glsl,
599 gpu->stats(), errorHandler)) {
600 shadersToDelete.push_back(shaderID);
601 return true;
602 } else {
603 return false;
604 }
605 };
606
607 if (!compileShader(SkSL::Program::kFragment_Kind,
608 shaders[kFragment_GrShaderType],
609 GR_GL_FRAGMENT_SHADER) ||
610 !compileShader(SkSL::Program::kVertex_Kind,
611 shaders[kVertex_GrShaderType],
612 GR_GL_VERTEX_SHADER) ||
613 (!shaders[kGeometry_GrShaderType].empty() &&
614 !compileShader(SkSL::Program::kGeometry_Kind,
615 shaders[kGeometry_GrShaderType],
616 GR_GL_GEOMETRY_SHADER))) {
617 cleanup_program(gpu, programID, shadersToDelete);
618 return false;
619 }
620
Brian Osman4524e842019-09-24 16:03:41 -0400621 for (int i = 0; i < meta.fAttributeNames.count(); ++i) {
622 GR_GL_CALL(gpu->glInterface(), BindAttribLocation(programID, i,
623 meta.fAttributeNames[i].c_str()));
624 }
625
626 if (meta.fHasCustomColorOutput && caps.bindFragDataLocationSupport()) {
627 GR_GL_CALL(gpu->glInterface(), BindFragDataLocation(programID, 0,
628 GrGLSLFragmentShaderBuilder::DeclaredColorOutputName()));
629 }
630 if (meta.fHasSecondaryColorOutput && caps.shaderCaps()->mustDeclareFragmentShaderOutput()) {
631 GR_GL_CALL(gpu->glInterface(), BindFragDataLocationIndexed(programID, 0, 1,
632 GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName()));
633 }
634
Brian Osmaned58e002019-09-06 14:42:43 -0400635 GR_GL_CALL(gpu->glInterface(), LinkProgram(programID));
636 GrGLint linked = GR_GL_INIT_ZERO;
637 GR_GL_CALL(gpu->glInterface(), GetProgramiv(programID, GR_GL_LINK_STATUS, &linked));
638 if (!linked) {
639 cleanup_program(gpu, programID, shadersToDelete);
640 return false;
641 }
642
643 cleanup_shaders(gpu, shadersToDelete);
644
645 precompiledProgram->fProgramID = programID;
646 precompiledProgram->fInputs = inputs;
647 return true;
648}