fix buffer binding on OpenGL

This commit is contained in:
Gregory Wells
2025-08-19 21:43:12 -04:00
parent 380c5d056f
commit 01de997df5
3 changed files with 17 additions and 3 deletions

View File

@@ -10,6 +10,9 @@ gnReturnCode openglCommandPoolAllocateCommandBuffers(gnCommandBufferHandle* comm
commandBuffers[i]->commandBuffer = &pool->commandPool->commandBuffers[c]; commandBuffers[i]->commandBuffer = &pool->commandPool->commandBuffers[c];
commandBuffers[i]->commandBuffer->commmandRunner = openglCreateCommandRunner(); commandBuffers[i]->commandBuffer->commmandRunner = openglCreateCommandRunner();
commandBuffers[i]->commandBuffer->boundVertexBuffer = GN_NULL_HANDLE;
commandBuffers[i]->commandBuffer->boundIndexBuffer = GN_NULL_HANDLE;
// glGenBuffers(1, &commandBuffers[i]->commandBuffer->vertexBuffer); // glGenBuffers(1, &commandBuffers[i]->commandBuffer->vertexBuffer);
// glBindBuffer(GL_ARRAY_BUFFER, commandBuffers[i]->commandBuffer->vertexBuffer); // glBindBuffer(GL_ARRAY_BUFFER, commandBuffers[i]->commandBuffer->vertexBuffer);
// glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); // glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

View File

@@ -7,6 +7,7 @@ typedef struct gnPlatformCommandBuffer_t {
int index; int index;
openglCommandRunner commmandRunner; openglCommandRunner commmandRunner;
gnGraphicsPipeline boundGraphicsPipeline; gnGraphicsPipeline boundGraphicsPipeline;
gnBuffer boundVertexBuffer, boundIndexBuffer;
} gnPlatformCommandBuffer; } gnPlatformCommandBuffer;
gnReturnCode openglCommandPoolAllocateCommandBuffers(gnCommandBufferHandle* commandBuffers, uint32_t count, gnCommandPoolHandle pool); gnReturnCode openglCommandPoolAllocateCommandBuffers(gnCommandBufferHandle* commandBuffers, uint32_t count, gnCommandPoolHandle pool);

View File

@@ -36,8 +36,17 @@ GN_CPP_FUNCTION void openglBindGraphicsPipeline(gnCommandBuffer commandBuffer, g
gnCommandBuffer buffer = commandBuffer; gnCommandBuffer buffer = commandBuffer;
openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([buffer, pipeline]{ openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([buffer, pipeline]{
buffer->commandBuffer->boundGraphicsPipeline = pipeline; buffer->commandBuffer->boundGraphicsPipeline = pipeline;
glBindVertexArray(buffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->vertexArrayObject); glBindVertexArray(buffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->vertexArrayObject);
if (buffer->commandBuffer->boundVertexBuffer != GN_NULL_HANDLE)
glVertexArrayVertexBuffer(
buffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->vertexArrayObject, 0,
buffer->commandBuffer->boundVertexBuffer->buffer->id, 0,
buffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->stride
);
if (buffer->commandBuffer->boundIndexBuffer != GN_NULL_HANDLE)
glVertexArrayElementBuffer(buffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->vertexArrayObject, buffer->commandBuffer->boundIndexBuffer->buffer->id);
glUseProgram(buffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->program); glUseProgram(buffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->program);
})); }));
} }
@@ -54,12 +63,14 @@ GN_CPP_FUNCTION void openglBindBuffer(gnCommandBufferHandle buffer, gnBufferHand
openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([bType, bBuffer, bBufferToBind]{ openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([bType, bBuffer, bBufferToBind]{
if (bType == GN_VERTEX_BUFFER) { if (bType == GN_VERTEX_BUFFER) {
bBuffer->commandBuffer->boundVertexBuffer = bBufferToBind;
glVertexArrayVertexBuffer( glVertexArrayVertexBuffer(
bBuffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->vertexArrayObject, 0, bBuffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->vertexArrayObject, 0,
bBufferToBind->buffer->id, 0, bBuffer->commandBuffer->boundVertexBuffer->buffer->id, 0,
bBuffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->stride bBuffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->stride
); );
} else if (bType == GN_INDEX_BUFFER) { } else if (bType == GN_INDEX_BUFFER) {
bBuffer->commandBuffer->boundIndexBuffer = bBufferToBind;
glVertexArrayElementBuffer(bBuffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->vertexArrayObject, bBufferToBind->buffer->id); glVertexArrayElementBuffer(bBuffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->vertexArrayObject, bBufferToBind->buffer->id);
} }
})); }));
@@ -70,7 +81,6 @@ GN_CPP_FUNCTION void openglDraw(gnCommandBuffer buffer, int sVertexCount, int sF
glDrawArraysInstancedBaseInstance(GL_TRIANGLES, firstVertex, vertexCount, instanceCount, firstInstance); glDrawArraysInstancedBaseInstance(GL_TRIANGLES, firstVertex, vertexCount, instanceCount, firstInstance);
})); }));
} }
// #include "stdio.h"
GN_CPP_FUNCTION void openglDrawIndexed(gnCommandBufferHandle sBuffer, gnIndexType sType, int sIndexCount, int sFirstIndex, int sVertexOffset, int sInstanceCount, int sFirstInstance) { GN_CPP_FUNCTION void openglDrawIndexed(gnCommandBufferHandle sBuffer, gnIndexType sType, int sIndexCount, int sFirstIndex, int sVertexOffset, int sInstanceCount, int sFirstInstance) {
gnCommandBuffer buffer = sBuffer; gnCommandBuffer buffer = sBuffer;
gnIndexType type = sType; gnIndexType type = sType;