19 FEB 2026

rahulmnavneeth

render hardware interface

homedocs

Overview

The RHI is a function-pointer table that decouples the viewport core from specific rendering APIs. The viewport calls through MopRhiBackend * — it never calls backend functions directly.

Location

src/rhi/
  rhi.h     — MopRhiBackend struct, opaque handle declarations
  rhi.c     — Backend resolution dispatch

Opaque Handles

HandleRepresents
MopRhiDeviceA rendering context (GL context, Vk device, CPU state)
MopRhiBufferVertex or index data storage
MopRhiFramebufferColor + depth + object-ID render targets

Function Table

Every backend must populate every function pointer. NULL entries are invalid.

FunctionSignaturePurpose
device_create() → Device*Create rendering context
device_destroy(Device*) → voidDestroy context
buffer_create(Device*, BufferDesc*) → Buffer*Upload vertex/index data
buffer_destroy(Device*, Buffer*) → voidFree buffer
framebuffer_create(Device*, FbDesc*) → Fb*Create render targets
framebuffer_destroy(Device*, Fb*) → voidFree render targets
framebuffer_resize(Device*, Fb*, w, h) → voidReallocate at new size
frame_begin(Device*, Fb*, ClearColor) → voidClear and begin frame
frame_end(Device*, Fb*) → voidFinalize frame
draw(Device*, Fb*, DrawCall*) → voidRasterize one mesh
pick_read_id(Device*, Fb*, x, y) → uint32_tRead object ID at pixel
pick_read_depth(Device*, Fb*, x, y) → floatRead depth at pixel
framebuffer_read_color(Device*, Fb*, &w, &h) → uint8_t*Read RGBA8 color buffer

Draw Call Descriptor

typedef struct MopRhiDrawCall {
    MopRhiBuffer *vertex_buffer;
    MopRhiBuffer *index_buffer;
    uint32_t      vertex_count;
    uint32_t      index_count;
    uint32_t      object_id;
    MopMat4       model, view, projection, mvp;
    MopColor      base_color;
    bool          wireframe, depth_test, backface_cull;
} MopRhiDrawCall;

The viewport precomputes mvp. Backends may use mvp directly (CPU) or upload separate matrices as uniforms (GPU).

Backend Registration

Each backend exposes a factory returning static const MopRhiBackend *:

const MopRhiBackend *mop_rhi_backend_cpu(void);

The dispatcher in rhi.c maps MopBackendType → factory. MOP_BACKEND_AUTO resolves to the platform default.

Implementing a New Backend

See Extension Strategy for the full checklist.