summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2024-11-11 19:29:18 -0800
committerGitHub <noreply@github.com>2024-11-12 11:29:18 +0800
commitea04ad12110df6b958c117f5d92f45813316cd70 (patch)
treefb5c90178425d3fcbd0c92e6d89d0b5835090756
parent3b7d0e01cce61a809757a3fd4c1b9f922a883c8b (diff)
Add wasm binding to get reflection json. (#5536)
Co-authored-by: Ellie Hermaszewska <ellieh@nvidia.com>
-rw-r--r--.gitignore1
-rw-r--r--include/slang.h5
-rw-r--r--source/slang-wasm/slang-wasm-bindings.cpp1
-rw-r--r--source/slang-wasm/slang-wasm.cpp13
-rw-r--r--source/slang-wasm/slang-wasm.h2
-rw-r--r--source/slang/slang-reflection-json.cpp2
6 files changed, 23 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index e407ed018..803fcbe14 100644
--- a/.gitignore
+++ b/.gitignore
@@ -93,3 +93,4 @@ vkd3d-proton.cache.write
*~
.*.swp
.*.swo
+/generators
diff --git a/include/slang.h b/include/slang.h
index 0e6724373..5e64ce0b9 100644
--- a/include/slang.h
+++ b/include/slang.h
@@ -3322,6 +3322,11 @@ struct ShaderReflection
return (VariableLayoutReflection*)spReflection_getGlobalParamsVarLayout(
(SlangReflection*)this);
}
+
+ SlangResult toJson(ISlangBlob** outBlob)
+ {
+ return spReflection_ToJson((SlangReflection*)this, nullptr, outBlob);
+ }
};
diff --git a/source/slang-wasm/slang-wasm-bindings.cpp b/source/slang-wasm/slang-wasm-bindings.cpp
index 346fe8a04..8c8257795 100644
--- a/source/slang-wasm/slang-wasm-bindings.cpp
+++ b/source/slang-wasm/slang-wasm-bindings.cpp
@@ -54,6 +54,7 @@ EMSCRIPTEN_BINDINGS(slang)
.function("getBindingIndex", &slang::wgsl::VariableLayoutReflection::getBindingIndex);
class_<slang::wgsl::ProgramLayout>("ProgramLayout")
+ .function("toJsonObject", &slang::wgsl::ProgramLayout::toJsonObject)
.function("getParameterCount", &slang::wgsl::ProgramLayout::getParameterCount)
.function(
"getParameterByIndex",
diff --git a/source/slang-wasm/slang-wasm.cpp b/source/slang-wasm/slang-wasm.cpp
index bdc2e5e6e..c6c008483 100644
--- a/source/slang-wasm/slang-wasm.cpp
+++ b/source/slang-wasm/slang-wasm.cpp
@@ -415,6 +415,19 @@ unsigned int ProgramLayout::getParameterCount()
return interface()->getParameterCount();
}
+emscripten::val ProgramLayout::toJsonObject()
+{
+ Slang::ComPtr<ISlangBlob> blob;
+ if (SLANG_FAILED(interface()->toJson(blob.writeRef())))
+ return {};
+ auto jsonString = std::string(
+ (char*)blob->getBufferPointer(),
+ (char*)blob->getBufferPointer() + blob->getBufferSize());
+ emscripten::val parsedObject =
+ emscripten::val::global("JSON").call<emscripten::val>("parse", jsonString);
+ return parsedObject;
+}
+
VariableLayoutReflection* ProgramLayout::getParameterByIndex(unsigned int index)
{
return (slang::wgsl::VariableLayoutReflection*)(interface()->getParameterByIndex(index));
diff --git a/source/slang-wasm/slang-wasm.h b/source/slang-wasm/slang-wasm.h
index 1f2be2fb5..4288aed18 100644
--- a/source/slang-wasm/slang-wasm.h
+++ b/source/slang-wasm/slang-wasm.h
@@ -82,6 +82,8 @@ public:
slang::wgsl::TypeLayoutReflection* getGlobalParamsTypeLayout();
slang::ProgramLayout* interface() const { return (slang::ProgramLayout*)this; }
+
+ emscripten::val toJsonObject();
};
class Session;
diff --git a/source/slang/slang-reflection-json.cpp b/source/slang/slang-reflection-json.cpp
index c7d11a6c3..6c00c8f07 100644
--- a/source/slang/slang-reflection-json.cpp
+++ b/source/slang/slang-reflection-json.cpp
@@ -1071,7 +1071,7 @@ static void emitReflectionEntryPointJSON(
}
// If code generation has been performed, print out the parameter usage by this entry point.
- if ((request->getCompileFlags() & SLANG_COMPILE_FLAG_NO_CODEGEN) == 0)
+ if (request && (request->getCompileFlags() & SLANG_COMPILE_FLAG_NO_CODEGEN) == 0)
{
writer << ",\n\"bindings\": [\n";
writer.indent();