Implement back text rendering (WIP)

This commit is contained in:
Jérôme Leclercq
2021-09-07 18:45:10 +02:00
parent 879b2f7aa6
commit ece18bf472
16 changed files with 613 additions and 77 deletions

View File

@@ -0,0 +1,35 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_GUILLOTINETEXTUREATLAS_HPP
#define NAZARA_GUILLOTINETEXTUREATLAS_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Utility/GuillotineImageAtlas.hpp>
namespace Nz
{
class RenderDevice;
class NAZARA_GRAPHICS_API GuillotineTextureAtlas : public GuillotineImageAtlas
{
public:
inline GuillotineTextureAtlas(RenderDevice& renderDevice);
~GuillotineTextureAtlas() = default;
DataStoreFlags GetStorage() const override;
private:
std::shared_ptr<AbstractImage> ResizeImage(const std::shared_ptr<AbstractImage>& oldImage, const Vector2ui& size) const override;
RenderDevice& m_renderDevice;
};
}
#include <Nazara/Graphics/GuillotineTextureAtlas.inl>
#endif // NAZARA_GUILLOTINETEXTUREATLAS_HPP

View File

@@ -0,0 +1,16 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/GuillotineTextureAtlas.hpp>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline GuillotineTextureAtlas::GuillotineTextureAtlas(RenderDevice& renderDevice) :
m_renderDevice(renderDevice)
{
}
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@@ -0,0 +1,103 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_TEXTSPRITE_HPP
#define NAZARA_TEXTSPRITE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/InstancedRenderable.hpp>
#include <Nazara/Renderer/RenderPipeline.hpp>
#include <Nazara/Utility/AbstractAtlas.hpp>
#include <Nazara/Utility/VertexDeclaration.hpp>
#include <Nazara/Utility/VertexStruct.hpp>
#include <array>
namespace Nz
{
class AbstractTextDrawer;
class NAZARA_GRAPHICS_API TextSprite : public InstancedRenderable
{
public:
TextSprite(std::shared_ptr<Material> material);
TextSprite(const TextSprite&) = delete;
TextSprite(TextSprite&&) noexcept = default;
~TextSprite() = default;
void BuildElement(std::size_t passIndex, const WorldInstance& worldInstance, std::vector<std::unique_ptr<RenderElement>>& elements) const override;
inline void Clear();
const std::shared_ptr<Material>& GetMaterial(std::size_t i) const;
std::size_t GetMaterialCount() const;
inline void SetMaterial(std::shared_ptr<Material> material);
void Update(const AbstractTextDrawer& drawer, float scale = 1.f);
TextSprite& operator=(const TextSprite&) = delete;
TextSprite& operator=(TextSprite&&) noexcept = default;
private:
void OnAtlasInvalidated(const AbstractAtlas* atlas);
void OnAtlasLayerChange(const AbstractAtlas* atlas, AbstractImage* oldLayer, AbstractImage* newLayer);
struct AtlasSlots
{
bool used;
NazaraSlot(AbstractAtlas, OnAtlasCleared, clearSlot);
NazaraSlot(AbstractAtlas, OnAtlasLayerChange, layerChangeSlot);
NazaraSlot(AbstractAtlas, OnAtlasRelease, releaseSlot);
};
struct RenderData
{
};
struct RenderKey
{
Texture* texture;
int renderOrder;
bool operator==(const RenderKey& rhs) const
{
return texture == rhs.texture && renderOrder == rhs.renderOrder;
}
bool operator!=(const RenderKey& rhs) const
{
return !operator==(rhs);
}
};
struct HashRenderKey
{
std::size_t operator()(const RenderKey& key) const
{
// Since renderOrder will be very small, this will be enough
return std::hash<Texture*>()(key.texture) + key.renderOrder;
}
};
struct RenderIndices
{
unsigned int first;
unsigned int count;
};
std::unordered_map<const AbstractAtlas*, AtlasSlots> m_atlases;
mutable std::unordered_map<RenderKey, RenderIndices, HashRenderKey> m_renderInfos;
std::shared_ptr<Material> m_material;
std::vector<RenderData> m_data;
std::vector<VertexStruct_XYZ_Color_UV> m_vertices;
};
}
#include <Nazara/Graphics/TextSprite.inl>
#endif

View File

@@ -0,0 +1,23 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/TextSprite.hpp>
#include <cassert>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline void TextSprite::Clear()
{
m_atlases.clear();
m_vertices.clear();
}
inline void TextSprite::SetMaterial(std::shared_ptr<Material> material)
{
m_material = std::move(material);
}
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@@ -12,6 +12,7 @@
#include <Nazara/Core/SparsePtr.hpp>
#include <Nazara/Math/Rect.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Enums.hpp>
namespace Nz
{
@@ -30,7 +31,7 @@ namespace Nz
virtual void Free(SparsePtr<const Rectui> rects, SparsePtr<unsigned int> layers, unsigned int count) = 0;
virtual AbstractImage* GetLayer(unsigned int layerIndex) const = 0;
virtual std::size_t GetLayerCount() const = 0;
virtual UInt32 GetStorage() const = 0;
virtual DataStoreFlags GetStorage() const = 0;
virtual bool Insert(const Image& image, Rectui* rect, bool* flipped, unsigned int* layerIndex) = 0;
AbstractAtlas& operator=(const AbstractAtlas&) = delete;

View File

@@ -127,6 +127,13 @@ namespace Nz
Max = Software
};
template<>
struct EnumAsFlags<DataStorage>
{
static constexpr DataStorage max = DataStorage::Max;
};
using DataStoreFlags = Flags<DataStorage>;
constexpr std::size_t DataStorageCount = static_cast<std::size_t>(DataStorage::Max) + 1;
enum class FaceFilling

View File

@@ -53,7 +53,7 @@ namespace Nz
void ClearKerningCache();
void ClearSizeInfoCache();
bool Create(FontData* data);
bool Create(std::unique_ptr<FontData> data);
void Destroy();
bool ExtractGlyph(unsigned int characterSize, char32_t character, TextStyleFlags style, float outlineThickness, FontGlyph* glyph) const;
@@ -74,7 +74,7 @@ namespace Nz
bool Precache(unsigned int characterSize, TextStyleFlags style, float outlineThickness, char32_t character) const;
bool Precache(unsigned int characterSize, TextStyleFlags style, float outlineThickness, const std::string& characterSet) const;
void SetAtlas(const std::shared_ptr<AbstractAtlas>& atlas);
void SetAtlas(std::shared_ptr<AbstractAtlas> atlas);
void SetGlyphBorder(unsigned int borderSize);
void SetMinimumStepSize(unsigned int minimumStepSize);
@@ -90,7 +90,7 @@ namespace Nz
static std::shared_ptr<Font> OpenFromMemory(const void* data, std::size_t size, const FontParams& params = FontParams());
static std::shared_ptr<Font> OpenFromStream(Stream& stream, const FontParams& params = FontParams());
static void SetDefaultAtlas(const std::shared_ptr<AbstractAtlas>& atlas);
static void SetDefaultAtlas(std::shared_ptr<AbstractAtlas> atlas);
static void SetDefaultGlyphBorder(unsigned int borderSize);
static void SetDefaultMinimumStepSize(unsigned int minimumStepSize);

View File

@@ -33,7 +33,7 @@ namespace Nz
GuillotineBinPack::GuillotineSplitHeuristic GetRectSplitHeuristic() const;
AbstractImage* GetLayer(unsigned int layerIndex) const override;
std::size_t GetLayerCount() const override;
UInt32 GetStorage() const override;
DataStoreFlags GetStorage() const override;
bool Insert(const Image& image, Rectui* rect, bool* flipped, unsigned int* layerIndex) override;
@@ -46,7 +46,7 @@ namespace Nz
protected:
struct Layer;
virtual AbstractImage* ResizeImage(AbstractImage* oldImage, const Vector2ui& size) const;
virtual std::shared_ptr<AbstractImage> ResizeImage(const std::shared_ptr<AbstractImage>& oldImage, const Vector2ui& size) const;
bool ResizeLayer(Layer& layer, const Vector2ui& size);
struct QueuedGlyph
@@ -59,7 +59,7 @@ namespace Nz
struct Layer
{
std::vector<QueuedGlyph> queuedGlyphs;
std::unique_ptr<AbstractImage> image;
std::shared_ptr<AbstractImage> image;
GuillotineBinPack binPack;
unsigned int freedRectangles = 0;
};

View File

@@ -70,18 +70,18 @@ namespace Nz
bool FlipVertically();
const UInt8* GetConstPixels(unsigned int x = 0, unsigned int y = 0, unsigned int z = 0, UInt8 level = 0) const;
unsigned int GetDepth(UInt8 level = 0) const override;
unsigned int GetDepth(UInt8 level = 0) const;
PixelFormat GetFormat() const override;
unsigned int GetHeight(UInt8 level = 0) const override;
unsigned int GetHeight(UInt8 level = 0) const;
UInt8 GetLevelCount() const override;
UInt8 GetMaxLevel() const override;
std::size_t GetMemoryUsage() const override;
std::size_t GetMemoryUsage(UInt8 level) const override;
UInt8 GetMaxLevel() const;
std::size_t GetMemoryUsage() const;
std::size_t GetMemoryUsage(UInt8 level) const;
Color GetPixelColor(unsigned int x, unsigned int y = 0, unsigned int z = 0) const;
UInt8* GetPixels(unsigned int x = 0, unsigned int y = 0, unsigned int z = 0, UInt8 level = 0);
Vector3ui GetSize(UInt8 level = 0) const override;
ImageType GetType() const override;
unsigned int GetWidth(UInt8 level = 0) const override;
unsigned int GetWidth(UInt8 level = 0) const;
bool HasAlpha() const;
@@ -101,9 +101,8 @@ namespace Nz
void SetLevelCount(UInt8 levelCount);
bool SetPixelColor(const Color& color, unsigned int x, unsigned int y = 0, unsigned int z = 0);
bool Update(const UInt8* pixels, unsigned int srcWidth = 0, unsigned int srcHeight = 0, UInt8 level = 0) override;
bool Update(const UInt8* pixels, const Boxui& box, unsigned int srcWidth = 0, unsigned int srcHeight = 0, UInt8 level = 0) override;
bool Update(const UInt8* pixels, const Rectui& rect, unsigned int z = 0, unsigned int srcWidth = 0, unsigned int srcHeight = 0, UInt8 level = 0) override;
using AbstractImage::Update;
bool Update(const void* pixels, const Boxui& box, unsigned int srcWidth = 0, unsigned int srcHeight = 0, UInt8 level = 0) override;
Image& operator=(const Image& image);
inline Image& operator=(Image&& image) noexcept;