1 | /* |
2 | * Copyright 2014 Google Inc. |
3 | * |
4 | * Use of this source code is governed by a BSD-style license that can be |
5 | * found in the LICENSE file. |
6 | */ |
7 | |
8 | #include "src/gpu/gl/builders/GrGLProgramBuilder.h" |
9 | |
10 | #include "include/gpu/GrContext.h" |
11 | #include "src/core/SkATrace.h" |
12 | #include "src/core/SkAutoMalloc.h" |
13 | #include "src/core/SkReader32.h" |
14 | #include "src/core/SkTraceEvent.h" |
15 | #include "src/core/SkWriter32.h" |
16 | #include "src/gpu/GrAutoLocaleSetter.h" |
17 | #include "src/gpu/GrContextPriv.h" |
18 | #include "src/gpu/GrCoordTransform.h" |
19 | #include "src/gpu/GrPersistentCacheUtils.h" |
20 | #include "src/gpu/GrProgramDesc.h" |
21 | #include "src/gpu/GrShaderCaps.h" |
22 | #include "src/gpu/GrShaderUtils.h" |
23 | #include "src/gpu/GrSwizzle.h" |
24 | #include "src/gpu/gl/GrGLGpu.h" |
25 | #include "src/gpu/gl/GrGLProgram.h" |
26 | #include "src/gpu/gl/builders/GrGLProgramBuilder.h" |
27 | #include "src/gpu/gl/builders/GrGLShaderStringBuilder.h" |
28 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
29 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
30 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
31 | #include "src/gpu/glsl/GrGLSLXferProcessor.h" |
32 | |
33 | #define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X) |
34 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X) |
35 | |
36 | static void cleanup_shaders(GrGLGpu* gpu, const SkTDArray<GrGLuint>& shaderIDs) { |
37 | for (int i = 0; i < shaderIDs.count(); ++i) { |
38 | GR_GL_CALL(gpu->glInterface(), DeleteShader(shaderIDs[i])); |
39 | } |
40 | } |
41 | |
42 | static void cleanup_program(GrGLGpu* gpu, GrGLuint programID, |
43 | const SkTDArray<GrGLuint>& shaderIDs) { |
44 | GR_GL_CALL(gpu->glInterface(), DeleteProgram(programID)); |
45 | cleanup_shaders(gpu, shaderIDs); |
46 | } |
47 | |
48 | sk_sp<GrGLProgram> GrGLProgramBuilder::CreateProgram( |
49 | GrGLGpu* gpu, |
50 | GrRenderTarget* renderTarget, |
51 | const GrProgramDesc& desc, |
52 | const GrProgramInfo& programInfo, |
53 | const GrGLPrecompiledProgram* precompiledProgram) { |
54 | ATRACE_ANDROID_FRAMEWORK_ALWAYS("shader_compile" ); |
55 | GrAutoLocaleSetter als("C" ); |
56 | |
57 | // create a builder. This will be handed off to effects so they can use it to add |
58 | // uniforms, varyings, textures, etc |
59 | GrGLProgramBuilder builder(gpu, renderTarget, desc, programInfo); |
60 | |
61 | auto persistentCache = gpu->getContext()->priv().getPersistentCache(); |
62 | if (persistentCache && !precompiledProgram) { |
63 | sk_sp<SkData> key = SkData::MakeWithoutCopy(desc.asKey(), desc.keyLength()); |
64 | builder.fCached = persistentCache->load(*key); |
65 | // the eventual end goal is to completely skip emitAndInstallProcs on a cache hit, but it's |
66 | // doing necessary setup in addition to generating the SkSL code. Currently we are only able |
67 | // to skip the SkSL->GLSL step on a cache hit. |
68 | } |
69 | if (!builder.emitAndInstallProcs()) { |
70 | return nullptr; |
71 | } |
72 | return builder.finalize(precompiledProgram); |
73 | } |
74 | |
75 | ///////////////////////////////////////////////////////////////////////////// |
76 | |
77 | GrGLProgramBuilder::GrGLProgramBuilder(GrGLGpu* gpu, |
78 | GrRenderTarget* renderTarget, |
79 | const GrProgramDesc& desc, |
80 | const GrProgramInfo& programInfo) |
81 | : INHERITED(renderTarget, desc, programInfo) |
82 | , fGpu(gpu) |
83 | , fVaryingHandler(this) |
84 | , fUniformHandler(this) |
85 | , fVertexAttributeCnt(0) |
86 | , fInstanceAttributeCnt(0) |
87 | , fVertexStride(0) |
88 | , fInstanceStride(0) {} |
89 | |
90 | const GrCaps* GrGLProgramBuilder::caps() const { |
91 | return fGpu->caps(); |
92 | } |
93 | |
94 | bool GrGLProgramBuilder::compileAndAttachShaders(const SkSL::String& glsl, |
95 | GrGLuint programId, |
96 | GrGLenum type, |
97 | SkTDArray<GrGLuint>* shaderIds, |
98 | GrContextOptions::ShaderErrorHandler* errHandler) { |
99 | GrGLGpu* gpu = this->gpu(); |
100 | GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(), |
101 | programId, |
102 | type, |
103 | glsl, |
104 | gpu->stats(), |
105 | errHandler); |
106 | if (!shaderId) { |
107 | return false; |
108 | } |
109 | |
110 | *shaderIds->append() = shaderId; |
111 | return true; |
112 | } |
113 | |
114 | void GrGLProgramBuilder::computeCountsAndStrides(GrGLuint programID, |
115 | const GrPrimitiveProcessor& primProc, |
116 | bool bindAttribLocations) { |
117 | fVertexAttributeCnt = primProc.numVertexAttributes(); |
118 | fInstanceAttributeCnt = primProc.numInstanceAttributes(); |
119 | fAttributes.reset( |
120 | new GrGLProgram::Attribute[fVertexAttributeCnt + fInstanceAttributeCnt]); |
121 | auto addAttr = [&](int i, const auto& a, size_t* stride) { |
122 | fAttributes[i].fCPUType = a.cpuType(); |
123 | fAttributes[i].fGPUType = a.gpuType(); |
124 | fAttributes[i].fOffset = *stride; |
125 | *stride += a.sizeAlign4(); |
126 | fAttributes[i].fLocation = i; |
127 | if (bindAttribLocations) { |
128 | GL_CALL(BindAttribLocation(programID, i, a.name())); |
129 | } |
130 | }; |
131 | fVertexStride = 0; |
132 | int i = 0; |
133 | for (const auto& attr : primProc.vertexAttributes()) { |
134 | addAttr(i++, attr, &fVertexStride); |
135 | } |
136 | SkASSERT(fVertexStride == primProc.vertexStride()); |
137 | fInstanceStride = 0; |
138 | for (const auto& attr : primProc.instanceAttributes()) { |
139 | addAttr(i++, attr, &fInstanceStride); |
140 | } |
141 | SkASSERT(fInstanceStride == primProc.instanceStride()); |
142 | } |
143 | |
144 | void GrGLProgramBuilder::addInputVars(const SkSL::Program::Inputs& inputs) { |
145 | if (inputs.fRTWidth) { |
146 | this->addRTWidthUniform(SKSL_RTWIDTH_NAME); |
147 | } |
148 | if (inputs.fRTHeight) { |
149 | this->addRTHeightUniform(SKSL_RTHEIGHT_NAME); |
150 | } |
151 | } |
152 | |
153 | static constexpr SkFourByteTag kSKSL_Tag = SkSetFourByteTag('S', 'K', 'S', 'L'); |
154 | static constexpr SkFourByteTag kGLSL_Tag = SkSetFourByteTag('G', 'L', 'S', 'L'); |
155 | static constexpr SkFourByteTag kGLPB_Tag = SkSetFourByteTag('G', 'L', 'P', 'B'); |
156 | |
157 | void GrGLProgramBuilder::storeShaderInCache(const SkSL::Program::Inputs& inputs, GrGLuint programID, |
158 | const SkSL::String shaders[], bool isSkSL, |
159 | SkSL::Program::Settings* settings) { |
160 | if (!this->gpu()->getContext()->priv().getPersistentCache()) { |
161 | return; |
162 | } |
163 | sk_sp<SkData> key = SkData::MakeWithoutCopy(this->desc().asKey(), this->desc().keyLength()); |
164 | if (fGpu->glCaps().programBinarySupport()) { |
165 | // binary cache |
166 | GrGLsizei length = 0; |
167 | GL_CALL(GetProgramiv(programID, GL_PROGRAM_BINARY_LENGTH, &length)); |
168 | if (length > 0) { |
169 | SkWriter32 writer; |
170 | writer.write32(kGLPB_Tag); |
171 | |
172 | writer.writePad(&inputs, sizeof(inputs)); |
173 | writer.write32(length); |
174 | |
175 | void* binary = writer.reservePad(length); |
176 | GrGLenum binaryFormat; |
177 | GL_CALL(GetProgramBinary(programID, length, &length, &binaryFormat, binary)); |
178 | writer.write32(binaryFormat); |
179 | |
180 | auto data = writer.snapshotAsData(); |
181 | this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data); |
182 | } |
183 | } else { |
184 | // source cache, plus metadata to allow for a complete precompile |
185 | GrPersistentCacheUtils::ShaderMetadata meta; |
186 | meta.fSettings = settings; |
187 | meta.fHasCustomColorOutput = fFS.hasCustomColorOutput(); |
188 | meta.fHasSecondaryColorOutput = fFS.hasSecondaryOutput(); |
189 | for (const auto& attr : this->primitiveProcessor().vertexAttributes()) { |
190 | meta.fAttributeNames.emplace_back(attr.name()); |
191 | } |
192 | for (const auto& attr : this->primitiveProcessor().instanceAttributes()) { |
193 | meta.fAttributeNames.emplace_back(attr.name()); |
194 | } |
195 | |
196 | auto data = GrPersistentCacheUtils::PackCachedShaders(isSkSL ? kSKSL_Tag : kGLSL_Tag, |
197 | shaders, &inputs, 1, &meta); |
198 | this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data); |
199 | } |
200 | } |
201 | |
202 | sk_sp<GrGLProgram> GrGLProgramBuilder::finalize(const GrGLPrecompiledProgram* precompiledProgram) { |
203 | TRACE_EVENT0("skia.gpu" , TRACE_FUNC); |
204 | |
205 | // verify we can get a program id |
206 | GrGLuint programID; |
207 | if (precompiledProgram) { |
208 | programID = precompiledProgram->fProgramID; |
209 | } else { |
210 | GL_CALL_RET(programID, CreateProgram()); |
211 | } |
212 | if (0 == programID) { |
213 | return nullptr; |
214 | } |
215 | |
216 | if (this->gpu()->glCaps().programBinarySupport() && |
217 | this->gpu()->glCaps().programParameterSupport() && |
218 | this->gpu()->getContext()->priv().getPersistentCache() && |
219 | !precompiledProgram) { |
220 | GL_CALL(ProgramParameteri(programID, GR_GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GR_GL_TRUE)); |
221 | } |
222 | |
223 | this->finalizeShaders(); |
224 | |
225 | // compile shaders and bind attributes / uniforms |
226 | auto errorHandler = this->gpu()->getContext()->priv().getShaderErrorHandler(); |
227 | const GrPrimitiveProcessor& primProc = this->primitiveProcessor(); |
228 | SkSL::Program::Settings settings; |
229 | settings.fCaps = this->gpu()->glCaps().shaderCaps(); |
230 | settings.fFlipY = this->origin() != kTopLeft_GrSurfaceOrigin; |
231 | settings.fSharpenTextures = |
232 | this->gpu()->getContext()->priv().options().fSharpenMipmappedTextures; |
233 | settings.fFragColorIsInOut = this->fragColorIsInOut(); |
234 | |
235 | SkSL::Program::Inputs inputs; |
236 | SkTDArray<GrGLuint> shadersToDelete; |
237 | |
238 | bool checkLinked = !fGpu->glCaps().skipErrorChecks(); |
239 | |
240 | bool cached = fCached.get() != nullptr; |
241 | bool usedProgramBinaries = false; |
242 | SkSL::String glsl[kGrShaderTypeCount]; |
243 | SkSL::String* sksl[kGrShaderTypeCount] = { |
244 | &fVS.fCompilerString, |
245 | &fGS.fCompilerString, |
246 | &fFS.fCompilerString, |
247 | }; |
248 | SkSL::String cached_sksl[kGrShaderTypeCount]; |
249 | if (precompiledProgram) { |
250 | // This is very similar to when we get program binaries. We even set that flag, as it's |
251 | // used to prevent other compile work later, and to force re-querying uniform locations. |
252 | this->addInputVars(precompiledProgram->fInputs); |
253 | this->computeCountsAndStrides(programID, primProc, false); |
254 | usedProgramBinaries = true; |
255 | } else if (cached) { |
256 | ATRACE_ANDROID_FRAMEWORK_ALWAYS("cache_hit" ); |
257 | SkReader32 reader(fCached->data(), fCached->size()); |
258 | SkFourByteTag shaderType = GrPersistentCacheUtils::GetType(&reader); |
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; |
267 | } |
268 | 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; |
286 | } |
287 | usedProgramBinaries = cached; |
288 | break; |
289 | } |
290 | |
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); |
299 | for (int i = 0; i < kGrShaderTypeCount; ++i) { |
300 | sksl[i] = &cached_sksl[i]; |
301 | } |
302 | break; |
303 | } |
304 | } |
305 | if (!usedProgramBinaries) { |
306 | ATRACE_ANDROID_FRAMEWORK_ALWAYS("cache_miss" ); |
307 | // Either a cache miss, or we got something other than binaries from the cache |
308 | |
309 | /* |
310 | Fragment Shader |
311 | */ |
312 | if (glsl[kFragment_GrShaderType].empty()) { |
313 | // Don't have cached GLSL, need to compile SkSL->GLSL |
314 | if (fFS.fForceHighPrecision) { |
315 | settings.fForceHighPrecision = true; |
316 | } |
317 | std::unique_ptr<SkSL::Program> fs = GrSkSLtoGLSL(gpu()->glContext(), |
318 | SkSL::Program::kFragment_Kind, |
319 | *sksl[kFragment_GrShaderType], |
320 | settings, |
321 | &glsl[kFragment_GrShaderType], |
322 | errorHandler); |
323 | if (!fs) { |
324 | cleanup_program(fGpu, programID, shadersToDelete); |
325 | return nullptr; |
326 | } |
327 | inputs = fs->fInputs; |
328 | } |
329 | |
330 | this->addInputVars(inputs); |
331 | if (!this->compileAndAttachShaders(glsl[kFragment_GrShaderType], programID, |
332 | GR_GL_FRAGMENT_SHADER, &shadersToDelete, errorHandler)) { |
333 | cleanup_program(fGpu, programID, shadersToDelete); |
334 | return nullptr; |
335 | } |
336 | |
337 | /* |
338 | Vertex Shader |
339 | */ |
340 | if (glsl[kVertex_GrShaderType].empty()) { |
341 | // Don't have cached GLSL, need to compile SkSL->GLSL |
342 | std::unique_ptr<SkSL::Program> vs = GrSkSLtoGLSL(gpu()->glContext(), |
343 | SkSL::Program::kVertex_Kind, |
344 | *sksl[kVertex_GrShaderType], |
345 | settings, |
346 | &glsl[kVertex_GrShaderType], |
347 | errorHandler); |
348 | if (!vs) { |
349 | cleanup_program(fGpu, programID, shadersToDelete); |
350 | return nullptr; |
351 | } |
352 | } |
353 | if (!this->compileAndAttachShaders(glsl[kVertex_GrShaderType], programID, |
354 | GR_GL_VERTEX_SHADER, &shadersToDelete, errorHandler)) { |
355 | cleanup_program(fGpu, programID, shadersToDelete); |
356 | return nullptr; |
357 | } |
358 | |
359 | // This also binds vertex attribute locations. NVPR doesn't really use vertices, |
360 | // even though it requires a vertex shader in the program. |
361 | if (!primProc.isPathRendering()) { |
362 | this->computeCountsAndStrides(programID, primProc, true); |
363 | } |
364 | |
365 | /* |
366 | Tessellation Shaders |
367 | */ |
368 | if (fProgramInfo.primProc().willUseTessellationShaders()) { |
369 | // Tessellation shaders are not currently supported by SkSL. So here, we temporarily |
370 | // generate GLSL strings directly using back door methods on GrPrimitiveProcessor, and |
371 | // pass those raw strings on to the driver. |
372 | SkString versionAndExtensionDecls; |
373 | versionAndExtensionDecls.appendf("%s\n" , this->shaderCaps()->versionDeclString()); |
374 | if (const char* extensionString = this->shaderCaps()->tessellationExtensionString()) { |
375 | versionAndExtensionDecls.appendf("#extension %s : require\n" , extensionString); |
376 | } |
377 | |
378 | SkString tessControlShader = primProc.getTessControlShaderGLSL( |
379 | versionAndExtensionDecls.c_str(), *this->shaderCaps()); |
380 | if (!this->compileAndAttachShaders(tessControlShader.c_str(), programID, |
381 | GR_GL_TESS_CONTROL_SHADER, &shadersToDelete, |
382 | errorHandler)) { |
383 | cleanup_program(fGpu, programID, shadersToDelete); |
384 | return nullptr; |
385 | } |
386 | |
387 | SkString tessEvaluationShader = primProc.getTessEvaluationShaderGLSL( |
388 | versionAndExtensionDecls.c_str(), *this->shaderCaps()); |
389 | if (!this->compileAndAttachShaders(tessEvaluationShader.c_str(), programID, |
390 | GR_GL_TESS_EVALUATION_SHADER, &shadersToDelete, |
391 | errorHandler)) { |
392 | cleanup_program(fGpu, programID, shadersToDelete); |
393 | return nullptr; |
394 | } |
395 | } |
396 | |
397 | /* |
398 | Geometry Shader |
399 | */ |
400 | if (primProc.willUseGeoShader()) { |
401 | if (glsl[kGeometry_GrShaderType].empty()) { |
402 | // Don't have cached GLSL, need to compile SkSL->GLSL |
403 | std::unique_ptr<SkSL::Program> gs; |
404 | gs = GrSkSLtoGLSL(gpu()->glContext(), |
405 | SkSL::Program::kGeometry_Kind, |
406 | *sksl[kGeometry_GrShaderType], |
407 | settings, |
408 | &glsl[kGeometry_GrShaderType], |
409 | errorHandler); |
410 | if (!gs) { |
411 | cleanup_program(fGpu, programID, shadersToDelete); |
412 | return nullptr; |
413 | } |
414 | } |
415 | if (!this->compileAndAttachShaders(glsl[kGeometry_GrShaderType], programID, |
416 | GR_GL_GEOMETRY_SHADER, &shadersToDelete, |
417 | errorHandler)) { |
418 | cleanup_program(fGpu, programID, shadersToDelete); |
419 | return nullptr; |
420 | } |
421 | } |
422 | this->bindProgramResourceLocations(programID); |
423 | |
424 | GL_CALL(LinkProgram(programID)); |
425 | if (checkLinked) { |
426 | if (!this->checkLinkStatus(programID, errorHandler, sksl, glsl)) { |
427 | cleanup_program(fGpu, programID, shadersToDelete); |
428 | return nullptr; |
429 | } |
430 | } |
431 | } |
432 | this->resolveProgramResourceLocations(programID, usedProgramBinaries); |
433 | |
434 | cleanup_shaders(fGpu, shadersToDelete); |
435 | |
436 | // We temporarily can't cache tessellation shaders while using back door GLSL. |
437 | // |
438 | // We also can't cache SkSL or GLSL if we were given a precompiled program, but there's not |
439 | // much point in doing so. |
440 | if (!cached && !primProc.willUseTessellationShaders() && !precompiledProgram) { |
441 | // FIXME: Remove the check for tessellation shaders in the above 'if' once the back door |
442 | // GLSL mechanism is removed. |
443 | (void)&GrPrimitiveProcessor::getTessControlShaderGLSL; |
444 | bool isSkSL = false; |
445 | if (fGpu->getContext()->priv().options().fShaderCacheStrategy == |
446 | GrContextOptions::ShaderCacheStrategy::kSkSL) { |
447 | for (int i = 0; i < kGrShaderTypeCount; ++i) { |
448 | glsl[i] = GrShaderUtils::PrettyPrint(*sksl[i]); |
449 | } |
450 | isSkSL = true; |
451 | } |
452 | this->storeShaderInCache(inputs, programID, glsl, isSkSL, &settings); |
453 | } |
454 | return this->createProgram(programID); |
455 | } |
456 | |
457 | void GrGLProgramBuilder::bindProgramResourceLocations(GrGLuint programID) { |
458 | fUniformHandler.bindUniformLocations(programID, fGpu->glCaps()); |
459 | |
460 | const GrGLCaps& caps = this->gpu()->glCaps(); |
461 | if (fFS.hasCustomColorOutput() && caps.bindFragDataLocationSupport()) { |
462 | GL_CALL(BindFragDataLocation(programID, 0, |
463 | GrGLSLFragmentShaderBuilder::DeclaredColorOutputName())); |
464 | } |
465 | if (fFS.hasSecondaryOutput() && caps.shaderCaps()->mustDeclareFragmentShaderOutput()) { |
466 | GL_CALL(BindFragDataLocationIndexed(programID, 0, 1, |
467 | GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName())); |
468 | } |
469 | |
470 | // handle NVPR separable varyings |
471 | if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() || |
472 | !fGpu->glPathRendering()->shouldBindFragmentInputs()) { |
473 | return; |
474 | } |
475 | int i = 0; |
476 | for (auto& varying : fVaryingHandler.fPathProcVaryingInfos.items()) { |
477 | GL_CALL(BindFragmentInputLocation(programID, i, varying.fVariable.c_str())); |
478 | varying.fLocation = i; |
479 | ++i; |
480 | } |
481 | } |
482 | |
483 | bool GrGLProgramBuilder::checkLinkStatus(GrGLuint programID, |
484 | GrContextOptions::ShaderErrorHandler* errorHandler, |
485 | SkSL::String* sksl[], const SkSL::String glsl[]) { |
486 | GrGLint linked = GR_GL_INIT_ZERO; |
487 | GL_CALL(GetProgramiv(programID, GR_GL_LINK_STATUS, &linked)); |
488 | if (!linked) { |
489 | SkSL::String allShaders; |
490 | if (sksl) { |
491 | allShaders.appendf("// Vertex SKSL\n%s\n" , sksl[kVertex_GrShaderType]->c_str()); |
492 | if (!sksl[kGeometry_GrShaderType]->empty()) { |
493 | allShaders.appendf("// Geometry SKSL\n%s\n" , sksl[kGeometry_GrShaderType]->c_str()); |
494 | } |
495 | allShaders.appendf("// Fragment SKSL\n%s\n" , sksl[kFragment_GrShaderType]->c_str()); |
496 | } |
497 | if (glsl) { |
498 | allShaders.appendf("// Vertex GLSL\n%s\n" , glsl[kVertex_GrShaderType].c_str()); |
499 | if (!glsl[kGeometry_GrShaderType].empty()) { |
500 | allShaders.appendf("// Geometry GLSL\n%s\n" , glsl[kGeometry_GrShaderType].c_str()); |
501 | } |
502 | allShaders.appendf("// Fragment GLSL\n%s\n" , glsl[kFragment_GrShaderType].c_str()); |
503 | } |
504 | GrGLint infoLen = GR_GL_INIT_ZERO; |
505 | GL_CALL(GetProgramiv(programID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
506 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
507 | if (infoLen > 0) { |
508 | // retrieve length even though we don't need it to workaround |
509 | // bug in chrome cmd buffer param validation. |
510 | GrGLsizei length = GR_GL_INIT_ZERO; |
511 | GL_CALL(GetProgramInfoLog(programID, infoLen+1, &length, (char*)log.get())); |
512 | } |
513 | errorHandler->compileError(allShaders.c_str(), infoLen > 0 ? (const char*)log.get() : "" ); |
514 | } |
515 | return SkToBool(linked); |
516 | } |
517 | |
518 | void GrGLProgramBuilder::resolveProgramResourceLocations(GrGLuint programID, bool force) { |
519 | fUniformHandler.getUniformLocations(programID, fGpu->glCaps(), force); |
520 | |
521 | // handle NVPR separable varyings |
522 | if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() || |
523 | fGpu->glPathRendering()->shouldBindFragmentInputs()) { |
524 | return; |
525 | } |
526 | for (auto& varying : fVaryingHandler.fPathProcVaryingInfos.items()) { |
527 | GrGLint location; |
528 | GL_CALL_RET(location, GetProgramResourceLocation( |
529 | programID, |
530 | GR_GL_FRAGMENT_INPUT, |
531 | varying.fVariable.c_str())); |
532 | varying.fLocation = location; |
533 | } |
534 | } |
535 | |
536 | sk_sp<GrGLProgram> GrGLProgramBuilder::createProgram(GrGLuint programID) { |
537 | return sk_sp<GrGLProgram>(new GrGLProgram(fGpu, |
538 | fUniformHandles, |
539 | programID, |
540 | fUniformHandler.fUniforms, |
541 | fUniformHandler.fSamplers, |
542 | fVaryingHandler.fPathProcVaryingInfos, |
543 | std::move(fGeometryProcessor), |
544 | std::move(fXferProcessor), |
545 | std::move(fFragmentProcessors), |
546 | fFragmentProcessorCnt, |
547 | std::move(fAttributes), |
548 | fVertexAttributeCnt, |
549 | fInstanceAttributeCnt, |
550 | fVertexStride, |
551 | fInstanceStride)); |
552 | } |
553 | |
554 | bool GrGLProgramBuilder::PrecompileProgram(GrGLPrecompiledProgram* precompiledProgram, |
555 | GrGLGpu* gpu, |
556 | const SkData& cachedData) { |
557 | SkReader32 reader(cachedData.data(), cachedData.size()); |
558 | SkFourByteTag shaderType = GrPersistentCacheUtils::GetType(&reader); |
559 | if (shaderType != kSKSL_Tag) { |
560 | // TODO: Support GLSL, and maybe even program binaries, too? |
561 | return false; |
562 | } |
563 | |
564 | const GrGLInterface* gl = gpu->glInterface(); |
565 | auto errorHandler = gpu->getContext()->priv().getShaderErrorHandler(); |
566 | GrGLuint programID; |
567 | GR_GL_CALL_RET(gl, programID, CreateProgram()); |
568 | if (0 == programID) { |
569 | return false; |
570 | } |
571 | |
572 | SkTDArray<GrGLuint> shadersToDelete; |
573 | |
574 | SkSL::Program::Settings settings; |
575 | const GrGLCaps& caps = gpu->glCaps(); |
576 | settings.fCaps = caps.shaderCaps(); |
577 | settings.fSharpenTextures = gpu->getContext()->priv().options().fSharpenMipmappedTextures; |
578 | GrPersistentCacheUtils::ShaderMetadata meta; |
579 | meta.fSettings = &settings; |
580 | |
581 | SkSL::String shaders[kGrShaderTypeCount]; |
582 | SkSL::Program::Inputs inputs; |
583 | GrPersistentCacheUtils::UnpackCachedShaders(&reader, shaders, &inputs, 1, &meta); |
584 | |
585 | auto compileShader = [&](SkSL::Program::Kind kind, const SkSL::String& sksl, GrGLenum type) { |
586 | SkSL::String glsl; |
587 | auto program = GrSkSLtoGLSL(gpu->glContext(), kind, sksl, settings, &glsl, errorHandler); |
588 | if (!program) { |
589 | return false; |
590 | } |
591 | |
592 | if (GrGLuint shaderID = GrGLCompileAndAttachShader(gpu->glContext(), programID, type, glsl, |
593 | gpu->stats(), errorHandler)) { |
594 | shadersToDelete.push_back(shaderID); |
595 | return true; |
596 | } else { |
597 | return false; |
598 | } |
599 | }; |
600 | |
601 | if (!compileShader(SkSL::Program::kFragment_Kind, |
602 | shaders[kFragment_GrShaderType], |
603 | GR_GL_FRAGMENT_SHADER) || |
604 | !compileShader(SkSL::Program::kVertex_Kind, |
605 | shaders[kVertex_GrShaderType], |
606 | GR_GL_VERTEX_SHADER) || |
607 | (!shaders[kGeometry_GrShaderType].empty() && |
608 | !compileShader(SkSL::Program::kGeometry_Kind, |
609 | shaders[kGeometry_GrShaderType], |
610 | GR_GL_GEOMETRY_SHADER))) { |
611 | cleanup_program(gpu, programID, shadersToDelete); |
612 | return false; |
613 | } |
614 | |
615 | for (int i = 0; i < meta.fAttributeNames.count(); ++i) { |
616 | GR_GL_CALL(gpu->glInterface(), BindAttribLocation(programID, i, |
617 | meta.fAttributeNames[i].c_str())); |
618 | } |
619 | |
620 | if (meta.fHasCustomColorOutput && caps.bindFragDataLocationSupport()) { |
621 | GR_GL_CALL(gpu->glInterface(), BindFragDataLocation(programID, 0, |
622 | GrGLSLFragmentShaderBuilder::DeclaredColorOutputName())); |
623 | } |
624 | if (meta.fHasSecondaryColorOutput && caps.shaderCaps()->mustDeclareFragmentShaderOutput()) { |
625 | GR_GL_CALL(gpu->glInterface(), BindFragDataLocationIndexed(programID, 0, 1, |
626 | GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName())); |
627 | } |
628 | |
629 | GR_GL_CALL(gpu->glInterface(), LinkProgram(programID)); |
630 | GrGLint linked = GR_GL_INIT_ZERO; |
631 | GR_GL_CALL(gpu->glInterface(), GetProgramiv(programID, GR_GL_LINK_STATUS, &linked)); |
632 | if (!linked) { |
633 | cleanup_program(gpu, programID, shadersToDelete); |
634 | return false; |
635 | } |
636 | |
637 | cleanup_shaders(gpu, shadersToDelete); |
638 | |
639 | precompiledProgram->fProgramID = programID; |
640 | precompiledProgram->fInputs = inputs; |
641 | return true; |
642 | } |
643 | |