Loading...
Searching...
No Matches
TextureToBuffer.hpp
1#pragma once
2
3#include <ossia/detail/pod_vector.hpp>
4#include <Threedim/GeometryToBufferStrategies.hpp>
5
6#include <boost/container/vector.hpp>
7
8#include <halp/controls.hpp>
9#include <halp/geometry.hpp>
10#include <halp/meta.hpp>
11#include <halp/texture.hpp>
12#include <halp/buffer.hpp>
13#include <halp/texture.hpp>
14
15namespace Threedim
16{
18{
19public:
20 halp_meta(name, "Texture to buffer")
21 halp_meta(category, "Visuals/Utilities")
22 halp_meta(c_name, "texture_to_buffer")
23 halp_meta(manual_url, "https://ossia.io/score-docs/processes/texture-to-buffer.html")
24 halp_meta(uuid, "fd7d6339-c745-4733-a1c2-6ebd0a25fd92")
25
26 struct ins
27 {
28 halp::texture_input<"Texture", halp::custom_variable_texture> texture;
29 } inputs;
30 struct
31 {
32 halp::gpu_buffer_output<"Buffer"> buffer;
33 } outputs;
34
35 QRhiBuffer* buf{};
36 void init(score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res)
37 {
38 outputs.buffer.buffer.handle = nullptr;
39 outputs.buffer.buffer.byte_size = 0;
40 outputs.buffer.buffer.byte_offset = 0;
41 outputs.buffer.buffer.changed = true;
42
43 if(!inputs.texture.texture.bytes)
44 return;
45 auto bytes = inputs.texture.texture.bytesize();
46
47 if(buf)
48 renderer.releaseBuffer(buf);
49 if(bytes <= 0)
50 return;
51
52 buf = renderer.state.rhi->newBuffer(QRhiBuffer::Static, QRhiBuffer::StorageBuffer | QRhiBuffer::VertexBuffer, bytes);
53 if(!buf->create())
54 {
55 delete buf;
56 buf = nullptr;
57 return;
58 }
59
60 outputs.buffer.buffer.handle = buf;
61 outputs.buffer.buffer.byte_size = bytes;
62 outputs.buffer.buffer.byte_offset = 0;
63 outputs.buffer.buffer.changed = true;
64 }
65
66 void update(
67 score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res,
69 {
70 if(!buf || inputs.texture.texture.bytesize() != buf->size())
71 {
72 init(renderer, res);
73 return;
74 }
75 // A dropout tick (bytes == nullptr) re-inits and zeroes the
76 // published outlet while keeping `buf` alive. When the source comes
77 // back at the SAME size no re-init happens, so republish the
78 // still-live, size-matching buffer — otherwise the outlet stays
79 // null forever while runInitialPasses() uploads into a buffer
80 // downstream can no longer see.
81 if(inputs.texture.texture.bytes && outputs.buffer.buffer.handle != buf)
82 {
83 outputs.buffer.buffer.handle = buf;
84 outputs.buffer.buffer.byte_size = buf->size();
85 outputs.buffer.buffer.byte_offset = 0;
86 outputs.buffer.buffer.changed = true;
87 }
88 }
89
90 void release(score::gfx::RenderList& r)
91 {
92 }
93
94 void runInitialPasses(
95 score::gfx::RenderList& renderer, QRhiCommandBuffer& commands,
96 QRhiResourceUpdateBatch*& res, score::gfx::Edge& edge)
97 {
98 if(!inputs.texture.texture.bytes)
99 return;
100 if(!buf)
101 return;
102 auto sz = buf->size();
103 if(inputs.texture.texture.bytesize() != sz)
104 return;
105
106
107 res->uploadStaticBuffer(
108 buf,
109 QByteArray::fromRawData((const char*)inputs.texture.texture.bytes, sz));
110 }
111};
112
113}
Definition TextureToBuffer.hpp:18
List of nodes to be rendered to an output.
Definition RenderList.hpp:30
RenderState & state
RenderState corresponding to this RenderList.
Definition RenderList.hpp:169
Definition TextureToBuffer.hpp:27
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:103