Skip to content

Commit ea04ad1

Browse files
Add wasm binding to get reflection json. (shader-slang#5536)
Co-authored-by: Ellie Hermaszewska <ellieh@nvidia.com>
1 parent 3b7d0e0 commit ea04ad1

File tree

6 files changed

+23
-1
lines changed

6 files changed

+23
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,4 @@ vkd3d-proton.cache.write
9393
*~
9494
.*.swp
9595
.*.swo
96+
/generators

include/slang.h

+5
Original file line numberDiff line numberDiff line change
@@ -3322,6 +3322,11 @@ struct ShaderReflection
33223322
return (VariableLayoutReflection*)spReflection_getGlobalParamsVarLayout(
33233323
(SlangReflection*)this);
33243324
}
3325+
3326+
SlangResult toJson(ISlangBlob** outBlob)
3327+
{
3328+
return spReflection_ToJson((SlangReflection*)this, nullptr, outBlob);
3329+
}
33253330
};
33263331

33273332

source/slang-wasm/slang-wasm-bindings.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ EMSCRIPTEN_BINDINGS(slang)
5454
.function("getBindingIndex", &slang::wgsl::VariableLayoutReflection::getBindingIndex);
5555

5656
class_<slang::wgsl::ProgramLayout>("ProgramLayout")
57+
.function("toJsonObject", &slang::wgsl::ProgramLayout::toJsonObject)
5758
.function("getParameterCount", &slang::wgsl::ProgramLayout::getParameterCount)
5859
.function(
5960
"getParameterByIndex",

source/slang-wasm/slang-wasm.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,19 @@ unsigned int ProgramLayout::getParameterCount()
415415
return interface()->getParameterCount();
416416
}
417417

418+
emscripten::val ProgramLayout::toJsonObject()
419+
{
420+
Slang::ComPtr<ISlangBlob> blob;
421+
if (SLANG_FAILED(interface()->toJson(blob.writeRef())))
422+
return {};
423+
auto jsonString = std::string(
424+
(char*)blob->getBufferPointer(),
425+
(char*)blob->getBufferPointer() + blob->getBufferSize());
426+
emscripten::val parsedObject =
427+
emscripten::val::global("JSON").call<emscripten::val>("parse", jsonString);
428+
return parsedObject;
429+
}
430+
418431
VariableLayoutReflection* ProgramLayout::getParameterByIndex(unsigned int index)
419432
{
420433
return (slang::wgsl::VariableLayoutReflection*)(interface()->getParameterByIndex(index));

source/slang-wasm/slang-wasm.h

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class ProgramLayout
8282
slang::wgsl::TypeLayoutReflection* getGlobalParamsTypeLayout();
8383

8484
slang::ProgramLayout* interface() const { return (slang::ProgramLayout*)this; }
85+
86+
emscripten::val toJsonObject();
8587
};
8688

8789
class Session;

source/slang/slang-reflection-json.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ static void emitReflectionEntryPointJSON(
10711071
}
10721072

10731073
// If code generation has been performed, print out the parameter usage by this entry point.
1074-
if ((request->getCompileFlags() & SLANG_COMPILE_FLAG_NO_CODEGEN) == 0)
1074+
if (request && (request->getCompileFlags() & SLANG_COMPILE_FLAG_NO_CODEGEN) == 0)
10751075
{
10761076
writer << ",\n\"bindings\": [\n";
10771077
writer.indent();

0 commit comments

Comments
 (0)