• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
7 
8 #ifndef SKSL_LAYOUT
9 #define SKSL_LAYOUT
10 
11 #include "src/base/SkEnumBitMask.h"
12 
13 #include <string>
14 
15 namespace SkSL {
16 
17 class Context;
18 class Position;
19 
20 enum class LayoutFlag : int {
21     kNone                       = 0,
22     kAll                        = ~0,
23 
24     kOriginUpperLeft            = 1 <<  0,
25     kPushConstant               = 1 <<  1,
26     kBlendSupportAllEquations   = 1 <<  2,
27     kColor                      = 1 <<  3,
28 
29     // These flags indicate if the qualifier appeared, regardless of the accompanying value.
30     kLocation                   = 1 <<  4,
31     kOffset                     = 1 <<  5,
32     kBinding                    = 1 <<  6,
33     kTexture                    = 1 <<  7,
34     kSampler                    = 1 <<  8,
35     kIndex                      = 1 <<  9,
36     kSet                        = 1 << 10,
37     kBuiltin                    = 1 << 11,
38     kInputAttachmentIndex       = 1 << 12,
39 
40     // These flags indicate the backend type; only one at most can be set.
41     kVulkan                     = 1 << 13,
42     kMetal                      = 1 << 14,
43     kWebGPU                     = 1 << 15,
44     kDirect3D                   = 1 << 16,
45 
46     kAllBackends                = kVulkan | kMetal | kWebGPU | kDirect3D,
47 
48     // These flags indicate the pixel format; only one at most can be set.
49     // (https://www.khronos.org/opengl/wiki/Layout_Qualifier_(GLSL)#Image_formats)
50     kRGBA8                      = 1 << 17,
51     kRGBA32F                    = 1 << 18,
52     kR32F                       = 1 << 19,
53 
54     kAllPixelFormats            = kRGBA8 | kRGBA32F | kR32F,
55 
56     // The local invocation size of a compute program.
57     kLocalSizeX                 = 1 << 20,
58     kLocalSizeY                 = 1 << 21,
59     kLocalSizeZ                 = 1 << 22,
60 #ifdef SKSL_EXT
61     kConstantId                 = 1 << 23,
62 #endif
63 };
64 
65 }  // namespace SkSL
66 
67 SK_MAKE_BITMASK_OPS(SkSL::LayoutFlag);
68 
69 namespace SkSL {
70 
71 using LayoutFlags = SkEnumBitMask<SkSL::LayoutFlag>;
72 
73 /**
74  * Represents a layout block appearing before a variable declaration, as in:
75  *
76  * layout (location = 0) int x;
77  */
78 struct Layout {
LayoutLayout79     Layout(LayoutFlags flags, int location, int offset, int binding, int index, int set,
80            int builtin, int inputAttachmentIndex)
81             : fFlags(flags)
82             , fLocation(location)
83             , fOffset(offset)
84             , fBinding(binding)
85             , fIndex(index)
86             , fSet(set)
87             , fBuiltin(builtin)
88             , fInputAttachmentIndex(inputAttachmentIndex) {}
89 
90     constexpr Layout() = default;
91 
builtinLayout92     static Layout builtin(int builtin) {
93         Layout result;
94         result.fBuiltin = builtin;
95         return result;
96     }
97 
98     std::string description() const;
99     std::string paddedDescription() const;
100 
101     /**
102      * Verifies that only permitted layout flags are included. Reports errors and returns false in
103      * the event of a violation.
104      */
105     bool checkPermittedLayout(const Context& context,
106                               Position pos,
107                               LayoutFlags permittedLayoutFlags) const;
108 
109     bool operator==(const Layout& other) const;
110 
111     bool operator!=(const Layout& other) const {
112         return !(*this == other);
113     }
114 
115     LayoutFlags fFlags = LayoutFlag::kNone;
116     int fLocation = -1;
117     int fOffset = -1;
118     int fBinding = -1;
119     int fTexture = -1;
120     int fSampler = -1;
121     int fIndex = -1;
122     int fSet = -1;
123     // builtin comes from SPIR-V and identifies which particular builtin value this object
124     // represents.
125     int fBuiltin = -1;
126     // input_attachment_index comes from Vulkan/SPIR-V to connect a shader variable to the a
127     // corresponding attachment on the subpass in which the shader is being used.
128     int fInputAttachmentIndex = -1;
129 
130     // The local invocation size dimensions of a compute program.
131     int fLocalSizeX = -1;
132     int fLocalSizeY = -1;
133     int fLocalSizeZ = -1;
134 #ifdef SKSL_EXT
135     int fConstantId = -1;
136 #endif
137 };
138 
139 }  // namespace SkSL
140 
141 #endif
142