Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 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 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame^] | 11 | #include "include/private/SkSLString.h" |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 12 | #include "src/core/SkSpan.h" |
| 13 | #include "src/core/SkVM.h" |
Brian Osman | e89d8ea | 2021-01-20 14:01:30 -0500 | [diff] [blame] | 14 | #include "src/sksl/ir/SkSLType.h" |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 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)>; |
| 24 | |
| 25 | // TODO: Have a generic entry point, supporting SkSpan<skvm::Val> for parameters and return values. |
| 26 | // That would be useful for interpreter use cases like SkParticleEffect. |
| 27 | |
| 28 | // Convert 'function' to skvm instructions in 'builder', for use by shaders and color filters |
| 29 | skvm::Color ProgramToSkVM(const Program& program, |
| 30 | const FunctionDefinition& function, |
| 31 | skvm::Builder* builder, |
| 32 | SkSpan<skvm::Val> uniforms, |
| 33 | skvm::Coord device, |
| 34 | skvm::Coord local, |
| 35 | SampleChildFn sampleChild); |
| 36 | |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 37 | struct SkVMSignature { |
| 38 | size_t fParameterSlots = 0; |
| 39 | size_t fReturnSlots = 0; |
| 40 | }; |
| 41 | |
| 42 | /* |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 43 | * Converts 'function' to skvm instructions in 'builder'. Always adds one arg per value in the |
| 44 | * parameter list, then one per value in the return type. For example: |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 45 | * |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 46 | * float2 fn(float2 a, float b) { ... } |
| 47 | * |
| 48 | * ... is mapped so that it can be called as: |
| 49 | * |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 50 | * p.eval(N, &a.x, &a.y, &b, &return.x, &return.y); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 51 | * |
| 52 | * The number of parameter and return slots (pointers) is placed in 'outSignature', if provided. |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 53 | * If the program declares any uniforms, 'uniforms' should contain the IDs of each individual value |
| 54 | * (eg, one ID per component of a vector). |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 55 | */ |
| 56 | bool ProgramToSkVM(const Program& program, |
| 57 | const FunctionDefinition& function, |
| 58 | skvm::Builder* b, |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 59 | SkSpan<skvm::Val> uniforms, |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 60 | SkVMSignature* outSignature = nullptr); |
| 61 | |
Brian Osman | 5933d4c | 2021-01-05 13:02:20 -0500 | [diff] [blame] | 62 | const FunctionDefinition* Program_GetFunction(const Program& program, const char* function); |
| 63 | |
Brian Osman | e89d8ea | 2021-01-20 14:01:30 -0500 | [diff] [blame] | 64 | struct UniformInfo { |
| 65 | struct Uniform { |
| 66 | String fName; |
| 67 | Type::NumberKind fKind; |
| 68 | int fColumns; |
| 69 | int fRows; |
| 70 | int fSlot; |
| 71 | }; |
| 72 | std::vector<Uniform> fUniforms; |
| 73 | int fUniformSlotCount = 0; |
| 74 | }; |
| 75 | |
| 76 | std::unique_ptr<UniformInfo> Program_GetUniformInfo(const Program& program); |
| 77 | |
Brian Osman | 47726a1 | 2020-12-17 16:02:08 -0500 | [diff] [blame] | 78 | bool testingOnly_ProgramToSkVMShader(const Program& program, skvm::Builder* builder); |
| 79 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 80 | } // namespace SkSL |
| 81 | |
| 82 | #endif |