Support global variables in pipeline stage generator
Bug: skia:11295 Change-Id: Iec11f3f4d26eb5b1c07707b3cedd09096bad80d0 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/371478 Commit-Queue: Brian Osman <brianosman@google.com> Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
parent
af68258c2e
commit
5e603c3a46
@ -35,6 +35,7 @@ bool FuzzSKSL2Pipeline(sk_sp<SkData> bytes) {
|
||||
|
||||
void defineFunction(const char* /*decl*/, const char* /*body*/, bool /*isMain*/) override {}
|
||||
void defineStruct(const char* /*definition*/) override {}
|
||||
void declareGlobal(const char* /*declaration*/) override {}
|
||||
|
||||
String sampleChild(int index, String coords) override {
|
||||
return SkSL::String::printf("sample(%d%s%s)", index, coords.empty() ? "" : ", ",
|
||||
|
@ -87,6 +87,10 @@ public:
|
||||
fArgs.fFragBuilder->definitionAppend(definition);
|
||||
}
|
||||
|
||||
void declareGlobal(const char* declaration) override {
|
||||
fArgs.fFragBuilder->definitionAppend(declaration);
|
||||
}
|
||||
|
||||
String sampleChild(int index, String coords) override {
|
||||
return String(fSelf->invokeChild(index, fArgs, coords).c_str());
|
||||
}
|
||||
|
@ -423,6 +423,10 @@ ResultCode processCommand(std::vector<SkSL::String>& args) {
|
||||
fOutput += definition;
|
||||
}
|
||||
|
||||
void declareGlobal(const char* declaration) override {
|
||||
fOutput += declaration;
|
||||
}
|
||||
|
||||
String sampleChild(int index, String coords) override {
|
||||
return String::printf("sample(%s%s%s)", fChildNames[index].c_str(),
|
||||
coords.empty() ? "" : ", ", coords.c_str());
|
||||
|
@ -110,7 +110,7 @@ private:
|
||||
const char* fSampleCoords;
|
||||
Callbacks* fCallbacks;
|
||||
|
||||
std::unordered_map<const Variable*, String> fUniformNames;
|
||||
std::unordered_map<const Variable*, String> fVariableNames;
|
||||
std::unordered_map<const FunctionDeclaration*, String> fFunctionNames;
|
||||
std::unordered_map<const Type*, String> fStructNames;
|
||||
|
||||
@ -231,15 +231,16 @@ void PipelineStageCodeGenerator::writeVariableReference(const VariableReference&
|
||||
return index;
|
||||
};
|
||||
|
||||
if (modifiers.fFlags & Modifiers::kUniform_Flag) {
|
||||
auto it = fUniformNames.find(var);
|
||||
SkASSERT(it != fUniformNames.end());
|
||||
this->write(it->second);
|
||||
} else if (modifiers.fFlags & Modifiers::kVarying_Flag) {
|
||||
if (modifiers.fFlags & Modifiers::kVarying_Flag) {
|
||||
this->write("_vtx_attr_");
|
||||
this->write(to_string(varIndexByFlag(Modifiers::kVarying_Flag)));
|
||||
} else {
|
||||
this->write(var->name());
|
||||
auto it = fVariableNames.find(var);
|
||||
if (it != fVariableNames.end()) {
|
||||
this->write(it->second);
|
||||
} else {
|
||||
this->write(var->name());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -331,11 +332,23 @@ void PipelineStageCodeGenerator::writeGlobalVarDeclaration(const GlobalVarDeclar
|
||||
const VarDeclaration& decl = g.declaration()->as<VarDeclaration>();
|
||||
const Variable& var = decl.var();
|
||||
|
||||
if (var.modifiers().fFlags & Modifiers::kUniform_Flag) {
|
||||
if (var.isBuiltin()) {
|
||||
// Don't mangle the name or re-declare this. (eg, sk_FragCoord)
|
||||
} else if (var.modifiers().fFlags & Modifiers::kUniform_Flag) {
|
||||
String uniformName = fCallbacks->declareUniform(&decl);
|
||||
fUniformNames.insert({&var, std::move(uniformName)});
|
||||
fVariableNames.insert({&var, std::move(uniformName)});
|
||||
} else {
|
||||
// TODO: Handle non-uniform global variable declarations. (skbug.com/11295)
|
||||
String mangledName = fCallbacks->getMangledName(String(var.name()).c_str());
|
||||
String declaration = this->typedVariable(var.type(), StringFragment(mangledName.c_str()));
|
||||
if (decl.value()) {
|
||||
AutoOutputBuffer outputToBuffer(this);
|
||||
this->writeExpression(*decl.value(), Precedence::kTopLevel);
|
||||
declaration += " = ";
|
||||
declaration += outputToBuffer.fBuffer.str();
|
||||
}
|
||||
declaration += ";\n";
|
||||
fCallbacks->declareGlobal(declaration.c_str());
|
||||
fVariableNames.insert({&var, std::move(mangledName)});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,7 @@ namespace PipelineStage {
|
||||
virtual String getMangledName(const char* name) { return name; }
|
||||
virtual void defineFunction(const char* declaration, const char* body, bool isMain) = 0;
|
||||
virtual void defineStruct(const char* definition) = 0;
|
||||
virtual void declareGlobal(const char* declaration) = 0;
|
||||
|
||||
virtual String declareUniform(const VarDeclaration*) = 0;
|
||||
virtual String sampleChild(int index, String coords) = 0;
|
||||
|
@ -1,9 +1,12 @@
|
||||
float gInitialized_0 = -1.0;
|
||||
float gInitializedFromOther_0 = -gInitialized_0;
|
||||
float gUninitialized_0;
|
||||
void init_globals_0()
|
||||
{
|
||||
gUninitialized = gInitialized + 2.0;
|
||||
gUninitialized_0 = gInitialized_0 + 2.0;
|
||||
}
|
||||
float4 main()
|
||||
{
|
||||
init_globals_0();
|
||||
return half4(float4(0.0, gInitializedFromOther, 0.0, gUninitialized));
|
||||
return half4(float4(0.0, gInitializedFromOther_0, 0.0, gUninitialized_0));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user