BrainRenderer
Namespace: DISP3DLIB · Library: 3D Display Library
#include <disp3D/brainrenderer.h>
class DISP3DLIB::BrainRenderer
BrainRenderer handles the low-level RHI rendering logic, managing pipelines, shaders, and draw calls for brain surfaces.
Qt RHI-based 3-D renderer managing scene objects, lighting, camera, and render pipeline for brain visualization.
Public Methods
BrainRenderer()
Default Constructor.
~BrainRenderer()
Destructor.
initialize(rhi, rp, sampleCount)
Initialize resources (shaders, pipelines) for the given RHI and render pass.
Ensures pipelines for all supported modes are created.
Parameters:
-
rhi : *QRhi ** Pointer to QRhi instance.
-
rp : *QRhiRenderPassDescriptor ** Render pass descriptor.
-
sampleCount : int MSAA sample count.
ensureRenderTargets(rhi, colorTex, pixelSize)
Create the dual render-target pair for multi-pass rendering.
Both render targets share the same color texture and depth-stencil buffer but differ in their load/store flags:
- m_rtClear: clears color+depth on beginPass (first pass)
- m_rtPreserve: preserves previous output (passes 2+)
Qt bakes these flags into native resources at create() time, so calling setFlags() dynamically does NOT work (especially on WebGL). This is the validated pattern from test_wasm_multi_pass.
Parameters:
-
rhi : *QRhi ** QRhi instance.
-
colorTex : *QRhiTexture ** Color texture (owned by QRhiWidget or caller).
-
pixelSize : const QSize & Render target dimensions.
rtClear()
Returns:
- *QRhiRenderTarget ** — The clearing render target (pass 1).
rtPreserve()
Returns:
- *QRhiRenderTarget ** — The preserving render target (passes 2+).
newFrame()
Start a new frame: frees all per-draw uniform slots.
Call before the frame's first prepare call.
A frame is recorded in two phases. First every prepare*() call writes its uniforms and uploads into resource update batches. Then beginFrame() commits them, and the issue*() calls only bind and draw: QRhi forbids resource updates inside a render pass, and prepare calls made inside one are rejected with a warning.
The renderer owns the GPU copies of the renderables' streams. They are created on the first draw, updated when a stream's generation changes, and freed once the renderable is destroyed or has not been drawn for a while.
cachedMeshCount()
Returns:
- int — Number of renderables whose GPU copies the renderer currently holds.
beginFrame(cb, updates)
Begin the first render pass of a frame.
Clears color and depth.
Parameters:
-
cb : *QRhiCommandBuffer ** Command buffer to record to.
-
updates : *QRhiResourceUpdateBatch ** Resource updates to commit before the pass, or nullptr.
beginPreservingPass(cb, updates)
Begin an additional render pass that preserves previous output.
Uses the internal preserving render target (m_rtPreserve).
Parameters:
-
cb : *QRhiCommandBuffer ** Command buffer.
-
updates : *QRhiResourceUpdateBatch ** Resource updates to commit before the pass, or nullptr.
prepareSurfaceDraw(u, data, surface)
Upload the surface's streams if they changed and write the uniforms of one surface draw.
Parameters:
-
u : *QRhiResourceUpdateBatch ** Resource update batch, committed before the pass.
-
data : const SceneData & Scene uniforms (MVP, light, etc).
-
surface : *BrainSurface ** Pointer to surface.
Returns:
- int — Uniform buffer offset for this draw, or -1 on failure.
issueSurfaceDraw(cb, surface, mode, uniformOffset)
Draw a surface with the uniforms from prepareSurfaceDraw().
Call inside a pass.
Parameters:
-
cb : *QRhiCommandBuffer ** Command buffer.
-
surface : *BrainSurface ** Pointer to surface to draw.
-
mode : ShaderMode Shader mode.
-
uniformOffset : int Offset returned by
prepareSurfaceDraw().
prepareDipoleDraw(u, data, dipoles)
Upload the dipoles' streams if they changed and write the uniforms of their draw.
Parameters:
-
u : *QRhiResourceUpdateBatch ** Resource update batch, committed before the pass.
-
data : const SceneData & Scene uniforms.
-
dipoles : *DipoleObject ** Pointer to
DipoleObject.
Returns:
- int — Uniform buffer offset, or -1 when nothing is drawn.
issueDipoleDraw(cb, dipoles, uniformOffset)
Draw dipoles as instanced cones.
Call inside a pass.
Parameters:
-
cb : *QRhiCommandBuffer ** Command buffer.
-
dipoles : *DipoleObject ** Pointer to
DipoleObject. -
uniformOffset : int Offset returned by
prepareDipoleDraw().
prepareNetworkDraw(u, data, network)
Upload the network's streams if they changed and write the uniforms of its draws.
Parameters:
-
u : *QRhiResourceUpdateBatch ** Resource update batch, committed before the pass.
-
data : const SceneData & Scene uniforms.
-
network : *NetworkObject ** Pointer to
NetworkObject.
Returns:
- int — Uniform buffer offset, or -1 when nothing is drawn.
issueNetworkDraw(cb, network, uniformOffset)
Draw a connectivity network: instanced node spheres and edge cylinders.
Call inside a pass.
Parameters:
-
cb : *QRhiCommandBuffer ** Command buffer.
-
network : *NetworkObject ** Pointer to
NetworkObject. -
uniformOffset : int Offset returned by
prepareNetworkDraw().
preparePolylineDraw(u, data, polyline)
Upload the polyline's streams if they changed and write the uniforms of its draw.
Parameters:
-
u : *QRhiResourceUpdateBatch ** Resource update batch, committed before the pass.
-
data : const SceneData & Scene uniforms.
-
polyline : *PolylineObject ** Pointer to
PolylineObject.
Returns:
- int — Uniform buffer offset, or -1 when nothing is drawn.
issuePolylineDraw(cb, polyline, uniformOffset)
Draw a polyline as connected segments.
Call inside a pass.
Parameters:
-
cb : *QRhiCommandBuffer ** Command buffer.
-
polyline : *PolylineObject ** Pointer to
PolylineObject. -
uniformOffset : int Offset returned by
preparePolylineDraw().
prepareElectrodeDraw(u, data, electrodes)
Upload the electrodes' streams if they changed and write the uniforms of their draws.
Parameters:
-
u : *QRhiResourceUpdateBatch ** Resource update batch, committed before the pass.
-
data : const SceneData & Scene uniforms.
-
electrodes : *ElectrodeObject ** Pointer to
ElectrodeObject.
Returns:
- int — Uniform buffer offset, or -1 when nothing is drawn.
issueElectrodeDraw(cb, electrodes, uniformOffset)
Draw electrode lead bodies and contacts, opaque and depth-tested.
Call inside a pass.
Parameters:
-
cb : *QRhiCommandBuffer ** Command buffer.
-
electrodes : *ElectrodeObject ** Pointer to
ElectrodeObject. -
uniformOffset : int Offset returned by
prepareElectrodeDraw().
prepareVideoOverlay(rhi, u, overlay)
Prepare live video overlay GPU resources and upload the latest frame.
Must be called before a render pass starts. Texture uploads require a blit encoder on Metal and must not be recorded while a render encoder is active.
Parameters:
-
rhi : *QRhi ** QRhi pointer.
-
u : *QRhiResourceUpdateBatch ** Resource update batch owned by the caller.
-
overlay : *VideoOverlay ** Pointer to
VideoOverlay; ignored when null/disabled.
prepareVideoOverlayDraw(u, data, overlay, surface)
Write the uniforms of one video overlay draw: a camera-facing quad at the focus position, or, with a target surface, a decal projected onto that mesh.
Call after prepareVideoOverlay().
Parameters:
-
u : *QRhiResourceUpdateBatch ** Resource update batch, committed before the pass.
-
data : const SceneData & Scene uniforms (MVP, camera position).
-
overlay : *VideoOverlay ** Pointer to
VideoOverlay; ignored when null/disabled. -
surface : *BrainSurface ** Target surface mesh (typically scalp/head), or nullptr for the quad.
Returns:
- int — Uniform buffer offset for this draw, or -1 when nothing is drawn.
issueVideoOverlayDraw(cb, overlay, surface, uniformOffset)
Draw a video overlay prepared with prepareVideoOverlayDraw().
Call inside a pass.
Parameters:
-
cb : *QRhiCommandBuffer ** Command buffer.
-
overlay : *VideoOverlay ** Pointer to
VideoOverlay. -
surface : *BrainSurface ** Same target surface as in
prepareVideoOverlayDraw(). -
uniformOffset : int Offset returned by
prepareVideoOverlayDraw().
prepareSlice(rhi, u, slice, slotIndex)
Prepare MRI slice GPU resources and upload texture data.
Must be called before a render pass starts. Creates the slice pipeline lazily on first use and uploads the slice texture.
Parameters:
-
rhi : *QRhi ** QRhi pointer.
-
u : *QRhiResourceUpdateBatch ** Resource update batch owned by the caller.
-
slice : *SliceObject ** Pointer to
SliceObject; ignored when null. -
slotIndex : int Slot index (0=axial, 1=coronal, 2=sagittal).
prepareSliceDraw(u, data, slotIndex)
Write the uniforms of one MRI slice draw into a resource update batch.
Parameters:
-
u : *QRhiResourceUpdateBatch ** Resource update batch, committed before the pass.
-
data : const SceneData & Scene uniforms (MVP, camera position).
-
slotIndex : int Slot index (0=axial, 1=coronal, 2=sagittal).
Returns:
- int — Uniform buffer offset for this draw, or -1 on failure.
issueSliceDraw(cb, slotIndex, uniformOffset)
Draw an MRI slice prepared with prepareSliceDraw().
Call inside a pass.
Parameters:
-
cb : *QRhiCommandBuffer ** Command buffer (must be inside a render pass).
-
slotIndex : int Slot index matching the prepareSliceDraw call.
-
uniformOffset : int Offset returned by
prepareSliceDraw().
endPass(cb)
End any render pass (clearing or preserving).
Parameters:
- cb : *QRhiCommandBuffer ** Command buffer.
Example
No example of its own: QRhi draw backend owned privately by BrainView; it needs a live GPU device and is driven only through BrainView.
It is exercised by:
BrainView view; // QRhi widget; nothing is rendered until it is shown
view.setViewCount(2);
const bool twoPanes = view.viewCount() == 2 && view.viewMode() == BrainView::MultiView;
LiveMarker trackerTip;
trackerTip.position = QVector3D(0.0f, 0.08f, 0.04f);
trackerTip.color = Qt::yellow;
view.setLiveMarkers({trackerTip}); // transient, does not move the camera
view.clearLiveMarkers();
view.setViewCount(1);
From ex_disp3d_scene.
Authors of this file
- Christoph Dinh <christoph.dinh@mne-cpp.org>