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
use gl::{self, types::*};
#[cfg(not(feature="webgl"))]
use texture::{Component, Swizzles};
use state::StateRef;

/// Wraps an OpenGL sampler
#[derive(Debug)]
pub struct Sampler{
    id: GLuint,
    gl: StateRef,
}

impl Drop for Sampler{
    fn drop(&mut self){
        unsafe{
            self.gl.DeleteSamplers(1, &self.id)
        }
    }
}

impl Sampler{
    pub(crate) fn new(gl: &StateRef) -> Sampler{
        let mut id = 0;
        unsafe{
            gl.GenSamplers(1, &mut id);
        }
        Sampler{id, gl: gl.clone()}
    }

    pub fn id(&self) -> GLuint {
        self.id
    }

    /// Swizzle r -> component
    #[cfg(not(feature="webgl"))]
    pub fn set_swizzle_r(&mut self, component: Component){
        unsafe{
            self.set_parameteri(gl::TEXTURE_SWIZZLE_R, component.to_dst() as GLint);
        }
    }

    /// Swizzle g -> component
    #[cfg(not(feature="webgl"))]
    pub fn set_swizzle_g(&mut self, component: Component){
        unsafe{
            self.set_parameteri(gl::TEXTURE_SWIZZLE_G, component.to_dst() as GLint);
        }
    }

    /// Swizzle b -> component
    #[cfg(not(feature="webgl"))]
    pub fn set_swizzle_b(&mut self, component: Component){
        unsafe{
            self.set_parameteri(gl::TEXTURE_SWIZZLE_B, component.to_dst() as GLint);
        }
    }

    /// Swizzle a -> component
    #[cfg(not(feature="webgl"))]
    pub fn set_swizzle_a(&mut self, component: Component){
        unsafe{
            self.set_parameteri(gl::TEXTURE_SWIZZLE_A, component.to_dst() as GLint);
        }
    }

    /// Sets a group of Swizzles
    #[cfg(not(feature="webgl"))]
    pub fn set_swizzles(&mut self, swizzles: Swizzles){
        self.set_swizzle_r(swizzles.r);
        self.set_swizzle_g(swizzles.g);
        self.set_swizzle_b(swizzles.b);
        self.set_swizzle_a(swizzles.a);
    }

    /// GL_TEXTURE_MAX_ANISOTROPY_EXT
    #[cfg(not(feature="webgl"))]
    pub fn set_anisotropy_level(&mut self, level: f32){
        unsafe{
            self.set_parameterf(gl::TEXTURE_MAX_ANISOTROPY_EXT, level);
        }
    }

    /// GL_TEXTURE_BASE_LEVEL
    pub fn set_base_level(&mut self, level: GLint){
        unsafe{
            self.set_parameteri(gl::TEXTURE_BASE_LEVEL, level);
        }
    }

    /// GL_TEXTURE_MAX_LEVEL
    pub fn set_max_level(&mut self, level: GLint){
        unsafe{
            self.set_parameteri(gl::TEXTURE_MAX_LEVEL, level);
        }
    }

    /// GL_TEXTURE_MIN_LOD
    pub fn set_min_lod(&mut self, level: GLint){
        unsafe{
            self.set_parameteri(gl::TEXTURE_MIN_LOD, level);
        }
    }

    /// GL_TEXTURE_MAX_LOD
    pub fn set_max_lod(&mut self, level: GLint){
        unsafe{
            self.set_parameteri(gl::TEXTURE_MAX_LOD, level);
        }
    }

    /// GL_TEXTURE_MIN_MAG_FILTERS
    pub fn set_min_mag_filters(&mut self, min: GLuint, mag: GLuint){
        unsafe{
            self.set_parameteri(gl::TEXTURE_MIN_FILTER, min as GLint);
            self.set_parameteri(gl::TEXTURE_MAG_FILTER, mag as GLint);
        }
    }

    /// GL_TEXTURE_WRAP_S
    pub fn set_wrap_s(&mut self, wrap: GLenum){
        unsafe{
            self.set_parameteri(gl::TEXTURE_WRAP_S, wrap as GLint);
        }
    }

    /// GL_TEXTURE_WRAP_T
    pub fn set_wrap_t(&mut self, wrap: GLenum){
        unsafe{
            self.set_parameteri(gl::TEXTURE_WRAP_T, wrap as GLint);
        }
    }

    /// GL_TEXTURE_WRAP_R
    pub fn set_wrap_r(&mut self, wrap: GLenum){
        unsafe{
            self.set_parameteri(gl::TEXTURE_WRAP_R, wrap as GLint);
        }
    }

    pub fn set_compare_mode(&mut self, mode: GLenum){
        unsafe{
            self.set_parameteri(gl::TEXTURE_COMPARE_MODE, mode as GLint)
        }
    }

    pub fn set_compare_func(&mut self, mode: GLenum){
        unsafe{
            self.set_parameteri(gl::TEXTURE_COMPARE_FUNC, mode as GLint)
        }
    }

    /// GL_TEXTURE_BORDER_COLOR
    #[cfg(not(feature="webgl"))]
    pub fn set_border_color(&mut self, color: &[f32;4]){
        unsafe{
            self.set_parameterfv(gl::TEXTURE_BORDER_COLOR, color);
        }
    }

    // /// GL_TEXTURE_WIDTH
    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // pub fn level_width(&self, level: u32) -> u32{
    //     unsafe{
    //         self.get_level_parameteriv(gl::TEXTURE_WIDTH, level) as u32
    //     }
    // }

    // /// GL_TEXTURE_HEIGHT
    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // pub fn level_height(&self, level: u32) -> u32{
    //     unsafe{
    //         self.get_level_parameteriv(gl::TEXTURE_HEIGHT, level) as u32
    //     }
    // }

    // /// GL_TEXTURE_INTERNAL_FORMAT
    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // pub fn level_internal_format(&self, level: u32) -> GLenum{
    //     unsafe{
    //         self.get_level_parameteriv(gl::TEXTURE_INTERNAL_FORMAT, level) as GLenum
    //     }
    // }

    // /// GL_TEXTURE_RED_TYPE
    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // pub fn level_red_type(&self, level: u32) -> GLenum{
    //     unsafe{
    //         self.get_level_parameteriv(gl::TEXTURE_RED_TYPE, level) as GLenum
    //     }
    // }

    // /// GL_TEXTURE_GREEN_TYPE
    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // pub fn level_green_type(&self, level: u32) -> GLenum{
    //     unsafe{
    //         self.get_level_parameteriv(gl::TEXTURE_GREEN_TYPE, level) as GLenum
    //     }
    // }

    // /// GL_TEXTURE_BLUE_TYPE
    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // pub fn level_blue_type(&self, level: u32) -> GLenum{
    //     unsafe{
    //         self.get_level_parameteriv(gl::TEXTURE_BLUE_TYPE, level) as GLenum
    //     }
    // }

    // /// GL_TEXTURE_ALPHA_TYPE
    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // pub fn level_alpha_type(&self, level: u32) -> GLenum{
    //     unsafe{
    //         self.get_level_parameteriv(gl::TEXTURE_ALPHA_TYPE, level) as GLenum
    //     }
    // }

    // /// GL_TEXTURE_COMPRESSED
    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // pub fn is_level_compressed(&self, level: u32) -> bool{
    //     unsafe{
    //         self.get_level_parameteriv(gl::TEXTURE_COMPRESSED, level)!=0
    //     }
    // }

    // /// GL_TEXTURE_COMPRESSED_IMAGE_SIZE
    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // pub fn level_compressed_image_len(&self, level: u32) -> usize{
    //     unsafe{
    //         self.get_level_parameteriv(gl::TEXTURE_COMPRESSED_IMAGE_SIZE, level) as usize
    //     }
    // }

    // /// GL_TEXTURE_RED_SIZE
    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // pub fn level_red_size(&self, level: u32) -> usize{
    //     unsafe{
    //         self.get_level_parameteriv(gl::TEXTURE_RED_SIZE, level) as usize
    //     }
    // }

    // /// GL_TEXTURE_GREEN_SIZE
    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // pub fn level_green_size(&self, level: u32) -> usize{
    //     unsafe{
    //         self.get_level_parameteriv(gl::TEXTURE_GREEN_SIZE, level) as usize
    //     }
    // }

    // /// GL_TEXTURE_BLUE_SIZE
    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // pub fn level_blue_size(&self, level: u32) -> usize{
    //     unsafe{
    //         self.get_level_parameteriv(gl::TEXTURE_BLUE_SIZE, level) as usize
    //     }
    // }

    // /// GL_TEXTURE_ALPHA_SIZE
    // #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    // pub fn level_alpha_size(&self, level: u32) -> usize{
    //     unsafe{
    //         self.get_level_parameteriv(gl::TEXTURE_ALPHA_SIZE, level) as usize
    //     }
    // }

    /// GL_DEPTH_STENCIL_TEXTURE_MODE
    #[cfg(not(feature="webgl"))]
    pub fn depth_stencil_texture_mode(&self) -> GLenum{
        unsafe{
            self.get_parameteri(gl::DEPTH_STENCIL_TEXTURE_MODE) as GLenum
        }
    }

    /// GL_TEXTURE_MAG_FILTER
    pub fn mag_filter(&self) -> GLenum{
        unsafe{
            self.get_parameteri(gl::TEXTURE_MAG_FILTER) as GLenum
        }
    }

    /// GL_TEXTURE_MIN_FILTER
    pub fn min_filter(&self) -> GLenum{
        unsafe{
            self.get_parameteri(gl::TEXTURE_MIN_FILTER) as GLenum
        }

    }

    /// GL_TEXTURE_MIN_LOD
    pub fn min_lod(&self) -> i32{
        unsafe{
            self.get_parameteri(gl::TEXTURE_MIN_LOD)
        }
    }

    /// GL_TEXTURE_MAX_LOD
    pub fn max_lod(&self) -> i32{
        unsafe{
            self.get_parameteri(gl::TEXTURE_MAX_LOD)
        }
    }

    /// GL_TEXTURE_BASE_LEVEL
    pub fn base_level(&self) -> i32{
        unsafe{
            self.get_parameteri(gl::TEXTURE_BASE_LEVEL)
        }
    }

    /// GL_TEXTURE_MAX_LEVEL
    pub fn max_level(&self) -> i32{
        unsafe{
            self.get_parameteri(gl::TEXTURE_MAX_LEVEL)
        }
    }

    /// GL_TEXTURE_SWIZZLE_R
    #[cfg(not(feature="webgl"))]
    pub fn swizzle_r(&self) -> Component{
        unsafe{
            let dst = self.get_parameteri(gl::TEXTURE_SWIZZLE_R);
            Component::from_dst(dst as GLenum).unwrap()
        }
    }

    /// GL_TEXTURE_SWIZZLE_G
    #[cfg(not(feature="webgl"))]
    pub fn swizzle_g(&self) -> Component{
        unsafe{
            let dst = self.get_parameteri(gl::TEXTURE_SWIZZLE_G);
            Component::from_dst(dst as GLenum).unwrap()
        }
    }

    /// GL_TEXTURE_SWIZZLE_B
    #[cfg(not(feature="webgl"))]
    pub fn swizzle_b(&self) -> Component{
        unsafe{
            let dst = self.get_parameteri(gl::TEXTURE_SWIZZLE_B);
            Component::from_dst(dst as GLenum).unwrap()
        }
    }

    /// GL_TEXTURE_SWIZZLE_A
    #[cfg(not(feature="webgl"))]
    pub fn swizzle_a(&self) -> Component{
        unsafe{
            let dst = self.get_parameteri(gl::TEXTURE_SWIZZLE_A);
            Component::from_dst(dst as GLenum).unwrap()
        }
    }

    /// GL_TEXTURE_SWIZZLE_RGBA
    #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    pub fn swizzles(&self) -> Swizzles{
        let rgba = unsafe{
            self.get_parameterfv(gl::TEXTURE_SWIZZLE_RGBA, 4)
        };

        Swizzles{
            r: Component::from_dst(rgba[0] as GLenum).unwrap(),
            g: Component::from_dst(rgba[1] as GLenum).unwrap(),
            b: Component::from_dst(rgba[2] as GLenum).unwrap(),
            a: Component::from_dst(rgba[3] as GLenum).unwrap(),
        }
    }

    /// GL_TEXTURE_SWIZZLE_RGBA
    #[cfg(feature = "gles")]
    pub fn swizzles(&self) -> Swizzles{
        let r;
        let g;
        let b;
        let a;
        unsafe{
            r = self.get_parameterf(gl::TEXTURE_SWIZZLE_R);
            g = self.get_parameterf(gl::TEXTURE_SWIZZLE_G);
            b = self.get_parameterf(gl::TEXTURE_SWIZZLE_B);
            a = self.get_parameterf(gl::TEXTURE_SWIZZLE_A);
        }

        Swizzles{
            r: Component::from_dst(r as GLenum).unwrap(),
            g: Component::from_dst(g as GLenum).unwrap(),
            b: Component::from_dst(b as GLenum).unwrap(),
            a: Component::from_dst(a as GLenum).unwrap(),
        }
    }

    /// GL_TEXTURE_WRAP_S
    pub fn wrap_s(&self) -> GLenum{
        unsafe{
            self.get_parameteri(gl::TEXTURE_WRAP_S) as GLenum
        }
    }

    /// GL_TEXTURE_WRAP_T
    pub fn wrap_t(&self) -> GLenum{
        unsafe{
            self.get_parameteri(gl::TEXTURE_WRAP_T) as GLenum
        }
    }

    /// GL_TEXTURE_WRAP_R
    pub fn wrap_r(&self) -> GLenum{
        unsafe{
            self.get_parameteri(gl::TEXTURE_WRAP_R) as GLenum
        }
    }

    /// GL_TEXTURE_BORDER_COLOR
    #[cfg(not(feature="webgl"))]
    pub fn border_color(&self) -> [f32;4]{
        unsafe{
            let vector = self.get_parameterfv(gl::TEXTURE_BORDER_COLOR, 4);
            let mut arr = [0f32; 4];
            arr.copy_from_slice(&vector);
            arr
        }
    }

    /// GL_TEXTURE_COMPARE_MODE
    pub fn compare_mode(&self) -> GLenum{
        unsafe{
            self.get_parameteri(gl::TEXTURE_COMPARE_MODE) as GLenum
        }
    }

    /// GL_TEXTURE_COMPARE_FUNC
    pub fn compare_func(&self) -> GLenum{
        unsafe{
            self.get_parameteri(gl::TEXTURE_COMPARE_FUNC) as GLenum
        }
    }

    /// GL_IMAGE_FORMAT_COMPATIBILITY_TYPE
    #[cfg(not(feature="webgl"))]
    pub fn image_format_compatibility_type(&self) -> GLenum{
        unsafe{
            self.get_parameteri(gl::IMAGE_FORMAT_COMPATIBILITY_TYPE) as GLenum
        }
    }

    #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    pub fn seamless(&self) -> bool {
        unsafe{
            self.get_parameteri(gl::TEXTURE_CUBE_MAP_SEAMLESS) != 0
        }
    }

    #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    pub fn set_seamless(&mut self, seamless: bool) {
        unsafe{
            self.set_parameteri(gl::TEXTURE_CUBE_MAP_SEAMLESS, seamless as u8 as i32)
        }
    }

    pub unsafe fn set_parameteri(&mut self, parameter: GLenum, value: GLint) {
        #[cfg(target_os="emscripten")]
        {
            extern "C"{
                fn emscripten_glSamplerParameteri(id: GLuint, parameter: GLenum, value: GLint);
            }
            emscripten_glSamplerParameteri(self.id, parameter, value)
        }

        #[cfg(not(target_os="emscripten"))]
        {
            self.gl.SamplerParameteri(self.id, parameter, value)
        }
    }

    pub unsafe fn set_parameterf(&mut self, parameter: GLenum, value: GLfloat) {
        #[cfg(target_os="emscripten")]
        {
            extern "C"{
                fn emscripten_glSamplerParameterf(id: GLuint, parameter: GLenum, value: GLfloat);
            }
            emscripten_glSamplerParameterf(self.id, parameter, value)
        }

        #[cfg(not(target_os="emscripten"))]
        {
            self.gl.SamplerParameterf(self.id, parameter, value)
        }
    }

    pub unsafe fn set_parameteriv(&mut self, parameter: GLenum, value: &[GLint]) {
        #[cfg(target_os="emscripten")]
        {
            extern "C"{
                fn emscripten_glSamplerParameteriv(id: GLuint, parameter: GLenum, value: *const GLint);
            }
            emscripten_glSamplerParameteriv(self.id, parameter, value.as_ptr())
        }

        #[cfg(not(target_os="emscripten"))]
        {
            self.gl.SamplerParameteriv(self.id, parameter, value.as_ptr())
        }
    }

    pub unsafe fn set_parameterfv(&mut self, parameter: GLenum, value: &[GLfloat]) {
        #[cfg(target_os="emscripten")]
        {
            extern "C"{
                fn emscripten_glSamplerParameterfv(id: GLuint, parameter: GLenum, value: *const GLfloat);
            }
            emscripten_glSamplerParameterfv(self.id, parameter, value.as_ptr())
        }

        #[cfg(not(target_os="emscripten"))]
        {
            self.gl.SamplerParameterfv(self.id, parameter, value.as_ptr())
        }
    }

    pub unsafe fn get_parameteri(&self, parameter: GLenum) -> GLint {
        let mut value = 0;

        #[cfg(target_os="emscripten")]
        {
            extern "C"{
                fn emscripten_glGetSamplerParameteriv(id: GLuint, parameter: GLenum, value: *const GLint);
            }
            emscripten_glGetSamplerParameteriv(self.id, parameter, &mut value)
        }

        #[cfg(not(target_os="emscripten"))]
        {
            self.gl.GetSamplerParameteriv(self.id, parameter, &mut value)
        }

        value
    }

    pub unsafe fn get_parameterf(&self, parameter: GLenum) -> GLfloat {
        let mut value = 0.;

        #[cfg(target_os="emscripten")]
        {
            extern "C"{
                fn emscripten_glGetSamplerParameterfv(id: GLuint, parameter: GLenum, value: *const GLfloat);
            }
            emscripten_glGetSamplerParameterfv(self.id, parameter, &mut value)
        }

        #[cfg(not(target_os="emscripten"))]
        {
            self.gl.GetSamplerParameterfv(self.id, parameter, &mut value)
        }

        value
    }

    pub unsafe fn get_parameteriv(&self, parameter: GLenum, len: usize) -> Vec<GLint> {
        let mut value = vec![0;len];

        #[cfg(target_os="emscripten")]
        {
            extern "C"{
                fn emscripten_glGetSamplerParameteriv(id: GLuint, parameter: GLenum, value: *const GLint);
            }
            emscripten_glGetSamplerParameteriv(self.id, parameter, value.as_mut_ptr())
        }

        #[cfg(not(target_os="emscripten"))]
        {
            self.gl.GetSamplerParameteriv(self.id, parameter, value.as_mut_ptr())
        }

        value
    }

    pub unsafe fn get_parameterfv(&self, parameter: GLenum, len: usize) -> Vec<GLfloat> {
        let mut value = vec![0.;len];

        #[cfg(target_os="emscripten")]
        {
            extern "C"{
                fn emscripten_glGetSamplerParameterfv(id: GLuint, parameter: GLenum, value: *const GLfloat);
            }
            emscripten_glGetSamplerParameterfv(self.id, parameter, value.as_mut_ptr())
        }

        #[cfg(not(target_os="emscripten"))]
        {
            self.gl.GetSamplerParameterfv(self.id, parameter, value.as_mut_ptr())
        }

        value
    }
}