blob: 743a5808e5b1bd65a05e136f05f9baf0b2026cef [file] [log] [blame]
Brian Osman0a442b72020-12-02 11:12:51 -05001/*
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 Osmana5842bc2021-05-11 13:41:46 -040011#include "include/core/SkSpan.h"
Ethan Nicholasdaed2592021-03-04 14:30:25 -050012#include "include/private/SkSLString.h"
Brian Osman0a442b72020-12-02 11:12:51 -050013#include "src/core/SkVM.h"
Brian Osmane89d8ea2021-01-20 14:01:30 -050014#include "src/sksl/ir/SkSLType.h"
Brian Osman0a442b72020-12-02 11:12:51 -050015
16#include <functional>
17
18namespace SkSL {
19
20class FunctionDefinition;
21struct Program;
22
John Stilesd9a56b92021-07-23 15:50:39 -040023using SampleShaderFn = std::function<skvm::Color(int, skvm::Coord)>;
John Stiles137482f2021-07-23 10:38:57 -040024using SampleColorFilterFn = std::function<skvm::Color(int, skvm::Color)>;
John Stiles2955c262021-07-23 15:51:05 -040025using SampleBlenderFn = std::function<skvm::Color(int, skvm::Color, skvm::Color)>;
Brian Osman0a442b72020-12-02 11:12:51 -050026
John Stiles50d0d092021-06-09 17:24:31 -040027// Convert 'function' to skvm instructions in 'builder', for use by blends, shaders, & color filters
Brian Osman0a442b72020-12-02 11:12:51 -050028skvm::Color ProgramToSkVM(const Program& program,
29 const FunctionDefinition& function,
30 skvm::Builder* builder,
31 SkSpan<skvm::Val> uniforms,
32 skvm::Coord device,
33 skvm::Coord local,
Brian Osman577c6062021-04-12 17:17:19 -040034 skvm::Color inputColor,
John Stiles50d0d092021-06-09 17:24:31 -040035 skvm::Color destColor,
John Stiles137482f2021-07-23 10:38:57 -040036 SampleShaderFn sampleShader,
John Stiles2955c262021-07-23 15:51:05 -040037 SampleColorFilterFn sampleColorFilter,
38 SampleBlenderFn sampleBlender);
Brian Osman0a442b72020-12-02 11:12:51 -050039
Brian Osmanf4a77732020-12-28 09:03:00 -050040struct SkVMSignature {
41 size_t fParameterSlots = 0;
42 size_t fReturnSlots = 0;
43};
44
45/*
Brian Osmanc92df392021-01-11 13:16:28 -050046 * Converts 'function' to skvm instructions in 'builder'. Always adds one arg per value in the
47 * parameter list, then one per value in the return type. For example:
Brian Osmanf4a77732020-12-28 09:03:00 -050048 *
Brian Osmanf4a77732020-12-28 09:03:00 -050049 * float2 fn(float2 a, float b) { ... }
50 *
51 * ... is mapped so that it can be called as:
52 *
Brian Osmanc92df392021-01-11 13:16:28 -050053 * p.eval(N, &a.x, &a.y, &b, &return.x, &return.y);
Brian Osmanf4a77732020-12-28 09:03:00 -050054 *
55 * The number of parameter and return slots (pointers) is placed in 'outSignature', if provided.
Brian Osmanc92df392021-01-11 13:16:28 -050056 * If the program declares any uniforms, 'uniforms' should contain the IDs of each individual value
57 * (eg, one ID per component of a vector).
Brian Osmanf4a77732020-12-28 09:03:00 -050058 */
59bool ProgramToSkVM(const Program& program,
60 const FunctionDefinition& function,
61 skvm::Builder* b,
Brian Osmanc92df392021-01-11 13:16:28 -050062 SkSpan<skvm::Val> uniforms,
Brian Osmanf4a77732020-12-28 09:03:00 -050063 SkVMSignature* outSignature = nullptr);
64
Brian Osman5933d4c2021-01-05 13:02:20 -050065const FunctionDefinition* Program_GetFunction(const Program& program, const char* function);
66
Brian Osmane89d8ea2021-01-20 14:01:30 -050067struct UniformInfo {
68 struct Uniform {
69 String fName;
70 Type::NumberKind fKind;
71 int fColumns;
72 int fRows;
73 int fSlot;
74 };
75 std::vector<Uniform> fUniforms;
76 int fUniformSlotCount = 0;
77};
78
79std::unique_ptr<UniformInfo> Program_GetUniformInfo(const Program& program);
80
Brian Osman47726a12020-12-17 16:02:08 -050081bool testingOnly_ProgramToSkVMShader(const Program& program, skvm::Builder* builder);
82
Brian Osman0a442b72020-12-02 11:12:51 -050083} // namespace SkSL
84
85#endif