1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2016 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef SKSL_LAYOUT
9cb93a386Sopenharmony_ci#define SKSL_LAYOUT
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/private/SkSLString.h"
12cb93a386Sopenharmony_ci
13cb93a386Sopenharmony_cinamespace SkSL {
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ci/**
16cb93a386Sopenharmony_ci * Represents a layout block appearing before a variable declaration, as in:
17cb93a386Sopenharmony_ci *
18cb93a386Sopenharmony_ci * layout (location = 0) int x;
19cb93a386Sopenharmony_ci */
20cb93a386Sopenharmony_cistruct Layout {
21cb93a386Sopenharmony_ci    enum Flag {
22cb93a386Sopenharmony_ci        kOriginUpperLeft_Flag            = 1 <<  0,
23cb93a386Sopenharmony_ci        kPushConstant_Flag               = 1 <<  1,
24cb93a386Sopenharmony_ci        kBlendSupportAllEquations_Flag   = 1 <<  2,
25cb93a386Sopenharmony_ci        kSRGBUnpremul_Flag               = 1 <<  3,
26cb93a386Sopenharmony_ci
27cb93a386Sopenharmony_ci        // These flags indicate if the qualifier appeared, regardless of the accompanying value.
28cb93a386Sopenharmony_ci        kLocation_Flag                   = 1 <<  4,
29cb93a386Sopenharmony_ci        kOffset_Flag                     = 1 <<  5,
30cb93a386Sopenharmony_ci        kBinding_Flag                    = 1 <<  6,
31cb93a386Sopenharmony_ci        kIndex_Flag                      = 1 <<  7,
32cb93a386Sopenharmony_ci        kSet_Flag                        = 1 <<  8,
33cb93a386Sopenharmony_ci        kBuiltin_Flag                    = 1 <<  9,
34cb93a386Sopenharmony_ci        kInputAttachmentIndex_Flag       = 1 << 10,
35cb93a386Sopenharmony_ci    };
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci    Layout(int flags, int location, int offset, int binding, int index, int set, int builtin,
38cb93a386Sopenharmony_ci           int inputAttachmentIndex)
39cb93a386Sopenharmony_ci    : fFlags(flags)
40cb93a386Sopenharmony_ci    , fLocation(location)
41cb93a386Sopenharmony_ci    , fOffset(offset)
42cb93a386Sopenharmony_ci    , fBinding(binding)
43cb93a386Sopenharmony_ci    , fIndex(index)
44cb93a386Sopenharmony_ci    , fSet(set)
45cb93a386Sopenharmony_ci    , fBuiltin(builtin)
46cb93a386Sopenharmony_ci    , fInputAttachmentIndex(inputAttachmentIndex) {}
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci    Layout()
49cb93a386Sopenharmony_ci    : fFlags(0)
50cb93a386Sopenharmony_ci    , fLocation(-1)
51cb93a386Sopenharmony_ci    , fOffset(-1)
52cb93a386Sopenharmony_ci    , fBinding(-1)
53cb93a386Sopenharmony_ci    , fIndex(-1)
54cb93a386Sopenharmony_ci    , fSet(-1)
55cb93a386Sopenharmony_ci    , fBuiltin(-1)
56cb93a386Sopenharmony_ci    , fInputAttachmentIndex(-1) {}
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci    static Layout builtin(int builtin) {
59cb93a386Sopenharmony_ci        Layout result;
60cb93a386Sopenharmony_ci        result.fBuiltin = builtin;
61cb93a386Sopenharmony_ci        return result;
62cb93a386Sopenharmony_ci    }
63cb93a386Sopenharmony_ci
64cb93a386Sopenharmony_ci    String description() const {
65cb93a386Sopenharmony_ci        String result;
66cb93a386Sopenharmony_ci        auto separator = [firstSeparator = true]() mutable -> String {
67cb93a386Sopenharmony_ci            if (firstSeparator) {
68cb93a386Sopenharmony_ci                firstSeparator = false;
69cb93a386Sopenharmony_ci                return "";
70cb93a386Sopenharmony_ci            } else {
71cb93a386Sopenharmony_ci                return ", ";
72cb93a386Sopenharmony_ci            }};
73cb93a386Sopenharmony_ci        if (fLocation >= 0) {
74cb93a386Sopenharmony_ci            result += separator() + "location = " + to_string(fLocation);
75cb93a386Sopenharmony_ci        }
76cb93a386Sopenharmony_ci        if (fOffset >= 0) {
77cb93a386Sopenharmony_ci            result += separator() + "offset = " + to_string(fOffset);
78cb93a386Sopenharmony_ci        }
79cb93a386Sopenharmony_ci        if (fBinding >= 0) {
80cb93a386Sopenharmony_ci            result += separator() + "binding = " + to_string(fBinding);
81cb93a386Sopenharmony_ci        }
82cb93a386Sopenharmony_ci        if (fIndex >= 0) {
83cb93a386Sopenharmony_ci            result += separator() + "index = " + to_string(fIndex);
84cb93a386Sopenharmony_ci        }
85cb93a386Sopenharmony_ci        if (fSet >= 0) {
86cb93a386Sopenharmony_ci            result += separator() + "set = " + to_string(fSet);
87cb93a386Sopenharmony_ci        }
88cb93a386Sopenharmony_ci        if (fBuiltin >= 0) {
89cb93a386Sopenharmony_ci            result += separator() + "builtin = " + to_string(fBuiltin);
90cb93a386Sopenharmony_ci        }
91cb93a386Sopenharmony_ci        if (fInputAttachmentIndex >= 0) {
92cb93a386Sopenharmony_ci            result += separator() + "input_attachment_index = " + to_string(fInputAttachmentIndex);
93cb93a386Sopenharmony_ci        }
94cb93a386Sopenharmony_ci        if (fFlags & kOriginUpperLeft_Flag) {
95cb93a386Sopenharmony_ci            result += separator() + "origin_upper_left";
96cb93a386Sopenharmony_ci        }
97cb93a386Sopenharmony_ci        if (fFlags & kBlendSupportAllEquations_Flag) {
98cb93a386Sopenharmony_ci            result += separator() + "blend_support_all_equations";
99cb93a386Sopenharmony_ci        }
100cb93a386Sopenharmony_ci        if (fFlags & kPushConstant_Flag) {
101cb93a386Sopenharmony_ci            result += separator() + "push_constant";
102cb93a386Sopenharmony_ci        }
103cb93a386Sopenharmony_ci        if (fFlags & kSRGBUnpremul_Flag) {
104cb93a386Sopenharmony_ci            result += separator() + "srgb_unpremul";
105cb93a386Sopenharmony_ci        }
106cb93a386Sopenharmony_ci        if (result.size() > 0) {
107cb93a386Sopenharmony_ci            result = "layout (" + result + ")";
108cb93a386Sopenharmony_ci        }
109cb93a386Sopenharmony_ci        return result;
110cb93a386Sopenharmony_ci    }
111cb93a386Sopenharmony_ci
112cb93a386Sopenharmony_ci    bool operator==(const Layout& other) const {
113cb93a386Sopenharmony_ci        return fFlags                == other.fFlags &&
114cb93a386Sopenharmony_ci               fLocation             == other.fLocation &&
115cb93a386Sopenharmony_ci               fOffset               == other.fOffset &&
116cb93a386Sopenharmony_ci               fBinding              == other.fBinding &&
117cb93a386Sopenharmony_ci               fIndex                == other.fIndex &&
118cb93a386Sopenharmony_ci               fSet                  == other.fSet &&
119cb93a386Sopenharmony_ci               fBuiltin              == other.fBuiltin &&
120cb93a386Sopenharmony_ci               fInputAttachmentIndex == other.fInputAttachmentIndex;
121cb93a386Sopenharmony_ci    }
122cb93a386Sopenharmony_ci
123cb93a386Sopenharmony_ci    bool operator!=(const Layout& other) const {
124cb93a386Sopenharmony_ci        return !(*this == other);
125cb93a386Sopenharmony_ci    }
126cb93a386Sopenharmony_ci
127cb93a386Sopenharmony_ci    int fFlags;
128cb93a386Sopenharmony_ci    int fLocation;
129cb93a386Sopenharmony_ci    int fOffset;
130cb93a386Sopenharmony_ci    int fBinding;
131cb93a386Sopenharmony_ci    int fIndex;
132cb93a386Sopenharmony_ci    int fSet;
133cb93a386Sopenharmony_ci    // builtin comes from SPIR-V and identifies which particular builtin value this object
134cb93a386Sopenharmony_ci    // represents.
135cb93a386Sopenharmony_ci    int fBuiltin;
136cb93a386Sopenharmony_ci    // input_attachment_index comes from Vulkan/SPIR-V to connect a shader variable to the a
137cb93a386Sopenharmony_ci    // corresponding attachment on the subpass in which the shader is being used.
138cb93a386Sopenharmony_ci    int fInputAttachmentIndex;
139cb93a386Sopenharmony_ci};
140cb93a386Sopenharmony_ci
141cb93a386Sopenharmony_ci}  // namespace SkSL
142cb93a386Sopenharmony_ci
143cb93a386Sopenharmony_ci#endif
144