Change TextDrawable base class

This commit is contained in:
Georg Hagen
2025-01-04 03:08:06 +01:00
parent 94025c79c6
commit ca93036f31
5 changed files with 101 additions and 45 deletions

View File

@@ -158,7 +158,7 @@ namespace OpenVulkano
void Close() override
{
for (SimpleDrawable* d: m_drawablesPool)
for (Drawable* d: m_drawablesPool)
{
d->Close();
delete d;
@@ -173,7 +173,7 @@ namespace OpenVulkano
SdfFontAtlasGenerator m_atlasGenerator;
MsdfFontAtlasGenerator m_msdfAtlasGenerator;
#endif
std::vector<SimpleDrawable*> m_drawablesPool;
std::vector<Drawable*> m_drawablesPool;
std::vector<Node> m_nodesPool;
Vector3f_SIMD m_position = { 0, 0, -10 };
OpenVulkano::Scene::UI::SimpleUi m_ui;

View File

@@ -44,7 +44,8 @@ namespace OpenVulkano::Scene
Shader DEFAULT_SHADER_MSDF = MakeDefaultShader(FontAtlasType::MSDF);
}
TextDrawable::TextDrawable(const TextConfig& config) : m_cfg(config)
TextDrawable::TextDrawable(const TextConfig& config)
: Drawable(DrawEncoder::GetDrawEncoder<TextDrawable>()), m_cfg(config)
{
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
m_uniBuffer.binding.stageFlags = ShaderProgramType::FRAGMENT;
@@ -63,7 +64,7 @@ namespace OpenVulkano::Scene
{}
TextDrawable::TextDrawable(const Array<char>& atlasMetadata, Texture* atlasTex, const TextConfig& config)
: m_cfg(config)
: Drawable(DrawEncoder::GetDrawEncoder<TextDrawable>()), m_cfg(config)
{
uint32_t isPacked;
std::memcpy(&isPacked, atlasMetadata.Data() + (atlasMetadata.Size() - sizeof(uint32_t)), sizeof(uint32_t));
@@ -74,18 +75,17 @@ namespace OpenVulkano::Scene
m_atlasData = std::make_shared<AtlasData>();
if (isPacked)
{
m_material.texture = &m_atlasData->texture;
Texture* texture = &m_atlasData->texture;
m_atlasData->img = Image::IImageLoader::loadData(reinterpret_cast<const uint8_t*>(atlasMetadata.Data()), offsetToMetadata);
m_material.texture->format = m_atlasData->img->dataFormat;
m_material.texture->resolution = m_atlasData->img->resolution;
m_material.texture->size = m_atlasData->img->data.Size();
m_material.texture->textureBuffer = m_atlasData->img->data.Data();
texture->format = m_atlasData->img->dataFormat;
texture->resolution = m_atlasData->img->resolution;
texture->size = m_atlasData->img->data.Size();
texture->textureBuffer = m_atlasData->img->data.Data();
}
else
{
if (atlasTex == nullptr) { throw std::runtime_error("Atlas texture cannot be null with non-packed atlas metadata"); }
m_atlasData->texture = *atlasTex;
m_material.texture = atlasTex;
}
// metadata info
@@ -110,12 +110,12 @@ namespace OpenVulkano::Scene
m_uniBuffer.binding.stageFlags = ShaderProgramType::FRAGMENT;
if (m_atlasData->meta.atlasType == FontAtlasType::BITMAP)
{
m_material.texture->m_samplerConfig = &SamplerConfig::NEAREST;
m_atlasData->texture.m_samplerConfig = &SamplerConfig::NEAREST;
}
}
TextDrawable::TextDrawable(const std::shared_ptr<AtlasData>& atlasData, const TextConfig& config)
: m_atlasData(atlasData), m_cfg(config)
: Drawable(DrawEncoder::GetDrawEncoder<TextDrawable>()), m_atlasData(atlasData), m_cfg(config)
{
if (!atlasData || !*atlasData) throw std::runtime_error("Cannot initialize text drawable with empty atlas data");
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
@@ -216,16 +216,16 @@ namespace OpenVulkano::Scene
}
m_bbox.Init(bmin, bmax);
Shader* shader = nullptr;
switch (m_atlasData->meta.atlasType)
if (!GetShader())
{
case FontAtlasType::SDF: shader = &DEFAULT_SHADER_SDF; break;
case FontAtlasType::MSDF: shader = &DEFAULT_SHADER_MSDF; break;
default: Logger::RENDER->warn("No default shader for atlas type: {}", m_atlasData->meta.atlasType.GetName());
case FontAtlasType::BITMAP: shader = &DEFAULT_SHADER_BITMAP; break;
switch (m_atlasData->meta.atlasType)
{
case FontAtlasType::SDF: SetShader(&DEFAULT_SHADER_SDF); break;
case FontAtlasType::MSDF: SetShader(&DEFAULT_SHADER_MSDF); break;
default: Logger::RENDER->warn("No default shader for atlas type: {}", m_atlasData->meta.atlasType.GetName());
case FontAtlasType::BITMAP: SetShader(&DEFAULT_SHADER_BITMAP); break;
}
}
SimpleDrawable::Init(shader, &m_geometry, &m_material, &m_uniBuffer);
}
void TextDrawable::SetAtlasData(const std::shared_ptr<AtlasData>& atlasData)

View File

@@ -6,9 +6,8 @@
#pragma once
#include "SimpleDrawable.hpp"
#include "Drawable.hpp"
#include "Texture.hpp"
#include "Material.hpp"
#include "Geometry.hpp"
#include "UniformBuffer.hpp"
#include "AtlasData.hpp"
@@ -26,10 +25,9 @@ namespace OpenVulkano::Scene
float smoothing = 1.f/32.f;
};
class TextDrawable : public SimpleDrawable
class TextDrawable : public Drawable
{
Geometry m_geometry;
Material m_material;
UniformBuffer m_uniBuffer;
std::shared_ptr<AtlasData> m_atlasData;
Math::AABB m_bbox;
@@ -52,5 +50,9 @@ namespace OpenVulkano::Scene
[[nodiscard]] TextConfig& GetConfig() { return m_cfg; }
[[nodiscard]] const std::string& GetText() const { return m_text; }
[[nodiscard]] const std::shared_ptr<AtlasData> GetAtlasData() { return m_atlasData; }
[[nodiscard]] Geometry* GetGeometry() { return &m_geometry; }
[[nodiscard]] Texture* GetTexture() { return &m_atlasData->texture; }
[[nodiscard]] UniformBuffer* GetUniformBuffer() { return &m_uniBuffer; }
};
}

View File

@@ -18,11 +18,11 @@ using namespace OpenVulkano::Scene;
namespace OpenVulkano::Vulkan
{
void EncodeBackground(LabelDrawable* labelDrawable, Vulkan::VulkanDrawContext* drawContext)
void EncodeBackground(LabelDrawable* labelDrawable, VulkanDrawContext* drawContext)
{
if (labelDrawable->IsBillboard())
{
OpenVulkano::Scene::UniformBuffer* buffer = labelDrawable->GetBillboardBuffer();
Scene::UniformBuffer* buffer = labelDrawable->GetBillboardBuffer();
VulkanUniformBuffer* vkBuffer = buffer->GetRenderResource();
if (!vkBuffer)
{
@@ -31,7 +31,7 @@ namespace OpenVulkano::Vulkan
vkBuffer->Record(drawContext);
}
OpenVulkano::Scene::UniformBuffer* labelBuffer = labelDrawable->GetLabelBuffer();
Scene::UniformBuffer* labelBuffer = labelDrawable->GetLabelBuffer();
VulkanUniformBuffer* vkBuffer = labelBuffer->GetRenderResource();
if (!vkBuffer)
{
@@ -50,13 +50,13 @@ namespace OpenVulkano::Vulkan
drawContext->commandBuffer.draw(4, 1, 0, 0);
}
void EncodeTextDrawable(LabelDrawable* labelDrawable, Vulkan::VulkanDrawContext* drawContext)
void EncodeTextDrawable(LabelDrawable* labelDrawable, VulkanDrawContext* drawContext)
{
for (TextDrawable& entry : labelDrawable->GetTexts())
{
OpenVulkano::Scene::Shader* shader = entry.GetShader();
Shader* shader = entry.GetShader();
drawContext->EncodeShader(shader);
Geometry* mesh = entry.GetMesh();
Geometry* mesh = entry.GetGeometry();
VulkanGeometry* renderGeo = mesh->GetRenderResource();
if (!renderGeo)
{
@@ -64,16 +64,16 @@ namespace OpenVulkano::Vulkan
}
renderGeo->RecordBind(drawContext->commandBuffer);
std::array<OpenVulkano::Scene::UniformBuffer*, 2> uniforms = { nullptr, nullptr };
std::array<Scene::UniformBuffer*, 2> uniforms = { nullptr, nullptr };
// fragment shader buffer
uniforms[0] = entry.GetBuffer();
uniforms[0] = entry.GetUniformBuffer();
if (labelDrawable->IsBillboard())
{
// vertex shader buffer
uniforms[1] = labelDrawable->GetBillboardBuffer();
}
for (OpenVulkano::Scene::UniformBuffer* buffer : uniforms)
for (Scene::UniformBuffer* buffer : uniforms)
{
if (buffer)
{
@@ -86,18 +86,14 @@ namespace OpenVulkano::Vulkan
}
}
if (Material* material = entry.GetMaterial())
if (Texture* texture = entry.GetTexture())
{
if (Texture* texture = material->texture)
VulkanTexture* renderTexture = texture->GetRenderResource();
if (!renderTexture)
{
VulkanTexture* renderTexture = texture->GetRenderResource();
if (!renderTexture)
{
drawContext->renderer->GetResourceManager().PrepareMaterial(entry.GetMaterial());
renderTexture = texture->GetRenderResource();
}
renderTexture->Record(drawContext);
renderTexture = drawContext->renderer->GetResourceManager().PrepareTexture(entry.GetTexture());
}
renderTexture->Record(drawContext);
}
for (Node* node: labelDrawable->GetNodes())
@@ -112,7 +108,7 @@ namespace OpenVulkano::Vulkan
}
}
void EncodeLabelDrawable(Drawable* instance, Vulkan::VulkanDrawContext* drawContext)
void EncodeLabelDrawable(Drawable* instance, VulkanDrawContext* drawContext)
{
LabelDrawable* labelDrawable = static_cast<LabelDrawable*>(instance);
EncodeBackground(labelDrawable, drawContext);
@@ -122,5 +118,5 @@ namespace OpenVulkano::Vulkan
namespace
{
void* labelDrawableVulkanEncoderReg = DrawEncoder::RegisterVulkanEncodeFunction<LabelDrawable>(&OpenVulkano::Vulkan::EncodeLabelDrawable);
[[maybe_unused]] void* labelDrawableVulkanEncoderReg = DrawEncoder::RegisterVulkanEncodeFunction<LabelDrawable>(&OpenVulkano::Vulkan::EncodeLabelDrawable);
}

View File

@@ -0,0 +1,58 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Scene/TextDrawable.hpp"
#include "VulkanGeometry.hpp"
#include "VulkanNode.hpp"
#include "Vulkan/VulkanDrawContext.hpp"
#include "Vulkan/Scene/VulkanUniformBuffer.hpp"
#include "VulkanTexture.hpp"
using namespace OpenVulkano::Scene;
namespace OpenVulkano::Vulkan
{
void EncodeTextDrawable(Drawable* instance, Vulkan::VulkanDrawContext* drawContext)
{
TextDrawable* drawable = static_cast<TextDrawable*>(instance);
Geometry* mesh = drawable->GetGeometry();
VulkanGeometry* renderGeo = mesh->GetRenderResource();
if (!renderGeo) renderGeo = drawContext->renderer->GetResourceManager().PrepareGeometry(mesh);
renderGeo->RecordBind(drawContext->commandBuffer);
if (drawable->GetUniformBuffer())
{
VulkanUniformBuffer* vkBuffer = drawable->GetUniformBuffer()->GetRenderResource();
if (!vkBuffer)
{
vkBuffer = drawContext->renderer->GetResourceManager().PrepareUniformBuffer(drawable->GetUniformBuffer());
}
vkBuffer->Record(drawContext);
}
if (Texture* texture = drawable->GetTexture())
{
VulkanTexture* renderTexture = texture->GetRenderResource();
if (!renderTexture)
{
renderTexture = drawContext->renderer->GetResourceManager().PrepareTexture(drawable->GetTexture());
}
renderTexture->Record(drawContext);
}
for(Node* node : instance->GetNodes())
{
if (!node->IsEnabled()) [[unlikely]] continue;
drawContext->EncodeNode(node);
renderGeo->RecordDraw(drawContext->commandBuffer);
}
}
}
namespace
{
[[maybe_unused]] void* textDrawableVulkanEncoderReg = DrawEncoder::RegisterVulkanEncodeFunction<TextDrawable>(&OpenVulkano::Vulkan::EncodeTextDrawable);
}