Start primitive restart;

This commit is contained in:
bjorn 2019-01-03 01:21:38 -08:00 committed by Bjorn Swenson
parent 6382c1f71c
commit 67727b092b
2 changed files with 42 additions and 3 deletions

View File

@ -455,7 +455,7 @@ void lovrGraphicsBatch(BatchRequest* req) {
// Try to find an existing batch to use
Batch* batch = NULL;
if (req->type != BATCH_LINES && (req->type != BATCH_MESH || req->params.mesh.instances == 1)) {
if (req->type != BATCH_MESH || req->params.mesh.instances == 1) {
for (int i = state.batchCount - 1; i >= 0; i--) {
Batch* b = &state.batches[i];
@ -1118,21 +1118,49 @@ void lovrGraphicsPoints(uint32_t count, float** vertices) {
}
void lovrGraphicsLine(uint32_t count, float** vertices) {
uint32_t indexCount = count + 1;
uint16_t* indices;
uint16_t baseVertex;
lovrGraphicsBatch(&(BatchRequest) {
.type = BATCH_LINES,
.vertexCount = count,
.vertices = vertices
.vertices = vertices,
.indexCount = indexCount,
.indices = &indices,
.baseVertex = &baseVertex
});
indices[0] = 0xffff;
for (uint32_t i = 1; i < indexCount; i++) {
indices[i] = baseVertex + i - 1;
}
}
void lovrGraphicsTriangle(DrawStyle style, Material* material, uint32_t count, float** vertices) {
uint32_t indexCount = style == STYLE_LINE ? (4 * count / 3) : 0;
uint16_t* indices;
uint16_t baseVertex;
lovrGraphicsBatch(&(BatchRequest) {
.type = BATCH_TRIANGLES,
.params.triangles.style = style,
.material = material,
.vertexCount = count,
.vertices = vertices
.vertices = vertices,
.indexCount = indexCount,
.indices = &indices,
.baseVertex = &baseVertex
});
if (style == STYLE_LINE) {
for (int i = 0; i < count; i += 3) {
*indices++ = 0xffff;
*indices++ = baseVertex + i + 0;
*indices++ = baseVertex + i + 1;
*indices++ = baseVertex + i + 2;
}
}
}
void lovrGraphicsPlane(DrawStyle style, Material* material, mat4 transform) {

View File

@ -56,6 +56,7 @@ static struct {
CompareMode depthTest;
bool depthWrite;
uint8_t lineWidth;
uint32_t primitiveRestart;
bool stencilEnabled;
CompareMode stencilMode;
int stencilValue;
@ -470,6 +471,12 @@ static void lovrGpuBindMesh(Mesh* mesh, Shader* shader, int divisorMultiplier) {
mesh->ibo = mesh->indexBuffer->id;
state.buffers[BUFFER_INDEX] = mesh->ibo;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->ibo);
uint32_t primitiveRestart = (1 << (mesh->indexSize * 8)) - 1;
if (state.primitiveRestart != primitiveRestart) {
state.primitiveRestart = primitiveRestart;
glPrimitiveRestartIndex(primitiveRestart);
}
}
if (mesh->flushEnd > 0) {
@ -927,6 +934,7 @@ void lovrGpuInit(bool srgb, getProcAddressProc getProcAddress) {
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &state.limits.blockAlign);
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &state.limits.textureAnisotropy);
glEnable(GL_PRIMITIVE_RESTART);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
state.srgb = srgb;
@ -953,6 +961,9 @@ void lovrGpuInit(bool srgb, getProcAddressProc getProcAddress) {
state.lineWidth = 1;
glLineWidth(state.lineWidth);
state.primitiveRestart = 0xffffffff;
glPrimitiveRestartIndex(state.primitiveRestart);
state.stencilEnabled = false;
state.stencilMode = COMPARE_NONE;
state.stencilValue = 0;