Skip to content

Commit acd0768

Browse files
committed
CreateCommandBuffer -> ResetCommandBuffer, test use in render example
1 parent 65c1db7 commit acd0768

File tree

2 files changed

+56
-64
lines changed

2 files changed

+56
-64
lines changed

examples/render/run.cpp

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,25 @@ int main(int argc, char **argv) {
117117
GPUTensor devScreen = CreateTensor(ctx, {NROWS, NCOLS}, kf32, screen.data());
118118
uint32_t zeroTime = getCurrentTimeInMilliseconds();
119119

120+
ShaderCode shader = CreateShader(kSDF, Shape{16, 16, 1});
121+
Kernel renderKernel =
122+
CreateKernel(ctx, shader, {}, 0, devScreen, {NCOLS, NROWS, 1}, params);
120123
while (true) {
121-
params.time = getCurrentTimeInMilliseconds() - zeroTime;
122-
ShaderCode shader = CreateShader(kSDF, Shape{16, 16, 1});
123-
124-
// TODO(avh): Clean this up - too easy to miscalculate # of workgroups in x
125-
// and y directions since tensor dimensions (rows, cols) are reversed from
126-
// screen dimensions (cols, rows)
127-
Kernel render = CreateKernel(ctx, shader, {}, 0, devScreen,
128-
static_cast<void *>(&params), sizeof(params),
129-
{NCOLS, NROWS, 1});
130-
DispatchKernel(ctx, render);
131-
Wait(ctx, render.future);
124+
DispatchKernel(ctx, renderKernel);
125+
Wait(ctx, renderKernel.future);
132126
ToCPU(ctx, devScreen, screen.data(), sizeof(screen));
133-
134-
static const char intensity[] = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
127+
// Update the time field, write pparams to GPU, and create a new command buffer
128+
params.time = getCurrentTimeInMilliseconds() - zeroTime;
129+
wgpuQueueWriteBuffer(ctx.queue,
130+
renderKernel.buffers[renderKernel.numBuffers - 1], 0,
131+
static_cast<void *>(&params), sizeof(params));
132+
ResetCommandBuffer(ctx.device, shader.workgroupSize, {NCOLS, NROWS, 1},
133+
renderKernel);
134+
135+
static const char intensity[] = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/"
136+
"\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
135137
// static const char intensity[] = "@%#8$X71x*+=-:^~'.` ";
136138

137-
// clear the screen, move cursor to the top
138-
printf("\033[2J\033[H");
139-
140-
// fprintf(stdout, "%s",
141-
// show<float, NROWS, NCOLS>(screen, "Raw values").c_str());
142-
143139
// normalize values
144140
float min = 0.0;
145141
float max = params.sphereRadius * 3;
@@ -148,14 +144,6 @@ int main(int argc, char **argv) {
148144
screen[i] = (screen[i] - min) / (max - min);
149145
}
150146

151-
fprintf(stdout, "Workgroup size: %zu %zu %zu \n", shader.workgroupSize[0],
152-
shader.workgroupSize[1], shader.workgroupSize[2]);
153-
fprintf(stdout, "Number of Threads: %zu %zu %d \n", devScreen.shape[1],
154-
devScreen.shape[0], 1);
155-
156-
// fprintf(stdout, "%s", show<float, NROWS, NCOLS>(screen,
157-
// "Scaled").c_str());
158-
159147
// index into intensity array
160148
std::array<char, screen.size()> raster;
161149
for (size_t i = 0; i < screen.size(); ++i) {
@@ -166,24 +154,33 @@ int main(int argc, char **argv) {
166154
raster[i] = intensity[index];
167155
}
168156

169-
// draw the tui screen
170-
printf("+");
157+
// Draw the raster
158+
char buffer[(NROWS + 2) * (NCOLS + 2)];
159+
char *offset = buffer;
160+
sprintf(offset, "+");
171161
for (size_t col = 0; col < NCOLS; ++col) {
172-
printf("-");
162+
sprintf(offset + col + 1, "-");
173163
}
174-
printf("+\n");
164+
sprintf(buffer + NCOLS + 1, "+\n");
165+
offset += NCOLS + 3;
175166
for (size_t row = 0; row < NROWS; ++row) {
176-
printf("|");
167+
sprintf(offset, "|");
177168
for (size_t col = 0; col < NCOLS; ++col) {
178-
printf("%c", raster[row * NCOLS + col]);
169+
sprintf(offset + col + 1, "%c", raster[row * NCOLS + col]);
179170
}
180-
printf("|\n");
171+
sprintf(offset + NCOLS + 1, "|\n");
172+
offset += NCOLS + 3;
181173
}
182-
printf("+");
174+
sprintf(offset, "+");
183175
for (size_t col = 0; col < NCOLS; ++col) {
184-
printf("-");
176+
sprintf(offset + col + 1, "-");
185177
}
186-
printf("+\n");
178+
sprintf(offset + NCOLS + 1, "+\n");
179+
printf("\033[2J\033[H");
180+
printf("Workgroup size: %zu %zu %zu \n", shader.workgroupSize[0],
181+
shader.workgroupSize[1], shader.workgroupSize[2]);
182+
printf("Number of Threads: %zu %zu %d \n", devScreen.shape[1],
183+
devScreen.shape[0], 1);
184+
printf("%s", buffer);
187185
}
188-
189186
}

gpu.h

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ struct Kernel {
153153
size_t outputSize;
154154
size_t numBuffers;
155155
size_t numInputs;
156-
WGPUCommandBuffer commandBuffer; // destroyed upon submission
156+
WGPUBindGroup bindGroup; // persists between submission
157157
WGPUComputePipeline computePipeline; // persists between submission
158+
WGPUCommandBuffer commandBuffer; // destroyed upon submission
158159
WGPUBuffer readbackBuffer;
159160
CallbackDataDyn callbackData;
160161
std::promise<void> promise;
@@ -532,36 +533,34 @@ void ToGPU(GPUContext &ctx, const float *data, GPUTensor &tensor) {
532533
}
533534

534535
// Separate this out since WGPUCommandBuffer is destroyed upon submission
535-
WGPUCommandBuffer
536-
CreateCommandBuffer(WGPUDevice &device,
537-
const WGPUComputePipeline &computePipeline,
538-
const WGPUBindGroup &bindGroup, const ShaderCode &shader,
539-
const Shape &nThreads) {
540-
WGPUCommandBuffer commandBuffer;
541-
log(kDefLog, kInfo, "Create command buffer 0x%x", commandBuffer);
536+
void ResetCommandBuffer(WGPUDevice &device,
537+
const Shape &workgroupSize,
538+
const Shape &nThreads, Kernel &op) {
539+
log(kDefLog, kInfo, "Create command buffer 0x%x", op.commandBuffer);
542540
{
543541
WGPUCommandEncoder commandEncoder =
544542
wgpuDeviceCreateCommandEncoder(device, nullptr);
545543
WGPUComputePassEncoder computePassEncoder =
546544
wgpuCommandEncoderBeginComputePass(commandEncoder, nullptr);
547-
wgpuComputePassEncoderSetPipeline(computePassEncoder, computePipeline);
548-
wgpuComputePassEncoderSetBindGroup(computePassEncoder, 0, bindGroup, 0,
545+
wgpuComputePassEncoderSetPipeline(computePassEncoder, op.computePipeline);
546+
wgpuComputePassEncoderSetBindGroup(computePassEncoder, 0, op.bindGroup, 0,
549547
nullptr);
550548
log(kDefLog, kInfo, "Dispatching workgroups for number of threads = %s",
551549
ToString(nThreads).c_str());
552550
wgpuComputePassEncoderDispatchWorkgroups(
553551
computePassEncoder,
554-
/* # X workgroups */ (nThreads[0] + (shader.workgroupSize[0] - 1)) /
555-
shader.workgroupSize[0],
556-
/* # Y workgroups */ (nThreads[1] + (shader.workgroupSize[1] - 1)) /
557-
shader.workgroupSize[1],
558-
/* # Z workgroups */ (nThreads[2] + (shader.workgroupSize[2] - 1)) /
559-
shader.workgroupSize[2]);
552+
/* # X workgroups */ (nThreads[0] + (workgroupSize[0] - 1)) /
553+
workgroupSize[0],
554+
/* # Y workgroups */ (nThreads[1] + (workgroupSize[1] - 1)) /
555+
workgroupSize[1],
556+
/* # Z workgroups */ (nThreads[2] + (workgroupSize[2] - 1)) /
557+
workgroupSize[2]);
560558
wgpuComputePassEncoderEnd(computePassEncoder);
561-
commandBuffer = wgpuCommandEncoderFinish(commandEncoder, nullptr);
562-
check(commandBuffer, "Create command buffer", __FILE__, __LINE__);
559+
op.commandBuffer = wgpuCommandEncoderFinish(commandEncoder, nullptr);
560+
check(op.commandBuffer, "Create command buffer", __FILE__, __LINE__);
563561
}
564-
return commandBuffer;
562+
op.promise = std::promise<void>();
563+
op.future = op.promise.get_future();
565564
}
566565

567566
Kernel CreateKernel(GPUContext &ctx, const ShaderCode &shader,
@@ -686,11 +685,7 @@ Kernel CreateKernel(GPUContext &ctx, const ShaderCode &shader,
686685
.entryCount = static_cast<uint32_t>(bindGroupEntries.size()),
687686
.entries = bindGroupEntries.data(),
688687
};
689-
WGPUBindGroup bindGroup = wgpuDeviceCreateBindGroup(device, &bindGroupDesc);
690-
691-
log(kDefLog, kInfo, "Initializing promise and future");
692-
op.promise = std::promise<void>();
693-
op.future = op.promise.get_future();
688+
op.bindGroup = wgpuDeviceCreateBindGroup(device, &bindGroupDesc);
694689

695690
log(kDefLog, kInfo, "Create the readback buffer");
696691
{
@@ -727,8 +722,8 @@ Kernel CreateKernel(GPUContext &ctx, const ShaderCode &shader,
727722
wgpuDeviceCreateComputePipeline(device, &computePipelineDesc);
728723
check(op.computePipeline, "Create compute pipeline", __FILE__, __LINE__);
729724
}
730-
op.commandBuffer = CreateCommandBuffer(device, op.computePipeline, bindGroup,
731-
shader, nThreads);
725+
ResetCommandBuffer(device, shader.workgroupSize, nThreads, op);
726+
732727

733728
log(kDefLog, kInfo, "Initializing callbackData");
734729
op.callbackData = {op.readbackBuffer, op.outputSize, nullptr, &op.promise};

0 commit comments

Comments
 (0)