| 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 |
#include "src/gpu/ganesh/gl/builders/GrGLProgramBuilder.h" |
| 8 |
|
| 9 |
#include "include/core/SkFourByteTag.h" |
| 10 |
#include "include/core/SkTypes.h" |
| 11 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 12 |
#include "include/gpu/ganesh/gl/GrGLFunctions.h" |
| 13 |
#include "include/gpu/ganesh/gl/GrGLInterface.h" |
| 14 |
#include "include/private/base/SkTArray.h" |
| 15 |
#include "include/private/base/SkTDArray.h" |
| 16 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 17 |
#include "src/base/SkAutoMalloc.h" |
| 18 |
#include "src/core/SkReadBuffer.h" |
| 19 |
#include "src/core/SkTraceEvent.h" |
| 20 |
#include "src/core/SkWriteBuffer.h" |
| 21 |
#include "src/gpu/ganesh/GrAutoLocaleSetter.h" |
| 22 |
#include "src/gpu/ganesh/GrCaps.h" |
| 23 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 24 |
#include "src/gpu/ganesh/GrGeometryProcessor.h" |
| 25 |
#include "src/gpu/ganesh/GrPersistentCacheUtils.h" |
| 26 |
#include "src/gpu/ganesh/GrProgramDesc.h" |
| 27 |
#include "src/gpu/ganesh/GrShaderCaps.h" |
| 28 |
#include "src/gpu/ganesh/GrThreadSafePipelineBuilder.h" |
| 29 |
#include "src/gpu/ganesh/gl/GrGLCaps.h" |
| 30 |
#include "src/gpu/ganesh/gl/GrGLDefines.h" |
| 31 |
#include "src/gpu/ganesh/gl/GrGLGpu.h" |
| 32 |
#include "src/gpu/ganesh/gl/GrGLProgram.h" |
| 33 |
#include "src/gpu/ganesh/gl/GrGLUtil.h" |
| 34 |
#include "src/gpu/ganesh/gl/builders/GrGLShaderStringBuilder.h" |
| 35 |
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h" |
| 36 |
#include "src/gpu/ganesh/glsl/GrGLSLVertexGeoBuilder.h" |
| 37 |
#include "src/sksl/SkSLProgramKind.h" |
| 38 |
#include "src/sksl/SkSLProgramSettings.h" |
| 39 |
#include "src/sksl/codegen/SkSLNativeShader.h" |
| 40 |
#include "src/utils/SkShaderUtils.h" |
| 41 |
|
| 42 |
#include <cstdint> |
| 43 |
#include <memory> |
| 44 |
#include <string> |
| 45 |
#include <utility> |
| 46 |
|
| 47 |
#define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X) |
| 48 |
#define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X) |
| 49 |
|
| 50 |
static void cleanup_shaders(GrGLGpu* gpu, const SkTDArray<GrGLuint>& shaderIDs) { |
| 51 |
for (int i = 0; i < shaderIDs.size(); ++i) { |
| 52 |
GR_GL_CALL(gpu->glInterface(), DeleteShader(shaderIDs[i])); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
static void cleanup_program(GrGLGpu* gpu, GrGLuint programID, |
| 57 |
const SkTDArray<GrGLuint>& shaderIDs) { |
| 58 |
GR_GL_CALL(gpu->glInterface(), DeleteProgram(programID)); |
| 59 |
cleanup_shaders(gpu, shaderIDs); |
| 60 |
} |
| 61 |
|
| 62 |
sk_sp<GrGLProgram> GrGLProgramBuilder::CreateProgram( |
| 63 |
GrDirectContext* dContext, |
| 64 |
const GrProgramDesc& desc, |
| 65 |
const GrProgramInfo& programInfo, |
| 66 |
const GrGLPrecompiledProgram* precompiledProgram) { |
| 67 |
TRACE_EVENT0_ALWAYS("skia.shaders", "shader_compile"); |
| 68 |
GrAutoLocaleSetter als("C"); |
| 69 |
|
| 70 |
GrGLGpu* glGpu = static_cast<GrGLGpu*>(dContext->priv().getGpu()); |
| 71 |
|
| 72 |
// create a builder. This will be handed off to effects so they can use it to add |
| 73 |
// uniforms, varyings, textures, etc |
| 74 |
GrGLProgramBuilder builder(glGpu, desc, programInfo); |
| 75 |
|
| 76 |
auto persistentCache = dContext->priv().getPersistentCache(); |
| 77 |
if (persistentCache && !precompiledProgram) { |
| 78 |
sk_sp<SkData> key = SkData::MakeWithoutCopy(desc.asKey(), desc.keyLength()); |
| 79 |
builder.fCached = persistentCache->load(*key); |
| 80 |
// the eventual end goal is to completely skip emitAndInstallProcs on a cache hit, but it's |
| 81 |
// doing necessary setup in addition to generating the SkSL code. Currently we are only able |
| 82 |
// to skip the SkSL->GLSL step on a cache hit. |
| 83 |
} |
| 84 |
if (!builder.emitAndInstallProcs()) { |
| 85 |
return nullptr; |
| 86 |
} |
| 87 |
return builder.finalize(precompiledProgram); |
| 88 |
} |
| 89 |
|
| 90 |
///////////////////////////////////////////////////////////////////////////// |
| 91 |
|
| 92 |
GrGLProgramBuilder::GrGLProgramBuilder(GrGLGpu* gpu, |
| 93 |
const GrProgramDesc& desc, |
| 94 |
const GrProgramInfo& programInfo) |
| 95 |
: INHERITED(desc, programInfo) |
| 96 |
, fGpu(gpu) |
| 97 |
, fVaryingHandler(this) |
| 98 |
, fUniformHandler(this) |
| 99 |
, fVertexAttributeCnt(0) |
| 100 |
, fInstanceAttributeCnt(0) |
| 101 |
, fVertexStride(0) |
| 102 |
, fInstanceStride(0) {} |
| 103 |
|
| 104 |
const GrCaps* GrGLProgramBuilder::caps() const { |
| 105 |
return fGpu->caps(); |
| 106 |
} |
| 107 |
|
| 108 |
bool GrGLProgramBuilder::compileAndAttachShaders(const SkSL::NativeShader& glsl, |
| 109 |
GrGLuint programId, |
| 110 |
GrGLenum type, |
| 111 |
SkTDArray<GrGLuint>* shaderIds, |
| 112 |
bool shaderWasCached, |
| 113 |
GrContextOptions::ShaderErrorHandler* errHandler) { |
| 114 |
GrGLGpu* gpu = this->gpu(); |
| 115 |
GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(), |
| 116 |
programId, |
| 117 |
type, |
| 118 |
glsl, |
| 119 |
shaderWasCached, |
| 120 |
gpu->pipelineBuilder()->stats(), |
| 121 |
errHandler); |
| 122 |
if (!shaderId) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
*shaderIds->append() = shaderId; |
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
void GrGLProgramBuilder::computeCountsAndStrides(GrGLuint programID, |
| 131 |
const GrGeometryProcessor& geomProc, |
| 132 |
bool bindAttribLocations) { |
| 133 |
fVertexAttributeCnt = geomProc.numVertexAttributes(); |
| 134 |
fInstanceAttributeCnt = geomProc.numInstanceAttributes(); |
| 135 |
fAttributes = std::make_unique<GrGLProgram::Attribute[]>( |
| 136 |
fVertexAttributeCnt + fInstanceAttributeCnt); |
| 137 |
auto addAttr = [&](int i, const auto& a) { |
| 138 |
fAttributes[i].fCPUType = a.cpuType(); |
| 139 |
fAttributes[i].fGPUType = a.gpuType(); |
| 140 |
fAttributes[i].fOffset = *a.offset(); |
| 141 |
fAttributes[i].fLocation = i; |
| 142 |
if (bindAttribLocations) { |
| 143 |
GL_CALL(BindAttribLocation(programID, i, a.name())); |
| 144 |
} |
| 145 |
}; |
| 146 |
fVertexStride = geomProc.vertexStride(); |
| 147 |
int i = 0; |
| 148 |
for (auto attr : geomProc.vertexAttributes()) { |
| 149 |
addAttr(i++, attr); |
| 150 |
} |
| 151 |
fInstanceStride = geomProc.instanceStride(); |
| 152 |
for (auto attr : geomProc.instanceAttributes()) { |
| 153 |
addAttr(i++, attr); |
| 154 |
} |
| 155 |
SkASSERT(fInstanceStride == geomProc.instanceStride()); |
| 156 |
} |
| 157 |
|
| 158 |
void GrGLProgramBuilder::addInputVars(const SkSL::Program::Interface& interface) { |
| 159 |
uint8_t useRTFlip = interface.fRTFlipUniform; |
| 160 |
if (!this->gpu()->glCaps().shaderCaps()->fCanUseFragCoord) { |
| 161 |
useRTFlip &= ~SkSL::Program::Interface::kRTFlip_FragCoord; |
| 162 |
} |
| 163 |
|
| 164 |
if (useRTFlip != SkSL::Program::Interface::kRTFlip_None) { |
| 165 |
this->addRTFlipUniform(SKSL_RTFLIP_NAME); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
static constexpr SkFourByteTag kSKSL_Tag = SkSetFourByteTag('S', 'K', 'S', 'L'); |
| 170 |
static constexpr SkFourByteTag kGLSL_Tag = SkSetFourByteTag('G', 'L', 'S', 'L'); |
| 171 |
static constexpr SkFourByteTag kGLPB_Tag = SkSetFourByteTag('G', 'L', 'P', 'B'); |
| 172 |
|
| 173 |
void GrGLProgramBuilder::storeShaderInCache(const SkSL::Program::Interface& interface, |
| 174 |
GrGLuint programID, |
| 175 |
const SkSL::NativeShader shaders[], |
| 176 |
bool isSkSL, |
| 177 |
SkSL::ProgramSettings* settings) { |
| 178 |
if (!this->gpu()->getContext()->priv().getPersistentCache()) { |
| 179 |
return; |
| 180 |
} |
| 181 |
sk_sp<SkData> key = SkData::MakeWithoutCopy(this->desc().asKey(), this->desc().keyLength()); |
| 182 |
SkString description = GrProgramDesc::Describe(fProgramInfo, *fGpu->caps()); |
| 183 |
if (fGpu->glCaps().programBinarySupport()) { |
| 184 |
// binary cache |
| 185 |
GrGLsizei length = 0; |
| 186 |
GL_CALL(GetProgramiv(programID, GL_PROGRAM_BINARY_LENGTH, &length)); |
| 187 |
if (length > 0) { |
| 188 |
SkBinaryWriteBuffer writer({}); |
| 189 |
writer.writeInt(GrPersistentCacheUtils::GetCurrentVersion()); |
| 190 |
writer.writeUInt(kGLPB_Tag); |
| 191 |
|
| 192 |
writer.writePad32(&interface, sizeof(interface)); |
| 193 |
|
| 194 |
SkAutoSMalloc<2048> binary(length); |
| 195 |
GrGLenum binaryFormat; |
| 196 |
GL_CALL(GetProgramBinary(programID, length, &length, &binaryFormat, binary.get())); |
| 197 |
|
| 198 |
writer.writeUInt(binaryFormat); |
| 199 |
writer.writeInt(length); |
| 200 |
writer.writePad32(binary.get(), length); |
| 201 |
|
| 202 |
auto data = writer.snapshotAsData(); |
| 203 |
this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data, description); |
| 204 |
} |
| 205 |
} else { |
| 206 |
// source cache, plus metadata to allow for a complete precompile |
| 207 |
GrPersistentCacheUtils::ShaderMetadata meta; |
| 208 |
meta.fSettings = settings; |
| 209 |
meta.fHasSecondaryColorOutput = fFS.hasSecondaryOutput(); |
| 210 |
for (auto attr : this->geometryProcessor().vertexAttributes()) { |
| 211 |
meta.fAttributeNames.emplace_back(attr.name()); |
| 212 |
} |
| 213 |
for (auto attr : this->geometryProcessor().instanceAttributes()) { |
| 214 |
meta.fAttributeNames.emplace_back(attr.name()); |
| 215 |
} |
| 216 |
|
| 217 |
auto data = GrPersistentCacheUtils::PackCachedShaders( |
| 218 |
isSkSL ? kSKSL_Tag : kGLSL_Tag, shaders, &interface, 1, &meta); |
| 219 |
this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data, description); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
sk_sp<GrGLProgram> GrGLProgramBuilder::finalize(const GrGLPrecompiledProgram* precompiledProgram) { |
| 224 |
TRACE_EVENT0("skia.shaders", TRACE_FUNC); |
| 225 |
|
| 226 |
// verify we can get a program id |
| 227 |
GrGLuint programID; |
| 228 |
if (precompiledProgram) { |
| 229 |
programID = precompiledProgram->fProgramID; |
| 230 |
} else { |
| 231 |
GL_CALL_RET(programID, CreateProgram()); |
| 232 |
} |
| 233 |
if (0 == programID) { |
| 234 |
return nullptr; |
| 235 |
} |
| 236 |
|
| 237 |
if (this->gpu()->glCaps().programBinarySupport() && |
| 238 |
this->gpu()->glCaps().programParameterSupport() && |
| 239 |
this->gpu()->getContext()->priv().getPersistentCache() && |
| 240 |
!precompiledProgram) { |
| 241 |
GL_CALL(ProgramParameteri(programID, GR_GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GR_GL_TRUE)); |
| 242 |
} |
| 243 |
|
| 244 |
this->finalizeShaders(); |
| 245 |
|
| 246 |
// compile shaders and bind attributes / uniforms |
| 247 |
auto errorHandler = this->gpu()->getContext()->priv().getShaderErrorHandler(); |
| 248 |
const GrGeometryProcessor& geomProc = this->geometryProcessor(); |
| 249 |
SkSL::ProgramSettings settings; |
| 250 |
settings.fSharpenTextures = |
| 251 |
this->gpu()->getContext()->priv().options().fSharpenMipmappedTextures; |
| 252 |
settings.fFragColorIsInOut = this->fragColorIsInOut(); |
| 253 |
|
| 254 |
SkSL::Program::Interface interface; |
| 255 |
SkTDArray<GrGLuint> shadersToDelete; |
| 256 |
|
| 257 |
bool cached = fCached.get() != nullptr; |
| 258 |
bool usedProgramBinaries = false; |
| 259 |
SkSL::NativeShader glsl[kGrShaderTypeCount]; |
| 260 |
const std::string* sksl[kGrShaderTypeCount] = { |
| 261 |
&fVS.fCompilerString, |
| 262 |
&fFS.fCompilerString, |
| 263 |
}; |
| 264 |
SkSL::NativeShader cached_sksl[kGrShaderTypeCount]; |
| 265 |
if (precompiledProgram) { |
| 266 |
// This is very similar to when we get program binaries. We even set that flag, as it's |
| 267 |
// used to prevent other compile work later, and to force re-querying uniform locations. |
| 268 |
this->addInputVars(precompiledProgram->fInterface); |
| 269 |
this->computeCountsAndStrides(programID, geomProc, false); |
| 270 |
usedProgramBinaries = true; |
| 271 |
} else if (cached) { |
| 272 |
TRACE_EVENT0_ALWAYS("skia.shaders", "cache_hit"); |
| 273 |
SkReadBuffer reader(fCached->data(), fCached->size()); |
| 274 |
SkFourByteTag shaderType = GrPersistentCacheUtils::GetType(&reader); |
| 275 |
|
| 276 |
switch (shaderType) { |
| 277 |
case kGLPB_Tag: { |
| 278 |
// Program binary cache hit. We may opt not to use this if we don't trust program |
| 279 |
// binaries on this driver |
| 280 |
if (!fGpu->glCaps().programBinarySupport()) { |
| 281 |
cached = false; |
| 282 |
break; |
| 283 |
} |
| 284 |
reader.readPad32(&interface, sizeof(interface)); |
| 285 |
GrGLenum binaryFormat = reader.readUInt(); |
| 286 |
GrGLsizei length = reader.readInt(); |
| 287 |
const void* binary = reader.skip(length); |
| 288 |
if (!reader.isValid()) { |
| 289 |
break; |
| 290 |
} |
| 291 |
if (length <= 0 || !fGpu->glCaps().programBinaryFormatIsValid(binaryFormat)) { |
| 292 |
cached = false; |
| 293 |
break; |
| 294 |
} |
| 295 |
GL_CALL(ProgramBinary(programID, binaryFormat, const_cast<void*>(binary), length)); |
| 296 |
// Pass nullptr for the error handler. We don't want to treat this as a compile |
| 297 |
// failure (we can still recover by compiling the program from source, below). |
| 298 |
// Clients won't be directly notified, but they can infer this from the trace |
| 299 |
// events, and from the traffic to the persistent cache. |
| 300 |
cached = GrGLCheckLinkStatus(fGpu, programID, /*shaderWasCached=*/true, |
| 301 |
/*errorHandler=*/nullptr, nullptr, nullptr); |
| 302 |
if (cached) { |
| 303 |
this->addInputVars(interface); |
| 304 |
this->computeCountsAndStrides(programID, geomProc, false); |
| 305 |
} |
| 306 |
usedProgramBinaries = cached; |
| 307 |
break; |
| 308 |
} |
| 309 |
|
| 310 |
case kGLSL_Tag: |
| 311 |
// Source cache hit, we don't need to compile the SkSL->GLSL |
| 312 |
GrPersistentCacheUtils::UnpackCachedShaders( |
| 313 |
&reader, glsl, /*areShadersBinary=*/false, &interface, 1); |
| 314 |
break; |
| 315 |
|
| 316 |
case kSKSL_Tag: |
| 317 |
// SkSL cache hit, this should only happen in tools overriding the generated SkSL |
| 318 |
if (GrPersistentCacheUtils::UnpackCachedShaders( |
| 319 |
&reader, cached_sksl, /*areShadersBinary=*/false, &interface, 1)) { |
| 320 |
for (int i = 0; i < kGrShaderTypeCount; ++i) { |
| 321 |
sksl[i] = &cached_sksl[i].fText; |
| 322 |
} |
| 323 |
} |
| 324 |
break; |
| 325 |
|
| 326 |
default: |
| 327 |
// We got something invalid, so pretend it wasn't there |
| 328 |
reader.validate(false); |
| 329 |
break; |
| 330 |
} |
| 331 |
if (!reader.isValid()) { |
| 332 |
cached = false; |
| 333 |
} |
| 334 |
} |
| 335 |
if (!usedProgramBinaries) { |
| 336 |
TRACE_EVENT0_ALWAYS("skia.shaders", "cache_miss"); |
| 337 |
// Either a cache miss, or we got something other than binaries from the cache |
| 338 |
|
| 339 |
/* |
| 340 |
Fragment Shader |
| 341 |
*/ |
| 342 |
if (glsl[kFragment_GrShaderType].fText.empty()) { |
| 343 |
// Don't have cached GLSL, need to compile SkSL->GLSL |
| 344 |
if (fFS.fForceHighPrecision) { |
| 345 |
settings.fForceHighPrecision = true; |
| 346 |
} |
| 347 |
if (!skgpu::SkSLToGLSL(this->gpu()->caps()->shaderCaps(), |
| 348 |
*sksl[kFragment_GrShaderType], |
| 349 |
SkSL::ProgramKind::kFragment, |
| 350 |
settings, |
| 351 |
&glsl[kFragment_GrShaderType], |
| 352 |
&interface, |
| 353 |
errorHandler)) { |
| 354 |
cleanup_program(fGpu, programID, shadersToDelete); |
| 355 |
return nullptr; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
this->addInputVars(interface); |
| 360 |
if (!this->compileAndAttachShaders(glsl[kFragment_GrShaderType], |
| 361 |
programID, |
| 362 |
GR_GL_FRAGMENT_SHADER, |
| 363 |
&shadersToDelete, |
| 364 |
cached, |
| 365 |
errorHandler)) { |
| 366 |
cleanup_program(fGpu, programID, shadersToDelete); |
| 367 |
return nullptr; |
| 368 |
} |
| 369 |
|
| 370 |
/* |
| 371 |
Vertex Shader |
| 372 |
*/ |
| 373 |
if (glsl[kVertex_GrShaderType].fText.empty()) { |
| 374 |
// Don't have cached GLSL, need to compile SkSL->GLSL |
| 375 |
SkSL::Program::Interface unusedInterface; |
| 376 |
if (!skgpu::SkSLToGLSL(this->gpu()->caps()->shaderCaps(), |
| 377 |
*sksl[kVertex_GrShaderType], |
| 378 |
SkSL::ProgramKind::kVertex, |
| 379 |
settings, |
| 380 |
&glsl[kVertex_GrShaderType], |
| 381 |
&unusedInterface, |
| 382 |
errorHandler)) { |
| 383 |
cleanup_program(fGpu, programID, shadersToDelete); |
| 384 |
return nullptr; |
| 385 |
} |
| 386 |
} |
| 387 |
if (!this->compileAndAttachShaders(glsl[kVertex_GrShaderType], |
| 388 |
programID, |
| 389 |
GR_GL_VERTEX_SHADER, |
| 390 |
&shadersToDelete, |
| 391 |
cached, |
| 392 |
errorHandler)) { |
| 393 |
cleanup_program(fGpu, programID, shadersToDelete); |
| 394 |
return nullptr; |
| 395 |
} |
| 396 |
|
| 397 |
// This also binds vertex attribute locations. |
| 398 |
this->computeCountsAndStrides(programID, geomProc, true); |
| 399 |
|
| 400 |
this->bindProgramResourceLocations(programID); |
| 401 |
|
| 402 |
{ |
| 403 |
TRACE_EVENT0_ALWAYS("skia.shaders", "driver_link_program"); |
| 404 |
GL_CALL(LinkProgram(programID)); |
| 405 |
if (!GrGLCheckLinkStatus(fGpu, programID, cached, errorHandler, sksl, glsl)) { |
| 406 |
cleanup_program(fGpu, programID, shadersToDelete); |
| 407 |
return nullptr; |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
this->resolveProgramResourceLocations(programID, usedProgramBinaries); |
| 412 |
|
| 413 |
cleanup_shaders(fGpu, shadersToDelete); |
| 414 |
|
| 415 |
// We can't cache SkSL or GLSL if we were given a precompiled program, but there's not |
| 416 |
// much point in doing so. |
| 417 |
if (!cached && !precompiledProgram) { |
| 418 |
bool isSkSL = false; |
| 419 |
if (fGpu->getContext()->priv().options().fShaderCacheStrategy == |
| 420 |
GrContextOptions::ShaderCacheStrategy::kSkSL) { |
| 421 |
for (int i = 0; i < kGrShaderTypeCount; ++i) { |
| 422 |
glsl[i].fText = SkShaderUtils::PrettyPrint(*sksl[i]); |
| 423 |
} |
| 424 |
isSkSL = true; |
| 425 |
} |
| 426 |
this->storeShaderInCache(interface, programID, glsl, isSkSL, &settings); |
| 427 |
} |
| 428 |
return this->createProgram(programID); |
| 429 |
} |
| 430 |
|
| 431 |
void GrGLProgramBuilder::bindProgramResourceLocations(GrGLuint programID) { |
| 432 |
fUniformHandler.bindUniformLocations(programID, fGpu->glCaps()); |
| 433 |
|
| 434 |
const GrGLCaps& caps = this->gpu()->glCaps(); |
| 435 |
if (caps.bindFragDataLocationSupport()) { |
| 436 |
SkASSERT(caps.shaderCaps()->mustDeclareFragmentShaderOutput()); |
| 437 |
GL_CALL(BindFragDataLocation(programID, 0, |
| 438 |
GrGLSLFragmentShaderBuilder::DeclaredColorOutputName())); |
| 439 |
if (fFS.hasSecondaryOutput()) { |
| 440 |
GL_CALL(BindFragDataLocationIndexed(programID, 0, 1, |
| 441 |
GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName())); |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
void GrGLProgramBuilder::resolveProgramResourceLocations(GrGLuint programID, bool force) { |
| 447 |
fUniformHandler.getUniformLocations(programID, fGpu->glCaps(), force); |
| 448 |
} |
| 449 |
|
| 450 |
sk_sp<GrGLProgram> GrGLProgramBuilder::createProgram(GrGLuint programID) { |
| 451 |
return GrGLProgram::Make(fGpu, |
| 452 |
fUniformHandles, |
| 453 |
programID, |
| 454 |
fUniformHandler.fUniforms, |
| 455 |
fUniformHandler.fSamplers, |
| 456 |
std::move(fGPImpl), |
| 457 |
std::move(fXPImpl), |
| 458 |
std::move(fFPImpls), |
| 459 |
std::move(fAttributes), |
| 460 |
fVertexAttributeCnt, |
| 461 |
fInstanceAttributeCnt, |
| 462 |
fVertexStride, |
| 463 |
fInstanceStride); |
| 464 |
} |
| 465 |
|
| 466 |
bool GrGLProgramBuilder::PrecompileProgram(GrDirectContext* dContext, |
| 467 |
GrGLPrecompiledProgram* precompiledProgram, |
| 468 |
const SkData& cachedData) { |
| 469 |
SkReadBuffer reader(cachedData.data(), cachedData.size()); |
| 470 |
SkFourByteTag shaderType = GrPersistentCacheUtils::GetType(&reader); |
| 471 |
if (shaderType != kSKSL_Tag) { |
| 472 |
// TODO: Support GLSL, and maybe even program binaries, too? |
| 473 |
return false; |
| 474 |
} |
| 475 |
|
| 476 |
GrGLGpu* glGpu = static_cast<GrGLGpu*>(dContext->priv().getGpu()); |
| 477 |
|
| 478 |
const GrGLInterface* gl = glGpu->glInterface(); |
| 479 |
auto errorHandler = dContext->priv().getShaderErrorHandler(); |
| 480 |
|
| 481 |
SkSL::ProgramSettings settings; |
| 482 |
settings.fSharpenTextures = dContext->priv().options().fSharpenMipmappedTextures; |
| 483 |
GrPersistentCacheUtils::ShaderMetadata meta; |
| 484 |
meta.fSettings = &settings; |
| 485 |
|
| 486 |
SkSL::NativeShader shaders[kGrShaderTypeCount]; |
| 487 |
SkSL::Program::Interface interface; |
| 488 |
if (!GrPersistentCacheUtils::UnpackCachedShaders( |
| 489 |
&reader, shaders, /*areShadersBinary=*/false, &interface, 1, &meta)) { |
| 490 |
return false; |
| 491 |
} |
| 492 |
|
| 493 |
GrGLuint programID; |
| 494 |
GR_GL_CALL_RET(gl, programID, CreateProgram()); |
| 495 |
if (0 == programID) { |
| 496 |
return false; |
| 497 |
} |
| 498 |
|
| 499 |
SkTDArray<GrGLuint> shadersToDelete; |
| 500 |
|
| 501 |
auto compileShader = [&](SkSL::ProgramKind kind, const std::string& sksl, GrGLenum type) { |
| 502 |
SkSL::NativeShader glsl; |
| 503 |
SkSL::Program::Interface unusedInterface; |
| 504 |
if (!skgpu::SkSLToGLSL(glGpu->caps()->shaderCaps(), |
| 505 |
sksl, |
| 506 |
kind, |
| 507 |
settings, |
| 508 |
&glsl, |
| 509 |
&unusedInterface, |
| 510 |
errorHandler)) { |
| 511 |
return false; |
| 512 |
} |
| 513 |
|
| 514 |
if (GrGLuint shaderID = GrGLCompileAndAttachShader(glGpu->glContext(), |
| 515 |
programID, |
| 516 |
type, |
| 517 |
glsl, |
| 518 |
/*shaderWasCached=*/false, |
| 519 |
glGpu->pipelineBuilder()->stats(), |
| 520 |
errorHandler)) { |
| 521 |
shadersToDelete.push_back(shaderID); |
| 522 |
return true; |
| 523 |
} else { |
| 524 |
return false; |
| 525 |
} |
| 526 |
}; |
| 527 |
|
| 528 |
if (!compileShader(SkSL::ProgramKind::kFragment, |
| 529 |
shaders[kFragment_GrShaderType].fText, |
| 530 |
GR_GL_FRAGMENT_SHADER) || |
| 531 |
!compileShader(SkSL::ProgramKind::kVertex, |
| 532 |
shaders[kVertex_GrShaderType].fText, |
| 533 |
GR_GL_VERTEX_SHADER)) { |
| 534 |
cleanup_program(glGpu, programID, shadersToDelete); |
| 535 |
return false; |
| 536 |
} |
| 537 |
|
| 538 |
for (int i = 0; i < meta.fAttributeNames.size(); ++i) { |
| 539 |
GR_GL_CALL(glGpu->glInterface(), BindAttribLocation(programID, i, |
| 540 |
meta.fAttributeNames[i].c_str())); |
| 541 |
} |
| 542 |
|
| 543 |
const GrGLCaps& caps = glGpu->glCaps(); |
| 544 |
if (caps.bindFragDataLocationSupport()) { |
| 545 |
SkASSERT(caps.shaderCaps()->mustDeclareFragmentShaderOutput()); |
| 546 |
GR_GL_CALL(glGpu->glInterface(), |
| 547 |
BindFragDataLocation(programID, 0, |
| 548 |
GrGLSLFragmentShaderBuilder::DeclaredColorOutputName())); |
| 549 |
|
| 550 |
if (meta.fHasSecondaryColorOutput) { |
| 551 |
GR_GL_CALL(glGpu->glInterface(), |
| 552 |
BindFragDataLocationIndexed(programID, 0, 1, |
| 553 |
GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName())); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
GR_GL_CALL(glGpu->glInterface(), LinkProgram(programID)); |
| 558 |
GrGLint linked = GR_GL_INIT_ZERO; |
| 559 |
GR_GL_CALL(glGpu->glInterface(), GetProgramiv(programID, GR_GL_LINK_STATUS, &linked)); |
| 560 |
if (!linked) { |
| 561 |
cleanup_program(glGpu, programID, shadersToDelete); |
| 562 |
return false; |
| 563 |
} |
| 564 |
|
| 565 |
cleanup_shaders(glGpu, shadersToDelete); |
| 566 |
|
| 567 |
precompiledProgram->fProgramID = programID; |
| 568 |
precompiledProgram->fInterface = interface; |
| 569 |
return true; |
| 570 |
} |