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
|
#include "example-base.h"
#include <chrono>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
using namespace Slang;
using namespace gfx;
Slang::Result WindowedAppBase::initializeBase(const char* title, int width, int height)
{
// Create a window for our application to render into.
//
platform::WindowDesc windowDesc;
windowDesc.title = title;
windowDesc.width = width;
windowDesc.height = height;
windowWidth = width;
windowHeight = height;
windowDesc.style = platform::WindowStyle::Default;
gWindow = platform::Application::createWindow(windowDesc);
gWindow->events.mainLoop = [this]() { mainLoop(); };
gWindow->events.sizeChanged = Slang::Action<>(this, &WindowedAppBase::windowSizeChanged);
// Initialize the rendering layer.
IDevice::Desc deviceDesc = {};
// deviceDesc.slang.targetFlags = SLANG_TARGET_FLAG_DUMP_IR;
gfx::Result res = gfxCreateDevice(&deviceDesc, gDevice.writeRef());
if (SLANG_FAILED(res))
return res;
auto deviceInfo = gDevice->getDeviceInfo();
Slang::StringBuilder titleSb;
titleSb << title << " (" << deviceInfo.apiName << ": " << deviceInfo.adapterName << ")";
gWindow->setText(titleSb.getBuffer());
ICommandQueue::Desc queueDesc = {};
queueDesc.type = ICommandQueue::QueueType::Graphics;
gQueue = gDevice->createCommandQueue(queueDesc);
// Create swapchain and framebuffers.
gfx::ISwapchain::Desc swapchainDesc = {};
swapchainDesc.format = gfx::Format::RGBA_Unorm_UInt8;
swapchainDesc.width = width;
swapchainDesc.height = height;
swapchainDesc.imageCount = kSwapchainImageCount;
swapchainDesc.queue = gQueue;
gfx::WindowHandle windowHandle = gWindow->getNativeHandle().convert<gfx::WindowHandle>();
gSwapchain = gDevice->createSwapchain(swapchainDesc, windowHandle);
IFramebufferLayout::AttachmentLayout renderTargetLayout = {gSwapchain->getDesc().format, 1};
IFramebufferLayout::AttachmentLayout depthLayout = {gfx::Format::D_Float32, 1};
IFramebufferLayout::Desc framebufferLayoutDesc;
framebufferLayoutDesc.renderTargetCount = 1;
framebufferLayoutDesc.renderTargets = &renderTargetLayout;
framebufferLayoutDesc.depthStencil = &depthLayout;
SLANG_RETURN_ON_FAIL(
gDevice->createFramebufferLayout(framebufferLayoutDesc, gFramebufferLayout.writeRef()));
createSwapchainFramebuffers();
for (uint32_t i = 0; i < kSwapchainImageCount; i++)
{
gfx::ITransientResourceHeap::Desc transientHeapDesc;
transientHeapDesc.constantBufferSize = 4096 * 1024;
auto transientHeap = gDevice->createTransientResourceHeap(transientHeapDesc);
gTransientHeaps.add(transientHeap);
}
gfx::IRenderPassLayout::Desc renderPassDesc = {};
renderPassDesc.framebufferLayout = gFramebufferLayout;
renderPassDesc.renderTargetCount = 1;
IRenderPassLayout::AttachmentAccessDesc renderTargetAccess = {};
IRenderPassLayout::AttachmentAccessDesc depthStencilAccess = {};
renderTargetAccess.loadOp = IRenderPassLayout::AttachmentLoadOp::Clear;
renderTargetAccess.storeOp = IRenderPassLayout::AttachmentStoreOp::Store;
renderTargetAccess.initialState = ResourceState::Undefined;
renderTargetAccess.finalState = ResourceState::Present;
depthStencilAccess.loadOp = IRenderPassLayout::AttachmentLoadOp::Clear;
depthStencilAccess.storeOp = IRenderPassLayout::AttachmentStoreOp::Store;
depthStencilAccess.initialState = ResourceState::Undefined;
depthStencilAccess.finalState = ResourceState::DepthWrite;
renderPassDesc.renderTargetAccess = &renderTargetAccess;
renderPassDesc.depthStencilAccess = &depthStencilAccess;
gRenderPass = gDevice->createRenderPassLayout(renderPassDesc);
return SLANG_OK;
}
void WindowedAppBase::mainLoop()
{
int frameBufferIndex = gSwapchain->acquireNextImage();
if (frameBufferIndex == -1)
return;
gTransientHeaps[frameBufferIndex]->synchronizeAndReset();
renderFrame(frameBufferIndex);
}
void WindowedAppBase::createSwapchainFramebuffers()
{
gFramebuffers.clear();
for (uint32_t i = 0; i < kSwapchainImageCount; i++)
{
gfx::ITextureResource::Desc depthBufferDesc;
depthBufferDesc.type = IResource::Type::Texture2D;
depthBufferDesc.size.width = gSwapchain->getDesc().width;
depthBufferDesc.size.height = gSwapchain->getDesc().height;
depthBufferDesc.size.depth = 1;
depthBufferDesc.format = gfx::Format::D_Float32;
depthBufferDesc.defaultState = ResourceState::DepthWrite;
depthBufferDesc.allowedStates = ResourceStateSet(ResourceState::DepthWrite);
ComPtr<gfx::ITextureResource> depthBufferResource =
gDevice->createTextureResource(depthBufferDesc, nullptr);
ComPtr<gfx::ITextureResource> colorBuffer;
gSwapchain->getImage(i, colorBuffer.writeRef());
gfx::IResourceView::Desc colorBufferViewDesc;
memset(&colorBufferViewDesc, 0, sizeof(colorBufferViewDesc));
colorBufferViewDesc.format = gSwapchain->getDesc().format;
colorBufferViewDesc.renderTarget.shape = gfx::IResource::Type::Texture2D;
colorBufferViewDesc.type = gfx::IResourceView::Type::RenderTarget;
ComPtr<gfx::IResourceView> rtv =
gDevice->createTextureView(colorBuffer.get(), colorBufferViewDesc);
gfx::IResourceView::Desc depthBufferViewDesc;
memset(&depthBufferViewDesc, 0, sizeof(depthBufferViewDesc));
depthBufferViewDesc.format = gfx::Format::D_Float32;
depthBufferViewDesc.renderTarget.shape = gfx::IResource::Type::Texture2D;
depthBufferViewDesc.type = gfx::IResourceView::Type::DepthStencil;
ComPtr<gfx::IResourceView> dsv =
gDevice->createTextureView(depthBufferResource.get(), depthBufferViewDesc);
gfx::IFramebuffer::Desc framebufferDesc;
framebufferDesc.renderTargetCount = 1;
framebufferDesc.depthStencilView = dsv.get();
framebufferDesc.renderTargetViews = rtv.readRef();
framebufferDesc.layout = gFramebufferLayout;
ComPtr<gfx::IFramebuffer> frameBuffer = gDevice->createFramebuffer(framebufferDesc);
gFramebuffers.add(frameBuffer);
}
}
void WindowedAppBase::windowSizeChanged()
{
// Wait for the GPU to finish.
gQueue->wait();
auto clientRect = gWindow->getClientRect();
if (clientRect.width > 0 && clientRect.height > 0)
{
// Free all framebuffers before resizing swapchain.
gFramebuffers = decltype(gFramebuffers)();
// Resize swapchain.
if (gSwapchain->resize(clientRect.width, clientRect.height) == SLANG_OK)
{
// Recreate framebuffers for each swapchain back buffer image.
createSwapchainFramebuffers();
windowWidth = clientRect.width;
windowHeight = clientRect.height;
}
}
}
int64_t getCurrentTime() { return std::chrono::high_resolution_clock::now().time_since_epoch().count(); }
int64_t getTimerFrequency() { return std::chrono::high_resolution_clock::period::den; }
#ifdef _WIN32
void _Win32OutputDebugString(const char* str) { OutputDebugStringW(Slang::String(str).toWString().begin()); }
#endif
|