Quellcode durchsuchen

context: Cache active buffer

This reduces calls to glUseBuffer.
Johannes Hofmann vor 7 Jahren
Ursprung
Commit
43ecb10350
2 geänderte Dateien mit 36 neuen und 11 gelöschten Zeilen
  1. 24
    11
      src/buffer.rs
  2. 12
    0
      src/context.rs

+ 24
- 11
src/buffer.rs Datei anzeigen

@@ -3,12 +3,29 @@ use context::Context;
3 3
 use std::mem;
4 4
 
5 5
 
6
+//TODO rename Buffer -> ArrayBuffer?
6 7
 #[derive(Clone, Debug)]
7 8
 pub struct Buffer {
8
-    buffer_obj: u32,
9
+    buffer_id: BufferId,
9 10
     num_elements: usize,
10 11
 }
11 12
 
13
+#[derive(Copy, Clone, Debug, Eq, PartialEq)]
14
+pub struct BufferId {
15
+    id: u32,
16
+}
17
+
18
+impl BufferId {
19
+    /// Returns an invalid `BufferId`.
20
+    pub fn invalid() -> Self {
21
+        BufferId{ id: 0 }
22
+    }
23
+
24
+    pub fn index(&self) -> u32 {
25
+        self.id
26
+    }
27
+}
28
+
12 29
 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
13 30
 pub enum DrawMode {
14 31
     Triangles,
@@ -28,11 +45,11 @@ impl DrawMode {
28 45
 
29 46
 impl Buffer {
30 47
     pub fn new(cx: &mut Context, vertex_data: &[f32], num_elements: usize) -> Buffer {
31
-        let mut buffer_obj = 0_u32;
48
+        let mut buffer_id = BufferId { id: 0 };
32 49
 
33 50
         unsafe {
34
-            cx.gl.GenBuffers(1, &mut buffer_obj);
35
-            cx.gl.BindBuffer(context::gl::ARRAY_BUFFER, buffer_obj);
51
+            cx.gl.GenBuffers(1, &mut buffer_id.id);
52
+            cx.bind_buffer(buffer_id);
36 53
             cx.gl.BufferData(context::gl::ARRAY_BUFFER,
37 54
                              (vertex_data.len() * mem::size_of::<f32>()) as context::gl::types::GLsizeiptr,
38 55
                              vertex_data.as_ptr() as *const _,
@@ -40,12 +57,13 @@ impl Buffer {
40 57
         }
41 58
 
42 59
         Buffer {
43
-            buffer_obj,
60
+            buffer_id,
44 61
             num_elements,
45 62
         }
46 63
     }
47 64
 
48 65
     pub fn set_data(&mut self, cx: &mut Context, vertex_data: &[f32], num_elements: usize) {
66
+        cx.bind_buffer(self.buffer_id);
49 67
         unsafe {
50 68
             cx.gl.BufferData(context::gl::ARRAY_BUFFER,
51 69
                                   (vertex_data.len() * mem::size_of::<f32>()) as context::gl::types::GLsizeiptr,
@@ -55,13 +73,8 @@ impl Buffer {
55 73
         self.num_elements = num_elements;
56 74
     }
57 75
 
58
-    pub fn bind(&self, cx: &mut Context) {
59
-        unsafe {
60
-            cx.gl.BindBuffer(context::gl::ARRAY_BUFFER, self.buffer_obj);
61
-        }
62
-    }
63
-
64 76
     pub fn draw(&self, cx: &mut Context, mode: DrawMode) {
77
+        cx.bind_buffer(self.buffer_id);
65 78
         unsafe {
66 79
             cx.gl.DrawArrays(
67 80
                 mode.to_gl_enum(),

+ 12
- 0
src/context.rs Datei anzeigen

@@ -1,3 +1,4 @@
1
+use buffer::BufferId;
1 2
 use glutin::GlContext;
2 3
 use glutin;
3 4
 use program::ProgramId;
@@ -26,6 +27,7 @@ pub struct Context {
26 27
     active_texture_unit: TextureUnit,
27 28
     next_free_texture_unit: TextureUnit,
28 29
     active_program: ProgramId,
30
+    active_buffer: BufferId,
29 31
 }
30 32
 
31 33
 impl ::std::fmt::Debug for Context {
@@ -53,6 +55,7 @@ impl Context {
53 55
             active_texture_unit: TextureUnit(0),
54 56
             next_free_texture_unit: TextureUnit(0),
55 57
             active_program: ProgramId::invalid(),
58
+            active_buffer: BufferId::invalid(),
56 59
         };
57 60
 
58 61
         // Initialize a vertex array object (VAO) if the current OpenGL context supports it. VAOs are
@@ -169,4 +172,13 @@ impl Context {
169 172
             self.active_program = prog;
170 173
         }
171 174
     }
175
+
176
+    pub fn bind_buffer(&mut self, buf: BufferId) {
177
+        if buf != self.active_buffer {
178
+            unsafe {
179
+                self.gl.BindBuffer(gl::ARRAY_BUFFER, buf.index());
180
+            }
181
+            self.active_buffer = buf;
182
+        }
183
+    }
172 184
 }