Skip to content

Commit ba9a04f

Browse files
Ethan NicholasSkia Commit-Bot
authored andcommitted
Revert "Revert "Additional SkSL benches""
This reverts commit 1277971. Change-Id: I7985ef22ddd19adcab468acc684b330ce6978c8d Reviewed-on: https://skia-review.googlesource.com/c/skia/+/332738 Commit-Queue: Ethan Nicholas <[email protected]> Commit-Queue: John Stiles <[email protected]> Auto-Submit: Ethan Nicholas <[email protected]> Reviewed-by: John Stiles <[email protected]>
1 parent 729a37f commit ba9a04f

File tree

9 files changed

+159
-47
lines changed

9 files changed

+159
-47
lines changed

bench/SkSLBench.cpp

Lines changed: 128 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
#include "bench/Benchmark.h"
88
#include "bench/ResultsWriter.h"
99
#include "bench/SkSLBench.h"
10+
#include "include/core/SkCanvas.h"
11+
#include "src/gpu/GrCaps.h"
12+
#include "src/gpu/GrRecordingContextPriv.h"
13+
#include "src/gpu/mock/GrMockCaps.h"
1014
#include "src/sksl/SkSLCompiler.h"
15+
#include "src/sksl/SkSLIRGenerator.h"
16+
#include "src/sksl/SkSLParser.h"
1117

1218
class SkSLCompilerStartupBench : public Benchmark {
1319
protected:
@@ -28,12 +34,37 @@ class SkSLCompilerStartupBench : public Benchmark {
2834

2935
DEF_BENCH(return new SkSLCompilerStartupBench();)
3036

31-
class SkSLBench : public Benchmark {
37+
enum class Output {
38+
kNone,
39+
kGLSL,
40+
kMetal,
41+
kSPIRV
42+
};
43+
44+
class SkSLCompileBench : public Benchmark {
3245
public:
33-
SkSLBench(SkSL::String name, const char* src)
34-
: fName("sksl_" + name)
46+
static const char* output_string(Output output) {
47+
switch (output) {
48+
case Output::kNone: return "";
49+
case Output::kGLSL: return "glsl_";
50+
case Output::kMetal: return "metal_";
51+
case Output::kSPIRV: return "spirv_";
52+
}
53+
SkUNREACHABLE;
54+
}
55+
56+
SkSLCompileBench(SkSL::String name, const char* src, bool optimize, Output output)
57+
: fName(SkSL::String("sksl_") + (optimize ? "" : "unoptimized_") + output_string(output) +
58+
name)
3559
, fSrc(src)
36-
, fCompiler(/*caps=*/nullptr) {}
60+
, fCaps(GrContextOptions(), GrMockOptions())
61+
, fCompiler(fCaps.shaderCaps())
62+
, fOutput(output) {
63+
fSettings.fOptimize = optimize;
64+
// The test programs we compile don't follow Vulkan rules and thus produce invalid
65+
// SPIR-V. This is harmless, so long as we don't try to validate them.
66+
fSettings.fValidateSPIRV = false;
67+
}
3768

3869
protected:
3970
const char* onGetName() override {
@@ -44,15 +75,67 @@ class SkSLBench : public Benchmark {
4475
return backend == kNonRendering_Backend;
4576
}
4677

47-
void onDraw(int loops, SkCanvas*) override {
78+
void onDraw(int loops, SkCanvas* canvas) override {
4879
for (int i = 0; i < loops; i++) {
4980
std::unique_ptr<SkSL::Program> program = fCompiler.convertProgram(
5081
SkSL::Program::kFragment_Kind,
5182
fSrc,
5283
fSettings);
5384
if (fCompiler.errorCount()) {
54-
printf("%s\n", fCompiler.errorText().c_str());
55-
SK_ABORT("shader compilation failed");
85+
SK_ABORT("shader compilation failed: %s\n", fCompiler.errorText().c_str());
86+
}
87+
SkSL::String result;
88+
switch (fOutput) {
89+
case Output::kNone: break;
90+
case Output::kGLSL: SkAssertResult(fCompiler.toGLSL(*program, &result)); break;
91+
case Output::kMetal: SkAssertResult(fCompiler.toMetal(*program, &result)); break;
92+
case Output::kSPIRV: SkAssertResult(fCompiler.toSPIRV(*program, &result)); break;
93+
}
94+
}
95+
}
96+
97+
private:
98+
SkSL::String fName;
99+
SkSL::String fSrc;
100+
GrMockCaps fCaps;
101+
SkSL::Compiler fCompiler;
102+
SkSL::Program::Settings fSettings;
103+
Output fOutput;
104+
105+
using INHERITED = Benchmark;
106+
};
107+
108+
class SkSLParseBench : public Benchmark {
109+
public:
110+
SkSLParseBench(SkSL::String name, const char* src)
111+
: fName("sksl_parse_" + name)
112+
, fSrc(src)
113+
, fCompiler(/*caps=*/nullptr) {}
114+
115+
protected:
116+
const char* onGetName() override {
117+
return fName.c_str();
118+
}
119+
120+
bool isSuitableFor(Backend backend) override {
121+
return backend == kNonRendering_Backend;
122+
}
123+
124+
void onDelayedSetup() override {
125+
SkSL::ParsedModule module = fCompiler.moduleForProgramKind(
126+
SkSL::Program::Kind::kFragment_Kind);
127+
fCompiler.irGenerator().setSymbolTable(module.fSymbols);
128+
}
129+
130+
void onDraw(int loops, SkCanvas*) override {
131+
for (int i = 0; i < loops; i++) {
132+
fCompiler.irGenerator().pushSymbolTable();
133+
SkSL::Parser parser(fSrc.c_str(), fSrc.length(), *fCompiler.irGenerator().symbolTable(),
134+
fCompiler);
135+
parser.compilationUnit();
136+
fCompiler.irGenerator().popSymbolTable();
137+
if (fCompiler.errorCount()) {
138+
SK_ABORT("shader compilation failed: %s\n", fCompiler.errorText().c_str());
56139
}
57140
}
58141
}
@@ -68,22 +151,33 @@ class SkSLBench : public Benchmark {
68151

69152
///////////////////////////////////////////////////////////////////////////////
70153

71-
DEF_BENCH(return new SkSLBench("large", R"(
72-
uniform half urange_Stage1;
73-
uniform half4 uleftBorderColor_Stage1_c0_c0;
74-
uniform half4 urightBorderColor_Stage1_c0_c0;
75-
uniform float3x3 umatrix_Stage1_c0_c0_c0;
76-
uniform half2 ufocalParams_Stage1_c0_c0_c0_c0;
77-
uniform float4 uscale0_1_Stage1_c0_c0_c1;
78-
uniform float4 uscale2_3_Stage1_c0_c0_c1;
79-
uniform float4 uscale4_5_Stage1_c0_c0_c1;
80-
uniform float4 uscale6_7_Stage1_c0_c0_c1;
81-
uniform float4 ubias0_1_Stage1_c0_c0_c1;
82-
uniform float4 ubias2_3_Stage1_c0_c0_c1;
83-
uniform float4 ubias4_5_Stage1_c0_c0_c1;
84-
uniform float4 ubias6_7_Stage1_c0_c0_c1;
85-
uniform half4 uthresholds1_7_Stage1_c0_c0_c1;
86-
uniform half4 uthresholds9_13_Stage1_c0_c0_c1;
154+
#define COMPILER_BENCH(name, text) \
155+
static constexpr char name ## _SRC[] = text; \
156+
DEF_BENCH(return new SkSLParseBench(#name, name ## _SRC);) \
157+
DEF_BENCH(return new SkSLCompileBench(#name, name ## _SRC, /*optimize=*/false, Output::kNone);) \
158+
DEF_BENCH(return new SkSLCompileBench(#name, name ## _SRC, /*optimize=*/true, Output::kNone);) \
159+
DEF_BENCH(return new SkSLCompileBench(#name, name ## _SRC, /*optimize=*/true, Output::kGLSL);) \
160+
DEF_BENCH(return new SkSLCompileBench(#name, name ## _SRC, /*optimize=*/true, Output::kMetal);) \
161+
DEF_BENCH(return new SkSLCompileBench(#name, name ## _SRC, /*optimize=*/true, Output::kSPIRV);)
162+
163+
// Metal requires a layout set and binding for all of its uniforms. We just care that these shaders
164+
// compile, not that they actually work, so we just fill them with zeroes.
165+
COMPILER_BENCH(large, R"(
166+
layout(set=0, binding=0) uniform half urange_Stage1;
167+
layout(set=0, binding=0) uniform half4 uleftBorderColor_Stage1_c0_c0;
168+
layout(set=0, binding=0) uniform half4 urightBorderColor_Stage1_c0_c0;
169+
layout(set=0, binding=0) uniform float3x3 umatrix_Stage1_c0_c0_c0;
170+
layout(set=0, binding=0) uniform half2 ufocalParams_Stage1_c0_c0_c0_c0;
171+
layout(set=0, binding=0) uniform float4 uscale0_1_Stage1_c0_c0_c1;
172+
layout(set=0, binding=0) uniform float4 uscale2_3_Stage1_c0_c0_c1;
173+
layout(set=0, binding=0) uniform float4 uscale4_5_Stage1_c0_c0_c1;
174+
layout(set=0, binding=0) uniform float4 uscale6_7_Stage1_c0_c0_c1;
175+
layout(set=0, binding=0) uniform float4 ubias0_1_Stage1_c0_c0_c1;
176+
layout(set=0, binding=0) uniform float4 ubias2_3_Stage1_c0_c0_c1;
177+
layout(set=0, binding=0) uniform float4 ubias4_5_Stage1_c0_c0_c1;
178+
layout(set=0, binding=0) uniform float4 ubias6_7_Stage1_c0_c0_c1;
179+
layout(set=0, binding=0) uniform half4 uthresholds1_7_Stage1_c0_c0_c1;
180+
layout(set=0, binding=0) uniform half4 uthresholds9_13_Stage1_c0_c0_c1;
87181
flat in half4 vcolor_Stage0;
88182
in float vcoverage_Stage0;
89183
flat in float4 vgeomSubset_Stage0;
@@ -347,12 +441,12 @@ void main()
347441
sk_FragColor = output_Stage1 * outputCoverage_Stage0;
348442
}
349443
}
350-
)");)
444+
)");
351445

352-
DEF_BENCH(return new SkSLBench("medium", R"(
353-
uniform half2 uDstTextureUpperLeft_Stage1;
354-
uniform half2 uDstTextureCoordScale_Stage1;
355-
uniform sampler2D uDstTextureSampler_Stage1;
446+
COMPILER_BENCH(medium, R"(
447+
layout(set=0, binding=0) uniform half2 uDstTextureUpperLeft_Stage1;
448+
layout(set=0, binding=0) uniform half2 uDstTextureCoordScale_Stage1;
449+
layout(set=0, binding=0) uniform sampler2D uDstTextureSampler_Stage1;
356450
noperspective in half4 vQuadEdge_Stage0;
357451
noperspective in half4 vinColor_Stage0;
358452
out half4 sk_FragColor;
@@ -414,11 +508,11 @@ DEF_BENCH(return new SkSLBench("medium", R"(
414508
(half4(1.0) - outputCoverage_Stage0) * _dstColor;
415509
}
416510
}
417-
)"); )
511+
)");
418512

419-
DEF_BENCH(return new SkSLBench("small", R"(
420-
uniform float3x3 umatrix_Stage1_c0_c0;
421-
uniform sampler2D uTextureSampler_0_Stage1;
513+
COMPILER_BENCH(small, R"(
514+
layout(set=0, binding=0) uniform float3x3 umatrix_Stage1_c0_c0;
515+
layout(set=0, binding=0) uniform sampler2D uTextureSampler_0_Stage1;
422516
noperspective in float2 vTransformedCoords_0_Stage0;
423517
out half4 sk_FragColor;
424518
half4 TextureEffect_Stage1_c0_c0_c0(half4 _input)
@@ -453,9 +547,9 @@ DEF_BENCH(return new SkSLBench("small", R"(
453547
sk_FragColor = output_Stage1 * outputCoverage_Stage0;
454548
}
455549
}
456-
)"); )
550+
)");
457551

458-
DEF_BENCH(return new SkSLBench("tiny", "void main() { sk_FragColor = half4(1); }"); )
552+
COMPILER_BENCH(tiny, "void main() { sk_FragColor = half4(1); }");
459553

460554
#if defined(SK_BUILD_FOR_UNIX)
461555

src/sksl/SkSLCompiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ bool Compiler::toSPIRV(Program& program, OutputStream& out) {
16551655
AutoSource as(this, program.fSource.get());
16561656
SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
16571657
bool result = cg.generateCode();
1658-
if (result) {
1658+
if (result && program.fSettings.fValidateSPIRV) {
16591659
spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
16601660
const String& data = buffer.str();
16611661
SkASSERT(0 == data.size() % 4);

src/sksl/SkSLCompiler.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#define SK_POSITION_BUILTIN 0
4343

4444
class SkBitSet;
45+
class SkSLCompileBench;
4546

4647
namespace SkSL {
4748

@@ -218,15 +219,19 @@ class SK_API Compiler : public ErrorReporter {
218219
LoadedModule loadModule(Program::Kind kind, ModuleData data, std::shared_ptr<SymbolTable> base);
219220
ParsedModule parseModule(Program::Kind kind, ModuleData data, const ParsedModule& base);
220221

222+
IRGenerator& irGenerator() {
223+
return *fIRGenerator;
224+
}
225+
226+
const ParsedModule& moduleForProgramKind(Program::Kind kind);
227+
221228
private:
222229
const ParsedModule& loadFPModule();
223230
const ParsedModule& loadGeometryModule();
224231
const ParsedModule& loadPublicModule();
225232
const ParsedModule& loadInterpreterModule();
226233
const ParsedModule& loadPipelineModule();
227234

228-
const ParsedModule& moduleForProgramKind(Program::Kind kind);
229-
230235
void addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
231236
DefinitionMap* definitions);
232237
void addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions);
@@ -295,6 +300,7 @@ class SK_API Compiler : public ErrorReporter {
295300
String fErrorText;
296301

297302
friend class AutoSource;
303+
friend class ::SkSLCompileBench;
298304
};
299305

300306
#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU

src/sksl/SkSLGLSLCodeGenerator.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,6 @@ void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) {
535535
case FunctionClass::kFwidth:
536536
if (!fFoundDerivatives &&
537537
fProgram.fCaps->shaderDerivativeExtensionString()) {
538-
SkASSERT(fProgram.fCaps->shaderDerivativeSupport());
539538
this->writeExtension(fProgram.fCaps->shaderDerivativeExtensionString());
540539
fFoundDerivatives = true;
541540
}
@@ -1485,8 +1484,10 @@ void GLSLCodeGenerator::writeReturnStatement(const ReturnStatement& r) {
14851484
}
14861485

14871486
void GLSLCodeGenerator::writeHeader() {
1488-
this->write(fProgram.fCaps->versionDeclString());
1489-
this->writeLine();
1487+
if (fProgram.fCaps->versionDeclString()) {
1488+
this->write(fProgram.fCaps->versionDeclString());
1489+
this->writeLine();
1490+
}
14901491
}
14911492

14921493
void GLSLCodeGenerator::writeProgramElement(const ProgramElement& e) {

src/sksl/SkSLIRGenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2993,7 +2993,7 @@ IRGenerator::IRBundle IRGenerator::convertProgram(
29932993
}
29942994

29952995
Parser parser(text, length, *fSymbolTable, fErrors);
2996-
fFile = parser.file();
2996+
fFile = parser.compilationUnit();
29972997
if (fErrors.errorCount()) {
29982998
return {};
29992999
}

src/sksl/SkSLIRGenerator.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ class IRGenerator {
133133

134134
const Program::Settings* settings() const { return fSettings; }
135135

136+
std::shared_ptr<SymbolTable>& symbolTable() {
137+
return fSymbolTable;
138+
}
139+
140+
void setSymbolTable(std::shared_ptr<SymbolTable>& symbolTable) {
141+
fSymbolTable = symbolTable;
142+
}
143+
144+
void pushSymbolTable();
145+
void popSymbolTable();
146+
136147
const Context& fContext;
137148

138149
private:
@@ -141,9 +152,6 @@ class IRGenerator {
141152
*/
142153
std::unique_ptr<ModifiersPool> releaseModifiers();
143154

144-
void pushSymbolTable();
145-
void popSymbolTable();
146-
147155
void checkModifiers(int offset, const Modifiers& modifiers, int permitted);
148156
StatementArray convertVarDeclarations(const ASTNode& decl, Variable::Storage storage);
149157
void convertFunction(const ASTNode& f);
@@ -242,7 +250,7 @@ class IRGenerator {
242250
int fRTAdjustFieldIndex;
243251
bool fCanInline = true;
244252
// true if we are currently processing one of the built-in SkSL include files
245-
bool fIsBuiltinCode;
253+
bool fIsBuiltinCode = false;
246254
std::unique_ptr<ModifiersPool> fModifiers;
247255

248256
friend class AutoSymbolTable;

src/sksl/SkSLParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Parser::Parser(const char* text, size_t length, SymbolTable& symbols, ErrorRepor
138138
} while (false)
139139

140140
/* (directive | section | declaration)* END_OF_FILE */
141-
std::unique_ptr<ASTFile> Parser::file() {
141+
std::unique_ptr<ASTFile> Parser::compilationUnit() {
142142
fFile = std::make_unique<ASTFile>();
143143
CREATE_NODE(result, 0, ASTNode::Kind::kFile);
144144
fFile->fRoot = result;

src/sksl/SkSLParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Parser {
9191
* Consumes a complete .sksl file and returns the parse tree. Errors are reported via the
9292
* ErrorReporter; the return value may contain some declarations even when errors have occurred.
9393
*/
94-
std::unique_ptr<ASTFile> file();
94+
std::unique_ptr<ASTFile> compilationUnit();
9595

9696
StringFragment text(Token token);
9797

src/sksl/ir/SkSLProgram.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ struct Program {
144144
// If true, implicit conversions to lower precision numeric types are allowed
145145
// (eg, float to half)
146146
bool fAllowNarrowingConversions = false;
147+
// If true, then Debug code will run SPIR-V output through the validator to ensure its
148+
// correctness
149+
bool fValidateSPIRV = true;
147150
};
148151

149152
struct Inputs {

0 commit comments

Comments
 (0)