|
| 1 | +// ============================================================================ |
| 2 | +// @file CommandBuffer.cpp |
| 3 | +// @brief CommandBuffer implementation |
| 4 | +// ============================================================================ |
| 5 | +#include "CommandBuffer.hpp" |
| 6 | + |
| 7 | +#include <SDL3/SDL_gpu.h> |
| 8 | + |
| 9 | +namespace Caffeine::RHI { |
| 10 | + |
| 11 | +CommandBuffer::CommandBuffer(CommandBuffer&& other) noexcept |
| 12 | + : m_device(other.m_device) |
| 13 | + , m_cmdBuffer(other.m_cmdBuffer) |
| 14 | + , m_renderPass(other.m_renderPass) |
| 15 | + , m_inRenderPass(other.m_inRenderPass) |
| 16 | + , m_acquired(other.m_acquired) { |
| 17 | + other.m_device = nullptr; |
| 18 | + other.m_cmdBuffer = nullptr; |
| 19 | + other.m_renderPass = nullptr; |
| 20 | + other.m_inRenderPass = false; |
| 21 | + other.m_acquired = false; |
| 22 | +} |
| 23 | + |
| 24 | +CommandBuffer& CommandBuffer::operator=(CommandBuffer&& other) noexcept { |
| 25 | + if (this != &other) { |
| 26 | + m_device = other.m_device; |
| 27 | + m_cmdBuffer = other.m_cmdBuffer; |
| 28 | + m_renderPass = other.m_renderPass; |
| 29 | + m_inRenderPass = other.m_inRenderPass; |
| 30 | + m_acquired = other.m_acquired; |
| 31 | + other.m_device = nullptr; |
| 32 | + other.m_cmdBuffer = nullptr; |
| 33 | + other.m_renderPass = nullptr; |
| 34 | + other.m_inRenderPass = false; |
| 35 | + other.m_acquired = false; |
| 36 | + } |
| 37 | + return *this; |
| 38 | +} |
| 39 | + |
| 40 | +void CommandBuffer::acquire(SDL_GPUDevice* device) { |
| 41 | + m_device = device; |
| 42 | + m_cmdBuffer = SDL_AcquireGPUCommandBuffer(device); |
| 43 | + m_acquired = (m_cmdBuffer != nullptr); |
| 44 | +} |
| 45 | + |
| 46 | +void CommandBuffer::submit() { |
| 47 | + if (m_cmdBuffer) { |
| 48 | + SDL_SubmitGPUCommandBuffer(m_cmdBuffer); |
| 49 | + m_cmdBuffer = nullptr; |
| 50 | + } |
| 51 | + m_acquired = false; |
| 52 | +} |
| 53 | + |
| 54 | +void CommandBuffer::reset() { |
| 55 | + m_cmdBuffer = nullptr; |
| 56 | + m_renderPass = nullptr; |
| 57 | + m_inRenderPass = false; |
| 58 | + m_acquired = false; |
| 59 | +} |
| 60 | + |
| 61 | +void CommandBuffer::beginRenderPass(const RenderPassDesc& desc) { |
| 62 | + if (!m_cmdBuffer || m_inRenderPass) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // This begins a render pass targeting the swapchain texture. |
| 67 | + // For off-screen rendering, the caller would provide a texture target. |
| 68 | + // For now, this sets up the clear color but actual render target |
| 69 | + // binding happens in RenderDevice::endFrame with the swapchain texture. |
| 70 | + m_inRenderPass = true; |
| 71 | + (void)desc; |
| 72 | +} |
| 73 | + |
| 74 | +void CommandBuffer::endRenderPass() { |
| 75 | + if (!m_inRenderPass) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + if (m_renderPass) { |
| 80 | + SDL_EndGPURenderPass(m_renderPass); |
| 81 | + m_renderPass = nullptr; |
| 82 | + } |
| 83 | + |
| 84 | + m_inRenderPass = false; |
| 85 | +} |
| 86 | + |
| 87 | +void CommandBuffer::bindPipeline(Pipeline* pipeline) { |
| 88 | + if (!m_renderPass || !pipeline || !pipeline->handle) { |
| 89 | + return; |
| 90 | + } |
| 91 | + SDL_BindGPUGraphicsPipeline(m_renderPass, pipeline->handle); |
| 92 | +} |
| 93 | + |
| 94 | +void CommandBuffer::bindVertexBuffer(Buffer* buf, u32 slot) { |
| 95 | + if (!m_renderPass || !buf || !buf->handle) { |
| 96 | + return; |
| 97 | + } |
| 98 | + SDL_GPUBufferBinding binding{}; |
| 99 | + binding.buffer = buf->handle; |
| 100 | + binding.offset = 0; |
| 101 | + SDL_BindGPUVertexBuffers(m_renderPass, slot, &binding, 1); |
| 102 | +} |
| 103 | + |
| 104 | +void CommandBuffer::bindIndexBuffer(Buffer* buf) { |
| 105 | + if (!m_renderPass || !buf || !buf->handle) { |
| 106 | + return; |
| 107 | + } |
| 108 | + SDL_GPUBufferBinding binding{}; |
| 109 | + binding.buffer = buf->handle; |
| 110 | + binding.offset = 0; |
| 111 | + SDL_BindGPUIndexBuffer(m_renderPass, &binding, SDL_GPU_INDEXELEMENTSIZE_32BIT); |
| 112 | +} |
| 113 | + |
| 114 | +void CommandBuffer::bindTexture(Texture* tex, u32 slot) { |
| 115 | + if (!m_renderPass || !tex || !tex->handle) { |
| 116 | + return; |
| 117 | + } |
| 118 | + SDL_GPUTextureSamplerBinding samplerBinding{}; |
| 119 | + samplerBinding.texture = tex->handle; |
| 120 | + samplerBinding.sampler = nullptr; |
| 121 | + SDL_BindGPUFragmentSamplers(m_renderPass, slot, &samplerBinding, 1); |
| 122 | +} |
| 123 | + |
| 124 | +void CommandBuffer::setViewport(f32 x, f32 y, f32 w, f32 h) { |
| 125 | + if (!m_renderPass) { |
| 126 | + return; |
| 127 | + } |
| 128 | + SDL_GPUViewport viewport{}; |
| 129 | + viewport.x = x; |
| 130 | + viewport.y = y; |
| 131 | + viewport.w = w; |
| 132 | + viewport.h = h; |
| 133 | + viewport.min_depth = 0.0f; |
| 134 | + viewport.max_depth = 1.0f; |
| 135 | + SDL_SetGPUViewport(m_renderPass, &viewport); |
| 136 | +} |
| 137 | + |
| 138 | +void CommandBuffer::setScissor(u32 x, u32 y, u32 w, u32 h) { |
| 139 | + if (!m_renderPass) { |
| 140 | + return; |
| 141 | + } |
| 142 | + SDL_Rect scissor{}; |
| 143 | + scissor.x = static_cast<int>(x); |
| 144 | + scissor.y = static_cast<int>(y); |
| 145 | + scissor.w = static_cast<int>(w); |
| 146 | + scissor.h = static_cast<int>(h); |
| 147 | + SDL_SetGPUScissor(m_renderPass, &scissor); |
| 148 | +} |
| 149 | + |
| 150 | +void CommandBuffer::draw(u32 vertexCount, u32 firstVertex) { |
| 151 | + if (!m_renderPass) { |
| 152 | + return; |
| 153 | + } |
| 154 | + SDL_DrawGPUPrimitives(m_renderPass, vertexCount, 1, firstVertex, 0); |
| 155 | +} |
| 156 | + |
| 157 | +void CommandBuffer::drawIndexed(u32 indexCount, u32 firstIndex, i32 vertexOffset) { |
| 158 | + if (!m_renderPass) { |
| 159 | + return; |
| 160 | + } |
| 161 | + SDL_DrawGPUIndexedPrimitives(m_renderPass, indexCount, 1, firstIndex, vertexOffset, 0); |
| 162 | +} |
| 163 | + |
| 164 | +void CommandBuffer::drawInstanced(u32 vertexCount, u32 instanceCount, |
| 165 | + u32 firstVertex, u32 firstInstance) { |
| 166 | + if (!m_renderPass) { |
| 167 | + return; |
| 168 | + } |
| 169 | + SDL_DrawGPUPrimitives(m_renderPass, vertexCount, instanceCount, |
| 170 | + firstVertex, firstInstance); |
| 171 | +} |
| 172 | + |
| 173 | +void CommandBuffer::pushUniformData(ShaderStage stage, u32 slot, |
| 174 | + const void* data, u32 size) { |
| 175 | + if (!m_cmdBuffer || !data || size == 0) { |
| 176 | + return; |
| 177 | + } |
| 178 | + SDL_PushGPUVertexUniformData(m_cmdBuffer, slot, data, size); |
| 179 | + if (stage == ShaderStage::Fragment) { |
| 180 | + SDL_PushGPUFragmentUniformData(m_cmdBuffer, slot, data, size); |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +} // namespace Caffeine::RHI |
0 commit comments