From d683de3181269eebfd5f3c33848598f4dedf37ed Mon Sep 17 00:00:00 2001 From: Anish Bhobe Date: Thu, 1 May 2025 20:05:31 +0200 Subject: [PATCH] Draw Triangle and bug-fixes. --- aster/include/aster/systems/device.h | 20 +++++++++++++------- aster/src/aster/systems/device.cpp | 20 ++++++++++++++++++++ samples/00_util/frame.cpp | 4 ++-- samples/01_triangle/triangle.cpp | 24 +++++------------------- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/aster/include/aster/systems/device.h b/aster/include/aster/systems/device.h index 0d41368..55bc56e 100644 --- a/aster/include/aster/systems/device.h +++ b/aster/include/aster/systems/device.h @@ -275,6 +275,8 @@ class Context void End(); }; +#define DEPRECATE_RAW_CALLS + class GraphicsContext : public Context { protected: @@ -287,14 +289,17 @@ class GraphicsContext : public Context } public: - void BindPipeline(vk::Pipeline pipeline); + DEPRECATE_RAW_CALLS void SetViewport(const vk::Viewport &viewport); + void BindVertexBuffer(const Ref &vertexBuffer); + DEPRECATE_RAW_CALLS void BindPipeline(vk::Pipeline pipeline); + void Draw(u32 vertexCount); void DrawIndexed(u32 indexCount); - [[deprecated]] - void Dependency(const vk::DependencyInfo& dependencyInfo); + DEPRECATE_RAW_CALLS + void Dependency(const vk::DependencyInfo &dependencyInfo); - [[deprecated]] - void BeginRendering(const vk::RenderingInfo& renderingInfo); + DEPRECATE_RAW_CALLS + void BeginRendering(const vk::RenderingInfo &renderingInfo); void EndRendering(); }; @@ -558,13 +563,14 @@ class Device final public: Frame &GetNextFrame(); - Size2D GetSwapchainSize() const + Size2D + GetSwapchainSize() const { return {m_Swapchain.m_Extent.width, m_Swapchain.m_Extent.height}; } using Receipt = u32; - Receipt Submit(Frame& frame, GraphicsContext& graphicsContext); + Receipt Submit(Frame &frame, GraphicsContext &graphicsContext); void Present(Frame &frame); diff --git a/aster/src/aster/systems/device.cpp b/aster/src/aster/systems/device.cpp index ff11012..9185889 100644 --- a/aster/src/aster/systems/device.cpp +++ b/aster/src/aster/systems/device.cpp @@ -661,12 +661,31 @@ systems::Context::End() ERROR_IF(Failed(result), "Could not end context") THEN_ABORT(result); } +void +systems::GraphicsContext::SetViewport(const vk::Viewport &viewport) +{ + m_Cmd.setViewport(0, 1, &viewport); +} + +void +systems::GraphicsContext::BindVertexBuffer(const Ref &vertexBuffer) +{ + constexpr vk::DeviceSize offset = 0; + m_Cmd.bindVertexBuffers(0, 1, &vertexBuffer->m_Buffer, &offset); +} + void systems::GraphicsContext::BindPipeline(vk::Pipeline pipeline) { m_Cmd.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline); } +void +systems::GraphicsContext::Draw(u32 vertexCount) +{ + m_Cmd.draw(vertexCount, 1, 0, 0); +} + void systems::GraphicsContext::DrawIndexed(u32 indexCount) { @@ -683,6 +702,7 @@ void systems::GraphicsContext::BeginRendering(const vk::RenderingInfo &renderingInfo) { m_Cmd.beginRendering(&renderingInfo); + m_Cmd.setScissor(0, 1, &renderingInfo.renderArea); } void diff --git a/samples/00_util/frame.cpp b/samples/00_util/frame.cpp index 132b016..913e8ad 100644 --- a/samples/00_util/frame.cpp +++ b/samples/00_util/frame.cpp @@ -71,7 +71,7 @@ Frame::Present(const vk::Queue commandQueue, Swapchain *swapchain, const Surface case vk::Result::eErrorOutOfDateKHR: case vk::Result::eSuboptimalKHR: DEBUG("Recreating Swapchain. Cause: {}", result); - swapchain->Create(surface, size); + swapchain->Create(*surface, size); break; // Present failed. We do nothing. Frame is skipped. default: AbortIfFailedM(result, "Swapchain Present failed."); @@ -154,7 +154,7 @@ FrameManager::GetNextFrame(Swapchain *swapchain, const Surface *surface, Size2D break; // Image acquired. Break out of loop. case vk::Result::eErrorOutOfDateKHR: DEBUG("Recreating Swapchain. Cause: {}", result); - swapchain->Create(surface, size); + swapchain->Create(*surface, size); break; // Image acquire has failed. We move to the next frame. default: AbortIfFailedMV(result, "Waiting for swapchain image {} failed.", frameIndex); diff --git a/samples/01_triangle/triangle.cpp b/samples/01_triangle/triangle.cpp index 7af7a48..8c45b15 100644 --- a/samples/01_triangle/triangle.cpp +++ b/samples/01_triangle/triangle.cpp @@ -81,7 +81,6 @@ main(int, char **) vbo->Write(0, vertices.size() * sizeof vertices[0], vertices.data()); Size2D swapchainSize = device.GetSwapchainSize(); - // Persistent variables vk::Viewport viewport = { .x = 0, @@ -136,21 +135,10 @@ main(int, char **) }; INFO("Starting loop"); - u32 frameIndex = 0; while (window.Poll()) { systems::Frame ¤tFrame = device.GetNextFrame(); - vk::CommandBuffer cmd; - - const vk::CommandBufferAllocateInfo allocateInfo{ - .commandPool = currentFrame.m_Pool, - .level = vk::CommandBufferLevel::ePrimary, - .commandBufferCount = 1, - }; - AbortIfFailedMV(device.m_Device.m_Device.allocateCommandBuffers(&allocateInfo, &cmd), - "Command buffer {} alloc failed.", currentFrame.m_FrameIdx); - auto context = currentFrame.CreateGraphicsContext(); topOfThePipeBarrier.image = currentFrame.m_SwapchainImage; @@ -171,7 +159,7 @@ main(int, char **) }; vk::RenderingInfo renderingInfo = { - .renderArea = {.extent = scissor.extent}, + .renderArea = scissor, .layerCount = 1, .colorAttachmentCount = 1, .pColorAttachments = &attachmentInfo, @@ -179,12 +167,10 @@ main(int, char **) context.BeginRendering(renderingInfo); - // cmd.setViewport(0, 1, &viewport); - // cmd.setScissor(0, 1, &scissor); - // cmd.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline.m_Pipeline); - // usize offsets = 0; - // cmd.bindVertexBuffers(0, 1, &vbo->m_Buffer, &offsets); - // cmd.draw(3, 1, 0, 0); + context.SetViewport(viewport); + context.BindPipeline(pipeline.m_Pipeline); + context.BindVertexBuffer(vbo); + context.Draw(3); context.EndRendering();