yum-mirror/slang

Making it easier to work with shaders

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

Gangzheng TongFix additional VVL violations (#7377)3822f9243

master
7.5 KiB354 linesraw
1#ifndef SLANG_TEST_SHADER_INPUT_LAYOUT_H
2#define SLANG_TEST_SHADER_INPUT_LAYOUT_H
3
4#include "core/slang-basic.h"
5#include "core/slang-random-generator.h"
6#include "core/slang-writer.h"
7
8#include <slang-rhi.h>
9
10namespace renderer_test
11{
12
13using namespace rhi;
14
15enum class ShaderInputType
16{
17    Buffer,
18    Texture,
19    Sampler,
20    CombinedTextureSampler,
21    Array,
22    UniformData,
23    Object,
24    Aggregate,
25    Specialize,
26    AccelerationStructure,
27};
28
29enum class InputTextureContent
30{
31    Zero,
32    One,
33    ChessBoard,
34    Gradient
35};
36
37enum InputTextureSampleCount
38{
39    One = 1,
40    Two = 2,
41    Four = 4,
42    Eight = 8,
43    Sixteen = 16,
44    ThirtyTwo = 32,
45    SixtyFour = 64,
46};
47struct InputTextureDesc
48{
49    int dimension = 2;
50    int arrayLength = 0;
51    bool isCube = false;
52    bool isDepthTexture = false;
53    bool isRWTexture = false;
54    int size = 4;
55    int mipMapCount = 0; ///< 0 means the maximum number of mips will be bound
56
57    InputTextureSampleCount sampleCount = InputTextureSampleCount::One;
58    Format format = Format::RGBA8Unorm;
59
60    InputTextureContent content = InputTextureContent::One;
61};
62
63enum class InputBufferType
64{
65    //    ConstantBuffer,
66    StorageBuffer,
67    //    RootConstantBuffer,
68};
69
70struct InputBufferDesc
71{
72    InputBufferType type = InputBufferType::StorageBuffer;
73    int stride = 0; // stride == 0 indicates an unstructured buffer.
74    int elementCount = 1;
75    Format format = Format::Undefined;
76    // For RWStructuredBuffer, AppendStructuredBuffer, ConsumeStructuredBuffer
77    // the default value of 0xffffffff indicates that a counter buffer should
78    // not be assigned
79    uint32_t counter = ~0u;
80};
81
82struct InputSamplerDesc
83{
84    bool isCompareSampler = false;
85    TextureFilteringMode filteringMode = TextureFilteringMode::Linear;
86};
87
88struct TextureData
89{
90    struct Slice
91    {
92        static Slice make(void* values, size_t size)
93        {
94            Slice slice;
95            slice.values = values;
96            slice.valuesCount = size;
97            return slice;
98        }
99
100        void* values = nullptr; ///< Values of the type format
101        size_t valuesCount = 0;
102    };
103
104    void addSlice(const void* data, size_t elemCount)
105    {
106        const size_t totalSize = m_formatSize * elemCount;
107        void* dst = ::malloc(totalSize);
108        ::memcpy(dst, data, totalSize);
109        m_slices.add(Slice::make(dst, elemCount));
110    }
111    void* addSlice(size_t elemCount)
112    {
113        const size_t totalSize = m_formatSize * elemCount;
114        void* dst = ::malloc(totalSize);
115        m_slices.add(Slice::make(dst, elemCount));
116        return dst;
117    }
118
119    /// Set the size of the slice in count of format sized elements
120    void* setSliceCount(Slang::Index sliceIndex, size_t count)
121    {
122        auto& slice = m_slices[sliceIndex];
123        if (count != slice.valuesCount)
124        {
125            slice.values = ::realloc(slice.values, count * m_formatSize);
126            slice.valuesCount = count;
127        }
128        return slice.values;
129    }
130
131    void init(Format format)
132    {
133        clearSlices();
134
135        const FormatInfo& formatInfo = getFormatInfo(format);
136        m_formatSize = uint8_t(formatInfo.blockSizeInBytes / formatInfo.pixelsPerBlock);
137        m_format = format;
138    }
139
140    ~TextureData() { clearSlices(); }
141
142    void clearSlices()
143    {
144        for (auto& slice : m_slices)
145        {
146            if (slice.values)
147            {
148                ::free(slice.values);
149            }
150        }
151        m_slices.clear();
152    }
153
154    rhi::Format m_format = rhi::Format::Undefined;
155    uint8_t m_formatSize = 0;
156
157    Slang::List<Slice> m_slices;
158    int m_textureSize = 0;
159    int m_mipLevels = 1;
160    int m_arraySize = 1;
161};
162
163class ShaderInputLayout
164{
165public:
166    class Val : public Slang::RefObject
167    {
168    public:
169        Val(ShaderInputType kind)
170            : kind(kind)
171        {
172        }
173
174        ShaderInputType kind;
175        bool isOutput = false;
176    };
177    typedef Slang::RefPtr<Val> ValPtr;
178
179    class TextureVal : public Val
180    {
181    public:
182        TextureVal()
183            : Val(ShaderInputType::Texture)
184        {
185        }
186
187        InputTextureDesc textureDesc;
188    };
189
190    class DataValBase : public Val
191    {
192    public:
193        DataValBase(ShaderInputType kind)
194            : Val(kind)
195        {
196        }
197
198        Slang::List<unsigned int> bufferData;
199    };
200
201    class BufferVal : public DataValBase
202    {
203    public:
204        BufferVal()
205            : DataValBase(ShaderInputType::Buffer)
206        {
207        }
208
209        InputBufferDesc bufferDesc;
210    };
211
212    class DataVal : public DataValBase
213    {
214    public:
215        DataVal()
216            : DataValBase(ShaderInputType::UniformData)
217        {
218        }
219    };
220
221    class SamplerVal : public Val
222    {
223    public:
224        SamplerVal()
225            : Val(ShaderInputType::Sampler)
226        {
227        }
228
229        InputSamplerDesc samplerDesc;
230    };
231
232    class CombinedTextureSamplerVal : public Val
233    {
234    public:
235        CombinedTextureSamplerVal()
236            : Val(ShaderInputType::CombinedTextureSampler)
237        {
238        }
239
240        Slang::RefPtr<TextureVal> textureVal;
241        Slang::RefPtr<SamplerVal> samplerVal;
242    };
243
244    class AccelerationStructureVal : public Val
245    {
246    public:
247        AccelerationStructureVal()
248            : Val(ShaderInputType::AccelerationStructure)
249        {
250        }
251    };
252
253    struct Field
254    {
255        Slang::String name;
256        ValPtr val;
257    };
258    typedef Field Entry;
259
260    class ParentVal : public Val
261    {
262    public:
263        ParentVal(ShaderInputType kind)
264            : Val(kind)
265        {
266        }
267
268        virtual void addField(Field const& field) = 0;
269    };
270
271    class AggVal : public ParentVal
272    {
273    public:
274        AggVal(ShaderInputType kind = ShaderInputType::Aggregate)
275            : ParentVal(kind)
276        {
277        }
278
279        Slang::List<Field> fields;
280
281        virtual void addField(Field const& field) override;
282    };
283
284    class ObjectVal : public Val
285    {
286    public:
287        ObjectVal()
288            : Val(ShaderInputType::Object)
289        {
290        }
291
292        Slang::String typeName;
293        ValPtr contentVal;
294    };
295
296    class SpecializeVal : public Val
297    {
298    public:
299        ValPtr contentVal;
300        Slang::List<Slang::String> typeArgs;
301        SpecializeVal()
302            : Val(ShaderInputType::Specialize)
303        {
304        }
305    };
306
307    class ArrayVal : public ParentVal
308    {
309    public:
310        ArrayVal()
311            : ParentVal(ShaderInputType::Array)
312        {
313        }
314
315        Slang::List<ValPtr> vals;
316
317        virtual void addField(Field const& field) override;
318    };
319
320    Slang::RefPtr<AggVal> rootVal;
321    Slang::List<Slang::String> globalSpecializationArgs;
322    Slang::List<Slang::String> entryPointSpecializationArgs;
323
324    class TypeConformanceVal
325    {
326    public:
327        Slang::String derivedTypeName;
328        Slang::String baseTypeName;
329        Slang::Int idOverride = -1;
330    };
331    Slang::List<TypeConformanceVal> typeConformances;
332
333    int numRenderTargets = 1;
334
335    Slang::Index findEntryIndexByName(const Slang::String& name) const;
336
337    void parse(Slang::RandomGenerator* rand, const char* source);
338
339    /// Writes a binding, if bindRoot is set, will try to honor the underlying type when outputting.
340    /// If not will dump as uint32_t hex.
341    static SlangResult writeBinding(
342        slang::TypeLayoutReflection* typeLayout,
343        const void* data,
344        size_t sizeInBytes,
345        Slang::WriterHelper writer);
346};
347
348void generateTextureDataRGB8(TextureData& output, const InputTextureDesc& desc);
349void generateTextureData(TextureData& output, const InputTextureDesc& desc);
350
351
352} // namespace renderer_test
353
354#endif