Change TextDrawable base class
This commit is contained in:
@@ -158,7 +158,7 @@ namespace OpenVulkano
|
|||||||
|
|
||||||
void Close() override
|
void Close() override
|
||||||
{
|
{
|
||||||
for (SimpleDrawable* d: m_drawablesPool)
|
for (Drawable* d: m_drawablesPool)
|
||||||
{
|
{
|
||||||
d->Close();
|
d->Close();
|
||||||
delete d;
|
delete d;
|
||||||
@@ -173,7 +173,7 @@ namespace OpenVulkano
|
|||||||
SdfFontAtlasGenerator m_atlasGenerator;
|
SdfFontAtlasGenerator m_atlasGenerator;
|
||||||
MsdfFontAtlasGenerator m_msdfAtlasGenerator;
|
MsdfFontAtlasGenerator m_msdfAtlasGenerator;
|
||||||
#endif
|
#endif
|
||||||
std::vector<SimpleDrawable*> m_drawablesPool;
|
std::vector<Drawable*> m_drawablesPool;
|
||||||
std::vector<Node> m_nodesPool;
|
std::vector<Node> m_nodesPool;
|
||||||
Vector3f_SIMD m_position = { 0, 0, -10 };
|
Vector3f_SIMD m_position = { 0, 0, -10 };
|
||||||
OpenVulkano::Scene::UI::SimpleUi m_ui;
|
OpenVulkano::Scene::UI::SimpleUi m_ui;
|
||||||
|
|||||||
@@ -44,7 +44,8 @@ namespace OpenVulkano::Scene
|
|||||||
Shader DEFAULT_SHADER_MSDF = MakeDefaultShader(FontAtlasType::MSDF);
|
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.Init(sizeof(TextConfig), &m_cfg, 3);
|
||||||
m_uniBuffer.binding.stageFlags = ShaderProgramType::FRAGMENT;
|
m_uniBuffer.binding.stageFlags = ShaderProgramType::FRAGMENT;
|
||||||
@@ -63,7 +64,7 @@ namespace OpenVulkano::Scene
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
TextDrawable::TextDrawable(const Array<char>& atlasMetadata, Texture* atlasTex, const TextConfig& config)
|
TextDrawable::TextDrawable(const Array<char>& atlasMetadata, Texture* atlasTex, const TextConfig& config)
|
||||||
: m_cfg(config)
|
: Drawable(DrawEncoder::GetDrawEncoder<TextDrawable>()), m_cfg(config)
|
||||||
{
|
{
|
||||||
uint32_t isPacked;
|
uint32_t isPacked;
|
||||||
std::memcpy(&isPacked, atlasMetadata.Data() + (atlasMetadata.Size() - sizeof(uint32_t)), sizeof(uint32_t));
|
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>();
|
m_atlasData = std::make_shared<AtlasData>();
|
||||||
if (isPacked)
|
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_atlasData->img = Image::IImageLoader::loadData(reinterpret_cast<const uint8_t*>(atlasMetadata.Data()), offsetToMetadata);
|
||||||
m_material.texture->format = m_atlasData->img->dataFormat;
|
texture->format = m_atlasData->img->dataFormat;
|
||||||
m_material.texture->resolution = m_atlasData->img->resolution;
|
texture->resolution = m_atlasData->img->resolution;
|
||||||
m_material.texture->size = m_atlasData->img->data.Size();
|
texture->size = m_atlasData->img->data.Size();
|
||||||
m_material.texture->textureBuffer = m_atlasData->img->data.Data();
|
texture->textureBuffer = m_atlasData->img->data.Data();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (atlasTex == nullptr) { throw std::runtime_error("Atlas texture cannot be null with non-packed atlas metadata"); }
|
if (atlasTex == nullptr) { throw std::runtime_error("Atlas texture cannot be null with non-packed atlas metadata"); }
|
||||||
m_atlasData->texture = *atlasTex;
|
m_atlasData->texture = *atlasTex;
|
||||||
m_material.texture = atlasTex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// metadata info
|
// metadata info
|
||||||
@@ -110,12 +110,12 @@ namespace OpenVulkano::Scene
|
|||||||
m_uniBuffer.binding.stageFlags = ShaderProgramType::FRAGMENT;
|
m_uniBuffer.binding.stageFlags = ShaderProgramType::FRAGMENT;
|
||||||
if (m_atlasData->meta.atlasType == FontAtlasType::BITMAP)
|
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)
|
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");
|
if (!atlasData || !*atlasData) throw std::runtime_error("Cannot initialize text drawable with empty atlas data");
|
||||||
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
|
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
|
||||||
@@ -216,16 +216,16 @@ namespace OpenVulkano::Scene
|
|||||||
}
|
}
|
||||||
m_bbox.Init(bmin, bmax);
|
m_bbox.Init(bmin, bmax);
|
||||||
|
|
||||||
Shader* shader = nullptr;
|
if (!GetShader())
|
||||||
|
{
|
||||||
switch (m_atlasData->meta.atlasType)
|
switch (m_atlasData->meta.atlasType)
|
||||||
{
|
{
|
||||||
case FontAtlasType::SDF: shader = &DEFAULT_SHADER_SDF; break;
|
case FontAtlasType::SDF: SetShader(&DEFAULT_SHADER_SDF); break;
|
||||||
case FontAtlasType::MSDF: shader = &DEFAULT_SHADER_MSDF; break;
|
case FontAtlasType::MSDF: SetShader(&DEFAULT_SHADER_MSDF); break;
|
||||||
default: Logger::RENDER->warn("No default shader for atlas type: {}", m_atlasData->meta.atlasType.GetName());
|
default: Logger::RENDER->warn("No default shader for atlas type: {}", m_atlasData->meta.atlasType.GetName());
|
||||||
case FontAtlasType::BITMAP: shader = &DEFAULT_SHADER_BITMAP; break;
|
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)
|
void TextDrawable::SetAtlasData(const std::shared_ptr<AtlasData>& atlasData)
|
||||||
|
|||||||
@@ -6,9 +6,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "SimpleDrawable.hpp"
|
#include "Drawable.hpp"
|
||||||
#include "Texture.hpp"
|
#include "Texture.hpp"
|
||||||
#include "Material.hpp"
|
|
||||||
#include "Geometry.hpp"
|
#include "Geometry.hpp"
|
||||||
#include "UniformBuffer.hpp"
|
#include "UniformBuffer.hpp"
|
||||||
#include "AtlasData.hpp"
|
#include "AtlasData.hpp"
|
||||||
@@ -26,10 +25,9 @@ namespace OpenVulkano::Scene
|
|||||||
float smoothing = 1.f/32.f;
|
float smoothing = 1.f/32.f;
|
||||||
};
|
};
|
||||||
|
|
||||||
class TextDrawable : public SimpleDrawable
|
class TextDrawable : public Drawable
|
||||||
{
|
{
|
||||||
Geometry m_geometry;
|
Geometry m_geometry;
|
||||||
Material m_material;
|
|
||||||
UniformBuffer m_uniBuffer;
|
UniformBuffer m_uniBuffer;
|
||||||
std::shared_ptr<AtlasData> m_atlasData;
|
std::shared_ptr<AtlasData> m_atlasData;
|
||||||
Math::AABB m_bbox;
|
Math::AABB m_bbox;
|
||||||
@@ -52,5 +50,9 @@ namespace OpenVulkano::Scene
|
|||||||
[[nodiscard]] TextConfig& GetConfig() { return m_cfg; }
|
[[nodiscard]] TextConfig& GetConfig() { return m_cfg; }
|
||||||
[[nodiscard]] const std::string& GetText() const { return m_text; }
|
[[nodiscard]] const std::string& GetText() const { return m_text; }
|
||||||
[[nodiscard]] const std::shared_ptr<AtlasData> GetAtlasData() { return m_atlasData; }
|
[[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; }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,11 +18,11 @@ using namespace OpenVulkano::Scene;
|
|||||||
namespace OpenVulkano::Vulkan
|
namespace OpenVulkano::Vulkan
|
||||||
{
|
{
|
||||||
|
|
||||||
void EncodeBackground(LabelDrawable* labelDrawable, Vulkan::VulkanDrawContext* drawContext)
|
void EncodeBackground(LabelDrawable* labelDrawable, VulkanDrawContext* drawContext)
|
||||||
{
|
{
|
||||||
if (labelDrawable->IsBillboard())
|
if (labelDrawable->IsBillboard())
|
||||||
{
|
{
|
||||||
OpenVulkano::Scene::UniformBuffer* buffer = labelDrawable->GetBillboardBuffer();
|
Scene::UniformBuffer* buffer = labelDrawable->GetBillboardBuffer();
|
||||||
VulkanUniformBuffer* vkBuffer = buffer->GetRenderResource();
|
VulkanUniformBuffer* vkBuffer = buffer->GetRenderResource();
|
||||||
if (!vkBuffer)
|
if (!vkBuffer)
|
||||||
{
|
{
|
||||||
@@ -31,7 +31,7 @@ namespace OpenVulkano::Vulkan
|
|||||||
vkBuffer->Record(drawContext);
|
vkBuffer->Record(drawContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
OpenVulkano::Scene::UniformBuffer* labelBuffer = labelDrawable->GetLabelBuffer();
|
Scene::UniformBuffer* labelBuffer = labelDrawable->GetLabelBuffer();
|
||||||
VulkanUniformBuffer* vkBuffer = labelBuffer->GetRenderResource();
|
VulkanUniformBuffer* vkBuffer = labelBuffer->GetRenderResource();
|
||||||
if (!vkBuffer)
|
if (!vkBuffer)
|
||||||
{
|
{
|
||||||
@@ -50,13 +50,13 @@ namespace OpenVulkano::Vulkan
|
|||||||
drawContext->commandBuffer.draw(4, 1, 0, 0);
|
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())
|
for (TextDrawable& entry : labelDrawable->GetTexts())
|
||||||
{
|
{
|
||||||
OpenVulkano::Scene::Shader* shader = entry.GetShader();
|
Shader* shader = entry.GetShader();
|
||||||
drawContext->EncodeShader(shader);
|
drawContext->EncodeShader(shader);
|
||||||
Geometry* mesh = entry.GetMesh();
|
Geometry* mesh = entry.GetGeometry();
|
||||||
VulkanGeometry* renderGeo = mesh->GetRenderResource();
|
VulkanGeometry* renderGeo = mesh->GetRenderResource();
|
||||||
if (!renderGeo)
|
if (!renderGeo)
|
||||||
{
|
{
|
||||||
@@ -64,16 +64,16 @@ namespace OpenVulkano::Vulkan
|
|||||||
}
|
}
|
||||||
renderGeo->RecordBind(drawContext->commandBuffer);
|
renderGeo->RecordBind(drawContext->commandBuffer);
|
||||||
|
|
||||||
std::array<OpenVulkano::Scene::UniformBuffer*, 2> uniforms = { nullptr, nullptr };
|
std::array<Scene::UniformBuffer*, 2> uniforms = { nullptr, nullptr };
|
||||||
// fragment shader buffer
|
// fragment shader buffer
|
||||||
uniforms[0] = entry.GetBuffer();
|
uniforms[0] = entry.GetUniformBuffer();
|
||||||
if (labelDrawable->IsBillboard())
|
if (labelDrawable->IsBillboard())
|
||||||
{
|
{
|
||||||
// vertex shader buffer
|
// vertex shader buffer
|
||||||
uniforms[1] = labelDrawable->GetBillboardBuffer();
|
uniforms[1] = labelDrawable->GetBillboardBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (OpenVulkano::Scene::UniformBuffer* buffer : uniforms)
|
for (Scene::UniformBuffer* buffer : uniforms)
|
||||||
{
|
{
|
||||||
if (buffer)
|
if (buffer)
|
||||||
{
|
{
|
||||||
@@ -86,19 +86,15 @@ namespace OpenVulkano::Vulkan
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Material* material = entry.GetMaterial())
|
if (Texture* texture = entry.GetTexture())
|
||||||
{
|
|
||||||
if (Texture* texture = material->texture)
|
|
||||||
{
|
{
|
||||||
VulkanTexture* renderTexture = texture->GetRenderResource();
|
VulkanTexture* renderTexture = texture->GetRenderResource();
|
||||||
if (!renderTexture)
|
if (!renderTexture)
|
||||||
{
|
{
|
||||||
drawContext->renderer->GetResourceManager().PrepareMaterial(entry.GetMaterial());
|
renderTexture = drawContext->renderer->GetResourceManager().PrepareTexture(entry.GetTexture());
|
||||||
renderTexture = texture->GetRenderResource();
|
|
||||||
}
|
}
|
||||||
renderTexture->Record(drawContext);
|
renderTexture->Record(drawContext);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (Node* node: labelDrawable->GetNodes())
|
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);
|
LabelDrawable* labelDrawable = static_cast<LabelDrawable*>(instance);
|
||||||
EncodeBackground(labelDrawable, drawContext);
|
EncodeBackground(labelDrawable, drawContext);
|
||||||
@@ -122,5 +118,5 @@ namespace OpenVulkano::Vulkan
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
void* labelDrawableVulkanEncoderReg = DrawEncoder::RegisterVulkanEncodeFunction<LabelDrawable>(&OpenVulkano::Vulkan::EncodeLabelDrawable);
|
[[maybe_unused]] void* labelDrawableVulkanEncoderReg = DrawEncoder::RegisterVulkanEncodeFunction<LabelDrawable>(&OpenVulkano::Vulkan::EncodeLabelDrawable);
|
||||||
}
|
}
|
||||||
58
openVulkanoCpp/Vulkan/Scene/TextDrawableVulkanEncoder.cpp
Normal file
58
openVulkanoCpp/Vulkan/Scene/TextDrawableVulkanEncoder.cpp
Normal 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user