Change shader handling

This commit is contained in:
2023-08-04 20:20:20 +02:00
parent 4dac821abb
commit 836e9dce42
13 changed files with 71 additions and 23 deletions

View File

@@ -8,25 +8,38 @@
#include "Base/ICloseable.hpp"
#include "DrawEncoder.hpp"
#include <memory>
#include <vector>
namespace openVulkanoCpp::Scene
{
class Node;
class Scene;
class Shader;
class Drawable
enum class DrawPhase
{
BACKGROUND = 0, MAIN, TRANSPARENT, POST
};
class Drawable : public ICloseable
{
std::vector<Node*> m_nodes;
Scene* m_scene = nullptr;
Shader* m_shader = nullptr;
const DrawEncoder m_encoder;
const DrawPhase m_drawPhase;
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;
@@ -36,6 +49,10 @@ namespace openVulkanoCpp::Scene
[[nodiscard]] const DrawEncoder& GetEncoder() const { return m_encoder; }
[[nodiscard]] DrawPhase GetDrawPhase() const { return m_drawPhase; }
[[nodiscard]] Shader* GetShader() const { return m_shader; }
private:
friend class Node;
friend class Scene;

View File

@@ -22,7 +22,6 @@ namespace openVulkanoCpp
public:
Node* root;
std::vector<Drawable*> shapeList;
Shader* shader;
Camera* camera;
public:

View File

@@ -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<VertexInputDescription> vertexInputDescriptions{};
Topology topology = Topology::TRIANGLE_LIST;
@@ -162,7 +163,7 @@ namespace openVulkanoCpp::Scene
Shader& AddVertexInputDescription(const VertexInputDescription& inputDescription, int bindingId = -1)
{
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())
{
vertexInputDescriptions.emplace_back(0, 0);

View File

@@ -9,11 +9,12 @@
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.");
m_mesh = mesh;
m_material = material;
SetShader(shader);
}
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.");
m_mesh = drawable->m_mesh;
m_material = drawable->m_material;
SetShader(drawable->GetShader());
}
}

View File

@@ -19,20 +19,23 @@ namespace openVulkanoCpp::Scene
Material* m_material = nullptr;
public:
SimpleDrawable() : Drawable(DrawEncoder::GetDrawEncoder<SimpleDrawable>()) {}
SimpleDrawable() : Drawable(DrawEncoder::GetDrawEncoder<SimpleDrawable>())
{}
explicit SimpleDrawable(const SimpleDrawable* toCopy)
: Drawable(DrawEncoder::GetDrawEncoder<SimpleDrawable>())
, m_mesh(toCopy->m_mesh)
, m_material(toCopy->m_material)
{}
{
SetShader(toCopy->GetShader());
}
~SimpleDrawable()
{
if (m_mesh) SimpleDrawable::Close();
}
void Init(Geometry* mesh, Material* material);
void Init(Shader* shader, Geometry* mesh, Material* material);
void Init(SimpleDrawable* drawable);