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

Commit ab649fd

Browse files
authored
Add Graphics::Texture, demo 2x2 RGBM texture (#26)
* Add `Graphics::Texture` * Add fallback texture in resource pool * Add `TextureSampler` and its pooling machinery * Add descriptor set binding for (main) texture * Add textures to `Primitive` and `BasicDrawable` * Sample texture in shaders * Demonstrate RGBM texture * RGBM ordering
1 parent 034fbf0 commit ab649fd

File tree

17 files changed

+179
-8
lines changed

17 files changed

+179
-8
lines changed

App/Src/Main.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <Tkge/Graphics/Shader.hpp>
55
#include <Tkge/Utilities.hpp>
66
#include <klib/assert.hpp>
7+
#include <kvf/color_bitmap.hpp>
78
#include <kvf/time.hpp>
89
#include <cmath>
910
#include <exception>
@@ -57,7 +58,16 @@ namespace
5758
auto quad = Tkge::Graphics::Quad{};
5859
quad.Create(glm::vec2{400.0f});
5960
quad.transform.position.x = -250.0f;
60-
quad.tint = kvf::magenta_v;
61+
62+
auto colourBitmap = kvf::ColorBitmap{glm::ivec2{2, 2}};
63+
colourBitmap[0, 0] = kvf::red_v;
64+
colourBitmap[1, 0] = kvf::green_v;
65+
colourBitmap[0, 1] = kvf::blue_v;
66+
colourBitmap[1, 1] = kvf::magenta_v;
67+
auto texture = Tkge::Graphics::Texture{&engine.RenderDevice()};
68+
texture.Create(colourBitmap.bitmap());
69+
texture.sampler.filter = vk::Filter::eNearest;
70+
quad.texture = &texture;
6171

6272
auto instancedQuad = Tkge::Graphics::InstancedQuad{};
6373
instancedQuad.Create(glm::vec2{150.0f});

Assets/Shaders/Default.frag

280 Bytes
Binary file not shown.

Assets/Shaders/Default.vert

96 Bytes
Binary file not shown.

Lib/Include/Tkge/Engine.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace Tkge
2525
explicit Engine(const WindowSurface& surface = {}, vk::SampleCountFlagBits aa = AntiAliasing);
2626

2727
[[nodiscard]] const kvf::RenderDevice& RenderDevice() const { return _renderDevice; }
28+
[[nodiscard]] kvf::RenderDevice& RenderDevice() { return _renderDevice; }
2829

2930
[[nodiscard]] glm::ivec2 FramebufferSize() const;
3031
[[nodiscard]] auto FramebufferFormat() const -> vk::Format { return _renderPass.get_color_format(); }

Lib/Include/Tkge/Graphics/Drawable.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ namespace Tkge::Graphics
2424
.vertices = this->GetVertices(),
2525
.indices = this->GetIndices(),
2626
.topology = this->GetTopology(),
27+
.texture = texture,
2728
};
2829
}
30+
31+
const Texture* texture{nullptr};
2932
};
3033

3134
template <std::derived_from<IGeometry> TGeometry>

Lib/Include/Tkge/Graphics/Primitive.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <Tkge/Graphics/Texture.hpp>
23
#include <Tkge/Graphics/Vertex.hpp>
34
#include <span>
45

@@ -10,5 +11,6 @@ namespace Tkge::Graphics
1011
std::span<const std::uint32_t> indices{};
1112

1213
vk::PrimitiveTopology topology{vk::PrimitiveTopology::eTriangleList};
14+
const Texture* texture{nullptr};
1315
};
1416
} // namespace Tkge::Graphics

Lib/Include/Tkge/Graphics/Renderer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace Tkge::Graphics
4141
};
4242

4343
void UpdateInstances(std::span<const RenderInstance> instances);
44-
[[nodiscard]] bool WriteSets() const;
44+
[[nodiscard]] bool WriteSets(const Texture* texture) const;
4545
void BindVboAndDraw(const Primitive& primitive, std::uint32_t instances) const;
4646

4747
kvf::RenderPass* _renderPass{};

Lib/Include/Tkge/Graphics/ResourcePool.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <Tkge/Graphics/PipelineFixedState.hpp>
33
#include <Tkge/Graphics/Shader.hpp>
4+
#include <Tkge/Graphics/Texture.hpp>
45
#include <kvf/vma.hpp>
56

67
namespace Tkge::Graphics
@@ -21,5 +22,8 @@ namespace Tkge::Graphics
2122

2223
/// \brief Allocate a Buffer for given usage and of given size.
2324
[[nodiscard]] virtual Buffer& AllocateBuffer(vk::BufferUsageFlags usage, vk::DeviceSize size) = 0;
25+
26+
[[nodiscard]] virtual vk::Sampler GetSampler(const Graphics::TextureSampler& sampler) = 0;
27+
[[nodiscard]] virtual const Graphics::Texture& GetFallbackTexture() const = 0;
2428
};
2529
} // namespace Tkge::Graphics

Lib/Include/Tkge/Graphics/Texture.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
#include <Tkge/Graphics/TextureSampler.hpp>
3+
#include <kvf/bitmap.hpp>
4+
#include <kvf/render_device.hpp>
5+
#include <kvf/vma.hpp>
6+
#include <cstddef>
7+
#include <span>
8+
9+
namespace Tkge::Graphics
10+
{
11+
class Texture
12+
{
13+
public:
14+
using Sampler = TextureSampler;
15+
16+
explicit Texture(gsl::not_null<kvf::RenderDevice*> renderDevice);
17+
18+
bool Create(const kvf::Bitmap& bitmap);
19+
bool Decompress(std::span<const std::byte> compressedImage);
20+
21+
[[nodiscard]] const kvf::vma::Image& GetImage() const { return _image; }
22+
[[nodiscard]] glm::ivec2 GetSize() const;
23+
24+
Sampler sampler{};
25+
26+
private:
27+
kvf::vma::Image _image{};
28+
};
29+
} // namespace Tkge::Graphics
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+
struct TextureSampler
7+
{
8+
vk::SamplerAddressMode wrap{vk::SamplerAddressMode::eRepeat};
9+
vk::Filter filter{vk::Filter::eLinear};
10+
vk::SamplerMipmapMode mipMap{vk::SamplerMipmapMode::eNearest};
11+
vk::BorderColor borderColour{vk::BorderColor::eFloatTransparentBlack};
12+
13+
bool operator==(const TextureSampler&) const = default;
14+
};
15+
} // namespace Tkge::Graphics

0 commit comments

Comments
 (0)