Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 66799bf

Browse files
karnkaulTymianekPL
andauthored
Add Vertex, Primitive; draw quad (#16)
* Add `Vertex`, `Primitive`; draw quad * Use constexpr verts and indices (just for demonstration) * Refactor `ResourcePool` - Use `IResourcePool` abstract interface for public API - Move individual pool classes to 'src', under `namespace detail` - Concretize in 'engine.cpp' This hides unecessary implementation details from users, and reduces includes in the public API. --------- Co-authored-by: Tymianek <[email protected]>
1 parent 97e9dfa commit 66799bf

20 files changed

+340
-124
lines changed

app/src/main.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,26 @@ namespace
4545
tkge::Engine engine{surface};
4646

4747
auto shader = tkge::graphics::Shader{};
48-
const auto vertexSpirV = LoadSpirV((assets_path / "shaders/triangle.vert").string().c_str());
49-
const auto fragmentSpirV = LoadSpirV((assets_path / "shaders/triangle.frag").string().c_str());
48+
const auto vertexSpirV = LoadSpirV((assets_path / "shaders/default.vert").string().c_str());
49+
const auto fragmentSpirV = LoadSpirV((assets_path / "shaders/default.frag").string().c_str());
5050
const auto& renderDevice = engine.RenderDevice();
5151
if (!shader.Load(renderDevice.get_device(), vertexSpirV, fragmentSpirV)) { throw std::runtime_error{"Failed to load shaders"}; }
5252

53-
const auto pipelineLayout = renderDevice.get_device().createPipelineLayoutUnique({});
53+
constexpr auto vertices = std::array{
54+
tkge::graphics::Vertex{.position = {-0.5f, -0.5f}, .colour = kvf::red_v.to_vec4()},
55+
tkge::graphics::Vertex{.position = {0.5f, -0.5f}, .colour = kvf::green_v.to_vec4()},
56+
tkge::graphics::Vertex{.position = {0.5f, 0.5f}, .colour = kvf::blue_v.to_vec4()},
57+
tkge::graphics::Vertex{.position = {-0.5f, 0.5f}, .colour = kvf::yellow_v.to_vec4()},
58+
};
5459

55-
const auto pipelineState = kvf::PipelineState{
56-
.vertex_bindings = {},
57-
.vertex_attributes = {},
58-
.vertex_shader = shader.VertexModule(),
59-
.fragment_shader = shader.FragmentModule(),
60+
constexpr auto indices = std::array{
61+
0u, 1u, 2u, 2u, 3u, 0u,
6062
};
61-
const auto pipelineFormat = kvf::PipelineFormat{
62-
.samples = engine.FramebufferSamples(),
63-
.color = engine.FramebufferFormat(),
63+
64+
const auto primitive = tkge::graphics::Primitive{
65+
.vertices = vertices,
66+
.indices = indices,
6467
};
65-
const auto pipeline = renderDevice.create_pipeline(*pipelineLayout, pipelineState, pipelineFormat);
66-
if (!pipeline) { throw std::runtime_error{"Failed to create graphics pipeline"}; }
6768

6869
auto wireframe = false;
6970
auto lineWidth = 3.0f;
@@ -84,7 +85,7 @@ namespace
8485
renderer.BindShader(shader);
8586
renderer.SetLineWidth(lineWidth);
8687
renderer.SetWireframe(wireframe);
87-
renderer.Draw(3);
88+
renderer.Draw(primitive);
8889
}
8990

9091
engine.Present();
File renamed without changes.

assets/shaders/default.vert

1.07 KB
Binary file not shown.

assets/shaders/triangle.vert

-1.51 KB
Binary file not shown.

lib/include/tkge/engine.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace tkge
4545
kvf::RenderDevice _renderDevice;
4646
kvf::RenderPass _renderPass;
4747

48-
graphics::ResourcePool _resourcePool;
48+
std::unique_ptr<graphics::IResourcePool> _resourcePool{};
4949

5050
vk::CommandBuffer _cmd{};
5151
AssetLoader _assetLoader;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
#include <vulkan/vulkan.hpp>
3+
4+
namespace tkge::graphics
5+
{
6+
/// \brief Description of a unique Pipeline's fixed state.
7+
struct PipelineFixedState
8+
{
9+
/// \brief Colour image format of target Render Pass.
10+
vk::Format colourFormat{};
11+
12+
vk::PrimitiveTopology topology{vk::PrimitiveTopology::eTriangleList};
13+
vk::PolygonMode polygonMode{vk::PolygonMode::eFill};
14+
};
15+
} // namespace tkge::graphics
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
#include <tkge/graphics/vertex.hpp>
3+
#include <span>
4+
5+
namespace tkge::graphics
6+
{
7+
struct Primitive
8+
{
9+
std::span<const Vertex> vertices{};
10+
std::span<const std::uint32_t> indices{};
11+
12+
vk::PrimitiveTopology topology{vk::PrimitiveTopology::eTriangleList};
13+
};
14+
} // namespace tkge::graphics

lib/include/tkge/graphics/renderer.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <kvf/render_pass.hpp>
3+
#include <tkge/graphics/primitive.hpp>
34
#include <tkge/graphics/resource_pool.hpp>
45

56
namespace tkge::graphics
@@ -14,7 +15,7 @@ namespace tkge::graphics
1415

1516
Renderer() = default;
1617

17-
explicit Renderer(kvf::RenderPass* renderPass, ResourcePool* resourcePool, vk::CommandBuffer commandBuffer, glm::ivec2 framebufferSize);
18+
explicit Renderer(kvf::RenderPass* renderPass, IResourcePool* resourcePool, vk::CommandBuffer commandBuffer, glm::ivec2 framebufferSize);
1819
~Renderer() { EndRender(); }
1920

2021
[[nodiscard]] bool IsRendering() const { return _renderPass != nullptr; }
@@ -24,14 +25,15 @@ namespace tkge::graphics
2425
void SetLineWidth(float width);
2526
void SetWireframe(bool wireframe);
2627

27-
// temporary, until we have vertices, primitives, etc
28-
void Draw(std::uint32_t vertices);
28+
void Draw(const Primitive& primitive);
2929

3030
explicit operator bool() const { return IsRendering(); }
3131

3232
private:
33+
void BindVboAndDraw(const Primitive& primitive) const;
34+
3335
kvf::RenderPass* _renderPass{};
34-
ResourcePool* _resourcePool{};
36+
IResourcePool* _resourcePool{};
3537

3638
const Shader* _shader{};
3739
vk::Pipeline _pipeline{};
Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,24 @@
11
#pragma once
2-
#include <kvf/render_device.hpp>
2+
#include <kvf/vma.hpp>
3+
#include <tkge/graphics/pipeline_fixed_state.hpp>
34
#include <tkge/graphics/shader.hpp>
4-
#include <unordered_map>
55

66
namespace tkge::graphics
77
{
8-
class PipelinePool
9-
{
10-
public:
11-
struct Params
12-
{
13-
vk::Format colourFormat{};
14-
15-
vk::PrimitiveTopology topology{vk::PrimitiveTopology::eTriangleList};
16-
vk::PolygonMode polygonMode{vk::PolygonMode::eFill};
17-
};
18-
19-
explicit PipelinePool(gsl::not_null<const kvf::RenderDevice*> renderDevice, vk::SampleCountFlagBits framebufferSamples);
20-
21-
[[nodiscard]] vk::PipelineLayout PipelineLayout() const { return *_pipelineLayout; }
8+
using Buffer = kvf::vma::Buffer;
229

23-
[[nodiscard]] vk::Pipeline GetPipeline(const Shader& shader, const Params& params);
24-
25-
private:
26-
gsl::not_null<const kvf::RenderDevice*> _renderDevice;
27-
vk::SampleCountFlagBits _framebufferSamples;
28-
29-
vk::UniquePipelineLayout _pipelineLayout{};
30-
std::unordered_map<std::size_t, vk::UniquePipeline> _pipelines{};
31-
};
32-
33-
class ResourcePool
10+
class IResourcePool : public klib::Polymorphic
3411
{
3512
public:
36-
explicit ResourcePool(gsl::not_null<const kvf::RenderDevice*> renderDevice, vk::SampleCountFlagBits framebufferSamples)
37-
: pipelinePool(renderDevice, framebufferSamples)
38-
{
39-
}
13+
[[nodiscard]] virtual vk::PipelineLayout PipelineLayout() const = 0;
14+
15+
/// \brief Get the Pipeline identified by the input parameters.
16+
/// \param shader Shader that will be used in draw calls (dynamic Pipeline state).
17+
/// \param state Fixed Pipeline state.
18+
/// \returns Existing Pipeline if already cached, otherwise a newly created one (unless creation fails).
19+
[[nodiscard]] virtual vk::Pipeline GetPipeline(const Shader& shader, const PipelineFixedState& state) = 0;
4020

41-
PipelinePool pipelinePool;
21+
/// \brief Allocate a Buffer for given usage and of given size.
22+
[[nodiscard]] virtual Buffer& AllocateBuffer(vk::BufferUsageFlags usage, vk::DeviceSize size) = 0;
4223
};
4324
} // namespace tkge::graphics

lib/include/tkge/graphics/vertex.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
#include <glm/vec2.hpp>
3+
#include <glm/vec4.hpp>
4+
5+
namespace tkge::graphics
6+
{
7+
struct Vertex
8+
{
9+
glm::vec2 position{};
10+
glm::vec4 colour{1.0f};
11+
glm::vec2 uv{};
12+
};
13+
} // namespace tkge::graphics

0 commit comments

Comments
 (0)