Permalink
Browse files

Vertex Buffer Tweaks (#1355)

* Shrink the capacity of static vertex and index buffers on the CPU after upload
* Rename vertex_get_size to vertex_get_buffer_size
* Rename index_get_size to index_get_buffer_size
  • Loading branch information...
RobertBColton committed Aug 16, 2018
1 parent 16807a7 commit d490b5067a8b6f256c135e21ea187705fa414c9e
@@ -109,7 +109,7 @@ void graphics_prepare_buffer(const int buffer, const bool isIndex) {
if (dirty) {
ID3D11Buffer* bufferPeer = NULL;
auto it = bufferPeers.find(buffer);
size_t size = isIndex ? enigma_user::index_get_size(buffer) : enigma_user::vertex_get_size(buffer);
size_t size = isIndex ? enigma_user::index_get_buffer_size(buffer) : enigma_user::vertex_get_buffer_size(buffer);

// if we have already created a native "peer" for this user buffer,
// then we have to release it if it isn't big enough to hold the new contents
@@ -148,11 +148,9 @@ void graphics_prepare_buffer(const int buffer, const bool isIndex) {
}

if (isIndex) {
indexBuffers[buffer]->indices.clear();
indexBuffers[buffer]->dirty = false;
indexBuffers[buffer]->clearData();
} else {
vertexBuffers[buffer]->vertices.clear();
vertexBuffers[buffer]->dirty = false;
vertexBuffers[buffer]->clearData();
}
}
}
@@ -58,7 +58,7 @@ void graphics_prepare_vertex_buffer(const int buffer) {
if (vertexBuffer->dirty) {
LPDIRECT3DVERTEXBUFFER9 vertexBufferPeer = NULL;
auto it = vertexBufferPeers.find(buffer);
size_t size = enigma_user::vertex_get_size(buffer);
size_t size = enigma_user::vertex_get_buffer_size(buffer);

// if we have already created a native "peer" vbo for this user buffer,
// then we have to release it if it isn't big enough to hold the new contents
@@ -91,8 +91,7 @@ void graphics_prepare_vertex_buffer(const int buffer) {
memcpy(pVoid, vertexBuffer->vertices.data(), size);
vertexBufferPeer->Unlock();

vertexBuffer->vertices.clear();
vertexBuffer->dirty = false;
vertexBuffer->clearData();
}
}

@@ -104,7 +103,7 @@ void graphics_prepare_index_buffer(const int buffer) {
if (indexBuffer->dirty) {
LPDIRECT3DINDEXBUFFER9 indexBufferPeer = NULL;
auto it = indexBufferPeers.find(buffer);
size_t size = enigma_user::index_get_size(buffer);
size_t size = enigma_user::index_get_buffer_size(buffer);

// if we have already created a native "peer" ibo for this user buffer,
// then we have to release it if it isn't big enough to hold the new contents
@@ -137,8 +136,7 @@ void graphics_prepare_index_buffer(const int buffer) {
memcpy(pVoid, indexBuffer->indices.data(), size);
indexBufferPeer->Unlock();

indexBuffer->indices.clear();
indexBuffer->dirty = false;
indexBuffer->clearData();
}
}

@@ -150,7 +150,7 @@ void vertex_set_format(int buffer, int format) {
enigma::vertexBuffers[buffer]->format = format;
}

unsigned vertex_get_size(int buffer) {
unsigned vertex_get_buffer_size(int buffer) {
const enigma::VertexBuffer* vertexBuffer = enigma::vertexBuffers[buffer];

return vertexBuffer->getNumber() * sizeof(gs_scalar);
@@ -342,7 +342,7 @@ bool index_exists(int buffer) {
RESOURCE_EXISTS(buffer, indexBuffers);
}

unsigned index_get_size(int buffer) {
unsigned index_get_buffer_size(int buffer) {
return enigma::indexBuffers[buffer]->getNumber() * sizeof(uint16_t);
}

@@ -72,7 +72,7 @@ int vertex_create_buffer_ext(unsigned size);
void vertex_delete_buffer(int buffer);
bool vertex_exists(int buffer);
void vertex_set_format(int buffer, int format);
unsigned vertex_get_size(int buffer);
unsigned vertex_get_buffer_size(int buffer);
unsigned vertex_get_number(int buffer);
void vertex_freeze(int buffer, bool dynamic = false);
void vertex_clear(int buffer);
@@ -105,7 +105,7 @@ int index_create_buffer();
int index_create_buffer_ext(unsigned size);
void index_delete_buffer(int buffer);
bool index_exists(int buffer);
unsigned index_get_size(int buffer);
unsigned index_get_buffer_size(int buffer);
unsigned index_get_number(int buffer);
void index_freeze(int buffer, bool dynamic = false);
void index_clear(int buffer);
@@ -105,9 +105,26 @@ struct VertexBuffer {

VertexBuffer(): frozen(false), dirty(false), format(-1), number(0) {}

// returns the number of vertex elements in the buffer
int getNumber() const {
return dirty ? vertices.size() : number;
}

// intuitively clears the vertex data on the CPU side
// intended to be called by the backend so that static
// buffers shrink all CPU resources and stream buffers
// only clear them leaving the reserved capacity
// for future primitives to be specified
void clearData() {
if (frozen) {
// this will give us 0 size and 0 capacity
std::vector<enigma::VertexElement>().swap(vertices);
} else {
// this will give us 0 size but keep capacity
vertices.clear();
}
dirty = false; // we aren't dirty anymore
}
};

struct IndexBuffer {
@@ -124,9 +141,26 @@ struct IndexBuffer {

IndexBuffer(): frozen(false), dirty(false), type(-1), number(0) {}

// returns the number of index elements in the buffer
int getNumber() const {
return dirty ? indices.size() : number;
}

// intuitively clears the index data on the CPU side
// intended to be called by the backend so that static
// buffers shrink all CPU resources and stream buffers
// only clear them leaving the reserved capacity
// for future primitives to be specified
void clearData() {
if (frozen) {
// this will give us 0 size and 0 capacity
std::vector<uint16_t>().swap(indices);
} else {
// this will give us 0 size but keep capacity
indices.clear();
}
dirty = false; // we aren't dirty anymore
}
};

extern vector<VertexFormat*> vertexFormats;
@@ -44,13 +44,11 @@ GLvoid* graphics_prepare_buffer_array(const int buffer, const bool isIndex) {
if (isIndex) {
IndexBuffer* indexBuffer = indexBuffers[buffer];
indexBufferArrays[buffer] = indexBuffer->indices;
indexBuffer->indices.clear();
indexBuffer->dirty = false;
indexBuffer->clearData();
} else {
VertexBuffer* vertexBuffer = vertexBuffers[buffer];
vertexBufferArrays[buffer] = vertexBuffer->vertices;
vertexBuffer->vertices.clear();
vertexBuffer->dirty = false;
vertexBuffer->clearData();
}
}
if (isIndex) {
@@ -73,7 +71,7 @@ void graphics_prepare_buffer_peer(const int buffer, const bool isIndex) {
// if the contents of the buffer are dirty then we need to update
// our native buffer object "peer"
if (dirty) {
size_t size = isIndex ? enigma_user::index_get_size(buffer) : enigma_user::vertex_get_size(buffer);
size_t size = isIndex ? enigma_user::index_get_buffer_size(buffer) : enigma_user::vertex_get_buffer_size(buffer);

// if we haven't created a native "peer" for this buffer yet,
// then we need to do so now
@@ -95,11 +93,9 @@ void graphics_prepare_buffer_peer(const int buffer, const bool isIndex) {
glBufferData(target, size, data, usage);

if (isIndex) {
indexBuffers[buffer]->indices.clear();
indexBuffers[buffer]->dirty = false;
indexBuffers[buffer]->clearData();
} else {
vertexBuffers[buffer]->vertices.clear();
vertexBuffers[buffer]->dirty = false;
vertexBuffers[buffer]->clearData();
}
} else {
glBindBuffer(target, it->second);
@@ -82,7 +82,7 @@ void graphics_prepare_buffer(const int buffer, const bool isIndex) {
// if the contents of the buffer are dirty then we need to update
// our native buffer object "peer"
if (dirty) {
size_t size = isIndex ? enigma_user::index_get_size(buffer) : enigma_user::vertex_get_size(buffer);
size_t size = isIndex ? enigma_user::index_get_buffer_size(buffer) : enigma_user::vertex_get_buffer_size(buffer);

// if we haven't created a native "peer" for this buffer yet,
// then we need to do so now
@@ -108,11 +108,9 @@ void graphics_prepare_buffer(const int buffer, const bool isIndex) {
glBufferData(target, size, data, usage);

if (isIndex) {
indexBuffers[buffer]->indices.clear();
indexBuffers[buffer]->dirty = false;
indexBuffers[buffer]->clearData();
} else {
vertexBuffers[buffer]->vertices.clear();
vertexBuffers[buffer]->dirty = false;
vertexBuffers[buffer]->clearData();
}
} else {
if (isIndex) {

0 comments on commit d490b50

Please sign in to comment.