@@ -14,6 +14,7 @@ namespace wgsl
14
14
{
15
15
16
16
Error g_error;
17
+ CompileTargets g_compileTargets;
17
18
18
19
Error getLastError ()
19
20
{
@@ -22,6 +23,11 @@ Error getLastError()
22
23
return currentError;
23
24
}
24
25
26
+ CompileTargets* getCompileTargets ()
27
+ {
28
+ return &g_compileTargets;
29
+ }
30
+
25
31
GlobalSession* createGlobalSession ()
26
32
{
27
33
IGlobalSession* globalSession = nullptr ;
@@ -38,15 +44,41 @@ GlobalSession* createGlobalSession()
38
44
return new GlobalSession (globalSession);
39
45
}
40
46
41
- Session* GlobalSession::createSession ()
47
+ CompileTargets::CompileTargets ()
48
+ {
49
+ #define MAKE_PAIR (x ) { #x, SLANG_##x }
50
+
51
+ m_compileTargetMap = {
52
+ MAKE_PAIR (GLSL),
53
+ MAKE_PAIR (HLSL),
54
+ MAKE_PAIR (WGSL),
55
+ MAKE_PAIR (SPIRV),
56
+ MAKE_PAIR (METAL),
57
+ };
58
+ }
59
+
60
+ int CompileTargets::findCompileTarget (const std::string& name)
61
+ {
62
+ auto res = m_compileTargetMap.find (name);
63
+ if ( res != m_compileTargetMap.end ())
64
+ {
65
+ return res->second ;
66
+ }
67
+ else
68
+ {
69
+ return SLANG_TARGET_UNKNOWN;
70
+ }
71
+ }
72
+
73
+ Session* GlobalSession::createSession (int compileTarget)
42
74
{
43
75
ISession* session = nullptr ;
44
76
{
45
77
SessionDesc sessionDesc = {};
46
78
sessionDesc.structureSize = sizeof (sessionDesc);
47
79
constexpr SlangInt targetCount = 1 ;
48
80
TargetDesc target = {};
49
- target.format = SLANG_WGSL ;
81
+ target.format = (SlangCompileTarget)compileTarget ;
50
82
sessionDesc.targets = ⌖
51
83
sessionDesc.targetCount = targetCount;
52
84
SlangResult result = m_interface->createSession (sessionDesc, &session);
@@ -202,5 +234,32 @@ std::string ComponentType::getEntryPointCode(int entryPointIndex, int targetInde
202
234
return {};
203
235
}
204
236
237
+ // Since spirv code is binary, we can't return it as a string, we will need to use emscripten::val
238
+ // to wrap it and return it to the javascript side.
239
+ emscripten::val ComponentType::getEntryPointCodeSpirv (int entryPointIndex, int targetIndex)
240
+ {
241
+ Slang::ComPtr<IBlob> kernelBlob;
242
+ Slang::ComPtr<ISlangBlob> diagnosticBlob;
243
+ SlangResult result = interface ()->getEntryPointCode (
244
+ entryPointIndex,
245
+ targetIndex,
246
+ kernelBlob.writeRef (),
247
+ diagnosticBlob.writeRef ());
248
+ if (result != SLANG_OK)
249
+ {
250
+ g_error.type = std::string (" USER" );
251
+ g_error.result = result;
252
+ g_error.message = std::string (
253
+ (char *)diagnosticBlob->getBufferPointer (),
254
+ (char *)diagnosticBlob->getBufferPointer () +
255
+ diagnosticBlob->getBufferSize ());
256
+ return {};
257
+ }
258
+
259
+ const uint8_t * ptr = (uint8_t *)kernelBlob->getBufferPointer ();
260
+ return emscripten::val (emscripten::typed_memory_view (kernelBlob->getBufferSize (),
261
+ ptr));
262
+ }
263
+
205
264
} // namespace wgsl
206
265
} // namespace slang
0 commit comments