aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/ir/SkSLLayout.h
blob: 3082b341335f2dad60576f31f0411f07f1a35745 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SKSL_LAYOUT
#define SKSL_LAYOUT

#include "SkSLString.h"
#include "SkSLUtil.h"

namespace SkSL {

/**
 * Represents a layout block appearing before a variable declaration, as in:
 *
 * layout (location = 0) int x;
 */
struct Layout {
    enum Flag {
        kOriginUpperLeft_Flag            = 1 <<  0,
        kOverrideCoverage_Flag           = 1 <<  1,
        kPushConstant_Flag               = 1 <<  2,
        kBlendSupportAllEquations_Flag   = 1 <<  3,
        kBlendSupportMultiply_Flag       = 1 <<  4,
        kBlendSupportScreen_Flag         = 1 <<  5,
        kBlendSupportOverlay_Flag        = 1 <<  6,
        kBlendSupportDarken_Flag         = 1 <<  7,
        kBlendSupportLighten_Flag        = 1 <<  8,
        kBlendSupportColorDodge_Flag     = 1 <<  9,
        kBlendSupportColorBurn_Flag      = 1 << 10,
        kBlendSupportHardLight_Flag      = 1 << 11,
        kBlendSupportSoftLight_Flag      = 1 << 12,
        kBlendSupportDifference_Flag     = 1 << 13,
        kBlendSupportExclusion_Flag      = 1 << 14,
        kBlendSupportHSLHue_Flag         = 1 << 15,
        kBlendSupportHSLSaturation_Flag  = 1 << 16,
        kBlendSupportHSLColor_Flag       = 1 << 17,
        kBlendSupportHSLLuminosity_Flag  = 1 << 18
    };

    enum Primitive {
        kUnspecified_Primitive = -1,
        kPoints_Primitive,
        kLines_Primitive,
        kLineStrip_Primitive,
        kLinesAdjacency_Primitive,
        kTriangles_Primitive,
        kTriangleStrip_Primitive,
        kTrianglesAdjacency_Primitive
    };

    // These are used by images in GLSL. We only support a subset of what GL supports.
    enum class Format {
        kUnspecified = -1,
        kRGBA32F,
        kR32F,
        kRGBA16F,
        kR16F,
        kRGBA8,
        kR8,
        kRGBA8I,
        kR8I,
    };

    // used by SkSL processors
    enum Key {
        // field is not a key
        kNo_Key,
        // field is a key
        kKey_Key,
        // key is 0 or 1 depending on whether the matrix is an identity matrix
        kIdentity_Key,
    };

    static const char* FormatToStr(Format format) {
        switch (format) {
            case Format::kUnspecified:  return "";
            case Format::kRGBA32F:      return "rgba32f";
            case Format::kR32F:         return "r32f";
            case Format::kRGBA16F:      return "rgba16f";
            case Format::kR16F:         return "r16f";
            case Format::kRGBA8:        return "rgba8";
            case Format::kR8:           return "r8";
            case Format::kRGBA8I:       return "rgba8i";
            case Format::kR8I:          return "r8i";
        }
        ABORT("Unexpected format");
    }

    static bool ReadFormat(String str, Format* format) {
        if (str == "rgba32f") {
            *format = Format::kRGBA32F;
            return true;
        } else if (str == "r32f") {
            *format = Format::kR32F;
            return true;
        } else if (str == "rgba16f") {
            *format = Format::kRGBA16F;
            return true;
        } else if (str == "r16f") {
            *format = Format::kR16F;
            return true;
        } else if (str == "rgba8") {
            *format = Format::kRGBA8;
            return true;
        } else if (str == "r8") {
            *format = Format::kR8;
            return true;
        } else if (str == "rgba8i") {
            *format = Format::kRGBA8I;
            return true;
        } else if (str == "r8i") {
            *format = Format::kR8I;
            return true;
        }
        return false;
    }

    Layout(int flags, int location, int offset, int binding, int index, int set, int builtin,
           int inputAttachmentIndex, Format format, Primitive primitive, int maxVertices,
           int invocations, String when, Key key, StringFragment ctype)
    : fFlags(flags)
    , fLocation(location)
    , fOffset(offset)
    , fBinding(binding)
    , fIndex(index)
    , fSet(set)
    , fBuiltin(builtin)
    , fInputAttachmentIndex(inputAttachmentIndex)
    , fFormat(format)
    , fPrimitive(primitive)
    , fMaxVertices(maxVertices)
    , fInvocations(invocations)
    , fWhen(when)
    , fKey(key)
    , fCType(ctype) {}

    Layout()
    : fFlags(0)
    , fLocation(-1)
    , fOffset(-1)
    , fBinding(-1)
    , fIndex(-1)
    , fSet(-1)
    , fBuiltin(-1)
    , fInputAttachmentIndex(-1)
    , fFormat(Format::kUnspecified)
    , fPrimitive(kUnspecified_Primitive)
    , fMaxVertices(-1)
    , fInvocations(-1)
    , fKey(kNo_Key) {}

    String description() const {
        String result;
        String separator;
        if (fLocation >= 0) {
            result += separator + "location = " + to_string(fLocation);
            separator = ", ";
        }
        if (fOffset >= 0) {
            result += separator + "offset = " + to_string(fOffset);
            separator = ", ";
        }
        if (fBinding >= 0) {
            result += separator + "binding = " + to_string(fBinding);
            separator = ", ";
        }
        if (fIndex >= 0) {
            result += separator + "index = " + to_string(fIndex);
            separator = ", ";
        }
        if (fSet >= 0) {
            result += separator + "set = " + to_string(fSet);
            separator = ", ";
        }
        if (fBuiltin >= 0) {
            result += separator + "builtin = " + to_string(fBuiltin);
            separator = ", ";
        }
        if (fInputAttachmentIndex >= 0) {
            result += separator + "input_attachment_index = " + to_string(fBuiltin);
            separator = ", ";
        }
        if (Format::kUnspecified != fFormat) {
            result += separator + FormatToStr(fFormat);
            separator = ", ";
        }
        if (fFlags & kOriginUpperLeft_Flag) {
            result += separator + "origin_upper_left";
            separator = ", ";
        }
        if (fFlags & kOverrideCoverage_Flag) {
            result += separator + "override_coverage";
            separator = ", ";
        }
        if (fFlags & kBlendSupportAllEquations_Flag) {
            result += separator + "blend_support_all_equations";
            separator = ", ";
        }
        if (fFlags & kBlendSupportMultiply_Flag) {
            result += separator + "blend_support_multiply";
            separator = ", ";
        }
        if (fFlags & kBlendSupportScreen_Flag) {
            result += separator + "blend_support_screen";
            separator = ", ";
        }
        if (fFlags & kBlendSupportOverlay_Flag) {
            result += separator + "blend_support_overlay";
            separator = ", ";
        }
        if (fFlags & kBlendSupportDarken_Flag) {
            result += separator + "blend_support_darken";
            separator = ", ";
        }
        if (fFlags & kBlendSupportLighten_Flag) {
            result += separator + "blend_support_lighten";
            separator = ", ";
        }
        if (fFlags & kBlendSupportColorDodge_Flag) {
            result += separator + "blend_support_colordodge";
            separator = ", ";
        }
        if (fFlags & kBlendSupportColorBurn_Flag) {
            result += separator + "blend_support_colorburn";
            separator = ", ";
        }
        if (fFlags & kBlendSupportHardLight_Flag) {
            result += separator + "blend_support_hardlight";
            separator = ", ";
        }
        if (fFlags & kBlendSupportSoftLight_Flag) {
            result += separator + "blend_support_softlight";
            separator = ", ";
        }
        if (fFlags & kBlendSupportDifference_Flag) {
            result += separator + "blend_support_difference";
            separator = ", ";
        }
        if (fFlags & kBlendSupportExclusion_Flag) {
            result += separator + "blend_support_exclusion";
            separator = ", ";
        }
        if (fFlags & kBlendSupportHSLHue_Flag) {
            result += separator + "blend_support_hsl_hue";
            separator = ", ";
        }
        if (fFlags & kBlendSupportHSLSaturation_Flag) {
            result += separator + "blend_support_hsl_saturation";
            separator = ", ";
        }
        if (fFlags & kBlendSupportHSLColor_Flag) {
            result += separator + "blend_support_hsl_color";
            separator = ", ";
        }
        if (fFlags & kBlendSupportHSLLuminosity_Flag) {
            result += separator + "blend_support_hsl_luminosity";
            separator = ", ";
        }
        if (fFlags & kPushConstant_Flag) {
            result += separator + "push_constant";
            separator = ", ";
        }
        switch (fPrimitive) {
            case kPoints_Primitive:
                result += separator + "points";
                separator = ", ";
                break;
            case kLines_Primitive:
                result += separator + "lines";
                separator = ", ";
                break;
            case kLineStrip_Primitive:
                result += separator + "line_strip";
                separator = ", ";
                break;
            case kLinesAdjacency_Primitive:
                result += separator + "lines_adjacency";
                separator = ", ";
                break;
            case kTriangles_Primitive:
                result += separator + "triangles";
                separator = ", ";
                break;
            case kTriangleStrip_Primitive:
                result += separator + "triangle_strip";
                separator = ", ";
                break;
            case kTrianglesAdjacency_Primitive:
                result += separator + "triangles_adjacency";
                separator = ", ";
                break;
            case kUnspecified_Primitive:
                break;
        }
        if (fMaxVertices >= 0) {
            result += separator + "max_vertices = " + to_string(fMaxVertices);
            separator = ", ";
        }
        if (fInvocations >= 0) {
            result += separator + "invocations = " + to_string(fInvocations);
            separator = ", ";
        }
        if (fWhen.size()) {
            result += separator + "when = " + fWhen;
            separator = ", ";
        }
        if (result.size() > 0) {
            result = "layout (" + result + ")";
        }
        return result;
    }

    bool operator==(const Layout& other) const {
        return fFlags                == other.fFlags &&
               fLocation             == other.fLocation &&
               fOffset               == other.fOffset &&
               fBinding              == other.fBinding &&
               fIndex                == other.fIndex &&
               fSet                  == other.fSet &&
               fBuiltin              == other.fBuiltin &&
               fInputAttachmentIndex == other.fInputAttachmentIndex &&
               fFormat               == other.fFormat &&
               fPrimitive            == other.fPrimitive &&
               fMaxVertices          == other.fMaxVertices &&
               fInvocations          == other.fInvocations;
    }

    bool operator!=(const Layout& other) const {
        return !(*this == other);
    }

    int fFlags;
    int fLocation;
    int fOffset;
    int fBinding;
    int fIndex;
    int fSet;
    // builtin comes from SPIR-V and identifies which particular builtin value this object
    // represents.
    int fBuiltin;
    // input_attachment_index comes from Vulkan/SPIR-V to connect a shader variable to the a
    // corresponding attachment on the subpass in which the shader is being used.
    int fInputAttachmentIndex;
    Format fFormat;
    Primitive fPrimitive;
    int fMaxVertices;
    int fInvocations;
    String fWhen;
    Key fKey;
    StringFragment fCType;
};

} // namespace

#endif