summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2025-09-05 22:37:34 -0700
committerGitHub <noreply@github.com>2025-09-05 22:37:34 -0700
commitbc6b82666fa4deda932c36cea93ee2059e0992b2 (patch)
tree6800fcd6839db782af056310653d35391b558c1e /tools
parentfe87a39e453b64e94446181a9ae5cbfc0f62bf0c (diff)
Relax restriction on using link-time types for shader parameters. (#8387)
This change relaxes a previous restriction on link-time types and constants, so that we now allow them to be used to define shader parameters. Doing so will result in a parameter layout that is incomplete prior to linking. The PR added a test to call the reflection API on a fully linked program and ensure that we can report correct binding info.
Diffstat (limited to 'tools')
-rw-r--r--tools/slang-test/test-reporter.cpp5
-rw-r--r--tools/slang-unit-test/unit-test-link-time-type-reflection.cpp228
2 files changed, 232 insertions, 1 deletions
diff --git a/tools/slang-test/test-reporter.cpp b/tools/slang-test/test-reporter.cpp
index 3aba10566..00f570b6c 100644
--- a/tools/slang-test/test-reporter.cpp
+++ b/tools/slang-test/test-reporter.cpp
@@ -610,9 +610,9 @@ void TestReporter::message(TestMessageType type, const String& message)
if (canWriteStdError())
{
+ fprintf(stderr, "[%s] ", m_currentInfo.name.getBuffer());
if (type == TestMessageType::RunError || type == TestMessageType::TestFailure)
{
- fprintf(stderr, "error: ");
fputs(message.getBuffer(), stderr);
fprintf(stderr, "\n");
}
@@ -627,6 +627,9 @@ void TestReporter::message(TestMessageType type, const String& message)
{
m_currentMessage << "\n";
}
+
+ if (m_currentInfo.name.getLength())
+ m_currentMessage << "[" << m_currentInfo.name << "] ";
m_currentMessage.append(message);
}
diff --git a/tools/slang-unit-test/unit-test-link-time-type-reflection.cpp b/tools/slang-unit-test/unit-test-link-time-type-reflection.cpp
new file mode 100644
index 000000000..c42fd2f16
--- /dev/null
+++ b/tools/slang-unit-test/unit-test-link-time-type-reflection.cpp
@@ -0,0 +1,228 @@
+// unit-test-translation-unit-import.cpp
+
+#include "../../source/core/slang-io.h"
+#include "../../source/core/slang-process.h"
+#include "slang-com-ptr.h"
+#include "slang.h"
+#include "unit-test/slang-unit-test.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+using namespace Slang;
+
+static String getTypeFullName(slang::TypeReflection* type)
+{
+ ComPtr<ISlangBlob> blob;
+ type->getFullName(blob.writeRef());
+ return String((const char*)blob->getBufferPointer());
+}
+
+// Test that the reflection API provides correct info about modules with link-time types.
+
+SLANG_UNIT_TEST(linkTimeTypeReflection)
+{
+ // Source for a module that contains can be specialized with a link-time type.
+ const char* userSourceBody = R"(
+ interface IMaterial { float4 load(); }
+ extern struct Material : IMaterial;
+ ConstantBuffer<Material> gMaterial;
+
+ RWTexture2D tex;
+
+ extern static const int count;
+ uniform uint4 buffers[count];
+
+ [numthreads(1,1,1)]
+ [shader("compute")]
+ void computeMain() {
+ tex[uint2(0, 0)] = gMaterial.load();
+ }
+ )";
+
+ String moduleName = "linkTimeTypeReflection_Compute";
+
+ ComPtr<slang::IGlobalSession> globalSession;
+ SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
+ slang::TargetDesc targetDesc = {};
+ targetDesc.format = SLANG_SPIRV_ASM;
+ targetDesc.profile = globalSession->findProfile("spirv_1_5");
+ slang::SessionDesc sessionDesc = {};
+ sessionDesc.targetCount = 1;
+ sessionDesc.targets = &targetDesc;
+ ComPtr<slang::ISession> session;
+ SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
+
+ ComPtr<slang::IBlob> diagnosticBlob;
+ auto module = session->loadModuleFromSourceString(
+ moduleName.getBuffer(),
+ (moduleName + ".slang").getBuffer(),
+ userSourceBody,
+ diagnosticBlob.writeRef());
+ SLANG_CHECK_ABORT(module != nullptr);
+
+ // Source for a module that defines the link-time type.
+ String configModuleSource = "import " + moduleName + ";\n" + R"(
+ export struct Material : IMaterial = MyMaterial;
+ export static const int count = 11;
+
+ struct MyMaterial : IMaterial {
+ int data;
+ Texture2D diffuse;
+ float4 load() { return diffuse.Load(uint3(0,0,0)); }
+ }
+ )";
+ auto configModule = session->loadModuleFromSourceString(
+ "config",
+ "config.slang",
+ configModuleSource.getBuffer(),
+ diagnosticBlob.writeRef());
+ SLANG_CHECK_ABORT(configModule != nullptr);
+
+ slang::IComponentType* components[] = {module, configModule};
+
+ ComPtr<slang::IComponentType> compositeProgram;
+ session->createCompositeComponentType(
+ components,
+ 2,
+ compositeProgram.writeRef(),
+ diagnosticBlob.writeRef());
+ SLANG_CHECK_ABORT(compositeProgram != nullptr);
+
+ ComPtr<slang::IComponentType> linkedProgram;
+ compositeProgram->link(linkedProgram.writeRef(), diagnosticBlob.writeRef());
+ SLANG_CHECK_ABORT(linkedProgram != nullptr);
+
+ auto programLayout = linkedProgram->getLayout();
+ auto var0 = programLayout->getParameterByIndex(0);
+
+ // `gMaterial`'s binding starts at 1, because there is an implicit global uniform buffer.
+ SLANG_CHECK(var0->getOffset(slang::ParameterCategory::DescriptorTableSlot) == 1);
+ SLANG_CHECK(var0->getTypeLayout()->getSize(slang::ParameterCategory::DescriptorTableSlot) == 2);
+
+ auto elementLayout = var0->getTypeLayout()->getElementTypeLayout();
+ SLANG_CHECK_ABORT(elementLayout != nullptr);
+ SLANG_CHECK(elementLayout->getSize() == 16);
+
+ auto var1 = programLayout->getParameterByIndex(1);
+ SLANG_CHECK(var1->getOffset(slang::ParameterCategory::DescriptorTableSlot) == 3);
+
+ auto var2 = programLayout->getParameterByIndex(2);
+ SLANG_CHECK(var2->getTypeLayout()->getSize() == 11 * 16);
+
+ ComPtr<slang::IBlob> codeBlob;
+ linkedProgram->getTargetCode(0, codeBlob.writeRef(), diagnosticBlob.writeRef());
+
+ SLANG_CHECK_ABORT(codeBlob.get());
+
+ auto spirvStr = UnownedStringSlice((const char*)codeBlob->getBufferPointer());
+
+ SLANG_CHECK(spirvStr.indexOf(toSlice("OpDecorate %tex Binding 3")) != -1);
+}
+
+
+// Test that the reflection API provides correct info about modules using link-time constants in a
+// `Conditional` field.
+
+SLANG_UNIT_TEST(linkTimeConditionalReflection)
+{
+ // Source for a module that contains can be specialized with a link-time constant.
+ const char* userSourceBody = R"(
+ module LinkTimeConditional;
+
+ extern static const bool hasNormal;
+ extern static const bool hasColor;
+
+ struct VertexOut
+ {
+ float4 pos : SV_Position;
+ float someData;
+ Conditional<float3, hasNormal> normal;
+ Conditional<float3, hasColor> color;
+ }
+
+ [shader("vertex")]
+ VertexOut vertexMain()
+ {
+ VertexOut v;
+ v.pos = float4(0,0,0,1);
+ v.someData = 2.0f;
+ v.normal.set(float3(1,0,0));
+ v.color.set(float3(1,1,1));
+ return v;
+ }
+ )";
+
+ String moduleName = "LinkTimeConditional";
+
+ ComPtr<slang::IGlobalSession> globalSession;
+ SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
+ slang::TargetDesc targetDesc = {};
+ targetDesc.format = SLANG_SPIRV_ASM;
+ targetDesc.profile = globalSession->findProfile("spirv_1_5");
+ slang::SessionDesc sessionDesc = {};
+ sessionDesc.targetCount = 1;
+ sessionDesc.targets = &targetDesc;
+ ComPtr<slang::ISession> session;
+ SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
+
+ ComPtr<slang::IBlob> diagnosticBlob;
+ auto module = session->loadModuleFromSourceString(
+ moduleName.getBuffer(),
+ (moduleName + ".slang").getBuffer(),
+ userSourceBody,
+ diagnosticBlob.writeRef());
+ SLANG_CHECK_ABORT(module != nullptr);
+
+ // Source for a module that defines the link-time constants.
+ String configModuleSource = R"(
+ export static const bool hasNormal = false;
+ export static const bool hasColor = true;
+ )";
+ auto configModule = session->loadModuleFromSourceString(
+ "config",
+ "config.slang",
+ configModuleSource.getBuffer(),
+ diagnosticBlob.writeRef());
+ SLANG_CHECK_ABORT(configModule != nullptr);
+
+ ComPtr<slang::IEntryPoint> entryPoint;
+ module->getDefinedEntryPoint(0, entryPoint.writeRef());
+
+ slang::IComponentType* components[] = {module, configModule, entryPoint};
+
+ ComPtr<slang::IComponentType> compositeProgram;
+ session->createCompositeComponentType(
+ components,
+ 3,
+ compositeProgram.writeRef(),
+ diagnosticBlob.writeRef());
+ SLANG_CHECK_ABORT(compositeProgram != nullptr);
+
+ ComPtr<slang::IComponentType> linkedProgram;
+ compositeProgram->link(linkedProgram.writeRef(), diagnosticBlob.writeRef());
+ SLANG_CHECK_ABORT(linkedProgram != nullptr);
+
+ auto programLayout = linkedProgram->getLayout();
+
+ auto entryPointLayout = programLayout->getEntryPointByIndex(0);
+
+ auto resultLayout = entryPointLayout->getResultVarLayout();
+ SLANG_CHECK_ABORT(resultLayout != nullptr);
+
+ // Number of varying output is 2, because `pos` is a system value that doesn't count towards
+ // varying output.
+ SLANG_CHECK(
+ resultLayout->getTypeLayout()->getSize(slang::ParameterCategory::VaryingOutput) == 2);
+
+ ComPtr<slang::IBlob> codeBlob;
+ linkedProgram->getTargetCode(0, codeBlob.writeRef(), diagnosticBlob.writeRef());
+
+ SLANG_CHECK_ABORT(codeBlob.get());
+
+ auto spirvStr = UnownedStringSlice((const char*)codeBlob->getBufferPointer());
+
+ // Test that the resulting spirv defines output at location 1, but not at location 2.
+ SLANG_CHECK(spirvStr.indexOf(toSlice("Location 1")) != -1);
+ SLANG_CHECK(spirvStr.indexOf(toSlice("Location 2")) == -1);
+}