Remove unnecessary buffer

This commit is contained in:
Georg Hagen
2025-01-05 23:31:42 +01:00
parent 4f1d730e30
commit 92b9fb597f
8 changed files with 79 additions and 121 deletions

View File

@@ -53,7 +53,6 @@ namespace OpenVulkano
m_nodesPool.resize(N); m_nodesPool.resize(N);
m_drawablesPool.reserve(N); m_drawablesPool.reserve(N);
BillboardControlBlock billboardSettings;
LabelDrawableSettings labelSettings; LabelDrawableSettings labelSettings;
for (int i = 0; i < N; i++) for (int i = 0; i < N; i++)
@@ -69,7 +68,6 @@ namespace OpenVulkano
} }
bool isBillboard = i % 2 == 0 ? 1 : 0; bool isBillboard = i % 2 == 0 ? 1 : 0;
LabelDrawable& label = m_drawablesPool.emplace_back(textDrawable.GetAtlasData(), labelSettings, isBillboard); LabelDrawable& label = m_drawablesPool.emplace_back(textDrawable.GetAtlasData(), labelSettings, isBillboard);
label.SetBillboardSettings(billboardSettings);
label.AddText(texts[i]); label.AddText(texts[i]);
if (i == 2) if (i == 2)
{ {
@@ -80,7 +78,7 @@ namespace OpenVulkano
} }
m_drawablesPool[i].SetIsHittable(true); m_drawablesPool[i].SetIsHittable(true);
m_scene.GetRoot()->AddChild(&m_nodesPool[i]); m_scene.GetRoot()->AddChild(&m_nodesPool[i]);
m_nodesPool[i].SetMatrix(Math::Utils::translate(glm::mat4x4(1.f), Vector3f(-5 + std::rand() % 5, -5 + std::rand() % 5, -std::rand() % 10))); m_nodesPool[i].SetMatrix(Math::Utils::translate(glm::mat4x4(1.f), Vector3f(5 - std::rand() % 10, 5 - std::rand() % 10, 5 - std::rand() % 10)));
m_nodesPool[i].AddDrawable(&m_drawablesPool[i]); m_nodesPool[i].AddDrawable(&m_drawablesPool[i]);
} }

View File

@@ -20,21 +20,10 @@ namespace OpenVulkano::Scene
Shader MakeLabelBgShader(const bool billboard) Shader MakeLabelBgShader(const bool billboard)
{ {
Shader backgroundShader; Shader backgroundShader;
if (!billboard) backgroundShader.AddShaderProgram(ShaderProgramType::VERTEX, billboard ? "Shader/labelBillboard" : "Shader/label");
{
backgroundShader.AddShaderProgram(ShaderProgramType::VERTEX, "Shader/label");
}
else
{
backgroundShader.AddShaderProgram(ShaderProgramType::VERTEX, "Shader/labelBillboard");
// binding for billboard's buffer
DescriptorSetLayoutBinding binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
binding.stageFlags = ShaderProgramType::Type::VERTEX;
backgroundShader.AddDescriptorSetLayoutBinding(binding, 4);
}
backgroundShader.AddShaderProgram(ShaderProgramType::FRAGMENT, "Shader/label"); backgroundShader.AddShaderProgram(ShaderProgramType::FRAGMENT, "Shader/label");
backgroundShader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING, 2); backgroundShader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING, 2);
backgroundShader.AddDescriptorSetLayoutBinding(UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING, 5); backgroundShader.AddDescriptorSetLayoutBinding(UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING, 4);
backgroundShader.topology = Topology::TRIANGLE_STRIP; backgroundShader.topology = Topology::TRIANGLE_STRIP;
backgroundShader.cullMode = CullMode::NONE; backgroundShader.cullMode = CullMode::NONE;
return backgroundShader; return backgroundShader;
@@ -44,6 +33,7 @@ namespace OpenVulkano::Scene
{ {
Shader shader = TextDrawable::MakeDefaultShader(type); Shader shader = TextDrawable::MakeDefaultShader(type);
shader.depthCompareOp = CompareOp::LESS_OR_EQUAL; shader.depthCompareOp = CompareOp::LESS_OR_EQUAL;
shader.EnableDepthBias();
if (billboard) if (billboard)
{ {
for (auto& program : shader.shaderPrograms) for (auto& program : shader.shaderPrograms)
@@ -55,10 +45,9 @@ namespace OpenVulkano::Scene
} }
} }
DescriptorSetLayoutBinding billboardUniformBinding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING; DescriptorSetLayoutBinding billboardUniformBinding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
billboardUniformBinding.stageFlags = ShaderProgramType::Type::VERTEX;
shader.AddDescriptorSetLayoutBinding(billboardUniformBinding, 4); shader.AddDescriptorSetLayoutBinding(billboardUniformBinding, 4);
shader.depthBiasConstant = 0.01f;
} }
shader.EnableDepthBias();
return shader; return shader;
} }
@@ -123,11 +112,6 @@ namespace OpenVulkano::Scene
if (m_settings.hasArrow) m_labelData.bboxCenter.y -= m_settings.arrowLength; if (m_settings.hasArrow) m_labelData.bboxCenter.y -= m_settings.arrowLength;
} }
void LabelDrawable::SetBillboardSettings(const BillboardControlBlock& settings)
{
m_billboardSettings = settings;
}
std::optional<RayHit> LabelDrawable::Intersect(const Ray& ray) const std::optional<RayHit> LabelDrawable::Intersect(const Ray& ray) const
{ {
return ray.IntersectAABB(m_bbox); return ray.IntersectAABB(m_bbox);
@@ -135,16 +119,9 @@ namespace OpenVulkano::Scene
void LabelDrawable::SetupBuffers() void LabelDrawable::SetupBuffers()
{ {
m_billboardBuffer.size = sizeof(BillboardControlBlock);
m_billboardBuffer.data = &m_billboardSettings;
m_billboardBuffer.setId = 4;
DescriptorSetLayoutBinding binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
binding.stageFlags = ShaderProgramType::Type::VERTEX;
m_billboardBuffer.binding = binding;
m_labelBuffer.size = sizeof(LabelUniformData); m_labelBuffer.size = sizeof(LabelUniformData);
m_labelBuffer.data = &m_labelData; m_labelBuffer.data = &m_labelData;
m_labelBuffer.setId = 5; m_labelBuffer.setId = 4;
m_labelBuffer.binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING; m_labelBuffer.binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
} }
} }

View File

@@ -8,7 +8,6 @@
#include "Scene/Drawable.hpp" #include "Scene/Drawable.hpp"
#include "Scene/UniformBuffer.hpp" #include "Scene/UniformBuffer.hpp"
#include "Scene/BillboardControlBlock.hpp"
#include "Math/AABB.hpp" #include "Math/AABB.hpp"
#include "Scene/TextDrawable.hpp" #include "Scene/TextDrawable.hpp"
#include <list> #include <list>
@@ -38,6 +37,7 @@ namespace OpenVulkano::Scene
float arrowWidth = 0.f; float arrowWidth = 0.f;
int32_t hasRoundedCorners = false; int32_t hasRoundedCorners = false;
int32_t hasArrow = false; int32_t hasArrow = false;
int32_t isBillboardFixedSize = false;
}; };
class LabelDrawable final : public Drawable class LabelDrawable final : public Drawable
@@ -47,13 +47,10 @@ namespace OpenVulkano::Scene
const LabelDrawableSettings& settings = LabelDrawableSettings(), bool isBillboard = false); const LabelDrawableSettings& settings = LabelDrawableSettings(), bool isBillboard = false);
void AddText(const std::string& text, const TextConfig& config = TextConfig()); void AddText(const std::string& text, const TextConfig& config = TextConfig());
void SetLabelSettings(const LabelDrawableSettings& settings); void SetLabelSettings(const LabelDrawableSettings& settings);
void SetBillboardSettings(const BillboardControlBlock& settings);
void SetPosition(const Math::Vector3f& pos) { m_position = pos; } void SetPosition(const Math::Vector3f& pos) { m_position = pos; }
[[nodiscard]] std::list<TextDrawable>& GetTexts() { return m_texts; } [[nodiscard]] std::list<TextDrawable>& GetTexts() { return m_texts; }
[[nodiscard]] LabelDrawableSettings& GetSettings() { return m_settings; } [[nodiscard]] LabelDrawableSettings& GetSettings() { return m_settings; }
[[nodiscard]] UniformBuffer* GetBillboardBuffer() { return &m_billboardBuffer; }
[[nodiscard]] UniformBuffer* GetLabelBuffer() { return &m_labelBuffer; } [[nodiscard]] UniformBuffer* GetLabelBuffer() { return &m_labelBuffer; }
[[nodiscard]] BillboardControlBlock& GetBillboardSettings() { return m_billboardSettings; }
[[nodiscard]] Math::Vector3f& GetPosition() { return m_position; } [[nodiscard]] Math::Vector3f& GetPosition() { return m_position; }
[[nodiscard]] bool IsBillboard() const { return m_isBillboard; } [[nodiscard]] bool IsBillboard() const { return m_isBillboard; }
[[nodiscard]] const Math::AABB& GetBoundingBox() const { return m_bbox; } [[nodiscard]] const Math::AABB& GetBoundingBox() const { return m_bbox; }
@@ -62,13 +59,11 @@ namespace OpenVulkano::Scene
private: private:
void SetupBuffers(); void SetupBuffers();
UniformBuffer m_billboardBuffer;
UniformBuffer m_labelBuffer; UniformBuffer m_labelBuffer;
std::list<TextDrawable> m_texts; // Using list instead of vector for stable iterators std::list<TextDrawable> m_texts; // Using list instead of vector for stable iterators
LabelDrawableSettings m_settings; LabelDrawableSettings m_settings;
LabelUniformData m_labelData; LabelUniformData m_labelData;
std::shared_ptr<AtlasData> m_atlasData; std::shared_ptr<AtlasData> m_atlasData;
BillboardControlBlock m_billboardSettings;
Math::Vector3f m_position = { 0, 0, 0 }; Math::Vector3f m_position = { 0, 0, 0 };
Math::AABB m_bbox; Math::AABB m_bbox;
bool m_isBillboard; bool m_isBillboard;

View File

@@ -3,7 +3,7 @@
layout(location = 1) in vec2 texCoord; layout(location = 1) in vec2 texCoord;
layout(location = 0) out vec4 outColor; layout(location = 0) out vec4 outColor;
layout(set = 5, binding = 0) uniform LabelData layout(set = 4, binding = 0) uniform LabelData
{ {
vec4 color; vec4 color;
vec2 textSize; vec2 textSize;

View File

@@ -11,13 +11,14 @@ layout(set = 1, binding = 0) uniform CameraData
mat4 viewProjection; mat4 viewProjection;
} cam; } cam;
layout(set = 5, binding = 0) uniform LabelData layout(set = 4, binding = 0) uniform LabelData
{ {
vec4 color; vec4 color;
vec2 textSize; vec2 textSize;
vec2 bboxCenter; vec2 bboxCenter;
float radius; float radius;
float arrowLength; float arrowLength;
float arrowWidth;
bool hasRoundedCorners; bool hasRoundedCorners;
bool hasArrow; bool hasArrow;
} labelInfo; } labelInfo;

View File

@@ -8,35 +8,31 @@ layout(set = 0, binding = 0) uniform NodeData
layout(set = 1, binding = 0) uniform CameraData layout(set = 1, binding = 0) uniform CameraData
{ {
mat4 viewProjection; mat4 viewProjection;
mat4 view; mat4 view;
mat4 projection; mat4 projection;
vec4 camPos; vec4 camPos;
float nearPlane; float nearPlane;
float farPlane; float farPlane;
float width; float width;
float height; float height;
float fov; float fov;
float aspect; float aspect;
float scaleFactor; float scaleFactor;
float pixelScaleFactor; float pixelScaleFactor;
} cam; } cam;
layout(set = 4, binding = 0) uniform BillboardData layout(set = 4, binding = 0) uniform LabelData
{ {
vec2 size; vec4 color;
bool isFixedSize; vec2 textSize;
} billboardInfo; vec2 bboxCenter;
float radius;
layout(set = 5, binding = 0) uniform LabelData float arrowLength;
{ float arrowWidth;
vec4 color; bool hasRoundedCorners;
vec2 textSize; bool hasArrow;
vec2 bboxCenter; bool isBillboardFixedSize;
float radius;
float arrowLength;
bool hasRoundedCorners;
bool hasArrow;
} labelInfo; } labelInfo;
layout(location = 0) out vec4 color; layout(location = 0) out vec4 color;
@@ -45,42 +41,41 @@ layout(location = 1) out vec2 textureCoordinates;
// Background plane positions are in clipped space // Background plane positions are in clipped space
const vec4 PLANE[4] = vec4[]( const vec4 PLANE[4] = vec4[](
vec4(-0.5, -0.5, 0, 1), vec4(0.5, -0.5, 0, 1), vec4(-0.5, 0.5, 0, 1), vec4(0.5, 0.5, 0, 1) vec4(-0.5, -0.5, 0, 1), vec4(0.5, -0.5, 0, 1), vec4(-0.5, 0.5, 0, 1), vec4(0.5, 0.5, 0, 1)
);
const vec2 TEX_COORDS[4] = vec2[](
vec2(0, 0), vec2(1, 0), vec2(0, 1), vec2(1, 1)
); );
void main() { const vec2 TEX_COORDS[4] = vec2[](vec2(0, 0), vec2(1, 0), vec2(0, 1), vec2(1, 1));
vec4 position = PLANE[gl_VertexIndex];
position.xy *= labelInfo.textSize;
position.xy += labelInfo.bboxCenter;
position.z = -0.001;
textureCoordinates = TEX_COORDS[gl_VertexIndex] * labelInfo.textSize;
if (!billboardInfo.isFixedSize) void main()
{ {
mat4 mv = cam.view * node.world; vec4 position = PLANE[gl_VertexIndex];
position.xy *= labelInfo.textSize;
position.xy += labelInfo.bboxCenter;
position.z = - 0.001;
textureCoordinates = TEX_COORDS[gl_VertexIndex] * labelInfo.textSize;
mv[0][0] = 1; if (!labelInfo.isBillboardFixedSize)
mv[0][1] = 0; {
mv[0][2] = 0; mat4 mv = cam.view * node.world;
mv[1][0] = 0; mv[0][0] = 1;
mv[1][1] = 1; mv[0][1] = 0;
mv[1][2] = 0; mv[0][2] = 0;
mv[2][0] = 0; mv[1][0] = 0;
mv[2][1] = 0; mv[1][1] = 1;
mv[2][2] = 1; mv[1][2] = 0;
gl_Position = cam.projection * mv * vec4(position.xyz, 1); mv[2][0] = 0;
} mv[2][1] = 0;
else mv[2][2] = 1;
{
vec4 billboardPos = vec4(0.5, 0.5, 0.5, 1); gl_Position = cam.projection * mv * vec4(position.xyz, 1);
vec4 viewPos = cam.view * node.world * billboardPos; }
float dist = -viewPos.z; else
gl_Position = cam.projection * (viewPos + vec4(position.xy*dist*0.2,0,0)); {
} vec4 billboardPos = vec4(0.5, 0.5, 0, 1);
vec4 viewPos = cam.view * node.world * billboardPos;
float dist = - viewPos.z;
gl_Position = cam.projection * (viewPos + vec4(position.xy *dist * 0.2, 0, 0));
}
} }

View File

@@ -26,14 +26,21 @@ layout(set = 1, binding = 0) uniform CameraData
layout(set = 4, binding = 0) uniform BillboardData layout(set = 4, binding = 0) uniform BillboardData
{ {
vec2 size; vec4 color;
bool isFixedSize; vec2 textSize;
vec2 bboxCenter;
float radius;
float arrowLength;
float arrowWidth;
bool hasRoundedCorners;
bool hasArrow;
bool isBillboardFixedSize;
} billboardInfo; } billboardInfo;
void main() void main()
{ {
vec4 pos = vec4(position[gl_VertexIndex], 0, 1); vec4 pos = vec4(position[gl_VertexIndex], 0, 1);
if (!billboardInfo.isFixedSize) if (!billboardInfo.isBillboardFixedSize)
{ {
mat4 mv = cam.view * node.world; mat4 mv = cam.view * node.world;

View File

@@ -21,20 +21,8 @@ namespace OpenVulkano::Vulkan
void EncodeBackground(LabelDrawable* labelDrawable, VulkanDrawContext* drawContext) void EncodeBackground(LabelDrawable* labelDrawable, VulkanDrawContext* drawContext)
{ {
if (labelDrawable->IsBillboard()) VulkanUniformBuffer* vkBuffer = labelDrawable->GetLabelBuffer()->GetRenderResource();
{ if (!vkBuffer) vkBuffer = drawContext->renderer->GetResourceManager().PrepareUniformBuffer(labelDrawable->GetLabelBuffer());
Scene::UniformBuffer* buffer = labelDrawable->GetBillboardBuffer();
VulkanUniformBuffer* vkBuffer = buffer->GetRenderResource();
if (!vkBuffer)
{
vkBuffer = drawContext->renderer->GetResourceManager().PrepareUniformBuffer(buffer);
}
vkBuffer->Record(drawContext);
}
Scene::UniformBuffer* labelBuffer = labelDrawable->GetLabelBuffer();
VulkanUniformBuffer* vkBuffer = labelBuffer->GetRenderResource();
if (!vkBuffer) vkBuffer = drawContext->renderer->GetResourceManager().PrepareUniformBuffer(labelBuffer);
vkBuffer->Record(drawContext); vkBuffer->Record(drawContext);
for (Node* node: labelDrawable->GetNodes()) for (Node* node: labelDrawable->GetNodes())
@@ -57,11 +45,8 @@ namespace OpenVulkano::Vulkan
if (labelDrawable->IsBillboard()) if (labelDrawable->IsBillboard())
{ {
VulkanUniformBuffer* vkBuffer = labelDrawable->GetBillboardBuffer()->GetRenderResource(); VulkanUniformBuffer* vkBuffer = labelDrawable->GetLabelBuffer()->GetRenderResource();
if (!vkBuffer) if (!vkBuffer) vkBuffer = drawContext->renderer->GetResourceManager().PrepareUniformBuffer(labelDrawable->GetLabelBuffer());
{
vkBuffer = drawContext->renderer->GetResourceManager().PrepareUniformBuffer(labelDrawable->GetBillboardBuffer());
}
vkBuffer->Record(drawContext); vkBuffer->Record(drawContext);
} }