summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-lower-glsl-ssbo-types.cpp
blob: b245a541bda474f1b53ca8471f244e9c49318da1 (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
#include "slang-ir-lower-glsl-ssbo-types.h"

#include "slang-ir-insts.h"
#include "slang-ir.h"

namespace Slang
{
template<typename F>
static void overAllInsts(IRModule* module, F f)
{
    InstWorkList workList{module};
    InstHashSet workListSet{module};

    workList.add(module->getModuleInst());
    workListSet.add(module->getModuleInst());
    while (workList.getCount())
    {
        const auto inst = workList.getLast();
        workList.removeLast();
        workListSet.remove(inst);

        f(inst);

        for (auto child = inst->getLastChild(); child; child = child->getPrevInst())
        {
            if (workListSet.contains(child))
                continue;
            workList.add(child);
            workListSet.add(child);
        }
    }
}

struct ArrayLikeSSBOInfo
{
    IRType* backingStruct;
    IRType* elementType;
    IRStructKey* elementsKey;
};

struct StructLikeSSBOInfo
{
    IRType* backingStruct;
};

struct SSBOIndexInfo
{
    IRInst* ssboVar;
    IRInst* index;
};

static void lowerArrayLikeSSBOs(IRModule* module)
{
    // Here we will:
    // - Identify SSBO types which comprise a single array field with
    //   element type T
    // - Create a (RW)StructuredBuffer<T> type
    // - Replace all uses of the SSBO with the StructuredBuffer type

    Dictionary<IRGLSLShaderStorageBufferType*, ArrayLikeSSBOInfo> arrayLikeSSBOTypes;
    overAllInsts(
        module,
        [&](IRInst* inst)
        {
            if (const auto ssbo = as<IRGLSLShaderStorageBufferType>(inst))
            {
                if (const auto backingStruct = as<IRStructType>(ssbo->getElementType()))
                {
                    auto members = backingStruct->getFields();
                    if (members.getFirst() != members.getLast())
                        return;
                    const auto onlyMember = members.getFirst();
                    if (!onlyMember)
                        return;
                    const auto onlyArrayMember = as<IRUnsizedArrayType>(onlyMember->getFieldType());
                    if (!onlyArrayMember)
                        return;
                    const auto elemType = onlyArrayMember->getElementType();
                    arrayLikeSSBOTypes.add(ssbo, {backingStruct, elemType, onlyMember->getKey()});
                }
            }
        });

    IRBuilder builder{module};
    for (const auto& [ssbo, info] : arrayLikeSSBOTypes)
    {
        builder.setInsertAfter(ssbo);
        IRInst* operands[2] = {info.elementType, ssbo->getDataLayout()};
        const auto sb = builder.getType(kIROp_HLSLRWStructuredBufferType, 2, operands);
        ssbo->replaceUsesWith(sb);
        ssbo->removeAndDeallocate();

        Dictionary<IRInst*, SSBOIndexInfo> indexes;
        traverseUses(
            info.elementsKey,
            [&](IRUse* use)
            {
                if (const auto fieldAddress = as<IRFieldAddress>(use->getUser()))
                {
                    traverseUses(
                        use->user,
                        [&](IRUse* arrayUse)
                        {
                            if (const auto gep = as<IRGetElementPtr>(arrayUse->user))
                            {
                                SLANG_ASSERT(gep->getBase() == use->user);
                                indexes.add(gep, {fieldAddress->getBase(), gep->getIndex()});
                            }
                            else
                            {
                                SLANG_UNIMPLEMENTED_X("Unhandled use of array-like SSBO");
                            }
                        });
                }
                else if (as<IRStructField>(use->getUser()))
                {
                    // We expect and can ignore the use when declaring a struct field
                }
                else
                {
                    SLANG_UNIMPLEMENTED_X("Unhandled use of array-like SSBO");
                }
            });
        for (const auto& [elemPtr, index] : indexes)
        {
            builder.setInsertBefore(elemPtr);
            const auto sbp =
                builder.emitRWStructuredBufferGetElementPtr(index.ssboVar, index.index);
            elemPtr->replaceUsesWith(sbp);
            elemPtr->removeAndDeallocate();
        }
    }
}

static void lowerStructLikeSSBOs(IRModule* module)
{
    // Here we will:
    // - Identify SSBO types without an unsized array member in the backing
    //   struct, T
    // - Create a (RW)StructuredBuffer<T> type
    // - Replace all uses of the SSBO with the StructuredBuffer type

    Dictionary<IRGLSLShaderStorageBufferType*, StructLikeSSBOInfo> structLikeSSBOTypes;
    overAllInsts(
        module,
        [&](IRInst* inst)
        {
            if (const auto ssbo = as<IRGLSLShaderStorageBufferType>(inst))
            {
                // Storage buffers are always backed by a struct
                if (const auto backingStruct = as<IRStructType>(ssbo->getElementType()))
                {
                    auto members = backingStruct->getFields();
                    const auto lastMember = members.getLast();
                    if (lastMember && as<IRUnsizedArrayType>(lastMember->getFieldType()))
                        return;
                    structLikeSSBOTypes.add(ssbo, {backingStruct});
                }
            }
        });

    // Collect the uses of these ssbos
    InstHashSet ssboUses(module);
    overAllInsts(
        module,
        [&](IRInst* inst)
        {
            StructLikeSSBOInfo slsi;
            if (const auto ssboType = as<IRGLSLShaderStorageBufferType>(inst->getDataType()))
                if (structLikeSSBOTypes.tryGetValue(ssboType, slsi))
                    ssboUses.add(inst);
        });

    IRBuilder builder{module};
    for (const auto& [ssbo, info] : structLikeSSBOTypes)
    {
        builder.setInsertAfter(ssbo);
        IRInst* operands = info.backingStruct;
        const auto sb = builder.getType(kIROp_HLSLRWStructuredBufferType, 1, &operands);

        ssbo->replaceUsesWith(sb);
        ssbo->removeAndDeallocate();
    }

    for (const auto& var : *ssboUses.set)
    {
        traverseUses(
            var,
            [&](IRUse* use)
            {
                // We only want to insert this access into blocks, anything
                // else we assume is just using the identity of the ssbo (for
                // instance IRStructFieldLayoutAttr)
                if (!as<IRBlock>(use->getUser()->getParent()))
                    return;
                builder.setInsertBefore(use->getUser());
                const auto sbp = builder.emitRWStructuredBufferGetElementPtr(
                    var,
                    builder.getIntValue(builder.getIntType(), 0));
                use->set(sbp);
            });
    }
}

static void lowerSSBOsToPointers(IRModule* module)
{
    IRBuilder builder{module};
    overAllInsts(
        module,
        [&](IRInst* inst)
        {
            if (const auto ssbo = as<IRGLSLShaderStorageBufferType>(inst))
            {
                builder.setInsertAfter(ssbo);
                const auto ptr = builder.getPtrType(ssbo->getElementType());
                ssbo->replaceUsesWith(ptr);
                ssbo->removeAndDeallocate();
            }
        });
}

static void diagnoseRemainingSSBOs(IRModule* module, DiagnosticSink* sink)
{
    overAllInsts(
        module,
        [&](IRInst* inst)
        {
            if (const auto ssbo = as<IRGLSLShaderStorageBufferType>(inst))
            {
                sink->diagnose(ssbo, Diagnostics::unhandledGLSLSSBOType);
            }
        });
}

void lowerGLSLShaderStorageBufferObjectsToStructuredBuffers(IRModule* module, DiagnosticSink* sink)
{
    lowerArrayLikeSSBOs(module);
    lowerStructLikeSSBOs(module);
    diagnoseRemainingSSBOs(module, sink);
}

void lowerGLSLShaderStorageBufferObjectsToPointers(IRModule* module, DiagnosticSink* sink)
{
    lowerSSBOsToPointers(module);
    diagnoseRemainingSSBOs(module, sink);
}
} // namespace Slang