Draw Triangle and bug-fixes.

This commit is contained in:
Anish Bhobe 2025-05-01 20:05:31 +02:00
parent d82e81d104
commit d683de3181
4 changed files with 40 additions and 28 deletions

View File

@ -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> &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);

View File

@ -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> &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

View File

@ -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);

View File

@ -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 &currentFrame = 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();