rust/lib-hedgewars-engine/src/render/gl.rs
author alfadur
Fri, 13 Nov 2020 02:52:15 +0300
changeset 15783 e7eb0cd5b0e4
parent 15782 ff1432e873bd
child 15784 84c07aa94b61
permissions -rw-r--r--
safetize buffer interface
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
     1
use integral_geometry::{Rect, Size};
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
     2
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
     3
use std::{ffi, ffi::CString, mem, num::NonZeroU32, ptr, slice};
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     4
14740
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
     5
#[derive(Default)]
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
     6
pub struct PipelineState {
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
     7
    blending: bool,
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
     8
}
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
     9
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    10
impl PipelineState {
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    11
    pub fn new() -> Self {
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    12
        Self::default()
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    13
    }
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    14
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    15
    pub fn with_blend(mut self) -> Self {
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    16
        unsafe {
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    17
            gl::Enable(gl::BLEND);
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    18
            gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    19
        }
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    20
        self.blending = true;
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    21
        self
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    22
    }
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    23
}
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    24
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    25
impl Drop for PipelineState {
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    26
    fn drop(&mut self) {
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    27
        if self.blending {
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    28
            unsafe { gl::Disable(gl::BLEND) }
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    29
        }
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    30
    }
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    31
}
abc6aaf481c4 add blending to map
alfadur
parents: 14728
diff changeset
    32
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    33
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    34
pub struct Texture2D {
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    35
    handle: Option<NonZeroU32>,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    36
    size: Size,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    37
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    38
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    39
impl Drop for Texture2D {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    40
    fn drop(&mut self) {
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    41
        if let Some(handle) = self.handle {
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    42
            unsafe {
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    43
                gl::DeleteTextures(1, &handle.get());
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    44
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    45
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    46
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    47
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    48
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    49
fn new_texture() -> Option<NonZeroU32> {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    50
    let mut handle = 0;
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    51
    unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    52
        gl::GenTextures(1, &mut handle);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    53
    }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    54
    NonZeroU32::new(handle)
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    55
}
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    56
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    57
fn tex_params(filter: TextureFilter) {
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    58
    unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    59
        gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    60
        gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    61
        gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, filter as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    62
        gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, filter as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    63
    }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    64
}
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
    65
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    66
#[derive(Clone, Copy, Debug)]
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    67
pub enum TextureFormat {
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    68
    Rgba = gl::RGBA as isize,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    69
}
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    70
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    71
#[derive(Clone, Copy, Debug)]
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    72
pub enum TextureInternalFormat {
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    73
    Rgba8 = gl::RGBA as isize,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    74
}
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    75
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    76
#[derive(Clone, Copy, Debug)]
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    77
pub enum TextureDataType {
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    78
    UnsignedByte = gl::UNSIGNED_BYTE as isize,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    79
}
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    80
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    81
#[derive(Clone, Copy, Debug)]
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    82
pub enum TextureFilter {
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    83
    Nearest = gl::NEAREST as isize,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    84
    Linear = gl::LINEAR as isize,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    85
}
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    86
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    87
#[inline]
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    88
fn get_u32(value: Option<NonZeroU32>) -> u32 {
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    89
    value.map_or(0, |v| v.get())
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    90
}
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    91
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    92
fn is_out_of_bounds(data: &[u8], data_stride: Option<NonZeroU32>, texture_size: Size) -> bool {
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    93
    let data_stride = get_u32(data_stride);
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    94
    data_stride == 0 && texture_size.area() * 4 > data.len()
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    95
        || data_stride != 0
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    96
            && texture_size.width > data_stride as usize
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    97
            && (texture_size.height * data_stride as usize) * 4 > data.len()
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    98
}
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
    99
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   100
impl Texture2D {
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   101
    pub fn new(size: Size, internal_format: TextureInternalFormat, filter: TextureFilter) -> Self {
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   102
        if let Some(handle) = new_texture() {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   103
            unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   104
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   105
                gl::TexImage2D(
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   106
                    gl::TEXTURE_2D,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   107
                    0,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   108
                    internal_format as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   109
                    size.width as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   110
                    size.height as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   111
                    0,
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   112
                    TextureFormat::Rgba as u32,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   113
                    TextureDataType::UnsignedByte as u32,
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   114
                    std::ptr::null(),
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   115
                )
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   116
            }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   117
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   118
            tex_params(filter);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   119
            Self {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   120
                handle: Some(handle),
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   121
                size,
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   122
            }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   123
        } else {
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   124
            Self { handle: None, size }
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   125
        }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   126
    }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   127
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   128
    pub fn with_data(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   129
        data: &[u8],
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   130
        data_stride: Option<NonZeroU32>,
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   131
        size: Size,
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   132
        internal_format: TextureInternalFormat,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   133
        format: TextureFormat,
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   134
        data_type: TextureDataType,
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   135
        filter: TextureFilter,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   136
    ) -> Self {
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   137
        if is_out_of_bounds(data, data_stride, size) {
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   138
            return Self { handle: None, size };
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   139
        }
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   140
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   141
        if let Some(handle) = new_texture() {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   142
            unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   143
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   144
                gl::PixelStorei(gl::UNPACK_ROW_LENGTH, get_u32(data_stride) as i32);
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   145
                gl::TexImage2D(
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   146
                    gl::TEXTURE_2D,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   147
                    0,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   148
                    internal_format as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   149
                    size.width as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   150
                    size.height as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   151
                    0,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   152
                    format as u32,
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   153
                    data_type as u32,
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   154
                    data.as_ptr() as *const _,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   155
                )
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   156
            }
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   157
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   158
            tex_params(filter);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   159
            Self {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   160
                handle: Some(handle),
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   161
                size,
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   162
            }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   163
        } else {
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   164
            Self { handle: None, size }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   165
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   166
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   167
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   168
    pub fn update(
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   169
        &self,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   170
        region: Rect,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   171
        data: &[u8],
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   172
        data_stride: Option<NonZeroU32>,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   173
        format: TextureFormat,
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   174
        data_type: TextureDataType,
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   175
    ) {
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   176
        if is_out_of_bounds(data, data_stride, self.size) {
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   177
            return;
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   178
        }
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   179
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   180
        if let Some(handle) = self.handle {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   181
            unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   182
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   183
                gl::PixelStorei(gl::UNPACK_ROW_LENGTH, get_u32(data_stride) as i32);
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   184
                gl::TexSubImage2D(
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   185
                    gl::TEXTURE_2D,
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   186
                    0,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   187
                    region.left(),
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   188
                    region.top(),
15310
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15307
diff changeset
   189
                    region.width() as i32,
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15307
diff changeset
   190
                    region.height() as i32,
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   191
                    format as u32,
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   192
                    data_type as u32,
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   193
                    data.as_ptr() as *const _,
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   194
                );
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   195
            }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   196
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   197
    }
15310
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15307
diff changeset
   198
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15307
diff changeset
   199
    pub fn retrieve(&self, data: &mut [u8]) {
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   200
        if self.size.area() * 4 > data.len() {
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   201
            return;
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   202
        }
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   203
15310
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15307
diff changeset
   204
        if let Some(handle) = self.handle {
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15307
diff changeset
   205
            unsafe {
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15307
diff changeset
   206
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15307
diff changeset
   207
                gl::GetTexImage(
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15307
diff changeset
   208
                    gl::TEXTURE_2D,
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   209
                    0,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   210
                    TextureFormat::Rgba as u32,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   211
                    TextureDataType::UnsignedByte as u32,
ff1432e873bd safetize texture interface
alfadur
parents: 15310
diff changeset
   212
                    data.as_mut_ptr() as *mut _,
15310
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15307
diff changeset
   213
                );
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15307
diff changeset
   214
            }
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15307
diff changeset
   215
        }
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15307
diff changeset
   216
    }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   217
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   218
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   219
#[derive(Clone, Copy, Debug)]
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   220
#[repr(u32)]
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   221
pub enum BufferType {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   222
    Array = gl::ARRAY_BUFFER,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   223
    ElementArray = gl::ELEMENT_ARRAY_BUFFER,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   224
}
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   225
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   226
#[derive(Clone, Copy, Debug)]
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   227
#[repr(u32)]
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   228
pub enum BufferUsage {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   229
    DynamicDraw = gl::DYNAMIC_DRAW,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   230
}
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   231
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   232
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   233
pub struct Buffer {
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   234
    pub handle: Option<NonZeroU32>,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   235
    pub buffer_type: BufferType,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   236
    pub usage: BufferUsage,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   237
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   238
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   239
impl Buffer {
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   240
    pub fn empty(buffer_type: BufferType, usage: BufferUsage) -> Buffer {
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   241
        let mut buffer = 0;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   242
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   243
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   244
            gl::GenBuffers(1, &mut buffer);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   245
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   246
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   247
        Buffer {
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   248
            handle: NonZeroU32::new(buffer),
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   249
            buffer_type: buffer_type,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   250
            usage,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   251
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   252
    }
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   253
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   254
    fn with_data(buffer_type: BufferType, usage: BufferUsage, data: &[u8]) -> Buffer {
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   255
        let mut buffer = 0;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   256
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   257
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   258
            gl::GenBuffers(1, &mut buffer);
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   259
            if buffer != 0 {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   260
                gl::BindBuffer(buffer_type as u32, buffer);
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   261
                gl::BufferData(
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   262
                    buffer_type as u32,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   263
                    data.len() as isize,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   264
                    data.as_ptr() as _,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   265
                    usage as u32,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   266
                );
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   267
            }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   268
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   269
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   270
        Buffer {
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   271
            handle: NonZeroU32::new(buffer),
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   272
            buffer_type,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   273
            usage,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   274
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   275
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   276
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   277
    pub fn ty(&self) -> BufferType {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   278
        self.buffer_type
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   279
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   280
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   281
    pub fn handle(&self) -> Option<NonZeroU32> {
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   282
        self.handle
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   283
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   284
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   285
    pub fn write_typed<T>(&self, data: &[T]) {
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   286
        if let Some(handle) = self.handle {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   287
            unsafe {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   288
                gl::BindBuffer(self.buffer_type as u32, handle.get());
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   289
                gl::BufferData(
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   290
                    self.buffer_type as u32,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   291
                    (data.len() * mem::size_of::<T>()) as isize,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   292
                    data.as_ptr() as *const _,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   293
                    self.usage as u32,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   294
                );
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   295
            }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   296
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   297
    }
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   298
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   299
    pub fn write(&self, data: &[u8]) {
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   300
        if let Some(handle) = self.handle {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   301
            unsafe {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   302
                gl::BindBuffer(self.buffer_type as u32, handle.get());
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   303
                gl::BufferData(
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   304
                    self.buffer_type as u32,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   305
                    data.len() as isize,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   306
                    data.as_ptr() as *const _,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   307
                    self.usage as u32,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   308
                );
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   309
            }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   310
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   311
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   312
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   313
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   314
impl Drop for Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   315
    fn drop(&mut self) {
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   316
        if let Some(handle) = self.handle {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   317
            let handle = handle.get();
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   318
            unsafe {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   319
                gl::DeleteBuffers(1, &handle);
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   320
            }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   321
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   322
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   323
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   324
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   325
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   326
pub enum VariableBinding<'a> {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   327
    Attribute(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   328
    Uniform(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   329
    UniformBlock(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   330
    Sampler(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   331
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   332
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   333
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   334
pub struct Shader {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   335
    pub program: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   336
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   337
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   338
impl Drop for Shader {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   339
    fn drop(&mut self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   340
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   341
            gl::DeleteProgram(self.program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   342
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   343
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   344
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   345
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   346
impl Shader {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   347
    pub fn new<'a>(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   348
        vs: &str,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   349
        ps: Option<&str>,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   350
        bindings: &[VariableBinding<'a>],
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   351
    ) -> Result<Self, String> {
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   352
        unsafe fn compile_shader(shader_type: u32, shader_code: &str) -> Result<u32, String> {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   353
            let shader = gl::CreateShader(shader_type);
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   354
            let len = shader_code.len() as i32;
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   355
            let code_strings = shader_code.as_ptr() as *const i8;
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   356
            gl::ShaderSource(shader, 1, &code_strings, &len);
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   357
            gl::CompileShader(shader);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   358
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   359
            let mut success = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   360
            gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   361
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   362
            if success == gl::FALSE as i32 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   363
                let mut log_size = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   364
                gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut log_size as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   365
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   366
                let mut log = vec![0u8; log_size as usize];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   367
                gl::GetShaderInfoLog(shader, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   368
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   369
                gl::DeleteShader(shader);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   370
                Err(String::from_utf8_unchecked(log))
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   371
            } else {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   372
                Ok(shader)
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   373
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   374
        }
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   375
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   376
        let vs = unsafe { compile_shader(gl::VERTEX_SHADER, vs)? };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   377
        let ps = if let Some(ps) = ps {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   378
            Some(unsafe { compile_shader(gl::FRAGMENT_SHADER, ps)? })
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   379
        } else {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   380
            None
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   381
        };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   382
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   383
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   384
            let program = gl::CreateProgram();
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   385
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   386
            gl::AttachShader(program, vs);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   387
            if let Some(ps) = ps {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   388
                gl::AttachShader(program, ps);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   389
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   390
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   391
            for bind in bindings {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   392
                match bind {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   393
                    &VariableBinding::Attribute(ref name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   394
                        let c_str = CString::new(name.as_bytes()).unwrap();
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   395
                        gl::BindAttribLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   396
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   397
                            id,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   398
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   399
                        );
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   400
                    }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   401
                    _ => {}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   402
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   403
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   404
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   405
            gl::LinkProgram(program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   406
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   407
            let mut success = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   408
            gl::GetProgramiv(program, gl::LINK_STATUS, &mut success);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   409
            if success == gl::FALSE as i32 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   410
                let mut log_size = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   411
                gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut log_size as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   412
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   413
                let mut log = vec![0u8; log_size as usize];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   414
                gl::GetProgramInfoLog(program, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   415
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   416
                gl::DeleteProgram(program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   417
                return Err(String::from_utf8_unchecked(log));
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   418
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   419
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   420
            //gl::DetachShader(program, vs);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   421
            if let Some(ps) = ps {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   422
                //gl::DetachShader(program, ps);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   423
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   424
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   425
            gl::UseProgram(program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   426
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   427
            // after linking we setup sampler bindings as specified in the shader
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   428
            for bind in bindings {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   429
                match bind {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   430
                    VariableBinding::Uniform(name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   431
                        let c_str = CString::new(name.as_bytes()).unwrap();
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   432
                        let index = gl::GetUniformLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   433
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   434
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   435
                        );
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   436
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   437
                        // TODO: impl for block?
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   438
                    }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   439
                    VariableBinding::UniformBlock(name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   440
                        let c_str = CString::new(name.as_bytes()).unwrap();
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   441
                        let index = gl::GetUniformBlockIndex(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   442
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   443
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   444
                        );
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   445
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   446
                        gl::UniformBlockBinding(program, index, *id);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   447
                    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   448
                    VariableBinding::Sampler(name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   449
                        let c_str = CString::new(name.as_bytes()).unwrap();
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   450
                        let index = gl::GetUniformLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   451
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   452
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   453
                        );
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   454
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   455
                        gl::Uniform1i(index, *id as i32);
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   456
                    }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   457
                    _ => {}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   458
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   459
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   460
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   461
            Ok(Shader { program })
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   462
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   463
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   464
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   465
    pub fn bind(&self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   466
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   467
            gl::UseProgram(self.program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   468
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   469
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   470
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   471
    pub fn set_matrix(&self, name: &str, matrix: *const f32) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   472
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   473
            let c_str = CString::new(name).unwrap();
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   474
            let index = gl::GetUniformLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   475
                self.program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   476
                c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   477
            );
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   478
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   479
            gl::UniformMatrix4fv(index, 1, gl::FALSE, matrix);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   480
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   481
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   482
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   483
    pub fn bind_texture_2d(&self, index: u32, texture: &Texture2D) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   484
        self.bind();
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   485
15306
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   486
        if let Some(handle) = texture.handle {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   487
            unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   488
                gl::ActiveTexture(gl::TEXTURE0 + index);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   489
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14740
diff changeset
   490
            }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   491
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   492
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   493
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   494
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   495
pub enum InputFormat {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   496
    Float(u32, bool),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   497
    Integer(u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   498
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   499
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   500
pub struct InputElement {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   501
    pub shader_slot: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   502
    pub buffer_slot: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   503
    pub format: InputFormat,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   504
    pub components: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   505
    pub stride: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   506
    pub offset: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   507
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   508
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   509
// TODO:
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   510
pub struct InputLayout {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   511
    pub elements: Vec<InputElement>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   512
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   513
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   514
pub struct LayoutGuard {
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   515
    vao: u32,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   516
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   517
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   518
impl Drop for LayoutGuard {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   519
    fn drop(&mut self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   520
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   521
            gl::DeleteVertexArrays(1, [self.vao].as_ptr());
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   522
            gl::BindVertexArray(0);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   523
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   524
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   525
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   526
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   527
impl InputLayout {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   528
    pub fn new(elements: Vec<InputElement>) -> Self {
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   529
        InputLayout { elements }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   530
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   531
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   532
    pub fn bind(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   533
        &mut self,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   534
        buffers: &[(u32, &Buffer)],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   535
        index_buffer: Option<&Buffer>,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   536
    ) -> LayoutGuard {
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   537
        let mut vao = 0;
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   538
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   539
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   540
            gl::GenVertexArrays(1, &mut vao);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   541
            gl::BindVertexArray(vao);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   542
        }
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   543
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   544
        for &(slot, ref buffer) in buffers {
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   545
            if let Some(handle) = buffer.handle() {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   546
                unsafe {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   547
                    gl::BindBuffer(buffer.ty() as u32, handle.get());
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   548
                }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   549
            }
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   550
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   551
            for attr in self.elements.iter().filter(|a| a.buffer_slot == slot) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   552
                unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   553
                    gl::EnableVertexAttribArray(attr.shader_slot);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   554
                    match attr.format {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   555
                        InputFormat::Float(fmt, normalized) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   556
                            gl::VertexAttribPointer(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   557
                                attr.shader_slot,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   558
                                attr.components as i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   559
                                fmt,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   560
                                if normalized { gl::TRUE } else { gl::FALSE },
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   561
                                attr.stride as i32,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   562
                                attr.offset as *const _,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   563
                            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   564
                        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   565
                        InputFormat::Integer(fmt) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   566
                            gl::VertexAttribIPointer(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   567
                                attr.shader_slot,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   568
                                attr.components as i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   569
                                fmt,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   570
                                attr.stride as i32,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   571
                                attr.offset as *const _,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   572
                            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   573
                        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   574
                    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   575
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   576
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   577
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   578
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   579
        if let Some(buf) = index_buffer {
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   580
            if let Some(handle) = buf.handle() {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   581
                unsafe {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   582
                    gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, handle.get());
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   583
                }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   584
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   585
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   586
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   587
        LayoutGuard { vao }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   588
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   589
}