Skip to content

Add Rendergraph module#41

Draft
jayrulez wants to merge 16 commits into
Redot-Engine:masterfrom
jayrulez:rendergraph
Draft

Add Rendergraph module#41
jayrulez wants to merge 16 commits into
Redot-Engine:masterfrom
jayrulez:rendergraph

Conversation

@jayrulez

@jayrulez jayrulez commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

A render graph over the RHI. Passes declare resource reads/writes; the graph builds a dependency DAG, culls dead passes, topologically sorts, aliases transient textures from a pool, and inserts barriers automatically (via a per-subresource state tracker).
compile() runs without an encoder so the graph is testable headless; execute() records into one. Also includes a validator, DOT debug export, and a per-pass GPU/CPU profiler.

Status for errors; std::unique_ptr / std::function / std::unordered_map for owned resources, callbacks, and maps. Three small local stand-ins (mapFind, appendFormat, getTicks) carry NOTE comments to replace once core has the equivalents.

@coderabbitai

coderabbitai Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8d01b012-f0ca-4341-a6f1-b63e355156a3

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@jayrulez jayrulez force-pushed the rendergraph branch 4 times, most recently from 5d6d8fc to 8cffff8 Compare July 11, 2026 17:02
jayrulez added 16 commits July 13, 2026 17:02
Fold both backends into a single Shell library (folder-separated only:
Desktop/ = SDL3, Null/ = headless), so Shell is the only shell library
and SDL3 is a fully internal PRIVATE dependency.

- The two per-backend factory units collapse into one Shell.cpp bridge
  that dispatches createShell()/destroyShell() to the backend chosen at
  compile time via DRACO_SHELL_SDL3.
- Both backend modules (shell.desktop, shell.null) compile into the one
  library, so both stay independently testable in every build.
- Runtime links only Shell; the sample links only Runtime. SDL propagates
  through Shell's PRIVATE link, so nothing downstream links SDL directly.
  The SDL whole-archive force-load stays on the executable (not Shell) so
  it does not force a full SDL copy into the intermediate Runtime library.

Both shell tests pass and RenderSample builds and runs.
Introduce core.math.matrix4 (4x4 row-major, row-vector convention: identity/
translation/scale/rotation, perspective/ortho/lookAt RH, multiply, transpose,
determinant, transformPoint/Direction/Point2D, and a scale-aware tryInverse/
inverse) and core.math.quaternion (fromAxisAngle, Hamilton product, conjugate/
dot/inverse/normalize/rotateVector/slerp, rotationMatrix), with the Matrix4
inverse bodies kept in Matrix4.cpp so the interface stays light.

Rewrite Transform to position / rotation (Quaternion) / scale with an inline
toMatrix() returning Matrix4 and a lerp helper, removing the last bx dependency
from Core. Update its two callers (Renderer, RenderSample) to the new API.

Add the math helpers these types need (sin/cos/tan/acos/sqrt, scalar and vector
nearlyEqual/nearlyZero) and unit tests for Matrix4/Quaternion/Transform.

Review follow-ups: expose singular inverses through tryInverse with a Hadamard
row-norm threshold instead of a silent identity; guard a zero-length axis in
fromAxisAngle; treat antipodal quaternions as equal; make the near-equal helpers
constexpr and drop the redundant approx_eq; use the multidimensional subscript
operator on Matrix4.
Drop the bgfx rendering stack entirely: delete the Rendering and Scene modules,
the bx/bimg/bgfx submodules and their ThirdParty build tooling, and the shader
compilation (cmake/Shaders.cmake). Rewire Runtime, Engine, and the draconic
module so the engine builds on Core + Shell + SDL3 only.

RenderSample becomes a shell-only placeholder that opens a window and pumps
events until it is closed.
Introduce the rhi module: a backend-agnostic GPU render-hardware-interface, as the primary module 'rhi' with partitions for enums, texture formats, core types, forward declarations, resource/descriptor structs, resources, command recording, extensions (mesh shader / ray tracing), queue, swap chain, and the Backend/Adapter/Device factory. Interface only - concrete backends (Null, Validation, Vulkan, DX12) land as separate modules.

Add a small Status/ErrorCode facility to Core (core.status, re-exported via core.defs) for the interface's status-code returns; value-or-error paths use std::expected.

Uses this engine's conventions throughout: draco::rhi namespace, camelCase methods/functions/constants (types and enum values stay PascalCase), and std container types (std::vector/span/optional/u8string). Wired via add_modules_library(Rendering/RHI) into Runtime.
Headless no-op RHI backend implementing every rhi interface with stubs: a NullDevice that creates/destroys resources, a mappable NullBuffer backed by host memory, no-op command encoders, and a createNullBackend() entry point. Unsupported extensions (mesh shaders, ray tracing) degrade to NotSupported rather than crash. Intended for headless testing, CI, and tools. Includes a doctest suite covering adapter enumeration, device/resource creation, buffer mapping, and extension degradation. Allocations use new/delete for now (the engine's Slice-based allocator does not compose with polymorphic base-pointer frees; revisit with the GPU backends).
The validation backend (rhi.validation) wraps any RHI backend and checks API usage before forwarding: encoder state machines (recording / in-pass / finished), non-null resource arguments, pipeline-bound-before-draw, viewport/scissor set before draw, monotonic fence signaling, swap chain acquire/present balance, and per-device live-resource leak tracking. Validated wrappers unwrap to the inner objects before forwarding, so an inner backend only ever sees its own types.
The Vulkan backend (rhi.vk) implements the full RHI surface on Vulkan 1.3+ dynamic rendering: adapter enumeration with preference ordering, logical device and queue setup, all resource/pipeline/bind-group creation, command encoders (render/compute/bundle passes), swap chains with platform surfaces (Win32/X11/Wayland), transfer batches, and the mesh-shader and ray-tracing extensions. Requires the Vulkan loader (find_package(Vulkan)); the loader is a private dependency since the headers live in the module's global fragment.

Includes a platform-agnostic smoke test that initializes the backend, enumerates adapters, and creates a device. It never touches surfaces, so it runs anywhere a loader is present and skips the device step gracefully when no adapter is available.
The DX12 backend (rhi.dx12) implements the full RHI surface on D3D12: adapter enumeration via DXGI, device/queue setup, all resource/pipeline/bind-group creation, descriptor heap management (CPU staging + GPU-visible ring), command encoders (render/compute/bundle passes with copy-on-bind descriptor staging), swap chains, transfer batches, and the mesh-shader and ray-tracing extensions. Windows only: registration is guarded by if(WIN32) and links the D3D12/DXGI system libraries privately.

Includes a platform-agnostic smoke test (built only on Windows) that initializes the backend, enumerates adapters, and creates a device.

Not yet compiled — this box has no D3D12 toolchain; the port is to be validated on Windows.
- DxBackend: sortAdaptersByPreference casing to match Device.cppm
- DxRenderBundleEncoder: PascalCase delegate calls to camelCase
- Math.test: qualify std::cos to resolve overload ambiguity
The shaders module wraps DXC (IDxcCompiler3) to compile HLSL to SPIR-V or DXIL. It loads dxcompiler at runtime (LoadLibrary/dlopen) rather than linking it, so nothing hard-links the compiler. The public compile types (ShaderStage, CompileOptions, CompileResult, ShaderFlags + variant key) are self-contained: the compiler produces bytecode and has no RHI dependency — the RHI-coupled variant cache is a separate module (shaders.system, not yet ported).

Draco::DXCHeaders (ThirdParty/DXC) carries the DXC include dir plus DRACO_DXC_PATH, the absolute path the compiler falls back to for the runtime library. The prebuilt DXC runtime libraries themselves are not committed here.

Test compiles a trivial vertex shader to SPIR-V (checks the magic number) and confirms a bad shader reports an error instead of crashing — passes against the vendored DXC on Linux.
The sample framework (samples.rhi.framework) is the shared base for the RHI samples, relocated into Samples/cpp/RHI so it is scoped to RHI samples only. SampleApp brings up an SDL3 window (shell.desktop), a Vulkan or DX12 backend (validation-wrapped), device, graphics queue, and swap chain, then runs an event/resize/render loop calling the subclass's onInit/onRender/onResize/onShutdown. DepthBuffer is a recreate-on-resize depth target; ShaderHelpers compiles HLSL to a ShaderModule, picking SPIR-V or DXIL by device type and applying Vulkan binding shifts.

The framework re-exports rhi + shell so samples import one module. DX12 is compiled in only on Windows (DRACO_HAS_DX12). Individual samples are added on top of this.
Ports the 30 numbered RHI samples (Triangle through RenderBundles) plus the Smoketest, verbatim apart from the namespace (draco::) and camelCase API changes. They exercise the RHI across vertex/index buffers, textures, uniform buffers, compute, bind groups, blending, instancing, depth, mipmaps, MSAA, MRT, wireframe, samplers, blit, queries, readback, multi-queue, bindless, batch upload, mesh shaders, ray tracing, stencil, cube maps, occlusion queries, multi-draw-indirect, dynamic offsets, 3D textures, procedural RT, resolve, and render bundles.

Each sample is one Main.cpp on the shared framework; a draco_add_rhi_sample() helper wires each executable with the whole-archive SDL link so its static video/input drivers survive. Math types are qualified under draco::math (Vector, Matrix4, Quaternion, degToRad). Build only: they open an SDL3 window and need a GPU/display, so they are not run in CI.
Commit the prebuilt DXC runtime libraries (dxcompiler + dxil, for win-x64 and linux-x86_64) that the shaders module loads at runtime via DRACO_DXC_PATH. Tracking them directly keeps a fresh checkout self-contained: the samples compile HLSL at runtime and need dxcompiler present.
The shader system (shaders.system) is a compile-on-demand cache for shader variants on top of the RHI-free shaders compiler. A shader is registered by name per stage; getVariant(name, stage, flags) turns the flags into #defines, compiles the permutation via DXC, creates a GPU ShaderModule, and caches it by (nameHash, stage, flags). Compile failures are not cached, so a later request retries after a fix. invalidateShader drops and destroys a shader's variants and bumps a monotonic version the PSO cache can poll.

Variants are keyed in a std::unordered_map via a hash functor for the variant key. Needs the RHI to create modules, so it is a separate library from the base shaders module. Test compiles real SPIR-V via DXC and creates modules on the Null backend, covering flags-to-defines, caching, distinct variants, and invalidation.
The graphics module is the shared RHI render host: GraphicsDevice (backend + adapter + logical device + graphics queue + CPU frame-in-flight ring) hands out RenderWindows, each owning a surface/swapchain and a per-window ring of command pools and fences. FrameContext is the per-window, per-frame hand-off — the host owns acquire, fence sync, submit, present, and the backbuffer state transitions.

Split into three modules in one target: graphics (backend-agnostic core, imports only base RHI + Shell), graphics.null (headless factory over the Null backend), and graphics.gpu (Vulkan/DX12 factory, validation-wrapped on request). DX12 is compiled in only on Windows via DRACO_HAS_DX12; validation defaults on for dev builds and off under NDEBUG.

Value-or-error returns use std::expected; owned handles use std::unique_ptr. Headless test over the Null backend + null shell passes (5 cases, 38 assertions).
The render graph (rendergraph) sits over the RHI: passes declare resource accesses, the graph builds a dependency DAG, culls unused work, topologically sorts, allocates and aliases transient textures from a pool, and inserts the right barriers automatically via a subresource state tracker. compile() runs without an encoder, so the graph logic is testable headless. Includes a validator, DOT-graph debug export, and a per-pass GPU/CPU profiler.

Value-or-error uses Status; owned resources use new/delete and std::unique_ptr; callbacks use std::function; maps/sets use std::unordered_map/set. A few local stand-ins (mapFind, appendFormat, getTicks) are marked with NOTE comments to replace once core grows the corresponding facilities.

Thirteen headless test executables over the Null backend cover handles/types, descriptors, pass building, dependency and culling logic, transient generation, barriers, bundle passes, validation, debug export, and profiling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants