yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Jay KwakFix compiler warning with clang 18.1.8 on windows (#6963)678de6547

master
3.4 KiB125 linesraw
1#ifndef SLANG_DECODER_HELPER_H
2#define SLANG_DECODER_HELPER_H
3
4#include "../../core/slang-list.h"
5#include "../util/record-format.h"
6#include "slang-com-helper.h"
7#include "slang.h"
8
9#include <stdint.h>
10
11namespace SlangRecord
12{
13
14// This class is used to allocate memory for the type decoder
15class DecoderAllocatorSingleton
16{
17public:
18    static DecoderAllocatorSingleton* getInstance();
19    void* allocate(size_t size);
20    ~DecoderAllocatorSingleton();
21
22private:
23    DecoderAllocatorSingleton() = default;
24    Slang::List<void*> m_allocations;
25};
26
27class DecoderBase
28{
29public:
30    virtual ~DecoderBase() = default;
31    void* allocate(size_t size) { return m_allocator->allocate(size); }
32
33protected:
34    DecoderAllocatorSingleton* m_allocator = DecoderAllocatorSingleton::getInstance();
35};
36
37// We don't allow pointer type to be used as a template parameter
38template<typename T, typename U = typename std::enable_if<!std::is_pointer<T>::value>::type>
39class ValueDecoder : public DecoderBase
40{
41public:
42    T& getValue() { return m_value; }
43
44protected:
45    T m_value{};
46};
47
48template<typename T, typename = typename std::enable_if<!std::is_pointer<T>::value>::type>
49class StructDecoder : public ValueDecoder<T>
50{
51public:
52    using Super = ValueDecoder<T>;
53    size_t decode(const uint8_t* buffer, int64_t bufferSize);
54};
55
56// We only allow pointer type to be used as a template parameter
57template<typename T, typename U = typename std::enable_if<std::is_pointer<T>::value>::type>
58class PointerDecoder : public DecoderBase
59{
60public:
61    void setPointer(void* data) { m_pointer = static_cast<T>(data); }
62    T getPointer() const { return m_pointer; }
63    void setPointerAddress(uint64_t address) { m_pointerAddress = address; }
64    void setDataSize(size_t size) { m_dataSize = size; }
65    size_t getDataSize() const { return m_dataSize; }
66
67private:
68    T m_pointer{nullptr};
69    uint64_t m_pointerAddress = 0;
70    size_t m_dataSize = 0;
71};
72
73class BlobDecoder
74{
75private:
76    PointerDecoder<void*> m_blobData;
77
78    class BlobImpl : public slang::IBlob
79    {
80    public:
81        // ISlangUnknown
82        virtual SLANG_NO_THROW SlangResult SLANG_MCALL
83        queryInterface(SlangUUID const& uuid, void** outObject) override
84        {
85            if (uuid == ISlangUnknown::getTypeGuid() || uuid == ISlangBlob::getTypeGuid())
86            {
87                *outObject = static_cast<ISlangBlob*>(this);
88                return SLANG_OK;
89            }
90            *outObject = nullptr;
91            return SLANG_E_NO_INTERFACE;
92        }
93
94        virtual SLANG_NO_THROW uint32_t SLANG_MCALL addRef() override { return 1; }
95        virtual SLANG_NO_THROW uint32_t SLANG_MCALL release() override { return 1; }
96
97        BlobImpl(const PointerDecoder<void*>* blobData)
98            : m_pBlobData(blobData)
99        {
100        }
101        virtual SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE
102        {
103            return m_pBlobData->getPointer();
104        }
105        virtual SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE
106        {
107            return m_pBlobData->getDataSize();
108        }
109
110    private:
111        const PointerDecoder<void*>* m_pBlobData;
112    };
113
114    BlobImpl m_blobImpl{&m_blobData};
115    AddressFormat m_address{0};
116
117public:
118    size_t decode(const uint8_t* buffer, int64_t bufferSize);
119    slang::IBlob* getBlob() { return &m_blobImpl; }
120};
121
122using StringDecoder = PointerDecoder<char*>;
123} // namespace SlangRecord
124
125#endif // SLANG_RECORD_DECODER_HELPER_H