Skip to content

Commit ad7b72d

Browse files
authored
Merge pull request #65 from CapsCollective/feat/texture-atlas
Created a TextureAtlas class for managing textures packed into a single space
2 parents 8f92f40 + d1aacea commit ad7b72d

File tree

12 files changed

+542
-43
lines changed

12 files changed

+542
-43
lines changed

engine/render/renderer/Renderer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ void Renderer::DrawQuad(const Vec2 position,
122122
renderer2D.DrawQuad(position, scale, colour, rotation, zIndex, texture);
123123
}
124124

125+
void Renderer::DrawQuad(const Vec2 position,
126+
Vulkan::TextureAtlas::SubTextureRef texture,
127+
const Vec2 scale,
128+
const IColour colour,
129+
float rotation,
130+
const uint8_t zIndex)
131+
{
132+
renderer2D.DrawQuad(position, texture, scale, colour, rotation, zIndex);
133+
}
134+
125135
void Renderer::DrawText2D(const char* text,
126136
Vulkan::Font& font,
127137
const Vec2 position,

engine/render/renderer/Renderer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class Renderer
5353
float rotation = 0.f,
5454
const uint8_t zIndex = 0,
5555
Vulkan::Texture2D* texture = nullptr);
56+
void DrawQuad(const Vec2 position,
57+
Vulkan::TextureAtlas::SubTextureRef texture,
58+
const Vec2 scale = {1.f, 1.f},
59+
const IColour colour = IColour::White,
60+
float rotation = 0.f,
61+
const uint8_t zIndex = 0);
5662
void DrawText2D(const char* text,
5763
Vulkan::Font& font,
5864
const Vec2 position,

engine/render/renderer/platform/vulkan/Material.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,17 @@ void Material::SetUniformData(Hash::StringId id, uint64_t dataSize, const void*
158158
}
159159
}
160160

161-
uint32_t Material::SetTexture(Hash::StringId id, Texture2D* texture)
161+
uint32_t Material::SetTexture(Hash::StringId id, const Texture2D& texture)
162162
{
163163
using namespace Utils;
164164
using namespace Descriptor;
165165

166-
auto texIndex = FindTextureIndex(texture->GetId());
166+
auto texIndex = FindTextureIndex(texture.GetId());
167167

168168
if (texIndex > -1) return texIndex;
169169

170170
texIndex = textureIds.Count();
171-
textureIds.Append(texture->GetId());
171+
textureIds.Append(texture.GetId());
172172

173173
for (auto it = propertiesSlots.CreateIterator(); it; ++it)
174174
{
@@ -179,7 +179,7 @@ uint32_t Material::SetTexture(Hash::StringId id, Texture2D* texture)
179179

180180
if (propIdx == -1) continue;
181181

182-
auto info = texture->GetInfo();
182+
auto info = texture.GetInfo();
183183

184184
textureInfos[texIndex] = {info.sampler, info.imageInfo.view, info.imageInfo.layout};
185185

engine/render/renderer/platform/vulkan/Material.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Material
9393
* @param index
9494
* @param textureInfo
9595
*/
96-
uint32_t SetTexture(Hash::StringId id, Texture2D* texture);
96+
uint32_t SetTexture(Hash::StringId id, const Texture2D& texture);
9797

9898
/**
9999
* Binds the Material for rendering (also binds the stored Pipeline)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//
2+
// Copyright (c) 2020-present Caps Collective & contributors
3+
// Originally authored by Jonathan Moallem (@jonjondev) & Aryeh Zinn (@Raelr)
4+
//
5+
// This code is released under an unmodified zlib license.
6+
// For conditions of distribution and use, please see:
7+
// https://opensource.org/licenses/Zlib
8+
//
9+
10+
#include "TextureAtlas.h"
11+
12+
namespace Siege::Vulkan
13+
{
14+
15+
Texture2D& TextureAtlas::SubTextureRef::operator*()
16+
{
17+
return parentAtlas->texture;
18+
}
19+
20+
TextureAtlas* TextureAtlas::SubTextureRef::operator->()
21+
{
22+
return parentAtlas;
23+
}
24+
25+
TextureAtlas::SubTextureRef::operator bool() const
26+
{
27+
return parentAtlas;
28+
}
29+
30+
void TextureAtlas::SubTextureRef::Swap(SubTextureRef& other)
31+
{
32+
auto tmpAtlas = parentAtlas;
33+
auto tmpMinX = minX;
34+
auto tmpMinY = minY;
35+
auto tmpWidth = width;
36+
auto tmpHeight = height;
37+
38+
parentAtlas = other.parentAtlas;
39+
minX = other.minX;
40+
minY = other.minY;
41+
width = other.width;
42+
height = other.height;
43+
44+
other.parentAtlas = tmpAtlas;
45+
other.minX = tmpMinX;
46+
other.minY = tmpMinY;
47+
other.width = tmpWidth;
48+
other.height = tmpHeight;
49+
}
50+
TextureAtlas::SubTextureRef& TextureAtlas::SubTextureRef::operator=(
51+
TextureAtlas::SubTextureRef& other)
52+
{
53+
parentAtlas = other.parentAtlas;
54+
minX = other.minX;
55+
minY = other.minY;
56+
width = other.width;
57+
height = other.height;
58+
59+
return *this;
60+
}
61+
62+
TextureAtlas::TextureAtlas(const char* name,
63+
const char* filePath,
64+
Utils::Extent2DF imageExtents,
65+
Utils::TextureFilter filter) :
66+
fixedExtent {imageExtents}
67+
{
68+
texture = Texture2D(name, filePath, filter);
69+
}
70+
71+
TextureAtlas::TextureAtlas(TextureAtlas&& other)
72+
{
73+
Swap(other);
74+
}
75+
76+
TextureAtlas::~TextureAtlas()
77+
{
78+
fixedExtent = {};
79+
}
80+
81+
TextureAtlas::SubTextureRef TextureAtlas::operator[](size_t index)
82+
{
83+
// TODO(Aryeh): Add some level of error handling here (assert if index is higher than number of
84+
// textures)
85+
86+
// NOTE(Aryeh): only works for fixed size textures
87+
size_t elementsInRow = 1 / fixedExtent.width;
88+
89+
return SubTextureRef(this,
90+
(index % elementsInRow) * fixedExtent.width, // potentially slow code
91+
(index / elementsInRow) * fixedExtent.height,
92+
fixedExtent.width,
93+
fixedExtent.height);
94+
}
95+
96+
TextureAtlas& TextureAtlas::operator=(TextureAtlas&& other)
97+
{
98+
Swap(other);
99+
return *this;
100+
}
101+
102+
void TextureAtlas::Swap(TextureAtlas& other)
103+
{
104+
auto tmpTexture = std::move(texture);
105+
auto tmpFixedExtent = fixedExtent;
106+
107+
texture = std::move(other.texture);
108+
fixedExtent = other.fixedExtent;
109+
110+
other.texture = std::move(tmpTexture);
111+
other.fixedExtent = tmpFixedExtent;
112+
}
113+
114+
} // namespace Siege::Vulkan

0 commit comments

Comments
 (0)