Skip to content

Commit 0a97df8

Browse files
committed
Switched TextureRef to use a pointer instead of a member reference
1 parent f2bba80 commit 0a97df8

File tree

10 files changed

+144
-54
lines changed

10 files changed

+144
-54
lines changed

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)

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

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ namespace Siege::Vulkan
1313
{
1414
Texture2D& TextureAtlas::TextureRef::operator*()
1515
{
16-
return parentAtlas.texture;
16+
return parentAtlas->texture;
17+
}
18+
19+
TextureAtlas::TextureRef::operator bool() const
20+
{
21+
return parentAtlas;
1722
}
1823

1924
TextureAtlas::TextureAtlas(const char* name,
@@ -30,29 +35,37 @@ TextureAtlas::TextureAtlas(TextureAtlas&& other)
3035
Swap(other);
3136
}
3237

33-
TextureAtlas::~TextureAtlas() {}
34-
35-
void TextureAtlas::Swap(TextureAtlas& other)
38+
TextureAtlas::~TextureAtlas()
3639
{
37-
auto tmpTexture = std::move(texture);
38-
auto tmpFixedExtent = fixedExtent;
39-
40-
texture = std::move(other.texture);
41-
fixedExtent = other.fixedExtent;
42-
43-
other.texture = std::move(tmpTexture);
44-
other.fixedExtent = tmpFixedExtent;
40+
fixedExtent = {};
4541
}
4642

4743
TextureAtlas::TextureRef TextureAtlas::operator[](size_t index)
4844
{
4945
size_t elementsInRow = 1 / fixedExtent.width;
5046

51-
return TextureRef(*this,
47+
return TextureRef(this,
5248
(index % elementsInRow) * fixedExtent.width, // potentially slow code
5349
(index / elementsInRow) * fixedExtent.height,
5450
fixedExtent.width,
5551
fixedExtent.height);
5652
}
5753

54+
TextureAtlas* TextureAtlas::TextureRef::operator->()
55+
{
56+
return parentAtlas;
57+
}
58+
59+
void TextureAtlas::Swap(TextureAtlas& other)
60+
{
61+
auto tmpTexture = std::move(texture);
62+
auto tmpFixedExtent = fixedExtent;
63+
64+
texture = std::move(other.texture);
65+
fixedExtent = other.fixedExtent;
66+
67+
other.texture = std::move(tmpTexture);
68+
other.fixedExtent = tmpFixedExtent;
69+
}
70+
5871
} // namespace Siege::Vulkan

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,25 @@ class TextureAtlas
2424
{
2525
public:
2626

27-
TextureRef(TextureAtlas& parent, float minX, float minY, float width, float height) :
27+
// 'Structors
28+
TextureRef(TextureAtlas* parent, float minX, float minY, float width, float height) :
2829
parentAtlas {parent},
2930
minX {minX},
3031
width {width},
3132
minY {minY},
3233
height {height}
3334
{}
35+
36+
// Operator Overloads
37+
3438
Texture2D& operator*();
3539

40+
TextureAtlas* operator->();
41+
42+
operator bool() const;
43+
44+
// Getters and setters
45+
3646
inline float GetMinX() const
3747
{
3848
return minX;
@@ -41,7 +51,6 @@ class TextureAtlas
4151
{
4252
return minY;
4353
}
44-
4554
inline float GetWidth() const
4655
{
4756
return width;
@@ -54,10 +63,12 @@ class TextureAtlas
5463
private:
5564

5665
// NOTE(Aryeh): Can we always guarantee that the atlas will exist?
57-
TextureAtlas& parentAtlas;
66+
TextureAtlas* parentAtlas {nullptr};
5867
float minX, width, minY, height {0.f};
5968
};
6069

70+
// 'Structors
71+
6172
TextureAtlas() = default;
6273
TextureAtlas(const char* name,
6374
const char* filePath,
@@ -66,11 +77,29 @@ class TextureAtlas
6677
TextureAtlas(TextureAtlas&& other);
6778
~TextureAtlas();
6879

80+
// Operator Overloads
81+
6982
TextureRef operator[](size_t index);
7083

84+
// Getters & Setters
85+
86+
inline Texture2D& GetTexture()
87+
{
88+
return texture;
89+
}
90+
inline const Texture2D& GetTexture() const
91+
{
92+
return texture;
93+
}
94+
7195
private:
7296

97+
// Private functions
98+
7399
void Swap(TextureAtlas& other);
100+
101+
// Private member variables
102+
74103
Texture2D texture;
75104
Utils::Extent2DF fixedExtent {};
76105
};

engine/render/renderer/renderer/BillboardRenderer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void BillboardRenderer::DrawBillboard(const Vec3& position,
8484
{
8585
auto targetTexture = texture == nullptr ? &defaultTexture : texture;
8686

87-
auto texIndex = billboardMaterial.SetTexture(textureId, targetTexture);
87+
auto texIndex = billboardMaterial.SetTexture(textureId, *targetTexture);
8888

8989
if (texIndex >= vertices.Count())
9090
vertices.Append(MHArray<BillboardVertex>(MAX_QUADS_PER_TEXTURE));

engine/render/renderer/renderer/QuadRenderer3D.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void QuadRenderer3D::DrawQuad(const Siege::Vec3& position,
6666
{
6767
auto targetTexture = texture == nullptr ? &defaultTexture : texture;
6868

69-
auto texIndex = defaultMaterial.SetTexture(textureId, targetTexture);
69+
auto texIndex = defaultMaterial.SetTexture(textureId, *targetTexture);
7070

7171
if (texIndex >= quads.Count()) quads.Append(MHArray<QuadVertex>(MAX_QUADS_PER_TEXTURE));
7272

engine/render/renderer/renderer/Renderer2D.cpp

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919

2020
namespace Siege
2121
{
22+
Renderer2D::~Renderer2D()
23+
{
24+
for (auto it = perFrameQuadVertexBuffers.CreateFIterator(); it; ++it) it->Free();
25+
for (auto it = perFrameTextVertexBuffers.CreateFIterator(); it; ++it) it->Free();
26+
}
27+
2228
void Renderer2D::Initialise(const char* const globalDataName)
2329
{
2430
globalDataId = INTERN_STR(globalDataName);
@@ -78,9 +84,24 @@ void Renderer2D::Initialise(const char* const globalDataName)
7884

7985
quadIndexBuffer = Vulkan::IndexBuffer(fontIndices, sizeof(unsigned int) * 6);
8086

81-
quadVertexBuffer = Vulkan::VertexBuffer(sizeof(QuadVertex) * QUAD_VERTEX_BUFFER_SIZE);
82-
quadVBuffer = Vulkan::VertexBuffer(sizeof(QuadVertex) * QUAD_VERTEX_BUFFER_SIZE);
83-
textVertexBuffer = Vulkan::VertexBuffer(sizeof(FontVertex) * TEXT_VERTEX_BUFFER_SIZE);
87+
perFrameQuadVertexBuffers =
88+
MHArray<Vulkan::VertexBuffer>(Vulkan::Swapchain::MAX_FRAMES_IN_FLIGHT);
89+
90+
for (size_t i = 0; i < Vulkan::Swapchain::MAX_FRAMES_IN_FLIGHT; i++)
91+
{
92+
perFrameQuadVertexBuffers[i] =
93+
Vulkan::VertexBuffer(sizeof(QuadVertex) * QUAD_VERTEX_BUFFER_SIZE);
94+
}
95+
96+
perFrameTextVertexBuffers =
97+
MHArray<Vulkan::VertexBuffer>(Vulkan::Swapchain::MAX_FRAMES_IN_FLIGHT);
98+
99+
for (size_t i = 0; i < Vulkan::Swapchain::MAX_FRAMES_IN_FLIGHT; i++)
100+
{
101+
perFrameTextVertexBuffers[i] =
102+
Vulkan::VertexBuffer(sizeof(FontVertex) * TEXT_VERTEX_BUFFER_SIZE);
103+
;
104+
}
84105
}
85106

86107
void Renderer2D::DrawQuad(const Vec2 position,
@@ -94,7 +115,7 @@ void Renderer2D::DrawQuad(const Vec2 position,
94115

95116
auto targetTexture = texture == nullptr ? &defaultTexture : texture;
96117

97-
auto texIndex = quadMaterial.SetTexture(textureId, targetTexture);
118+
auto texIndex = quadMaterial.SetTexture(textureId, *targetTexture);
98119

99120
auto& layerQuads = quads[zIndex];
100121

@@ -117,7 +138,9 @@ void Renderer2D::DrawQuad(const Vec2 position,
117138
{
118139
CC_ASSERT(zIndex < MAX_LAYERS, "zIndex provided is larger than the maximum number of layers")
119140

120-
auto texIndex = quadMaterial.SetTexture(textureId, &(*texture));
141+
auto& targetTexture = texture ? *texture : defaultTexture;
142+
143+
auto texIndex = quadMaterial.SetTexture(textureId, targetTexture);
121144

122145
auto& layerQuads = quads[zIndex];
123146

@@ -142,7 +165,7 @@ void Renderer2D::DrawText2D(const char* const text,
142165
{
143166
CC_ASSERT(zIndex < MAX_LAYERS, "zIndex provided is larger than the maximum number of layers")
144167

145-
auto texIndex = textMaterial.SetTexture(textureId, font.GetTexture());
168+
auto texIndex = textMaterial.SetTexture(textureId, *font.GetTexture());
146169

147170
auto& layerQuads = characters[zIndex];
148171

@@ -203,20 +226,22 @@ void Renderer2D::Render(Vulkan::CommandBuffer& buffer,
203226
{
204227
// Render 2D quads for this layer
205228

206-
RenderQuads(buffer, i);
229+
RenderQuads(buffer, i, frameIndex);
207230

208231
// Render text for this layer
209232

210-
RenderText(buffer, i);
233+
RenderText(buffer, i, frameIndex);
211234
}
212235
}
213236

214-
void Renderer2D::RenderText(Vulkan::CommandBuffer& buffer, size_t index)
237+
void Renderer2D::RenderText(Vulkan::CommandBuffer& buffer, size_t index, uint32_t frameIndex)
215238
{
216239
if (characters[index].Count() == 0) return;
217240

218241
auto& perFontQuads = characters[index];
219242

243+
auto& vertexBuffer = perFrameTextVertexBuffers[frameIndex];
244+
220245
for (size_t j = 0; j < perFontQuads.Count(); j++)
221246
{
222247
auto& quadArr = perFontQuads[j];
@@ -230,21 +255,20 @@ void Renderer2D::RenderText(Vulkan::CommandBuffer& buffer, size_t index)
230255
sizeof(FontVertex) *
231256
((index * MAX_TEXTURES) + (j * MAX_TEXTS_PER_FONT * MAX_CHARS_PER_TEXT));
232257

233-
textVertexBuffer.Copy(quadArr.Data(),
234-
sizeof(FontVertex) * quadArr.Count(),
235-
vertexBufferOffset);
258+
vertexBuffer.Copy(quadArr.Data(), sizeof(FontVertex) * quadArr.Count(), vertexBufferOffset);
236259

237-
textVertexBuffer.Bind(buffer, &vertexBufferOffset);
260+
vertexBuffer.Bind(buffer, &vertexBufferOffset);
238261

239262
Vulkan::Utils::DrawIndexed(buffer.Get(), 6, quadArr.Count(), 0, 0, 0);
240263
}
241264
}
242265

243-
void Renderer2D::RenderQuads(Vulkan::CommandBuffer& buffer, size_t index)
266+
void Renderer2D::RenderQuads(Vulkan::CommandBuffer& buffer, size_t index, uint32_t frameIndex)
244267
{
245268
if (quads[index].Count() == 0) return;
246269

247270
auto& perTextureQuads = quads[index];
271+
auto& vertexBuffer = perFrameQuadVertexBuffers[frameIndex];
248272

249273
for (size_t j = 0; j < perTextureQuads.Count(); j++)
250274
{
@@ -257,13 +281,13 @@ void Renderer2D::RenderQuads(Vulkan::CommandBuffer& buffer, size_t index)
257281

258282
uint64_t vertexBufferOffset = (index * MAX_TEXTURES) + (j * MAX_QUADS_PER_LAYER);
259283

260-
quadVBuffer.Copy(quadArr.Data(),
261-
sizeof(QuadVertex) * quadArr.Count(),
262-
sizeof(QuadVertex) * vertexBufferOffset);
284+
vertexBuffer.Copy(quadArr.Data(),
285+
sizeof(QuadVertex) * quadArr.Count(),
286+
sizeof(QuadVertex) * vertexBufferOffset);
263287

264288
uint64_t bindOffset = sizeof(QuadVertex) * vertexBufferOffset;
265289

266-
quadVBuffer.Bind(buffer, &bindOffset);
290+
vertexBuffer.Bind(buffer, &bindOffset);
267291

268292
Vulkan::Utils::DrawIndexed(buffer.Get(), 6, quadArr.Count(), 0, 0, 0);
269293
}

engine/render/renderer/renderer/Renderer2D.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Renderer2D
2727
public:
2828

2929
Renderer2D() = default;
30+
~Renderer2D();
3031
void Initialise(const char* const globalDataName);
3132
void DrawQuad(const Vec2 position,
3233
const Vec2 scale = Vec2::One(),
@@ -57,8 +58,8 @@ class Renderer2D
5758
const uint64_t& globalDataSize,
5859
const void* globalData,
5960
uint32_t frameIndex);
60-
void RenderText(Vulkan::CommandBuffer& buffer, size_t index);
61-
void RenderQuads(Vulkan::CommandBuffer& buffer, size_t index);
61+
void RenderText(Vulkan::CommandBuffer& buffer, size_t index, uint32_t frameIndex);
62+
void RenderQuads(Vulkan::CommandBuffer& buffer, size_t index, uint32_t frameIndex);
6263
void RenderGrid(Vulkan::CommandBuffer& buffer);
6364
void Update();
6465
void Flush();
@@ -118,16 +119,15 @@ class Renderer2D
118119
// 2D Quads (used for sprites)
119120

120121
Vulkan::Material quadMaterial;
121-
Vulkan::VertexBuffer quadVertexBuffer;
122122

123-
Vulkan::VertexBuffer quadVBuffer;
123+
MHArray<Vulkan::VertexBuffer> perFrameQuadVertexBuffers;
124124

125125
SArray<MSArray<MHArray<QuadVertex>, MAX_TEXTURES>, MAX_LAYERS> quads;
126126

127127
// 2D Text
128128

129129
Vulkan::Material textMaterial;
130-
Vulkan::VertexBuffer textVertexBuffer;
130+
MHArray<Vulkan::VertexBuffer> perFrameTextVertexBuffers;
131131

132132
SArray<MSArray<MHArray<FontVertex>, MAX_TEXTURES>, MAX_LAYERS> characters;
133133

engine/render/renderer/renderer/TextRenderer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void TextRenderer::DrawFont(const char* text,
7979
{
8080
if (font == nullptr) return;
8181

82-
auto texIndex = defaultMaterial.SetTexture(textureId, font->GetTexture());
82+
auto texIndex = defaultMaterial.SetTexture(textureId, *font->GetTexture());
8383

8484
if (texIndex >= characters.Count()) characters.Append(MHArray<QuadData>(OFFSET_PER_FONT));
8585

0 commit comments

Comments
 (0)