blob: fe30cc22933382bde1908cd3b22e70c108c15d1b [file] [log] [blame]
* Copyright 2021 Google LLC.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
#include "src/sksl/transform/SkSLTransform.h"
#include "include/private/SkSLProgramKind.h"
#include "src/sksl/SkSLBuiltinMap.h"
#include "src/sksl/SkSLCompiler.h"
#include "src/sksl/SkSLContext.h"
#include "src/sksl/SkSLThreadContext.h"
#include "src/sksl/analysis/SkSLProgramVisitor.h"
#include "src/sksl/ir/SkSLFunctionDefinition.h"
#include "src/sksl/ir/SkSLInterfaceBlock.h"
#include "src/sksl/ir/SkSLVarDeclarations.h"
namespace SkSL {
namespace Transform {
void FindAndDeclareBuiltinVariables(const Context& context,
ProgramKind programKind, std::vector<const ProgramElement*>& sharedElements) {
class BuiltinVariableScanner : public ProgramVisitor {
BuiltinVariableScanner(const Context& context)
: fContext(context) {}
void addDeclaringElement(const String& name) {
// If this is the *first* time we've seen this builtin, findAndInclude will return
// the corresponding ProgramElement.
BuiltinMap& builtins = *fContext.fBuiltins;
if (const ProgramElement* decl = builtins.findAndInclude(name)) {
SkASSERT(decl->is<GlobalVarDeclaration>() || decl->is<InterfaceBlock>());
bool visitProgramElement(const ProgramElement& pe) override {
if (<FunctionDefinition>()) {
const FunctionDefinition& funcDef =<FunctionDefinition>();
// We synthesize writes to sk_FragColor if main() returns a color, even if it's
// otherwise unreferenced. Check main's return type to see if it's half4.
if (funcDef.declaration().isMain() &&
funcDef.declaration().returnType().matches(*fContext.fTypes.fHalf4)) {
fPreserveFragColor = true;
return INHERITED::visitProgramElement(pe);
bool visitExpression(const Expression& e) override {
if (<VariableReference>() &&<VariableReference>().variable()->isBuiltin()) {
return INHERITED::visitExpression(e);
const Context& fContext;
std::vector<const ProgramElement*> fNewElements;
bool fPreserveFragColor = false;
using INHERITED = ProgramVisitor;
using INHERITED::visitProgramElement;
BuiltinVariableScanner scanner(context);
for (auto& e : ThreadContext::ProgramElements()) {
if (scanner.fPreserveFragColor) {
// main() returns a half4, so make sure we don't dead-strip sk_FragColor.
switch (programKind) {
case ProgramKind::kFragment:
// Vulkan requires certain builtin variables be present, even if they're unused. At one
// time, validation errors would result if sk_Clockwise was missing. Now, it's just
// (Adreno) driver bugs that drop or corrupt draws if they're missing.
sharedElements.insert(sharedElements.begin(), scanner.fNewElements.begin(),
} // namespace Transform
} // namespace SkSL