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
#![warn(missing_docs)]

//! Wrend is a ***W***ebGL2 ***Rend***ering library for making Rust/JavaScript + WebGL development easier and safer.
//!
//! Wrend provides an organized system for building custom rendering pipelines: you tell Wrend *how* all of your graphical resources
//! relate to one another through user-specified ids and callbacks, and it does all the work of actually putting things together.
//! It also comes with some out-of-the-box nice-to-have abstractions like recording canvas output and animation frame scheduling,
//! as well as automatic clean-up.
//!
//! Though most of the demo app examples are built using Yew, `wrend` itself is framework agnostic  and is designed
//! to be used in a variety of settings with diverse rendering pipelines, including contexts like React and
//! raw HTML & JavaScript.
//!
//! ## Overview
//!
//! ### Links
//!
//! The fundamental organizing components of Wrend are `links`, such as [`ProgramLink`], [`AttributeLink`],
//! and [`UniformLink`],  which get appended to a [RendererDataBuilder]. These links tell `wrend` how your data
//! should be created and how each resource relates to all the other resources in your pipeline.
//!
//! ### Callbacks
//!
//! Many links accept some sort of callback, which is used to create a particular resource in your  build pipeline.
//!
//! For example, [`BufferLink`] accepts a [`BufferCreateCallback`], which is called during the build process to
//! acquire a [`web_sys::WebGlBuffer`]. In this callback, you are free to initialize your [`web_sys::WebGlBuffer`]
//! however you like.
//!
//! ### Ids
//!
//! Most resources such as shaders, [`Uniform`]s and [`Attribute`]s retrieve resources using unique [`Id`]s, which can be
//! any data type that implements the [`Id`] trait. These Ids help Wrend understand how your data fits together.
//!
//! For example, you can load shaders into the build pipeline using [`RendererDataBuilder::add_vertex_shader_src`].
//! Then, when creating a [`ProgramLink`], you can refer to that shader using its `VertexShaderId` to link that shader
//! to any number of programs you create.
//!
//! ### Build
//!
//! Once all resources and `links` have been added to the [RendererDataBuilder], the pipeline can be built
//! on using [RendererDataBuilder::build_renderer].
//!
//! # Panics
//!
//! There are very few locations in which Rust code can panic in `wrend`, and those that exist are being slimmed down.
//!
//! The primary locations that *can* are where JavaScript types such as arrays are passed into Rust--because these types must be converted
//! to WebAssembly, there is currently the chance for panic if the wrong type is supplied. If using TypeScript, you should see TypeScript errors
//! for any incorrect types supplied, as the library as a whole is strongly typed.
//!
//! A long term goal of `wrend` is to provide matchable errors (or `catch`able errors in JavaScript) for all fallible operations.
//!
//! # Example
//!
//! The following is a "Hello, triangle!" example (the equivalent of "Hello, world!" for WebGL)
//!
//! ```no_run
//! use js_sys::Float32Array;
//! use wasm_bindgen::{prelude::*, JsCast};
//! use web_sys::{window, HtmlCanvasElement, WebGl2RenderingContext};
//! use wrend::{
//!     AttributeCreateContext, AttributeLink, BufferCreateContext, BufferLink, Id, IdDefault, IdName,
//!     ProgramLink, Renderer, RendererData,
//! };
//!
//! #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
//! pub struct ProgramId;
//!
//! impl Id for ProgramId {}
//!
//! #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
//! pub struct VaoId;
//!
//! impl Id for VaoId {}
//!
//! #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
//! pub enum BufferId {
//!     VertexBuffer,
//! }
//!
//! impl Id for BufferId {}
//!
//! impl Default for BufferId {
//!     fn default() -> Self {
//!         Self::VertexBuffer
//!     }
//! }
//!
//! impl IdName for BufferId {
//!     fn name(&self) -> String {
//!         match self {
//!             BufferId::VertexBuffer => "a_position".to_string(),
//!         }
//!     }
//! }
//!
//! #[derive(Clone, Default, Copy, PartialEq, Eq, Hash, Debug)]
//! pub struct VertexShaderId;
//!
//! impl Id for VertexShaderId {}
//!
//! #[derive(Clone, Default, Copy, PartialEq, Eq, Hash, Debug)]
//! pub struct FragmentShaderId;
//!
//! impl Id for FragmentShaderId {}
//!
//! #[derive(Clone, Default, Copy, PartialEq, Eq, Hash, Debug)]
//! pub struct PositionAttributeId;
//!
//! impl Id for PositionAttributeId {}
//!
//! impl IdName for PositionAttributeId {
//!     fn name(&self) -> String {
//!         String::from("a_position")
//!     }
//! }
//!
//! #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
//! struct AppState {
//!     count: u32,
//! }
//!
//! const VERTEX_SHADER: &str = r#"#version 300 es
//! in vec2 a_position;
//! out vec2 v_position;
//!
//! void main() {
//!     gl_Position = vec4(a_position, 0, 1);
//!     vec2 zero_to_two = a_position + 1.0;
//!     vec2 zero_to_one = zero_to_two * 0.5;
//!     v_position = zero_to_one;
//! }"#;
//!
//!
//! const FRAGMENT_SHADER: &str = r#"#version 300 es
//! precision mediump float;
//! in vec2 v_position;
//! out vec4 out_color;
//!
//! void main() {
//!   out_color = vec4(v_position.x, v_position.y, v_position.x * 0.5 + v_position.y * 0.5, 1);
//! }"#;
//!
//! pub fn main() -> Result<(), JsValue> {
//!     let canvas: HtmlCanvasElement = window()
//!         .unwrap()
//!         .document()
//!         .unwrap()
//!         .query_selector("canvas")
//!         .unwrap()
//!         .unwrap()
//!         .dyn_into()
//!         .unwrap();
//!
//!     let app_state = AppState::default();
//!
//!     let program_link = ProgramLink::new(ProgramId, VertexShaderId, FragmentShaderId);
//!
//!     let vertex_buffer_link =
//!         BufferLink::new(BufferId::VertexBuffer, |ctx: &BufferCreateContext| {
//!             let gl = ctx.gl();
//!             let buffer = gl.create_buffer().unwrap();
//!             gl.bind_buffer(WebGl2RenderingContext::ARRAY_BUFFER, Some(&buffer));
//!             let vertex_array = unsafe { Float32Array::view(&[-0.0, 1.0, 1.0, -1.0, -1.0, -1.0]) };
//!             gl.buffer_data_with_array_buffer_view(
//!                 WebGl2RenderingContext::ARRAY_BUFFER,
//!                 &vertex_array,
//!                 WebGl2RenderingContext::STATIC_DRAW,
//!             );
//!
//!             buffer
//!         });
//!
//!     let a_position_link = AttributeLink::new(
//!         VaoId,
//!         BufferId::VertexBuffer,
//!         PositionAttributeId,
//!         |ctx: &AttributeCreateContext| {
//!             let gl = ctx.gl();
//!             let attribute_location = ctx.attribute_location();
//!             let webgl_buffer = ctx.webgl_buffer();
//!             gl.bind_buffer(WebGl2RenderingContext::ARRAY_BUFFER, Some(webgl_buffer));
//!             gl.vertex_attrib_pointer_with_i32(
//!                 attribute_location.into(),
//!                 2,
//!                 WebGl2RenderingContext::FLOAT,
//!                 false,
//!                 0,
//!                 0,
//!             );
//!         },
//!     );
//!
//!     let render_callback = |renderer_data: &RendererData<
//!         VertexShaderId,
//!         FragmentShaderId,
//!         ProgramId,
//!         IdDefault,
//!         BufferId,
//!         PositionAttributeId,
//!         IdDefault,
//!         IdDefault,
//!         IdDefault,
//!         VaoId,
//!         AppState,
//!     >| {
//!         let gl = renderer_data.gl();
//!         let canvas: HtmlCanvasElement = gl.canvas().unwrap().dyn_into().unwrap();
//!
//!         renderer_data.use_program(&ProgramId);
//!         renderer_data.use_vao(&VaoId);
//!
//!         gl.viewport(0, 0, canvas.width() as i32, canvas.height() as i32);
//!         gl.clear_color(0.0, 0.0, 0.0, 0.0);
//!         gl.clear(WebGl2RenderingContext::COLOR_BUFFER_BIT);
//!         gl.draw_arrays(WebGl2RenderingContext::TRIANGLES, 0, 3);
//!     };
//!
//!     let mut render_builder = Renderer::builder();
//!
//!     render_builder
//!         .set_canvas(canvas)
//!         .set_user_ctx(app_state)
//!         .add_vertex_shader_src(VertexShaderId, VERTEX_SHADER.to_string())
//!         .add_fragment_shader_src(FragmentShaderId, FRAGMENT_SHADER.to_string())
//!         .add_program_link(program_link)
//!         .add_buffer_link(vertex_buffer_link)
//!         .add_attribute_link(a_position_link)
//!         .add_vao_link(VaoId)
//!         .set_render_callback(render_callback);
//!
//!     let renderer = render_builder
//!         .build_renderer()
//!         .expect("Renderer should successfully build");
//!
//!     renderer.render();
//!
//!     Ok(())
//! }
//! ```
//!
//! # Demos
//!
//! todo
//!
//! ## Future Work
//!
//! Currently, wrend only supports build pipelines where all resources are initialized up front.
//! That is, no *new* textures, buffers, uniforms can be added after the pipeline has been initialized.

mod animation;
mod attributes;
mod buffers;
mod callbacks;
mod constants;
mod framebuffers;
mod ids;
mod math;
mod programs;
mod recording;
mod renderer_data;
mod renderers;
mod shaders;
mod textures;
mod transform_feedback;
mod types;
mod uniforms;
mod utils;

pub(crate) use recording::*;

pub use animation::*;
pub use attributes::*;
pub use buffers::*;
pub use callbacks::*;
pub use constants::*;
pub use framebuffers::*;
pub use ids::*;
pub use math::*;
pub use programs::*;
pub use renderer_data::*;
pub use renderers::*;
pub use shaders::*;
pub use textures::*;
pub use transform_feedback::*;
pub use types::*;
pub use uniforms::*;
pub use utils::*;