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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
use super::{WebGlCommon, WebGlRenderer};
use crate::errors::{Error, NativeError};
use std::marker::PhantomData;
use web_sys::{WebGl2RenderingContext, WebGlRenderingContext};
use web_sys::{WebGlProgram, WebGlUniformLocation};

pub enum UniformType {
    Scalar1,
    Scalar2,
    Scalar3,
    Scalar4,
    Vector1,
    Vector2,
    Vector3,
    Vector4,
    Matrix2,
    Matrix3,
    Matrix4,
    MatrixTransposed2,
    MatrixTransposed3,
    MatrixTransposed4,
}

pub trait PartialWebGlUniforms {
    fn awsm_get_uniform_location(
        &self,
        program: &WebGlProgram,
        name: &str,
    ) -> Result<WebGlUniformLocation, Error>;
    fn awsm_upload_uniform_fvec<T: AsRef<[f32]>>(
        &self,
        loc: &WebGlUniformLocation,
        _type: UniformType,
        data: &T,
    ) -> Result<(), Error>;
    fn awsm_upload_uniform_ivec<T: AsRef<[i32]>>(
        &self,
        loc: &WebGlUniformLocation,
        _type: UniformType,
        data: &T,
    ) -> Result<(), Error>;

    fn awsm_uniform1f(&self, loc: &WebGlUniformLocation, x: f32);
    fn awsm_uniform2f(&self, loc: &WebGlUniformLocation, x: f32, y: f32);
    fn awsm_uniform3f(&self, loc: &WebGlUniformLocation, x: f32, y: f32, z: f32);
    fn awsm_uniform4f(&self, loc: &WebGlUniformLocation, x: f32, y: f32, z: f32, w: f32);

    fn awsm_uniform1fv_with_f32_array(&self, loc: &WebGlUniformLocation, data: &[f32]);
    fn awsm_uniform2fv_with_f32_array(&self, loc: &WebGlUniformLocation, data: &[f32]);
    fn awsm_uniform3fv_with_f32_array(&self, loc: &WebGlUniformLocation, data: &[f32]);
    fn awsm_uniform4fv_with_f32_array(&self, loc: &WebGlUniformLocation, data: &[f32]);

    fn awsm_uniform1i(&self, loc: &WebGlUniformLocation, x: i32);
    fn awsm_uniform2i(&self, loc: &WebGlUniformLocation, x: i32, y: i32);
    fn awsm_uniform3i(&self, loc: &WebGlUniformLocation, x: i32, y: i32, z: i32);
    fn awsm_uniform4i(&self, loc: &WebGlUniformLocation, x: i32, y: i32, z: i32, w: i32);

    fn awsm_uniform1iv_with_i32_array(&self, loc: &WebGlUniformLocation, data: &[i32]);
    fn awsm_uniform2iv_with_i32_array(&self, loc: &WebGlUniformLocation, data: &[i32]);
    fn awsm_uniform3iv_with_i32_array(&self, loc: &WebGlUniformLocation, data: &[i32]);
    fn awsm_uniform4iv_with_i32_array(&self, loc: &WebGlUniformLocation, data: &[i32]);

    fn awsm_uniform_matrix2fv_with_f32_array(
        &self,
        loc: &WebGlUniformLocation,
        transpose: bool,
        data: &[f32],
    );
    fn awsm_uniform_matrix3fv_with_f32_array(
        &self,
        loc: &WebGlUniformLocation,
        transpose: bool,
        data: &[f32],
    );
    fn awsm_uniform_matrix4fv_with_f32_array(
        &self,
        loc: &WebGlUniformLocation,
        transpose: bool,
        data: &[f32],
    );
}

pub trait PartialWebGl2Uniforms {
    fn awsm_upload_uniform_uvec<T: AsRef<[u32]>>(
        &self,
        loc: &WebGlUniformLocation,
        _type: UniformType,
        data: &T,
    ) -> Result<(), Error>;
    fn awsm_uniform1ui(&self, loc: &WebGlUniformLocation, x: u32);
    fn awsm_uniform2ui(&self, loc: &WebGlUniformLocation, x: u32, y: u32);
    fn awsm_uniform3ui(&self, loc: &WebGlUniformLocation, x: u32, y: u32, z: u32);
    fn awsm_uniform4ui(&self, loc: &WebGlUniformLocation, x: u32, y: u32, z: u32, w: u32);

    fn awsm_uniform1uiv_with_u32_array(&self, loc: &WebGlUniformLocation, data: &[u32]);
    fn awsm_uniform2uiv_with_u32_array(&self, loc: &WebGlUniformLocation, data: &[u32]);
    fn awsm_uniform3uiv_with_u32_array(&self, loc: &WebGlUniformLocation, data: &[u32]);
    fn awsm_uniform4uiv_with_u32_array(&self, loc: &WebGlUniformLocation, data: &[u32]);
}

macro_rules! impl_context {
    ($($type:ty { $($defs:tt)* })+) => {
        $(impl PartialWebGlUniforms for $type {

            fn awsm_get_uniform_location(&self, program:&WebGlProgram, name:&str) -> Result<WebGlUniformLocation, Error> {
                self.get_uniform_location(&program, &name)
                    .ok_or(Error::from(NativeError::UniformLocation(Some(name.to_owned()))))
            }

            fn awsm_upload_uniform_fvec<T: AsRef<[f32]>> (&self, loc:&WebGlUniformLocation, _type:UniformType, data:&T) -> Result<(), Error> {
                UniformSlice::new(data, _type).upload(self, &loc)
            }

            fn awsm_upload_uniform_ivec<T: AsRef<[i32]>> (&self, loc:&WebGlUniformLocation, _type:UniformType, data:&T) -> Result<(), Error> {
                UniformSlice::new(data, _type).upload(self, &loc)
            }


            fn awsm_uniform1f(&self, loc: &WebGlUniformLocation, x: f32) {
                self.uniform1f(Some(loc), x)
            }
            fn awsm_uniform2f(&self, loc: &WebGlUniformLocation, x: f32, y: f32) {
                self.uniform2f(Some(loc), x, y)
            }
            fn awsm_uniform3f(&self, loc: &WebGlUniformLocation, x: f32, y: f32, z: f32) {
                self.uniform3f(Some(loc), x, y, z)
            }
            fn awsm_uniform4f(&self, loc: &WebGlUniformLocation, x: f32, y: f32, z: f32, w: f32) {
                self.uniform4f(Some(loc), x, y, z, w)
            }

            fn awsm_uniform1fv_with_f32_array(&self, loc: &WebGlUniformLocation, data: &[f32]) {
                self.uniform1fv_with_f32_array(Some(loc), data)
            }
            fn awsm_uniform2fv_with_f32_array(&self, loc: &WebGlUniformLocation, data: &[f32]) {
                self.uniform2fv_with_f32_array(Some(loc), data)
            }
            fn awsm_uniform3fv_with_f32_array(&self, loc: &WebGlUniformLocation, data: &[f32]) {
                self.uniform3fv_with_f32_array(Some(loc), data)
            }
            fn awsm_uniform4fv_with_f32_array(&self, loc: &WebGlUniformLocation, data: &[f32]) {
                self.uniform4fv_with_f32_array(Some(loc), data)
            }


            fn awsm_uniform1i(&self, loc: &WebGlUniformLocation, x: i32) {
                self.uniform1i(Some(loc), x)
            }
            fn awsm_uniform2i(&self, loc: &WebGlUniformLocation, x: i32, y: i32) {
                self.uniform2i(Some(loc), x, y)
            }
            fn awsm_uniform3i(&self, loc: &WebGlUniformLocation, x: i32, y: i32, z: i32) {
                self.uniform3i(Some(loc), x, y, z)
            }
            fn awsm_uniform4i(&self, loc: &WebGlUniformLocation, x: i32, y: i32, z: i32, w: i32) {
                self.uniform4i(Some(loc), x, y, z, w)
            }

             /* TODO - followup with https://github.com/rustwasm/wasm-bindgen/pull/1539
             * When the i32 slices don't need mut anymore - get rid of making the mut clone
             */
            fn awsm_uniform1iv_with_i32_array(&self, loc: &WebGlUniformLocation, data: &[i32]) {
                self.uniform1iv_with_i32_array(Some(loc), data)
            }
            fn awsm_uniform2iv_with_i32_array(&self, loc: &WebGlUniformLocation, data: &[i32]) {
                self.uniform2iv_with_i32_array(Some(loc), data)
            }
            fn awsm_uniform3iv_with_i32_array(&self, loc: &WebGlUniformLocation, data: &[i32]) {
                self.uniform3iv_with_i32_array(Some(loc), data)
            }
            fn awsm_uniform4iv_with_i32_array(&self, loc: &WebGlUniformLocation, data: &[i32]) {
                self.uniform4iv_with_i32_array(Some(loc), data)
            }


            fn awsm_uniform_matrix2fv_with_f32_array(&self, loc: &WebGlUniformLocation, transpose: bool, data: &[f32]) {
                self.uniform_matrix2fv_with_f32_array(Some(loc), transpose, data);
            }
            fn awsm_uniform_matrix3fv_with_f32_array(&self, loc: &WebGlUniformLocation, transpose: bool, data: &[f32]) {
                self.uniform_matrix3fv_with_f32_array(Some(loc), transpose, data);
            }
            fn awsm_uniform_matrix4fv_with_f32_array(&self, loc: &WebGlUniformLocation, transpose: bool, data: &[f32]) {
                self.uniform_matrix4fv_with_f32_array(Some(loc), transpose, data);
            }
            $($defs)*
        })+
    };
}

impl_context! {
    WebGlRenderingContext{}
    WebGl2RenderingContext{}
}

impl PartialWebGl2Uniforms for WebGl2RenderingContext {
    fn awsm_upload_uniform_uvec<T: AsRef<[u32]>>(
        &self,
        loc: &WebGlUniformLocation,
        _type: UniformType,
        data: &T,
    ) -> Result<(), Error> {
        UniformSlice::new(data, _type).upload(self, &loc)
    }

    fn awsm_uniform1ui(&self, loc: &WebGlUniformLocation, x: u32) {
        self.uniform1ui(Some(loc), x)
    }
    fn awsm_uniform2ui(&self, loc: &WebGlUniformLocation, x: u32, y: u32) {
        self.uniform2ui(Some(loc), x, y)
    }
    fn awsm_uniform3ui(&self, loc: &WebGlUniformLocation, x: u32, y: u32, z: u32) {
        self.uniform3ui(Some(loc), x, y, z)
    }
    fn awsm_uniform4ui(&self, loc: &WebGlUniformLocation, x: u32, y: u32, z: u32, w: u32) {
        self.uniform4ui(Some(loc), x, y, z, w)
    }

    /* TODO - followup with https://github.com/rustwasm/wasm-bindgen/pull/1639
     * When the u32 slices don't need mut anymore - get rid of making the mut clone
     */
    fn awsm_uniform1uiv_with_u32_array(&self, loc: &WebGlUniformLocation, data: &[u32]) {
        let mut values = data.to_owned();
        self.uniform1uiv_with_u32_array(Some(loc), &mut values)
    }
    fn awsm_uniform2uiv_with_u32_array(&self, loc: &WebGlUniformLocation, data: &[u32]) {
        let mut values = data.to_owned();
        self.uniform2uiv_with_u32_array(Some(loc), &mut values)
    }
    fn awsm_uniform3uiv_with_u32_array(&self, loc: &WebGlUniformLocation, data: &[u32]) {
        let mut values = data.to_owned();
        self.uniform3uiv_with_u32_array(Some(loc), &mut values)
    }
    fn awsm_uniform4uiv_with_u32_array(&self, loc: &WebGlUniformLocation, data: &[u32]) {
        let mut values = data.to_owned();
        self.uniform4uiv_with_u32_array(Some(loc), &mut values)
    }
}
/*
 * The slice-based uploads are written as traits on this a newtype wrapper
 * in order to work with either f32 or i32 and still get simple checks
 *
 * There is no need to wrap the scalar versions because the only check
 * for those is the length, which is known at compile-time
 *
 * Realistically, the renderer's convenience functions provide more value
 * since they expand on this to also get the location by name and provide more wrappers
 *
 * Technique via https://users.rust-lang.org/t/different-impls-for-types-of-slices-and-arrays/29468
 * Playground proof-of-concept: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=19dc3ce889d1e7c8aedbd36ad45b8422
 * TODO
 * 1. followup with https://github.com/rustwasm/wasm-bindgen/pull/1539
 * When the i32 slices don't need mut anymore - simplify below
 *
 *
 */

pub struct UniformSlice<T, U> {
    values: T,
    _type: UniformType,
    phantom: PhantomData<U>,
}

impl<T: AsRef<[U]>, U> UniformSlice<T, U> {
    pub fn new(values: T, _type: UniformType) -> Self {
        Self {
            values,
            _type,
            phantom: PhantomData,
        }
    }
}

pub trait UniformUploadImpl {
    fn upload<T: PartialWebGlUniforms>(
        &self,
        gl: &T,
        loc: &WebGlUniformLocation,
    ) -> Result<(), Error>;
}

pub trait UniformUploadImpl2 {
    fn upload<T: PartialWebGl2Uniforms>(
        &self,
        gl: &T,
        loc: &WebGlUniformLocation,
    ) -> Result<(), Error>;
}

fn is_length_enough(len: usize, _type: &UniformType) -> Result<(), Error> {
    let min_length = match _type {
        UniformType::Scalar1 | UniformType::Vector1 => 1,
        UniformType::Scalar2 | UniformType::Vector2 => 2,
        UniformType::Scalar3 | UniformType::Vector3 => 3,
        UniformType::Scalar4
        | UniformType::Vector4
        | UniformType::Matrix2
        | UniformType::MatrixTransposed2 => 4,
        UniformType::Matrix3 | UniformType::MatrixTransposed3 => 9,
        UniformType::Matrix4 | UniformType::MatrixTransposed4 => 16,
    };

    if len >= min_length {
        Ok(())
    } else {
        Err(Error::from(NativeError::UniformSize))
    }
}

impl<T: AsRef<[f32]>> UniformUploadImpl for UniformSlice<T, f32> {
    fn upload<G: PartialWebGlUniforms>(
        &self,
        gl: &G,
        loc: &WebGlUniformLocation,
    ) -> Result<(), Error> {
        let values = self.values.as_ref();
        is_length_enough(values.len(), &self._type)?;

        match self._type {
            UniformType::Scalar1 => gl.awsm_uniform1f(loc, values[0]),
            UniformType::Scalar2 => gl.awsm_uniform2f(loc, values[0], values[1]),
            UniformType::Scalar3 => gl.awsm_uniform3f(loc, values[0], values[1], values[2]),
            UniformType::Scalar4 => {
                gl.awsm_uniform4f(loc, values[0], values[1], values[2], values[3])
            }

            UniformType::Vector1 => gl.awsm_uniform1fv_with_f32_array(loc, values),
            UniformType::Vector2 => gl.awsm_uniform2fv_with_f32_array(loc, values),
            UniformType::Vector3 => gl.awsm_uniform3fv_with_f32_array(loc, values),
            UniformType::Vector4 => gl.awsm_uniform4fv_with_f32_array(loc, values),

            UniformType::Matrix2 => gl.awsm_uniform_matrix2fv_with_f32_array(loc, false, values),
            UniformType::Matrix3 => gl.awsm_uniform_matrix3fv_with_f32_array(loc, false, values),
            UniformType::Matrix4 => gl.awsm_uniform_matrix4fv_with_f32_array(loc, false, values),

            UniformType::MatrixTransposed2 => {
                gl.awsm_uniform_matrix2fv_with_f32_array(loc, true, values)
            }
            UniformType::MatrixTransposed3 => {
                gl.awsm_uniform_matrix3fv_with_f32_array(loc, true, values)
            }
            UniformType::MatrixTransposed4 => {
                gl.awsm_uniform_matrix4fv_with_f32_array(loc, true, values)
            }
        };

        Ok(())
    }
}

impl<T: AsRef<[i32]>> UniformUploadImpl for UniformSlice<T, i32> {
    fn upload<G: PartialWebGlUniforms>(
        &self,
        gl: &G,
        loc: &WebGlUniformLocation,
    ) -> Result<(), Error> {
        let values = self.values.as_ref();
        is_length_enough(values.len(), &self._type)?;

        match self._type {
            UniformType::Scalar1 => gl.awsm_uniform1i(loc, values[0]),
            UniformType::Scalar2 => gl.awsm_uniform2i(loc, values[0], values[1]),
            UniformType::Scalar3 => gl.awsm_uniform3i(loc, values[0], values[1], values[2]),
            UniformType::Scalar4 => {
                gl.awsm_uniform4i(loc, values[0], values[1], values[2], values[3])
            }

            UniformType::Vector1 => gl.awsm_uniform1iv_with_i32_array(loc, values),
            UniformType::Vector2 => gl.awsm_uniform2iv_with_i32_array(loc, values),
            UniformType::Vector3 => gl.awsm_uniform3iv_with_i32_array(loc, values),
            UniformType::Vector4 => gl.awsm_uniform4iv_with_i32_array(loc, values),

            _ => return Err(Error::from(NativeError::UniformMatrixMustBeFloat)),
        };

        Ok(())
    }
}

impl<T: AsRef<[u32]>> UniformUploadImpl2 for UniformSlice<T, u32> {
    fn upload<G: PartialWebGl2Uniforms>(
        &self,
        gl: &G,
        loc: &WebGlUniformLocation,
    ) -> Result<(), Error> {
        let values = self.values.as_ref();
        is_length_enough(values.len(), &self._type)?;

        match self._type {
            UniformType::Scalar1 => gl.awsm_uniform1ui(loc, values[0]),
            UniformType::Scalar2 => gl.awsm_uniform2ui(loc, values[0], values[1]),
            UniformType::Scalar3 => gl.awsm_uniform3ui(loc, values[0], values[1], values[2]),
            UniformType::Scalar4 => {
                gl.awsm_uniform4ui(loc, values[0], values[1], values[2], values[3])
            }

            UniformType::Vector1 => gl.awsm_uniform1uiv_with_u32_array(loc, values),
            UniformType::Vector2 => gl.awsm_uniform2uiv_with_u32_array(loc, values),
            UniformType::Vector3 => gl.awsm_uniform3uiv_with_u32_array(loc, values),
            UniformType::Vector4 => gl.awsm_uniform4uiv_with_u32_array(loc, values),

            _ => return Err(Error::from(NativeError::UniformMatrixMustBeFloat)),
        };

        Ok(())
    }
}

//Renderer wrapper
//The uniform lookups are cached at shader compilation (see shader.rs)
impl<G: WebGlCommon> WebGlRenderer<G> {
    pub fn get_uniform_location_value(&self, name: &str) -> Result<WebGlUniformLocation, Error> {
        let program_id = self
            .current_program_id
            .ok_or(Error::from(NativeError::MissingShaderProgram))?;
        let program_info = self
            .program_lookup
            .get(program_id)
            .ok_or(Error::from(NativeError::MissingShaderProgram))?;

        program_info
            .uniform_lookup
            .get(name)
            .map(|v| v.clone())
            .ok_or_else(|| Error::from(NativeError::UniformLocation(Some(name.to_string()))))
    }

    //this covers all the slice-based versions due to the impl above

    //Just some convenience helpers
    pub fn upload_uniform_fvec<T: AsRef<[f32]>>(
        &self,
        target_name: &str,
        _type: UniformType,
        data: &T,
    ) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_upload_uniform_fvec(&loc, _type, data)
    }

    pub fn upload_uniform_ivec<T: AsRef<[i32]>>(
        &self,
        target_name: &str,
        _type: UniformType,
        data: &T,
    ) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_upload_uniform_ivec(&loc, _type, data)
    }

    pub fn upload_uniform_mat_4<T: AsRef<[f32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_fvec(target_name, UniformType::Matrix4, &data)
    }
    pub fn upload_uniform_mat_3<T: AsRef<[f32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_fvec(target_name, UniformType::Matrix3, data)
    }
    pub fn upload_uniform_mat_2<T: AsRef<[f32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_fvec(target_name, UniformType::Matrix2, data)
    }
    pub fn upload_uniform_mat_transposed_4<T: AsRef<[f32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_fvec(target_name, UniformType::MatrixTransposed4, data)
    }
    pub fn upload_uniform_mat_transposed_3<T: AsRef<[f32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_fvec(target_name, UniformType::MatrixTransposed3, data)
    }
    pub fn upload_uniform_mat_transposed_2<T: AsRef<[f32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_fvec(target_name, UniformType::MatrixTransposed2, data)
    }

    pub fn upload_uniform_fvec_4<T: AsRef<[f32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_fvec(target_name, UniformType::Vector4, data)
    }
    pub fn upload_uniform_fvec_3<T: AsRef<[f32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_fvec(target_name, UniformType::Vector3, data)
    }
    pub fn upload_uniform_fvec_2<T: AsRef<[f32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_fvec(target_name, UniformType::Vector2, data)
    }
    pub fn upload_uniform_fvec_1<T: AsRef<[f32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_fvec(target_name, UniformType::Vector1, data)
    }

    pub fn upload_uniform_ivec_4<T: AsRef<[i32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_ivec(target_name, UniformType::Vector4, data)
    }
    pub fn upload_uniform_ivec_3<T: AsRef<[i32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_ivec(target_name, UniformType::Vector3, data)
    }
    pub fn upload_uniform_ivec_2<T: AsRef<[i32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_ivec(target_name, UniformType::Vector2, data)
    }
    pub fn upload_uniform_ivec_1<T: AsRef<[i32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_ivec(target_name, UniformType::Vector1, data)
    }

    //Scalar versions - only need "convenience" form with string because if the caller
    //already knows the location, there's no reason to just use the context directly

    pub fn upload_uniform_fvals_4(
        &self,
        target_name: &str,
        data: (f32, f32, f32, f32),
    ) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_uniform4f(&loc, data.0, data.1, data.2, data.3);
        Ok(())
    }
    pub fn upload_uniform_fvals_3(
        &self,
        target_name: &str,
        data: (f32, f32, f32),
    ) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_uniform3f(&loc, data.0, data.1, data.2);
        Ok(())
    }
    pub fn upload_uniform_fvals_2(&self, target_name: &str, data: (f32, f32)) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_uniform2f(&loc, data.0, data.1);
        Ok(())
    }
    pub fn upload_uniform_fval(&self, target_name: &str, data: f32) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_uniform1f(&loc, data);
        Ok(())
    }

    pub fn upload_uniform_ivals_4(
        &self,
        target_name: &str,
        data: (i32, i32, i32, i32),
    ) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_uniform4i(&loc, data.0, data.1, data.2, data.3);
        Ok(())
    }
    pub fn upload_uniform_ivals_3(
        &self,
        target_name: &str,
        data: (i32, i32, i32),
    ) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_uniform3i(&loc, data.0, data.1, data.2);
        Ok(())
    }
    pub fn upload_uniform_ivals_2(&self, target_name: &str, data: (i32, i32)) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_uniform2i(&loc, data.0, data.1);
        Ok(())
    }
    pub fn upload_uniform_ival(&self, target_name: &str, data: i32) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_uniform1i(&loc, data);
        Ok(())
    }
}

impl WebGlRenderer<WebGl2RenderingContext> {
    pub fn upload_uniform_uvec<T: AsRef<[u32]>>(
        &self,
        target_name: &str,
        _type: UniformType,
        data: &T,
    ) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_upload_uniform_uvec(&loc, _type, data)
    }
    pub fn upload_uniform_uvec_4<T: AsRef<[u32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_uvec(target_name, UniformType::Vector4, data)
    }
    pub fn upload_uniform_uvec_3<T: AsRef<[u32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_uvec(target_name, UniformType::Vector3, data)
    }
    pub fn upload_uniform_uvec_2<T: AsRef<[u32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_uvec(target_name, UniformType::Vector2, data)
    }
    pub fn upload_uniform_uvec_1<T: AsRef<[u32]>>(
        &self,
        target_name: &str,
        data: &T,
    ) -> Result<(), Error> {
        self.upload_uniform_uvec(target_name, UniformType::Vector1, data)
    }

    pub fn upload_uniform_uvals_4(
        &self,
        target_name: &str,
        data: (u32, u32, u32, u32),
    ) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl
            .awsm_uniform4ui(&loc, data.0, data.1, data.2, data.3);
        Ok(())
    }
    pub fn upload_uniform_uvals_3(
        &self,
        target_name: &str,
        data: (u32, u32, u32),
    ) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_uniform3ui(&loc, data.0, data.1, data.2);
        Ok(())
    }
    pub fn upload_uniform_uvals_2(&self, target_name: &str, data: (u32, u32)) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_uniform2ui(&loc, data.0, data.1);
        Ok(())
    }
    pub fn upload_uniform_uval(&self, target_name: &str, data: u32) -> Result<(), Error> {
        let loc = self.get_uniform_location_value(&target_name)?;
        self.gl.awsm_uniform1ui(&loc, data);
        Ok(())
    }
}