MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Target.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
7/**
8 * @class FormaTarget
9 * @brief Where an element's geometry and texture are written.
10 *
11 * A handle to a FormaBuffer, or to one slice of a FormaBuffer that several
12 * elements share. Everything that writes or controls an element's output goes
13 * through it, so the element does not need to know whether it owns a buffer or
14 * a slice of one. A shared_ptr<FormaBuffer> converts to it implicitly and means
15 * the whole buffer, so code that creates its own buffer is unchanged.
16 *
17 * Copies of one handle share their state: when a slice moves to another
18 * buffer, every copy follows. operator-> reaches the underlying buffer for
19 * buffer-level state such as the render processor or vertex layout; content
20 * and visibility go through the methods below.
21 */
22class MAYAFLUX_API FormaTarget {
23public:
25
26 FormaTarget() = default;
27 FormaTarget(std::nullptr_t) noexcept { }
28
29 /**
30 * @brief A new empty slice of @p buffer, drawn after the others.
31 *
32 * Hands several creators one buffer. Slices of a topology other than the
33 * buffer's need a buffer set up to triangulate.
34 *
35 * @return An empty handle if the buffer is not ready for slices.
36 */
37 [[nodiscard]] static FormaTarget slice_of(
38 const std::shared_ptr<Buffers::FormaBuffer>& buffer,
39 std::optional<Graphics::PrimitiveTopology> topology = std::nullopt);
40
41 /** @brief The whole buffer. */
42 FormaTarget(std::shared_ptr<Buffers::FormaBuffer> buffer);
43
44 /** @brief One slice of @p buffer, as returned by FormaBuffer::add_slice. */
45 FormaTarget(std::shared_ptr<Buffers::FormaBuffer> buffer, SliceId slice);
46
47 [[nodiscard]] explicit operator bool() const noexcept { return m_state != nullptr; }
48
49 [[nodiscard]] Buffers::FormaBuffer* operator->() const noexcept;
50 [[nodiscard]] Buffers::FormaBuffer& operator*() const noexcept;
51
52 /** @brief The underlying buffer; null for an empty handle. */
53 [[nodiscard]] const std::shared_ptr<Buffers::FormaBuffer>& buffer() const noexcept;
54
55 /** @brief Slice id, 0 when the handle means the whole buffer. */
56 [[nodiscard]] SliceId slice() const noexcept;
57
58 /** @brief Replace this target's geometry. */
59 void submit(const std::vector<uint8_t>& bytes) const;
60
61 /** @brief Replace this target's geometry with a contiguous range of vertices. */
62 template <typename V>
63 requires std::ranges::contiguous_range<V>
64 && std::is_trivially_copyable_v<std::ranges::range_value_t<V>>
65 void submit(const V& vertices) const
66 {
67 const auto* src = reinterpret_cast<const uint8_t*>(std::ranges::data(vertices));
68 const size_t n = std::ranges::size(vertices) * sizeof(std::ranges::range_value_t<V>);
69 submit(std::vector<uint8_t>(src, src + n));
70 }
71
72 /** @brief Replace this target's geometry with a single vertex. */
73 template <typename V>
74 requires std::is_trivially_copyable_v<V>
75 && (!std::ranges::range<V>)
76 void submit(const V& vertex) const
77 {
78 const auto* src = reinterpret_cast<const uint8_t*>(&vertex);
79 submit(std::vector<uint8_t>(src, src + sizeof(V)));
80 }
81
82 /** @brief Bind a texture in the buffer's texture array. */
83 void bind_texture(uint32_t array_index, const std::shared_ptr<Core::VKImage>& image) const;
84
85 /** @brief Show or hide this target's output. */
86 void set_visible(bool visible) const;
87
88 /**
89 * @brief Draw this target after the others.
90 * @param priority Draw priority given to a whole buffer. A slice keeps the
91 * priority of its buffer and moves to the end of the buffer's order.
92 */
93 void bring_to_front(int64_t priority) const;
94
95 /** @brief Draw this target before the others. See bring_to_front. */
96 void send_to_back(int64_t priority) const;
97
98 /**
99 * @brief Release this target: retire a whole buffer, or remove a slice.
100 *
101 * A buffer left with no slice is retired with the last one.
102 */
103 void retire() const;
104
105 [[nodiscard]] friend bool operator==(const FormaTarget& a, const FormaTarget& b) noexcept
106 {
107 return a.buffer() == b.buffer() && a.slice() == b.slice();
108 }
109
110 [[nodiscard]] friend bool operator==(const FormaTarget& a, const std::shared_ptr<Buffers::FormaBuffer>& b) noexcept
111 {
112 return a.buffer() == b;
113 }
114
115 [[nodiscard]] friend bool operator==(const FormaTarget& a, std::nullptr_t) noexcept
116 {
117 return !a;
118 }
119
120private:
121 struct State {
122 std::shared_ptr<Buffers::FormaBuffer> buffer;
123 SliceId slice {};
124 };
125
126 std::shared_ptr<State> m_state;
127};
128
129} // namespace MayaFlux::Portal::Forma
IO::ImageData image
Definition Decoder.cpp:47
std::shared_ptr< NetworkGeometryBuffer > buffer
const uint32_t * src
size_t a
size_t b
PrimitiveTopology topology
VKBuffer for 2D screen-space Forma geometry.
friend bool operator==(const FormaTarget &a, const std::shared_ptr< Buffers::FormaBuffer > &b) noexcept
Definition Target.hpp:110
std::shared_ptr< State > m_state
Definition Target.hpp:126
FormaTarget(std::nullptr_t) noexcept
Definition Target.hpp:27
Buffers::FormaBuffer::SliceId SliceId
Definition Target.hpp:24
friend bool operator==(const FormaTarget &a, const FormaTarget &b) noexcept
Definition Target.hpp:105
void submit(const V &vertex) const
Replace this target's geometry with a single vertex.
Definition Target.hpp:76
friend bool operator==(const FormaTarget &a, std::nullptr_t) noexcept
Definition Target.hpp:115
Where an element's geometry and texture are written.
Definition Target.hpp:22
std::shared_ptr< Buffers::FormaBuffer > buffer
Definition Target.hpp:122