blob: d04cea974ffb24eeac714d829c522edd56d86d6d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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();
}
|