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
//! OpenGL back-end for Piston-Graphics.

// External crates.
use std::ffi::CString;
use shader_version::{OpenGL, Shaders};
use shader_version::glsl::GLSL;
use graphics::{Context, DrawState, Graphics, Viewport};
use graphics::color::gamma_srgb_to_linear;
use graphics::BACK_END_MAX_VERTEX_COUNT as BUFFER_SIZE;
use gl;
use gl::types::{GLint, GLsizei, GLuint};

// Local crate.
use draw_state;
use Texture;
use shader_utils::{compile_shader, DynamicAttribute};

// The number of chunks to fill up before rendering.
// Amount of memory used: `BUFFER_SIZE * CHUNKS * 4 * (2 + 4)`
// `4` for bytes per f32, and `2 + 4` for position and color.
const CHUNKS: usize = 100;

// Whether to use WebGL-specific features; currently used to select appropriate shaders.
const USE_WEBGL: bool = cfg!(target_os = "emscripten") || cfg!(feature = "webgl");

/// Describes how to render colored objects.
pub struct Colored {
    vao: GLuint,
    vertex_shader: GLuint,
    fragment_shader: GLuint,
    program: GLuint,
    pos: DynamicAttribute,
    color: DynamicAttribute,
    pos_buffer: Vec<[f32; 2]>,
    color_buffer: Vec<[f32; 4]>,
    offset: usize,
}

impl Drop for Colored {
    fn drop(&mut self) {
        unsafe {
            gl::DeleteVertexArrays(1, &self.vao);
            gl::DeleteProgram(self.program);
            gl::DeleteShader(self.vertex_shader);
            gl::DeleteShader(self.fragment_shader);
        }
    }
}

impl Colored {
    /// Generate using pass-through shaders.
    ///
    /// # Panics
    /// If the default pass-through shaders fail to compile
    pub fn new(glsl: GLSL) -> Self {
        use shaders::colored;
        let src = |bytes| unsafe { ::std::str::from_utf8_unchecked(bytes) };

        let mut vertex_shaders = Shaders::new();
        if USE_WEBGL {
            vertex_shaders
               .set(GLSL::V1_20, src(colored::VERTEX_GLSL_120_WEBGL))
               .set(GLSL::V1_50, src(colored::VERTEX_GLSL_150_CORE_WEBGL))
        } else {
            vertex_shaders
               .set(GLSL::V1_20, src(colored::VERTEX_GLSL_120))
               .set(GLSL::V1_50, src(colored::VERTEX_GLSL_150_CORE))
        };

        let mut fragment_shaders = Shaders::new();
        if USE_WEBGL {
            fragment_shaders
               .set(GLSL::V1_20, src(colored::FRAGMENT_GLSL_120_WEBGL))
               .set(GLSL::V1_50, src(colored::FRAGMENT_GLSL_150_CORE_WEBGL))
        } else {
           fragment_shaders
               .set(GLSL::V1_20, src(colored::FRAGMENT_GLSL_120))
               .set(GLSL::V1_50, src(colored::FRAGMENT_GLSL_150_CORE))
        };

        Colored::from_vs_fs(glsl, &vertex_shaders, &fragment_shaders).unwrap()
    }

    /// Generate using custom vertex and fragment shaders.
    pub fn from_vs_fs(glsl: GLSL, vertex_shaders   : &Shaders<GLSL, str>,
                                  fragment_shaders : &Shaders<GLSL, str>)
            -> Result<Self, String> {

        let v_shader = vertex_shaders.get(glsl)
            .ok_or("No compatible vertex shader")?;

        let v_shader_compiled =
            compile_shader(gl::VERTEX_SHADER, v_shader)
            .map_err(|s| format!("Error compiling vertex shader: {}", s))?;

        let f_shader = fragment_shaders.get(glsl)
            .ok_or("No compatible fragment shader")?;

        let f_shader_compiled =
            compile_shader(gl::FRAGMENT_SHADER, f_shader)
            .map_err(|s| format!("Error compiling fragment shader: {}", s))?;

        let program;
        unsafe {
            program = gl::CreateProgram();
            gl::AttachShader(program, v_shader_compiled);
            gl::AttachShader(program, f_shader_compiled);

            let c_o_color = CString::new("o_Color").unwrap();
            if !USE_WEBGL {
                gl::BindFragDataLocation(program, 0, c_o_color.as_ptr());
            }
            drop(c_o_color);
        }

        let mut vao = 0;
        unsafe {
            gl::GenVertexArrays(1, &mut vao);
            gl::LinkProgram(program);
        }
        let pos = DynamicAttribute::xy(program, "pos", vao).unwrap();
        let color = DynamicAttribute::rgba(program, "color", vao).unwrap();
        Ok(Colored {
            vao: vao,
            vertex_shader: v_shader_compiled,
            fragment_shader: f_shader_compiled,
            program: program,
            pos: pos,
            color: color,
            pos_buffer: vec![[0.0; 2]; CHUNKS * BUFFER_SIZE],
            color_buffer: vec![[0.0; 4]; CHUNKS * BUFFER_SIZE],
            offset: 0,
        })

    }

    fn flush(&mut self) {
        unsafe {
            gl::BindVertexArray(self.vao);
            // Render triangles whether they are facing
            // clockwise or counter clockwise.
            gl::Disable(gl::CULL_FACE);
            self.color.set(&self.color_buffer[..self.offset]);
            self.pos.set(&self.pos_buffer[..self.offset]);
            gl::DrawArrays(gl::TRIANGLES, 0, self.offset as i32);
            gl::BindVertexArray(0);
        }

        self.offset = 0;
    }
}

/// Describes how to render textured objects.
pub struct Textured {
    vertex_shader: GLuint,
    fragment_shader: GLuint,
    program: GLuint,
    vao: GLuint,
    color: GLint,
    pos: DynamicAttribute,
    uv: DynamicAttribute,
    pos_buffer: Vec<[f32; 2]>,
    uv_buffer: Vec<[f32; 2]>,
    offset: usize,
    last_texture_id: GLuint,
    last_color: [f32; 4],
}

impl Drop for Textured {
    fn drop(&mut self) {
        unsafe {
            gl::DeleteVertexArrays(1, &self.vao);
            gl::DeleteProgram(self.program);
            gl::DeleteShader(self.vertex_shader);
            gl::DeleteShader(self.fragment_shader);
        }
    }
}

impl Textured {
    /// Generate using pass-through shaders.
    ///
    /// # Panics
    /// If the default pass-through shaders fail to compile
    pub fn new(glsl: GLSL) -> Self {
        use shaders::textured;
        let src = |bytes| unsafe { ::std::str::from_utf8_unchecked(bytes) };

        let mut vertex_shaders = Shaders::new();
        if USE_WEBGL {
            vertex_shaders
               .set(GLSL::V1_20, src(textured::VERTEX_GLSL_120_WEBGL))
               .set(GLSL::V1_50, src(textured::VERTEX_GLSL_150_CORE_WEBGL))
        } else {
            vertex_shaders
               .set(GLSL::V1_20, src(textured::VERTEX_GLSL_120))
               .set(GLSL::V1_50, src(textured::VERTEX_GLSL_150_CORE))
        };

        let mut fragment_shaders = Shaders::new();
        if USE_WEBGL {
            fragment_shaders
               .set(GLSL::V1_20, src(textured::FRAGMENT_GLSL_120_WEBGL))
               .set(GLSL::V1_50, src(textured::FRAGMENT_GLSL_150_CORE_WEBGL))
        } else {
           fragment_shaders
               .set(GLSL::V1_20, src(textured::FRAGMENT_GLSL_120))
               .set(GLSL::V1_50, src(textured::FRAGMENT_GLSL_150_CORE))
        };

        Textured::from_vs_fs(glsl, &vertex_shaders, &fragment_shaders).unwrap()
    }

    /// Generate using custom vertex and fragment shaders.
    pub fn from_vs_fs(glsl: GLSL, vertex_shaders   : &Shaders<GLSL, str>,
                                  fragment_shaders : &Shaders<GLSL, str>)
            -> Result<Self, String> {
        let v_shader = vertex_shaders.get(glsl)
            .ok_or("No compatible vertex shader")?;

        let v_shader_compiled =
            compile_shader(gl::VERTEX_SHADER, v_shader)
            .map_err(|s| format!("Error compiling vertex shader: {}", s))?;

        let f_shader = fragment_shaders.get(glsl)
            .ok_or("No compatible fragment shader")?;

        let f_shader_compiled =
            compile_shader(gl::FRAGMENT_SHADER, f_shader)
            .map_err(|s| format!("Error compiling fragment shader: {}", s))?;

        let program;
        unsafe {
            program = gl::CreateProgram();
            gl::AttachShader(program, v_shader_compiled);
            gl::AttachShader(program, f_shader_compiled);

            let c_o_color = CString::new("o_Color").unwrap();
            if !USE_WEBGL {
                gl::BindFragDataLocation(program, 0, c_o_color.as_ptr());
            }
            drop(c_o_color);
        }

        let mut vao = 0;
        unsafe {
            gl::GenVertexArrays(1, &mut vao);
            gl::LinkProgram(program);
        }
        let pos = DynamicAttribute::xy(program, "pos", vao).unwrap();
        let c_color = CString::new("color").unwrap();
        let color = unsafe { gl::GetUniformLocation(program, c_color.as_ptr()) };
        drop(c_color);
        if color == -1 {
            panic!("Could not find uniform `color`");
        }
        let uv = DynamicAttribute::uv(program, "uv", vao).unwrap();
        Ok(Textured {
            vao: vao,
            vertex_shader: v_shader_compiled,
            fragment_shader: f_shader_compiled,
            program: program,
            pos: pos,
            color: color,
            uv: uv,
            pos_buffer: vec![[0.0; 2]; CHUNKS * BUFFER_SIZE],
            uv_buffer: vec![[0.0; 2]; CHUNKS * BUFFER_SIZE],
            offset: 0,
            last_texture_id: 0,
            last_color: [0.0; 4],
        })
    }

    fn flush(&mut self) {
        let texture_id = self.last_texture_id;
        let color = self.last_color;
        unsafe {
            gl::BindVertexArray(self.vao);
            gl::BindTexture(gl::TEXTURE_2D, texture_id);
            gl::Uniform4f(self.color, color[0], color[1], color[2], color[3]);
            // Render triangles whether they are facing
            // clockwise or counter clockwise.
            gl::Disable(gl::CULL_FACE);
            self.pos.set(&self.pos_buffer[..self.offset]);
            self.uv.set(&self.uv_buffer[..self.offset]);
            gl::DrawArrays(gl::TRIANGLES, 0, self.offset as i32);
            gl::BindVertexArray(0);
        }

        self.offset = 0;
    }
}

// Newlines and indents for cleaner panic message.
const GL_FUNC_NOT_LOADED: &'static str = "
    OpenGL function pointers must be loaded before creating the `Gl` backend!
    For more info, see the following issue on GitHub:
    https://github.com/PistonDevelopers/opengl_graphics/issues/103
";

/// Contains OpenGL data.
pub struct GlGraphics {
    colored: Colored,
    textured: Textured,
    // Keeps track of the current shader program.
    current_program: Option<GLuint>,
    // Keeps track of the current draw state.
    current_draw_state: Option<DrawState>,
    // Keeps track of the current viewport
    current_viewport: Option<Viewport>,
}

impl<'a> GlGraphics {
    /// Creates a new OpenGL back-end.
    ///
    /// # Panics
    /// If the OpenGL function pointers have not been loaded yet.
    /// See https://github.com/PistonDevelopers/opengl_graphics/issues/103 for more info.
    pub fn new(opengl: OpenGL) -> Self {
        assert!(gl::Enable::is_loaded(), GL_FUNC_NOT_LOADED);

        let glsl = opengl.to_glsl();
        // Load the vertices, color and texture coord buffers.
        GlGraphics {
            colored: Colored::new(glsl),
            textured: Textured::new(glsl),
            current_program: None,
            current_draw_state: None,
            current_viewport: None,
        }
    }

    /// Create a new OpenGL back-end with `Colored` and `Textured` structs to describe
    /// how to render objects.
    ///
    /// # Panics
    /// If the OpenGL function pointers have not been loaded yet.
    /// See https://github.com/PistonDevelopers/opengl_graphics/issues/103 for more info.
    pub fn from_colored_textured(colored : Colored, textured : Textured) -> Self {
        assert!(gl::Enable::is_loaded(), GL_FUNC_NOT_LOADED);

        // Load the vertices, color and texture coord buffers.
        GlGraphics {
            colored: colored,
            textured: textured,
            current_program: None,
            current_draw_state: None,
            current_viewport: None,
        }
    }

    /// Sets viewport with normalized coordinates and center as origin.
    fn viewport(&mut self, x: i32, y: i32, w: i32, h: i32) {
        unsafe {
            gl::Viewport(x as GLint, y as GLint, w as GLsizei, h as GLsizei);
        }
    }

    /// Returns the current program
    pub fn get_current_program(&self) -> Option<GLuint> {
        self.current_program
    }

    /// Sets the current program only if the program is not in use.
    pub fn use_program(&mut self, program: GLuint) {
        match self.current_program {
            None => {}
            Some(current_program) => {
                if program == current_program {
                    return;
                }
            }
        }

        unsafe {
            gl::UseProgram(program);
        }
        self.current_program = Some(program);
    }

    /// Unset the current program.
    ///
    /// This forces the current program to be set on next drawing call.
    pub fn clear_program(&mut self) {
        self.current_program = None
    }

    /// Sets the current draw state, by detecting changes.
    pub fn use_draw_state(&mut self, draw_state: &DrawState) {
        match self.current_draw_state {
            None => {
                draw_state::bind_scissor(draw_state.scissor, &self.current_viewport);
                draw_state::bind_stencil(draw_state.stencil);
                draw_state::bind_blend(draw_state.blend);
            }
            Some(ref old_state) => {
                draw_state::bind_state(old_state, draw_state, &self.current_viewport);
            }
        }
        self.current_draw_state = Some(*draw_state);
    }

    /// Unsets the current draw state.
    ///
    /// This forces the current draw state to be set on next drawing call.
    pub fn clear_draw_state(&mut self) {
        self.current_draw_state = None;
    }

    /// Setup that should be called at the start of a frame's draw call.
    pub fn draw_begin(&mut self, viewport: Viewport) -> Context {
        let rect = viewport.rect;
        let (x, y, w, h) = (rect[0], rect[1], rect[2], rect[3]);
        self.viewport(x, y, w, h);
        self.current_viewport = Some(viewport);
        self.clear_program();
        unsafe {
            gl::Enable(gl::FRAMEBUFFER_SRGB);
        }
        Context::new_viewport(viewport)
    }

    /// Finalize the frame's draw calls.
    pub fn draw_end(&mut self) {
        if self.colored.offset > 0 {
            let program = self.colored.program;
            self.use_program(program);
            self.colored.flush();
        }
        if self.textured.offset > 0 {
            let program = self.textured.program;
            self.use_program(program);
            self.textured.flush();
        }
    }

    /// Convenience for wrapping draw calls with the begin and end methods.
    ///
    /// This is preferred over using the draw_begin & draw_end methods
    /// explicitly but may be less flexible.
    pub fn draw<F, U>(&mut self, viewport: Viewport, f: F) -> U
        where F: FnOnce(Context, &mut Self) -> U
    {
        let c = self.draw_begin(viewport);
        let res = f(c, self);
        self.draw_end();
        res
    }

    /// Assume all textures has alpha channel for now.
    pub fn has_texture_alpha(&self, _texture: &Texture) -> bool {
        true
    }
}

impl Graphics for GlGraphics {
    type Texture = Texture;

    fn clear_color(&mut self, color: [f32; 4]) {
        let color = gamma_srgb_to_linear(color);
        unsafe {
            let (r, g, b, a) = (color[0], color[1], color[2], color[3]);
            gl::ClearColor(r, g, b, a);
            gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
        }
    }

    fn clear_stencil(&mut self, value: u8) {
        unsafe {
            gl::ClearStencil(value as i32);
            gl::Clear(gl::STENCIL_BUFFER_BIT);
        }
    }

    fn tri_list<F>(&mut self, draw_state: &DrawState, color: &[f32; 4], mut f: F)
        where F: FnMut(&mut dyn FnMut(&[[f32; 2]]))
    {
        let color = gamma_srgb_to_linear(*color);

        if self.textured.offset > 0 {
            let program = self.textured.program;
            self.use_program(program);
            self.textured.flush();
        }

        // Flush when draw state changes.
        if self.current_draw_state.is_none() ||
           self.current_draw_state.as_ref().unwrap() != draw_state {
            let program = self.colored.program;
            self.use_program(program);
            if self.current_draw_state.is_none() {
                self.use_draw_state(&Default::default());
            }
            if self.colored.offset > 0 {
                self.colored.flush();
            }
            self.use_draw_state(draw_state);
        }

        f(&mut |vertices: &[[f32; 2]]| {
            let items = vertices.len();

            // Render if there is not enough room.
            if self.colored.offset + items > BUFFER_SIZE * CHUNKS {
                let program = self.colored.program;
                self.use_program(program);
                self.colored.flush();
            }

            let ref mut shader = self.colored;
            for i in 0..items {
                shader.color_buffer[shader.offset + i] = color;
            }
            shader.pos_buffer[shader.offset..shader.offset + items]
                  .copy_from_slice(vertices);
            shader.offset += items;
        });
    }

    fn tri_list_uv<F>(&mut self,
                      draw_state: &DrawState,
                      color: &[f32; 4],
                      texture: &Texture,
                      mut f: F)
        where F: FnMut(&mut dyn FnMut(&[[f32; 2]], &[[f32; 2]]))
    {
        let color = gamma_srgb_to_linear(*color);

        if self.colored.offset > 0 {
            let program = self.colored.program;
            self.use_program(program);
            self.colored.flush();
        }

        // Flush when draw state changes.
        if self.current_draw_state.is_none() ||
           self.current_draw_state.as_ref().unwrap() != draw_state ||
           self.textured.last_texture_id != texture.get_id() ||
           self.textured.last_color != color
        {
            let program = self.textured.program;
            if self.current_draw_state.is_none() {
                self.use_draw_state(&Default::default());
            }
            if self.textured.offset > 0 {
                self.use_program(program);
                self.textured.flush();
            }
            self.use_draw_state(draw_state);
        }

        self.textured.last_texture_id = texture.get_id();
        self.textured.last_color = color;
        f(&mut |vertices: &[[f32; 2]], texture_coords: &[[f32; 2]]| {
            let items = vertices.len();

            // Render if there is not enough room.
            if self.textured.offset + items > BUFFER_SIZE * CHUNKS {
                let shader_program = self.textured.program;
                self.use_program(shader_program);
                self.textured.flush();
            }

            let ref mut shader = self.textured;
            shader.pos_buffer[shader.offset..shader.offset + items]
                  .copy_from_slice(vertices);
            shader.uv_buffer[shader.offset..shader.offset + items]
                  .copy_from_slice(texture_coords);
            shader.offset += items;
        });
    }
}

// Might not fail if previous tests loaded functions.
#[test]
#[should_panic]
fn test_gl_loaded() {
    GlGraphics::new(OpenGL::V3_2);
}