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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
#if 0
// Duplicated: This test is similar to slang-rhi\tests\test-surface.cpp
#include "gfx-test-util.h"
#include "platform/window.h"
#include "unit-test/slang-unit-test.h"
#include <slang-rhi.h>
#include <slang-rhi/shader-cursor.h>
using namespace rhi;
namespace gfx_test
{
struct Vertex
{
float position[3];
};
static const int kVertexCount = 3;
static const Vertex kVertexData[kVertexCount] = {
{0, 0, 1},
{4, 0, 1},
{0, 4, 1},
};
struct SwapchainResizeTest
{
IDevice* device;
UnitTestContext* context;
ComPtr<platform::Window> window;
ComPtr<ICommandQueue> queue;
ComPtr<ISurface> surface;
ComPtr<ITexture> swapchainImages[2];
uint32_t swapchainImageCount = 2;
Format desiredFormat = Format::RGBA8Unorm;
ComPtr<IBuffer> vertexBuffer;
ComPtr<IInputLayout> inputLayout;
ComPtr<IRenderPipeline> pipeline;
ComPtr<IShaderProgram> shaderProgram;
ComPtr<IShaderObject> rootShaderObject;
uint32_t width = 500;
uint32_t height = 500;
void init(IDevice* device, UnitTestContext* context)
{
this->device = device;
this->context = context;
}
void createSwapchainAndResources()
{
// Create window
platform::Application::init();
platform::WindowDesc windowDesc;
windowDesc.title = "";
windowDesc.width = width;
windowDesc.height = height;
windowDesc.style = platform::WindowStyle::Default;
window = platform::Application::createWindow(windowDesc);
// Create surface
WindowHandle windowHandle = WindowHandle::fromHwnd((void*)window->getNativeHandle().handleValues[0]);
surface = device->createSurface(windowHandle);
// Configure surface (swapchain)
SurfaceConfig config = {};
config.format = desiredFormat;
config.width = width;
config.height = height;
config.desiredImageCount = swapchainImageCount;
config.vsync = true;
surface->configure(config);
// Create vertex buffer
BufferDesc vertexBufferDesc = {};
vertexBufferDesc.size = sizeof(Vertex) * kVertexCount;
vertexBufferDesc.memoryType = MemoryType::DeviceLocal;
vertexBufferDesc.usage = BufferUsage::VertexBuffer;
vertexBufferDesc.defaultState = ResourceState::VertexBuffer;
vertexBuffer = device->createBuffer(vertexBufferDesc, kVertexData);
// Input layout
InputElementDesc inputElements[] = {
{"POSITIONA", 0, Format::RGB32Float, offsetof(Vertex, position), 0},
};
VertexStreamDesc vertexStreams[] = {
{sizeof(Vertex), InputSlotClass::PerVertex, 0},
};
InputLayoutDesc inputLayoutDesc = {};
inputLayoutDesc.inputElementCount = sizeof(inputElements) / sizeof(InputElementDesc);
inputLayoutDesc.inputElements = inputElements;
inputLayoutDesc.vertexStreamCount = sizeof(vertexStreams) / sizeof(VertexStreamDesc);;
inputLayoutDesc.vertexStreams = vertexStreams;
GFX_CHECK_CALL_ABORT(device->createInputLayout(inputLayoutDesc, inputLayout.writeRef()));
ComPtr<IShaderProgram> shaderProgram;
slang::ProgramLayout* slangReflection = nullptr;
GFX_CHECK_CALL_ABORT(loadGraphicsProgram(
device,
shaderProgram,
"swapchain-shader",
"vertexMain",
"fragmentMain",
slangReflection
));
// Pipeline
ColorTargetDesc colorTarget = {};
colorTarget.format = desiredFormat;
RenderPipelineDesc pipelineDesc = {};
pipelineDesc.program = shaderProgram.get();
pipelineDesc.inputLayout = inputLayout.get();
pipelineDesc.primitiveTopology = PrimitiveTopology::TriangleList;
pipelineDesc.targets = &colorTarget;
pipelineDesc.targetCount = 1;
pipeline = device->createRenderPipeline(pipelineDesc);
}
void renderFrame(uint32_t imageIndex)
{
// Acquire next image
ComPtr<ITexture> backBuffer;
if (SLANG_FAILED(surface->acquireNextImage(backBuffer.writeRef())))
{
return;
}
// Create command encoder
ComPtr<ICommandQueue> queue = device->getQueue(QueueType::Graphics);
ComPtr<ICommandEncoder> encoder = queue->createCommandEncoder();
// Render pass setup
RenderPassColorAttachment colorAttachment = {};
colorAttachment.view = backBuffer->getDefaultView();
colorAttachment.loadOp = LoadOp::Clear;
colorAttachment.storeOp = StoreOp::Store;
float clearColor[4] = {0.2f, 0.2f, 0.2f, 1.0f};
memcpy(colorAttachment.clearValue, clearColor, sizeof(clearColor));
RenderPassDesc passDesc = {};
passDesc.colorAttachments = &colorAttachment;
passDesc.colorAttachmentCount = 1;
// Begin render pass
auto pass = encoder->beginRenderPass(passDesc);
// Bind pipeline and root object
pass->bindPipeline(pipeline, rootShaderObject);
// Set render state
RenderState state = {};
state.vertexBuffers[0] = BufferOffsetPair(vertexBuffer, 0);
state.vertexBufferCount = 1;
// Set viewport
Viewport viewport = Viewport::fromSize((float)width, (float)height);
state.viewportCount = 1;
state.viewports[0] = viewport;
pass->setRenderState(state);
// Draw
DrawArguments args = {};
args.vertexCount = kVertexCount;
pass->draw(args);
pass->end();
ComPtr<ICommandBuffer> cmdBuffer;
encoder->finish(cmdBuffer.writeRef());
queue->submit(cmdBuffer);
// Present
surface->present();
}
void run()
{
createSwapchainAndResources();
for (uint32_t i = 0; i < 5; ++i)
{
renderFrame(i % swapchainImageCount);
}
queue->waitOnHost();
// Resize swapchain
width = 700;
height = 700;
SurfaceConfig config = surface->getConfig();
config.width = width;
config.height = height;
surface->configure(config);
for (uint32_t i = 0; i < 5; ++i)
{
renderFrame(i % swapchainImageCount);
}
queue->waitOnHost();
}
};
void swapchainResizeTestImpl(IDevice* device, UnitTestContext* context)
{
SwapchainResizeTest t;
t.init(device, context);
t.run();
}
SLANG_UNIT_TEST(swapchainResizeD3D12)
{
runTestImpl(swapchainResizeTestImpl, unitTestContext, DeviceType::D3D12);
}
SLANG_UNIT_TEST(swapchainResizeVulkan)
{
runTestImpl(swapchainResizeTestImpl, unitTestContext, DeviceType::Vulkan);
}
} // namespace gfx_test
#endif
|