1 module ecs_utils.gfx.buffer;
2 
3 import bubel.ecs.std;
4 
5 //import glad.gl.gl;
6 //import glad.gl.gles2;
7 
8 version(WebAssembly)import glad.gl.gles2;
9 else version(Android)import glad.gl.gles2;
10 else import glad.gl.gl;
11 
12 extern(C):
13 
14 struct Buffer
15 {
16 
17     void create() nothrow
18     {
19         data = Mallocator.make!Data;
20         data.gl_handle = 0;
21         glGenBuffers(1,&data.gl_handle);
22         data.elem_size = 0;
23     }
24 
25     //---------------------------------------------------------------------------------------------------------------------------------------------------
26 
27     void destroy() nothrow
28     {
29         if(data.gl_handle)glDeleteBuffers(1,&data.gl_handle);
30     }
31 
32     //---------------------------------------------------------------------------------------------------------------------------------------------------
33 
34     void bind(BindTarget target) nothrow
35     {
36         //if(vbo != this)glBindBuffer(GL_ARRAY_BUFFER,data.gl_handle);
37         //vbo = this;
38         glBindBuffer(target,data.gl_handle);
39     }
40 
41     void bindRange(BindTarget target, uint index, uint offset, uint size) nothrow
42     {
43         glBindBufferRange(target, index, data.gl_handle, offset, size);
44     }
45 
46     //---------------------------------------------------------------------------------------------------------------------------------------------------
47 
48     void bufferData(BindTarget target, uint size, uint count, uint usage, void* data) nothrow
49     {
50         bind(target);
51         this.data.elem_size = size;
52         glBufferData(target,size*count,data,usage);
53     }
54 
55     /*void bufferStorage(uint size, uint count, void* data, uint flags = StorageFlagBits.write)
56     {
57         bind(BindTarget.array);
58         this.data.elem_size = size;
59         glBufferStorage(GL_ARRAY_BUFFER,size*count,data, flags);
60     }*/
61 
62     void bufferSubData(BindTarget target, uint size, uint offset, void* data) nothrow
63     {
64         bind(target);
65         glBufferSubData(target,offset,size,data);
66     }
67     
68     void map(BindTarget target) nothrow
69     {
70         bind(target);
71         version(Android){}else version(WebAssembly){}else data.map_ptr = glMapBuffer(target,GL_WRITE_ONLY);
72     }
73 
74     void map(uint offset, uint size, BindTarget target, uint flags = MapFlagBits.write | MapFlagBits.flush_explict | MapFlagBits.invalidate_buffer) nothrow
75     {
76         bind(target);
77         version(Android){}else data.map_ptr = glMapBufferRange(target,offset,size,flags);
78     }
79 
80     void flush(uint offset, uint size, BindTarget target) nothrow
81     {
82         glFlushMappedBufferRange(target, offset, size);
83     }
84 
85     void unmap(BindTarget target) nothrow
86     {
87         bind(target);
88         glUnmapBuffer(target);
89         data.map_ptr = null;
90     }
91 
92     void* mappedPointer() nothrow
93     {
94         return data.map_ptr;
95     }
96 
97     //---------------------------------------------------------------------------------------------------------------------------------------------------
98 
99     static void unbind(BindTarget target) nothrow
100     {
101         //vbo = 0;
102         glBindBuffer(target,0);
103     }
104 
105     enum BindTarget
106     {
107         array = GL_ARRAY_BUFFER,
108         element_array = GL_ELEMENT_ARRAY_BUFFER,
109         uniform = GL_UNIFORM_BUFFER,
110         //shader_storage = GL_SHADER_STORAGE_BUFFER,
111         //indirect = GL_DRAW_INDIRECT_BUFFER
112     }
113 
114     enum MapFlagBits
115     {
116         write = GL_MAP_WRITE_BIT,
117         invalidate_buffer = GL_MAP_INVALIDATE_BUFFER_BIT,
118         flush_explict = GL_MAP_FLUSH_EXPLICIT_BIT,
119         //coherent = GL_MAP_COHERENT_BIT,
120         //persistent = GL_MAP_PERSISTENT_BIT
121     }
122 
123     enum StorageFlagBits
124     {
125         write = GL_MAP_WRITE_BIT,
126         //coherent = GL_MAP_COHERENT_BIT,
127         //persistent = GL_MAP_PERSISTENT_BIT
128     }
129 
130     struct Data
131     {
132         uint elem_size;
133         uint gl_handle;
134         void* map_ptr;
135     }
136 
137     Data* data;
138 }