blob: 387886d47e32807c63ce4058d38f248291797d5c [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
joshualitt47bb3822014-10-07 16:43:25 -07008#include "GrGLProgramBuilder.h"
joshualitt8072caa2015-02-12 14:20:52 -08009
bsalomon6f7f2012015-03-16 14:00:52 -070010#include "GrAutoLocaleSetter.h"
Robert Phillips9bee2e52017-05-29 12:37:20 -040011#include "GrContext.h"
Robert Phillips6be756b2018-01-16 15:07:54 -050012#include "GrContextPriv.h"
joshualitt47bb3822014-10-07 16:43:25 -070013#include "GrCoordTransform.h"
joshualitt30ba4362014-08-21 20:18:45 -070014#include "GrGLProgramBuilder.h"
egdaniel5d8f69f2016-09-07 07:24:12 -070015#include "GrProgramDesc.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050016#include "GrShaderCaps.h"
bsalomon7f9b2e42016-01-12 13:29:26 -080017#include "GrSwizzle.h"
Hal Canary95e3c052017-01-11 12:44:43 -050018#include "SkAutoMalloc.h"
Derek Sollenberger488f0d62017-03-03 15:48:33 -050019#include "SkATrace.h"
joshualitt30ba4362014-08-21 20:18:45 -070020#include "SkTraceEvent.h"
joshualitte7afc2d2015-09-11 10:44:13 -070021#include "gl/GrGLGpu.h"
joshualitte7afc2d2015-09-11 10:44:13 -070022#include "gl/GrGLProgram.h"
egdaniel574a4c12015-11-02 06:22:44 -080023#include "gl/builders/GrGLShaderStringBuilder.h"
egdaniel64c47282015-11-13 06:54:19 -080024#include "glsl/GrGLSLFragmentProcessor.h"
egdaniele659a582015-11-13 09:55:43 -080025#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070026#include "glsl/GrGLSLProgramDataManager.h"
egdanielfa4cc8b2015-11-13 08:34:52 -080027#include "glsl/GrGLSLXferProcessor.h"
joshualitt30ba4362014-08-21 20:18:45 -070028
joshualitt30ba4362014-08-21 20:18:45 -070029#define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X)
30#define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X)
31
Brian Salomonff168d92018-06-23 15:17:27 -040032GrGLProgram* GrGLProgramBuilder::CreateProgram(const GrPrimitiveProcessor& primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -050033 const GrTextureProxy* const primProcProxies[],
Brian Salomonff168d92018-06-23 15:17:27 -040034 const GrPipeline& pipeline,
Ethan Nicholas38657112017-02-09 17:01:22 -050035 GrProgramDesc* desc,
egdaniel0e1853c2016-03-17 11:35:45 -070036 GrGLGpu* gpu) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000037 SkASSERT(!pipeline.isBad());
Robert Phillipsa91e0b72017-05-01 13:12:20 -040038
Derek Sollenberger488f0d62017-03-03 15:48:33 -050039 ATRACE_ANDROID_FRAMEWORK("Shader Compile");
bsalomon3318ee72015-03-16 11:56:29 -070040 GrAutoLocaleSetter als("C");
41
joshualitt47bb3822014-10-07 16:43:25 -070042 // create a builder. This will be handed off to effects so they can use it to add
43 // uniforms, varyings, textures, etc
Greg Daniel9a51a862018-11-30 10:18:14 -050044 GrGLProgramBuilder builder(gpu, pipeline, primProc, primProcProxies, desc);
joshualitt47bb3822014-10-07 16:43:25 -070045
Robert Phillips0c4b7b12018-03-06 08:20:37 -050046 auto persistentCache = gpu->getContext()->contextPriv().getPersistentCache();
Ethan Nicholas255f73a2018-12-12 20:54:32 +000047 if (persistentCache && gpu->glCaps().programBinarySupport()) {
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040048 sk_sp<SkData> key = SkData::MakeWithoutCopy(desc->asKey(), desc->keyLength());
Robert Phillips0c4b7b12018-03-06 08:20:37 -050049 builder.fCached = persistentCache->load(*key);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040050 // the eventual end goal is to completely skip emitAndInstallProcs on a cache hit, but it's
51 // doing necessary setup in addition to generating the SkSL code. Currently we are only able
52 // to skip the SkSL->GLSL step on a cache hit.
53 }
Ethan Nicholas2983f402017-05-08 09:36:08 -040054 if (!builder.emitAndInstallProcs()) {
halcanary96fcdcc2015-08-27 07:41:13 -070055 return nullptr;
joshualitt6c891102015-05-13 08:51:49 -070056 }
egdanielc1e71012016-01-20 07:53:51 -080057 return builder.finalize();
joshualitt47bb3822014-10-07 16:43:25 -070058}
59
joshualitt47bb3822014-10-07 16:43:25 -070060/////////////////////////////////////////////////////////////////////////////
61
egdaniel0e1853c2016-03-17 11:35:45 -070062GrGLProgramBuilder::GrGLProgramBuilder(GrGLGpu* gpu,
63 const GrPipeline& pipeline,
64 const GrPrimitiveProcessor& primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -050065 const GrTextureProxy* const primProcProxies[],
Ethan Nicholas38657112017-02-09 17:01:22 -050066 GrProgramDesc* desc)
Greg Daniel9a51a862018-11-30 10:18:14 -050067 : INHERITED(primProc, primProcProxies, pipeline, desc)
Brian Salomon802cb312018-06-08 18:05:20 -040068 , fGpu(gpu)
69 , fVaryingHandler(this)
70 , fUniformHandler(this)
Brian Salomon92be2f72018-06-19 14:33:47 -040071 , fVertexAttributeCnt(0)
72 , fInstanceAttributeCnt(0)
Brian Salomon802cb312018-06-08 18:05:20 -040073 , fVertexStride(0)
74 , fInstanceStride(0) {}
joshualitt30ba4362014-08-21 20:18:45 -070075
egdanielfa896322016-01-13 12:19:30 -080076const GrCaps* GrGLProgramBuilder::caps() const {
77 return fGpu->caps();
78}
79
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040080bool GrGLProgramBuilder::compileAndAttachShaders(const char* glsl,
81 int length,
egdaniel574a4c12015-11-02 06:22:44 -080082 GrGLuint programId,
83 GrGLenum type,
Ethan Nicholas941e7e22016-12-12 15:33:30 -050084 SkTDArray<GrGLuint>* shaderIds,
85 const SkSL::Program::Settings& settings,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040086 const SkSL::Program::Inputs& inputs) {
egdaniel574a4c12015-11-02 06:22:44 -080087 GrGLGpu* gpu = this->gpu();
88 GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(),
89 programId,
90 type,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040091 glsl,
92 length,
Ethan Nicholas941e7e22016-12-12 15:33:30 -050093 gpu->stats(),
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040094 settings);
egdaniel574a4c12015-11-02 06:22:44 -080095 if (!shaderId) {
96 return false;
97 }
98
99 *shaderIds->append() = shaderId;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400100 if (inputs.fFlipY) {
Ethan Nicholas38657112017-02-09 17:01:22 -0500101 GrProgramDesc* d = this->desc();
Robert Phillips7f861922018-01-30 13:13:42 +0000102 d->setSurfaceOriginKey(GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(
103 this->pipeline().proxy()->origin()));
Ethan Nicholas38657112017-02-09 17:01:22 -0500104 d->finalize();
105 }
egdaniel574a4c12015-11-02 06:22:44 -0800106
107 return true;
108}
109
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400110bool GrGLProgramBuilder::compileAndAttachShaders(GrGLSLShaderBuilder& shader,
111 GrGLuint programId,
112 GrGLenum type,
113 SkTDArray<GrGLuint>* shaderIds,
114 const SkSL::Program::Settings& settings,
115 SkSL::Program::Inputs* outInputs) {
116 SkSL::String glsl;
117 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(gpu()->glContext(), type,
118 shader.fCompilerStrings.begin(),
119 shader.fCompilerStringLengths.begin(),
120 shader.fCompilerStrings.count(),
121 settings,
122 &glsl);
123 *outInputs = program->fInputs;
124 return this->compileAndAttachShaders(glsl.c_str(),
125 glsl.size(),
126 programId,
127 type,
128 shaderIds,
129 settings,
130 *outInputs);
131}
132
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400133void GrGLProgramBuilder::computeCountsAndStrides(GrGLuint programID,
134 const GrPrimitiveProcessor& primProc,
135 bool bindAttribLocations) {
136 fVertexAttributeCnt = primProc.numVertexAttributes();
137 fInstanceAttributeCnt = primProc.numInstanceAttributes();
138 fAttributes.reset(
139 new GrGLProgram::Attribute[fVertexAttributeCnt + fInstanceAttributeCnt]);
140 auto addAttr = [&](int i, const auto& a, size_t* stride) {
Brian Osman4a3f5c82018-09-18 16:16:38 -0400141 fAttributes[i].fCPUType = a.cpuType();
142 fAttributes[i].fGPUType = a.gpuType();
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400143 fAttributes[i].fOffset = *stride;
144 *stride += a.sizeAlign4();
145 fAttributes[i].fLocation = i;
146 if (bindAttribLocations) {
147 GL_CALL(BindAttribLocation(programID, i, a.name()));
148 }
149 };
150 fVertexStride = 0;
151 int i = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500152 for (const auto& attr : primProc.vertexAttributes()) {
153 addAttr(i++, attr, &fVertexStride);
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400154 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500155 SkASSERT(fVertexStride == primProc.vertexStride());
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400156 fInstanceStride = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500157 for (const auto& attr : primProc.instanceAttributes()) {
158 addAttr(i++, attr, &fInstanceStride);
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400159 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500160 SkASSERT(fInstanceStride == primProc.instanceStride());
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400161}
162
Ethan Nicholascd700e92018-08-24 16:43:57 -0400163void GrGLProgramBuilder::addInputVars(const SkSL::Program::Inputs& inputs) {
164 if (inputs.fRTWidth) {
165 this->addRTWidthUniform(SKSL_RTWIDTH_NAME);
166 }
167 if (inputs.fRTHeight) {
168 this->addRTHeightUniform(SKSL_RTHEIGHT_NAME);
169 }
170}
171
joshualitt47bb3822014-10-07 16:43:25 -0700172GrGLProgram* GrGLProgramBuilder::finalize() {
Brian Osman39c08ac2017-07-26 09:36:09 -0400173 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -0700174
joshualitt47bb3822014-10-07 16:43:25 -0700175 // verify we can get a program id
176 GrGLuint programID;
177 GL_CALL_RET(programID, CreateProgram());
178 if (0 == programID) {
halcanary96fcdcc2015-08-27 07:41:13 -0700179 return nullptr;
joshualitt30ba4362014-08-21 20:18:45 -0700180 }
181
Ethan Nicholas06d55fb2017-11-08 09:48:50 -0500182 if (this->gpu()->glCaps().programBinarySupport() &&
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500183 this->gpu()->getContext()->contextPriv().getPersistentCache()) {
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400184 GL_CALL(ProgramParameteri(programID, GR_GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GR_GL_TRUE));
185 }
186
egdaniel9f1d4152016-02-10 09:50:38 -0800187 this->finalizeShaders();
188
joshualitt47bb3822014-10-07 16:43:25 -0700189 // compile shaders and bind attributes / uniforms
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400190 const GrPrimitiveProcessor& primProc = this->primitiveProcessor();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500191 SkSL::Program::Settings settings;
192 settings.fCaps = this->gpu()->glCaps().shaderCaps();
Robert Phillips2890fbf2017-07-26 15:48:41 -0400193 settings.fFlipY = this->pipeline().proxy()->origin() != kTopLeft_GrSurfaceOrigin;
Brian Osman8a83ca42018-02-12 14:32:17 -0500194 settings.fSharpenTextures = this->gpu()->getContext()->contextPriv().sharpenMipmappedTextures();
Brian Salomondc092132018-04-04 10:14:16 -0400195 settings.fFragColorIsInOut = this->fragColorIsInOut();
196
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500197 SkSL::Program::Inputs inputs;
joshualitt30ba4362014-08-21 20:18:45 -0700198 SkTDArray<GrGLuint> shadersToDelete;
Ethan Nicholas5a0338c2019-01-02 11:48:56 -0500199 // Calling GetProgramiv is expensive in Chromium. Assume success in release builds.
200 bool checkLinked = kChromium_GrGLDriver != fGpu->ctxInfo().driver();
201#ifdef SK_DEBUG
202 checkLinked = true;
203#endif
Ethan Nicholas255f73a2018-12-12 20:54:32 +0000204 bool cached = fGpu->glCaps().programBinarySupport() && nullptr != fCached.get();
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400205 if (cached) {
Ethan Nicholas255f73a2018-12-12 20:54:32 +0000206 this->bindProgramResourceLocations(programID);
207 // cache hit, just hand the binary to GL
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400208 const uint8_t* bytes = fCached->bytes();
209 size_t offset = 0;
210 memcpy(&inputs, bytes + offset, sizeof(inputs));
211 offset += sizeof(inputs);
Ethan Nicholas255f73a2018-12-12 20:54:32 +0000212 int binaryFormat;
213 memcpy(&binaryFormat, bytes + offset, sizeof(binaryFormat));
214 offset += sizeof(binaryFormat);
215 GrGLClearErr(this->gpu()->glInterface());
216 GR_GL_CALL_NOERRCHECK(this->gpu()->glInterface(),
217 ProgramBinary(programID, binaryFormat, (void*) (bytes + offset),
218 fCached->size() - offset));
219 if (GR_GL_GET_ERROR(this->gpu()->glInterface()) == GR_GL_NO_ERROR) {
Ethan Nicholas5a0338c2019-01-02 11:48:56 -0500220 if (checkLinked) {
221 cached = this->checkLinkStatus(programID);
222 }
Ethan Nicholas255f73a2018-12-12 20:54:32 +0000223 if (cached) {
224 this->addInputVars(inputs);
225 this->computeCountsAndStrides(programID, primProc, false);
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400226 }
Ethan Nicholas98ad5b72018-03-13 09:53:02 -0400227 } else {
Ethan Nicholas255f73a2018-12-12 20:54:32 +0000228 cached = false;
Ethan Nicholas98ad5b72018-03-13 09:53:02 -0400229 }
230 }
Ethan Nicholas255f73a2018-12-12 20:54:32 +0000231 if (!cached) {
232 // cache miss, compile shaders
233 if (fFS.fForceHighPrecision) {
234 settings.fForceHighPrecision = true;
egdaniel8dcdedc2015-11-11 06:27:20 -0800235 }
Ethan Nicholas255f73a2018-12-12 20:54:32 +0000236 SkSL::String glsl;
237 std::unique_ptr<SkSL::Program> fs = GrSkSLtoGLSL(gpu()->glContext(),
238 GR_GL_FRAGMENT_SHADER,
239 fFS.fCompilerStrings.begin(),
240 fFS.fCompilerStringLengths.begin(),
241 fFS.fCompilerStrings.count(),
242 settings,
243 &glsl);
244 if (!fs) {
245 this->cleanupProgram(programID, shadersToDelete);
246 return nullptr;
247 }
248 inputs = fs->fInputs;
Ethan Nicholascd700e92018-08-24 16:43:57 -0400249 this->addInputVars(inputs);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400250 if (!this->compileAndAttachShaders(glsl.c_str(), glsl.size(), programID,
251 GR_GL_FRAGMENT_SHADER, &shadersToDelete, settings,
252 inputs)) {
253 this->cleanupProgram(programID, shadersToDelete);
254 return nullptr;
255 }
256
257 std::unique_ptr<SkSL::Program> vs = GrSkSLtoGLSL(gpu()->glContext(),
258 GR_GL_VERTEX_SHADER,
259 fVS.fCompilerStrings.begin(),
260 fVS.fCompilerStringLengths.begin(),
261 fVS.fCompilerStrings.count(),
262 settings,
263 &glsl);
Brian Salomon7c934722018-06-01 14:42:53 -0400264 if (!vs || !this->compileAndAttachShaders(glsl.c_str(), glsl.size(), programID,
265 GR_GL_VERTEX_SHADER, &shadersToDelete, settings,
266 inputs)) {
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400267 this->cleanupProgram(programID, shadersToDelete);
268 return nullptr;
269 }
270
271 // NVPR actually requires a vertex shader to compile
272 bool useNvpr = primProc.isPathRendering();
273 if (!useNvpr) {
Ethan Nicholasad77dce2018-07-11 13:59:03 -0400274 this->computeCountsAndStrides(programID, primProc, true);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400275 }
276
277 if (primProc.willUseGeoShader()) {
278 std::unique_ptr<SkSL::Program> gs;
279 gs = GrSkSLtoGLSL(gpu()->glContext(),
280 GR_GL_GEOMETRY_SHADER,
281 fGS.fCompilerStrings.begin(),
282 fGS.fCompilerStringLengths.begin(),
283 fGS.fCompilerStrings.count(),
284 settings,
285 &glsl);
Brian Salomon7c934722018-06-01 14:42:53 -0400286 if (!gs || !this->compileAndAttachShaders(glsl.c_str(), glsl.size(), programID,
287 GR_GL_GEOMETRY_SHADER, &shadersToDelete,
288 settings, inputs)) {
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400289 this->cleanupProgram(programID, shadersToDelete);
290 return nullptr;
291 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400292 }
293 this->bindProgramResourceLocations(programID);
294
295 GL_CALL(LinkProgram(programID));
Ethan Nicholas5a0338c2019-01-02 11:48:56 -0500296 if (checkLinked) {
297 if (!this->checkLinkStatus(programID)) {
298 GL_CALL(DeleteProgram(programID));
299 SkDebugf("VS:\n");
300 GrGLPrintShader(fGpu->glContext(),
301 GR_GL_VERTEX_SHADER,
302 fVS.fCompilerStrings.begin(),
303 fVS.fCompilerStringLengths.begin(),
304 fVS.fCompilerStrings.count(),
305 settings);
306 if (primProc.willUseGeoShader()) {
307 SkDebugf("\nGS:\n");
308 GrGLPrintShader(fGpu->glContext(),
309 GR_GL_GEOMETRY_SHADER,
310 fGS.fCompilerStrings.begin(),
311 fGS.fCompilerStringLengths.begin(),
312 fGS.fCompilerStrings.count(), settings);
313 }
314 SkDebugf("\nFS:\n");
315 GrGLPrintShader(fGpu->glContext(),
316 GR_GL_FRAGMENT_SHADER,
317 fFS.fCompilerStrings.begin(),
318 fFS.fCompilerStringLengths.begin(),
319 fFS.fCompilerStrings.count(),
320 settings);
321 return nullptr;
Brian Salomone334c592017-05-15 11:00:58 -0400322 }
Brian Salomone334c592017-05-15 11:00:58 -0400323 }
joshualittfe1233c2014-10-07 12:16:35 -0700324 }
kkinnunen7aedda52015-06-29 23:01:28 -0700325 this->resolveProgramResourceLocations(programID);
joshualittdb0d3ca2014-10-07 12:42:26 -0700326
joshualitt47bb3822014-10-07 16:43:25 -0700327 this->cleanupShaders(shadersToDelete);
Ethan Nicholas255f73a2018-12-12 20:54:32 +0000328 if (!cached && this->gpu()->getContext()->contextPriv().getPersistentCache() &&
329 fGpu->glCaps().programBinarySupport()) {
330 GrGLsizei length = 0;
331 GL_CALL(GetProgramiv(programID, GL_PROGRAM_BINARY_LENGTH, &length));
332 if (length > 0) {
333 // store shader in cache
334 sk_sp<SkData> key = SkData::MakeWithoutCopy(desc()->asKey(), desc()->keyLength());
335 GrGLenum binaryFormat;
336 std::unique_ptr<char[]> binary(new char[length]);
337 GL_CALL(GetProgramBinary(programID, length, &length, &binaryFormat, binary.get()));
338 size_t dataLength = sizeof(inputs) + sizeof(binaryFormat) + length;
339 std::unique_ptr<uint8_t[]> data(new uint8_t[dataLength]);
340 size_t offset = 0;
341 memcpy(data.get() + offset, &inputs, sizeof(inputs));
342 offset += sizeof(inputs);
343 memcpy(data.get() + offset, &binaryFormat, sizeof(binaryFormat));
344 offset += sizeof(binaryFormat);
345 memcpy(data.get() + offset, binary.get(), length);
346 this->gpu()->getContext()->contextPriv().getPersistentCache()->store(
347 *key, *SkData::MakeWithoutCopy(data.get(), dataLength));
348 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400349 }
joshualitt47bb3822014-10-07 16:43:25 -0700350 return this->createProgram(programID);
351}
352
kkinnunen7aedda52015-06-29 23:01:28 -0700353void GrGLProgramBuilder::bindProgramResourceLocations(GrGLuint programID) {
egdaniel7ea439b2015-12-03 09:20:44 -0800354 fUniformHandler.bindUniformLocations(programID, fGpu->glCaps());
kkinnunen7aedda52015-06-29 23:01:28 -0700355
egdaniel8dcdedc2015-11-11 06:27:20 -0800356 const GrGLCaps& caps = this->gpu()->glCaps();
357 if (fFS.hasCustomColorOutput() && caps.bindFragDataLocationSupport()) {
358 GL_CALL(BindFragDataLocation(programID, 0,
egdaniel2d721d32015-11-11 13:06:05 -0800359 GrGLSLFragmentShaderBuilder::DeclaredColorOutputName()));
egdaniel8dcdedc2015-11-11 06:27:20 -0800360 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500361 if (fFS.hasSecondaryOutput() && caps.shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdaniel8dcdedc2015-11-11 06:27:20 -0800362 GL_CALL(BindFragDataLocationIndexed(programID, 0, 1,
egdaniel2d721d32015-11-11 13:06:05 -0800363 GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName()));
egdaniel8dcdedc2015-11-11 06:27:20 -0800364 }
joshualittd8dd47b2015-09-11 11:45:01 -0700365
366 // handle NVPR separable varyings
367 if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() ||
368 !fGpu->glPathRendering()->shouldBindFragmentInputs()) {
369 return;
370 }
egdaniel0eafe792015-11-20 14:01:22 -0800371 int count = fVaryingHandler.fPathProcVaryingInfos.count();
joshualittd8dd47b2015-09-11 11:45:01 -0700372 for (int i = 0; i < count; ++i) {
egdaniel0eafe792015-11-20 14:01:22 -0800373 GL_CALL(BindFragmentInputLocation(programID, i,
374 fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str()));
375 fVaryingHandler.fPathProcVaryingInfos[i].fLocation = i;
joshualittd8dd47b2015-09-11 11:45:01 -0700376 }
joshualitt47bb3822014-10-07 16:43:25 -0700377}
378
379bool GrGLProgramBuilder::checkLinkStatus(GrGLuint programID) {
380 GrGLint linked = GR_GL_INIT_ZERO;
381 GL_CALL(GetProgramiv(programID, GR_GL_LINK_STATUS, &linked));
382 if (!linked) {
Brian Salomone334c592017-05-15 11:00:58 -0400383 SkDebugf("Program linking failed.\n");
joshualitt47bb3822014-10-07 16:43:25 -0700384 GrGLint infoLen = GR_GL_INIT_ZERO;
385 GL_CALL(GetProgramiv(programID, GR_GL_INFO_LOG_LENGTH, &infoLen));
386 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
387 if (infoLen > 0) {
388 // retrieve length even though we don't need it to workaround
389 // bug in chrome cmd buffer param validation.
390 GrGLsizei length = GR_GL_INIT_ZERO;
391 GL_CALL(GetProgramInfoLog(programID,
392 infoLen+1,
393 &length,
394 (char*)log.get()));
kkinnunen297aaf92015-02-19 06:32:12 -0800395 SkDebugf("%s", (char*)log.get());
joshualitt47bb3822014-10-07 16:43:25 -0700396 }
joshualitt47bb3822014-10-07 16:43:25 -0700397 }
398 return SkToBool(linked);
399}
400
kkinnunen7aedda52015-06-29 23:01:28 -0700401void GrGLProgramBuilder::resolveProgramResourceLocations(GrGLuint programID) {
egdaniel7ea439b2015-12-03 09:20:44 -0800402 fUniformHandler.getUniformLocations(programID, fGpu->glCaps());
joshualittd8dd47b2015-09-11 11:45:01 -0700403
404 // handle NVPR separable varyings
405 if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() ||
kkinnunen7bdb05d2016-01-25 00:47:23 -0800406 fGpu->glPathRendering()->shouldBindFragmentInputs()) {
joshualittd8dd47b2015-09-11 11:45:01 -0700407 return;
408 }
egdaniel0eafe792015-11-20 14:01:22 -0800409 int count = fVaryingHandler.fPathProcVaryingInfos.count();
joshualittd8dd47b2015-09-11 11:45:01 -0700410 for (int i = 0; i < count; ++i) {
411 GrGLint location;
egdaniel0eafe792015-11-20 14:01:22 -0800412 GL_CALL_RET(location, GetProgramResourceLocation(
413 programID,
414 GR_GL_FRAGMENT_INPUT,
415 fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str()));
416 fVaryingHandler.fPathProcVaryingInfos[i].fLocation = location;
joshualittd8dd47b2015-09-11 11:45:01 -0700417 }
joshualitt30ba4362014-08-21 20:18:45 -0700418}
419
joshualitt47bb3822014-10-07 16:43:25 -0700420void GrGLProgramBuilder::cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs) {
421 GL_CALL(DeleteProgram(programID));
egdanielfa896322016-01-13 12:19:30 -0800422 this->cleanupShaders(shaderIDs);
joshualitt47bb3822014-10-07 16:43:25 -0700423}
424void GrGLProgramBuilder::cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs) {
425 for (int i = 0; i < shaderIDs.count(); ++i) {
Brian Salomon802cb312018-06-08 18:05:20 -0400426 GL_CALL(DeleteShader(shaderIDs[i]));
joshualitt47bb3822014-10-07 16:43:25 -0700427 }
428}
429
430GrGLProgram* GrGLProgramBuilder::createProgram(GrGLuint programID) {
egdaniel7ea439b2015-12-03 09:20:44 -0800431 return new GrGLProgram(fGpu,
egdaniel7ea439b2015-12-03 09:20:44 -0800432 fUniformHandles,
433 programID,
434 fUniformHandler.fUniforms,
egdaniel09aa1fc2016-04-20 07:09:46 -0700435 fUniformHandler.fSamplers,
egdaniel0eafe792015-11-20 14:01:22 -0800436 fVaryingHandler.fPathProcVaryingInfos,
Robert Phillips369e8b72017-08-01 16:13:04 -0400437 std::move(fGeometryProcessor),
438 std::move(fXferProcessor),
Brian Salomon4d3f5172018-06-07 14:42:52 -0400439 std::move(fFragmentProcessors),
Brian Salomon802cb312018-06-08 18:05:20 -0400440 fFragmentProcessorCnt,
441 std::move(fAttributes),
Brian Salomon92be2f72018-06-19 14:33:47 -0400442 fVertexAttributeCnt,
443 fInstanceAttributeCnt,
Brian Salomon802cb312018-06-08 18:05:20 -0400444 fVertexStride,
445 fInstanceStride);
joshualitt47bb3822014-10-07 16:43:25 -0700446}