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
use std::collections::HashMap;
use std::cell::RefCell;
use crate::math::*;
use crate::core::*;
use crate::context::{Context, consts};

///
/// A shader program consisting of a programmable vertex shader followed by a programmable fragment shader.
/// Functionality includes transferring per vertex data to the vertex shader (see the use_attribute functionality)
/// and transferring uniform data to both shader stages (see the use_uniform and use_texture functionality)
/// and execute the shader program (see the draw functionality).
///
pub struct Program {
    context: Context,
    id: crate::context::Program,
    vertex_attributes: HashMap<String, u32>,
    textures: RefCell<HashMap<String, u32>>,
    uniforms: HashMap<String, crate::context::UniformLocation>,
    uniform_blocks: RefCell<HashMap<String, (u32, u32)>>
}

impl Program
{
    pub fn from_source(context: &Context, vertex_shader_source: &str, fragment_shader_source: &str) -> Result<Program, Error>
    {
        let vert_shader = context.create_shader(consts::VERTEX_SHADER)
            .ok_or(Error::FailedToCreateShader{ shader_type: "Vertex shader".to_string(), message:"Unable to create shader object".to_string() })?;
        let frag_shader = context.create_shader(consts::FRAGMENT_SHADER)
            .ok_or(Error::FailedToCreateShader{ shader_type: "Fragment shader".to_string(), message:"Unable to create shader object".to_string() })?;
        context.compile_shader(vertex_shader_source, &vert_shader);
        context.compile_shader(fragment_shader_source, &frag_shader);

        let id = context.create_program();
        context.attach_shader(&id, &vert_shader);
        context.attach_shader(&id, &frag_shader);
        let success = context.link_program(&id);

        if !success {
            let mut message = "Failed to compile shader program:\n".to_string();
            if let Some(log) = context.get_program_info_log(&id) {
                message = format!("{}\nLink error: {}", message, log);
            }
            if let Some(log) = context.get_shader_info_log(&vert_shader) {
                message = format!("{}\nVertex shader error: {}", message, log);
            }
            if let Some(log) = context.get_shader_info_log(&frag_shader) {
                message = format!("{}\nFragment shader error: {}", message, log);
            }
            return Err(Error::FailedToLinkProgram { message });
        }

        context.detach_shader(&id, &vert_shader);
        context.detach_shader(&id, &frag_shader);
        context.delete_shader(Some(&vert_shader));
        context.delete_shader(Some(&frag_shader));

        // Init vertex attributes
        let num_attribs = context.get_program_parameter(&id, consts::ACTIVE_ATTRIBUTES);
        let mut vertex_attributes = HashMap::new();
        for i in 0..num_attribs {
            let info = context.get_active_attrib(&id, i);
            let location = context.get_attrib_location(&id, &info.name()).unwrap();
            //println!("Attribute location: {}, name: {}, type: {}, size: {}", location, info.name(), info.type_(), info.size());
            vertex_attributes.insert(info.name(), location);
        }

        // Init uniforms
        let num_uniforms = context.get_program_parameter(&id, consts::ACTIVE_UNIFORMS);
        let mut uniforms = HashMap::new();
        for i in 0..num_uniforms {
            let info = context.get_active_uniform(&id, i);
            let location = context.get_uniform_location(&id, &info.name());
            //println!("Uniform location: {:?}, name: {}, type: {}, size: {}", location, info.name(), info.type_(), info.size());
            if let Some(loc) = location {
                uniforms.insert(info.name(), loc);
            }
        }

        Ok(Program { context: context.clone(), id, vertex_attributes, uniforms, uniform_blocks: RefCell::new(HashMap::new()),
            textures: RefCell::new(HashMap::new())})
    }

    pub fn use_uniform_int(&self, name: &str, data: &i32) -> Result<(), Error>
    {
        let location = self.get_uniform_location(name)?;
        self.context.uniform1i(location, *data);
        self.context.unuse_program();
        Ok(())
    }

    pub fn use_uniform_float(&self, name: &str, data: &f32) -> Result<(), Error>
    {
        let location = self.get_uniform_location(name)?;
        self.context.uniform1f(location, *data);
        self.context.unuse_program();
        Ok(())
    }

    pub fn use_uniform_vec2(&self, name: &str, data: &Vec2) -> Result<(), Error>
    {
        let location = self.get_uniform_location(name)?;
        self.context.uniform2fv(location, &mut [data.x, data.y]);
        self.context.unuse_program();
        Ok(())
    }

    pub fn use_uniform_vec3(&self, name: &str, data: &Vec3) -> Result<(), Error>
    {
        let location = self.get_uniform_location(name)?;
        self.context.uniform3fv(location, &mut [data.x, data.y, data.z]);
        self.context.unuse_program();
        Ok(())
    }

    pub fn use_uniform_vec4(&self, name: &str, data: &Vec4) -> Result<(), Error>
    {
        let location= self.get_uniform_location(name)?;
        self.context.uniform4fv(location, &mut [data.x, data.y, data.z, data.w]);
        self.context.unuse_program();
        Ok(())
    }

    pub fn use_uniform_mat2(&self, name: &str, data: &Mat2) -> Result<(), Error>
    {
        let location = self.get_uniform_location(name)?;
        self.context.uniform_matrix2fv(location, &mut data.to_slice());
        self.context.unuse_program();
        Ok(())
    }

    pub fn use_uniform_mat3(&self, name: &str, data: &Mat3) -> Result<(), Error>
    {
        let location = self.get_uniform_location(name)?;
        self.context.uniform_matrix3fv(location, &mut data.to_slice());
        self.context.unuse_program();
        Ok(())
    }

    pub fn use_uniform_mat4(&self, name: &str, data: &Mat4) -> Result<(), Error>
    {
        let location = self.get_uniform_location(name)?;
        self.context.uniform_matrix4fv(location, &mut data.to_slice());
        self.context.unuse_program();
        Ok(())
    }

    fn get_uniform_location(&self, name: &str) -> Result<&crate::context::UniformLocation, Error>
    {
        self.set_used();
        let loc = self.uniforms.get(name).ok_or_else(|| Error::FailedToFindUniform {message: format!("Failed to find uniform {}", name)})?;
        Ok(loc)
    }

    pub fn use_texture(&self, texture: &dyn Texture, texture_name: &str) -> Result<(), Error>
    {
        if !self.textures.borrow().contains_key(texture_name) {
            let mut map = self.textures.borrow_mut();
            let index = map.len() as u32;
            map.insert(texture_name.to_owned(), index);
        };
        let index = self.textures.borrow().get(texture_name).unwrap().clone();
        texture.bind(index);
        self.use_uniform_int(texture_name, &(index as i32))?;
        Ok(())
    }

    pub fn use_uniform_block(&self, buffer: &UniformBuffer, block_name: &str)
    {
        if !self.uniform_blocks.borrow().contains_key(block_name) {
            let mut map = self.uniform_blocks.borrow_mut();
            let location = self.context.get_uniform_block_index(&self.id, block_name);
            let index = map.len() as u32;
            map.insert(block_name.to_owned(), (location, index));
        };
        let (location, index) = self.uniform_blocks.borrow().get(block_name).unwrap().clone();
        self.context.uniform_block_binding(&self.id, location, index);
        buffer.bind(index);
        self.context.unbind_buffer(consts::UNIFORM_BUFFER);
    }

    pub fn use_attribute(&self, buffer: &VertexBuffer, attribute_name: &str) -> Result<(), Error>
    {
        self.use_attribute_divisor(buffer, attribute_name, 0)?;
        Ok(())
    }

    pub fn use_attribute_divisor(&self, buffer: &VertexBuffer, attribute_name: &str, divisor: usize) -> Result<(), Error>
    {
        if buffer.count() > 0 {
            buffer.bind();
            let loc = self.location(&attribute_name)?;
            self.context.enable_vertex_attrib_array(loc);
            self.context.vertex_attrib_pointer(loc, 1, buffer.data_type(), false, 0, 0);
            self.context.vertex_attrib_divisor(loc, divisor as u32);
            self.context.unbind_buffer(consts::ARRAY_BUFFER);
            self.context.unuse_program();
        }
        Ok(())
    }

    pub fn use_attribute_vec2(&self, buffer: &VertexBuffer, attribute_name: &str) -> Result<(), Error>
    {
        self.use_attribute_vec2_divisor(buffer, attribute_name, 0)?;
        Ok(())
    }

    pub fn use_attribute_vec2_divisor(&self, buffer: &VertexBuffer, attribute_name: &str, divisor: usize) -> Result<(), Error>
    {
        if buffer.count() > 0 {
            buffer.bind();
            let loc = self.location(&attribute_name)?;
            self.context.enable_vertex_attrib_array(loc);
            self.context.vertex_attrib_pointer(loc, 2, buffer.data_type(), false, 0, 0);
            self.context.vertex_attrib_divisor(loc, divisor as u32);
            self.context.unbind_buffer(consts::ARRAY_BUFFER);
            self.context.unuse_program();
        }
        Ok(())
    }

    pub fn use_attribute_vec3(&self, buffer: &VertexBuffer, attribute_name: &str) -> Result<(), Error>
    {
        self.use_attribute_vec3_divisor(buffer, attribute_name, 0)?;
        Ok(())
    }

    pub fn use_attribute_vec3_divisor(&self, buffer: &VertexBuffer, attribute_name: &str, divisor: usize) -> Result<(), Error>
    {
        if buffer.count() > 0 {
            buffer.bind();
            let loc = self.location(&attribute_name)?;
            self.context.enable_vertex_attrib_array(loc);
            self.context.vertex_attrib_pointer(loc, 3, buffer.data_type(), false, 0, 0);
            self.context.vertex_attrib_divisor(loc, divisor as u32);
            self.context.unbind_buffer(consts::ARRAY_BUFFER);
            self.context.unuse_program();
        }
        Ok(())
    }

    pub fn use_attribute_vec4(&self, buffer: &VertexBuffer, attribute_name: &str) -> Result<(), Error>
    {
        self.use_attribute_vec4_divisor(buffer, attribute_name, 0)?;
        Ok(())
    }

    pub fn use_attribute_vec4_divisor(&self, buffer: &VertexBuffer, attribute_name: &str, divisor: usize) -> Result<(), Error>
    {
        if buffer.count() > 0 {
            buffer.bind();
            let loc = self.location(&attribute_name)?;
            self.context.enable_vertex_attrib_array(loc);
            self.context.vertex_attrib_pointer(loc, 4, buffer.data_type(), false, 0, 0);
            self.context.vertex_attrib_divisor(loc, divisor as u32);
            self.context.unbind_buffer(consts::ARRAY_BUFFER);
            self.context.unuse_program();
        }
        Ok(())
    }

    pub fn draw_arrays(&self, render_states: RenderStates, viewport: Viewport, count: u32)
    {
        Self::set_viewport(&self.context, viewport);
        Self::set_states(&self.context, render_states);
        self.set_used();
        self.context.draw_arrays(consts::TRIANGLES, 0, count);
        for location in self.vertex_attributes.values() {
            self.context.disable_vertex_attrib_array(*location);
        }
        self.context.unuse_program();
    }

    pub fn draw_arrays_instanced(&self, render_states: RenderStates, viewport: Viewport, count: u32, instance_count: u32)
    {
        Self::set_viewport(&self.context, viewport);
        Self::set_states(&self.context, render_states);
        self.set_used();
        self.context.draw_arrays_instanced(consts::TRIANGLES, 0, count, instance_count);
        self.context.unbind_buffer(consts::ELEMENT_ARRAY_BUFFER);
        for location in self.vertex_attributes.values() {
            self.context.disable_vertex_attrib_array(*location);
        }
        self.context.unuse_program();
    }

    pub fn draw_elements(&self, render_states: RenderStates, viewport: Viewport, element_buffer: &ElementBuffer)
    {
        self.draw_subset_of_elements(render_states, viewport, element_buffer, 0,element_buffer.count() as u32);
    }

    pub fn draw_subset_of_elements(&self, render_states: RenderStates, viewport: Viewport, element_buffer: &ElementBuffer, first: u32, count: u32)
    {
        Self::set_viewport(&self.context, viewport);
        Self::set_states(&self.context, render_states);
        self.set_used();
        element_buffer.bind();
        self.context.draw_elements(consts::TRIANGLES, count, consts::UNSIGNED_INT, first);
        self.context.unbind_buffer(consts::ELEMENT_ARRAY_BUFFER);

        for location in self.vertex_attributes.values() {
            self.context.disable_vertex_attrib_array(*location);
        }
        self.context.unuse_program();
    }

    pub fn draw_elements_instanced(&self, render_states: RenderStates, viewport: Viewport, element_buffer: &ElementBuffer, count: u32)
    {
        Self::set_viewport(&self.context, viewport);
        Self::set_states(&self.context, render_states);
        self.set_used();
        element_buffer.bind();
        self.context.draw_elements_instanced(consts::TRIANGLES, element_buffer.count() as u32, consts::UNSIGNED_INT, 0, count);
        self.context.unbind_buffer(consts::ELEMENT_ARRAY_BUFFER);
        for location in self.vertex_attributes.values() {
            self.context.disable_vertex_attrib_array(*location);
        }
        self.context.unuse_program();
    }

    fn location(&self, name: &str) -> Result<u32, Error>
    {
        self.set_used();
        let location = self.vertex_attributes.get(name).ok_or_else(
            || Error::FailedToFindAttribute {message: format!("The attribute {} is sent to the shader but never used.", name)})?;
        Ok(*location)
    }

    fn set_used(&self) {
        self.context.use_program(&self.id);
    }

    fn set_states(context: &Context, render_states: RenderStates) {
        Self::set_cull(context, render_states.cull);
        Self::set_write_mask(context, render_states.write_mask);
        Self::set_depth(context, Some(render_states.depth_test), render_states.write_mask.depth);
        Self::set_blend(context, render_states.blend);
    }

    fn set_viewport(context: &Context, viewport: Viewport) {
        unsafe {
            static mut CURRENT_VIEWPORT: Viewport = Viewport {x: 0, y: 0, width: 0, height: 0};
            if viewport != CURRENT_VIEWPORT
            {
                context.viewport(viewport.x, viewport.y, viewport.width, viewport.height);
                CURRENT_VIEWPORT = viewport;
            }
        }
    }

    fn set_cull(context: &Context, cull: CullType) {
        unsafe {
            static mut CURRENT_CULL: CullType = CullType::None;
            if cull != CURRENT_CULL
            {
                match cull {
                    CullType::None => {
                        context.disable(consts::CULL_FACE);
                    },
                    CullType::Back => {
                        context.enable(consts::CULL_FACE);
                        context.cull_face(consts::BACK);
                    },
                    CullType::Front => {
                        context.enable(consts::CULL_FACE);
                        context.cull_face(consts::FRONT);
                    },
                    CullType::FrontAndBack => {
                        context.enable(consts::CULL_FACE);
                        context.cull_face(consts::FRONT_AND_BACK);
                    }
                }
                CURRENT_CULL = cull;
            }
        }
    }

    fn set_blend(context: &Context, blend: Option<BlendParameters>)
    {
        unsafe {
            static mut CURRENT: Option<BlendParameters> = None;
            if blend != CURRENT
            {
                if let Some(blend_parameters) = blend {
                    context.enable(consts::BLEND);
                    context.blend_func_separate(Self::blend_const_from_multiplier(blend_parameters.source_rgb_multiplier),
                                           Self::blend_const_from_multiplier(blend_parameters.destination_rgb_multiplier),
                                           Self::blend_const_from_multiplier(blend_parameters.source_alpha_multiplier),
                                           Self::blend_const_from_multiplier(blend_parameters.destination_alpha_multiplier));
                    context.blend_equation_separate(Self::blend_const_from_equation(blend_parameters.rgb_equation),
                                               Self::blend_const_from_equation(blend_parameters.alpha_equation));
                } else {
                    context.disable(consts::BLEND);
                }
                CURRENT = blend;
            }
        }
    }

    fn blend_const_from_multiplier(multiplier: BlendMultiplierType) -> u32 {
        match multiplier {
            BlendMultiplierType::Zero => consts::ZERO,
            BlendMultiplierType::One => consts::ONE,
            BlendMultiplierType::SrcColor => consts::SRC_COLOR,
            BlendMultiplierType::OneMinusSrcColor => consts::ONE_MINUS_SRC_COLOR,
            BlendMultiplierType::DstColor => consts::DST_COLOR,
            BlendMultiplierType::OneMinusDstColor => consts::ONE_MINUS_DST_COLOR,
            BlendMultiplierType::SrcAlpha => consts::SRC_ALPHA,
            BlendMultiplierType::OneMinusSrcAlpha => consts::ONE_MINUS_SRC_ALPHA,
            BlendMultiplierType::DstAlpha => consts::DST_ALPHA,
            BlendMultiplierType::OneMinusDstAlpha => consts::ONE_MINUS_DST_ALPHA,
            BlendMultiplierType::SrcAlphaSaturate => consts::SRC_ALPHA_SATURATE
        }
    }

    fn blend_const_from_equation(equation: BlendEquationType) -> u32 {
        match equation {
            BlendEquationType::Add => consts::FUNC_ADD,
            BlendEquationType::Subtract => consts::FUNC_SUBTRACT,
            BlendEquationType::ReverseSubtract => consts::FUNC_REVERSE_SUBTRACT,
            BlendEquationType::Min => consts::MIN,
            BlendEquationType::Max => consts::MAX,
        }
    }

    pub(crate) fn set_write_mask(context: &Context, write_mask: WriteMask)
    {
        unsafe {
            static mut CURRENT_COLOR_MASK: WriteMask = WriteMask::COLOR_AND_DEPTH;
            if write_mask != CURRENT_COLOR_MASK
            {
                context.color_mask(write_mask.red, write_mask.green, write_mask.blue, write_mask.alpha);
                Self::set_depth(context, None, write_mask.depth);
                CURRENT_COLOR_MASK = write_mask;
            }
        }
    }

    fn set_depth(context: &Context, depth_test: Option<DepthTestType>, depth_mask: bool) {
        unsafe {
            static mut CURRENT_DEPTH_ENABLE: bool = false;
            static mut CURRENT_DEPTH_MASK: bool = true;
            static mut CURRENT_DEPTH_TEST: DepthTestType = DepthTestType::Less;

            if depth_mask == false && depth_test == Some(DepthTestType::Always) {
                if CURRENT_DEPTH_ENABLE {
                    context.disable(consts::DEPTH_TEST);
                    CURRENT_DEPTH_ENABLE = false;
                    return;
                }
            }
            else {
                if !CURRENT_DEPTH_ENABLE {
                    context.enable(consts::DEPTH_TEST);
                    CURRENT_DEPTH_ENABLE = true;
                }
            }

            if depth_mask != CURRENT_DEPTH_MASK
            {
                context.depth_mask(depth_mask);
                CURRENT_DEPTH_MASK = depth_mask;
            }

            if depth_test.is_some() && depth_test.unwrap() != CURRENT_DEPTH_TEST
            {
                match depth_test.unwrap() {
                    DepthTestType::Never => {
                        context.depth_func(consts::NEVER);
                    },
                    DepthTestType::Less => {
                        context.depth_func(consts::LESS);
                    },
                    DepthTestType::Equal => {
                        context.depth_func(consts::EQUAL);
                    },
                    DepthTestType::LessOrEqual => {
                        context.depth_func(consts::LEQUAL);
                    },
                    DepthTestType::Greater => {
                        context.depth_func(consts::GREATER);
                    },
                    DepthTestType::NotEqual => {
                        context.depth_func(consts::NOTEQUAL);
                    },
                    DepthTestType::GreaterOrEqual => {
                        context.depth_func(consts::GEQUAL);
                    },
                    DepthTestType::Always => {
                        context.depth_func(consts::ALWAYS);
                    }
                }
                CURRENT_DEPTH_TEST = depth_test.unwrap();
            }
        }
    }
}

impl Drop for Program {
    fn drop(&mut self) {
        self.context.delete_program(&self.id);
    }
}