aboutsummaryrefslogtreecommitdiff
path: root/glslang/MachineIndependent/iomapper.cpp
blob: 15847ddbc20d9256ac21a6680ffae180ca35e2a8 (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
//
//Copyright (C) 2016 LunarG, Inc.
//
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions
//are met:
//
//    Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//
//    Redistributions in binary form must reproduce the above
//    copyright notice, this list of conditions and the following
//    disclaimer in the documentation and/or other materials provided
//    with the distribution.
//
//    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//POSSIBILITY OF SUCH DAMAGE.
//

#include "../Include/Common.h"
#include "../Include/InfoSink.h"
#include "iomapper.h"
#include "LiveTraverser.h"
#include "localintermediate.h"

#include "gl_types.h"

#include <unordered_set>
#include <unordered_map>

//
// Map IO bindings.
//
// High-level algorithm for one stage:
//
// 1. Traverse all code (live+dead) to find the explicitly provided bindings.
//
// 2. Traverse (just) the live code to determine which non-provided bindings
//    require auto-numbering.  We do not auto-number dead ones.
//
// 3. Traverse all the code to apply the bindings:
//    a. explicitly given bindings are offset according to their type
//    b. implicit live bindings are auto-numbered into the holes, using
//       any open binding slot.
//    c. implicit dead bindings are left un-bound.
//

   
namespace glslang {

// Map of IDs to bindings
typedef std::unordered_map<unsigned int, int> TBindingMap;
typedef std::unordered_set<int> TUsedBindings;


// This traverses the AST to determine which bindings are used, and which are implicit
// (for subsequent auto-numbering)
class TBindingTraverser : public TLiveTraverser {
public:
    TBindingTraverser(const TIntermediate& i, TBindingMap& bindingMap, TUsedBindings& usedBindings,
                      bool traverseDeadCode = false) :
        TLiveTraverser(i, traverseDeadCode, true, true, false),
        bindingMap(bindingMap),
        usedBindings(usedBindings)
    { }

protected:
    virtual void visitSymbol(TIntermSymbol* base) {
        if (base->getQualifier().storage == EvqUniform)
            addUniform(*base);
    }

    // Return the right binding base given the variable type.
    int getBindingBase(const TType& type) {
        if (type.getBasicType() == EbtSampler) {
            const TSampler& sampler = type.getSampler();
            if (sampler.isPureSampler())
                return intermediate.getShiftSamplerBinding();
            if (sampler.isTexture())
                return intermediate.getShiftTextureBinding();
        }

        if (type.getQualifier().isUniformOrBuffer())
            return intermediate.getShiftUboBinding();

        return -1;  // not a type with a binding
    }

    // Mark a given base symbol ID as being bound to 'binding'
    void markBinding(const TIntermSymbol& base, int binding) {
        bindingMap[base.getId()] = binding;

        if (binding >= 0) {
            // const TType& type = base.getType();
            const unsigned int size = 1; // type.isArray() ? type.getCumulativeArraySize() : 1;

            for (unsigned int offset=0; offset<size; ++offset)
                usedBindings.insert(binding + offset);
        }
    }

    // Mark the bindings that are given explicitly, and set ones that need
    // implicit bindings to -1 for a subsequent pass.  (Can't happen in this
    // pass because explicit bindings in dead code reserve the location).
    virtual void addUniform(TIntermSymbol& base)
    {
        // Skip ones we've already seen.
        if (bindingMap.find(base.getId()) != bindingMap.end())
            return;

        const TType& type = base.getType();
        const int bindingBase = getBindingBase(type);

        // Return if it's not a type we bind
        if (bindingBase == -1)
            return;

        if (type.getQualifier().hasBinding()) {
            // It has a binding: keep that one.
            markBinding(base, type.getQualifier().layoutBinding + bindingBase);
        } else if (!traverseAll) {
            // Mark it as something we need to dynamically create a binding for,
            // only if we're walking just the live code.  We don't auto-number
            // in dead code.
            markBinding(base, -1);
        }
    }

    TBindingMap&         bindingMap;
    TUsedBindings&       usedBindings;
};


// This traverses the AST and applies binding maps it's given.
class TIoMappingTraverser : public TBindingTraverser {
public:
    TIoMappingTraverser(TIntermediate& i, TBindingMap& bindingMap, TUsedBindings& usedBindings,
                        TInfoSink& infoSink, bool traverseDeadCode) :
        TBindingTraverser(i, bindingMap, usedBindings, traverseDeadCode),
        infoSink(infoSink),
        assignError(false)
    { }

    bool success() const { return !assignError; }

protected:
    unsigned checkBindingRange(const TIntermSymbol& base, unsigned binding)
    {
        if (binding >= TQualifier::layoutBindingEnd) {
            TString err = "mapped binding out of range: ";
            err += base.getName();

            infoSink.info.message(EPrefixInternalError, err.c_str());
            assignError = true;
            
            return 0;
        }

        return binding;
    }

    void addUniform(TIntermSymbol& base) override
    {
        // Skip things we don't intend to bind.
        if (bindingMap.find(base.getId()) == bindingMap.end())
            return;

        const int existingBinding = bindingMap[base.getId()];

        // Apply existing binding, if we were given one or already made one up.
        if (existingBinding != -1) {
            base.getWritableType().getQualifier().layoutBinding = checkBindingRange(base, existingBinding);
            return;
        }

        if (intermediate.getAutoMapBindings()) {
            // Otherwise, find a free spot for it.
            const int freeBinding = getFreeBinding(base.getType(), getBindingBase(base.getType()));

            markBinding(base, freeBinding);
            base.getWritableType().getQualifier().layoutBinding = checkBindingRange(base, freeBinding);
        }
    }

    // Search for N free consecutive binding slots in [base, base+required).
    // E.g, if we want to reserve consecutive bindings for flattened arrays.
    bool hasNFreeSlots(int base, int required) {
        for (int binding = base; binding < (base + required); ++binding)
            if (usedBindings.find(binding) != usedBindings.end())
                return false;
        
        return true;
    }

    // Find a free binding spot 
    int getFreeBinding(const TType&, int nextBinding) {
        while (!hasNFreeSlots(nextBinding, 1))
            ++nextBinding;

        return nextBinding;
    }

private:
    bool assignError;  // true if there was an error assigning the bindings
    TInfoSink& infoSink;
};

// Map I/O variables to provided offsets, and make bindings for
// unbound but live variables.
//
// Returns false if the input is too malformed to do this.
bool TIoMapper::addStage(EShLanguage, TIntermediate& intermediate, TInfoSink& infoSink)
{
    // Trivial return if there is nothing to do.
    if (intermediate.getShiftSamplerBinding() == 0 &&
        intermediate.getShiftTextureBinding() == 0 &&
        intermediate.getShiftUboBinding() == 0 &&
        intermediate.getAutoMapBindings() == false)
        return true;

    if (intermediate.getNumEntryPoints() != 1 || intermediate.isRecursive())
        return false;

    TIntermNode* root = intermediate.getTreeRoot();
    if (root == nullptr)
        return false;

    // The lifetime of this data spans several passes.
    TBindingMap   bindingMap;
    TUsedBindings usedBindings;

    TBindingTraverser it_binding_all(intermediate, bindingMap, usedBindings, true);
    TBindingTraverser it_binding_live(intermediate, bindingMap, usedBindings, false);
    TIoMappingTraverser it_iomap(intermediate, bindingMap, usedBindings, infoSink, true);

    // Traverse all (live+dead) code to find explicit bindings, so we can avoid those.
    root->traverse(&it_binding_all);

    // Traverse just live code to find things that need implicit bindings.
    it_binding_live.pushFunction(intermediate.getEntryPointMangledName().c_str());

    while (! it_binding_live.functions.empty()) {
        TIntermNode* function = it_binding_live.functions.back();
        it_binding_live.functions.pop_back();
        function->traverse(&it_binding_live);
    }

    // Bind everything that needs a binding and doesn't have one.
    root->traverse(&it_iomap);

    return it_iomap.success();
}

} // end namespace glslang