1 module evael.renderer.gl.gl_buffer;
2 
3 import evael.renderer.gl.gl_wrapper;
4 import evael.renderer.gl.gl_enum_converter;
5 
6 import evael.renderer.graphics_buffer;
7 import evael.renderer.enums.buffer_type;
8 
9 alias VertexBuffer = GLBuffer!(BufferType.Vertex);
10 alias IndexBuffer = GLBuffer!(BufferType.Index);
11 alias UniformBuffer = GLBuffer!(BufferType.Uniform);
12     
13 class GLBuffer(BufferType type, uint usage = GL_DYNAMIC_DRAW) : GraphicsBuffer!type
14 {
15     /**
16      * GLBuffer constructor.
17      */
18     @nogc
19     public this(in size_t size, void* data = null)
20     {
21         super(size);
22         
23         this.m_internalType = GLEnumConverter.bufferType(type);
24 
25         gl.GenBuffers(1, &this.m_id);
26         gl.BindBuffer(this.m_internalType, this.m_id);
27         gl.BufferData(this.m_internalType, size, data, usage);
28     }
29 
30     /**
31      * GLBuffer destructor.
32      */
33     @nogc
34     public ~this()
35     {
36         gl.DeleteBuffers(1, &this.m_id);
37     }
38 
39     /**
40      * Updates a subset of a buffer object's data store.
41      * Params:
42      *		 offet : offset into the buffer object's data store where data replacement will begin, measured in bytes
43      *		 size : size in bytes of the data store region being replaced
44      *		 data : pointer to the new data that will be copied into the data store
45      */
46     @nogc
47     public override void update(in ptrdiff_t offset, in ptrdiff_t size, in void* data) const nothrow
48     {
49         assert(offset + size <= this.m_size, "Updating buffer with invalid offset/size.");
50 
51         gl.BindBuffer(this.m_internalType, this.m_id);
52         gl.BufferSubData(this.m_internalType, offset, size, data);
53     }
54 }
55 
56