Change shader handling
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cassert>
|
||||||
#include "Data/Containers/Array.hpp"
|
#include "Data/Containers/Array.hpp"
|
||||||
|
|
||||||
namespace openVulkanoCpp
|
namespace openVulkanoCpp
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public:
|
|||||||
{
|
{
|
||||||
Geometry* geo = new Geometry();
|
Geometry* geo = new Geometry();
|
||||||
geo->InitCube(std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, Vector4f((std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, 1));
|
geo->InitCube(std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, Vector4f((std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, 1));
|
||||||
drawablesPool[i].Init(geo, &mat);
|
drawablesPool[i].Init(&shader, geo, &mat);
|
||||||
}
|
}
|
||||||
nodesPool.resize(OBJECTS);
|
nodesPool.resize(OBJECTS);
|
||||||
for(int i = 0; i < OBJECTS; i++)
|
for(int i = 0; i < OBJECTS; i++)
|
||||||
@@ -74,8 +74,6 @@ public:
|
|||||||
nodesPool[i].SetMatrix(Utils::translate(glm::mat4x4(1), Vector3f((std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5)));
|
nodesPool[i].SetMatrix(Utils::translate(glm::mat4x4(1), Vector3f((std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5)));
|
||||||
}
|
}
|
||||||
|
|
||||||
scene.shader = &shader;
|
|
||||||
|
|
||||||
GetGraphicsAppManager()->GetRenderer()->SetScene(&scene);
|
GetGraphicsAppManager()->GetRenderer()->SetScene(&scene);
|
||||||
|
|
||||||
camController.Init(&cam);
|
camController.Init(&cam);
|
||||||
|
|||||||
@@ -8,25 +8,38 @@
|
|||||||
|
|
||||||
#include "Base/ICloseable.hpp"
|
#include "Base/ICloseable.hpp"
|
||||||
#include "DrawEncoder.hpp"
|
#include "DrawEncoder.hpp"
|
||||||
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace openVulkanoCpp::Scene
|
namespace openVulkanoCpp::Scene
|
||||||
{
|
{
|
||||||
class Node;
|
class Node;
|
||||||
class Scene;
|
class Scene;
|
||||||
|
class Shader;
|
||||||
|
|
||||||
class Drawable
|
enum class DrawPhase
|
||||||
|
{
|
||||||
|
BACKGROUND = 0, MAIN, TRANSPARENT, POST
|
||||||
|
};
|
||||||
|
|
||||||
|
class Drawable : public ICloseable
|
||||||
{
|
{
|
||||||
std::vector<Node*> m_nodes;
|
std::vector<Node*> m_nodes;
|
||||||
Scene* m_scene = nullptr;
|
Scene* m_scene = nullptr;
|
||||||
|
Shader* m_shader = nullptr;
|
||||||
const DrawEncoder m_encoder;
|
const DrawEncoder m_encoder;
|
||||||
|
const DrawPhase m_drawPhase;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Drawable(const DrawEncoder& encoder) : m_encoder(encoder) {}
|
explicit Drawable(const DrawEncoder& encoder,
|
||||||
|
const DrawPhase phase = DrawPhase::MAIN)
|
||||||
|
: m_encoder(encoder), m_drawPhase(phase) {}
|
||||||
|
|
||||||
~Drawable() { if (m_scene) Drawable::Close(); }
|
~Drawable() override { if (m_scene) Drawable::Close(); }
|
||||||
|
|
||||||
void Close();
|
void Close() override;
|
||||||
|
|
||||||
|
void SetShader(Shader* shader) { m_shader = shader; }
|
||||||
|
|
||||||
[[nodiscard]] virtual Drawable* Copy() = 0;
|
[[nodiscard]] virtual Drawable* Copy() = 0;
|
||||||
|
|
||||||
@@ -36,6 +49,10 @@ namespace openVulkanoCpp::Scene
|
|||||||
|
|
||||||
[[nodiscard]] const DrawEncoder& GetEncoder() const { return m_encoder; }
|
[[nodiscard]] const DrawEncoder& GetEncoder() const { return m_encoder; }
|
||||||
|
|
||||||
|
[[nodiscard]] DrawPhase GetDrawPhase() const { return m_drawPhase; }
|
||||||
|
|
||||||
|
[[nodiscard]] Shader* GetShader() const { return m_shader; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class Node;
|
friend class Node;
|
||||||
friend class Scene;
|
friend class Scene;
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ namespace openVulkanoCpp
|
|||||||
public:
|
public:
|
||||||
Node* root;
|
Node* root;
|
||||||
std::vector<Drawable*> shapeList;
|
std::vector<Drawable*> shapeList;
|
||||||
Shader* shader;
|
|
||||||
Camera* camera;
|
Camera* camera;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -125,8 +125,9 @@ namespace openVulkanoCpp::Scene
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Shader final : public virtual ICloseable
|
class Shader final : public ICloseable
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
std::vector<ShaderProgram> shaderPrograms{};
|
std::vector<ShaderProgram> shaderPrograms{};
|
||||||
std::vector<VertexInputDescription> vertexInputDescriptions{};
|
std::vector<VertexInputDescription> vertexInputDescriptions{};
|
||||||
Topology topology = Topology::TRIANGLE_LIST;
|
Topology topology = Topology::TRIANGLE_LIST;
|
||||||
@@ -162,7 +163,7 @@ namespace openVulkanoCpp::Scene
|
|||||||
Shader& AddVertexInputDescription(const VertexInputDescription& inputDescription, int bindingId = -1)
|
Shader& AddVertexInputDescription(const VertexInputDescription& inputDescription, int bindingId = -1)
|
||||||
{
|
{
|
||||||
if (renderShader) throw std::runtime_error("Shader already initialized!");
|
if (renderShader) throw std::runtime_error("Shader already initialized!");
|
||||||
if (bindingId < 0) bindingId = vertexInputDescriptions.size();
|
if (bindingId < 0) bindingId = static_cast<int>(vertexInputDescriptions.size());
|
||||||
if (bindingId > vertexInputDescriptions.size())
|
if (bindingId > vertexInputDescriptions.size())
|
||||||
{
|
{
|
||||||
vertexInputDescriptions.emplace_back(0, 0);
|
vertexInputDescriptions.emplace_back(0, 0);
|
||||||
|
|||||||
@@ -9,11 +9,12 @@
|
|||||||
|
|
||||||
namespace openVulkanoCpp::Scene
|
namespace openVulkanoCpp::Scene
|
||||||
{
|
{
|
||||||
void SimpleDrawable::Init(Geometry* mesh, Material* material)
|
void SimpleDrawable::Init(Shader* shader, Geometry* mesh, Material* material)
|
||||||
{
|
{
|
||||||
if (m_mesh || m_material) throw std::runtime_error("Drawable is already initialized.");
|
if (m_mesh || m_material) throw std::runtime_error("Drawable is already initialized.");
|
||||||
m_mesh = mesh;
|
m_mesh = mesh;
|
||||||
m_material = material;
|
m_material = material;
|
||||||
|
SetShader(shader);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SimpleDrawable::Init(SimpleDrawable* drawable)
|
void SimpleDrawable::Init(SimpleDrawable* drawable)
|
||||||
@@ -21,5 +22,6 @@ namespace openVulkanoCpp::Scene
|
|||||||
if (m_mesh || m_material) throw std::runtime_error("Drawable is already initialized.");
|
if (m_mesh || m_material) throw std::runtime_error("Drawable is already initialized.");
|
||||||
m_mesh = drawable->m_mesh;
|
m_mesh = drawable->m_mesh;
|
||||||
m_material = drawable->m_material;
|
m_material = drawable->m_material;
|
||||||
|
SetShader(drawable->GetShader());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,20 +19,23 @@ namespace openVulkanoCpp::Scene
|
|||||||
Material* m_material = nullptr;
|
Material* m_material = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SimpleDrawable() : Drawable(DrawEncoder::GetDrawEncoder<SimpleDrawable>()) {}
|
SimpleDrawable() : Drawable(DrawEncoder::GetDrawEncoder<SimpleDrawable>())
|
||||||
|
{}
|
||||||
|
|
||||||
explicit SimpleDrawable(const SimpleDrawable* toCopy)
|
explicit SimpleDrawable(const SimpleDrawable* toCopy)
|
||||||
: Drawable(DrawEncoder::GetDrawEncoder<SimpleDrawable>())
|
: Drawable(DrawEncoder::GetDrawEncoder<SimpleDrawable>())
|
||||||
, m_mesh(toCopy->m_mesh)
|
, m_mesh(toCopy->m_mesh)
|
||||||
, m_material(toCopy->m_material)
|
, m_material(toCopy->m_material)
|
||||||
{}
|
{
|
||||||
|
SetShader(toCopy->GetShader());
|
||||||
|
}
|
||||||
|
|
||||||
~SimpleDrawable()
|
~SimpleDrawable()
|
||||||
{
|
{
|
||||||
if (m_mesh) SimpleDrawable::Close();
|
if (m_mesh) SimpleDrawable::Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Init(Geometry* mesh, Material* material);
|
void Init(Shader* shader, Geometry* mesh, Material* material);
|
||||||
|
|
||||||
void Init(SimpleDrawable* drawable);
|
void Init(SimpleDrawable* drawable);
|
||||||
|
|
||||||
|
|||||||
@@ -59,8 +59,6 @@ namespace openVulkanoCpp::Vulkan
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
shader = resourceManager.CreateShader(scene->shader);
|
|
||||||
|
|
||||||
logger->info("Vulkan renderer initialized");
|
logger->info("Vulkan renderer initialized");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,15 +143,12 @@ namespace openVulkanoCpp::Vulkan
|
|||||||
|
|
||||||
void Renderer::RecordSecondaryBuffer(Data::ReadOnlyAtomicArrayQueue<Scene::Drawable*>* jobQueue, uint32_t poolId)
|
void Renderer::RecordSecondaryBuffer(Data::ReadOnlyAtomicArrayQueue<Scene::Drawable*>* jobQueue, uint32_t poolId)
|
||||||
{
|
{
|
||||||
Scene::Geometry* lastGeo = nullptr;
|
|
||||||
Scene::Node* lastNode = nullptr;
|
|
||||||
CommandHelper* cmdHelper = GetCommandData(poolId);
|
CommandHelper* cmdHelper = GetCommandData(poolId);
|
||||||
cmdHelper->Reset();
|
cmdHelper->Reset();
|
||||||
vk::CommandBufferInheritanceInfo inheritance = { context.swapChainRenderPass.renderPass, 0, context.swapChainRenderPass.GetFrameBuffer()->GetCurrentFrameBuffer() };
|
vk::CommandBufferInheritanceInfo inheritance = { context.swapChainRenderPass.renderPass, 0, context.swapChainRenderPass.GetFrameBuffer()->GetCurrentFrameBuffer() };
|
||||||
cmdHelper->cmdBuffer.begin(vk::CommandBufferBeginInfo{ vk::CommandBufferUsageFlagBits::eOneTimeSubmit | vk::CommandBufferUsageFlagBits::eRenderPassContinue, &inheritance });
|
cmdHelper->cmdBuffer.begin(vk::CommandBufferBeginInfo{ vk::CommandBufferUsageFlagBits::eOneTimeSubmit | vk::CommandBufferUsageFlagBits::eRenderPassContinue, &inheritance });
|
||||||
cmdHelper->cmdBuffer.pushConstants(context.pipeline.pipelineLayout, vk::ShaderStageFlagBits::eVertex, 0, 3 * sizeof(Math::Matrix4f) + sizeof(Math::Vector4f) + 4 * sizeof(float), &scene->GetCamera()->GetViewProjectionMatrix());
|
cmdHelper->cmdBuffer.pushConstants(context.pipeline.pipelineLayout, vk::ShaderStageFlagBits::eVertex, 0, 3 * sizeof(Math::Matrix4f) + sizeof(Math::Vector4f) + 4 * sizeof(float), &scene->GetCamera()->GetViewProjectionMatrix());
|
||||||
|
|
||||||
shader->Record(cmdHelper->cmdBuffer, currentImageId);
|
|
||||||
Scene::Drawable** drawablePointer;
|
Scene::Drawable** drawablePointer;
|
||||||
VulkanDrawContext drawContext { poolId, currentImageId, cmdHelper->cmdBuffer, this };
|
VulkanDrawContext drawContext { poolId, currentImageId, cmdHelper->cmdBuffer, this };
|
||||||
while((drawablePointer = jobQueue->Pop()) != nullptr)
|
while((drawablePointer = jobQueue->Pop()) != nullptr)
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ namespace openVulkanoCpp::Vulkan
|
|||||||
std::vector<std::vector<CommandHelper>> commands;
|
std::vector<std::vector<CommandHelper>> commands;
|
||||||
std::vector<std::vector<vk::CommandBuffer>> submitBuffers;
|
std::vector<std::vector<vk::CommandBuffer>> submitBuffers;
|
||||||
UiRenderer uiRenderer;
|
UiRenderer uiRenderer;
|
||||||
VulkanShader* shader;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Renderer() = default;
|
Renderer() = default;
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ namespace openVulkanoCpp::Vulkan
|
|||||||
{
|
{
|
||||||
void EncodeSimpleDrawable(Drawable* instance, Vulkan::VulkanDrawContext* drawContext)
|
void EncodeSimpleDrawable(Drawable* instance, Vulkan::VulkanDrawContext* drawContext)
|
||||||
{
|
{
|
||||||
|
drawContext->EncodeShader(instance->GetShader());
|
||||||
Geometry* mesh = dynamic_cast<SimpleDrawable*>(instance)->GetMesh();
|
Geometry* mesh = dynamic_cast<SimpleDrawable*>(instance)->GetMesh();
|
||||||
VulkanGeometry* renderGeo = dynamic_cast<VulkanGeometry*>(mesh->renderGeo);
|
VulkanGeometry* renderGeo = dynamic_cast<VulkanGeometry*>(mesh->renderGeo);
|
||||||
if (!mesh->renderGeo) renderGeo = drawContext->renderer->GetResourceManager().PrepareGeometry(mesh);
|
if (!mesh->renderGeo) renderGeo = drawContext->renderer->GetResourceManager().PrepareGeometry(mesh);
|
||||||
|
|||||||
@@ -23,15 +23,15 @@ namespace openVulkanoCpp
|
|||||||
class Context;
|
class Context;
|
||||||
class IShaderOwner;
|
class IShaderOwner;
|
||||||
|
|
||||||
struct VulkanShader final : virtual public ICloseable, virtual public IRecordable
|
struct VulkanShader final : public ICloseable, public IRecordable
|
||||||
{
|
{
|
||||||
Scene::Shader* shader = nullptr;
|
Scene::Shader* shader = nullptr;
|
||||||
vk::Device device;
|
vk::Device device;
|
||||||
std::vector<vk::ShaderModule> shaderModules; // TODO manage live time somewhere else to allow sharing of shader programs
|
std::vector<vk::ShaderModule> shaderModules; // TODO manage live time somewhere else to allow sharing of shader programs
|
||||||
std::vector<vk::PipelineShaderStageCreateInfo> shaderStageCreateInfo;
|
std::vector<vk::PipelineShaderStageCreateInfo> shaderStageCreateInfo;
|
||||||
vk::Pipeline pipeline; // TODO pipeline and shader config should be split
|
vk::Pipeline pipeline; // TODO pipeline and shader config should be split
|
||||||
IShaderOwner* owner;
|
IShaderOwner* owner = nullptr;
|
||||||
Context* context;
|
Context* context = nullptr;
|
||||||
|
|
||||||
VulkanShader() = default;
|
VulkanShader() = default;
|
||||||
|
|
||||||
|
|||||||
24
openVulkanoCpp/Vulkan/VulkanDrawContext.cpp
Normal file
24
openVulkanoCpp/Vulkan/VulkanDrawContext.cpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* 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 "VulkanDrawContext.hpp"
|
||||||
|
#include "Scene/Shader.hpp"
|
||||||
|
#include "Scene/VulkanShader.hpp"
|
||||||
|
|
||||||
|
namespace openVulkanoCpp::Vulkan
|
||||||
|
{
|
||||||
|
void VulkanDrawContext::EncodeShader(Scene::Shader* shader)
|
||||||
|
{
|
||||||
|
VulkanShader* vkShader = static_cast<VulkanShader*>(shader->renderShader);
|
||||||
|
if (!vkShader)
|
||||||
|
{
|
||||||
|
vkShader = renderer->GetResourceManager().CreateShader(shader);
|
||||||
|
}
|
||||||
|
else if (m_lastShader == vkShader) return; // Skip it if shader is already bound
|
||||||
|
vkShader->Record(commandBuffer, currentImageId);
|
||||||
|
m_lastShader = vkShader;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,10 +10,18 @@ namespace openVulkanoCpp::Vulkan
|
|||||||
{
|
{
|
||||||
class VulkanDrawContext
|
class VulkanDrawContext
|
||||||
{
|
{
|
||||||
|
VulkanShader* m_lastShader = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
size_t encoderThreadId;
|
size_t encoderThreadId;
|
||||||
size_t currentImageId;
|
size_t currentImageId;
|
||||||
vk::CommandBuffer& commandBuffer;
|
vk::CommandBuffer& commandBuffer;
|
||||||
Renderer* renderer;
|
Renderer* renderer;
|
||||||
|
|
||||||
|
VulkanDrawContext(size_t encThreadId, size_t currentImgId, vk::CommandBuffer& buffer, Renderer* render)
|
||||||
|
: encoderThreadId(encThreadId), currentImageId(currentImgId), commandBuffer(buffer), renderer(render)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void EncodeShader(Scene::Shader* shader);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user