aboutsummaryrefslogtreecommitdiff
path: root/src/rendering
diff options
context:
space:
mode:
Diffstat (limited to 'src/rendering')
-rw-r--r--src/rendering/renderer_backend.h73
-rw-r--r--src/rendering/rhi/rhi.h131
2 files changed, 204 insertions, 0 deletions
diff --git a/src/rendering/renderer_backend.h b/src/rendering/renderer_backend.h
new file mode 100644
index 0000000..6a4f1af
--- /dev/null
+++ b/src/rendering/renderer_backend.h
@@ -0,0 +1,73 @@
+#pragma once
+
+#include <glm/glm.hpp>
+#include <functional>
+#include <string>
+#include <vector>
+
+namespace Donut
+{
+ // A placeable/editable sphere in the world-builder scene view. Backend-
+ // agnostic: the app owns the list, every backend renders it the same way.
+ struct SceneObject
+ {
+ glm::vec3 position = { 0.0f, 2.0f, 0.0f };
+ float radius = 2.0f;
+ glm::vec3 color = { 0.85f, 0.35f, 0.2f };
+ };
+
+ // Live-tunable black-hole / accretion-disk parameters. Radii are in
+ // Schwarzschild radii; the inner edge is clamped to the ISCO (3 r_s).
+ struct BlackHoleParams
+ {
+ float disk_inner_rs = 3.0f; // inner edge (x r_s)
+ float disk_outer_rs = 12.0f; // outer edge (x r_s)
+ float temperature = 4800.0f; // Kelvin at the flux peak (disk colour)
+ float brightness = 0.9f; // disk exposure
+ float turbulence = 0.4f; // 0 = smooth physical disk
+ int quality_steps = 15000; // geodesic integration steps
+ float fov_degrees = 60.0f; // camera field of view
+ };
+
+ // The swappable high-level renderer. Each graphics backend (OpenGL, Vulkan,
+ // and later Metal / D3D12) implements this one interface; the Application,
+ // states and UI drive it identically, so the app behaves the same no matter
+ // which backend is active. Backends own their own device/swapchain/pipelines
+ // and translate these high-level calls into their native API.
+ class RendererBackend
+ {
+ public:
+ virtual ~RendererBackend() = default;
+
+ // glfwWindow is an opaque GLFW handle (created NO_API for Vulkan/Metal,
+ // with a GL context for OpenGL). Returns false on failure.
+ virtual auto init(void* glfwWindow, int width, int height) -> bool = 0;
+
+ // Creates the ImGui context + this backend's ImGui platform/render impl.
+ virtual auto init_ui() -> bool = 0;
+ virtual auto shutdown() -> void = 0;
+ virtual auto resize(int width, int height) -> void = 0;
+
+ // Renders one frame: draws the current view (black hole or scene) from
+ // the shared state, then invokes build_ui to draw the ImGui panel on top.
+ virtual auto draw_frame(const glm::vec4& clear_color,
+ const std::function<void()>& build_ui) -> void = 0;
+
+ // --- shared view / camera state ------------------------------------
+ virtual auto set_scene_mode(bool enabled) -> void = 0; // black hole vs scene view
+ virtual auto is_scene_mode() const -> bool = 0;
+ virtual auto set_free_fly(bool enabled) -> void = 0; // free-fly vs orbital camera
+ virtual auto is_free_fly() const -> bool = 0;
+ virtual auto reset_camera() -> void = 0;
+
+ // --- shared content ------------------------------------------------
+ virtual auto set_hdri(const std::string& path) -> void = 0;
+ virtual auto current_hdri() const -> const std::string& = 0;
+ virtual auto scene_objects() -> std::vector<SceneObject>& = 0;
+ virtual auto set_selected_object(int index) -> void = 0;
+ virtual auto black_hole_params() -> BlackHoleParams& = 0;
+
+ // --- introspection for the UI --------------------------------------
+ virtual auto device_name() const -> const std::string& = 0;
+ };
+}
diff --git a/src/rendering/rhi/rhi.h b/src/rendering/rhi/rhi.h
new file mode 100644
index 0000000..f64252c
--- /dev/null
+++ b/src/rendering/rhi/rhi.h
@@ -0,0 +1,131 @@
+#pragma once
+
+// Donut RHI (Render Hardware Interface): a small, portable GPU abstraction that
+// OpenGL, Vulkan (and later Metal / D3D12) implement behind ONE interface, so the
+// app's rendering — the black hole, the scene — is written ONCE on top and runs
+// on any backend. The shape is modelled on the explicit APIs (baked pipelines,
+// recorded command lists, explicit render targets); OpenGL emulates that, which
+// is easy, whereas the reverse (making Vulkan speak GL's immediate mode) is not.
+
+#include "core/memory.h"
+#include <glm/glm.hpp>
+#include <cstdint>
+#include <string>
+#include <vector>
+#include <functional>
+
+namespace Donut::rhi
+{
+ enum class Format
+ {
+ RGBA8, // 8-bit unorm colour (swapchain / LDR targets)
+ RGBA16F, // half-float colour (HDR / cubemap)
+ D32, // 32-bit depth
+ };
+
+ enum class BufferType { Vertex, Index, Uniform };
+ enum class Topology { Triangles, Lines };
+ enum class CullMode { None, Back, Front };
+ enum class BlendMode { Opaque, AlphaBlend };
+ enum class CompareOp { Always, Less, LessEqual };
+ enum class Filter { Nearest, Linear };
+
+ // One vertex attribute; offsets/stride are in bytes. `components` is 1..4 floats.
+ struct VertexAttribute { uint32_t location; uint32_t components; uint32_t offset; };
+ struct VertexLayout { uint32_t stride = 0; std::vector<VertexAttribute> attributes; };
+
+ // A shader resource slot the pipeline exposes. `binding` is the set-0 binding
+ // index shared by the GLSL/SPIR-V (the app binds resources to these before a draw).
+ enum class ResourceKind { UniformBuffer, Texture };
+ struct ResourceSlot { ResourceKind kind; uint32_t binding; };
+
+ // --- opaque GPU resources (backends subclass) --------------------------
+ class Buffer { public: virtual ~Buffer() = default; virtual auto update(const void* data, size_t size) -> void = 0; };
+ class Texture { public: virtual ~Texture() = default; };
+ class Pipeline{ public: virtual ~Pipeline() = default; };
+
+ // An off-screen target (colour, optional depth). The swapchain is the implicit
+ // default target, addressed by passing nullptr to begin_render_pass.
+ class RenderTarget
+ {
+ public:
+ virtual ~RenderTarget() = default;
+ virtual auto width() const -> int = 0;
+ virtual auto height() const -> int = 0;
+ virtual auto color_texture() -> Texture* = 0; // to sample this target's colour
+ };
+
+ struct PipelineDesc
+ {
+ std::string shader; // base name; backend loads .spv or .glsl
+ VertexLayout vertex_layout;
+ std::vector<ResourceSlot> resources; // UBO/texture slots the shader reads
+ Topology topology = Topology::Triangles;
+ CullMode cull = CullMode::None;
+ BlendMode blend = BlendMode::Opaque;
+ bool depth_test = false;
+ bool depth_write = false;
+ CompareOp depth_op = CompareOp::Less;
+ Format color_format = Format::RGBA8; // format of the target it renders into
+ bool has_depth = false; // target has a depth attachment
+ };
+
+ // Records draws for one frame. Obtained from Device::begin_frame (targets the
+ // swapchain) or created transiently for off-screen passes via the Device.
+ class CommandList
+ {
+ public:
+ virtual ~CommandList() = default;
+
+ // target == nullptr renders to the swapchain; otherwise to the RenderTarget.
+ virtual auto begin_render_pass(RenderTarget* target, const glm::vec4& clear) -> void = 0;
+ virtual auto end_render_pass() -> void = 0;
+
+ virtual auto bind_pipeline(Pipeline* pipeline) -> void = 0;
+ virtual auto set_viewport(int x, int y, int w, int h, bool flip_y = false) -> void = 0;
+
+ virtual auto bind_uniform(uint32_t binding, Buffer* ubo) -> void = 0;
+ virtual auto bind_texture(uint32_t binding, Texture* texture) -> void = 0;
+
+ virtual auto bind_vertex_buffer(Buffer* vb) -> void = 0;
+ virtual auto bind_index_buffer(Buffer* ib) -> void = 0; // 32-bit indices
+
+ virtual auto draw(uint32_t vertex_count) -> void = 0;
+ virtual auto draw_indexed(uint32_t index_count) -> void = 0;
+ };
+
+ // The backend root: owns the device/swapchain and creates every resource.
+ class Device
+ {
+ public:
+ virtual ~Device() = default;
+
+ // glfwWindow: opaque GLFW handle (GL context for OpenGL, NO_API otherwise).
+ virtual auto init(void* glfwWindow, int width, int height) -> bool = 0;
+ virtual auto shutdown() -> void = 0;
+ virtual auto resize(int width, int height) -> void = 0;
+ virtual auto wait_idle() -> void = 0;
+
+ virtual auto create_buffer(BufferType type, size_t size, const void* data = nullptr) -> Ref<Buffer> = 0;
+ virtual auto create_texture(int width, int height, Format format, Filter filter, const void* data = nullptr) -> Ref<Texture> = 0;
+ virtual auto create_cubemap_from_hdri(const std::string& equirect_path) -> Ref<Texture> = 0;
+ virtual auto create_render_target(int width, int height, Format color, bool with_depth,
+ Filter sample_filter = Filter::Linear, int mip_levels = 1) -> Ref<RenderTarget> = 0;
+ virtual auto create_pipeline(const PipelineDesc& desc) -> Ref<Pipeline> = 0;
+
+ // Frame loop: begin_frame returns the swapchain command list (or nullptr if
+ // the frame is skipped, e.g. minimised); record into it, then end_frame
+ // submits + presents. record_offscreen runs a self-contained pass whose
+ // command list targets a RenderTarget (used before the swapchain frame).
+ virtual auto begin_frame(const glm::vec4& clear) -> CommandList* = 0;
+ virtual auto end_frame() -> void = 0;
+ virtual auto record_offscreen(const std::function<void(CommandList&)>& record) -> void = 0;
+
+ // ImGui lives above the RHI but its platform/render backend is per-device.
+ virtual auto init_imgui() -> void = 0;
+ virtual auto imgui_new_frame() -> void = 0;
+ virtual auto imgui_render(CommandList& swapchain_cmds) -> void = 0;
+
+ virtual auto device_name() const -> const std::string& = 0;
+ };
+}