1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//! The [`RenderDevice`] trait that Rust-side device implementations satisfy,
//! plus the handle / desc / binding plain-data types that flow through it.
//!
//! Implement [`RenderDevice`] to back a Noesis view with your own GPU backend
//! (a `wgpu` device, say). This layer is pure Rust, no FFI. A C++ shim
//! translates Noesis's pure-virtual `RenderDevice` surface into calls on this
//! trait through a vtable of `extern "C"` trampolines.
use NonZeroU64;
use crate;
// ────────────────────────────────────────────────────────────────────────────
// Handles: opaque IDs the device owns. NonZeroU64 lets `Option<Handle>` reuse
// the handle's niche.
// ────────────────────────────────────────────────────────────────────────────
/// Opaque identifier for a Rust-owned texture resource (e.g. a `wgpu::Texture`
/// in the eventual Bevy impl). Allocated by [`RenderDevice::create_texture`];
/// released by [`RenderDevice::drop_texture`] when the C++ `RustTexture`
/// wrapper is destroyed.
;
/// Opaque identifier for a Rust-owned render-target resource. Allocated by
/// [`RenderDevice::create_render_target`] / [`RenderDevice::clone_render_target`];
/// released by [`RenderDevice::drop_render_target`].
;
/// Sub-rectangle of a texture mip level; used by
/// [`RenderDevice::update_texture`].
// ────────────────────────────────────────────────────────────────────────────
// Texture creation
// ────────────────────────────────────────────────────────────────────────────
/// Inputs to [`RenderDevice::create_texture`]. Borrowed for the duration of
/// the call only.
/// Returned from [`RenderDevice::create_texture`]. The C++ `RustTexture`
/// wrapper stores the metadata and delegates `GetWidth` / `GetHeight` /
/// `HasMipMaps` / `IsInverted` / `HasAlpha` to it without further round-trips.
// ────────────────────────────────────────────────────────────────────────────
// Render-target creation
// ────────────────────────────────────────────────────────────────────────────
/// Inputs to [`RenderDevice::create_render_target`].
/// Returned from [`RenderDevice::create_render_target`] and
/// [`RenderDevice::clone_render_target`].
///
/// `resolve_texture.handle` may be the same as a freshly-created texture's
/// handle (cloned RTs may share the underlying resolve resource); it is the
/// Rust impl's choice. The C++ `RustRenderTarget::GetTexture` returns the
/// `RustTexture` instance built from this binding.
/// Rust-side implementation of `Noesis::RenderDevice`.
///
/// Method order mirrors the frame protocol documented at the top of
/// `NsRender/RenderDevice.h`:
///
/// ```text
/// // Per-frame (texture phase, before any rendering):
/// for each dirty dynamic texture:
/// update_texture()
/// end_updating_textures()
///
/// // Offscreen phase (when needed):
/// begin_offscreen_render()
/// for each render target:
/// set_render_target()
/// for each tile:
/// begin_tile()
/// map_vertices() / map_indices()
/// draw_batch() ...
/// end_tile()
/// resolve_render_target()
/// end_offscreen_render()
///
/// // Onscreen phase:
/// begin_onscreen_render()
/// map_vertices() / map_indices()
/// draw_batch() ...
/// end_onscreen_render()
/// ```
///
/// Noesis calls every method on a single thread (the render thread). `&mut`
/// receivers reflect that; impls do not need internal locking.
///
/// The `Send + Sync` supertrait bounds make the boxed impl behind the
/// [`Registered`] guard `Send` (so the guard can be *moved* to the thread
/// that owns the Noesis view) and let Noesis invoke the device trampolines
/// from its render thread. The guard itself is `Send` but **not** `Sync`:
/// it exposes `&self` accessors (e.g. [`Registered::offscreen_width`]) that
/// read live Noesis state, so it must not be shared across threads. See the
/// crate-level "Thread affinity" docs; store it in a `NonSend` resource, not
/// a plain `Send + Sync` Bevy `Resource`.
///
/// [`Registered`]: crate::render_device::Registered
/// [`Registered::offscreen_width`]: crate::render_device::Registered::offscreen_width