Some extra graphics state diffing;

This commit is contained in:
bjorn 2018-06-02 15:30:26 -07:00
parent c68d65441c
commit 7273811120
7 changed files with 36 additions and 35 deletions

View File

@ -976,10 +976,10 @@ int l_lovrGraphicsNewMesh(lua_State* L) {
Mesh* mesh = lovrMeshCreate(count, format, *drawMode, *usage);
if (dataIndex) {
VertexPointer vertices = lovrMeshMapVertices(mesh, 0, lua_objlen(L, dataIndex), false, true);
VertexPointer vertices = lovrMeshMapVertices(mesh, 0, lua_objlen(L, dataIndex), false, true, true);
luax_loadvertices(L, dataIndex, lovrMeshGetVertexFormat(mesh), vertices);
} else if (vertexData) {
VertexPointer vertices = lovrMeshMapVertices(mesh, 0, count, false, true);
VertexPointer vertices = lovrMeshMapVertices(mesh, 0, count, false, true, true);
memcpy(vertices.raw, vertexData->blob.data, vertexData->count * vertexData->format.stride);
}

View File

@ -95,7 +95,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;
VertexPointer vertex = lovrMeshMapVertices(mesh, index, 1, true, false);
VertexPointer vertex = lovrMeshMapVertices(mesh, index, 1, true, false, false);
VertexFormat* format = lovrMeshGetVertexFormat(mesh);
return luax_pushvertex(L, &vertex, format);
}
@ -105,7 +105,7 @@ int l_lovrMeshSetVertex(lua_State* L) {
int index = luaL_checkint(L, 2) - 1;
lovrAssert(index >= 0 && index < lovrMeshGetVertexCount(mesh), "Invalid mesh vertex index: %d", index + 1);
VertexFormat* format = lovrMeshGetVertexFormat(mesh);
VertexPointer vertex = lovrMeshMapVertices(mesh, index, 1, false, true);
VertexPointer vertex = lovrMeshMapVertices(mesh, index, 1, false, true, false);
luax_setvertex(L, 3, &vertex, format);
return 0;
}
@ -118,7 +118,7 @@ int l_lovrMeshGetVertexAttribute(lua_State* L) {
lovrAssert(vertexIndex >= 0 && vertexIndex < lovrMeshGetVertexCount(mesh), "Invalid mesh vertex: %d", vertexIndex + 1);
lovrAssert(attributeIndex >= 0 && attributeIndex < format->count, "Invalid mesh attribute: %d", attributeIndex + 1);
Attribute attribute = format->attributes[attributeIndex];
VertexPointer vertex = lovrMeshMapVertices(mesh, vertexIndex, 1, true, false);
VertexPointer vertex = lovrMeshMapVertices(mesh, vertexIndex, 1, true, false, false);
vertex.bytes += attribute.offset;
return luax_pushvertexattribute(L, &vertex, attribute);
}
@ -131,7 +131,7 @@ int l_lovrMeshSetVertexAttribute(lua_State* L) {
lovrAssert(vertexIndex >= 0 && vertexIndex < lovrMeshGetVertexCount(mesh), "Invalid mesh vertex: %d", vertexIndex + 1);
lovrAssert(attributeIndex >= 0 && attributeIndex < format->count, "Invalid mesh attribute: %d", attributeIndex + 1);
Attribute attribute = format->attributes[attributeIndex];
VertexPointer vertex = lovrMeshMapVertices(mesh, vertexIndex, 1, false, true);
VertexPointer vertex = lovrMeshMapVertices(mesh, vertexIndex, 1, false, true, false);
vertex.bytes += attribute.offset;
luax_setvertexattribute(L, 4, &vertex, attribute);
return 0;
@ -158,7 +158,7 @@ int l_lovrMeshSetVertices(lua_State* L) {
lovrAssert(start + count <= capacity, "Overflow in Mesh:setVertices: Mesh can only hold %d vertices", capacity);
lovrAssert(count <= sourceSize, "Cannot set %d vertices on Mesh: source only has %d vertices", count, sourceSize);
VertexPointer vertices = lovrMeshMapVertices(mesh, start, count, false, true);
VertexPointer vertices = lovrMeshMapVertices(mesh, start, count, false, true, false);
if (vertexData) {
memcpy(vertices.raw, vertexData->blob.data, count * format->stride);

View File

@ -54,7 +54,7 @@ void lovrGraphicsDestroy() {
lovrRelease(state.defaultFont);
lovrRelease(state.defaultTexture);
lovrRelease(state.mesh);
glDeleteBuffers(1, &state.cameraUBO);
glDeleteBuffers(1, &state.cameraBuffer);
memset(&state, 0, sizeof(GraphicsState));
}
@ -180,10 +180,10 @@ void lovrGraphicsCreateWindow(int w, int h, bool fullscreen, int msaa, const cha
vertexFormatAppend(&format, "lovrNormal", ATTR_FLOAT, 3);
vertexFormatAppend(&format, "lovrTexCoord", ATTR_FLOAT, 2);
state.mesh = lovrMeshCreate(64, format, MESH_TRIANGLES, MESH_STREAM);
glGenBuffers(1, &state.cameraUBO);
lovrGraphicsBindUniformBuffer(state.cameraUBO);
glGenBuffers(1, &state.cameraBuffer);
glBindBuffer(GL_UNIFORM_BUFFER, state.cameraBuffer);
glBufferData(GL_UNIFORM_BUFFER, 4 * 16 * sizeof(float), NULL, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, LOVR_SHADER_BLOCK_CAMERA, state.cameraUBO);
glBindBufferBase(GL_UNIFORM_BUFFER, LOVR_SHADER_BLOCK_CAMERA, state.cameraBuffer);
lovrGraphicsReset();
state.initialized = true;
}
@ -543,7 +543,7 @@ VertexPointer lovrGraphicsGetVertexPointer(uint32_t count) {
lovrMeshResize(state.mesh, capacity);
}
return lovrMeshMapVertices(state.mesh, 0, count, false, true);
return lovrMeshMapVertices(state.mesh, 0, count, false, true, true);
}
void lovrGraphicsPoints(uint32_t count) {
@ -1015,12 +1015,21 @@ void lovrGraphicsDraw(Mesh* mesh, mat4 transform, DefaultShader defaultShader, i
// Layer
Layer layer = state.layers[state.layer];
Canvas* canvas = state.canvasCount > 0 ? state.canvas[0] : layer.canvas;
int w = canvas ? canvas->texture.width : lovrGraphicsGetWidth();
int h = canvas ? canvas->texture.height : lovrGraphicsGetHeight();
lovrGraphicsBindFramebuffer(canvas ? canvas->framebuffer : 0);
lovrGraphicsSetViewport(0, 0, w, h);
lovrGraphicsBindUniformBuffer(state.cameraUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 4 * 16 * sizeof(float), &layer); // TODO
uint32_t width = canvas ? canvas->texture.width : lovrGraphicsGetWidth();
uint32_t height = canvas ? canvas->texture.height : lovrGraphicsGetHeight();
uint32_t viewport[4] = { 0, 0, width, height };
if (memcmp(state.viewport, viewport, 4 * sizeof(uint32_t))) {
memcpy(state.viewport, viewport, 4 * sizeof(uint32_t));
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
}
if (memcmp(state.cameraData, &layer, 4 * 16 * sizeof(float))) {
memcpy(state.cameraData, &layer, 4 * 16 * sizeof(float));
glBindBuffer(GL_UNIFORM_BUFFER, state.cameraBuffer);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 4 * 16 * sizeof(float), &layer);
}
// Transforms
if (transform) {
@ -1151,10 +1160,6 @@ void lovrGraphicsPopLayer() {
lovrAssert(--state.layer >= 0, "Layer underflow");
}
void lovrGraphicsSetViewport(int x, int y, int w, int h) {
glViewport(x, y, w, h);
}
Texture* lovrGraphicsGetTexture(int slot) {
return state.textures[slot];
}
@ -1216,13 +1221,6 @@ void lovrGraphicsBindVertexBuffer(uint32_t vertexBuffer) {
}
}
void lovrGraphicsBindUniformBuffer(uint32_t uniformBuffer) {
if (state.uniformBuffer != uniformBuffer) {
state.uniformBuffer = uniformBuffer;
glBindBuffer(GL_UNIFORM_BUFFER, uniformBuffer);
}
}
void lovrGraphicsBindIndexBuffer(uint32_t indexBuffer) {
if (state.indexBuffer != indexBuffer) {
state.indexBuffer = indexBuffer;

View File

@ -119,7 +119,8 @@ typedef struct {
Winding winding;
bool wireframe;
Mesh* mesh;
uint32_t cameraUBO;
uint32_t cameraBuffer;
float cameraData[4][16];
Layer layers[MAX_LAYERS];
int layer;
Texture* textures[MAX_TEXTURES];
@ -127,6 +128,7 @@ typedef struct {
bool stencilWriting;
uint32_t program;
uint32_t framebuffer;
uint32_t viewport[4];
uint32_t vertexArray;
uint32_t vertexBuffer;
uint32_t uniformBuffer;
@ -207,7 +209,6 @@ void lovrGraphicsDraw(Mesh* mesh, mat4 transform, DefaultShader shader, int inst
VertexPointer lovrGraphicsGetVertexPointer(uint32_t capacity);
void lovrGraphicsPushLayer(Layer layer);
void lovrGraphicsPopLayer();
void lovrGraphicsSetViewport(int x, int y, int w, int h);
Texture* lovrGraphicsGetTexture(int slot);
void lovrGraphicsBindTexture(Texture* texture, TextureType type, int slot);
Material* lovrGraphicsGetDefaultMaterial();
@ -215,5 +216,4 @@ void lovrGraphicsUseProgram(uint32_t program);
void lovrGraphicsBindFramebuffer(uint32_t framebuffer);
void lovrGraphicsBindVertexArray(uint32_t vao);
void lovrGraphicsBindVertexBuffer(uint32_t vbo);
void lovrGraphicsBindUniformBuffer(uint32_t ubo);
void lovrGraphicsBindIndexBuffer(uint32_t ibo);

View File

@ -195,7 +195,7 @@ void lovrMeshSetPose(Mesh* mesh, float* pose) {
mesh->pose = pose;
}
VertexPointer lovrMeshMapVertices(Mesh* mesh, uint32_t start, uint32_t count, bool read, bool write) {
VertexPointer lovrMeshMapVertices(Mesh* mesh, uint32_t start, uint32_t count, bool read, bool write, bool invalidate) {
#ifdef EMSCRIPTEN
mesh->mappedVertices = true;
mesh->mapStart = start;
@ -210,7 +210,10 @@ VertexPointer lovrMeshMapVertices(Mesh* mesh, uint32_t start, uint32_t count, bo
mesh->mapStart = start;
mesh->mapCount = count;
size_t stride = mesh->format.stride;
GLbitfield access = (read ? GL_MAP_READ_BIT : 0) | (write ? GL_MAP_WRITE_BIT : 0);
GLbitfield access = 0;
access |= read ? GL_MAP_READ_BIT : 0;
access |= write ? GL_MAP_WRITE_BIT : 0;
access |= invalidate ? GL_MAP_INVALIDATE_BUFFER_BIT : 0;
lovrGraphicsBindVertexBuffer(mesh->vbo);
return (VertexPointer) { .raw = glMapBufferRange(GL_ARRAY_BUFFER, start * stride, count * stride, access) };
#endif

View File

@ -83,7 +83,7 @@ Material* lovrMeshGetMaterial(Mesh* mesh);
void lovrMeshSetMaterial(Mesh* mesh, Material* material);
float* lovrMeshGetPose(Mesh* mesh);
void lovrMeshSetPose(Mesh* mesh, float* pose);
VertexPointer lovrMeshMapVertices(Mesh* mesh, uint32_t start, uint32_t count, bool read, bool write);
VertexPointer lovrMeshMapVertices(Mesh* mesh, uint32_t start, uint32_t count, bool read, bool write, bool invalidate);
void lovrMeshUnmapVertices(Mesh* mesh);
IndexPointer lovrMeshReadIndices(Mesh* mesh, uint32_t* count, size_t* size);
IndexPointer lovrMeshWriteIndices(Mesh* mesh, uint32_t count, size_t size);

View File

@ -59,7 +59,7 @@ Model* lovrModelCreate(ModelData* modelData) {
model->aabbDirty = true;
model->mesh = lovrMeshCreate(modelData->vertexData->count, modelData->vertexData->format, MESH_TRIANGLES, MESH_STATIC);
VertexPointer vertices = lovrMeshMapVertices(model->mesh, 0, modelData->vertexData->count, false, true);
VertexPointer vertices = lovrMeshMapVertices(model->mesh, 0, modelData->vertexData->count, false, true, true);
memcpy(vertices.raw, modelData->vertexData->blob.data, modelData->vertexData->count * modelData->vertexData->format.stride);
IndexPointer indices = lovrMeshWriteIndices(model->mesh, modelData->indexCount, modelData->indexSize);