1 /** 2 Copyright: Copyright (c) 2017-2018 Andrey Penechko. 3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 module voxelman.graphics.vbo; 7 8 import voxelman.graphics.gl; 9 10 11 struct Vbo 12 { 13 import core.atomic; 14 shared static size_t numAllocated; 15 16 private GLuint handle_; 17 private size_t uploadedBytes_; 18 19 GLuint handle() const { 20 return handle_; 21 } 22 23 // data size in bytes 24 size_t uploadedBytes() const { 25 return uploadedBytes_; 26 } 27 28 bool isGenerated() const { 29 return handle_ != 0; 30 } 31 32 bool empty() const { 33 return uploadedBytes_ == 0; 34 } 35 36 void gen() { 37 if (isGenerated) return; 38 checkgl!glGenBuffers(1, &handle_); 39 atomicOp!"+="(numAllocated, 1); 40 } 41 42 void del() { 43 if (handle_ == 0) return; 44 atomicOp!"-="(numAllocated, 1); 45 checkgl!glDeleteBuffers(1, &handle_); 46 uploadedBytes_ = 0; 47 } 48 49 void bind() const { 50 assert(isGenerated); 51 checkgl!glBindBuffer(GL_ARRAY_BUFFER, handle_); 52 } 53 54 static void unbind() { 55 checkgl!glBindBuffer(GL_ARRAY_BUFFER, 0); 56 } 57 58 // requires binding 59 void uploadData(Vert)(const Vert[] data, int usage = GL_STATIC_DRAW) { 60 uploadedBytes_ = data.length*Vert.sizeof; 61 checkgl!glBufferData(GL_ARRAY_BUFFER, uploadedBytes_, data.ptr, usage); 62 } 63 }