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
|
#include "example-base.h"
#include "slang.h"
#include <chrono>
#ifdef _WIN32
#include <windows.h>
#endif
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
using namespace Slang;
using namespace rhi;
class DebugCallback : public rhi::IDebugCallback
{
public:
virtual SLANG_NO_THROW void SLANG_MCALL handleMessage(
rhi::DebugMessageType type,
rhi::DebugMessageSource source,
const char* message) override
{
const char* typeStr = "";
switch (type)
{
case rhi::DebugMessageType::Info:
typeStr = "INFO: ";
break;
case rhi::DebugMessageType::Warning:
typeStr = "WARNING: ";
break;
case rhi::DebugMessageType::Error:
typeStr = "ERROR: ";
break;
default:
break;
}
const char* sourceStr = "[GraphicsLayer]: ";
switch (source)
{
case rhi::DebugMessageSource::Slang:
sourceStr = "[Slang]: ";
break;
case rhi::DebugMessageSource::Driver:
sourceStr = "[Driver]: ";
break;
}
printf("%s%s%s\n", sourceStr, typeStr, message);
#ifdef _WIN32
OutputDebugStringA(sourceStr);
OutputDebugStringA(typeStr);
OutputDebugStringW(String(message).toWString());
OutputDebugStringW(L"\n");
#endif
}
};
Slang::Result WindowedAppBase::initializeBase(
const char* title,
int width,
int height,
DeviceType deviceType)
{
DeviceDesc deviceDesc = {};
deviceDesc.deviceType = deviceType;
// Enable validation when not in test mode to avoid output pollution during testing
deviceDesc.enableValidation = !isTestMode();
// Set debug callback (only used when validation is enabled, i.e., non-test mode)
static DebugCallback debugCallback;
deviceDesc.debugCallback = &debugCallback;
slang::CompilerOptionEntry slangOptions[] = {
{slang::CompilerOptionName::EmitSpirvDirectly, {slang::CompilerOptionValueKind::Int, 1}},
{slang::CompilerOptionName::DebugInformation,
{slang::CompilerOptionValueKind::Int, SLANG_DEBUG_INFO_LEVEL_STANDARD}}};
deviceDesc.slang.compilerOptionEntries = slangOptions;
// When in test mode, don't include debug information to avoid altering hash values during
// testing Otherwise, include debug information for better debugging experience
deviceDesc.slang.compilerOptionEntryCount = isTestMode() ? 1 : 2;
gDevice = getRHI()->createDevice(deviceDesc);
if (!gDevice)
{
return SLANG_FAIL;
}
gQueue = gDevice->getQueue(QueueType::Graphics);
windowWidth = width;
windowHeight = height;
// Do not create swapchain and windows in test mode, because there won't be any display.
if (!isTestMode())
{
// Create a window for our application to render into.
//
platform::WindowDesc windowDesc;
windowDesc.title = title;
windowDesc.width = width;
windowDesc.height = height;
windowDesc.style = platform::WindowStyle::Default;
gWindow = platform::Application::createWindow(windowDesc);
gWindow->events.mainLoop = [this]() { mainLoop(); };
gWindow->events.sizeChanged = Slang::Action<>(this, &WindowedAppBase::windowSizeChanged);
WindowHandle windowHandle = gWindow->getNativeHandle().convert<WindowHandle>();
gSurface = gDevice->createSurface(windowHandle);
auto deviceInfo = gDevice->getInfo();
Slang::StringBuilder titleSb;
titleSb << title << " (" << deviceInfo.apiName << ": " << deviceInfo.adapterName << ")";
gWindow->setText(titleSb.getBuffer());
rhi::SurfaceConfig surfaceConfig = {};
surfaceConfig.format = gSurface->getInfo().preferredFormat;
surfaceConfig.width = width;
surfaceConfig.height = height;
surfaceConfig.desiredImageCount = kSwapchainImageCount;
gSurface->configure(surfaceConfig);
}
else
{
createOfflineTextures();
}
return SLANG_OK;
}
void WindowedAppBase::mainLoop()
{
auto texture = gSurface->acquireNextImage();
renderFrame(texture);
}
ComPtr<ITextureView> WindowedAppBase::createTextureFromFile(
String fileName,
int& textureWidth,
int& textureHeight)
{
int channelsInFile = 0;
auto textureContent =
stbi_load(fileName.getBuffer(), &textureWidth, &textureHeight, &channelsInFile, 4);
TextureDesc textureDesc = {};
textureDesc.type = TextureType::Texture2D;
textureDesc.usage = TextureUsage::ShaderResource;
textureDesc.format = Format::RGBA8Unorm;
textureDesc.mipCount = Math::Log2Ceil(Math::Min(textureWidth, textureHeight)) + 1;
textureDesc.size.width = textureWidth;
textureDesc.size.height = textureHeight;
textureDesc.size.depth = 1;
List<SubresourceData> subresData;
List<List<uint32_t>> mipMapData;
mipMapData.setCount(textureDesc.mipCount);
subresData.setCount(textureDesc.mipCount);
mipMapData[0].setCount(textureWidth * textureHeight);
memcpy(mipMapData[0].getBuffer(), textureContent, textureWidth * textureHeight * 4);
stbi_image_free(textureContent);
subresData[0].data = mipMapData[0].getBuffer();
subresData[0].rowPitch = textureWidth * 4;
subresData[0].slicePitch = textureWidth * textureHeight * 4;
// Build mipmaps.
struct RGBA
{
uint8_t v[4];
};
auto castToRGBA = [](uint32_t v)
{
RGBA result;
memcpy(&result, &v, 4);
return result;
};
auto castToUint = [](RGBA v)
{
uint32_t result;
memcpy(&result, &v, 4);
return result;
};
int lastMipWidth = textureWidth;
int lastMipHeight = textureHeight;
for (uint32_t m = 1; m < textureDesc.mipCount; m++)
{
auto lastMipmapData = mipMapData[m - 1].getBuffer();
int w = lastMipWidth / 2;
int h = lastMipHeight / 2;
mipMapData[m].setCount(w * h);
subresData[m].data = mipMapData[m].getBuffer();
subresData[m].rowPitch = w * 4;
subresData[m].slicePitch = h * w * 4;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
auto pix1 = castToRGBA(lastMipmapData[(y * 2) * lastMipWidth + (x * 2)]);
auto pix2 = castToRGBA(lastMipmapData[(y * 2) * lastMipWidth + (x * 2 + 1)]);
auto pix3 = castToRGBA(lastMipmapData[(y * 2 + 1) * lastMipWidth + (x * 2)]);
auto pix4 = castToRGBA(lastMipmapData[(y * 2 + 1) * lastMipWidth + (x * 2 + 1)]);
RGBA pix;
for (int c = 0; c < 4; c++)
{
pix.v[c] =
(uint8_t)(((uint32_t)pix1.v[c] + pix2.v[c] + pix3.v[c] + pix4.v[c]) / 4);
}
mipMapData[m][y * w + x] = castToUint(pix);
}
}
lastMipWidth = w;
lastMipHeight = h;
}
auto texture = gDevice->createTexture(textureDesc, subresData.getBuffer());
TextureViewDesc viewDesc = {};
return gDevice->createTextureView(texture.get(), viewDesc);
}
void WindowedAppBase::createOfflineTextures()
{
for (uint32_t i = 0; i < kSwapchainImageCount; i++)
{
TextureDesc textureDesc = {};
textureDesc.size.width = this->windowWidth;
textureDesc.size.height = this->windowHeight;
textureDesc.format = Format::RGBA8Unorm;
textureDesc.mipCount = 1;
textureDesc.usage = TextureUsage::UnorderedAccess | TextureUsage::CopySource;
auto texture = gDevice->createTexture(textureDesc);
gOfflineTextures.add(texture);
}
}
void WindowedAppBase::offlineRender()
{
SLANG_ASSERT(gOfflineTextures.getCount() > 0);
renderFrame(gOfflineTextures[0]);
}
void WindowedAppBase::windowSizeChanged()
{
// Wait for the GPU to finish.
gQueue->waitOnHost();
auto clientRect = gWindow->getClientRect();
if (clientRect.width > 0 && clientRect.height > 0)
{
SurfaceConfig config = {};
config.format = gSurface->getInfo().preferredFormat;
config.width = clientRect.width;
config.height = clientRect.height;
config.vsync = false;
gSurface->configure(config);
}
}
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
|