summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-legalize-uniform-buffer-load.cpp
blob: 9c6b27551aad84e243d51157edea108003213200 (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
#include "slang-ir-legalize-uniform-buffer-load.h"

namespace Slang
{
void legalizeUniformBufferLoad(IRModule* module)
{
    List<IRLoad*> workList;
    for (auto globalInst : module->getGlobalInsts())
    {
        if (auto func = as<IRGlobalValueWithCode>(globalInst))
        {
            for (auto block : func->getBlocks())
            {
                for (auto inst : block->getChildren())
                {
                    if (auto load = as<IRLoad>(inst))
                    {
                        auto uniformBufferType =
                            as<IRConstantBufferType>(load->getPtr()->getDataType());
                        if (!uniformBufferType)
                            continue;
                        workList.add(load);
                    }
                }
            }
        }
    }

    IRBuilder builder(module);
    for (auto load : workList)
    {
        auto uniformBufferType = as<IRConstantBufferType>(load->getPtr()->getDataType());
        SLANG_ASSERT(uniformBufferType);
        auto structType = as<IRStructType>(uniformBufferType->getElementType());
        if (!structType)
            continue;
        builder.setInsertBefore(load);
        List<IRInst*> fieldLoads;
        for (auto field : structType->getFields())
        {
            auto fieldAddr = builder.emitFieldAddress(
                builder.getPtrType(field->getFieldType()),
                load->getPtr(),
                field->getKey());
            auto fieldLoad = builder.emitLoad(fieldAddr);
            fieldLoads.add(fieldLoad);
        }
        auto makeStruct = builder.emitMakeStruct(structType, fieldLoads);
        load->replaceUsesWith(makeStruct);
    }
}

} // namespace Slang