yum-mirror/slang

Making it easier to work with shaders

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

Ellie Hermaszewskaformatf65d756bf

master
7.6 KiB283 linesraw
1// slang-artifact-representation-impl.cpp
2#include "slang-artifact-representation-impl.h"
3
4#include "../core/slang-array-view.h"
5#include "../core/slang-castable.h"
6#include "../core/slang-file-system.h"
7#include "../core/slang-io.h"
8#include "../core/slang-type-text-util.h"
9#include "slang-artifact-util.h"
10
11namespace Slang
12{
13
14/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ExtFileArtifactRepresentation !!!!!!!!!!!!!!!!!!!!!!!!!!! */
15
16void* ExtFileArtifactRepresentation::getInterface(const Guid& guid)
17{
18    if (guid == ISlangUnknown::getTypeGuid() || guid == ICastable::getTypeGuid() ||
19        guid == IArtifactRepresentation::getTypeGuid() ||
20        guid == IPathArtifactRepresentation::getTypeGuid() ||
21        guid == IExtFileArtifactRepresentation::getTypeGuid())
22    {
23        return static_cast<IExtFileArtifactRepresentation*>(this);
24    }
25    return nullptr;
26}
27
28void* ExtFileArtifactRepresentation::getObject(const Guid& guid)
29{
30    SLANG_UNUSED(guid);
31    return nullptr;
32}
33
34void* ExtFileArtifactRepresentation::castAs(const Guid& guid)
35{
36    if (auto intf = getInterface(guid))
37    {
38        return intf;
39    }
40    return getObject(guid);
41}
42
43SlangResult ExtFileArtifactRepresentation::createRepresentation(
44    const Guid& typeGuid,
45    ICastable** outCastable)
46{
47    // We can convert into a blob only, and only if we have a path
48    // If it's referenced by a name only, it's a file that *can't* be loaded as a blob in general.
49    if (typeGuid != ISlangBlob::getTypeGuid())
50    {
51        return SLANG_E_NOT_AVAILABLE;
52    }
53
54    ComPtr<ISlangBlob> blob;
55    SLANG_RETURN_ON_FAIL(m_fileSystem->loadFile(m_path.getBuffer(), blob.writeRef()));
56
57    *outCastable = CastableUtil::getCastable(blob).detach();
58    return SLANG_OK;
59}
60
61bool ExtFileArtifactRepresentation::exists()
62{
63    SlangPathType pathType;
64    const auto res = m_fileSystem->getPathType(m_path.getBuffer(), &pathType);
65    // It exists if it is a file
66    return SLANG_SUCCEEDED(res) && pathType == getPathType();
67}
68
69const char* ExtFileArtifactRepresentation::getUniqueIdentity()
70{
71    if (m_uniqueIdentity.getLength() == 0)
72    {
73        ComPtr<ISlangBlob> uniqueIdentityBlob;
74        if (SLANG_FAILED(m_fileSystem->getFileUniqueIdentity(
75                m_path.getBuffer(),
76                uniqueIdentityBlob.writeRef())))
77        {
78            return nullptr;
79        }
80        m_uniqueIdentity = StringUtil::getString(uniqueIdentityBlob);
81    }
82
83    return m_uniqueIdentity.getLength() ? m_uniqueIdentity.getBuffer() : nullptr;
84}
85
86/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SourceBlobWithPathArtifactRepresentation
87 * !!!!!!!!!!!!!!!!!!!!!!!!!!! */
88
89void* SourceBlobWithPathInfoArtifactRepresentation::getInterface(const Guid& guid)
90{
91    if (guid == ISlangUnknown::getTypeGuid() || guid == ICastable::getTypeGuid() ||
92        guid == IArtifactRepresentation::getTypeGuid() ||
93        guid == IPathArtifactRepresentation::getTypeGuid())
94    {
95        return static_cast<IPathArtifactRepresentation*>(this);
96    }
97    return nullptr;
98}
99
100void* SourceBlobWithPathInfoArtifactRepresentation::getObject(const Guid& guid)
101{
102    SLANG_UNUSED(guid);
103    return nullptr;
104}
105
106void* SourceBlobWithPathInfoArtifactRepresentation::castAs(const Guid& guid)
107{
108    if (auto intf = getInterface(guid))
109    {
110        return intf;
111    }
112    return getObject(guid);
113}
114
115SlangResult SourceBlobWithPathInfoArtifactRepresentation::createRepresentation(
116    const Guid& typeGuid,
117    ICastable** outCastable)
118{
119    // We can convert into a blob only.
120    if (typeGuid != ISlangBlob::getTypeGuid())
121    {
122        return SLANG_E_NOT_AVAILABLE;
123    }
124
125    if (!m_blob)
126    {
127        return SLANG_E_NOT_AVAILABLE;
128    }
129
130    *outCastable = CastableUtil::getCastable(m_blob).detach();
131    return SLANG_OK;
132}
133
134/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! FileArtifactRepresentation !!!!!!!!!!!!!!!!!!!!!!!!!!! */
135
136void* OSFileArtifactRepresentation::getInterface(const Guid& guid)
137{
138    if (guid == ISlangUnknown::getTypeGuid() || guid == ICastable::getTypeGuid() ||
139        guid == IArtifactRepresentation::getTypeGuid() ||
140        guid == IPathArtifactRepresentation::getTypeGuid() ||
141        guid == IOSFileArtifactRepresentation::getTypeGuid())
142    {
143        return static_cast<IOSFileArtifactRepresentation*>(this);
144    }
145    return nullptr;
146}
147
148void* OSFileArtifactRepresentation::getObject(const Guid& guid)
149{
150    SLANG_UNUSED(guid);
151    return nullptr;
152}
153
154/* static */ ISlangMutableFileSystem* OSFileArtifactRepresentation::_getFileSystem()
155{
156    return OSFileSystem::getMutableSingleton();
157}
158
159void* OSFileArtifactRepresentation::castAs(const Guid& guid)
160{
161    if (auto intf = getInterface(guid))
162    {
163        return intf;
164    }
165    return getObject(guid);
166}
167
168SlangResult OSFileArtifactRepresentation::createRepresentation(
169    const Guid& typeGuid,
170    ICastable** outCastable)
171{
172    // We can convert into a blob only, and only if we have a path
173    // If it's referenced by a name only, it's a file that *can't* be loaded as a blob in general.
174    if (typeGuid != ISlangBlob::getTypeGuid() || m_kind == Kind::NameOnly)
175    {
176        return SLANG_E_NOT_AVAILABLE;
177    }
178
179    ComPtr<ISlangBlob> blob;
180
181    auto fileSystem = _getFileSystem();
182    SLANG_RETURN_ON_FAIL(fileSystem->loadFile(m_path.getBuffer(), blob.writeRef()));
183
184    *outCastable = CastableUtil::getCastable(blob).detach();
185    return SLANG_OK;
186}
187
188bool OSFileArtifactRepresentation::exists()
189{
190    // TODO(JS):
191    // If it's a name only it's hard to know what exists should do. It can't *check* because it
192    // relies on the 'system' doing the actual location. We could ask the IArtifactUtil, and that
193    // could change the behavior. For now we just assume it does.
194    if (m_kind == Kind::NameOnly)
195    {
196        return true;
197    }
198
199    auto fileSystem = _getFileSystem();
200
201    SlangPathType pathType;
202    const auto res = fileSystem->getPathType(m_path.getBuffer(), &pathType);
203
204    // It exists if it is a file
205    return SLANG_SUCCEEDED(res) && pathType == SLANG_PATH_TYPE_FILE;
206}
207
208const char* OSFileArtifactRepresentation::getUniqueIdentity()
209{
210    if (m_uniqueIdentity.getLength() == 0)
211    {
212        auto fileSystem = _getFileSystem();
213
214        ComPtr<ISlangBlob> uniqueIdentityBlob;
215        if (SLANG_FAILED(fileSystem->getFileUniqueIdentity(
216                m_path.getBuffer(),
217                uniqueIdentityBlob.writeRef())))
218        {
219            return nullptr;
220        }
221        m_uniqueIdentity = StringUtil::getString(uniqueIdentityBlob);
222    }
223
224    return m_uniqueIdentity.getLength() ? m_uniqueIdentity.getBuffer() : nullptr;
225}
226
227void OSFileArtifactRepresentation::disown()
228{
229    if (_isOwned())
230    {
231        m_kind = Kind::Reference;
232    }
233}
234
235OSFileArtifactRepresentation::~OSFileArtifactRepresentation()
236{
237    if (_isOwned())
238    {
239        auto fileSystem = _getFileSystem();
240        fileSystem->remove(m_path.getBuffer());
241    }
242}
243
244/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! PostEmitMetadataArtifactRepresentation !!!!!!!!!!!!!!!!!!!!!!!!!!!
245 */
246
247void* ObjectArtifactRepresentation::castAs(const Guid& guid)
248{
249
250    if (auto ptr = getInterface(guid))
251    {
252        return ptr;
253    }
254    return getObject(guid);
255}
256
257void* ObjectArtifactRepresentation::getInterface(const Guid& guid)
258{
259    if (guid == ISlangUnknown::getTypeGuid() || guid == ICastable::getTypeGuid() ||
260        guid == IArtifactRepresentation::getTypeGuid())
261    {
262        return static_cast<IArtifactRepresentation*>(this);
263    }
264    return nullptr;
265}
266
267void* ObjectArtifactRepresentation::getObject(const Guid& guid)
268{
269    if (guid == getTypeGuid())
270    {
271        return this;
272    }
273
274    // If matches the guid saved in the object, we return that
275    if (m_object && m_typeGuid == guid)
276    {
277        return m_object;
278    }
279
280    return nullptr;
281}
282
283} // namespace Slang