sKit  0.0.9
renderpass.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <SDL3/SDL_gpu.h>
4 
5 #include <vector>
6 
7 namespace sKit {
8 namespace sdl {
9 
10 inline void singleBufferIndexedDraw(SDL_GPUCommandBuffer *commandBuffer,
11  SDL_Window *window,
12  SDL_GPUGraphicsPipeline *pipeline,
13  SDL_GPUBuffer *vertexBuffer,
14  SDL_GPUBuffer *indexBuffer,
15  Uint32 numIndices) {
16  SDL_GPUTexture *tex;
17  SDL_WaitAndAcquireGPUSwapchainTexture(commandBuffer, window, &tex, nullptr,
18  nullptr);
19  SDL_GPUColorTargetInfo colorTarget = {
20  .texture = tex,
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);
25 
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);
32 
33  SDL_EndGPURenderPass(pass);
34 }
35 
36 class DrawPass {
37  public:
38  DrawPass(SDL_GPUCommandBuffer *commandBuffer, SDL_Window *window)
39  : cmd_(commandBuffer), w_(window) {};
40 
41  void addVertexBinding(SDL_GPUBuffer *buffer) {
42  bindings_.push_back(SDL_GPUBufferBinding{.buffer = buffer});
43  }
44 
45  void addIndexBuffer(SDL_GPUBuffer *buffer, Uint32 numIndices) {
46  indexBuffer_ = buffer;
47  numIndices_ = numIndices;
48  };
49 
50  void draw(SDL_GPUGraphicsPipeline *pipeline = nullptr) {
51  SDL_GPUTexture *tex;
52  SDL_WaitAndAcquireGPUSwapchainTexture(cmd_, w_, &tex, nullptr, nullptr);
53  SDL_GPUColorTargetInfo colorTarget = {
54  .texture = tex,
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);
59 
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);
67  }
68 
69  SDL_EndGPURenderPass(pass);
70  }
71 
72  private:
73  SDL_GPUCommandBuffer *cmd_;
74  SDL_Window *w_;
75  std::vector<SDL_GPUBufferBinding> bindings_;
76  SDL_GPUBuffer *indexBuffer_;
77  Uint32 numIndices_ = 0;
78 };
79 
80 } // namespace sdl
81 } // namespace sKit
sKit::sdl::DrawPass::draw
void draw(SDL_GPUGraphicsPipeline *pipeline=nullptr)
Definition: renderpass.hpp:50
sKit::sdl::DrawPass::addVertexBinding
void addVertexBinding(SDL_GPUBuffer *buffer)
Definition: renderpass.hpp:41
sKit::sdl::DrawPass
Definition: renderpass.hpp:36
sKit::sdl::DrawPass::DrawPass
DrawPass(SDL_GPUCommandBuffer *commandBuffer, SDL_Window *window)
Definition: renderpass.hpp:38
sKit::sdl::singleBufferIndexedDraw
void singleBufferIndexedDraw(SDL_GPUCommandBuffer *commandBuffer, SDL_Window *window, SDL_GPUGraphicsPipeline *pipeline, SDL_GPUBuffer *vertexBuffer, SDL_GPUBuffer *indexBuffer, Uint32 numIndices)
Definition: renderpass.hpp:10
sKit
Definition: camera.hpp:8
sKit::sdl::DrawPass::addIndexBuffer
void addIndexBuffer(SDL_GPUBuffer *buffer, Uint32 numIndices)
Definition: renderpass.hpp:45