willow/
traits.rs

1use std::ops::RangeBounds;
2
3use super::{resolve_range, Buffer, BufferDataUsage, Context, Indices, RenderPrimitiveType};
4
5/// Represents WebGL programs.
6///
7/// This type should only be implemented by the [`Program`][super::Program] macro.
8pub trait Program: Sized {
9    /// The struct generated for storing attributes.
10    /// Always `#[repr(C)]`.
11    type AttrStruct: AttrStruct;
12
13    /// Compiles and links the program in the given [`Context`](struct.Context.html).
14    fn create(context: &Context) -> Self {
15        let p = Self::create_internally(context);
16        p.compile_shaders(context);
17        p.link_shaders(context);
18        p
19    }
20
21    /// Creates an instance of the type. Allocate necessary resources like `gl.createShader()`.
22    fn create_internally(gl: &Context) -> Self;
23
24    /// Compiles the vertex and fragment shaders.
25    fn compile_shaders(&self, gl: &Context);
26
27    /// Attaches and links the vertex and fragment shaders.
28    fn link_shaders(&self, gl: &Context);
29
30    /// Prepares a buffer with the attributes in the vec.
31    fn prepare_buffer(
32        context: &Context,
33        attrs: &[Self::AttrStruct],
34        usage: BufferDataUsage,
35    ) -> Buffer<Self::AttrStruct> {
36        Buffer::from_slice(context, attrs, usage)
37    }
38
39    /// Runs the program with the given attributes.
40    fn draw_fully(
41        &self,
42        context: &Context,
43        mode: RenderPrimitiveType,
44        buffer: &Buffer<Self::AttrStruct>,
45        items: impl RangeBounds<usize>,
46    ) {
47        let (start, end) = resolve_range(items, buffer.count);
48
49        self.apply_attrs(context, buffer);
50
51        context.native.draw_arrays(mode.to_const(), start, end);
52    }
53
54    /// Runs the program with the given attributes indexed by `indices`.
55    fn draw_indexed(
56        &self,
57        context: &Context,
58        mode: RenderPrimitiveType,
59        buffer: &Buffer<Self::AttrStruct>,
60        indices: &Indices,
61        items: impl RangeBounds<usize>,
62    ) {
63        self.apply_attrs(context, buffer);
64
65        indices.draw(mode, context, items);
66    }
67
68    /// Applies the buffer ot the attributes in this program.
69    fn apply_attrs(&self, context: &Context, buffer: &Buffer<Self::AttrStruct>);
70}
71
72/// The trait implemented by attribute structs.
73///
74/// Methods in this struct describe the structure of the fields.
75pub trait AttrStruct {
76    /// The number of fields in the struct
77    fn fields_count() -> usize;
78
79    /// The GLSL name of the attribute corresponding to field `i`
80    fn field_gl_name(i: usize) -> &'static str;
81
82    /// The offset of field `i` in the struct in bytes
83    fn field_offset(i: usize) -> usize;
84
85    /// The base type of field `i` in the struct with constants like `WebGlRenderingContext::BYTE`
86    fn field_type(i: usize) -> u32;
87
88    /// The number of components for the type in field `i`
89    fn field_num_comps(i: usize) -> usize;
90
91    /// Whether the field `i` should be normalized
92    fn field_normalized(i: usize) -> bool;
93}