• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class SkVMDebugTrace;
23 
24 class SkVMCallbacks {
25 public:
26     virtual ~SkVMCallbacks() = default;
27 
28     virtual skvm::Color sampleShader(int index, skvm::Coord coord) = 0;
29     virtual skvm::Color sampleColorFilter(int index, skvm::Color color) = 0;
30     virtual skvm::Color sampleBlender(int index, skvm::Color src, skvm::Color dst) = 0;
31 
32     virtual skvm::Color toLinearSrgb(skvm::Color color) = 0;
33     virtual skvm::Color fromLinearSrgb(skvm::Color color) = 0;
34 };
35 
36 // Convert 'function' to skvm instructions in 'builder', for use by blends, shaders, & color filters
37 skvm::Color ProgramToSkVM(const Program& program,
38                           const FunctionDefinition& function,
39                           skvm::Builder* builder,
40                           SkVMDebugTrace* debugTrace,
41                           SkSpan<skvm::Val> uniforms,
42                           skvm::Coord device,
43                           skvm::Coord local,
44                           skvm::Color inputColor,
45                           skvm::Color destColor,
46                           SkVMCallbacks* callbacks);
47 
48 struct SkVMSignature {
49     size_t fParameterSlots = 0;
50     size_t fReturnSlots    = 0;
51 };
52 
53 /*
54  * Converts 'function' to skvm instructions in 'builder'. Always adds one arg per value in the
55  * parameter list, then one per value in the return type. For example:
56  *
57  *   float2 fn(float2 a, float b) { ... }
58  *
59  * ... is mapped so that it can be called as:
60  *
61  *   p.eval(N, &a.x, &a.y, &b, &return.x, &return.y);
62  *
63  * The number of parameter and return slots (pointers) is placed in 'outSignature', if provided.
64  * If the program declares any uniforms, 'uniforms' should contain the IDs of each individual value
65  * (eg, one ID per component of a vector).
66  */
67 bool ProgramToSkVM(const Program& program,
68                    const FunctionDefinition& function,
69                    skvm::Builder* b,
70                    SkVMDebugTrace* debugTrace,
71                    SkSpan<skvm::Val> uniforms,
72                    SkVMSignature* outSignature = nullptr);
73 
74 const FunctionDefinition* Program_GetFunction(const Program& program, const char* function);
75 
76 struct UniformInfo {
77     struct Uniform {
78         std::string fName;
79         Type::NumberKind fKind;
80         int fColumns;
81         int fRows;
82         int fSlot;
83     };
84     std::vector<Uniform> fUniforms;
85     int fUniformSlotCount = 0;
86 };
87 
88 std::unique_ptr<UniformInfo> Program_GetUniformInfo(const Program& program);
89 
90 bool testingOnly_ProgramToSkVMShader(const Program& program,
91                                      skvm::Builder* builder,
92                                      SkVMDebugTrace* debugTrace);
93 
94 }  // namespace SkSL
95 
96 #endif
97