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_drawablesPool.reserve(N);
BillboardControlBlock billboardSettings;
LabelDrawableSettings labelSettings;
for (int i = 0; i < N; i++)
@@ -69,7 +68,6 @@ namespace OpenVulkano
}
bool isBillboard = i % 2 == 0 ? 1 : 0;
LabelDrawable& label = m_drawablesPool.emplace_back(textDrawable.GetAtlasData(), labelSettings, isBillboard);
label.SetBillboardSettings(billboardSettings);
label.AddText(texts[i]);
if (i == 2)
{
@@ -80,7 +78,7 @@ namespace OpenVulkano
}
m_drawablesPool[i].SetIsHittable(true);
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]);
}

View File

@@ -20,21 +20,10 @@ namespace OpenVulkano::Scene
Shader MakeLabelBgShader(const bool billboard)
{
Shader backgroundShader;
if (!billboard)
{
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::VERTEX, billboard ? "Shader/labelBillboard" : "Shader/label");
backgroundShader.AddShaderProgram(ShaderProgramType::FRAGMENT, "Shader/label");
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.cullMode = CullMode::NONE;
return backgroundShader;
@@ -44,6 +33,7 @@ namespace OpenVulkano::Scene
{
Shader shader = TextDrawable::MakeDefaultShader(type);
shader.depthCompareOp = CompareOp::LESS_OR_EQUAL;
shader.EnableDepthBias();
if (billboard)
{
for (auto& program : shader.shaderPrograms)
@@ -55,10 +45,9 @@ namespace OpenVulkano::Scene
}
}
DescriptorSetLayoutBinding billboardUniformBinding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
billboardUniformBinding.stageFlags = ShaderProgramType::Type::VERTEX;
shader.AddDescriptorSetLayoutBinding(billboardUniformBinding, 4);
shader.depthBiasConstant = 0.01f;
}
shader.EnableDepthBias();
return shader;
}
@@ -123,11 +112,6 @@ namespace OpenVulkano::Scene
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
{
return ray.IntersectAABB(m_bbox);
@@ -135,16 +119,9 @@ namespace OpenVulkano::Scene
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.data = &m_labelData;
m_labelBuffer.setId = 5;
m_labelBuffer.setId = 4;
m_labelBuffer.binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
}
}

View File

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

View File

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

View File

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

View File

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

View File

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