mln-rs-engine 0.0.22

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! What the renderer makes its buffers and textures with on its backend's
//! `Gpu`, with the rendering stats that count them and the queue that they
//! go to when they drop, and the handle a layer makes its meshes and
//! targets with ([`Resources`]).

use std::rc::Rc;

use mln_gfx::gpu::{
    BufferDesc, BufferUsage, Caps, Gpu, GpuError, ShaderKey, TextureDesc, TextureFormat, TextureId, TextureUsage,
};
use mln_gfx::projection_variant::ProjectionVariant;
use mln_gfx::rendering_stats::RenderingStats;
use mln_gfx::shaders::program::Program;
use mln_gfx::texture2d::SamplerState;
use mln_gfx::types::TextureChannelDataType;
use mln_gfx::{Rgba, Size};
use mln_style::bitmap::PremultipliedImage;

use crate::draw::buffer::{self, Buffer};
use crate::draw::context::Context;
use crate::draw::indexes::Indexes;
use crate::draw::objects::{Mesh, MeshDesc};
use crate::draw::release::{Release, ReleaseQueue};
use crate::draw::store::{IndexBuffer, Texture};
use crate::draw::target::Target;
use crate::draw::texture2d::Texture2D;
use crate::draw::vertex_attribute::Uniforms;
use crate::draw::vertices::VertexBuffer;
use crate::log_target;

/// The renderer's GPU resources, as a layer's hooks make and read them: the
/// vertex and index buffers and the textures of its meshes, the meshes it
/// draws ([`mesh`](Self::mesh)), and the targets its draws draw into
/// ([`target`](Self::target)). What it makes stays while the layer keeps a
/// handle to it, and the buffers and textures while a mesh reads them.
pub struct Resources<'a> {
    pub(crate) gpu: &'a mut dyn Gpu,
    pub(crate) context: &'a mut Context,
}

impl<'a> Resources<'a> {
    pub(crate) fn new(gpu: &'a mut dyn Gpu, context: &'a mut Context) -> Self {
        Resources { gpu, context }
    }

    /// The mesh that `desc` describes.
    #[must_use]
    pub fn mesh(&mut self, desc: MeshDesc) -> Mesh {
        Mesh::of(self.context, desc)
    }

    /// A target of `size`, whose texture's channels are of `kind`, or of
    /// the type the backend draws into for it, and which clears to `clear`
    /// before its draws. Its texture is sampled linearly and clamped to its
    /// edges.
    #[must_use]
    pub fn target(&mut self, size: Size, kind: TextureChannelDataType, clear: Rgba) -> Target {
        Target::new(self.context, size, kind, clear)
    }

    /// A buffer of a copy of `vertices`, which the first draw that reads it
    /// uploads.
    #[must_use]
    pub fn vertices<V: bytemuck::Pod>(&mut self, vertices: &[V]) -> VertexBuffer {
        self.context.add_vertices(vertices.to_vec())
    }

    /// A buffer of a copy of `vertices`, as [`vertices`](Self::vertices)
    /// makes it, that [`update_vertices`](Self::update_vertices) replaces
    /// often: it keeps the vertices' storage for the next update.
    #[must_use]
    pub fn dynamic_vertices<V: bytemuck::Pod>(&mut self, vertices: &[V]) -> VertexBuffer {
        self.context.add_dynamic_vertices(vertices)
    }

    /// Replaces the vertices of `buffer`, of the type `V`, with a copy of
    /// `vertices`, as many, which the next draw that reads them uploads.
    /// Returns false, leaving them be, for another number of vertices.
    pub fn update_vertices<V: bytemuck::Pod>(&mut self, buffer: &VertexBuffer, vertices: &[V]) -> bool {
        self.context.update_vertices(buffer, vertices)
    }

    /// A buffer of a copy of `indexes`, which the first draw that reads it
    /// uploads.
    #[must_use]
    pub fn indexes(&mut self, indexes: &[u16]) -> IndexBuffer {
        self.context.add_indexes(Indexes::new(indexes.to_vec()))
    }

    /// The indexes of as many quads as 16-bit indexes reach, four vertices
    /// apart, two triangles each, which every mesh that draws such quads
    /// shares.
    #[must_use]
    pub fn quad_indexes(&mut self) -> IndexBuffer {
        self.context.quad_indexes()
    }

    /// A texture of `format`, sampled as `sampler` says, which
    /// [`write_texture`](Self::write_texture) makes and fills.
    #[must_use]
    pub fn texture(&mut self, format: TextureFormat, sampler: SamplerState) -> Texture {
        self.context.add_texture_with(|texture| {
            texture.set_format(format);
            texture.set_sampler(sampler);
        })
    }

    /// A texture of `image`, sampled as `sampler` says, which the first draw
    /// that binds it uploads.
    #[must_use]
    pub fn image_texture(&mut self, image: Rc<PremultipliedImage>, sampler: SamplerState) -> Texture {
        self.context.add_texture_with(|texture| {
            texture.set_image(Some(image));
            texture.set_sampler(sampler);
        })
    }

    /// A texture of `image` with a full chain of mips, sampled as `sampler`
    /// says, which the first draw that binds it uploads, making the mips
    /// from the image, as the upload of an image that
    /// [`set_image`](Self::set_image) sets does. A
    /// [`mipmapped`](SamplerState::mipmapped) sampler reads the mips.
    #[must_use]
    pub fn mipmapped_image_texture(&mut self, image: Rc<PremultipliedImage>, sampler: SamplerState) -> Texture {
        self.context.add_texture_with(|texture| {
            texture.set_image(Some(image));
            texture.set_mipmapped(true);
            texture.set_sampler(sampler);
        })
    }

    /// Makes `image` what `texture` holds, which the next draw that binds it
    /// uploads.
    pub fn set_image(&mut self, texture: &Texture, image: Rc<PremultipliedImage>) {
        if let Some(mut texture) = self.context.texture_mut(texture) {
            texture.set_image(Some(image));
        }
    }

    /// Writes `data`, the texels of an image of `size` in rows from the top,
    /// to the whole of `texture`, first making it of that size.
    pub fn write_texture(&mut self, texture: &Texture, data: &[u8], size: Size) -> Result<(), GpuError> {
        self.context.upload_texture_data(self.gpu, texture, data, size)
    }

    /// Samples `texture` as `sampler` says, where a mesh doesn't say
    /// otherwise ([`Mesh::set_sampler`]).
    pub fn set_sampler(&mut self, texture: &Texture, sampler: SamplerState) {
        if let Some(mut texture) = self.context.texture_mut(texture) {
            texture.set_sampler(sampler);
        }
    }

    /// The size of `texture`, empty before it has one.
    #[must_use]
    pub fn texture_size(&self, texture: &Texture) -> Size {
        self.context.texture(texture).map(Texture2D::size).unwrap_or_default()
    }

    /// The key of the variant of `program` that reads its properties of
    /// `uniforms` from their uniforms, for `projection`, at the frame's
    /// pixel ratio.
    pub fn shader_key(
        &mut self,
        program: &'static Program,
        uniforms: Uniforms,
        projection: ProjectionVariant,
    ) -> ShaderKey {
        self.context.shader_key(program, uniforms, projection)
    }

    /// What the backend's device allows.
    #[must_use]
    pub fn caps(&self) -> &Caps {
        self.context.caps()
    }

    /// Fails the frame being encoded with `message`, for a GPU operation of
    /// a layer that failed.
    pub fn fail(&mut self, message: String) {
        self.context.fail(message);
    }
}

/// A texture of the context's `Gpu`, which goes to the context's release
/// queue when it drops, as a [`Buffer`] does.
pub(crate) struct GpuTexture {
    id: TextureId,
    /// The bytes the rendering stats count it with, `None` where they don't.
    counted: Option<usize>,
    queue: Rc<ReleaseQueue>,
}

impl GpuTexture {
    #[must_use]
    pub(crate) fn id(&self) -> TextureId {
        self.id
    }
}

impl Drop for GpuTexture {
    fn drop(&mut self) {
        self.queue.push(match self.counted {
            Some(bytes) => Release::Texture(self.id, bytes),
            None => Release::Resource(self.id.into()),
        });
    }
}

/// A texture of the context's `Gpu` that draws share: a
/// [`Texture2D`](crate::draw::texture2d::Texture2D)'s, which bindings made
/// of it hold after the texture replaces it.
pub(crate) type SharedGpuTexture = Rc<GpuTexture>;

/// What the renderer makes its buffers and textures with: the rendering
/// stats that count them, the queue that they go to when they drop, the
/// epochs of its uniform buffer arrays, and what failed on the GPU in the
/// frame being encoded.
#[derive(Default)]
pub(crate) struct Device {
    pub(crate) stats: RenderingStats,
    pub(crate) queue: Rc<ReleaseQueue>,
    /// The last epoch a uniform buffer array took.
    uniform_epoch: u64,
    /// Why the first GPU failure since [`take_failure`](Self::take_failure)
    /// failed.
    failure: Option<String>,
}

impl Device {
    /// Logs `message`, why something of the frame being encoded failed on
    /// the GPU, and keeps it as the frame's failure unless one came first.
    pub(crate) fn fail(&mut self, message: String) {
        tracing::error!(target: log_target::RENDER, error = message, "A GPU operation of the frame failed");
        self.failure.get_or_insert(message);
    }

    /// Whether something failed since the last [`take_failure`](Self::take_failure).
    pub(crate) fn failed(&self) -> bool {
        self.failure.is_some()
    }

    /// Why the first GPU failure since the last call failed, if one did.
    pub(crate) fn take_failure(&mut self) -> Option<String> {
        self.failure.take()
    }

    /// An epoch for a uniform buffer array that changed, one that no array
    /// of the device has had, and never 0.
    pub(crate) fn next_uniform_epoch(&mut self) -> u64 {
        self.uniform_epoch += 1;
        self.uniform_epoch
    }

    /// A buffer of `gpu` of `usage` holding `data`.
    pub(crate) fn buffer(&mut self, gpu: &mut dyn Gpu, usage: BufferUsage, data: &[u8]) -> Result<Buffer, GpuError> {
        self.buffer_of_size(gpu, usage, data, data.len())
    }

    /// A buffer of `gpu` of `usage` and `size` bytes, at least `data`'s,
    /// holding `data` at its start.
    fn buffer_of_size(
        &mut self,
        gpu: &mut dyn Gpu,
        usage: BufferUsage,
        data: &[u8],
        size: usize,
    ) -> Result<Buffer, GpuError> {
        let buffer = self.allocate(gpu, usage, size)?;
        write_bytes(gpu, &buffer, data)?;
        buffer::count_write(&mut self.stats, usage, data.len(), false);
        Ok(buffer)
    }

    /// A buffer of `usage` holding the bytes of `data`, written from the JS
    /// array they're in.
    #[cfg(target_family = "wasm")]
    pub(crate) fn buffer_from_host(
        &mut self,
        gpu: &mut dyn Gpu,
        usage: BufferUsage,
        data: &mln_wire::host_bytes::HostBytes,
    ) -> Result<Buffer, GpuError> {
        let buffer = self.allocate(gpu, usage, data.len())?;
        if !data.is_empty()
            && let Some(array) = data.array()
        {
            gpu.write_buffer_from_host(buffer.id(), 0, &array)?;
        }
        Ok(buffer)
    }

    /// A buffer of `usage` that writes of up to `size` bytes fit in, which
    /// the stats count until the context destroys it.
    fn allocate(&mut self, gpu: &mut dyn Gpu, usage: BufferUsage, size: usize) -> Result<Buffer, GpuError> {
        let allocated = buffer::allocated_size(usage, size);
        let id = gpu.create_buffer(&BufferDesc { label: buffer::label(usage), usage, size: allocated as u64 })?;
        buffer::count(&mut self.stats, usage, allocated, 1);
        self.stats.total_buffers += 1;
        self.stats.total_buffer_objs += 1;
        Ok(Buffer::new(id, usage, size, &self.queue))
    }

    /// Writes `data` at the start of `buffer`, and counts the write. Fails
    /// if `data` is longer than the buffer.
    pub(crate) fn write(&mut self, gpu: &mut dyn Gpu, buffer: &Buffer, data: &[u8]) -> Result<(), GpuError> {
        write_bytes(gpu, buffer, data)?;
        buffer::count_write(&mut self.stats, buffer.usage(), data.len(), true);
        Ok(())
    }

    /// Writes `data` to the uniform or, with `storage`, storage buffer in
    /// `slot`, first making one if the slot has none or `data` doesn't fit
    /// in it: a storage buffer that replaces a smaller one at least doubles
    /// its size. Returns whether it made one in place of one the slot had,
    /// whose bindings its owner then drops.
    pub(crate) fn write_uniforms(
        &mut self,
        gpu: &mut dyn Gpu,
        slot: &mut Option<Buffer>,
        data: &[u8],
        storage: bool,
    ) -> bool {
        let usage = if storage { BufferUsage::Storage } else { BufferUsage::Uniform };
        if let Some(buffer) = slot.as_ref().filter(|buffer| buffer.usage() == usage && buffer.size() >= data.len()) {
            if let Err(error) = self.write(gpu, buffer, data) {
                self.fail(format!("Writing a buffer failed: {error}"));
            }
            return false;
        }
        let replaced = slot.is_some();
        let size = match slot.as_ref() {
            Some(old) if storage && old.usage() == usage => data.len().max(2 * old.size()),
            _ => data.len(),
        };
        *slot = match self.buffer_of_size(gpu, usage, data, size) {
            Ok(buffer) => Some(buffer),
            Err(error) => {
                self.fail(format!("Making a buffer failed: {error}"));
                None
            }
        };
        replaced
    }

    /// A texture of `gpu` of `desc`, which the stats count with `counted`
    /// bytes, or not at all with `None`, until the context destroys it.
    pub(crate) fn texture(
        &mut self,
        gpu: &mut dyn Gpu,
        desc: &TextureDesc<'_>,
        counted: Option<usize>,
    ) -> Result<GpuTexture, GpuError> {
        let id = gpu.create_texture(desc)?;
        if let Some(bytes) = counted {
            self.stats.num_created_textures += 1;
            self.stats.num_active_textures += 1;
            self.stats.mem_textures += bytes as i32;
        }
        Ok(GpuTexture { id, counted, queue: Rc::clone(&self.queue) })
    }

    /// A texture of `size` and `format` that passes draw into and nothing
    /// samples, such as a target's depth and stencil. The stats don't count
    /// it.
    pub(crate) fn attachment(
        &mut self,
        gpu: &mut dyn Gpu,
        label: &str,
        size: Size,
        format: TextureFormat,
    ) -> Result<GpuTexture, GpuError> {
        let usage = TextureUsage { render_target: true, ..TextureUsage::default() };
        self.texture(gpu, &TextureDesc { label, size, format, usage, mipmapped: false }, None)
    }
}

/// The bytes a texture of `size` and `format` that passes draw into takes,
/// as [`RenderingStats`] counts it: a depth and stencil format at 4 bytes a
/// texel, or 8 with a 32-bit depth and a stencil, though a backend may
/// store it in more.
pub(crate) fn attachment_bytes(size: Size, format: TextureFormat) -> u64 {
    let texel = match format {
        TextureFormat::Depth32Float | TextureFormat::Depth24PlusStencil8 => 4,
        TextureFormat::Depth32FloatStencil8 => 8,
        format => u64::from(format.bytes_per_texel().unwrap_or(4)),
    };
    u64::from(size.width) * u64::from(size.height) * texel
}

/// Writes `data` at the start of `buffer` through `gpu`. Fails if `data` is
/// longer than the buffer.
fn write_bytes(gpu: &mut dyn Gpu, buffer: &Buffer, data: &[u8]) -> Result<(), GpuError> {
    if data.len() > buffer.size() {
        return Err(GpuError::Invalid(format!("a write of {} bytes to a buffer of {}", data.len(), buffer.size())));
    }
    if data.is_empty() {
        return Ok(());
    }
    // `Gpu::write_buffer` takes whole 4-byte words.
    if data.len().is_multiple_of(4) {
        gpu.write_buffer(buffer.id(), 0, data)
    } else {
        let mut padded = data.to_vec();
        padded.resize(data.len().next_multiple_of(4), 0);
        gpu.write_buffer(buffer.id(), 0, &padded)
    }
}

/// What the tests of a crate of layer types read of the resources: not
/// API.
#[doc(hidden)]
impl Resources<'_> {
    /// Runs `f` with resources over `gpu`, at a pixel ratio of 1, which it
    /// then tears down.
    pub fn with_test_resources<R>(gpu: &mut dyn Gpu, f: impl FnOnce(&mut Resources<'_>) -> R) -> R {
        let mut context = Context::new(*gpu.caps(), 1.0);
        let result = f(&mut Resources::new(gpu, &mut context));
        context.tear_down(gpu);
        result
    }

    /// The bytes of `vertices` that the next upload writes, empty once the
    /// upload wrote and freed them.
    #[must_use]
    pub fn test_vertex_bytes(&self, vertices: &VertexBuffer) -> Option<&[u8]> {
        self.context.vertices(vertices).map(crate::draw::vertices::Vertices::bytes)
    }

    /// Whether the next upload writes `vertices`.
    #[must_use]
    pub fn test_vertices_need_upload(&self, vertices: &VertexBuffer) -> bool {
        self.context.vertices(vertices).is_some_and(crate::draw::vertices::Vertices::needs_upload)
    }

    /// The buffer of `vertices`, which this uploads.
    pub fn test_upload_vertices(&mut self, vertices: &VertexBuffer) -> Option<mln_gfx::gpu::BufferId> {
        self.context.upload_vertices(self.gpu, vertices).ok().flatten()
    }

    /// The buffer of `indexes`, which this uploads, and whether their data
    /// stays after, with their count.
    pub fn test_upload_indexes(&mut self, indexes: &IndexBuffer) -> (Option<mln_gfx::gpu::BufferId>, bool, usize) {
        let mut draw = crate::draw::indexes::DrawIndexes::Shared(indexes.clone());
        let uploaded = draw.upload(self.gpu, self.context).ok().and_then(|()| draw.buffer(self.context));
        (uploaded, draw.has_data(self.context), draw.len(self.context))
    }

    /// Uploads `mesh`, as a frame that draws it does.
    pub fn test_upload_mesh(&mut self, mesh: &Mesh) {
        mesh.upload(self.gpu, self.context);
    }

    /// The buffer `mesh` binds as its storage buffer `id`, as of its last
    /// upload.
    #[must_use]
    pub fn test_storage_buffer(&self, mesh: &Mesh, id: usize) -> Option<mln_gfx::gpu::BufferId> {
        mesh.storage_buffer(self.context, id)
    }

    /// The rendering stats so far.
    #[must_use]
    pub fn test_rendering_stats(&mut self) -> RenderingStats {
        self.context.rendering_stats().clone()
    }
}