blob: 3a13d7a44b41782c3af6c53831df3a6160b7f485 [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
Ethan Nicholasdaed2592021-03-04 14:30:25 -050011#include "include/private/SkSLString.h"
Brian Osman0a442b72020-12-02 11:12:51 -050012#include "src/core/SkSpan.h"
13#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
23using 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
29skvm::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,
Brian Osman577c6062021-04-12 17:17:19 -040035 skvm::Color inputColor,
Brian Osman0a442b72020-12-02 11:12:51 -050036 SampleChildFn sampleChild);
37
Brian Osmanf4a77732020-12-28 09:03:00 -050038struct SkVMSignature {
39 size_t fParameterSlots = 0;
40 size_t fReturnSlots = 0;
41};
42
43/*
Brian Osmanc92df392021-01-11 13:16:28 -050044 * 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 Osmanf4a77732020-12-28 09:03:00 -050046 *
Brian Osmanf4a77732020-12-28 09:03:00 -050047 * float2 fn(float2 a, float b) { ... }
48 *
49 * ... is mapped so that it can be called as:
50 *
Brian Osmanc92df392021-01-11 13:16:28 -050051 * p.eval(N, &a.x, &a.y, &b, &return.x, &return.y);
Brian Osmanf4a77732020-12-28 09:03:00 -050052 *
53 * The number of parameter and return slots (pointers) is placed in 'outSignature', if provided.
Brian Osmanc92df392021-01-11 13:16:28 -050054 * 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 Osmanf4a77732020-12-28 09:03:00 -050056 */
57bool ProgramToSkVM(const Program& program,
58 const FunctionDefinition& function,
59 skvm::Builder* b,
Brian Osmanc92df392021-01-11 13:16:28 -050060 SkSpan<skvm::Val> uniforms,
Brian Osmanf4a77732020-12-28 09:03:00 -050061 SkVMSignature* outSignature = nullptr);
62
Brian Osman5933d4c2021-01-05 13:02:20 -050063const FunctionDefinition* Program_GetFunction(const Program& program, const char* function);
64
Brian Osmane89d8ea2021-01-20 14:01:30 -050065struct 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
77std::unique_ptr<UniformInfo> Program_GetUniformInfo(const Program& program);
78
Brian Osman47726a12020-12-17 16:02:08 -050079bool testingOnly_ProgramToSkVMShader(const Program& program, skvm::Builder* builder);
80
Brian Osman0a442b72020-12-02 11:12:51 -050081} // namespace SkSL
82
83#endif