-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompute_test.c
More file actions
447 lines (370 loc) · 15.4 KB
/
Copy pathcompute_test.c
File metadata and controls
447 lines (370 loc) · 15.4 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#include "pal2/pal_graphics.h"
#include "tests.h"
#define BUFFER_SIZE 400
// layout must match shader
typedef struct {
uint32_t width;
uint32_t height;
uint32_t _padding[2];
float color[4];
} PushConstant;
static void PAL_CALL onGraphicsDebug(
void* userData,
PalDebugMessageSeverity severity,
PalDebugMessageType type,
const char* msg)
{
palLog(nullptr, msg);
}
PalBool computeTest()
{
PalAdapter* adapter = nullptr;
PalDevice* device = nullptr;
PalQueue* queue = nullptr;
PalCommandPool* cmdPool = nullptr;
PalCommandBuffer* cmdBuffer;
PalShader* shader = nullptr;
PalBuffer* buffer = nullptr;
PalBuffer* stagingBuffer = nullptr;
PalDescriptorSetLayout* descriptorSetLayout = nullptr;
PalDescriptorPool* descriptorPool = nullptr;
PalDescriptorSet* descriptorSet = nullptr;
PalPipelineLayout* pipelineLayout = nullptr;
PalPipeline* pipeline = nullptr;
PalFence* fence = nullptr;
PalGraphicsDebugger debugger = {0};
debugger.callback = onGraphicsDebug;
debugger.userData = nullptr;
PalResult result = palInitGraphics(nullptr, nullptr, 0, nullptr);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to initialize graphics");
return PAL_FALSE;
}
// enumerate all available adapters
uint32_t adapterCount = 0;
result = palEnumerateAdapters(&adapterCount, nullptr);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to get adapters");
return PAL_FALSE;
}
if (adapterCount == 0) {
palLog(nullptr, "No adapters found");
return PAL_FALSE;
}
palLog(nullptr, "Adapter count: %d", adapterCount);
PalAdapter** adapters = nullptr;
adapters = palAllocate(nullptr, sizeof(PalAdapter*) * adapterCount, 0);
if (!adapters) {
palLog(nullptr, "Failed to allocate memory");
return PAL_FALSE;
}
result = palEnumerateAdapters(&adapterCount, adapters);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to get adapters");
return PAL_FALSE;
}
PalAdapterCapabilities caps = {0};
PalAdapterFeatures adapterFeatures = 0;
PalAdapterInfo adapterInfo = {0};
for (int32_t i = 0; i < adapterCount; i++) {
adapter = adapters[i];
palGetAdapterCapabilities(adapter, &caps);
if (caps.maxComputeQueues == 0) {
adapter = nullptr;
continue;
}
// We want an adapter that supports spirv 1.0 or dxil 6.0
palGetAdapterInfo(adapter, &adapterInfo);
// we prefer spirv first if an adapter supports multiple shader formats
uint32_t target = 0;
if (adapterInfo.shaderFormats & PAL_SHADER_FORMAT_SPIRV) {
target = palGetHighestSupportedShaderTarget(adapter, PAL_SHADER_FORMAT_SPIRV);
if (target >= PAL_MAKE_SHADER_TARGET(1, 0)) {
break;
}
}
if (adapterInfo.shaderFormats & PAL_SHADER_FORMAT_DXIL) {
target = palGetHighestSupportedShaderTarget(adapter, PAL_SHADER_FORMAT_DXIL);
if (target >= PAL_MAKE_SHADER_TARGET(6, 0)) {
break;
}
}
adapter = nullptr;
continue;
}
palFree(nullptr, adapters);
if (!adapter) {
palLog(nullptr, "Failed to find a required adapter");
return PAL_FALSE;
}
// create a device
result = palCreateDevice(adapter, 0, &device);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create device");
return PAL_FALSE;
}
// create a compute command queue
result = palCreateQueue(device, PAL_QUEUE_TYPE_COMPUTE, &queue);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create queue");
return PAL_FALSE;
}
result = palCreateCommandPool(device, queue, &cmdPool);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create command pool");
return PAL_FALSE;
}
result = palAllocateCommandBuffer(device, cmdPool, PAL_COMMAND_BUFFER_TYPE_PRIMARY, &cmdBuffer);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to allocate command buffer");
return PAL_FALSE;
}
// create a compute shader
uint32_t bytecodeSize = 0;
void* bytecode = nullptr;
const char* source = nullptr;
PalShaderCreateInfo shaderCreateInfo = {0};
if (adapterInfo.shaderFormats & PAL_SHADER_FORMAT_SPIRV) {
source = "graphics/shaders/bin/spirv/compute.spv";
} else if (adapterInfo.shaderFormats & PAL_SHADER_FORMAT_DXIL) {
source = "graphics/shaders/bin/dxil/compute.dxil";
}
// read file
if (!readFile(source, nullptr, &bytecodeSize)) {
palLog(nullptr, "Failed to read shader file");
return PAL_FALSE;
}
bytecode = palAllocate(nullptr, bytecodeSize, 0);
if (!bytecode) {
palLog(nullptr, "Failed to allocate memory");
return PAL_FALSE;
}
readFile(source, bytecode, &bytecodeSize);
PalShaderEntryInfo computeEntry = {0};
computeEntry.entryName = "main";
computeEntry.stage = PAL_SHADER_STAGE_COMPUTE;
shaderCreateInfo.code = bytecode;
shaderCreateInfo.codeSize = bytecodeSize;
shaderCreateInfo.entries = &computeEntry;
shaderCreateInfo.entryCount = 1;
result = palCreateShader(device, &shaderCreateInfo, &shader);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create shader");
return PAL_FALSE;
}
palFree(nullptr, bytecode);
// create a storage buffer
uint32_t bufferBytes = BUFFER_SIZE * BUFFER_SIZE * sizeof(float) * 4; // must match shader
PalBufferCreateInfo bufferCreateInfo = {0};
bufferCreateInfo.size = bufferBytes;
bufferCreateInfo.usages = PAL_BUFFER_USAGE_STORAGE | PAL_BUFFER_USAGE_TRANSFER_SRC;
bufferCreateInfo.memoryUsage = PAL_BUFFER_MEMORY_USAGE_AUTO_GPU_ONLY;
result = palCreateBuffer(device, &bufferCreateInfo, &buffer);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create buffer");
return PAL_FALSE;
}
bufferCreateInfo.usages = PAL_BUFFER_USAGE_TRANSFER_DST;
bufferCreateInfo.memoryUsage = PAL_BUFFER_MEMORY_USAGE_AUTO_CPU_READBACK;
result = palCreateBuffer(device, &bufferCreateInfo, &stagingBuffer);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create buffer");
return PAL_FALSE;
}
// create descriptor set layout
PalDescriptorSetLayoutBinding descriptorBinding = {0};
descriptorBinding.descriptorCount = 1; // not an array
descriptorBinding.descriptorType = PAL_DESCRIPTOR_TYPE_STORAGE_BUFFER;
PalDescriptorSetLayoutCreateInfo descriptorSetLayoutcreateInfo = {0};
descriptorSetLayoutcreateInfo.bindingCount = 1;
descriptorSetLayoutcreateInfo.bindings = &descriptorBinding;
result =
palCreateDescriptorSetLayout(device, &descriptorSetLayoutcreateInfo, &descriptorSetLayout);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create descriptor set layout");
return PAL_FALSE;
}
// create descriptor pool
PalDescriptorPoolBindingSize storageBufferBindingsize = {0};
storageBufferBindingsize.bindingCount = 1;
storageBufferBindingsize.descriptorType = PAL_DESCRIPTOR_TYPE_STORAGE_BUFFER;
PalDescriptorPoolCreateInfo descriptorPoolCreateInfo = {0};
descriptorPoolCreateInfo.maxDescriptorSets = 1; // only one set
descriptorPoolCreateInfo.bindingSizeCount = 1; // one binding type
descriptorPoolCreateInfo.bindingSizes = &storageBufferBindingsize;
result = palCreateDescriptorPool(device, &descriptorPoolCreateInfo, &descriptorPool);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create descriptor pool");
return PAL_FALSE;
}
// allocate a single descriptor set from the descriptor pool
// using the layout we created above
result = palAllocateDescriptorSet(device, descriptorPool, descriptorSetLayout, &descriptorSet);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to allocate descriptor set");
return PAL_FALSE;
}
// write the inital data to the descriptor set since its created empty
PalDescriptorBufferInfo descriptorBufferInfo = {0};
descriptorBufferInfo.buffer = buffer;
descriptorBufferInfo.offset = 0;
descriptorBufferInfo.size = bufferBytes;
PalDescriptorSetWriteInfo writeInfo = {0};
writeInfo.layoutBindingIndex = 0;
writeInfo.bufferInfos = &descriptorBufferInfo;
writeInfo.descriptorSet = descriptorSet;
writeInfo.descriptorType = PAL_DESCRIPTOR_TYPE_STORAGE_BUFFER;
writeInfo.descriptorCount = 1;
result = palUpdateDescriptorSet(device, 1, &writeInfo);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to update descriptor set");
return PAL_FALSE;
}
// create pipeline layout
PalPipelineLayoutCreateInfo pipelineLayoutCreateInfo = {0};
pipelineLayoutCreateInfo.descriptorSetLayoutCount = 1;
pipelineLayoutCreateInfo.descriptorSetLayouts = &descriptorSetLayout;
pipelineLayoutCreateInfo.usePushConstant = PAL_TRUE;
pipelineLayoutCreateInfo.pushConstantInfo.offset = 0;
pipelineLayoutCreateInfo.pushConstantInfo.size = sizeof(PushConstant); // must match shader
result = palCreatePipelineLayout(device, &pipelineLayoutCreateInfo, &pipelineLayout);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create pipeline layout");
return PAL_FALSE;
}
// create a compute pipeline
PalComputePipelineCreateInfo pipelineCreateInfo = {0};
pipelineCreateInfo.computeShader = shader;
pipelineCreateInfo.pipelineLayout = pipelineLayout;
result = palCreateComputePipeline(device, &pipelineCreateInfo, &pipeline);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create pipeline");
return PAL_FALSE;
}
palDestroyShader(shader);
// create fence
result = palCreateFence(device, PAL_FALSE, &fence);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create fence");
return PAL_FALSE;
}
// record commands
result = palCmdBegin(cmdBuffer, nullptr);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to begin command buffer");
return PAL_FALSE;
}
PushConstant pushConstant = {0};
pushConstant.width = BUFFER_SIZE;
pushConstant.height = BUFFER_SIZE;
pushConstant.color[0] = 1.0f;
pushConstant.color[1] = 0.0f;
pushConstant.color[2] = 0.0f;
pushConstant.color[3] = 1.0f;
palCmdBindPipeline(cmdBuffer, pipeline);
palCmdPushConstants(cmdBuffer, 0, sizeof(PushConstant), &pushConstant);
palCmdBindDescriptorSet(cmdBuffer, 0, descriptorSet);
// we will use a helper function to calculate the number of group count
// we need on each axis. The workGroupSize on each axis must be less or equal
// to maxComputeWorkGroupInvocations from the adapter capabilities struct
PalWorkGroupBuildData buildData = {0};
buildData.workCount[0] = BUFFER_SIZE;
buildData.workCount[1] = BUFFER_SIZE;
buildData.workCount[2] = 1; // no Z
buildData.workGroupSize[0] = 16; // must match shader (local_size on glsl)
buildData.workGroupSize[1] = 16; // must match shader (local_size on glsl)
buildData.workGroupSize[2] = 1; // must match shader (local_size on glsl)
// device limits
buildData.workGroupCount[0] = caps.computeCaps.maxWorkGroupCount[0];
buildData.workGroupCount[1] = caps.computeCaps.maxWorkGroupCount[1];
buildData.workGroupCount[2] = caps.computeCaps.maxWorkGroupCount[2];
uint32_t workGroupInfoCount = 0;
PalWorkGroupInfo* workGroupInfos = nullptr;
palBuildWorkGroupInfo(&buildData, &workGroupInfoCount, nullptr);
workGroupInfos = palAllocate(nullptr, sizeof(PalWorkGroupInfo) * workGroupInfoCount, 0);
if (!workGroupInfos) {
palLog(nullptr, "Failed to allocate memory");
return PAL_FALSE;
}
palBuildWorkGroupInfo(&buildData, &workGroupInfoCount, workGroupInfos);
// dispatch with the work group info
for (int i = 0; i < workGroupInfoCount; i++) {
// palDispatchBase is not supported on all platforms so we dont use it for this example
// We cap the buffer size small so we only get a single dispatch
// but you can add fields in yout constants to send the base to the shader driectly
uint32_t groupCountX = workGroupInfos[i].workGroupCount[0];
uint32_t groupCountY = workGroupInfos[i].workGroupCount[1];
uint32_t groupCountZ = workGroupInfos[i].workGroupCount[2];
palCmdDispatch(cmdBuffer, groupCountX, groupCountY, groupCountZ);
}
palFree(nullptr, workGroupInfos);
// set a barrier so we only read from the buffer after the shader has written to it
PalBarrierInfo barrierInfo = {0};
barrierInfo.oldState = PAL_USAGE_STATE_SHADER_WRITE;
barrierInfo.srcStages = PAL_PIPELINE_STAGE_COMPUTE_SHADER;
barrierInfo.newState = PAL_USAGE_STATE_TRANSFER_READ;
barrierInfo.dstStages = PAL_PIPELINE_STAGE_TRANSFER;
palCmdBufferBarrier(cmdBuffer, buffer, &barrierInfo);
// now we copy from the GPU buffer into the staging buffer
PalBufferCopyInfo copyInfo = {0};
copyInfo.size = bufferBytes;
palCmdCopyBuffer(cmdBuffer, stagingBuffer, buffer, ©Info);
result = palCmdEnd(cmdBuffer);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to end command buffer");
return PAL_FALSE;
}
// submit the command buffer to the GPU
PalCommandBufferSubmitInfo submitInfo = {0};
submitInfo.cmdBuffer = cmdBuffer;
submitInfo.fence = fence;
result = palSubmitCommandBuffer(queue, &submitInfo);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to submit command buffer");
return PAL_FALSE;
}
// wait for the fence
result = palWaitFence(fence, PAL_INFINITE);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to wait fence");
return PAL_FALSE;
}
// now our staging buffer has the contents of the GPU buffer
// we map it and copy the contents to a ppm buffer and save it
void* ptr = nullptr;
result = palMapBuffer(stagingBuffer, 0, bufferBytes, &ptr);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to map buffer");
return PAL_FALSE;
}
// write to a ppm output file
FILE* file = fopen("compute_output.ppm", "wb");
fprintf(file, "P6\n%d %d\n255\n", BUFFER_SIZE, BUFFER_SIZE);
float* pixels = (float*)ptr;
for (int y = 0; y < BUFFER_SIZE; y++) {
int row = BUFFER_SIZE - 1 - y; // flip y
for (int x = 0; x < BUFFER_SIZE; x++) {
int index = row * BUFFER_SIZE + x;
uint8_t rgb[3];
rgb[0] = pixels[index * 4 + 0] > 0.5f ? 255 : 0;
rgb[1] = pixels[index * 4 + 1] > 0.5f ? 255 : 0;
rgb[2] = pixels[index * 4 + 2] > 0.5f ? 255 : 0;
fwrite(rgb, 1, 3, file);
}
}
fclose(file);
palUnmapBuffer(stagingBuffer);
palDestroyPipeline(pipeline);
palDestroyPipelineLayout(pipelineLayout);
palFreeCommandBuffer(cmdBuffer);
palDestroyCommandPool(cmdPool);
palDestroyDescriptorPool(descriptorPool);
palDestroyDescriptorSetLayout(descriptorSetLayout);
palDestroyBuffer(buffer);
palDestroyBuffer(stagingBuffer);
palDestroyFence(fence);
palDestroyQueue(queue);
palDestroyDevice(device);
palShutdownGraphics();
return PAL_TRUE;
}