summaryrefslogtreecommitdiffstats
path: root/tools/slang-unit-test/unit-test-metal-parameter-block-constant-buffer.cpp
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2025-02-14 01:55:28 +0800
committerGitHub <noreply@github.com>2025-02-13 09:55:28 -0800
commit1ea2ab1b638b0e6d2c385b2b06157e6109417e6b (patch)
tree438aede974cc87fffbe58e9c99d99719bb25680a /tools/slang-unit-test/unit-test-metal-parameter-block-constant-buffer.cpp
parentccc75cdd9508a4e19efa22e7c911cc2013f514fa (diff)
Disallow only resources in constant buffers in parameterblocks on metal (#6342)
* Neaten metal parameter block checking * Disallow only resources in constant buffers in parameterblocks on metal closes https://github.com/shader-slang/slang/issues/6200 * add unit test for metal parameterblock cbuffer --------- Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'tools/slang-unit-test/unit-test-metal-parameter-block-constant-buffer.cpp')
-rw-r--r--tools/slang-unit-test/unit-test-metal-parameter-block-constant-buffer.cpp133
1 files changed, 133 insertions, 0 deletions
diff --git a/tools/slang-unit-test/unit-test-metal-parameter-block-constant-buffer.cpp b/tools/slang-unit-test/unit-test-metal-parameter-block-constant-buffer.cpp
new file mode 100644
index 000000000..d04cea974
--- /dev/null
+++ b/tools/slang-unit-test/unit-test-metal-parameter-block-constant-buffer.cpp
@@ -0,0 +1,133 @@
+// unit-test-ptr-layout.cpp
+
+#include "slang-com-ptr.h"
+#include "slang.h"
+#include "unit-test/slang-unit-test.h"
+
+#include <stdlib.h>
+
+using namespace Slang;
+
+SLANG_UNIT_TEST(metalConstantBufferInParameterBlockLayout)
+{
+ const char* testSource = R"(
+ struct T
+ {
+ float4 m0;
+ float m1;
+ float3 m2;
+ };
+
+ ParameterBlock<ConstantBuffer<T>> params;
+ )";
+
+ ComPtr<slang::IGlobalSession> globalSession;
+ SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
+
+ slang::TargetDesc targetDesc = {};
+ targetDesc.format = SLANG_METAL;
+ targetDesc.profile = globalSession->findProfile("metal");
+
+ 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(
+ "test",
+ "test.slang",
+ testSource,
+ diagnosticBlob.writeRef());
+ SLANG_CHECK(module != nullptr);
+
+ auto testBody = [&]()
+ {
+ auto reflection = module->getLayout();
+
+ // Collect our layouts
+ auto paramBlockType = reflection->findTypeByName("ParameterBlock<ConstantBuffer<T>>");
+ SLANG_CHECK(paramBlockType != nullptr);
+ auto paramBlockLayout = reflection->getTypeLayout(paramBlockType);
+ SLANG_CHECK(paramBlockLayout != nullptr);
+ auto cbufferLayout = paramBlockLayout->getElementTypeLayout();
+ SLANG_CHECK(cbufferLayout != nullptr);
+ auto structLayout = cbufferLayout->getElementTypeLayout();
+ SLANG_CHECK(structLayout != nullptr);
+
+ // Check offsets follow constant buffer rules (uniform alignment)
+ // m0 : float4 should be at offset 0
+ // m1 : float should be at offset 16 (after float4)
+ // m2 : float3 should be at offset 32 (aligned to 16-byte boundary)
+ SLANG_CHECK(structLayout->getFieldCount() == 3);
+ SLANG_CHECK(structLayout->getFieldByIndex(0)->getOffset() == 0);
+ SLANG_CHECK(structLayout->getFieldByIndex(1)->getOffset() == 16);
+ SLANG_CHECK(structLayout->getFieldByIndex(2)->getOffset() == 32);
+ };
+
+ testBody();
+}
+
+SLANG_UNIT_TEST(metalArgumentBufferLayout)
+{
+ const char* testSource = R"(
+ struct T
+ {
+ float4 m0;
+ float m1;
+ float3 m2;
+ };
+
+ // Using ParameterBlock directly without ConstantBuffer wrapper
+ ParameterBlock<T> params;
+ )";
+
+ ComPtr<slang::IGlobalSession> globalSession;
+ SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
+
+ slang::TargetDesc targetDesc = {};
+ targetDesc.format = SLANG_METAL;
+ targetDesc.profile = globalSession->findProfile("metal");
+
+ 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(
+ "test",
+ "test.slang",
+ testSource,
+ diagnosticBlob.writeRef());
+ SLANG_CHECK(module != nullptr);
+
+ auto testBody = [&]()
+ {
+ auto reflection = module->getLayout();
+
+ // Collect our layouts
+ auto paramBlockType = reflection->findTypeByName("ParameterBlock<T>");
+ SLANG_CHECK(paramBlockType != nullptr);
+ auto paramBlockLayout = reflection->getTypeLayout(paramBlockType);
+ SLANG_CHECK(paramBlockLayout != nullptr);
+ auto structLayout = paramBlockLayout->getElementTypeLayout();
+ SLANG_CHECK(structLayout != nullptr);
+
+ // Check that offsets follow Metal argument buffer rules
+ // Fields should have 0 offset and meaningful binding indices
+ SLANG_CHECK(structLayout->getFieldCount() == 3);
+ SLANG_CHECK(structLayout->getFieldByIndex(0)->getOffset() == 0);
+ SLANG_CHECK(structLayout->getFieldByIndex(1)->getOffset() == 0);
+ SLANG_CHECK(structLayout->getFieldByIndex(2)->getOffset() == 0);
+ SLANG_CHECK(structLayout->getFieldByIndex(0)->getBindingIndex() == 0);
+ SLANG_CHECK(structLayout->getFieldByIndex(1)->getBindingIndex() == 1);
+ SLANG_CHECK(structLayout->getFieldByIndex(2)->getBindingIndex() == 2);
+ };
+
+ testBody();
+}