1 /* 2 * Copyright 2020 Google LLC 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_VMGENERATOR 9 #define SKSL_VMGENERATOR 10 11 #include "include/core/SkSpan.h" 12 #include "include/private/SkSLString.h" 13 #include "src/core/SkVM.h" 14 #include "src/sksl/ir/SkSLType.h" 15 16 #include <functional> 17 18 namespace SkSL { 19 20 class FunctionDefinition; 21 struct Program; 22 23 using SampleChildFn = std::function<skvm::Color(int, skvm::Coord, skvm::Color)>; 24 25 // Convert 'function' to skvm instructions in 'builder', for use by shaders and color filters 26 skvm::Color ProgramToSkVM(const Program& program, 27 const FunctionDefinition& function, 28 skvm::Builder* builder, 29 SkSpan<skvm::Val> uniforms, 30 skvm::Coord device, 31 skvm::Coord local, 32 skvm::Color inputColor, 33 SampleChildFn sampleChild); 34 35 struct SkVMSignature { 36 size_t fParameterSlots = 0; 37 size_t fReturnSlots = 0; 38 }; 39 40 /* 41 * Converts 'function' to skvm instructions in 'builder'. Always adds one arg per value in the 42 * parameter list, then one per value in the return type. For example: 43 * 44 * float2 fn(float2 a, float b) { ... } 45 * 46 * ... is mapped so that it can be called as: 47 * 48 * p.eval(N, &a.x, &a.y, &b, &return.x, &return.y); 49 * 50 * The number of parameter and return slots (pointers) is placed in 'outSignature', if provided. 51 * If the program declares any uniforms, 'uniforms' should contain the IDs of each individual value 52 * (eg, one ID per component of a vector). 53 */ 54 bool ProgramToSkVM(const Program& program, 55 const FunctionDefinition& function, 56 skvm::Builder* b, 57 SkSpan<skvm::Val> uniforms, 58 SkVMSignature* outSignature = nullptr); 59 60 const FunctionDefinition* Program_GetFunction(const Program& program, const char* function); 61 62 struct UniformInfo { 63 struct Uniform { 64 String fName; 65 Type::NumberKind fKind; 66 int fColumns; 67 int fRows; 68 int fSlot; 69 }; 70 std::vector<Uniform> fUniforms; 71 int fUniformSlotCount = 0; 72 }; 73 74 std::unique_ptr<UniformInfo> Program_GetUniformInfo(const Program& program); 75 76 bool testingOnly_ProgramToSkVMShader(const Program& program, skvm::Builder* builder); 77 78 } // namespace SkSL 79 80 #endif 81