blob: 03a94fa03f84061e6dd58479e429b83e7703dfb5 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Ethan Nicholas941e7e22016-12-12 15:33:30 -05007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#ifndef SKSL_PROGRAM
9#define SKSL_PROGRAM
10
11#include <vector>
12#include <memory>
13
Ethan Nicholas762466e2017-06-29 10:03:38 -040014#include "SkSLBoolLiteral.h"
15#include "SkSLExpression.h"
16#include "SkSLIntLiteral.h"
ethannicholas5961bc92016-10-12 06:39:56 -070017#include "SkSLModifiers.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070018#include "SkSLProgramElement.h"
ethannicholasd598f792016-07-25 10:08:54 -070019#include "SkSLSymbolTable.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070020
Ethan Nicholas941e7e22016-12-12 15:33:30 -050021// name of the render target height uniform
22#define SKSL_RTHEIGHT_NAME "u_skRTHeight"
23
ethannicholasb3058bd2016-07-01 08:22:01 -070024namespace SkSL {
25
Ethan Nicholas762466e2017-06-29 10:03:38 -040026class Context;
27
ethannicholasb3058bd2016-07-01 08:22:01 -070028/**
29 * Represents a fully-digested program, ready for code generation.
30 */
31struct Program {
Ethan Nicholas941e7e22016-12-12 15:33:30 -050032 struct Settings {
Ethan Nicholas762466e2017-06-29 10:03:38 -040033 struct Value {
34 Value(bool b)
35 : fKind(kBool_Kind)
36 , fValue(b) {}
37
38 Value(int i)
39 : fKind(kInt_Kind)
40 , fValue(i) {}
41
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070042 std::unique_ptr<Expression> literal(const Context& context, int offset) const {
Ethan Nicholas762466e2017-06-29 10:03:38 -040043 switch (fKind) {
44 case Program::Settings::Value::kBool_Kind:
45 return std::unique_ptr<Expression>(new BoolLiteral(context,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070046 offset,
Ethan Nicholas762466e2017-06-29 10:03:38 -040047 fValue));
48 case Program::Settings::Value::kInt_Kind:
49 return std::unique_ptr<Expression>(new IntLiteral(context,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070050 offset,
Ethan Nicholas762466e2017-06-29 10:03:38 -040051 fValue));
52 default:
53 ASSERT(false);
54 return nullptr;
55 }
56 }
57
58 enum {
59 kBool_Kind,
60 kInt_Kind,
61 } fKind;
62
63 int fValue;
64 };
65
Ethan Nicholas0df1b042017-03-31 13:56:23 -040066#ifdef SKSL_STANDALONE
67 const StandaloneShaderCaps* fCaps = &standaloneCaps;
68#else
Ethan Nicholas941e7e22016-12-12 15:33:30 -050069 const GrShaderCaps* fCaps = nullptr;
Ethan Nicholas0df1b042017-03-31 13:56:23 -040070#endif
Ethan Nicholas38657112017-02-09 17:01:22 -050071 // if false, sk_FragCoord is exactly the same as gl_FragCoord. If true, the y coordinate
72 // must be flipped.
Ethan Nicholas941e7e22016-12-12 15:33:30 -050073 bool fFlipY = false;
Brian Salomondc092132018-04-04 10:14:16 -040074 // If true the destination fragment color is read sk_FragColor. It must be declared inout.
75 bool fFragColorIsInOut = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -040076 // if true, Setting objects (e.g. sk_Caps.fbFetchSupport) should be replaced with their
77 // constant equivalents during compilation
78 bool fReplaceSettings = true;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040079 // if true, all halfs are forced to be floats
80 bool fForceHighPrecision = false;
Brian Osman8a83ca42018-02-12 14:32:17 -050081 // if true, add -0.5 bias to LOD of all texture lookups
82 bool fSharpenTextures = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -040083 std::unordered_map<String, Value> fArgs;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050084 };
85
86 struct Inputs {
87 // if true, this program requires the render target height uniform to be defined
88 bool fRTHeight;
89
Ethan Nicholas38657112017-02-09 17:01:22 -050090 // if true, this program must be recompiled if the flipY setting changes. If false, the
91 // program will compile to the same code regardless of the flipY setting.
92 bool fFlipY;
93
Ethan Nicholas941e7e22016-12-12 15:33:30 -050094 void reset() {
95 fRTHeight = false;
Ethan Nicholas38657112017-02-09 17:01:22 -050096 fFlipY = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050097 }
98
99 bool isEmpty() {
Ethan Nicholas38657112017-02-09 17:01:22 -0500100 return !fRTHeight && !fFlipY;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500101 }
102 };
103
ethannicholasb3058bd2016-07-01 08:22:01 -0700104 enum Kind {
105 kFragment_Kind,
Ethan Nicholas52cad152017-02-16 16:37:32 -0500106 kVertex_Kind,
Ethan Nicholas762466e2017-06-29 10:03:38 -0400107 kGeometry_Kind,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400108 kFragmentProcessor_Kind,
109 kCPU_Kind
ethannicholasb3058bd2016-07-01 08:22:01 -0700110 };
111
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500112 Program(Kind kind,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700113 std::unique_ptr<String> source,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500114 Settings settings,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400115 std::shared_ptr<Context> context,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500116 std::vector<std::unique_ptr<ProgramElement>> elements,
117 std::shared_ptr<SymbolTable> symbols,
118 Inputs inputs)
119 : fKind(kind)
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700120 , fSource(std::move(source))
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500121 , fSettings(settings)
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500122 , fContext(context)
Ethan Nicholas6415e0d2017-01-19 16:31:32 +0000123 , fSymbols(symbols)
Ethan Nicholas86a43402017-01-19 13:32:00 -0500124 , fElements(std::move(elements))
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500125 , fInputs(inputs) {}
ethannicholasb3058bd2016-07-01 08:22:01 -0700126
127 Kind fKind;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700128 std::unique_ptr<String> fSource;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500129 Settings fSettings;
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400130 std::shared_ptr<Context> fContext;
Ethan Nicholas86a43402017-01-19 13:32:00 -0500131 // it's important to keep fElements defined after (and thus destroyed before) fSymbols,
132 // because destroying elements can modify reference counts in symbols
Ethan Nicholas6415e0d2017-01-19 16:31:32 +0000133 std::shared_ptr<SymbolTable> fSymbols;
Ethan Nicholas86a43402017-01-19 13:32:00 -0500134 std::vector<std::unique_ptr<ProgramElement>> fElements;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500135 Inputs fInputs;
ethannicholasb3058bd2016-07-01 08:22:01 -0700136};
137
138} // namespace
139
140#endif