Skip to content

Commit 9475b11

Browse files
authoredApr 1, 2021
Associating GUID (or UUID) with types (shader-slang#1776)
* #include an absolute path didn't work - because paths were taken to always be relative. * Add mechanism to embed guid inside of type.
1 parent 2a32fae commit 9475b11

16 files changed

+97
-124
lines changed
 

‎slang.h

+62-27
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,27 @@ extern "C"
844844
uint8_t data4[8];
845845
};
846846

847+
// Place at the start of an interface with the guid.
848+
// Guid should be specified as SLANG_COM_INTERFACE(0x00000000, 0x0000, 0x0000, { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 })
849+
// NOTE: it's the typical guid struct definition, without the surrounding {}
850+
// It is not necessary to use the multiple parameters (we can wrap in parens), but this is simple.
851+
#define SLANG_COM_INTERFACE(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
852+
public: \
853+
SLANG_FORCE_INLINE static const SlangUUID& getTypeGuid() \
854+
{ \
855+
static const SlangUUID guid = { a, b, c, d0, d1, d2, d3, d4, d5, d6, d7 }; \
856+
return guid; \
857+
}
858+
859+
// Sometimes it's useful to associate a guid with a class to identify it. This macro can used for this,
860+
// and the guid extracted via the getTypeGuid() function defined in the type
861+
#define SLANG_CLASS_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
862+
SLANG_FORCE_INLINE static const SlangUUID& getTypeGuid() \
863+
{ \
864+
static const SlangUUID guid = { a, b, c, d0, d1, d2, d3, d4, d5, d6, d7 }; \
865+
return guid; \
866+
}
867+
847868
/** Base interface for components exchanged through the API.
848869
849870
This interface definition is compatible with the COM `IUnknown`,
@@ -852,7 +873,8 @@ extern "C"
852873
*/
853874
struct ISlangUnknown
854875
{
855-
public:
876+
SLANG_COM_INTERFACE(0x00000000, 0x0000, 0x0000, { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 })
877+
856878
virtual SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const& uuid, void** outObject) = 0;
857879
virtual SLANG_NO_THROW uint32_t SLANG_MCALL addRef() = 0;
858880
virtual SLANG_NO_THROW uint32_t SLANG_MCALL release() = 0;
@@ -865,19 +887,20 @@ extern "C"
865887
uint32_t AddRef() { return addRef(); }
866888
uint32_t Release() { return release(); }
867889
};
868-
#define SLANG_UUID_ISlangUnknown { 0x00000000, 0x0000, 0x0000, { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } }
890+
#define SLANG_UUID_ISlangUnknown ISlangUnknown::getTypeGuid()
869891

870892
/** A "blob" of binary data.
871893
872894
This interface definition is compatible with the `ID3DBlob` and `ID3D10Blob` interfaces.
873895
*/
874896
struct ISlangBlob : public ISlangUnknown
875897
{
876-
public:
898+
SLANG_COM_INTERFACE(0x8BA5FB08, 0x5195, 0x40e2, { 0xAC, 0x58, 0x0D, 0x98, 0x9C, 0x3A, 0x01, 0x02 })
899+
877900
virtual SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() = 0;
878901
virtual SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() = 0;
879902
};
880-
#define SLANG_UUID_ISlangBlob { 0x8BA5FB08, 0x5195, 0x40e2, { 0xAC, 0x58, 0x0D, 0x98, 0x9C, 0x3A, 0x01, 0x02 } }
903+
#define SLANG_UUID_ISlangBlob ISlangBlob::getTypeGuid()
881904

882905
/** A (real or virtual) file system.
883906
@@ -892,7 +915,8 @@ extern "C"
892915

893916
struct ISlangFileSystem : public ISlangUnknown
894917
{
895-
public:
918+
SLANG_COM_INTERFACE(0x003A09FC, 0x3A4D, 0x4BA0, { 0xAD, 0x60, 0x1F, 0xD8, 0x63, 0xA9, 0x15, 0xAB })
919+
896920
/** Load a file from `path` and return a blob of its contents
897921
@param path The path to load from, as a null-terminated UTF-8 string.
898922
@param outBlob A destination pointer to receive the blob of the file contents.
@@ -910,7 +934,7 @@ extern "C"
910934
char const* path,
911935
ISlangBlob** outBlob) = 0;
912936
};
913-
#define SLANG_UUID_ISlangFileSystem { 0x003A09FC, 0x3A4D, 0x4BA0, { 0xAD, 0x60, 0x1F, 0xD8, 0x63, 0xA9, 0x15, 0xAB } }
937+
#define SLANG_UUID_ISlangFileSystem ISlangFileSystem::getTypeGuid()
914938

915939

916940
typedef void(*SlangFuncPtr)(void);
@@ -920,7 +944,8 @@ extern "C"
920944
*/
921945
struct ISlangSharedLibrary: public ISlangUnknown
922946
{
923-
public:
947+
SLANG_COM_INTERFACE( 0x9c9d5bc5, 0xeb61, 0x496f,{ 0x80, 0xd7, 0xd1, 0x47, 0xc4, 0xa2, 0x37, 0x30 })
948+
924949
/** Get a function by name. If the library is unloaded will only return nullptr.
925950
@param name The name of the function
926951
@return The function pointer related to the name or nullptr if not found
@@ -935,11 +960,12 @@ extern "C"
935960
*/
936961
virtual SLANG_NO_THROW void* SLANG_MCALL findSymbolAddressByName(char const* name) = 0;
937962
};
938-
#define SLANG_UUID_ISlangSharedLibrary { 0x9c9d5bc5, 0xeb61, 0x496f,{ 0x80, 0xd7, 0xd1, 0x47, 0xc4, 0xa2, 0x37, 0x30 } };
963+
#define SLANG_UUID_ISlangSharedLibrary ISlangSharedLibrary::getTypeGuid()
939964

940965
struct ISlangSharedLibraryLoader: public ISlangUnknown
941966
{
942-
public:
967+
SLANG_COM_INTERFACE(0x6264ab2b, 0xa3e8, 0x4a06, { 0x97, 0xf1, 0x49, 0xbc, 0x2d, 0x2a, 0xb1, 0x4d })
968+
943969
/** Load a shared library. In typical usage the library name should *not* contain any platform
944970
specific elements. For example on windows a dll name should *not* be passed with a '.dll' extension,
945971
and similarly on linux a shared library should *not* be passed with the 'lib' prefix and '.so' extension
@@ -949,7 +975,7 @@ extern "C"
949975
const char* path,
950976
ISlangSharedLibrary** sharedLibraryOut) = 0;
951977
};
952-
#define SLANG_UUID_ISlangSharedLibraryLoader { 0x6264ab2b, 0xa3e8, 0x4a06, { 0x97, 0xf1, 0x49, 0xbc, 0x2d, 0x2a, 0xb1, 0x4d } };
978+
#define SLANG_UUID_ISlangSharedLibraryLoader ISlangSharedLibraryLoader::getTypeGuid()
953979

954980
/* Type that identifies how a path should be interpreted */
955981
typedef unsigned int SlangPathType;
@@ -973,7 +999,8 @@ extern "C"
973999
*/
9741000
struct ISlangFileSystemExt : public ISlangFileSystem
9751001
{
976-
public:
1002+
SLANG_COM_INTERFACE(0x5fb632d2, 0x979d, 0x4481, { 0x9f, 0xee, 0x66, 0x3c, 0x3f, 0x14, 0x49, 0xe1 })
1003+
9771004
/** Get a uniqueIdentity which uniquely identifies an object of the file system.
9781005
9791006
Given a path, returns a 'uniqueIdentity' which ideally is the same value for the same file on the file system.
@@ -1075,10 +1102,12 @@ extern "C"
10751102
void* userData) = 0;
10761103
};
10771104

1078-
#define SLANG_UUID_ISlangFileSystemExt { 0x5fb632d2, 0x979d, 0x4481, { 0x9f, 0xee, 0x66, 0x3c, 0x3f, 0x14, 0x49, 0xe1 } }
1105+
#define SLANG_UUID_ISlangFileSystemExt ISlangFileSystemExt::getTypeGuid()
10791106

10801107
struct ISlangMutableFileSystem : public ISlangFileSystemExt
10811108
{
1109+
SLANG_COM_INTERFACE(0xa058675c, 0x1d65, 0x452a, { 0x84, 0x58, 0xcc, 0xde, 0xd1, 0x42, 0x71, 0x5 })
1110+
10821111
/** Write the data specified with data and size to the specified path.
10831112
10841113
@param path The path for data to be saved to
@@ -1111,7 +1140,7 @@ extern "C"
11111140
const char* path) = 0;
11121141
};
11131142

1114-
#define SLANG_UUID_ISlangMutableFileSystem { 0xa058675c, 0x1d65, 0x452a, { 0x84, 0x58, 0xcc, 0xde, 0xd1, 0x42, 0x71, 0x5 } }
1143+
#define SLANG_UUID_ISlangMutableFileSystem ISlangMutableFileSystem::getTypeGuid()
11151144

11161145
/* Identifies different types of writer target*/
11171146
typedef unsigned int SlangWriterChannel;
@@ -1134,7 +1163,8 @@ extern "C"
11341163
*/
11351164
struct ISlangWriter : public ISlangUnknown
11361165
{
1137-
public:
1166+
SLANG_COM_INTERFACE(0xec457f0e, 0x9add, 0x4e6b,{ 0x85, 0x1c, 0xd7, 0xfa, 0x71, 0x6d, 0x15, 0xfd })
1167+
11381168
/** Begin an append buffer.
11391169
NOTE! Only one append buffer can be active at any time.
11401170
@param maxNumChars The maximum of chars that will be appended
@@ -1162,7 +1192,7 @@ extern "C"
11621192
virtual SLANG_NO_THROW SlangResult SLANG_MCALL setMode(SlangWriterMode mode) = 0;
11631193
};
11641194

1165-
#define SLANG_UUID_ISlangWriter { 0xec457f0e, 0x9add, 0x4e6b,{ 0x85, 0x1c, 0xd7, 0xfa, 0x71, 0x6d, 0x15, 0xfd } };
1195+
#define SLANG_UUID_ISlangWriter ISlangWriter::getTypeGuid()
11661196

11671197
namespace slang {
11681198
struct IGlobalSession;
@@ -2991,7 +3021,8 @@ namespace slang
29913021
*/
29923022
struct IGlobalSession : public ISlangUnknown
29933023
{
2994-
public:
3024+
SLANG_COM_INTERFACE(0xc140b5fd, 0xc78, 0x452e, { 0xba, 0x7c, 0x1a, 0x1e, 0x70, 0xc7, 0xf7, 0x1c })
3025+
29953026
/** Create a new session for loading and compiling code.
29963027
*/
29973028
virtual SLANG_NO_THROW SlangResult SLANG_MCALL createSession(
@@ -3164,15 +3195,15 @@ namespace slang
31643195
char const* name) = 0;
31653196
};
31663197

3167-
#define SLANG_UUID_IGlobalSession { 0xc140b5fd, 0xc78, 0x452e, { 0xba, 0x7c, 0x1a, 0x1e, 0x70, 0xc7, 0xf7, 0x1c } };
3198+
#define SLANG_UUID_IGlobalSession IGlobalSession::getTypeGuid()
31683199

31693200
/*!
31703201
@brief A request for one or more compilation actions to be performed.
31713202
*/
31723203
struct ICompileRequest : public ISlangUnknown
31733204
{
3174-
public:
3175-
3205+
SLANG_COM_INTERFACE( 0x96d33993, 0x317c, 0x4db5, { 0xaf, 0xd8, 0x66, 0x6e, 0xe7, 0x72, 0x48, 0xe2 } )
3206+
31763207
/** Set the filesystem hook to use for a compile request
31773208
31783209
The provided `fileSystem` will be used to load any files that
@@ -3716,7 +3747,7 @@ namespace slang
37163747

37173748
};
37183749

3719-
#define SLANG_UUID_ICompileRequest { 0x96d33993, 0x317c, 0x4db5, { 0xaf, 0xd8, 0x66, 0x6e, 0xe7, 0x72, 0x48, 0xe2 } };
3750+
#define SLANG_UUID_ICompileRequest ICompileRequest::getTypeGuid()
37203751

37213752
/** Description of a code generation target.
37223753
*/
@@ -3816,7 +3847,8 @@ namespace slang
38163847
*/
38173848
struct ISession : public ISlangUnknown
38183849
{
3819-
public:
3850+
SLANG_COM_INTERFACE( 0x67618701, 0xd116, 0x468f, { 0xab, 0x3b, 0x47, 0x4b, 0xed, 0xce, 0xe, 0x3d } )
3851+
38203852
/** Get the global session thas was used to create this session.
38213853
*/
38223854
virtual SLANG_NO_THROW IGlobalSession* SLANG_MCALL getGlobalSession() = 0;
@@ -3905,7 +3937,7 @@ namespace slang
39053937
SlangCompileRequest** outCompileRequest) = 0;
39063938
};
39073939

3908-
#define SLANG_UUID_ISession { 0x67618701, 0xd116, 0x468f, { 0xab, 0x3b, 0x47, 0x4b, 0xed, 0xce, 0xe, 0x3d } }
3940+
#define SLANG_UUID_ISession ISession::getTypeGuid()
39093941

39103942
/** A component type is a unit of shader code layout, reflection, and linking.
39113943
@@ -3970,6 +4002,8 @@ namespace slang
39704002
*/
39714003
struct IComponentType : public ISlangUnknown
39724004
{
4005+
SLANG_COM_INTERFACE(0x5bc42be8, 0x5c50, 0x4929, { 0x9e, 0x5e, 0xd1, 0x5e, 0x7c, 0x24, 0x1, 0x5f })
4006+
39734007
/** Get the runtime session that this component type belongs to.
39744008
*/
39754009
virtual SLANG_NO_THROW ISession* SLANG_MCALL getSession() = 0;
@@ -4073,14 +4107,14 @@ namespace slang
40734107
ISlangSharedLibrary** outSharedLibrary,
40744108
slang::IBlob** outDiagnostics = 0) = 0;
40754109
};
4076-
#define SLANG_UUID_IComponentType { 0x5bc42be8, 0x5c50, 0x4929, { 0x9e, 0x5e, 0xd1, 0x5e, 0x7c, 0x24, 0x1, 0x5f } };
4110+
#define SLANG_UUID_IComponentType IComponentType::getTypeGuid()
40774111

40784112
struct IEntryPoint : public IComponentType
40794113
{
4080-
public:
4114+
SLANG_COM_INTERFACE(0x8f241361, 0xf5bd, 0x4ca0, { 0xa3, 0xac, 0x2, 0xf7, 0xfa, 0x24, 0x2, 0xb8 })
40814115
};
40824116

4083-
#define SLANG_UUID_IEntryPoint { 0x8f241361, 0xf5bd, 0x4ca0, { 0xa3, 0xac, 0x2, 0xf7, 0xfa, 0x24, 0x2, 0xb8 } }
4117+
#define SLANG_UUID_IEntryPoint IEntryPoint::getTypeGuid()
40844118

40854119
/** A module is the granularity of shader code compilation and loading.
40864120
@@ -4097,13 +4131,14 @@ namespace slang
40974131
*/
40984132
struct IModule : public IComponentType
40994133
{
4100-
public:
4134+
SLANG_COM_INTERFACE(0xc720e64, 0x8722, 0x4d31, { 0x89, 0x90, 0x63, 0x8a, 0x98, 0xb1, 0xc2, 0x79 })
4135+
41014136
virtual SLANG_NO_THROW SlangResult SLANG_MCALL findEntryPointByName(
41024137
char const* name,
41034138
IEntryPoint** outEntryPoint) = 0;
41044139
};
41054140

4106-
#define SLANG_UUID_IModule { 0xc720e64, 0x8722, 0x4d31, { 0x89, 0x90, 0x63, 0x8a, 0x98, 0xb1, 0xc2, 0x79 } }
4141+
#define SLANG_UUID_IModule IModule::getTypeGuid()
41074142

41084143
/** Argument used for specialization to types/values.
41094144
*/

‎source/core/slang-blob.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22

33
namespace Slang {
44

5-
// Allocate static const storage for the various interface IDs that the Slang API needs to expose
6-
static const Guid IID_ISlangUnknown = SLANG_UUID_ISlangUnknown;
7-
static const Guid IID_ISlangBlob = SLANG_UUID_ISlangBlob;
8-
95
ISlangUnknown* BlobBase::getInterface(const Guid& guid)
106
{
11-
return (guid == IID_ISlangUnknown || guid == IID_ISlangBlob) ? static_cast<ISlangBlob*>(this) : nullptr;
7+
return (guid == ISlangUnknown::getTypeGuid() || guid == ISlangBlob::getTypeGuid()) ? static_cast<ISlangBlob*>(this) : nullptr;
128
}
139

1410
SlangResult StaticBlob::queryInterface(SlangUUID const& guid, void** outObject)
1511
{
16-
if (guid == IID_ISlangUnknown || guid == IID_ISlangBlob)
12+
if (guid == ISlangUnknown::getTypeGuid() || guid == ISlangBlob::getTypeGuid())
1713
{
1814
*outObject = static_cast<ISlangBlob*>(this);
1915
return SLANG_OK;

‎source/core/slang-compression-system.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ enum class CompressionSystemType
2929

3030
class ICompressionSystem : public ISlangUnknown
3131
{
32-
public:
32+
SLANG_COM_INTERFACE(0xcc935840, 0xe059, 0x4bb8, { 0xa2, 0x2d, 0x92, 0x7b, 0x3c, 0x73, 0x8f, 0x85 })
3333

3434
/** Get the compression system type
3535
@return The compression system type */
@@ -51,8 +51,6 @@ class ICompressionSystem : public ISlangUnknown
5151
virtual SLANG_NO_THROW SlangResult SLANG_MCALL decompress(const void* compressed, size_t compressedSizeInBytes, size_t decompressedSizeInBytes, void* outDecompressed) = 0;
5252
};
5353

54-
#define SLANG_UUID_ICompressionSystem { 0xcc935840, 0xe059, 0x4bb8, { 0xa2, 0x2d, 0x92, 0x7b, 0x3c, 0x73, 0x8f, 0x85 } };
55-
5654
}
5755

5856
#endif

‎source/core/slang-deflate-compression-system.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ namespace Slang
1717
{
1818

1919
// Allocate static const storage for the various interface IDs that the Slang API needs to expose
20-
static const Guid IID_ISlangUnknown = SLANG_UUID_ISlangUnknown;
21-
static const Guid IID_ICompressionSystem = SLANG_UUID_ICompressionSystem;
2220

2321
class DeflateCompressionSystemImpl : public RefObject, public ICompressionSystem
2422
{
@@ -42,7 +40,7 @@ class DeflateCompressionSystemImpl : public RefObject, public ICompressionSystem
4240

4341
ICompressionSystem* DeflateCompressionSystemImpl::getInterface(const Guid& guid)
4442
{
45-
return (guid == IID_ISlangUnknown || guid == IID_ICompressionSystem) ? static_cast<ICompressionSystem*>(this) : nullptr;
43+
return (guid == ISlangUnknown::getTypeGuid() || guid == ICompressionSystem::getTypeGuid()) ? static_cast<ICompressionSystem*>(this) : nullptr;
4644
}
4745

4846
SlangResult DeflateCompressionSystemImpl::compress(const CompressionStyle* style, const void* src, size_t srcSizeInBytes, ISlangBlob** outBlob)

‎source/core/slang-file-system.cpp

+7-13
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,21 @@ namespace Slang
88
{
99

1010
// Allocate static const storage for the various interface IDs that the Slang API needs to expose
11-
static const Guid IID_ISlangUnknown = SLANG_UUID_ISlangUnknown;
12-
static const Guid IID_ISlangFileSystem = SLANG_UUID_ISlangFileSystem;
13-
static const Guid IID_ISlangFileSystemExt = SLANG_UUID_ISlangFileSystemExt;
14-
static const Guid IID_ISlangMutableFileSystem = SLANG_UUID_ISlangMutableFileSystem;
15-
16-
static const Guid IID_SlangCacheFileSystem = SLANG_UUID_CacheFileSystem;
1711

1812
SLANG_FORCE_INLINE static SlangResult _checkExt(FileSystemStyle style) { return Index(style) >= Index(FileSystemStyle::Ext) ? SLANG_OK : SLANG_E_NOT_IMPLEMENTED; }
1913
SLANG_FORCE_INLINE static SlangResult _checkMutable(FileSystemStyle style) { return Index(style) >= Index(FileSystemStyle::Mutable) ? SLANG_OK : SLANG_E_NOT_IMPLEMENTED; }
2014

2115
SLANG_FORCE_INLINE static bool _canCast(FileSystemStyle style, const Guid& guid)
2216
{
23-
if (guid == IID_ISlangUnknown || guid == IID_ISlangFileSystem)
17+
if (guid == ISlangUnknown::getTypeGuid() || guid == ISlangFileSystem::getTypeGuid())
2418
{
2519
return true;
2620
}
27-
else if (guid == IID_ISlangFileSystemExt)
21+
else if (guid == ISlangFileSystemExt::getTypeGuid())
2822
{
2923
return Index(style) >= Index(FileSystemStyle::Ext);
3024
}
31-
else if (guid == IID_ISlangMutableFileSystem)
25+
else if (guid == ISlangMutableFileSystem::getTypeGuid())
3226
{
3327
return Index(style) >= Index(FileSystemStyle::Mutable);
3428
}
@@ -41,11 +35,11 @@ static FileSystemStyle _getFileSystemStyle(ISlangFileSystem* system, ComPtr<ISla
4135

4236
FileSystemStyle style = FileSystemStyle::Load;
4337

44-
if (SLANG_SUCCEEDED(system->queryInterface(IID_ISlangMutableFileSystem, (void**)out.writeRef())))
38+
if (SLANG_SUCCEEDED(system->queryInterface(ISlangMutableFileSystem::getTypeGuid(), (void**)out.writeRef())))
4539
{
4640
style = FileSystemStyle::Mutable;
4741
}
48-
else if (SLANG_SUCCEEDED(system->queryInterface(IID_ISlangFileSystemExt, (void**)out.writeRef())))
42+
else if (SLANG_SUCCEEDED(system->queryInterface(ISlangFileSystemExt::getTypeGuid(), (void**)out.writeRef())))
4943
{
5044
style = FileSystemStyle::Ext;
5145
}
@@ -269,7 +263,7 @@ SlangResult OSFileSystem::createDirectory(const char* path)
269263

270264
SLANG_NO_THROW SlangResult SLANG_MCALL CacheFileSystem::queryInterface(SlangUUID const& uuid, void** outObject)
271265
{
272-
if (uuid == IID_SlangCacheFileSystem)
266+
if (uuid == CacheFileSystem::getTypeGuid())
273267
{
274268
*outObject = this;
275269
return SLANG_OK;
@@ -310,7 +304,7 @@ void CacheFileSystem::setInnerFileSystem(ISlangFileSystem* fileSystem, UniqueIde
310304
if (fileSystem)
311305
{
312306
// Try to get the more sophisticated interface
313-
fileSystem->queryInterface(IID_ISlangFileSystemExt, (void**)m_fileSystemExt.writeRef());
307+
fileSystem->queryInterface(ISlangFileSystemExt::getTypeGuid(), (void**)m_fileSystemExt.writeRef());
314308
}
315309

316310
switch (m_uniqueIdentityMode)

0 commit comments

Comments
 (0)
Please sign in to comment.