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 | |
Brian Osman | a5842bc | 2021-05-11 13:41:46 -0400 | [diff] [blame^] | 11 | #include "include/core/SkSpan.h" |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 12 | #include "include/private/SkSLString.h" |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 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 | |
Brian Osman | c9125aa | 2021-04-21 09:57:19 -0400 | [diff] [blame] | 23 | using SampleChildFn = std::function<skvm::Color(int, skvm::Coord, skvm::Color)>; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 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, |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 32 | skvm::Color inputColor, |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 33 | SampleChildFn sampleChild); |
| 34 | |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 35 | struct SkVMSignature { |
| 36 | size_t fParameterSlots = 0; |
| 37 | size_t fReturnSlots = 0; |
| 38 | }; |
| 39 | |
| 40 | /* |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 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: |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 43 | * |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 44 | * float2 fn(float2 a, float b) { ... } |
| 45 | * |
| 46 | * ... is mapped so that it can be called as: |
| 47 | * |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 48 | * p.eval(N, &a.x, &a.y, &b, &return.x, &return.y); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 49 | * |
| 50 | * 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] | 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). |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 53 | */ |
| 54 | bool ProgramToSkVM(const Program& program, |
| 55 | const FunctionDefinition& function, |
| 56 | skvm::Builder* b, |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 57 | SkSpan<skvm::Val> uniforms, |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 58 | SkVMSignature* outSignature = nullptr); |
| 59 | |
Brian Osman | 5933d4c | 2021-01-05 13:02:20 -0500 | [diff] [blame] | 60 | const FunctionDefinition* Program_GetFunction(const Program& program, const char* function); |
| 61 | |
Brian Osman | e89d8ea | 2021-01-20 14:01:30 -0500 | [diff] [blame] | 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 | |
Brian Osman | 47726a1 | 2020-12-17 16:02:08 -0500 | [diff] [blame] | 76 | bool testingOnly_ProgramToSkVMShader(const Program& program, skvm::Builder* builder); |
| 77 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 78 | } // namespace SkSL |
| 79 | |
| 80 | #endif |