Skip to content

Commit

Permalink
what a BIG commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Sep 9, 2024
1 parent 096b10e commit ac9a01e
Show file tree
Hide file tree
Showing 79 changed files with 7,275 additions and 184 deletions.
11 changes: 11 additions & 0 deletions Runtime/Includes/Core/AntiX11.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// no header guards

#ifndef _X11_XLIB_H_
#error This header should only be included after including X11/Xlib.h directly or indirectly in a .cpp
#endif

#undef Always
#undef Bool
#undef False
#undef None
#undef True
8 changes: 3 additions & 5 deletions Runtime/Includes/Core/Enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ namespace kbh

enum class Event
{
SceneHasChangedEventCode = 55,
ResizeEventCode = 56,
FrameBeginEventCode = 57,
FatalErrorEventCode = 168,
QuitEventCode = 168,
FrameBeginEventCode = 0,
FatalErrorEventCode,
ResizeEventCode,

EndEnum
};
Expand Down
40 changes: 40 additions & 0 deletions Runtime/Includes/Graphics/Actor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef KANEL_3D_RENDERER_ACTOR
#define KANEL_3D_RENDERER_ACTOR

#include <Maths/Vec3.h>
#include <Maths/Vec4.h>
#include <Maths/Quaternions.h>
#include <Graphics/Model.h>

namespace kbh
{
class Actor
{
public:
Actor() = default;
Actor(Model model) : m_model(std::move(model)) {}

inline void SetColor(Vec4f color) noexcept { m_color = color; }
inline void SetPosition(Vec3f position) noexcept { m_position = position; }
inline void SetScale(Vec3f scale) noexcept { m_scale = scale; }
inline void SetOrientation(Quatf orientation) noexcept { m_orientation = orientation; }

[[nodiscard]] inline const Vec4f& GetColor() const noexcept { return m_color; }
[[nodiscard]] inline const Vec3f& GetPosition() const noexcept { return m_position; }
[[nodiscard]] inline const Vec3f& GetScale() const noexcept { return m_scale; }
[[nodiscard]] inline const Quatf& GetOrientation() const noexcept { return m_orientation; }
[[nodiscard]] inline const Model& GetModel() const noexcept { return m_model; }
[[nodiscard]] inline Model& GetModelRef() noexcept { return m_model; }

~Actor() = default;

private:
Model m_model;
Quatf m_orientation = Quatf::Identity();
Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f };
Vec3f m_position = Vec3f{ 0.0f, 0.0f, 0.0f };
Vec3f m_scale = Vec3f{ 1.0f, 1.0f, 1.0f };
};
}

#endif
30 changes: 30 additions & 0 deletions Runtime/Includes/Graphics/Cameras/Base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef KANEL_3D_CAMERAS_BASE
#define KANEL_3D_CAMERAS_BASE

#include <Maths/Mat4.h>

namespace kbh
{
class BaseCamera
{
public:
BaseCamera() = default;

virtual void Update(float aspect, float timestep) {};

[[nodiscard]] inline const Mat4f& GetView() const noexcept { return m_view; }
[[nodiscard]] inline const Mat4f& GetProj() const noexcept { return m_proj; }

[[nodiscard]] virtual const Vec3f& GetPosition() const noexcept = 0;

virtual constexpr std::string GetCameraType() = 0;

virtual ~BaseCamera() = default;

protected:
Mat4f m_view;
Mat4f m_proj;
};
}

#endif
67 changes: 67 additions & 0 deletions Runtime/Includes/Graphics/Material.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef KANEL_3D_RENDERER_MATERIAL
#define KANEL_3D_RENDERER_MATERIAL

#include <memory>

#include <Core/EventBus.h>
#include <Renderer/Image.h>
#include <Renderer/Buffer.h>
#include <Renderer/Descriptor.h>

namespace kbh
{
struct MaterialTextures
{
std::shared_ptr<Texture> albedo;
};

class Material
{
friend class Model;

public:
Material() { SetupEventListener(); }
Material(const MaterialTextures& textures) : m_textures(textures) { SetupEventListener(); }

~Material() { m_data_buffer.Destroy(); }

private:
[[nodiscard]] inline bool IsSetInit() const noexcept { return m_set.IsInit(); }
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t frame_index) const noexcept { return m_set.GetSet(frame_index); }

inline void SetupEventListener()
{
std::function<void(const EventBase&)> functor = [this](const EventBase& event)
{
if(event.What() == Event::FrameBeginEventCode)
m_have_been_updated_this_frame = false;
};
EventBus::RegisterListener({ functor, "__Material" + std::to_string(reinterpret_cast<std::uintptr_t>(this)) });
}

inline void UpdateDescriptorSet(const DescriptorSet& set)
{
m_set = set.Duplicate();
}

inline void Bind(std::size_t frame_index, VkCommandBuffer cmd)
{
if(m_have_been_updated_this_frame)
return;
m_set.SetImage(frame_index, 0, *m_textures.albedo);
m_set.SetUniformBuffer(frame_index, 1, m_data_buffer.Get(frame_index));
m_set.Update(frame_index, cmd);

m_have_been_updated_this_frame = true;
}

private:
UniformBuffer m_data_buffer;
MaterialTextures m_textures;
DescriptorSet m_set;
bool m_have_been_updated_this_frame = false;
};
}

#endif

57 changes: 57 additions & 0 deletions Runtime/Includes/Graphics/Mesh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef KANEL_3D_RENDERER_MESH
#define KANEL_3D_RENDERER_MESH

#include <vector>
#include <cstdint>
#include <cstring>

#include <Renderer/Vertex.h>
#include <Renderer/Buffer.h>
#include <Utils/Buffer.h>

namespace kbh
{
class Mesh
{
public:
struct SubMesh
{
VertexBuffer vbo;
IndexBuffer ibo;
std::size_t triangle_count = 0;

inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices)
{
CPUBuffer vb(vertices.size() * sizeof(Vertex));
std::memcpy(vb.GetData(), vertices.data(), vb.GetSize());
vbo.Init(vb.GetSize());
vbo.SetData(std::move(vb));

CPUBuffer ib(indices.size() * sizeof(std::uint32_t));
std::memcpy(ib.GetData(), indices.data(), ib.GetSize());
ibo.Init(ib.GetSize());
ibo.SetData(std::move(ib));

triangle_count = vertices.size() / 3;
}
};

public:
Mesh() = default;

void Draw(VkCommandBuffer cmd, std::size_t& drawcalls, std::size_t& polygondrawn) const noexcept;
void Draw(VkCommandBuffer cmd, std::size_t& drawcalls, std::size_t& polygondrawn, std::size_t submesh_index) const noexcept;

inline std::size_t GetSubMeshCount() const { return m_sub_meshes.size(); }

inline void AddSubMesh(SubMesh mesh) { m_sub_meshes.emplace_back(std::move(mesh)); }
[[nodiscard]] inline SubMesh& GetSubMesh(std::size_t index) { return m_sub_meshes.at(index); }

~Mesh();

private:
std::vector<SubMesh> m_sub_meshes;
};
}

#endif
40 changes: 40 additions & 0 deletions Runtime/Includes/Graphics/Model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef KANEL_3D_RENDERER_MODEL
#define KANEL_3D_RENDERER_MODEL

#include <memory>
#include <filesystem>

#include <kvf.h>

#include <Maths/Vec3.h>
#include <Graphics/Mesh.h>
#include <Graphics/Material.h>

namespace kbh
{
// Only static meshes for now
class Model
{
public:
Model() = default;
Model(std::shared_ptr<Mesh> mesh);

inline void SetMaterial(std::shared_ptr<Material> material, std::size_t mesh_index) { m_materials[mesh_index] = material; }
inline std::size_t GetSubMeshCount() const { return p_mesh->GetSubMeshCount(); }

[[nodiscard]] inline std::shared_ptr<Material> GetMaterial(std::size_t mesh_index) { return m_materials[mesh_index]; }
[[nodiscard]] inline std::vector<std::shared_ptr<Material>>& GetAllMaterials() { return m_materials; }
[[nodiscard]] inline Vec3f GetCenter() const noexcept { return m_center; }

void Draw(VkCommandBuffer cmd, const DescriptorSet& matrices_set, const class GraphicPipeline& pipeline, DescriptorSet& set, std::size_t& drawcalls, std::size_t& polygondrawn, std::size_t frame_index) const;

~Model() = default;

private:
Vec3f m_center = { 0.0f, 0.0f, 0.0f };
std::vector<std::shared_ptr<Material>> m_materials;
std::shared_ptr<Mesh> p_mesh;
};
}

#endif
37 changes: 37 additions & 0 deletions Runtime/Includes/Graphics/NzslLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef KANEL_3D_NZSL_LOADER
#define KANEL_3D_NZSL_LOADER

#include <vector>
#include <cstdint>
#include <string_view>
#include <filesystem>

#undef Always
#undef Bool
#undef False
#undef None
#undef True

#include <NZSL/Parser.hpp>
#include <NZSL/SpirvWriter.hpp>
#include <NZSL/FilesystemModuleResolver.hpp>

namespace kbh
{
class NzslLoader
{
public:
NzslLoader();

std::vector<std::uint32_t> LoadShader(std::string_view shader);
std::vector<std::uint32_t> LoadShader(const std::filesystem::path& path);

~NzslLoader() = default;

private:
std::shared_ptr<nzsl::FilesystemModuleResolver> m_resolver;
nzsl::SpirvWriter m_writer;
};
}

#endif
66 changes: 66 additions & 0 deletions Runtime/Includes/Graphics/Scene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef KANEL_3D_SCENE
#define KANEL_3D_SCENE

#include <memory>
#include <string_view>

#include <Utils/NonOwningPtr.h>

#include <Graphics/Actor.h>
#include <Renderer/Buffer.h>
#include <Renderer/Descriptor.h>
#include <Renderer/RenderCore.h>
#include <Graphics/Cameras/Base.h>
#include <Renderer/Pipelines/Shader.h>
#include <Renderer/Pipelines/Graphics.h>

namespace kbh
{
struct SceneDescriptor
{
std::shared_ptr<Shader> fragment_shader;
std::shared_ptr<BaseCamera> camera;
bool render_3D_enabled = true;
};

class Scene
{
public:
struct ForwardData
{
std::shared_ptr<DescriptorSet> matrices_set;
std::shared_ptr<DescriptorSet> albedo_set;
std::shared_ptr<UniformBuffer> matrices_buffer;
bool wireframe = false;
};

public:
Scene(SceneDescriptor desc);

void Init(NonOwningPtr<class Renderer> renderer);
void Update(float delta, float aspect);
void Destroy();

Actor& CreateActor(Model model) noexcept;
Actor& CreateActor(std::string_view name, Model model);

[[nodiscard]] inline ForwardData& GetForwardData() noexcept { return m_forward; }
[[nodiscard]] inline const std::vector<std::shared_ptr<Actor>>& GetActors() const noexcept { return m_actors; }
[[nodiscard]] inline GraphicPipeline& GetPipeline() noexcept { return m_pipeline; }
[[nodiscard]] inline std::shared_ptr<BaseCamera> GetCamera() const { return m_descriptor.camera; }
[[nodiscard]] inline DepthImage& GetDepth() noexcept { return m_depth; }
[[nodiscard]] inline std::shared_ptr<Shader> GetFragmentShader() const { return m_descriptor.fragment_shader; }
[[nodiscard]] inline const SceneDescriptor& GetDescription() const noexcept { return m_descriptor; }

~Scene() = default;

private:
GraphicPipeline m_pipeline;
ForwardData m_forward;
DepthImage m_depth;
SceneDescriptor m_descriptor;
std::vector<std::shared_ptr<Actor>> m_actors;
};
}

#endif
Loading

0 comments on commit ac9a01e

Please sign in to comment.