-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathResize.cpp
More file actions
394 lines (306 loc) · 15.3 KB
/
Resize.cpp
File metadata and controls
394 lines (306 loc) · 15.3 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// © 2023 NVIDIA Corporation
#include "NRIFramework.h"
#define SWITCH_TIME 2.5f
#define NOT_ALLOW_TEARING nri::SwapChainBits::NONE // no "ALLOW_TEARING" to avoid getting a VIDMODE switch caused by the VK driver
#define SCALING nri::Scaling::ONE_TO_ONE // looks nicer
struct QueuedFrame {
nri::CommandAllocator* commandAllocator;
nri::CommandBuffer* commandBuffer;
};
class Sample : public SampleBase {
public:
Sample() {
m_Resizable = true;
}
~Sample();
bool Initialize(nri::GraphicsAPI graphicsAPI, bool) override;
void LatencySleep(uint32_t frameIndex) override;
void PrepareFrame(uint32_t frameIndex) override;
void RenderFrame(uint32_t frameIndex) override;
void ResizeSwapChain();
private:
NRIInterface NRI = {};
nri::Device* m_Device = nullptr;
nri::Streamer* m_Streamer = nullptr;
nri::SwapChain* m_SwapChain = nullptr;
nri::Queue* m_GraphicsQueue = nullptr;
nri::Fence* m_FrameFence = nullptr;
std::vector<QueuedFrame> m_QueuedFrames = {};
std::vector<nri::Memory*> m_MemoryAllocations;
std::vector<SwapChainTexture> m_SwapChainTextures;
float m_Time = SWITCH_TIME;
uint2 m_PrevWindowResolution;
bool m_IsFullscreen = false;
};
Sample::~Sample() {
if (NRI.HasCore()) {
NRI.DeviceWaitIdle(m_Device);
for (QueuedFrame& queuedFrame : m_QueuedFrames) {
NRI.DestroyCommandBuffer(queuedFrame.commandBuffer);
NRI.DestroyCommandAllocator(queuedFrame.commandAllocator);
}
for (SwapChainTexture& swapChainTexture : m_SwapChainTextures) {
NRI.DestroyFence(swapChainTexture.acquireSemaphore);
NRI.DestroyFence(swapChainTexture.releaseSemaphore);
NRI.DestroyDescriptor(swapChainTexture.colorAttachment);
}
NRI.DestroyFence(m_FrameFence);
for (size_t i = 0; i < m_MemoryAllocations.size(); i++)
NRI.FreeMemory(m_MemoryAllocations[i]);
}
if (NRI.HasSwapChain())
NRI.DestroySwapChain(m_SwapChain);
if (NRI.HasStreamer())
NRI.DestroyStreamer(m_Streamer);
DestroyImgui();
nri::nriDestroyDevice(m_Device);
}
bool Sample::Initialize(nri::GraphicsAPI graphicsAPI, bool) {
m_PrevWindowResolution = m_OutputResolution;
// Adapters
nri::AdapterDesc adapterDesc[2] = {};
uint32_t adapterDescsNum = helper::GetCountOf(adapterDesc);
NRI_ABORT_ON_FAILURE(nri::nriEnumerateAdapters(adapterDesc, adapterDescsNum));
// Device
nri::DeviceCreationDesc deviceCreationDesc = {};
deviceCreationDesc.graphicsAPI = graphicsAPI;
deviceCreationDesc.enableGraphicsAPIValidation = m_DebugAPI;
deviceCreationDesc.enableNRIValidation = m_DebugNRI;
deviceCreationDesc.enableD3D11CommandBufferEmulation = D3D11_ENABLE_COMMAND_BUFFER_EMULATION;
deviceCreationDesc.disableD3D12EnhancedBarriers = D3D12_DISABLE_ENHANCED_BARRIERS;
deviceCreationDesc.vkBindingOffsets = VK_BINDING_OFFSETS;
deviceCreationDesc.adapterDesc = &adapterDesc[std::min(m_AdapterIndex, adapterDescsNum - 1)];
deviceCreationDesc.allocationCallbacks = m_AllocationCallbacks;
NRI_ABORT_ON_FAILURE(nri::nriCreateDevice(deviceCreationDesc, m_Device));
// NRI
NRI_ABORT_ON_FAILURE(nri::nriGetInterface(*m_Device, NRI_INTERFACE(nri::CoreInterface), (nri::CoreInterface*)&NRI));
NRI_ABORT_ON_FAILURE(nri::nriGetInterface(*m_Device, NRI_INTERFACE(nri::HelperInterface), (nri::HelperInterface*)&NRI));
NRI_ABORT_ON_FAILURE(nri::nriGetInterface(*m_Device, NRI_INTERFACE(nri::StreamerInterface), (nri::StreamerInterface*)&NRI));
NRI_ABORT_ON_FAILURE(nri::nriGetInterface(*m_Device, NRI_INTERFACE(nri::SwapChainInterface), (nri::SwapChainInterface*)&NRI));
// Create streamer
nri::StreamerDesc streamerDesc = {};
streamerDesc.dynamicBufferMemoryLocation = nri::MemoryLocation::HOST_UPLOAD;
streamerDesc.dynamicBufferDesc = {0, 0, nri::BufferUsageBits::VERTEX_BUFFER | nri::BufferUsageBits::INDEX_BUFFER};
streamerDesc.constantBufferMemoryLocation = nri::MemoryLocation::HOST_UPLOAD;
streamerDesc.queuedFrameNum = GetQueuedFrameNum();
NRI_ABORT_ON_FAILURE(NRI.CreateStreamer(*m_Device, streamerDesc, m_Streamer));
// Command queue
NRI_ABORT_ON_FAILURE(NRI.GetQueue(*m_Device, nri::QueueType::GRAPHICS, 0, m_GraphicsQueue));
// Fences
NRI_ABORT_ON_FAILURE(NRI.CreateFence(*m_Device, 0, m_FrameFence));
{ // Swap chain
nri::SwapChainDesc swapChainDesc = {};
swapChainDesc.window = GetWindow();
swapChainDesc.queue = m_GraphicsQueue;
swapChainDesc.format = nri::SwapChainFormat::BT709_G22_8BIT;
swapChainDesc.flags = (m_Vsync ? nri::SwapChainBits::VSYNC : nri::SwapChainBits::NONE) | NOT_ALLOW_TEARING;
swapChainDesc.width = (uint16_t)m_OutputResolution.x;
swapChainDesc.height = (uint16_t)m_OutputResolution.y;
swapChainDesc.textureNum = GetOptimalSwapChainTextureNum();
swapChainDesc.queuedFrameNum = GetQueuedFrameNum();
swapChainDesc.scaling = SCALING;
NRI_ABORT_ON_FAILURE(NRI.CreateSwapChain(*m_Device, swapChainDesc, m_SwapChain));
uint32_t swapChainTextureNum;
nri::Texture* const* swapChainTextures = NRI.GetSwapChainTextures(*m_SwapChain, swapChainTextureNum);
nri::Format swapChainFormat = NRI.GetTextureDesc(*swapChainTextures[0]).format;
for (uint32_t i = 0; i < swapChainTextureNum; i++) {
nri::TextureViewDesc textureViewDesc = {swapChainTextures[i], nri::TextureView::COLOR_ATTACHMENT, swapChainFormat};
nri::Descriptor* colorAttachment = nullptr;
NRI_ABORT_ON_FAILURE(NRI.CreateTextureView(textureViewDesc, colorAttachment));
nri::Fence* acquireSemaphore = nullptr;
NRI_ABORT_ON_FAILURE(NRI.CreateFence(*m_Device, nri::SWAPCHAIN_SEMAPHORE, acquireSemaphore));
nri::Fence* releaseSemaphore = nullptr;
NRI_ABORT_ON_FAILURE(NRI.CreateFence(*m_Device, nri::SWAPCHAIN_SEMAPHORE, releaseSemaphore));
SwapChainTexture& swapChainTexture = m_SwapChainTextures.emplace_back();
swapChainTexture = {};
swapChainTexture.acquireSemaphore = acquireSemaphore;
swapChainTexture.releaseSemaphore = releaseSemaphore;
swapChainTexture.texture = swapChainTextures[i];
swapChainTexture.colorAttachment = colorAttachment;
swapChainTexture.attachmentFormat = swapChainFormat;
}
}
// Queued frames
m_QueuedFrames.resize(GetQueuedFrameNum());
for (QueuedFrame& queuedFrame : m_QueuedFrames) {
NRI_ABORT_ON_FAILURE(NRI.CreateCommandAllocator(*m_GraphicsQueue, queuedFrame.commandAllocator));
NRI_ABORT_ON_FAILURE(NRI.CreateCommandBuffer(*queuedFrame.commandAllocator, queuedFrame.commandBuffer));
}
return InitImgui(*m_Device);
}
void Sample::LatencySleep(uint32_t frameIndex) {
uint32_t queuedFrameIndex = frameIndex % GetQueuedFrameNum();
const QueuedFrame& queuedFrame = m_QueuedFrames[queuedFrameIndex];
NRI.Wait(*m_FrameFence, frameIndex >= GetQueuedFrameNum() ? 1 + frameIndex - GetQueuedFrameNum() : 0);
NRI.ResetCommandAllocator(*queuedFrame.commandAllocator);
}
void Sample::PrepareFrame(uint32_t) {
// Info text
m_Time -= m_Timer.GetSmoothedFrameTime() / 1000.0f;
m_Time = std::max(m_Time, 0.0f);
char s[64];
if (m_IsFullscreen)
snprintf(s, sizeof(s), "Going windowed in %.1f...", m_Time);
else
snprintf(s, sizeof(s), "Going fullscreen in %.1f...", m_Time);
// Resize
if (m_Time == 0.0f) {
m_IsFullscreen = !m_IsFullscreen;
m_Time = SWITCH_TIME;
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* vidmode = glfwGetVideoMode(monitor);
uint32_t w = (uint32_t)vidmode->width;
uint32_t h = (uint32_t)vidmode->height;
if (m_IsFullscreen)
m_OutputResolution = uint2(w, h);
else
m_OutputResolution = m_PrevWindowResolution;
uint32_t x = (w - m_OutputResolution.x) >> 1;
uint32_t y = (h - m_OutputResolution.y) >> 1;
glfwSetWindowAttrib(m_Window, GLFW_DECORATED, m_IsFullscreen ? 0 : 1);
#if (NRIF_PLATFORM != NRIF_WAYLAND)
glfwSetWindowPos(m_Window, x, y);
#endif
glfwSetWindowSize(m_Window, m_OutputResolution.x, m_OutputResolution.y);
ResizeSwapChain();
}
// UI
ImGui::NewFrame();
{
ImVec2 dims = ImGui::CalcTextSize(s);
ImVec2 p;
p.x = ((float)m_OutputResolution.x - dims.x) * 0.5f;
p.y = ((float)m_OutputResolution.y - dims.y) * 0.5f;
ImGui::SetNextWindowPos(p);
ImGui::Begin("Color", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize);
{
ImGui::Text("%s", s);
}
ImGui::End();
}
ImGui::EndFrame();
ImGui::Render();
}
void Sample::ResizeSwapChain() {
// Wait for idle
NRI.QueueWaitIdle(m_GraphicsQueue);
// Destroy old swapchain
for (SwapChainTexture& swapChainTexture : m_SwapChainTextures) {
NRI.DestroyFence(swapChainTexture.acquireSemaphore);
NRI.DestroyFence(swapChainTexture.releaseSemaphore);
NRI.DestroyDescriptor(swapChainTexture.colorAttachment);
}
NRI.DestroySwapChain(m_SwapChain);
// Create new swapchain
nri::SwapChainDesc swapChainDesc = {};
swapChainDesc.window = GetWindow();
swapChainDesc.queue = m_GraphicsQueue;
swapChainDesc.format = nri::SwapChainFormat::BT709_G22_8BIT;
swapChainDesc.flags = (m_Vsync ? nri::SwapChainBits::VSYNC : nri::SwapChainBits::NONE) | NOT_ALLOW_TEARING;
swapChainDesc.width = (uint16_t)m_OutputResolution.x;
swapChainDesc.height = (uint16_t)m_OutputResolution.y;
swapChainDesc.textureNum = GetOptimalSwapChainTextureNum();
swapChainDesc.queuedFrameNum = GetQueuedFrameNum();
swapChainDesc.scaling = SCALING;
NRI_ABORT_ON_FAILURE(NRI.CreateSwapChain(*m_Device, swapChainDesc, m_SwapChain));
uint32_t swapChainTextureNum;
nri::Texture* const* swapChainTextures = NRI.GetSwapChainTextures(*m_SwapChain, swapChainTextureNum);
nri::Format swapChainFormat = NRI.GetTextureDesc(*swapChainTextures[0]).format;
m_SwapChainTextures.clear();
for (uint32_t i = 0; i < swapChainTextureNum; i++) {
nri::TextureViewDesc textureViewDesc = {swapChainTextures[i], nri::TextureView::COLOR_ATTACHMENT, swapChainFormat};
nri::Descriptor* colorAttachment = nullptr;
NRI_ABORT_ON_FAILURE(NRI.CreateTextureView(textureViewDesc, colorAttachment));
nri::Fence* acquireSemaphore = nullptr;
NRI_ABORT_ON_FAILURE(NRI.CreateFence(*m_Device, nri::SWAPCHAIN_SEMAPHORE, acquireSemaphore));
nri::Fence* releaseSemaphore = nullptr;
NRI_ABORT_ON_FAILURE(NRI.CreateFence(*m_Device, nri::SWAPCHAIN_SEMAPHORE, releaseSemaphore));
SwapChainTexture& swapChainTexture = m_SwapChainTextures.emplace_back();
swapChainTexture = {};
swapChainTexture.acquireSemaphore = acquireSemaphore;
swapChainTexture.releaseSemaphore = releaseSemaphore;
swapChainTexture.texture = swapChainTextures[i];
swapChainTexture.colorAttachment = colorAttachment;
swapChainTexture.attachmentFormat = swapChainFormat;
}
}
void Sample::RenderFrame(uint32_t frameIndex) {
uint32_t queuedFrameIndex = frameIndex % GetQueuedFrameNum();
const QueuedFrame& queuedFrame = m_QueuedFrames[queuedFrameIndex];
// Acquire a swap chain texture
uint32_t recycledSemaphoreIndex = frameIndex % (uint32_t)m_SwapChainTextures.size();
nri::Fence* swapChainAcquireSemaphore = m_SwapChainTextures[recycledSemaphoreIndex].acquireSemaphore;
uint32_t currentSwapChainTextureIndex = 0;
NRI.AcquireNextTexture(*m_SwapChain, *swapChainAcquireSemaphore, currentSwapChainTextureIndex);
const SwapChainTexture& swapChainTexture = m_SwapChainTextures[currentSwapChainTextureIndex];
// Record
nri::CommandBuffer& commandBuffer = *queuedFrame.commandBuffer;
NRI.BeginCommandBuffer(commandBuffer, nullptr);
{
nri::TextureBarrierDesc textureBarriers = {};
textureBarriers.texture = swapChainTexture.texture;
textureBarriers.after = {nri::AccessBits::COPY_SOURCE, nri::Layout::COPY_SOURCE};
textureBarriers.layerNum = 1;
textureBarriers.mipNum = 1;
nri::BarrierDesc barrierDesc = {};
barrierDesc.textureNum = 1;
barrierDesc.textures = &textureBarriers;
NRI.CmdBarrier(commandBuffer, barrierDesc);
nri::TextureDataLayoutDesc dstDataLayoutDesc = {};
dstDataLayoutDesc.rowPitch = NRI.GetDeviceDesc(*m_Device).memoryAlignment.uploadBufferTextureRow;
textureBarriers.before = textureBarriers.after;
textureBarriers.after = {nri::AccessBits::COLOR_ATTACHMENT, nri::Layout::COLOR_ATTACHMENT};
NRI.CmdBarrier(commandBuffer, barrierDesc);
nri::AttachmentDesc colorAttachmentDesc = {};
colorAttachmentDesc.descriptor = swapChainTexture.colorAttachment;
nri::RenderingDesc renderingDesc = {};
renderingDesc.colorNum = 1;
renderingDesc.colors = &colorAttachmentDesc;
CmdCopyImguiData(commandBuffer, *m_Streamer);
NRI.CmdBeginRendering(commandBuffer, renderingDesc);
{
helper::Annotation annotation(NRI, commandBuffer, "Clear");
nri::ClearAttachmentDesc clearDesc = {};
clearDesc.planes = nri::PlaneBits::COLOR;
if (m_IsFullscreen)
clearDesc.value.color.f = {0.0f, 1.0f, 0.0f, 1.0f};
else
clearDesc.value.color.f = {1.0f, 0.0f, 0.0f, 1.0f};
NRI.CmdClearAttachments(commandBuffer, &clearDesc, 1, nullptr, 0);
CmdDrawImgui(commandBuffer, swapChainTexture.attachmentFormat, 1.0f, true);
}
NRI.CmdEndRendering(commandBuffer);
textureBarriers.before = textureBarriers.after;
textureBarriers.after = {nri::AccessBits::NONE, nri::Layout::PRESENT, nri::StageBits::NONE};
NRI.CmdBarrier(commandBuffer, barrierDesc);
}
NRI.EndCommandBuffer(commandBuffer);
{ // Submit
nri::FenceSubmitDesc textureAcquiredFence = {};
textureAcquiredFence.fence = swapChainAcquireSemaphore;
textureAcquiredFence.stages = nri::StageBits::COLOR_ATTACHMENT;
nri::FenceSubmitDesc renderingFinishedFence = {};
renderingFinishedFence.fence = swapChainTexture.releaseSemaphore;
nri::QueueSubmitDesc queueSubmitDesc = {};
queueSubmitDesc.waitFences = &textureAcquiredFence;
queueSubmitDesc.waitFenceNum = 1;
queueSubmitDesc.commandBuffers = &queuedFrame.commandBuffer;
queueSubmitDesc.commandBufferNum = 1;
queueSubmitDesc.signalFences = &renderingFinishedFence;
queueSubmitDesc.signalFenceNum = 1;
NRI.QueueSubmit(*m_GraphicsQueue, queueSubmitDesc);
}
NRI.EndStreamerFrame(*m_Streamer);
// Present
NRI.QueuePresent(*m_SwapChain, *swapChainTexture.releaseSemaphore);
{ // Signaling after "Present" improves D3D11 performance a bit
nri::FenceSubmitDesc signalFence = {};
signalFence.fence = m_FrameFence;
signalFence.value = 1 + frameIndex;
nri::QueueSubmitDesc queueSubmitDesc = {};
queueSubmitDesc.signalFences = &signalFence;
queueSubmitDesc.signalFenceNum = 1;
NRI.QueueSubmit(*m_GraphicsQueue, queueSubmitDesc);
}
}
SAMPLE_MAIN(Sample, 0);