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(); void End();
}; };
#define DEPRECATE_RAW_CALLS
class GraphicsContext : public Context class GraphicsContext : public Context
{ {
protected: protected:
@ -287,14 +289,17 @@ class GraphicsContext : public Context
} }
public: 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); void DrawIndexed(u32 indexCount);
[[deprecated]] DEPRECATE_RAW_CALLS
void Dependency(const vk::DependencyInfo& dependencyInfo); void Dependency(const vk::DependencyInfo &dependencyInfo);
[[deprecated]] DEPRECATE_RAW_CALLS
void BeginRendering(const vk::RenderingInfo& renderingInfo); void BeginRendering(const vk::RenderingInfo &renderingInfo);
void EndRendering(); void EndRendering();
}; };
@ -558,13 +563,14 @@ class Device final
public: public:
Frame &GetNextFrame(); Frame &GetNextFrame();
Size2D GetSwapchainSize() const Size2D
GetSwapchainSize() const
{ {
return {m_Swapchain.m_Extent.width, m_Swapchain.m_Extent.height}; return {m_Swapchain.m_Extent.width, m_Swapchain.m_Extent.height};
} }
using Receipt = u32; using Receipt = u32;
Receipt Submit(Frame& frame, GraphicsContext& graphicsContext); Receipt Submit(Frame &frame, GraphicsContext &graphicsContext);
void Present(Frame &frame); void Present(Frame &frame);

View File

@ -661,12 +661,31 @@ systems::Context::End()
ERROR_IF(Failed(result), "Could not end context") THEN_ABORT(result); 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 void
systems::GraphicsContext::BindPipeline(vk::Pipeline pipeline) systems::GraphicsContext::BindPipeline(vk::Pipeline pipeline)
{ {
m_Cmd.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline); m_Cmd.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline);
} }
void
systems::GraphicsContext::Draw(u32 vertexCount)
{
m_Cmd.draw(vertexCount, 1, 0, 0);
}
void void
systems::GraphicsContext::DrawIndexed(u32 indexCount) systems::GraphicsContext::DrawIndexed(u32 indexCount)
{ {
@ -683,6 +702,7 @@ void
systems::GraphicsContext::BeginRendering(const vk::RenderingInfo &renderingInfo) systems::GraphicsContext::BeginRendering(const vk::RenderingInfo &renderingInfo)
{ {
m_Cmd.beginRendering(&renderingInfo); m_Cmd.beginRendering(&renderingInfo);
m_Cmd.setScissor(0, 1, &renderingInfo.renderArea);
} }
void void

View File

@ -71,7 +71,7 @@ Frame::Present(const vk::Queue commandQueue, Swapchain *swapchain, const Surface
case vk::Result::eErrorOutOfDateKHR: case vk::Result::eErrorOutOfDateKHR:
case vk::Result::eSuboptimalKHR: case vk::Result::eSuboptimalKHR:
DEBUG("Recreating Swapchain. Cause: {}", result); DEBUG("Recreating Swapchain. Cause: {}", result);
swapchain->Create(surface, size); swapchain->Create(*surface, size);
break; // Present failed. We do nothing. Frame is skipped. break; // Present failed. We do nothing. Frame is skipped.
default: default:
AbortIfFailedM(result, "Swapchain Present failed."); AbortIfFailedM(result, "Swapchain Present failed.");
@ -154,7 +154,7 @@ FrameManager::GetNextFrame(Swapchain *swapchain, const Surface *surface, Size2D
break; // Image acquired. Break out of loop. break; // Image acquired. Break out of loop.
case vk::Result::eErrorOutOfDateKHR: case vk::Result::eErrorOutOfDateKHR:
DEBUG("Recreating Swapchain. Cause: {}", result); DEBUG("Recreating Swapchain. Cause: {}", result);
swapchain->Create(surface, size); swapchain->Create(*surface, size);
break; // Image acquire has failed. We move to the next frame. break; // Image acquire has failed. We move to the next frame.
default: default:
AbortIfFailedMV(result, "Waiting for swapchain image {} failed.", frameIndex); 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()); vbo->Write(0, vertices.size() * sizeof vertices[0], vertices.data());
Size2D swapchainSize = device.GetSwapchainSize(); Size2D swapchainSize = device.GetSwapchainSize();
// Persistent variables // Persistent variables
vk::Viewport viewport = { vk::Viewport viewport = {
.x = 0, .x = 0,
@ -136,21 +135,10 @@ main(int, char **)
}; };
INFO("Starting loop"); INFO("Starting loop");
u32 frameIndex = 0;
while (window.Poll()) while (window.Poll())
{ {
systems::Frame &currentFrame = device.GetNextFrame(); 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(); auto context = currentFrame.CreateGraphicsContext();
topOfThePipeBarrier.image = currentFrame.m_SwapchainImage; topOfThePipeBarrier.image = currentFrame.m_SwapchainImage;
@ -171,7 +159,7 @@ main(int, char **)
}; };
vk::RenderingInfo renderingInfo = { vk::RenderingInfo renderingInfo = {
.renderArea = {.extent = scissor.extent}, .renderArea = scissor,
.layerCount = 1, .layerCount = 1,
.colorAttachmentCount = 1, .colorAttachmentCount = 1,
.pColorAttachments = &attachmentInfo, .pColorAttachments = &attachmentInfo,
@ -179,12 +167,10 @@ main(int, char **)
context.BeginRendering(renderingInfo); context.BeginRendering(renderingInfo);
// cmd.setViewport(0, 1, &viewport); context.SetViewport(viewport);
// cmd.setScissor(0, 1, &scissor); context.BindPipeline(pipeline.m_Pipeline);
// cmd.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline.m_Pipeline); context.BindVertexBuffer(vbo);
// usize offsets = 0; context.Draw(3);
// cmd.bindVertexBuffers(0, 1, &vbo->m_Buffer, &offsets);
// cmd.draw(3, 1, 0, 0);
context.EndRendering(); context.EndRendering();