yum-mirror/slang

Making it easier to work with shaders

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

Yong HeFix unused space discovery for bindless heap. (#8075)de7ccaf12

master
7.4 KiB219 linesraw
1// slang-artifact-associated-impl.h
2#ifndef SLANG_ARTIFACT_ASSOCIATED_IMPL_H
3#define SLANG_ARTIFACT_ASSOCIATED_IMPL_H
4
5#include "../core/slang-com-object.h"
6#include "../core/slang-memory-arena.h"
7#include "slang-artifact-associated.h"
8#include "slang-artifact-diagnostic-util.h"
9#include "slang-artifact-util.h"
10#include "slang-com-helper.h"
11#include "slang-com-ptr.h"
12
13namespace Slang
14{
15
16class ArtifactDiagnostics : public ComBaseObject, public IArtifactDiagnostics
17{
18public:
19    typedef ArtifactDiagnostics ThisType;
20
21    SLANG_COM_BASE_IUNKNOWN_ALL
22
23    // ICastable
24    SLANG_NO_THROW void* SLANG_MCALL castAs(const Guid& guid) SLANG_OVERRIDE;
25    // IClonable
26    SLANG_NO_THROW virtual void* SLANG_MCALL clone(const Guid& intf) SLANG_OVERRIDE;
27    // IDiagnostic
28    SLANG_NO_THROW virtual const Diagnostic* SLANG_MCALL getAt(Index i) SLANG_OVERRIDE
29    {
30        return &m_diagnostics[i];
31    }
32    SLANG_NO_THROW virtual Count SLANG_MCALL getCount() SLANG_OVERRIDE
33    {
34        return m_diagnostics.getCount();
35    }
36    SLANG_NO_THROW virtual void SLANG_MCALL add(const Diagnostic& diagnostic) SLANG_OVERRIDE;
37    SLANG_NO_THROW virtual void SLANG_MCALL removeAt(Index i) SLANG_OVERRIDE
38    {
39        m_diagnostics.removeAt(i);
40    }
41    SLANG_NO_THROW virtual SlangResult SLANG_MCALL getResult() SLANG_OVERRIDE { return m_result; }
42    SLANG_NO_THROW virtual void SLANG_MCALL setResult(SlangResult res) SLANG_OVERRIDE
43    {
44        m_result = res;
45    }
46    SLANG_NO_THROW virtual void SLANG_MCALL setRaw(const CharSlice& slice) SLANG_OVERRIDE;
47    SLANG_NO_THROW virtual void SLANG_MCALL appendRaw(const CharSlice& slice) SLANG_OVERRIDE;
48    SLANG_NO_THROW virtual TerminatedCharSlice SLANG_MCALL getRaw() SLANG_OVERRIDE
49    {
50        return SliceUtil::asTerminatedCharSlice(m_raw);
51    }
52    SLANG_NO_THROW virtual void SLANG_MCALL reset() SLANG_OVERRIDE;
53    SLANG_NO_THROW virtual Count SLANG_MCALL getCountAtLeastSeverity(Diagnostic::Severity severity)
54        SLANG_OVERRIDE;
55    SLANG_NO_THROW virtual Count SLANG_MCALL getCountBySeverity(Diagnostic::Severity severity)
56        SLANG_OVERRIDE;
57    SLANG_NO_THROW virtual bool SLANG_MCALL hasOfAtLeastSeverity(Diagnostic::Severity severity)
58        SLANG_OVERRIDE;
59    SLANG_NO_THROW virtual Count SLANG_MCALL getCountByStage(
60        Diagnostic::Stage stage,
61        Count outCounts[Int(Diagnostic::Severity::CountOf)]) SLANG_OVERRIDE;
62    SLANG_NO_THROW virtual void SLANG_MCALL removeBySeverity(Diagnostic::Severity severity)
63        SLANG_OVERRIDE;
64    SLANG_NO_THROW virtual void SLANG_MCALL maybeAddNote(const CharSlice& in) SLANG_OVERRIDE;
65    SLANG_NO_THROW virtual void SLANG_MCALL requireErrorDiagnostic() SLANG_OVERRIDE;
66    SLANG_NO_THROW virtual void SLANG_MCALL calcSummary(ISlangBlob** outBlob) SLANG_OVERRIDE;
67    SLANG_NO_THROW virtual void SLANG_MCALL calcSimplifiedSummary(ISlangBlob** outBlob)
68        SLANG_OVERRIDE;
69
70    /// Default ctor
71    ArtifactDiagnostics()
72        : ComBaseObject()
73    {
74    }
75    /// Copy ctor
76    ArtifactDiagnostics(const ThisType& rhs);
77
78    /// Create
79    static ComPtr<IArtifactDiagnostics> create()
80    {
81        return ComPtr<IArtifactDiagnostics>(new ThisType);
82    }
83
84protected:
85    void* getInterface(const Guid& uuid);
86    void* getObject(const Guid& uuid);
87
88    SliceAllocator m_allocator;
89
90    List<Diagnostic> m_diagnostics;
91    SlangResult m_result = SLANG_OK;
92
93    // Raw diagnostics
94    StringBuilder m_raw;
95};
96
97/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArtifactPostEmitMetadata !!!!!!!!!!!!!!!!!!!!!!!!!! */
98
99struct ShaderBindingRange
100{
101    slang::ParameterCategory category = slang::ParameterCategory::None;
102    UInt spaceIndex = 0;
103    UInt registerIndex = 0;
104    UInt registerCount = 0; // 0 for unsized
105
106    bool isInfinite() const { return registerCount == 0; }
107
108    bool containsBinding(slang::ParameterCategory _category, UInt _spaceIndex, UInt _registerIndex)
109        const
110    {
111        return category == _category && spaceIndex == _spaceIndex &&
112               registerIndex <= _registerIndex &&
113               (isInfinite() || registerCount + registerIndex > _registerIndex);
114    }
115
116    bool intersectsWith(const ShaderBindingRange& other) const
117    {
118        if (category != other.category || spaceIndex != other.spaceIndex)
119            return false;
120
121        const bool leftIntersection =
122            (registerIndex < other.registerIndex + other.registerCount) || other.isInfinite();
123        const bool rightIntersection =
124            (other.registerIndex < registerIndex + registerCount) || isInfinite();
125
126        return leftIntersection && rightIntersection;
127    }
128
129    bool adjacentTo(const ShaderBindingRange& other) const
130    {
131        if (category != other.category || spaceIndex != other.spaceIndex)
132            return false;
133
134        const bool leftIntersection =
135            (registerIndex <= other.registerIndex + other.registerCount) || other.isInfinite();
136        const bool rightIntersection =
137            (other.registerIndex <= registerIndex + registerCount) || isInfinite();
138
139        return leftIntersection && rightIntersection;
140    }
141
142    void mergeWith(const ShaderBindingRange other)
143    {
144        UInt newRegisterIndex = Math::Min(registerIndex, other.registerIndex);
145
146        if (other.isInfinite())
147            registerCount = 0;
148        else if (!isInfinite())
149            registerCount = Math::Max(
150                                registerIndex + registerCount,
151                                other.registerIndex + other.registerCount) -
152                            newRegisterIndex;
153
154        registerIndex = newRegisterIndex;
155    }
156
157    static bool isUsageTracked(slang::ParameterCategory category)
158    {
159        switch (category)
160        {
161        case slang::ConstantBuffer:
162        case slang::ShaderResource:
163        case slang::UnorderedAccess:
164        case slang::SamplerState:
165        case slang::DescriptorTableSlot:
166        case slang::VaryingInput:
167        case slang::VaryingOutput:
168        case slang::SpecializationConstant:
169        case slang::SubElementRegisterSpace:
170            return true;
171        default:
172            return false;
173        }
174    }
175};
176
177class ArtifactPostEmitMetadata : public ComBaseObject, public IArtifactPostEmitMetadata
178{
179public:
180    typedef ArtifactPostEmitMetadata ThisType;
181
182    SLANG_CLASS_GUID(0x6f82509f, 0xe48b, 0x4b83, {0xa3, 0x84, 0x5d, 0x70, 0x83, 0x19, 0x83, 0xcc})
183
184    SLANG_COM_BASE_IUNKNOWN_ALL
185
186    // ICastable
187    SLANG_NO_THROW void* SLANG_MCALL castAs(const Guid& guid) SLANG_OVERRIDE;
188
189    // IArtifactPostEmitMetadata
190    SLANG_NO_THROW virtual Slice<ShaderBindingRange> SLANG_MCALL getUsedBindingRanges()
191        SLANG_OVERRIDE;
192    SLANG_NO_THROW virtual Slice<String> SLANG_MCALL getExportedFunctionMangledNames()
193        SLANG_OVERRIDE;
194
195    // IMetadata
196    SLANG_NO_THROW virtual SlangResult isParameterLocationUsed(
197        SlangParameterCategory category, // is this a `t` register? `s` register?
198        SlangUInt spaceIndex,            // `space` for D3D12, `set` for Vulkan
199        SlangUInt registerIndex,         // `register` for D3D12, `binding` for Vulkan
200        bool& outUsed) SLANG_OVERRIDE;
201
202    SLANG_NO_THROW virtual const char* SLANG_MCALL getDebugBuildIdentifier() SLANG_OVERRIDE;
203
204    void* getInterface(const Guid& uuid);
205    void* getObject(const Guid& uuid);
206
207    static ComPtr<IArtifactPostEmitMetadata> create()
208    {
209        return ComPtr<IArtifactPostEmitMetadata>(new ThisType);
210    }
211
212    List<ShaderBindingRange> m_usedBindings;
213    List<String> m_exportedFunctionMangledNames;
214    String m_debugBuildIdentifier;
215};
216
217} // namespace Slang
218
219#endif