Use access bits better when mapping Meshes;

This commit is contained in:
bjorn 2017-08-02 02:06:36 -07:00
parent 3da48027ce
commit 03c01cb97a
5 changed files with 25 additions and 24 deletions

View File

@ -604,7 +604,7 @@ int l_lovrGraphicsNewMesh(lua_State* L) {
if (dataIndex) {
int count = lua_objlen(L, dataIndex);
MeshFormat format = lovrMeshGetVertexFormat(mesh);
char* vertex = lovrMeshMap(mesh, 0, count);
char* vertex = lovrMeshMap(mesh, 0, count, 0, 1);
for (int i = 0; i < count; i++) {
lua_rawgeti(L, dataIndex, i + 1);
@ -629,8 +629,6 @@ int l_lovrGraphicsNewMesh(lua_State* L) {
lua_pop(L, 1);
}
lovrMeshUnmap(mesh);
}
vec_deinit(&format);

View File

@ -82,7 +82,7 @@ int l_lovrMeshGetVertexCount(lua_State* L) {
int l_lovrMeshGetVertex(lua_State* L) {
Mesh* mesh = luax_checktype(L, 1, Mesh);
int index = luaL_checkint(L, 2) - 1;
char* vertex = lovrMeshMap(mesh, index, 1);
char* vertex = lovrMeshMap(mesh, index, 1, 1, 0);
MeshFormat format = lovrMeshGetVertexFormat(mesh);
int total = 0;
@ -112,7 +112,7 @@ int l_lovrMeshSetVertex(lua_State* L) {
}
MeshFormat format = lovrMeshGetVertexFormat(mesh);
char* vertex = lovrMeshMap(mesh, index, 1);
char* vertex = lovrMeshMap(mesh, index, 1, 0, 1);
// Unwrap table
int arg = 3;
@ -151,7 +151,7 @@ int l_lovrMeshGetVertexAttribute(lua_State* L) {
return luaL_error(L, "Invalid mesh attribute index: %d", attributeIndex + 1);
}
char* vertex = lovrMeshMap(mesh, vertexIndex, 1);
char* vertex = lovrMeshMap(mesh, vertexIndex, 1, 1, 0);
MeshAttribute attribute;
for (int i = 0; i <= attributeIndex; i++) {
@ -185,7 +185,7 @@ int l_lovrMeshSetVertexAttribute(lua_State* L) {
return luaL_error(L, "Invalid mesh attribute index: %d", attributeIndex + 1);
}
char* vertex = lovrMeshMap(mesh, vertexIndex, 1);
char* vertex = lovrMeshMap(mesh, vertexIndex, 1, 0, 1);
int arg = 4;
for (int i = 0; i <= attributeIndex; i++) {
@ -219,7 +219,7 @@ int l_lovrMeshSetVertices(lua_State* L) {
return luaL_error(L, "Mesh can only hold %d vertices", maxVertices);
}
void* vertices = lovrMeshMap(mesh, start, vertexCount);
void* vertices = lovrMeshMap(mesh, start, vertexCount, 0, 1);
char* vertex = vertices;
for (int i = 0; i < vertexCount; i++) {
@ -246,7 +246,7 @@ int l_lovrMeshSetVertices(lua_State* L) {
int l_lovrMeshGetVertexMap(lua_State* L) {
Mesh* mesh = luax_checktype(L, 1, Mesh);
int count;
size_t count;
unsigned int* indices = lovrMeshGetVertexMap(mesh, &count);
if (count == 0) {
@ -255,7 +255,7 @@ int l_lovrMeshGetVertexMap(lua_State* L) {
}
lua_newtable(L);
for (int i = 0; i < count; i++) {
for (size_t i = 0; i < count; i++) {
lua_pushinteger(L, indices[i] + 1);
lua_rawseti(L, -2, i + 1);
}

View File

@ -39,7 +39,7 @@ static void lovrMeshBindAttributes(Mesh* mesh) {
mesh->attributesDirty = 0;
}
Mesh* lovrMeshCreate(int count, MeshFormat* format, MeshDrawMode drawMode, MeshUsage usage) {
Mesh* lovrMeshCreate(size_t count, MeshFormat* format, MeshDrawMode drawMode, MeshUsage usage) {
Mesh* mesh = lovrAlloc(sizeof(Mesh), lovrMeshDestroy);
if (!mesh) return NULL;
@ -157,12 +157,12 @@ int lovrMeshGetVertexSize(Mesh* mesh) {
return mesh->stride;
}
unsigned int* lovrMeshGetVertexMap(Mesh* mesh, int* count) {
unsigned int* lovrMeshGetVertexMap(Mesh* mesh, size_t* count) {
*count = mesh->map.length;
return mesh->map.data;
}
void lovrMeshSetVertexMap(Mesh* mesh, unsigned int* map, int count) {
void lovrMeshSetVertexMap(Mesh* mesh, unsigned int* map, size_t count) {
if (count == 0 || !map) {
vec_clear(&mesh->map);
} else {
@ -223,7 +223,7 @@ void lovrMeshGetDrawRange(Mesh* mesh, int* start, int* count) {
}
int lovrMeshSetDrawRange(Mesh* mesh, int start, int count) {
if (start < 0 || count < 0 || start + count > mesh->count) {
if (start < 0 || count < 0 || (size_t) start + count > mesh->count) {
return 1;
}
@ -250,7 +250,7 @@ void lovrMeshSetTexture(Mesh* mesh, Texture* texture) {
}
}
void* lovrMeshMap(Mesh* mesh, int start, int count) {
void* lovrMeshMap(Mesh* mesh, int start, size_t count, int read, int write) {
#ifdef EMSCRIPTEN
mesh->isMapped = 1;
mesh->mapStart = start;
@ -264,7 +264,10 @@ void* lovrMeshMap(Mesh* mesh, int start, int count) {
mesh->isMapped = 1;
mesh->mapStart = start;
mesh->mapCount = count;
GLbitfield access = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
GLbitfield access = 0;
access |= read ? GL_MAP_READ_BIT : 0;
access |= write ? GL_MAP_WRITE_BIT : 0;
access |= (write && start == 0 && count == mesh->count) ? GL_MAP_INVALIDATE_BUFFER_BIT : 0;
glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
return glMapBufferRange(GL_ARRAY_BUFFER, start * mesh->stride, count * mesh->stride, access);
#endif
@ -280,7 +283,7 @@ void lovrMeshUnmap(Mesh* mesh) {
#ifdef EMSCRIPTEN
int start = mesh->mapStart * mesh->stride;
int count = mesh->mapCount * mesh->stride;
size_t count = mesh->mapCount * mesh->stride;
glBufferSubData(GL_ARRAY_BUFFER, start, count, (char*) mesh->data + start);
#else
glUnmapBuffer(GL_ARRAY_BUFFER);

View File

@ -36,13 +36,13 @@ typedef vec_t(MeshAttribute) MeshFormat;
typedef struct {
Ref ref;
void* data;
int count;
size_t count;
int stride;
int enabledAttributes;
int attributesDirty;
int isMapped;
int mapStart;
int mapCount;
size_t mapCount;
MeshFormat format;
MeshDrawMode drawMode;
MeshUsage usage;
@ -57,7 +57,7 @@ typedef struct {
Shader* lastShader;
} Mesh;
Mesh* lovrMeshCreate(int count, MeshFormat* format, MeshDrawMode drawMode, MeshUsage usage);
Mesh* lovrMeshCreate(size_t count, MeshFormat* format, MeshDrawMode drawMode, MeshUsage usage);
void lovrMeshDestroy(const Ref* ref);
void lovrMeshDraw(Mesh* mesh, mat4 transform);
MeshFormat lovrMeshGetVertexFormat(Mesh* mesh);
@ -65,8 +65,8 @@ MeshDrawMode lovrMeshGetDrawMode(Mesh* mesh);
int lovrMeshSetDrawMode(Mesh* mesh, MeshDrawMode drawMode);
int lovrMeshGetVertexCount(Mesh* mesh);
int lovrMeshGetVertexSize(Mesh* mesh);
unsigned int* lovrMeshGetVertexMap(Mesh* mesh, int* count);
void lovrMeshSetVertexMap(Mesh* mesh, unsigned int* map, int count);
unsigned int* lovrMeshGetVertexMap(Mesh* mesh, size_t* count);
void lovrMeshSetVertexMap(Mesh* mesh, unsigned int* map, size_t count);
int lovrMeshIsAttributeEnabled(Mesh* mesh, const char* name);
void lovrMeshSetAttributeEnabled(Mesh* mesh, const char* name, int enabled);
int lovrMeshIsRangeEnabled(Mesh* mesh);
@ -75,5 +75,5 @@ void lovrMeshGetDrawRange(Mesh* mesh, int* start, int* count);
int lovrMeshSetDrawRange(Mesh* mesh, int start, int count);
Texture* lovrMeshGetTexture(Mesh* mesh);
void lovrMeshSetTexture(Mesh* mesh, Texture* texture);
void* lovrMeshMap(Mesh* mesh, int start, int count);
void* lovrMeshMap(Mesh* mesh, int start, size_t count, int read, int write);
void lovrMeshUnmap(Mesh* mesh);

View File

@ -105,7 +105,7 @@ Model* lovrModelCreate(ModelData* modelData) {
}
model->mesh = lovrMeshCreate(vertices.length / components, &format, MESH_TRIANGLES, MESH_STATIC);
void* data = lovrMeshMap(model->mesh, 0, vertices.length / components);
void* data = lovrMeshMap(model->mesh, 0, vertices.length / components, 0, 1);
memcpy(data, vertices.data, vertices.length * sizeof(float));
lovrMeshUnmap(model->mesh);
lovrMeshSetVertexMap(model->mesh, indices.data, indices.length);