Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSLCodeGenerator.h
Go to the documentation of this file.
1/*
2 * Copyright 2016 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#ifndef SKSL_CODEGENERATOR
9#define SKSL_CODEGENERATOR
10
14
15namespace SkSL {
16
17struct ShaderCaps;
18
19/**
20 * Abstract superclass of all code generators, which take a Program as input and produce code as
21 * output.
22 */
24public:
25 CodeGenerator(const Context* context,
26 const ShaderCaps* caps,
27 const Program* program,
28 OutputStream* stream)
29 : fProgram(*program)
30 , fContext(fProgram.fContext->fTypes, *fProgram.fContext->fErrors)
31 , fCaps(*caps)
32 , fOut(stream) {
36 }
37
38 virtual ~CodeGenerator() = default;
39
40 virtual bool generateCode() = 0;
41
42 // Intended for use by AutoOutputStream.
44 void setOutputStream(OutputStream* output) { fOut = output; }
45
46protected:
47#if defined(SK_USE_LEGACY_MIPMAP_LOD_BIAS)
48 static constexpr float kSharpenTexturesBias = -.5f;
49#else
50 // For SkMipmapMode::kLinear we want a bias such that when the unbiased LOD value is
51 // midway between levels we select just the larger level, i.e. a bias of -.5. However, using
52 // this bias with kNearest mode with a draw that is a perfect power of two downscale puts us
53 // right on the rounding edge where it could go up or down depending on the particular GPU.
54 // Experimentally we found that at -.49 most iOS devices (iPhone 7, 8, and iPad Pro
55 // [PowerVRGT7800 version]) all round to the level twice as big as the device space footprint
56 // for some such draws in our unit tests on GLES. However, the iPhone 11 still fails and so
57 // we are using -.475. They do not at -.48. All other GPUs passed tests with -.499. Though, at
58 // this time the bias is not implemented in the MSL codegen and so iOS/Metal was not tested.
59 static constexpr float kSharpenTexturesBias = -.475f;
60#endif
61
66};
67
69public:
70 // Maintains the current indentation level while writing to the new output stream.
72 : fCodeGen(codeGen)
73 , fOldOutput(codeGen->outputStream()) {
74 fCodeGen->setOutputStream(newOutput);
75 }
76 // Resets the indentation when entering the scope, and restores it when leaving.
77 AutoOutputStream(CodeGenerator* codeGen, OutputStream* newOutput, int *indentationPtr)
78 : fCodeGen(codeGen)
79 , fOldOutput(codeGen->outputStream())
80 , fIndentationPtr(indentationPtr)
81 , fOldIndentation(indentationPtr ? *indentationPtr : 0) {
82 fCodeGen->setOutputStream(newOutput);
83 *fIndentationPtr = 0;
84 }
86 fCodeGen->setOutputStream(fOldOutput);
87 if (fIndentationPtr) {
88 *fIndentationPtr = fOldIndentation;
89 }
90 }
91
92private:
93 CodeGenerator* fCodeGen = nullptr;
94 OutputStream* fOldOutput = nullptr;
95 int *fIndentationPtr = nullptr;
96 int fOldIndentation = 0;
97};
98
99} // namespace SkSL
100
101#endif
AutoOutputStream(CodeGenerator *codeGen, OutputStream *newOutput, int *indentationPtr)
AutoOutputStream(CodeGenerator *codeGen, OutputStream *newOutput)
void setOutputStream(OutputStream *output)
virtual ~CodeGenerator()=default
OutputStream * outputStream()
static constexpr float kSharpenTexturesBias
const ShaderCaps & fCaps
const Program & fProgram
CodeGenerator(const Context *context, const ShaderCaps *caps, const Program *program, OutputStream *stream)
virtual bool generateCode()=0
const Module * fModule
Definition SkSLContext.h:44
SymbolTable * fSymbolTable
Definition SkSLContext.h:48
ProgramConfig * fConfig
Definition SkSLContext.h:33
std::shared_ptr< Context > fContext
std::unique_ptr< SymbolTable > fSymbols
std::unique_ptr< ProgramConfig > fConfig