Skip to content

Commit

Permalink
Revert back VertexBuffer code to original from bspguy
Browse files Browse the repository at this point in the history
  • Loading branch information
Karaulov committed Nov 26, 2022
1 parent cda4951 commit 294304e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 110 deletions.
145 changes: 61 additions & 84 deletions src/gl/VertexBuffer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "VertexBuffer.h"
#include "util.h"
#include <string.h>
#include "Renderer.h"

VertexAttr commonAttr[VBUF_FLAGBITS] =
{
Expand All @@ -22,8 +21,8 @@ VertexAttr commonAttr[VBUF_FLAGBITS] =
VertexAttr(3, GL_FLOAT, -1, GL_FALSE, ""), // POS_3F
};

VertexAttr::VertexAttr(int numValues, int valueType, int vhandle, int normalized, const char* varName)
: numValues(numValues), valueType(valueType), handle(vhandle), normalized(normalized), varName(varName)
VertexAttr::VertexAttr(int numValues, int valueType, int handle, int normalized, const char* varName)
: numValues(numValues), valueType(valueType), handle(handle), normalized(normalized), varName(varName)
{
switch (valueType)
{
Expand All @@ -47,30 +46,30 @@ VertexAttr::VertexAttr(int numValues, int valueType, int vhandle, int normalized
}
}

VertexBuffer::VertexBuffer(ShaderProgram* shaderProgram, int attFlags, const void* dat, GLsizei numVerts, int primitive)


VertexBuffer::VertexBuffer(ShaderProgram* shaderProgram, int attFlags, const void* dat, int numVerts, int primitive)
{
this->shaderProgram = shaderProgram;
this->primitive = primitive;
addAttributes(attFlags);
setData(dat, numVerts, primitive);
vboId = 0xFFFFFFFF;
setData(dat, numVerts);
vboId = (unsigned int)-1;
}

VertexBuffer::VertexBuffer(ShaderProgram* shaderProgram, int attFlags, int primitive)
{
numVerts = 0;
data = NULL;
vboId = 0xFFFFFFFF;
vboId = (unsigned int)-1;
this->shaderProgram = shaderProgram;
this->primitive = primitive;
addAttributes(attFlags);
}

VertexBuffer::~VertexBuffer()
{
VertexBuffer::~VertexBuffer() {
deleteBuffer();
if (ownData)
{
if (ownData) {
delete[] data;
}
}
Expand All @@ -86,34 +85,33 @@ void VertexBuffer::addAttributes(int attFlags)
commonAttr[i].handle = shaderProgram->vposID;
else if (i >= VBUF_COLOR_START)
commonAttr[i].handle = shaderProgram->vcolorID;
else
else if (i >= VBUF_TEX_START)
commonAttr[i].handle = shaderProgram->vtexID;
else
logf("Unused vertex buffer flag bit %d", i);

attribs.push_back(commonAttr[i]);
elementSize += commonAttr[i].size;
}
}
}

void VertexBuffer::addAttribute(int numValues, int valueType, int normalized, const char* varName)
{
void VertexBuffer::addAttribute(int numValues, int valueType, int normalized, const char* varName) {
VertexAttr attribute(numValues, valueType, -1, normalized, varName);

attribs.push_back(attribute);
elementSize += attribute.size;
}

void VertexBuffer::addAttribute(int type, const char* varName)
{
void VertexBuffer::addAttribute(int type, const char* varName) {

int idx = 0;
while (type >>= 1) // unroll for more speed...
{
idx++;
}

if (idx >= VBUF_FLAGBITS)
{
if (idx >= VBUF_FLAGBITS) {
logf("Invalid attribute type\n");
return;
}
Expand All @@ -126,28 +124,24 @@ void VertexBuffer::addAttribute(int type, const char* varName)
elementSize += attribute.size;
}

void VertexBuffer::setShader(ShaderProgram* program, bool hideErrors)
{
void VertexBuffer::setShader(ShaderProgram* program, bool hideErrors) {
shaderProgram = program;
attributesBound = false;
for (int i = 0; i < attribs.size(); i++)
{
if (attribs[i].varName[0] != '\0')
{
if (strlen(attribs[i].varName) > 0) {
attribs[i].handle = -1;
}
}

bindAttributes(hideErrors);
if (vboId != 0xFFFFFFFF)
{
if (vboId != -1) {
deleteBuffer();
upload();
}
}

void VertexBuffer::bindAttributes(bool hideErrors)
{
void VertexBuffer::bindAttributes(bool hideErrors) {
if (attributesBound)
return;

Expand All @@ -165,36 +159,27 @@ void VertexBuffer::bindAttributes(bool hideErrors)
attributesBound = true;
}

void VertexBuffer::setData(const void* _data, GLsizei _numVerts)
{
data = (unsigned char*)_data;
numVerts = _numVerts;
}

void VertexBuffer::setData(const void* _data, GLsizei _numVerts, int _primitive)
void VertexBuffer::setData(const void* _data, int _numVerts)
{
data = (unsigned char*)_data;
numVerts = _numVerts;
primitive = _primitive;
}

void VertexBuffer::upload()
{
void VertexBuffer::upload() {
shaderProgram->bind();
bindAttributes();

glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, elementSize * numVerts, data, GL_STATIC_DRAW);

size_t offset = 0;
int offset = 0;
for (int i = 0; i < attribs.size(); i++)
{
VertexAttr& a = attribs[i];
void* ptr = (void*)offset;
void* ptr = ((char*)0) + offset;
offset += a.size;
if (a.handle == -1)
{
if (a.handle == -1) {
continue;
}
glBindBuffer(GL_ARRAY_BUFFER, vboId);
Expand All @@ -205,37 +190,24 @@ void VertexBuffer::upload()
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void VertexBuffer::deleteBuffer()
{
if (vboId != 0xFFFFFFFF)
void VertexBuffer::deleteBuffer() {
if (vboId != (unsigned int)-1)
glDeleteBuffers(1, &vboId);
vboId = 0xFFFFFFFF;
vboId = (unsigned int)-1;
}

void VertexBuffer::drawRange(GLint start, GLsizei end)
void VertexBuffer::drawRange(int _primitive, int start, int end)
{
if (start < 0 || start > numVerts)
logf("Invalid start index: %d\n", start);
else if (end > numVerts || end < 0)
logf("Invalid end index: %d\n", end);
else if (end - start <= 0)
logf("Invalid draw range: %d -> %d\n", start, end);
else
{
char* offsetPtr = (char*)data;

shaderProgram->bind();
bindAttributes();

if (vboId != 0xFFFFFFFF)
{
glBindBuffer(GL_ARRAY_BUFFER, vboId);
offsetPtr = NULL;
}
shaderProgram->bind();
bindAttributes();

char* offsetPtr = (char*)data;
if (vboId != (unsigned int)-1) {
glBindBuffer(GL_ARRAY_BUFFER, vboId);
offsetPtr = NULL;
}
{
int offset = 0;

// Drop FPS below: FIXME
for (int i = 0; i < attribs.size(); i++)
{
VertexAttr& a = attribs[i];
Expand All @@ -246,31 +218,36 @@ void VertexBuffer::drawRange(GLint start, GLsizei end)
glEnableVertexAttribArray(a.handle);
glVertexAttribPointer(a.handle, a.numValues, a.valueType, a.normalized != 0, elementSize, ptr);
}
// Drop FPS above

glDrawArrays(primitive, start, end - start);
}

for (int i = 0; i < attribs.size(); i++)
{
VertexAttr& a = attribs[i];
if (a.handle == -1)
continue;
glDisableVertexAttribArray(a.handle);
}
if (start < 0 || start > numVerts)
logf("Invalid start index: %d\n", start);
else if (end > numVerts || end < 0)
logf("Invalid end index: %d\n", end);
else if (end - start <= 0)
logf("Invalid draw range: %d -> %d\n", start, end);
else
glDrawArrays(_primitive, start, end - start);

if (vboId != (unsigned int)-1) {
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

for (int i = 0; i < attribs.size(); i++)
{
VertexAttr& a = attribs[i];
if (a.handle == -1)
continue;
glDisableVertexAttribArray(a.handle);
}
for (int i = 0; i < attribs.size(); i++)
{
VertexAttr& a = attribs[i];
if (a.handle == -1)
continue;
glDisableVertexAttribArray(a.handle);
}
}

void VertexBuffer::drawFull()
void VertexBuffer::draw(int _primitive)
{
return drawRange(0, numVerts);
drawRange(_primitive, 0, numVerts);
}

void VertexBuffer::drawFull()
{
drawRange(primitive, 0, numVerts);
}
43 changes: 17 additions & 26 deletions src/gl/VertexBuffer.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
#pragma once
#include <GL/glew.h>

#include <vector>
#include "ShaderProgram.h"
#include "util.h"

// Combinable flags for setting common vertex attributes
#define TEX_2B (1 << 0) // 2D unsigned char texture coordinates
#define TEX_2B (1 << 0) // 2D byte texture coordinates
#define TEX_2S (1 << 1) // 2D short texture coordinates
#define TEX_2F (1 << 2) // 2D float texture coordinates
#define COLOR_3B (1 << 3) // RGB unsigned char color values
#define COLOR_3B (1 << 3) // RGB byte color values
#define COLOR_3F (1 << 4) // RGB float color values
#define COLOR_4B (1 << 5) // RGBA unsigned char color values
#define COLOR_4B (1 << 5) // RGBA byte color values
#define COLOR_4F (1 << 6) // RGBA float color values
#define NORM_3B (1 << 7) // 3D unsigned char normal coordinates
#define NORM_3B (1 << 7) // 3D byte normal coordinates
#define NORM_3F (1 << 8) // 3D float normal coordinates
#define POS_2B (1 << 9) // 2D unsigned char position coordinates
#define POS_2B (1 << 9) // 2D byte position coordinates
#define POS_2S (1 << 10) // 2D short position coordinates
#define POS_2I (1 << 11) // 2D integer position coordinates
#define POS_2F (1 << 12) // 2D float position coordinates
Expand All @@ -38,13 +37,10 @@ struct VertexAttr
int valueType; // Ex: GL_FLOAT
int handle; // location in shader program (-1 indicates invalid attribute)
int size; // size of the attribute in bytes
int normalized; // GL_TRUE/GL_FALSE Ex: unsigned char color values are normalized (0-255 = 0.0-1.0)
int normalized; // GL_TRUE/GL_FALSE Ex: byte color values are normalized (0-255 = 0.0-1.0)
const char* varName;

VertexAttr() : handle(-1)
{
numValues = valueType = handle = size = normalized = 0; varName = "";
}
VertexAttr() : handle(-1) {}

VertexAttr(int numValues, int valueType, int handle, int normalized, const char* varName);
};
Expand All @@ -55,42 +51,37 @@ class VertexBuffer
unsigned char* data = NULL;
std::vector<VertexAttr> attribs;
int elementSize;
GLsizei numVerts;
int numVerts;
int primitive;
bool ownData = false; // set to true if buffer should delete data on destruction

// Specify which common attributes to use. They will be located in the
// shader program. If passing data, note that data is not copied, but referenced
VertexBuffer(ShaderProgram* shaderProgram, int attFlags, int primitive);
VertexBuffer(ShaderProgram* shaderProgram, int attFlags, const void* dat, GLsizei numVerts, int primitive);
VertexBuffer(ShaderProgram* shaderProgram, int attFlags, int primitive = 0);
VertexBuffer(ShaderProgram* shaderProgram, int attFlags, const void* dat, int numVerts, int primitive = 0);
~VertexBuffer();

// Note: Data is not copied into the class - don't delete your data.
// Data will be deleted when the buffer is destroyed.
void setData(const void* data, GLsizei numVerts, int primitive);
void setData(const void* data, GLsizei numVerts);
void setData(const void* data, int numVerts);

void upload();
void deleteBuffer();
void setShader(ShaderProgram* program, bool hideErrors = false);
void drawRange(GLint start, GLsizei end);

void drawRange(int primitive, int start, int end);
void draw(int primitive);
void drawFull();

void addAttribute(int numValues, int valueType, int normalized, const char* varName);
void addAttribute(int type, const char* varName);
void bindAttributes(bool hideErrors = false); // find handles for all vertex attributes (call from main thread only)

private:
bool needDrawChecked = true;
bool needDraw = true;
vec3 camOrigin, camAngles;
double drawTime = 0.0;
ShaderProgram* shaderProgram = NULL; // for getting handles to vertex attributes
unsigned int vboId = 0xFFFFFFFF;

unsigned int vboId = (unsigned int)-1;
bool attributesBound = false;
GLuint drawQuery = 0xFFFFFFFF;

// add attributes according to the attribute flags
void addAttributes(int attFlags);
};

};

0 comments on commit 294304e

Please sign in to comment.