Loading...
Searching...
No Matches
IsfBindingsBuilder.hpp
1#pragma once
2
3// Shared infrastructure for binding `storage_input` and `csf_image_input`
4// declarations into a graphics pipeline's shader resource bindings.
5//
6// Mirrors the pattern established by RenderedCSFNode (for compute) but wired
7// to Vertex|Fragment stages for ISF / Raw Raster Pipeline / Scene Pass nodes.
8
9#include <Gfx/Graph/Utils.hpp>
10#include <isf.hpp>
11
12#include <QtGui/private/qrhi_p.h>
13
14#include <score_plugin_gfx_export.h>
15
16#include <cstdint>
17#include <string>
18#include <string_view>
19#include <vector>
20
21namespace score::gfx
22{
23
34{
35 std::string name;
36 std::string access;
37 std::string buffer_usage;
38 bool persistent{false};
39 bool owned{true};
40 int64_t size{0};
41
42 // Layout fields (for size computation + validation). May be empty for auxiliaries.
43 std::vector<isf::storage_input::layout_field> layout;
44
45 // Buffer handles. `buffer` is the currently-written slot (R/W for persistent).
46 // `prev` is only set when persistent — holds the previous frame's data (R/O).
47 QRhiBuffer* buffer{};
48 QRhiBuffer* prev{};
49
50 // Resolved SRB binding slots.
51 int binding{-1};
52 int prev_binding{-1};
53
54 // Stages that see this binding (fragment / vertex / both).
55 QRhiShaderResourceBinding::StageFlags stages{};
56
57 // Optional: indices into the Node's input/output port vectors. -1 = not
58 // connected to a port (e.g. private aux buffer or persistent-only).
59 int input_port_index{-1};
60 int output_port_index{-1};
61};
62
67{
68 std::string name;
69 std::string access;
70 std::string format;
71 bool is3D{false};
72 bool cubemap{false};
73 bool is_array{false};
74 bool persistent{false};
75 int depth{0};
76 int layers{0};
77
78 QRhiTexture* texture{};
79 QRhiTexture* prev{};
80 bool owned{true};
81
82 int binding{-1};
83 int prev_binding{-1};
84 QRhiShaderResourceBinding::StageFlags stages{};
85
86 int input_port_index{-1};
87 int output_port_index{-1};
88};
89
97{
98 std::string name;
99 QRhiBuffer* buffer{};
100 bool owned{false};
101 int binding{-1};
102 QRhiShaderResourceBinding::StageFlags stages{};
103 int input_port_index{-1};
104
108 int64_t declared_size{};
109};
110
115{
116 std::vector<GraphicsSSBO> ssbos;
117 std::vector<GraphicsStorageImage> images;
118 std::vector<GraphicsUBO> ubos;
119
120 // Quick aliases: first SSBO with BUFFER_USAGE="indirect_draw*". Populated
121 // by collectGraphicsStorageResources. Updated by callers when the underlying
122 // SSBO's buffer pointer changes (e.g. when an upstream CSF rebuilds it).
123 QRhiBuffer* indirectDrawBuffer{};
124 bool indirectDrawIndexed{false};
125 int indirectDrawSsboIndex{-1};
126
127 // Next free binding index after all graphics-visible storage resources
128 // (SSBOs + images + UBOs) have been assigned by
129 // collectGraphicsStorageResources. This is exactly the value libisf's
130 // isf_emit_graphics_storage() returns (isf.cpp:3406-3449) and the binding
131 // at which the codegen places the multiview UBO (isf.cpp:3773-3783). Callers
132 // that append a multiview UBO MUST use this rather than re-deriving a max
133 // over ssbos/images alone: that omits uniform_input UBOs and collides the
134 // multiview binding with the last UBO's slot. -1 until the first
135 // collectGraphicsStorageResources() call.
136 int nextBinding{-1};
137
138 // Sentinel zero-buffer bound when an SSBO/UBO upstream port disconnects
139 // mid-session. QRhi (especially Vulkan) requires every SRB binding to
140 // point at a valid resource — without a sentinel, a disconnect leaves
141 // the binding pointing at a dangling QRhiBuffer* (the prior upstream's
142 // buffer, which was deleteLater'd when the upstream node was destroyed).
143 // Lazily allocated on first disconnect, sized to the largest binding
144 // observed (sentinelSize). Single buffer reused for both SSBO and UBO
145 // disconnects since the descriptor type is set on the SRB binding side,
146 // not the buffer side; QRhi accepts a buffer with both StorageBuffer and
147 // UniformBuffer usage flags. owned=true; freed in release().
148 QRhiBuffer* sentinelBuffer{};
149 QRhiBuffer* sentinelUniformBuffer{};
150 uint32_t sentinelSize{0};
151
152 void release()
153 {
154 for(auto& s : ssbos)
155 {
156 if(s.owned)
157 {
158 if(s.buffer) s.buffer->deleteLater();
159 if(s.prev) s.prev->deleteLater();
160 }
161 s.buffer = nullptr;
162 s.prev = nullptr;
163 }
164 ssbos.clear();
165
166 for(auto& i : images)
167 {
168 if(i.owned)
169 {
170 if(i.texture) i.texture->deleteLater();
171 if(i.prev) i.prev->deleteLater();
172 }
173 i.texture = nullptr;
174 i.prev = nullptr;
175 }
176 images.clear();
177
178 for(auto& u : ubos)
179 {
180 if(u.owned && u.buffer)
181 u.buffer->deleteLater();
182 u.buffer = nullptr;
183 }
184 ubos.clear();
185
187 {
188 sentinelBuffer->deleteLater();
189 sentinelBuffer = nullptr;
190 }
192 {
193 sentinelUniformBuffer->deleteLater();
194 sentinelUniformBuffer = nullptr;
195 }
196 sentinelSize = 0;
197
198 indirectDrawBuffer = nullptr;
199 indirectDrawSsboIndex = -1;
200 nextBinding = -1;
201 }
202};
203
204// --- API ------------------------------------------------------------------
205
215SCORE_PLUGIN_GFX_EXPORT
217 const isf::descriptor& desc, int firstBinding, GraphicsStorageResources& out);
218
226SCORE_PLUGIN_GFX_EXPORT
228 QRhi& rhi, QRhiResourceUpdateBatch& res, const RenderList& renderer,
229 const isf::descriptor& desc, GraphicsStorageResources& store,
230 QSize renderSize);
231
239SCORE_PLUGIN_GFX_EXPORT
240QVarLengthArray<QRhiShaderResourceBinding, 8> buildExtraBindings(
241 const GraphicsStorageResources& store);
242
251SCORE_PLUGIN_GFX_EXPORT
253 RenderList& renderer, const std::vector<Port*>& inputPorts,
254 GraphicsStorageResources& store,
255 QRhiShaderResourceBindings* srb = nullptr);
256
265SCORE_PLUGIN_GFX_EXPORT
267 GraphicsStorageResources& store, QRhiShaderResourceBindings& srb);
268
277SCORE_PLUGIN_GFX_EXPORT
278void swapPersistentSSBOsState(GraphicsStorageResources& store);
279
289SCORE_PLUGIN_GFX_EXPORT
291 const GraphicsStorageResources& store, QRhiShaderResourceBindings& srb);
292
313SCORE_PLUGIN_GFX_EXPORT
315 GraphicsStorageResources& store, const ossia::geometry& geometry,
316 QRhiShaderResourceBindings* srb = nullptr);
317
344SCORE_PLUGIN_GFX_EXPORT
346 QRhi& rhi, QRhiResourceUpdateBatch& res,
347 GraphicsStorageResources& store, const ossia::geometry& geometry,
348 QRhiShaderResourceBindings* srb = nullptr);
349
359SCORE_PLUGIN_GFX_EXPORT
360QRhiShaderResourceBinding::StageFlags visibilityToStages(std::string_view visibility) noexcept;
361
391SCORE_PLUGIN_GFX_EXPORT
392int64_t glslTypeSizeBytes(std::string_view type) noexcept;
393
403SCORE_PLUGIN_GFX_EXPORT
404int64_t glslTypeSizeBytes(std::string_view type, const isf::descriptor& d) noexcept;
405
412SCORE_PLUGIN_GFX_EXPORT
413int64_t std430LayoutSize(
414 const std::vector<isf::storage_input::layout_field>& layout) noexcept;
415
432SCORE_PLUGIN_GFX_EXPORT
433int64_t std430ArrayStride(std::string_view type) noexcept;
434
440SCORE_PLUGIN_GFX_EXPORT
441int64_t std430ArrayStride(std::string_view type, const isf::descriptor& d) noexcept;
442
443}
Graphics rendering pipeline for ossia score.
Definition Filter/PreviewWidget.hpp:11
void bindUpstreamImagesFromGeometry(GraphicsStorageResources &store, const ossia::geometry &geometry, QRhiShaderResourceBindings *srb)
Wire read-only csf_image_input storage images to an upstream geometry's published auxiliary_textures.
Definition IsfBindingsBuilder.cpp:852
int64_t glslTypeSizeBytes(std::string_view type) noexcept
Byte size of a single GLSL primitive type as used for SSBO element strides in this codebase.
Definition IsfBindingsBuilder.cpp:16
int64_t std430ArrayStride(std::string_view type) noexcept
std430 array stride for a GLSL primitive type when laid out as T array[] inside a shader storage bloc...
Definition IsfBindingsBuilder.cpp:35
QVarLengthArray< QRhiShaderResourceBinding, 8 > buildExtraBindings(const GraphicsStorageResources &store)
Produce the QRhiShaderResourceBinding list for the graphics pipeline.
Definition IsfBindingsBuilder.cpp:613
void swapPersistentSSBOs(GraphicsStorageResources &store, QRhiShaderResourceBindings &srb)
Swap current/prev for all persistent SSBOs and storage images, then update the SRB.
Definition IsfBindingsBuilder.cpp:1037
void bindUpstreamBuffers(RenderList &renderer, const std::vector< Port * > &inputPorts, GraphicsStorageResources &store, QRhiShaderResourceBindings *srb)
Wire read-only SSBOs to upstream geometry buffers.
Definition IsfBindingsBuilder.cpp:682
void collectGraphicsStorageResources(const isf::descriptor &desc, int firstBinding, GraphicsStorageResources &out)
Walk desc.inputs once and populate out with the storage buffers and images declared by the shader.
Definition IsfBindingsBuilder.cpp:183
void reapplyStorageBindings(const GraphicsStorageResources &store, QRhiShaderResourceBindings &srb)
Re-apply the current persistent-storage state to a single SRB.
Definition IsfBindingsBuilder.cpp:1014
QRhiShaderResourceBinding::StageFlags visibilityToStages(std::string_view v) noexcept
Decode an isf::storage_input::visibility string to Qt RHI stage flags.
Definition IsfBindingsBuilder.cpp:152
void bindUpstreamBuffersFromGeometry(QRhi &rhi, QRhiResourceUpdateBatch &res, GraphicsStorageResources &store, const ossia::geometry &geometry, QRhiShaderResourceBindings *srb)
Wire INPUTS storage_input / uniform_input bindings to upstream geometry's published auxiliary_buffers...
Definition IsfBindingsBuilder.cpp:903
void ensureStorageResources(QRhi &rhi, QRhiResourceUpdateBatch &res, const RenderList &renderer, const isf::descriptor &, GraphicsStorageResources &store, QSize renderSize)
Create missing buffers and textures.
Definition IsfBindingsBuilder.cpp:463
void swapPersistentSSBOsState(GraphicsStorageResources &store)
Swap current/prev pointers in store without touching any SRB.
Definition IsfBindingsBuilder.cpp:1004
int64_t std430LayoutSize(const std::vector< isf::storage_input::layout_field > &layout) noexcept
Compute the std430 element size of a layout (vector of {name,type} field entries),...
Definition IsfBindingsBuilder.cpp:50
One SSBO attached to a graphics pipeline.
Definition IsfBindingsBuilder.hpp:34
std::string access
"read_only" / "write_only" / "read_write"
Definition IsfBindingsBuilder.hpp:36
std::string buffer_usage
"", "indirect_draw", "indirect_draw_indexed"
Definition IsfBindingsBuilder.hpp:37
bool persistent
Ping-pong swapped every frame.
Definition IsfBindingsBuilder.hpp:38
std::string name
Base GLSL identifier (e.g.
Definition IsfBindingsBuilder.hpp:35
int prev_binding
Binding of prev (only set when persistent)
Definition IsfBindingsBuilder.hpp:52
int64_t size
Buffer size in bytes (0 = auto from layout)
Definition IsfBindingsBuilder.hpp:40
bool owned
This SSBO owns buffer and prev (releases them)
Definition IsfBindingsBuilder.hpp:39
int binding
Binding of buffer
Definition IsfBindingsBuilder.hpp:51
One storage image attached to a graphics pipeline.
Definition IsfBindingsBuilder.hpp:67
bool is_array
image2DArray — N-layer array texture
Definition IsfBindingsBuilder.hpp:73
int depth
Explicit Z dimension for 3D textures; 0 = use default (16)
Definition IsfBindingsBuilder.hpp:75
bool persistent
Ping-pong two textures swapped every frame.
Definition IsfBindingsBuilder.hpp:74
std::string format
e.g.
Definition IsfBindingsBuilder.hpp:70
QRhiTexture * texture
Current (write / read_write) slot.
Definition IsfBindingsBuilder.hpp:78
int layers
Layer count for is_array (0 = use parser-supplied default)
Definition IsfBindingsBuilder.hpp:76
int prev_binding
Binding of prev (only set when persistent)
Definition IsfBindingsBuilder.hpp:83
bool cubemap
imageCube — 6-layer cubemap storage image
Definition IsfBindingsBuilder.hpp:72
std::string access
"read_only" / "write_only" / "read_write"
Definition IsfBindingsBuilder.hpp:69
QRhiTexture * prev
Previous frame (read-only); only set when persistent.
Definition IsfBindingsBuilder.hpp:79
Aggregate of all graphics-visible storage resources for a node.
Definition IsfBindingsBuilder.hpp:115
QRhiBuffer * sentinelBuffer
StorageBuffer usage only.
Definition IsfBindingsBuilder.hpp:148
QRhiBuffer * sentinelUniformBuffer
UniformBuffer usage only (GL rejects combined usages)
Definition IsfBindingsBuilder.hpp:149
One UBO sourced from an upstream Buffer port (uniform_input).
Definition IsfBindingsBuilder.hpp:97
bool owned
Always false for now: borrowed from upstream.
Definition IsfBindingsBuilder.hpp:100
int64_t declared_size
Definition IsfBindingsBuilder.hpp:108