Skip to content

Commit

Permalink
Merge pull request #65 from CapsCollective/feat/texture-atlas
Browse files Browse the repository at this point in the history
Created a TextureAtlas class for managing textures packed into a single space
  • Loading branch information
Raelr committed Nov 25, 2023
2 parents 8f92f40 + d1aacea commit ad7b72d
Show file tree
Hide file tree
Showing 12 changed files with 542 additions and 43 deletions.
10 changes: 10 additions & 0 deletions engine/render/renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ void Renderer::DrawQuad(const Vec2 position,
renderer2D.DrawQuad(position, scale, colour, rotation, zIndex, texture);
}

void Renderer::DrawQuad(const Vec2 position,
Vulkan::TextureAtlas::SubTextureRef texture,
const Vec2 scale,
const IColour colour,
float rotation,
const uint8_t zIndex)
{
renderer2D.DrawQuad(position, texture, scale, colour, rotation, zIndex);
}

void Renderer::DrawText2D(const char* text,
Vulkan::Font& font,
const Vec2 position,
Expand Down
6 changes: 6 additions & 0 deletions engine/render/renderer/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class Renderer
float rotation = 0.f,
const uint8_t zIndex = 0,
Vulkan::Texture2D* texture = nullptr);
void DrawQuad(const Vec2 position,
Vulkan::TextureAtlas::SubTextureRef texture,
const Vec2 scale = {1.f, 1.f},
const IColour colour = IColour::White,
float rotation = 0.f,
const uint8_t zIndex = 0);
void DrawText2D(const char* text,
Vulkan::Font& font,
const Vec2 position,
Expand Down
8 changes: 4 additions & 4 deletions engine/render/renderer/platform/vulkan/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,17 @@ void Material::SetUniformData(Hash::StringId id, uint64_t dataSize, const void*
}
}

uint32_t Material::SetTexture(Hash::StringId id, Texture2D* texture)
uint32_t Material::SetTexture(Hash::StringId id, const Texture2D& texture)
{
using namespace Utils;
using namespace Descriptor;

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

if (texIndex > -1) return texIndex;

texIndex = textureIds.Count();
textureIds.Append(texture->GetId());
textureIds.Append(texture.GetId());

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

if (propIdx == -1) continue;

auto info = texture->GetInfo();
auto info = texture.GetInfo();

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

Expand Down
2 changes: 1 addition & 1 deletion engine/render/renderer/platform/vulkan/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Material
* @param index
* @param textureInfo
*/
uint32_t SetTexture(Hash::StringId id, Texture2D* texture);
uint32_t SetTexture(Hash::StringId id, const Texture2D& texture);

/**
* Binds the Material for rendering (also binds the stored Pipeline)
Expand Down
114 changes: 114 additions & 0 deletions engine/render/renderer/platform/vulkan/TextureAtlas.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// Copyright (c) 2020-present Caps Collective & contributors
// Originally authored by Jonathan Moallem (@jonjondev) & Aryeh Zinn (@Raelr)
//
// This code is released under an unmodified zlib license.
// For conditions of distribution and use, please see:
// https://opensource.org/licenses/Zlib
//

#include "TextureAtlas.h"

namespace Siege::Vulkan
{

Texture2D& TextureAtlas::SubTextureRef::operator*()
{
return parentAtlas->texture;
}

TextureAtlas* TextureAtlas::SubTextureRef::operator->()
{
return parentAtlas;
}

TextureAtlas::SubTextureRef::operator bool() const
{
return parentAtlas;
}

void TextureAtlas::SubTextureRef::Swap(SubTextureRef& other)
{
auto tmpAtlas = parentAtlas;
auto tmpMinX = minX;
auto tmpMinY = minY;
auto tmpWidth = width;
auto tmpHeight = height;

parentAtlas = other.parentAtlas;
minX = other.minX;
minY = other.minY;
width = other.width;
height = other.height;

other.parentAtlas = tmpAtlas;
other.minX = tmpMinX;
other.minY = tmpMinY;
other.width = tmpWidth;
other.height = tmpHeight;
}
TextureAtlas::SubTextureRef& TextureAtlas::SubTextureRef::operator=(
TextureAtlas::SubTextureRef& other)
{
parentAtlas = other.parentAtlas;
minX = other.minX;
minY = other.minY;
width = other.width;
height = other.height;

return *this;
}

TextureAtlas::TextureAtlas(const char* name,
const char* filePath,
Utils::Extent2DF imageExtents,
Utils::TextureFilter filter) :
fixedExtent {imageExtents}
{
texture = Texture2D(name, filePath, filter);
}

TextureAtlas::TextureAtlas(TextureAtlas&& other)
{
Swap(other);
}

TextureAtlas::~TextureAtlas()
{
fixedExtent = {};
}

TextureAtlas::SubTextureRef TextureAtlas::operator[](size_t index)
{
// TODO(Aryeh): Add some level of error handling here (assert if index is higher than number of
// textures)

// NOTE(Aryeh): only works for fixed size textures
size_t elementsInRow = 1 / fixedExtent.width;

return SubTextureRef(this,
(index % elementsInRow) * fixedExtent.width, // potentially slow code
(index / elementsInRow) * fixedExtent.height,
fixedExtent.width,
fixedExtent.height);
}

TextureAtlas& TextureAtlas::operator=(TextureAtlas&& other)
{
Swap(other);
return *this;
}

void TextureAtlas::Swap(TextureAtlas& other)
{
auto tmpTexture = std::move(texture);
auto tmpFixedExtent = fixedExtent;

texture = std::move(other.texture);
fixedExtent = other.fixedExtent;

other.texture = std::move(tmpTexture);
other.fixedExtent = tmpFixedExtent;
}

} // namespace Siege::Vulkan
Loading

0 comments on commit ad7b72d

Please sign in to comment.