Loading...
Searching...
No Matches
ComputeRingDispatcher.hpp
Go to the documentation of this file.
1#pragma once
2
32#include <Gfx/Graph/encoders/ComputeEncoder.hpp>
35
36#include <functional>
37#include <memory>
38#include <vector>
39
40class QRhiTexture;
41
42namespace score::gfx
43{
44struct RenderState;
45}
46
47namespace score::gfx::interop
48{
49
51{
52 QRhi* rhi{};
53 const score::gfx::RenderState* state{};
55 QRhiTexture* sourceTexture{};
56 int width{};
57 int height{};
60 std::function<std::unique_ptr<score::gfx::ComputeEncoder>()> encoderFactory;
66};
67
69{
70public:
71 ComputeRingDispatcher() = default;
72 ~ComputeRingDispatcher() { release(); }
73
75 ComputeRingDispatcher& operator=(const ComputeRingDispatcher&) = delete;
76
77 bool init(const ComputeRingDispatcherConfig& cfg)
78 {
79 if(!cfg.rhi || !cfg.state || !cfg.ring || !cfg.ring->valid()
80 || !cfg.sourceTexture || !cfg.encoderFactory)
81 return false;
82 release();
83 m_cfg = cfg;
84 m_encoders.resize(cfg.ring->slotCount());
85 for(std::size_t i = 0; i < m_encoders.size(); ++i)
86 {
87 m_encoders[i] = cfg.encoderFactory();
88 if(!m_encoders[i])
89 return false;
90 if(!m_encoders[i]->init(
91 *cfg.rhi, *cfg.state, cfg.sourceTexture, cfg.width, cfg.height,
92 cfg.ring->slot(i).qrhiBuffer, cfg.colorConversion))
93 return false;
94 }
95 return true;
96 }
97
98 void release()
99 {
100 for(auto& e : m_encoders)
101 {
102 if(e)
103 {
104 e->release();
105 e.reset();
106 }
107 }
108 m_encoders.clear();
109 m_cfg = {};
110 }
111
114 void encode(QRhiCommandBuffer& cb, std::uint64_t fenceValue)
115 {
116 if(!m_cfg.ring || !m_cfg.rhi)
117 return;
118 auto* res = m_cfg.rhi->nextResourceUpdateBatch();
119 const auto idx = m_cfg.ring->writeIndex();
120 if(idx < m_encoders.size() && m_encoders[idx])
121 m_encoders[idx]->exec(*m_cfg.rhi, cb, res);
122 if(m_cfg.fence)
123 m_cfg.fence->signalAfterEncode(cb, fenceValue);
124 }
125
128 bool waitOnCuda(std::uint64_t fenceValue)
129 {
130 if(m_cfg.fence)
131 return m_cfg.fence->waitOnCuda(fenceValue);
132 return true; // no fence configured — caller assumes implicit ordering
133 }
134
138 {
139 return m_cfg.ring->slot(m_cfg.ring->writeIndex());
140 }
141
143 void advance() noexcept
144 {
145 if(m_cfg.ring)
146 m_cfg.ring->advance();
147 }
148
149private:
151 std::vector<std::unique_ptr<score::gfx::ComputeEncoder>> m_encoders;
152};
153
154} // namespace score::gfx::interop
Backend-uniform N-slot exportable GPU buffer ring for vendor P2P.
Backend-uniform fence between QRhi command-buffer + CUDA stream.
Definition ComputeRingDispatcher.hpp:69
void encode(QRhiCommandBuffer &cb, std::uint64_t fenceValue)
Definition ComputeRingDispatcher.hpp:114
void advance() noexcept
Rotate the ring. Call after the peer has accepted finishedSlot().
Definition ComputeRingDispatcher.hpp:143
GpuRingBufferSlot & finishedSlot() noexcept
Definition ComputeRingDispatcher.hpp:137
bool waitOnCuda(std::uint64_t fenceValue)
Definition ComputeRingDispatcher.hpp:128
Vendor-neutral helper that allocates + manages the slot ring.
Definition ImportedGpuBufferRing.hpp:103
std::size_t advance() noexcept
Rotate the ring; returns the new write index.
Definition ImportedGpuBufferRing.cpp:82
Graphics rendering pipeline for ossia score.
Definition Filter/PreviewWidget.hpp:11
Global state associated to a rendering context.
Definition RenderState.hpp:37
Definition ComputeRingDispatcher.hpp:51
QString colorConversion
Definition ComputeRingDispatcher.hpp:63
InteropFence * fence
Optional. nullptr means no cross-API fence (D3D11/GL no-op style).
Definition ComputeRingDispatcher.hpp:65
std::function< std::unique_ptr< score::gfx::ComputeEncoder >()> encoderFactory
Definition ComputeRingDispatcher.hpp:60
One slot of the ring. Lifetime owned by ImportedGpuBufferRing.
Definition ImportedGpuBufferRing.hpp:50
QRhiBuffer * qrhiBuffer
QRhi-owned UAV / Storage buffer.
Definition ImportedGpuBufferRing.hpp:51
Backend-uniform fence interface. Per-vendor adapters hold one instance, signal it after the per-frame...
Definition InteropFence.hpp:53
virtual void signalAfterEncode(QRhiCommandBuffer &cb, std::uint64_t value)=0
virtual bool waitOnCuda(std::uint64_t value)=0