| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SKSL_SPIRVCODEGENERATOR | 8 #ifndef SKSL_SPIRVCODEGENERATOR |
| 9 #define SKSL_SPIRVCODEGENERATOR | 9 #define SKSL_SPIRVCODEGENERATOR |
| 10 | 10 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 | 54 |
| 55 // returns a pointer to the lvalue, if possible. If the lvalue cannot be
directly referenced | 55 // returns a pointer to the lvalue, if possible. If the lvalue cannot be
directly referenced |
| 56 // by a pointer (e.g. vector swizzles), returns 0. | 56 // by a pointer (e.g. vector swizzles), returns 0. |
| 57 virtual SpvId getPointer() = 0; | 57 virtual SpvId getPointer() = 0; |
| 58 | 58 |
| 59 virtual SpvId load(std::ostream& out) = 0; | 59 virtual SpvId load(std::ostream& out) = 0; |
| 60 | 60 |
| 61 virtual void store(SpvId value, std::ostream& out) = 0; | 61 virtual void store(SpvId value, std::ostream& out) = 0; |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 SPIRVCodeGenerator() | 64 SPIRVCodeGenerator(const Context* context) |
| 65 : fCapabilities(1 << SpvCapabilityShader) | 65 : fContext(*context) |
| 66 , fCapabilities(1 << SpvCapabilityShader) |
| 66 , fIdCount(1) | 67 , fIdCount(1) |
| 67 , fBoolTrue(0) | 68 , fBoolTrue(0) |
| 68 , fBoolFalse(0) | 69 , fBoolFalse(0) |
| 69 , fCurrentBlock(0) { | 70 , fCurrentBlock(0) { |
| 70 this->setupIntrinsics(); | 71 this->setupIntrinsics(); |
| 71 } | 72 } |
| 72 | 73 |
| 73 void generateCode(Program& program, std::ostream& out) override; | 74 void generateCode(Program& program, std::ostream& out) override; |
| 74 | 75 |
| 75 private: | 76 private: |
| 76 enum IntrinsicKind { | 77 enum IntrinsicKind { |
| 77 kGLSL_STD_450_IntrinsicKind, | 78 kGLSL_STD_450_IntrinsicKind, |
| 78 kSPIRV_IntrinsicKind, | 79 kSPIRV_IntrinsicKind, |
| 79 kSpecial_IntrinsicKind | 80 kSpecial_IntrinsicKind |
| 80 }; | 81 }; |
| 81 | 82 |
| 82 enum SpecialIntrinsic { | 83 enum SpecialIntrinsic { |
| 83 kAtan_SpecialIntrinsic, | 84 kAtan_SpecialIntrinsic, |
| 84 kTexture_SpecialIntrinsic, | 85 kTexture_SpecialIntrinsic, |
| 85 kTexture2D_SpecialIntrinsic, | 86 kTexture2D_SpecialIntrinsic, |
| 86 kTextureProj_SpecialIntrinsic | 87 kTextureProj_SpecialIntrinsic |
| 87 }; | 88 }; |
| 88 | 89 |
| 89 void setupIntrinsics(); | 90 void setupIntrinsics(); |
| 90 | 91 |
| 91 SpvId nextId(); | 92 SpvId nextId(); |
| 92 | 93 |
| 93 SpvId getType(const Type& type); | 94 SpvId getType(const Type& type); |
| 94 | 95 |
| 95 SpvId getFunctionType(std::shared_ptr<FunctionDeclaration> function); | 96 SpvId getFunctionType(const FunctionDeclaration& function); |
| 96 | 97 |
| 97 SpvId getPointerType(std::shared_ptr<Type> type, SpvStorageClass_ storageCla
ss); | 98 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass); |
| 98 | 99 |
| 99 std::vector<SpvId> getAccessChain(Expression& expr, std::ostream& out); | 100 std::vector<SpvId> getAccessChain(Expression& expr, std::ostream& out); |
| 100 | 101 |
| 101 void writeLayout(const Layout& layout, SpvId target); | 102 void writeLayout(const Layout& layout, SpvId target); |
| 102 | 103 |
| 103 void writeLayout(const Layout& layout, SpvId target, int member); | 104 void writeLayout(const Layout& layout, SpvId target, int member); |
| 104 | 105 |
| 105 void writeStruct(const Type& type, SpvId resultId); | 106 void writeStruct(const Type& type, SpvId resultId); |
| 106 | 107 |
| 107 void writeProgramElement(ProgramElement& pe, std::ostream& out); | 108 void writeProgramElement(ProgramElement& pe, std::ostream& out); |
| 108 | 109 |
| 109 SpvId writeInterfaceBlock(InterfaceBlock& intf); | 110 SpvId writeInterfaceBlock(InterfaceBlock& intf); |
| 110 | 111 |
| 111 SpvId writeFunctionStart(std::shared_ptr<FunctionDeclaration> f, std::ostrea
m& out); | 112 SpvId writeFunctionStart(const FunctionDeclaration& f, std::ostream& out); |
| 112 | 113 |
| 113 SpvId writeFunctionDeclaration(std::shared_ptr<FunctionDeclaration> f, std::
ostream& out); | 114 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, std::ostream& o
ut); |
| 114 | 115 |
| 115 SpvId writeFunction(FunctionDefinition& f, std::ostream& out); | 116 SpvId writeFunction(const FunctionDefinition& f, std::ostream& out); |
| 116 | 117 |
| 117 void writeGlobalVars(VarDeclaration& v, std::ostream& out); | 118 void writeGlobalVars(VarDeclaration& v, std::ostream& out); |
| 118 | 119 |
| 119 void writeVarDeclaration(VarDeclaration& decl, std::ostream& out); | 120 void writeVarDeclaration(VarDeclaration& decl, std::ostream& out); |
| 120 | 121 |
| 121 SpvId writeVariableReference(VariableReference& ref, std::ostream& out); | 122 SpvId writeVariableReference(VariableReference& ref, std::ostream& out); |
| 122 | 123 |
| 123 std::unique_ptr<LValue> getLValue(Expression& value, std::ostream& out); | 124 std::unique_ptr<LValue> getLValue(Expression& value, std::ostream& out); |
| 124 | 125 |
| 125 SpvId writeExpression(Expression& expr, std::ostream& out); | 126 SpvId writeExpression(Expression& expr, std::ostream& out); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t w
ord3, int32_t word4, | 221 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t w
ord3, int32_t word4, |
| 221 int32_t word5, int32_t word6, std::ostream& out); | 222 int32_t word5, int32_t word6, std::ostream& out); |
| 222 | 223 |
| 223 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t w
ord3, int32_t word4, | 224 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t w
ord3, int32_t word4, |
| 224 int32_t word5, int32_t word6, int32_t word7, std::ostr
eam& out); | 225 int32_t word5, int32_t word6, int32_t word7, std::ostr
eam& out); |
| 225 | 226 |
| 226 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t w
ord3, int32_t word4, | 227 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t w
ord3, int32_t word4, |
| 227 int32_t word5, int32_t word6, int32_t word7, int32_t w
ord8, | 228 int32_t word5, int32_t word6, int32_t word7, int32_t w
ord8, |
| 228 std::ostream& out); | 229 std::ostream& out); |
| 229 | 230 |
| 231 const Context& fContext; |
| 232 |
| 230 uint64_t fCapabilities; | 233 uint64_t fCapabilities; |
| 231 SpvId fIdCount; | 234 SpvId fIdCount; |
| 232 SpvId fGLSLExtendedInstructions; | 235 SpvId fGLSLExtendedInstructions; |
| 233 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrin
sic; | 236 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrin
sic; |
| 234 std::unordered_map<std::string, Intrinsic> fIntrinsicMap; | 237 std::unordered_map<std::string, Intrinsic> fIntrinsicMap; |
| 235 std::unordered_map<std::shared_ptr<FunctionDeclaration>, SpvId> fFunctionMap
; | 238 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap; |
| 236 std::unordered_map<std::shared_ptr<Variable>, SpvId> fVariableMap; | 239 std::unordered_map<const Variable*, SpvId> fVariableMap; |
| 237 std::unordered_map<std::shared_ptr<Variable>, int32_t> fInterfaceBlockMap; | 240 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap; |
| 238 std::unordered_map<std::string, SpvId> fTypeMap; | 241 std::unordered_map<std::string, SpvId> fTypeMap; |
| 239 std::stringstream fCapabilitiesBuffer; | 242 std::stringstream fCapabilitiesBuffer; |
| 240 std::stringstream fGlobalInitializersBuffer; | 243 std::stringstream fGlobalInitializersBuffer; |
| 241 std::stringstream fConstantBuffer; | 244 std::stringstream fConstantBuffer; |
| 242 std::stringstream fExternalFunctionsBuffer; | 245 std::stringstream fExternalFunctionsBuffer; |
| 243 std::stringstream fVariableBuffer; | 246 std::stringstream fVariableBuffer; |
| 244 std::stringstream fNameBuffer; | 247 std::stringstream fNameBuffer; |
| 245 std::stringstream fDecorationBuffer; | 248 std::stringstream fDecorationBuffer; |
| 246 | 249 |
| 247 SpvId fBoolTrue; | 250 SpvId fBoolTrue; |
| 248 SpvId fBoolFalse; | 251 SpvId fBoolFalse; |
| 249 std::unordered_map<int64_t, SpvId> fIntConstants; | 252 std::unordered_map<int64_t, SpvId> fIntConstants; |
| 250 std::unordered_map<uint64_t, SpvId> fUIntConstants; | 253 std::unordered_map<uint64_t, SpvId> fUIntConstants; |
| 251 std::unordered_map<float, SpvId> fFloatConstants; | 254 std::unordered_map<float, SpvId> fFloatConstants; |
| 252 std::unordered_map<double, SpvId> fDoubleConstants; | 255 std::unordered_map<double, SpvId> fDoubleConstants; |
| 253 // label of the current block, or 0 if we are not in a block | 256 // label of the current block, or 0 if we are not in a block |
| 254 SpvId fCurrentBlock; | 257 SpvId fCurrentBlock; |
| 255 std::stack<SpvId> fBreakTarget; | 258 std::stack<SpvId> fBreakTarget; |
| 256 std::stack<SpvId> fContinueTarget; | 259 std::stack<SpvId> fContinueTarget; |
| 257 | 260 |
| 258 friend class PointerLValue; | 261 friend class PointerLValue; |
| 259 friend class SwizzleLValue; | 262 friend class SwizzleLValue; |
| 260 }; | 263 }; |
| 261 | 264 |
| 262 } | 265 } |
| 263 | 266 |
| 264 #endif | 267 #endif |
| OLD | NEW |