blob: b08eda3eda3abc49c9ccb2eee4ea40ee976883ca [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
Robert Phillipsd0fe8752019-01-31 14:13:59 -050036GrGLProgram* GrGLProgramBuilder::CreateProgram(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
37 const GrPrimitiveProcessor& primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -050038 const GrTextureProxy* const primProcProxies[],
Brian Salomonff168d92018-06-23 15:17:27 -040039 const GrPipeline& pipeline,
Ethan Nicholas38657112017-02-09 17:01:22 -050040 GrProgramDesc* desc,
egdaniel0e1853c2016-03-17 11:35:45 -070041 GrGLGpu* gpu) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000042 SkASSERT(!pipeline.isBad());
Robert Phillipsa91e0b72017-05-01 13:12:20 -040043
Derek Sollenberger488f0d62017-03-03 15:48:33 -050044 ATRACE_ANDROID_FRAMEWORK("Shader Compile");
bsalomon3318ee72015-03-16 11:56:29 -070045 GrAutoLocaleSetter als("C");
46
joshualitt47bb3822014-10-07 16:43:25 -070047 // create a builder. This will be handed off to effects so they can use it to add
48 // uniforms, varyings, textures, etc
Robert Phillipsd0fe8752019-01-31 14:13:59 -050049 GrGLProgramBuilder builder(gpu, renderTarget, origin,
50 pipeline, primProc, primProcProxies, desc);
joshualitt47bb3822014-10-07 16:43:25 -070051
Robert Phillips9da87e02019-02-04 13:26:26 -050052 auto persistentCache = gpu->getContext()->priv().getPersistentCache();
Ethan Nicholas8d058832019-01-07 10:49:58 -050053 if (persistentCache) {
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040054 sk_sp<SkData> key = SkData::MakeWithoutCopy(desc->asKey(), desc->keyLength());
Robert Phillips0c4b7b12018-03-06 08:20:37 -050055 builder.fCached = persistentCache->load(*key);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040056 // the eventual end goal is to completely skip emitAndInstallProcs on a cache hit, but it's
57 // doing necessary setup in addition to generating the SkSL code. Currently we are only able
58 // to skip the SkSL->GLSL step on a cache hit.
59 }
Ethan Nicholas2983f402017-05-08 09:36:08 -040060 if (!builder.emitAndInstallProcs()) {
halcanary96fcdcc2015-08-27 07:41:13 -070061 return nullptr;
joshualitt6c891102015-05-13 08:51:49 -070062 }
egdanielc1e71012016-01-20 07:53:51 -080063 return builder.finalize();
joshualitt47bb3822014-10-07 16:43:25 -070064}
65
joshualitt47bb3822014-10-07 16:43:25 -070066/////////////////////////////////////////////////////////////////////////////
67
egdaniel0e1853c2016-03-17 11:35:45 -070068GrGLProgramBuilder::GrGLProgramBuilder(GrGLGpu* gpu,
Robert Phillipsd0fe8752019-01-31 14:13:59 -050069 GrRenderTarget* renderTarget,
70 GrSurfaceOrigin origin,
egdaniel0e1853c2016-03-17 11:35:45 -070071 const GrPipeline& pipeline,
72 const GrPrimitiveProcessor& primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -050073 const GrTextureProxy* const primProcProxies[],
Ethan Nicholas38657112017-02-09 17:01:22 -050074 GrProgramDesc* desc)
Robert Phillipsd0fe8752019-01-31 14:13:59 -050075 : INHERITED(renderTarget, origin, primProc, primProcProxies, pipeline, desc)
Brian Salomon802cb312018-06-08 18:05:20 -040076 , fGpu(gpu)
77 , fVaryingHandler(this)
78 , fUniformHandler(this)
Brian Salomon92be2f72018-06-19 14:33:47 -040079 , fVertexAttributeCnt(0)
80 , fInstanceAttributeCnt(0)
Brian Salomon802cb312018-06-08 18:05:20 -040081 , fVertexStride(0)
82 , fInstanceStride(0) {}
joshualitt30ba4362014-08-21 20:18:45 -070083
egdanielfa896322016-01-13 12:19:30 -080084const GrCaps* GrGLProgramBuilder::caps() const {
85 return fGpu->caps();
86}
87
Brian Osmanac9be9d2019-05-01 10:29:34 -040088bool GrGLProgramBuilder::compileAndAttachShaders(const SkSL::String& glsl,
egdaniel574a4c12015-11-02 06:22:44 -080089 GrGLuint programId,
90 GrGLenum type,
Ethan Nicholas941e7e22016-12-12 15:33:30 -050091 SkTDArray<GrGLuint>* shaderIds,
Brian Osman5e7fbfd2019-05-03 13:13:35 -040092 GrContextOptions::ShaderErrorHandler* errHandler) {
egdaniel574a4c12015-11-02 06:22:44 -080093 GrGLGpu* gpu = this->gpu();
94 GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(),
95 programId,
96 type,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040097 glsl,
Brian Osman8518f2e2019-05-01 14:13:41 -040098 gpu->stats(),
Brian Osman5e7fbfd2019-05-03 13:13:35 -040099 errHandler);
egdaniel574a4c12015-11-02 06:22:44 -0800100 if (!shaderId) {
101 return false;
102 }
103
104 *shaderIds->append() = shaderId;
egdaniel574a4c12015-11-02 06:22:44 -0800105 return true;
106}
107
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400108void GrGLProgramBuilder::computeCountsAndStrides(GrGLuint programID,
109 const GrPrimitiveProcessor& primProc,
110 bool bindAttribLocations) {
111 fVertexAttributeCnt = primProc.numVertexAttributes();
112 fInstanceAttributeCnt = primProc.numInstanceAttributes();
113 fAttributes.reset(
114 new GrGLProgram::Attribute[fVertexAttributeCnt + fInstanceAttributeCnt]);
115 auto addAttr = [&](int i, const auto& a, size_t* stride) {
Brian Osman4a3f5c82018-09-18 16:16:38 -0400116 fAttributes[i].fCPUType = a.cpuType();
117 fAttributes[i].fGPUType = a.gpuType();
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400118 fAttributes[i].fOffset = *stride;
119 *stride += a.sizeAlign4();
120 fAttributes[i].fLocation = i;
121 if (bindAttribLocations) {
122 GL_CALL(BindAttribLocation(programID, i, a.name()));
123 }
124 };
125 fVertexStride = 0;
126 int i = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500127 for (const auto& attr : primProc.vertexAttributes()) {
128 addAttr(i++, attr, &fVertexStride);
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400129 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500130 SkASSERT(fVertexStride == primProc.vertexStride());
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400131 fInstanceStride = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500132 for (const auto& attr : primProc.instanceAttributes()) {
133 addAttr(i++, attr, &fInstanceStride);
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400134 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500135 SkASSERT(fInstanceStride == primProc.instanceStride());
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400136}
137
Ethan Nicholascd700e92018-08-24 16:43:57 -0400138void GrGLProgramBuilder::addInputVars(const SkSL::Program::Inputs& inputs) {
139 if (inputs.fRTWidth) {
140 this->addRTWidthUniform(SKSL_RTWIDTH_NAME);
141 }
142 if (inputs.fRTHeight) {
143 this->addRTHeightUniform(SKSL_RTHEIGHT_NAME);
144 }
145}
146
Brian Osmana085a412019-04-25 09:44:43 -0400147static constexpr SkFourByteTag kSKSL_Tag = SkSetFourByteTag('S', 'K', 'S', 'L');
148static constexpr SkFourByteTag kGLSL_Tag = SkSetFourByteTag('G', 'L', 'S', 'L');
Brian Osmana66081d2019-09-03 14:59:26 -0400149static constexpr SkFourByteTag kGLPB_Tag = SkSetFourByteTag('G', 'L', 'P', 'B');
Brian Osmana085a412019-04-25 09:44:43 -0400150
Ethan Nicholas8d058832019-01-07 10:49:58 -0500151void GrGLProgramBuilder::storeShaderInCache(const SkSL::Program::Inputs& inputs, GrGLuint programID,
Brian Osmana085a412019-04-25 09:44:43 -0400152 const SkSL::String shaders[], bool isSkSL) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500153 if (!this->gpu()->getContext()->priv().getPersistentCache()) {
Ethan Nicholas8d058832019-01-07 10:49:58 -0500154 return;
155 }
156 sk_sp<SkData> key = SkData::MakeWithoutCopy(desc()->asKey(), desc()->keyLength());
157 if (fGpu->glCaps().programBinarySupport()) {
158 // binary cache
159 GrGLsizei length = 0;
160 GL_CALL(GetProgramiv(programID, GL_PROGRAM_BINARY_LENGTH, &length));
161 if (length > 0) {
Brian Osman6b797fe2019-04-08 13:56:36 -0400162 SkWriter32 writer;
Brian Osmana66081d2019-09-03 14:59:26 -0400163 writer.write32(kGLPB_Tag);
164
Brian Osman6b797fe2019-04-08 13:56:36 -0400165 writer.writePad(&inputs, sizeof(inputs));
166 writer.write32(length);
167
168 void* binary = writer.reservePad(length);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500169 GrGLenum binaryFormat;
Brian Osman6b797fe2019-04-08 13:56:36 -0400170 GL_CALL(GetProgramBinary(programID, length, &length, &binaryFormat, binary));
171 writer.write32(binaryFormat);
172
173 auto data = writer.snapshotAsData();
174 this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500175 }
176 } else {
177 // source cache
Brian Osmana085a412019-04-25 09:44:43 -0400178 auto data = GrPersistentCacheUtils::PackCachedShaders(isSkSL ? kSKSL_Tag : kGLSL_Tag,
179 shaders, &inputs, 1);
Brian Osman6b797fe2019-04-08 13:56:36 -0400180 this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500181 }
182}
183
joshualitt47bb3822014-10-07 16:43:25 -0700184GrGLProgram* GrGLProgramBuilder::finalize() {
Brian Salomon5f394272019-07-02 14:07:49 -0400185 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -0700186
joshualitt47bb3822014-10-07 16:43:25 -0700187 // verify we can get a program id
188 GrGLuint programID;
189 GL_CALL_RET(programID, CreateProgram());
190 if (0 == programID) {
halcanary96fcdcc2015-08-27 07:41:13 -0700191 return nullptr;
joshualitt30ba4362014-08-21 20:18:45 -0700192 }
193
Ethan Nicholas06d55fb2017-11-08 09:48:50 -0500194 if (this->gpu()->glCaps().programBinarySupport() &&
Brian Osman064729e2019-06-18 17:22:59 -0400195 this->gpu()->glCaps().programParameterSupport() &&
Robert Phillips9da87e02019-02-04 13:26:26 -0500196 this->gpu()->getContext()->priv().getPersistentCache()) {
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400197 GL_CALL(ProgramParameteri(programID, GR_GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GR_GL_TRUE));
198 }
199
egdaniel9f1d4152016-02-10 09:50:38 -0800200 this->finalizeShaders();
201
joshualitt47bb3822014-10-07 16:43:25 -0700202 // compile shaders and bind attributes / uniforms
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400203 auto errorHandler = this->gpu()->getContext()->priv().getShaderErrorHandler();
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400204 const GrPrimitiveProcessor& primProc = this->primitiveProcessor();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500205 SkSL::Program::Settings settings;
206 settings.fCaps = this->gpu()->glCaps().shaderCaps();
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500207 settings.fFlipY = this->origin() != kTopLeft_GrSurfaceOrigin;
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500208 settings.fSharpenTextures =
Robert Phillips9da87e02019-02-04 13:26:26 -0500209 this->gpu()->getContext()->priv().options().fSharpenMipmappedTextures;
Brian Salomondc092132018-04-04 10:14:16 -0400210 settings.fFragColorIsInOut = this->fragColorIsInOut();
211
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500212 SkSL::Program::Inputs inputs;
joshualitt30ba4362014-08-21 20:18:45 -0700213 SkTDArray<GrGLuint> shadersToDelete;
Ethan Nicholas5a0338c2019-01-02 11:48:56 -0500214 // Calling GetProgramiv is expensive in Chromium. Assume success in release builds.
215 bool checkLinked = kChromium_GrGLDriver != fGpu->ctxInfo().driver();
216#ifdef SK_DEBUG
217 checkLinked = true;
218#endif
Ethan Nicholas8d058832019-01-07 10:49:58 -0500219 bool cached = fCached.get() != nullptr;
Brian Osman2c60a382019-06-26 15:19:30 -0400220 bool usedProgramBinaries = false;
Brian Osman6b797fe2019-04-08 13:56:36 -0400221 SkSL::String glsl[kGrShaderTypeCount];
Brian Osmancbc33b82019-04-19 14:16:19 -0400222 SkSL::String* sksl[kGrShaderTypeCount] = {
223 &fVS.fCompilerString,
224 &fGS.fCompilerString,
225 &fFS.fCompilerString,
226 };
Brian Osmancbc33b82019-04-19 14:16:19 -0400227 SkSL::String cached_sksl[kGrShaderTypeCount];
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400228 if (cached) {
Brian Osmana66081d2019-09-03 14:59:26 -0400229 SkReader32 reader(fCached->data(), fCached->size());
230 SkFourByteTag shaderType = reader.readU32();
231
232 switch (shaderType) {
233 case kGLPB_Tag: {
234 // Program binary cache hit. We may opt not to use this if we don't trust program
235 // binaries on this driver
236 if (!fGpu->glCaps().programBinarySupport()) {
237 cached = false;
238 break;
Ethan Nicholas8d058832019-01-07 10:49:58 -0500239 }
Brian Osmana66081d2019-09-03 14:59:26 -0400240 reader.read(&inputs, sizeof(inputs));
241 GrGLsizei length = reader.readInt();
242 const void* binary = reader.skip(length);
243 GrGLenum binaryFormat = reader.readU32();
244 GrGLClearErr(this->gpu()->glInterface());
245 GR_GL_CALL_NOERRCHECK(this->gpu()->glInterface(),
246 ProgramBinary(programID, binaryFormat,
247 const_cast<void*>(binary), length));
248 if (GR_GL_GET_ERROR(this->gpu()->glInterface()) == GR_GL_NO_ERROR) {
249 if (checkLinked) {
250 cached = this->checkLinkStatus(programID, errorHandler, nullptr, nullptr);
251 }
252 if (cached) {
253 this->addInputVars(inputs);
254 this->computeCountsAndStrides(programID, primProc, false);
255 }
256 } else {
257 cached = false;
Ethan Nicholas8d058832019-01-07 10:49:58 -0500258 }
Brian Osmana66081d2019-09-03 14:59:26 -0400259 usedProgramBinaries = cached;
260 break;
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400261 }
Brian Osmana66081d2019-09-03 14:59:26 -0400262
263 case kGLSL_Tag:
264 // Source cache hit, we don't need to compile the SkSL->GLSL
265 GrPersistentCacheUtils::UnpackCachedShaders(&reader, glsl, &inputs, 1);
266 break;
267
268 case kSKSL_Tag:
269 // SkSL cache hit, this should only happen in tools overriding the generated SkSL
270 GrPersistentCacheUtils::UnpackCachedShaders(&reader, cached_sksl, &inputs, 1);
Brian Osmana085a412019-04-25 09:44:43 -0400271 for (int i = 0; i < kGrShaderTypeCount; ++i) {
272 sksl[i] = &cached_sksl[i];
273 }
Brian Osmana66081d2019-09-03 14:59:26 -0400274 break;
Ethan Nicholas98ad5b72018-03-13 09:53:02 -0400275 }
276 }
Brian Osmana66081d2019-09-03 14:59:26 -0400277 if (!usedProgramBinaries) {
278 // either a cache miss, or we got something other than binaries from the cache
Brian Osman6b797fe2019-04-08 13:56:36 -0400279 if (glsl[kFragment_GrShaderType].empty()) {
Brian Osmanf71b0702019-04-03 13:04:16 -0400280 // Don't have cached GLSL, need to compile SkSL->GLSL
Ethan Nicholas8d058832019-01-07 10:49:58 -0500281 if (fFS.fForceHighPrecision) {
282 settings.fForceHighPrecision = true;
283 }
284 std::unique_ptr<SkSL::Program> fs = GrSkSLtoGLSL(gpu()->glContext(),
Brian Osmanac9be9d2019-05-01 10:29:34 -0400285 SkSL::Program::kFragment_Kind,
Brian Osmancbc33b82019-04-19 14:16:19 -0400286 *sksl[kFragment_GrShaderType],
Ethan Nicholas8d058832019-01-07 10:49:58 -0500287 settings,
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400288 &glsl[kFragment_GrShaderType],
289 errorHandler);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500290 if (!fs) {
291 this->cleanupProgram(programID, shadersToDelete);
292 return nullptr;
293 }
294 inputs = fs->fInputs;
Brian Osmanf71b0702019-04-03 13:04:16 -0400295 this->addInputVars(inputs);
Ethan Nicholas8d058832019-01-07 10:49:58 -0500296 } else {
297 // we've pulled GLSL and inputs from the cache, but still need to do some setup
298 this->addInputVars(inputs);
299 this->computeCountsAndStrides(programID, primProc, false);
egdaniel8dcdedc2015-11-11 06:27:20 -0800300 }
Brian Osmanac9be9d2019-05-01 10:29:34 -0400301 if (!this->compileAndAttachShaders(glsl[kFragment_GrShaderType], programID,
Brian Osmane11dfd32019-07-23 10:29:41 -0400302 GR_GL_FRAGMENT_SHADER, &shadersToDelete, errorHandler)) {
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400303 this->cleanupProgram(programID, shadersToDelete);
304 return nullptr;
305 }
306
Brian Osman6b797fe2019-04-08 13:56:36 -0400307 if (glsl[kVertex_GrShaderType].empty()) {
Brian Osmanf71b0702019-04-03 13:04:16 -0400308 // Don't have cached GLSL, need to compile SkSL->GLSL
309 std::unique_ptr<SkSL::Program> vs = GrSkSLtoGLSL(gpu()->glContext(),
Brian Osmanac9be9d2019-05-01 10:29:34 -0400310 SkSL::Program::kVertex_Kind,
Brian Osmancbc33b82019-04-19 14:16:19 -0400311 *sksl[kVertex_GrShaderType],
Brian Osmanf71b0702019-04-03 13:04:16 -0400312 settings,
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400313 &glsl[kVertex_GrShaderType],
314 errorHandler);
Brian Osmanf71b0702019-04-03 13:04:16 -0400315 if (!vs) {
316 this->cleanupProgram(programID, shadersToDelete);
317 return nullptr;
318 }
319 }
Brian Osmanac9be9d2019-05-01 10:29:34 -0400320 if (!this->compileAndAttachShaders(glsl[kVertex_GrShaderType], programID,
Brian Osmane11dfd32019-07-23 10:29:41 -0400321 GR_GL_VERTEX_SHADER, &shadersToDelete, errorHandler)) {
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400322 this->cleanupProgram(programID, shadersToDelete);
323 return nullptr;
324 }
325
326 // NVPR actually requires a vertex shader to compile
327 bool useNvpr = primProc.isPathRendering();
328 if (!useNvpr) {
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400329 this->computeCountsAndStrides(programID, primProc, true);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400330 }
331
332 if (primProc.willUseGeoShader()) {
Brian Osman6b797fe2019-04-08 13:56:36 -0400333 if (glsl[kGeometry_GrShaderType].empty()) {
Brian Osmanf71b0702019-04-03 13:04:16 -0400334 // Don't have cached GLSL, need to compile SkSL->GLSL
335 std::unique_ptr<SkSL::Program> gs;
336 gs = GrSkSLtoGLSL(gpu()->glContext(),
Brian Osmanac9be9d2019-05-01 10:29:34 -0400337 SkSL::Program::kGeometry_Kind,
Brian Osmancbc33b82019-04-19 14:16:19 -0400338 *sksl[kGeometry_GrShaderType],
Brian Osmanf71b0702019-04-03 13:04:16 -0400339 settings,
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400340 &glsl[kGeometry_GrShaderType],
341 errorHandler);
Brian Osmanf71b0702019-04-03 13:04:16 -0400342 if (!gs) {
343 this->cleanupProgram(programID, shadersToDelete);
344 return nullptr;
345 }
346 }
Brian Osmanac9be9d2019-05-01 10:29:34 -0400347 if (!this->compileAndAttachShaders(glsl[kGeometry_GrShaderType], programID,
Brian Osmane11dfd32019-07-23 10:29:41 -0400348 GR_GL_GEOMETRY_SHADER, &shadersToDelete,
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400349 errorHandler)) {
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400350 this->cleanupProgram(programID, shadersToDelete);
351 return nullptr;
352 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400353 }
354 this->bindProgramResourceLocations(programID);
355
356 GL_CALL(LinkProgram(programID));
Ethan Nicholas5a0338c2019-01-02 11:48:56 -0500357 if (checkLinked) {
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400358 if (!this->checkLinkStatus(programID, errorHandler, sksl, glsl)) {
Ethan Nicholas5a0338c2019-01-02 11:48:56 -0500359 GL_CALL(DeleteProgram(programID));
Ethan Nicholas5a0338c2019-01-02 11:48:56 -0500360 return nullptr;
Brian Salomone334c592017-05-15 11:00:58 -0400361 }
Brian Salomone334c592017-05-15 11:00:58 -0400362 }
joshualittfe1233c2014-10-07 12:16:35 -0700363 }
Brian Osman2c60a382019-06-26 15:19:30 -0400364 this->resolveProgramResourceLocations(programID, usedProgramBinaries);
joshualittdb0d3ca2014-10-07 12:42:26 -0700365
joshualitt47bb3822014-10-07 16:43:25 -0700366 this->cleanupShaders(shadersToDelete);
Brian Osmane4c88bb2019-06-27 16:15:11 -0400367
368 // With ANGLE, we can't cache path-rendering programs. We use ProgramPathFragmentInputGen,
369 // and ANGLE's deserialized program state doesn't restore enough state to handle that.
370 // The native NVIDIA drivers do, but this is such an edge case that it's easier to just
371 // black-list caching these programs in all cases. See: anglebug.com/3619
372 if (!cached && !primProc.isPathRendering()) {
Brian Osmana085a412019-04-25 09:44:43 -0400373 bool isSkSL = false;
Brian Osmana66081d2019-09-03 14:59:26 -0400374 if (fGpu->getContext()->priv().options().fShaderCacheStrategy ==
375 GrContextOptions::ShaderCacheStrategy::kSkSL) {
Brian Osmancbc33b82019-04-19 14:16:19 -0400376 for (int i = 0; i < kGrShaderTypeCount; ++i) {
Brian Osmanac9be9d2019-05-01 10:29:34 -0400377 glsl[i] = GrShaderUtils::PrettyPrint(*sksl[i]);
Brian Osmancbc33b82019-04-19 14:16:19 -0400378 }
Brian Osmana085a412019-04-25 09:44:43 -0400379 isSkSL = true;
Brian Osmancbc33b82019-04-19 14:16:19 -0400380 }
Brian Osmana085a412019-04-25 09:44:43 -0400381 this->storeShaderInCache(inputs, programID, glsl, isSkSL);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400382 }
joshualitt47bb3822014-10-07 16:43:25 -0700383 return this->createProgram(programID);
384}
385
kkinnunen7aedda52015-06-29 23:01:28 -0700386void GrGLProgramBuilder::bindProgramResourceLocations(GrGLuint programID) {
egdaniel7ea439b2015-12-03 09:20:44 -0800387 fUniformHandler.bindUniformLocations(programID, fGpu->glCaps());
kkinnunen7aedda52015-06-29 23:01:28 -0700388
egdaniel8dcdedc2015-11-11 06:27:20 -0800389 const GrGLCaps& caps = this->gpu()->glCaps();
390 if (fFS.hasCustomColorOutput() && caps.bindFragDataLocationSupport()) {
391 GL_CALL(BindFragDataLocation(programID, 0,
egdaniel2d721d32015-11-11 13:06:05 -0800392 GrGLSLFragmentShaderBuilder::DeclaredColorOutputName()));
egdaniel8dcdedc2015-11-11 06:27:20 -0800393 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500394 if (fFS.hasSecondaryOutput() && caps.shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdaniel8dcdedc2015-11-11 06:27:20 -0800395 GL_CALL(BindFragDataLocationIndexed(programID, 0, 1,
egdaniel2d721d32015-11-11 13:06:05 -0800396 GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName()));
egdaniel8dcdedc2015-11-11 06:27:20 -0800397 }
joshualittd8dd47b2015-09-11 11:45:01 -0700398
399 // handle NVPR separable varyings
400 if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() ||
401 !fGpu->glPathRendering()->shouldBindFragmentInputs()) {
402 return;
403 }
egdaniel0eafe792015-11-20 14:01:22 -0800404 int count = fVaryingHandler.fPathProcVaryingInfos.count();
joshualittd8dd47b2015-09-11 11:45:01 -0700405 for (int i = 0; i < count; ++i) {
egdaniel0eafe792015-11-20 14:01:22 -0800406 GL_CALL(BindFragmentInputLocation(programID, i,
407 fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str()));
408 fVaryingHandler.fPathProcVaryingInfos[i].fLocation = i;
joshualittd8dd47b2015-09-11 11:45:01 -0700409 }
joshualitt47bb3822014-10-07 16:43:25 -0700410}
411
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400412bool GrGLProgramBuilder::checkLinkStatus(GrGLuint programID,
413 GrContextOptions::ShaderErrorHandler* errorHandler,
414 SkSL::String* sksl[], const SkSL::String glsl[]) {
joshualitt47bb3822014-10-07 16:43:25 -0700415 GrGLint linked = GR_GL_INIT_ZERO;
416 GL_CALL(GetProgramiv(programID, GR_GL_LINK_STATUS, &linked));
417 if (!linked) {
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400418 SkSL::String allShaders;
419 if (sksl) {
420 allShaders.appendf("// Vertex SKSL\n%s\n", sksl[kVertex_GrShaderType]->c_str());
421 if (!sksl[kGeometry_GrShaderType]->empty()) {
422 allShaders.appendf("// Geometry SKSL\n%s\n", sksl[kGeometry_GrShaderType]->c_str());
423 }
424 allShaders.appendf("// Fragment SKSL\n%s\n", sksl[kFragment_GrShaderType]->c_str());
425 }
426 if (glsl) {
427 allShaders.appendf("// Vertex GLSL\n%s\n", glsl[kVertex_GrShaderType].c_str());
428 if (!glsl[kGeometry_GrShaderType].empty()) {
429 allShaders.appendf("// Geometry GLSL\n%s\n", glsl[kGeometry_GrShaderType].c_str());
430 }
431 allShaders.appendf("// Fragment GLSL\n%s\n", glsl[kFragment_GrShaderType].c_str());
432 }
joshualitt47bb3822014-10-07 16:43:25 -0700433 GrGLint infoLen = GR_GL_INIT_ZERO;
434 GL_CALL(GetProgramiv(programID, GR_GL_INFO_LOG_LENGTH, &infoLen));
435 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
436 if (infoLen > 0) {
437 // retrieve length even though we don't need it to workaround
438 // bug in chrome cmd buffer param validation.
439 GrGLsizei length = GR_GL_INIT_ZERO;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400440 GL_CALL(GetProgramInfoLog(programID, infoLen+1, &length, (char*)log.get()));
joshualitt47bb3822014-10-07 16:43:25 -0700441 }
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400442 errorHandler->compileError(allShaders.c_str(), infoLen > 0 ? (const char*)log.get() : "");
joshualitt47bb3822014-10-07 16:43:25 -0700443 }
444 return SkToBool(linked);
445}
446
Brian Osman2c60a382019-06-26 15:19:30 -0400447void GrGLProgramBuilder::resolveProgramResourceLocations(GrGLuint programID, bool force) {
448 fUniformHandler.getUniformLocations(programID, fGpu->glCaps(), force);
joshualittd8dd47b2015-09-11 11:45:01 -0700449
450 // handle NVPR separable varyings
451 if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() ||
kkinnunen7bdb05d2016-01-25 00:47:23 -0800452 fGpu->glPathRendering()->shouldBindFragmentInputs()) {
joshualittd8dd47b2015-09-11 11:45:01 -0700453 return;
454 }
egdaniel0eafe792015-11-20 14:01:22 -0800455 int count = fVaryingHandler.fPathProcVaryingInfos.count();
joshualittd8dd47b2015-09-11 11:45:01 -0700456 for (int i = 0; i < count; ++i) {
457 GrGLint location;
egdaniel0eafe792015-11-20 14:01:22 -0800458 GL_CALL_RET(location, GetProgramResourceLocation(
459 programID,
460 GR_GL_FRAGMENT_INPUT,
461 fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str()));
462 fVaryingHandler.fPathProcVaryingInfos[i].fLocation = location;
joshualittd8dd47b2015-09-11 11:45:01 -0700463 }
joshualitt30ba4362014-08-21 20:18:45 -0700464}
465
joshualitt47bb3822014-10-07 16:43:25 -0700466void GrGLProgramBuilder::cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs) {
467 GL_CALL(DeleteProgram(programID));
egdanielfa896322016-01-13 12:19:30 -0800468 this->cleanupShaders(shaderIDs);
joshualitt47bb3822014-10-07 16:43:25 -0700469}
470void GrGLProgramBuilder::cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs) {
471 for (int i = 0; i < shaderIDs.count(); ++i) {
Brian Salomon802cb312018-06-08 18:05:20 -0400472 GL_CALL(DeleteShader(shaderIDs[i]));
joshualitt47bb3822014-10-07 16:43:25 -0700473 }
474}
475
476GrGLProgram* GrGLProgramBuilder::createProgram(GrGLuint programID) {
egdaniel7ea439b2015-12-03 09:20:44 -0800477 return new GrGLProgram(fGpu,
egdaniel7ea439b2015-12-03 09:20:44 -0800478 fUniformHandles,
479 programID,
480 fUniformHandler.fUniforms,
egdaniel09aa1fc2016-04-20 07:09:46 -0700481 fUniformHandler.fSamplers,
egdaniel0eafe792015-11-20 14:01:22 -0800482 fVaryingHandler.fPathProcVaryingInfos,
Robert Phillips369e8b72017-08-01 16:13:04 -0400483 std::move(fGeometryProcessor),
484 std::move(fXferProcessor),
Brian Salomon4d3f5172018-06-07 14:42:52 -0400485 std::move(fFragmentProcessors),
Brian Salomon802cb312018-06-08 18:05:20 -0400486 fFragmentProcessorCnt,
487 std::move(fAttributes),
Brian Salomon92be2f72018-06-19 14:33:47 -0400488 fVertexAttributeCnt,
489 fInstanceAttributeCnt,
Brian Salomon802cb312018-06-08 18:05:20 -0400490 fVertexStride,
491 fInstanceStride);
joshualitt47bb3822014-10-07 16:43:25 -0700492}