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 | |
John Stiles | d9a56b9 | 2021-07-23 15:50:39 -0400 | [diff] [blame^] | 23 | using SampleShaderFn = std::function<skvm::Color(int, skvm::Coord)>; |
John Stiles | 137482f | 2021-07-23 10:38:57 -0400 | [diff] [blame] | 24 | using SampleColorFilterFn = std::function<skvm::Color(int, skvm::Color)>; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 25 | |
John Stiles | 50d0d09 | 2021-06-09 17:24:31 -0400 | [diff] [blame] | 26 | // Convert 'function' to skvm instructions in 'builder', for use by blends, shaders, & color filters |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 27 | skvm::Color ProgramToSkVM(const Program& program, |
| 28 | const FunctionDefinition& function, |
| 29 | skvm::Builder* builder, |
| 30 | SkSpan<skvm::Val> uniforms, |
| 31 | skvm::Coord device, |
| 32 | skvm::Coord local, |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 33 | skvm::Color inputColor, |
John Stiles | 50d0d09 | 2021-06-09 17:24:31 -0400 | [diff] [blame] | 34 | skvm::Color destColor, |
John Stiles | 137482f | 2021-07-23 10:38:57 -0400 | [diff] [blame] | 35 | SampleShaderFn sampleShader, |
| 36 | SampleColorFilterFn sampleColorFilter); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 37 | |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 38 | struct SkVMSignature { |
| 39 | size_t fParameterSlots = 0; |
| 40 | size_t fReturnSlots = 0; |
| 41 | }; |
| 42 | |
| 43 | /* |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 44 | * Converts 'function' to skvm instructions in 'builder'. Always adds one arg per value in the |
| 45 | * parameter list, then one per value in the return type. For example: |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 46 | * |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 47 | * float2 fn(float2 a, float b) { ... } |
| 48 | * |
| 49 | * ... is mapped so that it can be called as: |
| 50 | * |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 51 | * p.eval(N, &a.x, &a.y, &b, &return.x, &return.y); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 52 | * |
| 53 | * 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] | 54 | * If the program declares any uniforms, 'uniforms' should contain the IDs of each individual value |
| 55 | * (eg, one ID per component of a vector). |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 56 | */ |
| 57 | bool ProgramToSkVM(const Program& program, |
| 58 | const FunctionDefinition& function, |
| 59 | skvm::Builder* b, |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 60 | SkSpan<skvm::Val> uniforms, |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 61 | SkVMSignature* outSignature = nullptr); |
| 62 | |
Brian Osman | 5933d4c | 2021-01-05 13:02:20 -0500 | [diff] [blame] | 63 | const FunctionDefinition* Program_GetFunction(const Program& program, const char* function); |
| 64 | |
Brian Osman | e89d8ea | 2021-01-20 14:01:30 -0500 | [diff] [blame] | 65 | struct UniformInfo { |
| 66 | struct Uniform { |
| 67 | String fName; |
| 68 | Type::NumberKind fKind; |
| 69 | int fColumns; |
| 70 | int fRows; |
| 71 | int fSlot; |
| 72 | }; |
| 73 | std::vector<Uniform> fUniforms; |
| 74 | int fUniformSlotCount = 0; |
| 75 | }; |
| 76 | |
| 77 | std::unique_ptr<UniformInfo> Program_GetUniformInfo(const Program& program); |
| 78 | |
Brian Osman | 47726a1 | 2020-12-17 16:02:08 -0500 | [diff] [blame] | 79 | bool testingOnly_ProgramToSkVMShader(const Program& program, skvm::Builder* builder); |
| 80 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 81 | } // namespace SkSL |
| 82 | |
| 83 | #endif |