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_PROGRAM 9 #define SKSL_PROGRAM 10 11 #include <vector> 12 #include <memory> 13 14 #include "SkSLBoolLiteral.h" 15 #include "SkSLExpression.h" 16 #include "SkSLIntLiteral.h" 17 #include "SkSLModifiers.h" 18 #include "SkSLProgramElement.h" 19 #include "SkSLSymbolTable.h" 20 21 // name of the render target height uniform 22 #define SKSL_RTHEIGHT_NAME "u_skRTHeight" 23 24 namespace SkSL { 25 26 class Context; 27 28 /** 29 * Represents a fully-digested program, ready for code generation. 30 */ 31 struct Program { 32 struct Settings { 33 struct Value { ValueProgram::Settings::Value34 Value(bool b) 35 : fKind(kBool_Kind) 36 , fValue(b) {} 37 ValueProgram::Settings::Value38 Value(int i) 39 : fKind(kInt_Kind) 40 , fValue(i) {} 41 literalProgram::Settings::Value42 std::unique_ptr<Expression> literal(const Context& context, Position position) const { 43 switch (fKind) { 44 case Program::Settings::Value::kBool_Kind: 45 return std::unique_ptr<Expression>(new BoolLiteral(context, 46 position, 47 fValue)); 48 case Program::Settings::Value::kInt_Kind: 49 return std::unique_ptr<Expression>(new IntLiteral(context, 50 position, 51 fValue)); 52 default: 53 ASSERT(false); 54 return nullptr; 55 } 56 } 57 58 enum { 59 kBool_Kind, 60 kInt_Kind, 61 } fKind; 62 63 int fValue; 64 }; 65 66 #ifdef SKSL_STANDALONE 67 const StandaloneShaderCaps* fCaps = &standaloneCaps; 68 #else 69 const GrShaderCaps* fCaps = nullptr; 70 #endif 71 // if false, sk_FragCoord is exactly the same as gl_FragCoord. If true, the y coordinate 72 // must be flipped. 73 bool fFlipY = false; 74 // if true, Setting objects (e.g. sk_Caps.fbFetchSupport) should be replaced with their 75 // constant equivalents during compilation 76 bool fReplaceSettings = true; 77 std::unordered_map<String, Value> fArgs; 78 }; 79 80 struct Inputs { 81 // if true, this program requires the render target height uniform to be defined 82 bool fRTHeight; 83 84 // if true, this program must be recompiled if the flipY setting changes. If false, the 85 // program will compile to the same code regardless of the flipY setting. 86 bool fFlipY; 87 resetProgram::Inputs88 void reset() { 89 fRTHeight = false; 90 fFlipY = false; 91 } 92 isEmptyProgram::Inputs93 bool isEmpty() { 94 return !fRTHeight && !fFlipY; 95 } 96 }; 97 98 enum Kind { 99 kFragment_Kind, 100 kVertex_Kind, 101 kGeometry_Kind, 102 kFragmentProcessor_Kind 103 }; 104 ProgramProgram105 Program(Kind kind, 106 Settings settings, 107 Modifiers::Flag defaultPrecision, 108 Context* context, 109 std::vector<std::unique_ptr<ProgramElement>> elements, 110 std::shared_ptr<SymbolTable> symbols, 111 Inputs inputs) 112 : fKind(kind) 113 , fSettings(settings) 114 , fDefaultPrecision(defaultPrecision) 115 , fContext(context) 116 , fSymbols(symbols) 117 , fElements(std::move(elements)) 118 , fInputs(inputs) {} 119 120 Kind fKind; 121 Settings fSettings; 122 // FIXME handle different types; currently it assumes this is for floats 123 Modifiers::Flag fDefaultPrecision; 124 Context* fContext; 125 // it's important to keep fElements defined after (and thus destroyed before) fSymbols, 126 // because destroying elements can modify reference counts in symbols 127 std::shared_ptr<SymbolTable> fSymbols; 128 std::vector<std::unique_ptr<ProgramElement>> fElements; 129 Inputs fInputs; 130 }; 131 132 } // namespace 133 134 #endif 135