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
|
#include "test-base.h"
#ifdef _WIN32
// clang-format off
// include ordering sensitive
# include <windows.h>
# include <shellapi.h>
# include <io.h>
# include <fcntl.h>
// clang-format on
#include <iostream>
#include <string>
#endif
static rhi::DeviceType parseApiString(const char* apiStr)
{
static const struct
{
const char* name;
rhi::DeviceType type;
} apiTable[] = {
#ifdef _WIN32
{"d3d11", rhi::DeviceType::D3D11},
{"d3d12", rhi::DeviceType::D3D12},
#endif
{"vulkan", rhi::DeviceType::Vulkan},
#ifdef __APPLE__
{"metal", rhi::DeviceType::Metal},
#endif
{"cpu", rhi::DeviceType::CPU},
{"cuda", rhi::DeviceType::CUDA},
{"webgpu", rhi::DeviceType::WGPU}};
for (auto& api : apiTable)
{
if (strcmp(apiStr, api.name) == 0)
{
return api.type;
}
}
return rhi::DeviceType::Default; // Invalid/unknown
}
#ifdef _WIN32
static std::string wstringToString(const std::wstring& wstr)
{
char buffer[256];
wcstombs(buffer, wstr.c_str(), sizeof(buffer));
return std::string(buffer);
}
#endif
// Simple output function that works for both console and WIN32 apps
static void printOutput(const char* text)
{
printf("%s", text);
#ifdef _WIN32
OutputDebugStringA(text);
#endif
}
#ifdef _WIN32
// For WIN32 apps, allocate a console window to show help text
static bool ensureConsoleVisible()
{
// Check if we already have a console (running from cmd.exe or already allocated)
if (GetConsoleWindow() != NULL)
{
// We already have a console, just use it
return false; // No new window created
}
// Check if stdout is connected to a console or redirected
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdOut != INVALID_HANDLE_VALUE)
{
DWORD fileType = GetFileType(hStdOut);
switch (fileType)
{
case FILE_TYPE_CHAR: // Console
case FILE_TYPE_DISK: // File redirection
case FILE_TYPE_PIPE: // Pipe redirection
// Output goes to console or is redirected, don't create a console
return false;
}
// FILE_TYPE_UNKNOWN or other cases: proceed to create console
}
// No console exists and output isn't redirected, so allocate a new one
if (AllocConsole())
{
// Redirect stdout to the console
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
freopen_s((FILE**)stderr, "CONOUT$", "w", stderr);
freopen_s((FILE**)stdin, "CONIN$", "r", stdin);
// Make sure cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
// point to console as well
std::ios::sync_with_stdio(true);
// Set console title
SetConsoleTitleA("Triangle Example - Help");
return true; // New window was created
}
return false; // Failed to create console
}
#endif
int TestBase::parseOption(int argc, char** argv)
{
// Parse command line arguments for help, API selection, and test mode
#ifdef _WIN32
wchar_t** szArglist;
szArglist = CommandLineToArgvW(GetCommandLineW(), &argc);
#endif
for (int i = 0; i < argc; i++)
{
#ifdef _WIN32
std::string arg = wstringToString(szArglist[i]);
#else
std::string arg = argv[i];
#endif
if (arg == "--test-mode" || arg == "-test-mode")
{
m_isTestMode = true;
}
else if (arg == "-h" || arg == "--help")
{
m_showHelp = true;
}
else if ((arg == "-api" || arg == "--api") && i + 1 < argc)
{
i++; // Move to the next argument which should be the API name
#ifdef _WIN32
std::string apiStr = wstringToString(szArglist[i]);
#else
std::string apiStr = argv[i];
#endif
rhi::DeviceType newType = parseApiString(apiStr.c_str());
if (newType != rhi::DeviceType::Default)
{
m_deviceType = newType;
}
else
{
#ifdef _WIN32
// For WIN32 apps, ensure we have a visible console window for errors too
ensureConsoleVisible();
#endif
std::string errorMsg = "Unknown API: " + apiStr + "\n";
printOutput(errorMsg.c_str());
m_showHelp = true;
}
}
}
#ifdef _WIN32
LocalFree(szArglist);
#endif
return 0;
}
void TestBase::printEntrypointHashes(
int entryPointCount,
int targetCount,
ComPtr<slang::IComponentType>& composedProgram)
{
for (int targetIndex = 0; targetIndex < targetCount; targetIndex++)
{
for (int entryPointIndex = 0; entryPointIndex < entryPointCount; entryPointIndex++)
{
ComPtr<slang::IBlob> entryPointHashBlob;
composedProgram->getEntryPointHash(
entryPointIndex,
targetIndex,
entryPointHashBlob.writeRef());
Slang::StringBuilder strBuilder;
strBuilder << "callIdx: " << m_globalCounter << ", entrypoint: " << entryPointIndex
<< ", target: " << targetIndex << ", hash: ";
m_globalCounter++;
uint8_t* buffer = (uint8_t*)entryPointHashBlob->getBufferPointer();
for (size_t i = 0; i < entryPointHashBlob->getBufferSize(); i++)
{
strBuilder << Slang::StringUtil::makeStringWithFormat("%.2X", buffer[i]);
}
fprintf(stdout, "%s\n", strBuilder.begin());
}
}
}
void TestBase::printUsage(const char* programName) const
{
#ifdef _WIN32
// For WIN32 apps, ensure we have a visible console window
bool createdNewWindow = ensureConsoleVisible();
#endif
// Use printOutput to ensure output appears in both console and debug output (for WIN32 apps)
std::string usage = "Usage: " + std::string(programName) + " [options]\n\n";
printOutput(usage.c_str());
printOutput("Options:\n");
printOutput(" -h, --help Show this help message\n");
printOutput(" -api (");
#ifdef _WIN32
printOutput("d3d11|d3d12|");
#endif
printOutput("vulkan");
#ifdef __APPLE__
printOutput("|metal");
#endif
printOutput("|cpu|cuda|webgpu)\n");
#ifdef _WIN32
printOutput(" Use a given rendering API (Default: d3d12)\n");
#elif defined(__APPLE__)
printOutput(" Use a given rendering API (Default: metal)\n");
#else
printOutput(" Use a given rendering API (Default: vulkan)\n");
#endif
printOutput(" -test-mode Print hash values of compiled shader entry points "
"and skip rendering\n");
printOutput("\n");
printOutput("Supported APIs:\n");
#ifdef _WIN32
printOutput(" d3d11 - Direct3D 11\n");
printOutput(" d3d12 - Direct3D 12\n");
#endif
printOutput(" vulkan - Vulkan\n");
#ifdef __APPLE__
printOutput(" metal - Metal (macOS/iOS)\n");
#endif
printOutput(" cpu - CPU execution\n");
printOutput(" cuda - CUDA\n");
printOutput(" webgpu - WebGPU\n");
#ifdef _WIN32
// For WIN32 apps, only prompt if we created a new console window
if (createdNewWindow)
{
printOutput("\nPress Enter to continue or close this window...\n");
getchar(); // Wait for user input
}
#endif
}
|