bottomless-pit 0.4.0

A very simple 2D rendering/game engine inspired by raylib
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//! Cointains the interface into the texture cache and by
//! extension accsss the texture interface

use crate::context::WgpuClump;
use crate::engine_handle::Engine;
use crate::resource::{self, InProgressResource, LoadingOp, ResourceId, ResourceType};
use crate::vectors::Vec2;
use crate::{layouts, ERROR_TEXTURE_DATA};
use image::{GenericImageView, ImageError};
use std::fmt::Display;
use std::io::Error;
use std::path::Path;

/// Contains all the information need to render an image/texture to the screen.
/// In order to be used it must be put inside a [Material](crate::material::Material)
pub struct Texture {
    pub(crate) _view: wgpu::TextureView,
    pub(crate) bind_group: wgpu::BindGroup,
    pub(crate) size: Vec2<f32>,
}

impl Texture {
    /// Attempts to both read a file at the specified path and turn it into an image.
    pub fn new<P>(engine: &mut Engine, path: P, loading_op: LoadingOp) -> ResourceId<Texture>
    where
        P: AsRef<Path>,
    {
        let typed_id = resource::generate_id::<Texture>();
        let id = typed_id.get_id();
        let path = path.as_ref();
        let ip_resource = InProgressResource::new(
            path,
            id,
            ResourceType::Image(
                SamplerType::LinearInterpolation,
                SamplerType::NearestNeighbor,
            ),
            loading_op,
        );

        engine.loader.load(ip_resource, engine.get_proxy());

        typed_id
    }

    /// Attempts to both read a file at the specified path and turn it into an image.
    pub fn new_with_sampler<P>(
        engine: &mut Engine,
        path: P,
        sampler: SamplerType,
        loading_op: LoadingOp,
    ) -> ResourceId<Texture>
    where
        P: AsRef<Path>,
    {
        let typed_id = resource::generate_id::<Texture>();
        let id = typed_id.get_id();
        let path = path.as_ref();
        let ip_resource =
            InProgressResource::new(path, id, ResourceType::Image(sampler, sampler), loading_op);

        engine.loader.blocking_load(ip_resource, engine.get_proxy());

        typed_id
    }

    /// Attempts to load the file at the path and then turn it into a texture. This also allows you to select what sampling type to use
    /// for both the `mag_sampler`, when the texture is being drawn larger than the orignal resolution and `min_sampler`, when the texture
    /// is being drawn smaller than the original resolution.
    pub fn new_with_mag_min_sampler<P>(
        engine: &mut Engine,
        path: P,
        mag_sampler: SamplerType,
        min_sampler: SamplerType,
        loading_op: LoadingOp,
    ) -> ResourceId<Texture>
    where
        P: AsRef<Path>,
    {
        let typed_id = resource::generate_id::<Texture>();
        let id = typed_id.get_id();
        let path = path.as_ref();
        let ip_resource = InProgressResource::new(
            path,
            id,
            ResourceType::Image(mag_sampler, min_sampler),
            loading_op,
        );

        engine.loader.blocking_load(ip_resource, engine.get_proxy());

        typed_id
    }

    pub(crate) fn from_resource_data(
        engine: &Engine,
        label: Option<&str>,
        data: Vec<u8>,
        mag_sampler: SamplerType,
        min_sampler: SamplerType,
    ) -> Result<Self, TextureError> {
        let img = image::load_from_memory(&data)?;
        Ok(Self::from_image(
            engine,
            img,
            label,
            mag_sampler,
            min_sampler,
        ))
    }

    pub(crate) fn new_direct(
        view: wgpu::TextureView,
        bind_group: wgpu::BindGroup,
        size: Vec2<f32>,
    ) -> Self {
        Self {
            _view: view,
            bind_group,
            size,
        }
    }

    pub(crate) fn default(engine: &Engine) -> Self {
        let image = image::load_from_memory(ERROR_TEXTURE_DATA).unwrap();
        Self::from_image(
            engine,
            image,
            Some("Error Texture"),
            SamplerType::LinearInterpolation,
            SamplerType::NearestNeighbor,
        )
    }

    fn from_image(
        engine: &Engine,
        img: image::DynamicImage,
        label: Option<&str>,
        mag_filter: SamplerType,
        min_filter: SamplerType,
    ) -> Self {
        let wgpu = &engine.context.as_ref().expect("need graphic context").wgpu;
        let diffuse_rgba = img.to_rgba8();
        let (width, height) = img.dimensions();

        let texture_size = wgpu::Extent3d {
            width,
            height,
            depth_or_array_layers: 1,
        };

        let texture = wgpu.device.create_texture(&wgpu::TextureDescriptor {
            size: texture_size,
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: wgpu::TextureFormat::Rgba8UnormSrgb,
            view_formats: &[],
            // TEXTURE_BINDING tells wgpu that we want to use this texture in shaders
            // COPY_DST means that we want to copy data to this texture
            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
            label,
        });

        wgpu.queue.write_texture(
            wgpu::ImageCopyTextureBase {
                texture: &texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            &diffuse_rgba,
            wgpu::ImageDataLayout {
                offset: 0,
                bytes_per_row: Some(4 * width),
                rows_per_image: Some(height),
            },
            texture_size,
        );

        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
        let bind_group_layout = layouts::create_texture_layout(&wgpu.device);

        let texture_sampler = wgpu.device.create_sampler(&wgpu::SamplerDescriptor {
            // what to do when given cordinates outside the textures height/width
            address_mode_u: wgpu::AddressMode::Repeat,
            address_mode_v: wgpu::AddressMode::Repeat,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            // what do when give less or more than 1 pixel to sample
            // linear interprelates between all of them nearest gives the closet colour
            mag_filter: mag_filter.into(),
            min_filter: min_filter.into(),
            mipmap_filter: wgpu::FilterMode::Nearest,
            ..Default::default()
        });

        let bind_group = wgpu.device.create_bind_group(&wgpu::BindGroupDescriptor {
            layout: &bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(&view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(&texture_sampler),
                },
            ],
            label: Some("diffuse_bind_group"),
        });

        let size = Vec2 {
            x: width as f32,
            y: height as f32,
        };

        Self {
            _view: view,
            bind_group,
            size,
        }
    }
}

/// Loading a texture can fail in two senarios. Either the file cant be opened, or the
/// file loaded is not a supported image file type.
#[derive(Debug)]
pub(crate) enum TextureError {
    IoError(Error),
    ImageError(ImageError),
}

impl From<Error> for TextureError {
    fn from(value: Error) -> TextureError {
        Self::IoError(value)
    }
}

impl From<ImageError> for TextureError {
    fn from(value: ImageError) -> Self {
        Self::ImageError(value)
    }
}

/// A UniformTexture is a special type of texture that you can renderer to
/// and also render itself to the screen. Uniform Textures is usefull for things like lightmaps or
/// graphics techniques that reuqire multiple rendering passes.
pub struct UniformTexture {
    inner_texture: Option<InnerTexture>,
    size: Vec2<u32>,
    mag_sampler: SamplerType,
    min_sampler: SamplerType,
    // marks wether or not the view needs to updatred
    // starts at true as we have to set the view later.
    needs_update: bool,
}

impl UniformTexture {
    /// creates a `UniformTexture` of a specified size
    /// this can be reszied at anytime with
    /// [Material::resize_uniform_texture](crate::material::Material::resize_uniform_texture)
    pub fn new(engine: &Engine, size: Vec2<u32>) -> Self {
        let inner_texture = engine.context.as_ref().map(|c| {
            InnerTexture::from_wgpu(
                size,
                SamplerType::LinearInterpolation,
                SamplerType::NearestNeighbor,
                c.get_texture_format(),
                &c.wgpu,
            )
        });

        Self {
            inner_texture,
            size,
            mag_sampler: SamplerType::LinearInterpolation,
            min_sampler: SamplerType::NearestNeighbor,
            needs_update: true,
        }
    }

    /// This creates a UniformTexture with samplers which allows you to select what sampling type to use for both the `mag_sampler`,
    /// when the texture is being drawn larger than the orignal resolution and `min_sampler`, when the texture is being drawn
    /// smaller than the original resolution.
    pub fn new_with_sampler(
        engine: &Engine,
        size: Vec2<u32>,
        mag_sampler: SamplerType,
        min_sampler: SamplerType,
    ) -> Self {
        let inner_texture = engine.context.as_ref().map(|c| {
            InnerTexture::from_wgpu(
                size,
                mag_sampler,
                min_sampler,
                c.get_texture_format(),
                &c.wgpu,
            )
        });

        Self {
            inner_texture,
            size,
            mag_sampler,
            min_sampler,
            needs_update: true,
        }
    }

    pub(crate) fn resize(
        &mut self,
        new_size: Vec2<u32>,
        wgpu: &WgpuClump,
        format: wgpu::TextureFormat,
    ) {
        if self.inner_texture.is_none() {
            self.inner_texture = Some(InnerTexture::from_wgpu(
                new_size,
                self.mag_sampler,
                self.min_sampler,
                format,
                wgpu,
            ));
            self.size = new_size;
            return;
        }

        self.inner_texture
            .as_mut()
            .unwrap()
            .resize(new_size, wgpu, format);
        self.needs_update = true;
    }

    /// Gets the current size of the texture
    pub fn get_size(&self) -> Vec2<u32> {
        self.size
    }

    pub(crate) fn get_sampler(&self) -> &wgpu::Sampler {
        &self.inner_texture.as_ref().unwrap().sampler
    }

    pub(crate) fn get_sampler_info(&self) -> (SamplerType, SamplerType) {
        (self.mag_sampler, self.min_sampler)
    }

    pub(crate) fn make_render_view<'a>(
        &'a mut self,
        wgpu: &WgpuClump,
        format: wgpu::TextureFormat,
    ) -> &'a wgpu::TextureView {
        if self.inner_texture.is_none() {
            self.inner_texture = Some(InnerTexture::from_wgpu(
                self.size,
                self.mag_sampler,
                self.min_sampler,
                format,
                wgpu,
            ));
        }

        self.inner_texture.as_mut().unwrap().make_render_view()
    }

    pub(crate) fn make_view(
        &mut self,
        wgpu: &WgpuClump,
        format: wgpu::TextureFormat,
    ) -> wgpu::TextureView {
        if self.inner_texture.is_none() {
            self.inner_texture = Some(InnerTexture::from_wgpu(
                self.size,
                self.mag_sampler,
                self.min_sampler,
                format,
                wgpu,
            ));
        }

        self.inner_texture.as_ref().unwrap().make_view()
    }

    pub(crate) fn updated(&mut self) {
        self.needs_update = false;
    }

    pub(crate) fn needs_update(&self) -> bool {
        self.needs_update
    }
}

struct InnerTexture {
    inner_texture: wgpu::Texture,
    view: wgpu::TextureView,
    sampler: wgpu::Sampler,
}

impl InnerTexture {
    fn from_wgpu(
        size: Vec2<u32>,
        mag_sampler: SamplerType,
        min_sampler: SamplerType,
        format: wgpu::TextureFormat,
        wgpu: &WgpuClump,
    ) -> Self {
        let sampler = wgpu.device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("Uniform Texture Sampler"),
            address_mode_u: wgpu::AddressMode::Repeat,
            address_mode_v: wgpu::AddressMode::Repeat,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: mag_sampler.into(),
            min_filter: min_sampler.into(),
            mipmap_filter: wgpu::FilterMode::Nearest,
            ..Default::default()
        });

        let inner_texture = wgpu.device.create_texture(&wgpu::TextureDescriptor {
            label: Some("Uniform Texture"),
            size: wgpu::Extent3d {
                width: size.x,
                height: size.y,
                depth_or_array_layers: 1,
            },
            dimension: wgpu::TextureDimension::D2,
            mip_level_count: 1,
            sample_count: 1,
            format,
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
            view_formats: &[],
        });

        let view = inner_texture.create_view(&wgpu::TextureViewDescriptor::default());

        Self {
            inner_texture,
            view,
            sampler,
        }
    }

    fn resize(&mut self, new_size: Vec2<u32>, wgpu: &WgpuClump, format: wgpu::TextureFormat) {
        let inner_texture = wgpu.device.create_texture(&wgpu::TextureDescriptor {
            label: Some("Uniform Texture"),
            size: wgpu::Extent3d {
                width: new_size.x,
                height: new_size.y,
                depth_or_array_layers: 1,
            },
            dimension: wgpu::TextureDimension::D2,
            mip_level_count: 1,
            sample_count: 1,
            format,
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
            view_formats: &[],
        });

        let new_view = inner_texture.create_view(&wgpu::TextureViewDescriptor {
            label: Some("Uniform Texture View"),
            ..Default::default()
        });

        self.inner_texture = inner_texture;
        self.view = new_view;
    }

    pub(crate) fn make_view(&self) -> wgpu::TextureView {
        self.inner_texture
            .create_view(&wgpu::TextureViewDescriptor {
                label: Some("Uniform Texture View"),
                ..Default::default()
            })
    }

    pub(crate) fn make_render_view(&mut self) -> &wgpu::TextureView {
        self.view = self
            .inner_texture
            .create_view(&wgpu::TextureViewDescriptor {
                label: Some("Uniform Texture View"),
                ..Default::default()
            });

        &self.view
    }
}

impl std::error::Error for TextureError {}

impl Display for TextureError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::IoError(e) => write!(f, "{}", e),
            Self::ImageError(e) => write!(f, "{}", e),
        }
    }
}

/// The diffrent types of sampling modes
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SamplerType {
    /// Nearest Neighbor sampling
    ///
    /// This creates a pixelated look when used in upscaling best
    /// for pixel art games
    NearestNeighbor,
    /// Linear Interpolation sampling
    ///
    /// Creates a smoother blury look when used in upscaling
    LinearInterpolation,
}

impl From<SamplerType> for wgpu::FilterMode {
    fn from(value: SamplerType) -> Self {
        match value {
            SamplerType::LinearInterpolation => wgpu::FilterMode::Linear,
            SamplerType::NearestNeighbor => wgpu::FilterMode::Nearest,
        }
    }
}