3 #include <SDL3/SDL_gpu.h>
12 SDL_GPUGraphicsPipeline *pipeline,
13 SDL_GPUBuffer *vertexBuffer,
14 SDL_GPUBuffer *indexBuffer,
17 SDL_WaitAndAcquireGPUSwapchainTexture(commandBuffer, window, &tex,
nullptr,
19 SDL_GPUColorTargetInfo colorTarget = {
21 .clear_color = SDL_FColor{1.0, 1.0, 1.0, 1},
22 .load_op = SDL_GPU_LOADOP_CLEAR,
23 .store_op = SDL_GPU_STOREOP_STORE};
24 auto pass = SDL_BeginGPURenderPass(commandBuffer, &colorTarget, 1,
nullptr);
26 SDL_BindGPUGraphicsPipeline(pass, pipeline);
27 SDL_GPUBufferBinding vertexBinding{.buffer = vertexBuffer};
28 SDL_GPUBufferBinding indexBinding{.buffer = indexBuffer};
29 SDL_BindGPUIndexBuffer(pass, &indexBinding, SDL_GPU_INDEXELEMENTSIZE_16BIT);
30 SDL_BindGPUVertexBuffers(pass, 0, &vertexBinding, 1);
31 SDL_DrawGPUIndexedPrimitives(pass, numIndices, 1, 0, 0, 0);
33 SDL_EndGPURenderPass(pass);
38 DrawPass(SDL_GPUCommandBuffer *commandBuffer, SDL_Window *window)
39 : cmd_(commandBuffer), w_(window) {};
42 bindings_.push_back(SDL_GPUBufferBinding{.buffer = buffer});
46 indexBuffer_ = buffer;
47 numIndices_ = numIndices;
50 void draw(SDL_GPUGraphicsPipeline *pipeline =
nullptr) {
52 SDL_WaitAndAcquireGPUSwapchainTexture(cmd_, w_, &tex,
nullptr,
nullptr);
53 SDL_GPUColorTargetInfo colorTarget = {
55 .clear_color = SDL_FColor{0.0, 0.0, 0.0, 1},
56 .load_op = SDL_GPU_LOADOP_CLEAR,
57 .store_op = SDL_GPU_STOREOP_STORE};
58 auto pass = SDL_BeginGPURenderPass(cmd_, &colorTarget, 1,
nullptr);
60 if (indexBuffer_ && bindings_.size() > 0 && pipeline) {
61 SDL_BindGPUGraphicsPipeline(pass, pipeline);
62 SDL_GPUBufferBinding indexBinding{.buffer = indexBuffer_};
63 SDL_BindGPUIndexBuffer(pass, &indexBinding,
64 SDL_GPU_INDEXELEMENTSIZE_16BIT);
65 SDL_BindGPUVertexBuffers(pass, 0, bindings_.data(), 1);
66 SDL_DrawGPUIndexedPrimitives(pass, numIndices_, 1, 0, 0, 0);
69 SDL_EndGPURenderPass(pass);
73 SDL_GPUCommandBuffer *cmd_;
75 std::vector<SDL_GPUBufferBinding> bindings_;
76 SDL_GPUBuffer *indexBuffer_;
77 Uint32 numIndices_ = 0;