finish the most basic rendering pipeline

This commit is contained in:
Gregory Wells
2025-08-19 16:31:19 -04:00
parent 50d8a669b3
commit 7f6ec430de
9 changed files with 113 additions and 23 deletions

View File

@@ -14,11 +14,11 @@ gnCommandFunctions loadOpenGLCommandFunctions() {
._gnCommandBeginRenderPass = openglBeginRenderPass, ._gnCommandBeginRenderPass = openglBeginRenderPass,
._gnCommandEndRenderPass = openglEndRenderPass, ._gnCommandEndRenderPass = openglEndRenderPass,
._gnCommandBindGraphicsPipeline = NULL, ._gnCommandBindGraphicsPipeline = openglBindGraphicsPipeline,
._gnCommandSetViewport = NULL, ._gnCommandSetViewport = openglSetViewport,
._gnCommandSetScissor = NULL, ._gnCommandSetScissor = openglSetScissor,
._gnCommandBindUniform = NULL, ._gnCommandBindUniform = openglBindUniform,
._gnCommandPushConstant = NULL, ._gnCommandPushConstant = openglPushConstant,
._gnCommandBindBuffer = openglBindBuffer, ._gnCommandBindBuffer = openglBindBuffer,
._gnCommandDraw = openglDraw, ._gnCommandDraw = openglDraw,

View File

@@ -14,14 +14,16 @@ gnReturnCode openglCreateBuffer(gnBufferHandle buffer, gnDevice device, gnBuffer
glCreateBuffers(1, &buffer->buffer->id); glCreateBuffers(1, &buffer->buffer->id);
buffer->buffer->type = gnBufferTypeToGLEnum(info.type); buffer->buffer->type = gnBufferTypeToGLEnum(info.type);
buffer->buffer->usage = (info.usage == GN_DYNAMIC_DRAW) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW; buffer->buffer->usage = (info.usage == GN_DYNAMIC_DRAW) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW;
glNamedBufferData(buffer->buffer->id, info.size, NULL, buffer->buffer->usage); glNamedBufferStorage(buffer->buffer->id, info.size, NULL, GL_DYNAMIC_STORAGE_BIT | GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_COHERENT_BIT | GL_MAP_PERSISTENT_BIT);
return GN_SUCCESS; return GN_SUCCESS;
} }
void openglBufferData(gnBufferHandle buffer, size_t dataSize, void* data) { void openglBufferData(gnBufferHandle buffer, size_t dataSize, void* data) {
glNamedBufferData(buffer->buffer->id, dataSize, data, buffer->buffer->usage); openglBufferSubData(buffer, 0, dataSize, data);
} }
#include "stdio.h"
void openglBufferSubData(gnBufferHandle buffer, size_t offset, size_t dataSize, gnBufferMemory data) { void openglBufferSubData(gnBufferHandle buffer, size_t offset, size_t dataSize, gnBufferMemory data) {
glNamedBufferSubData(buffer->buffer->id, offset, dataSize, data); glBindBuffer(buffer->buffer->type, buffer->buffer->id);
glBufferSubData(buffer->buffer->type, 0, dataSize, data);
} }
void* openglMapBuffer(gnBufferHandle buffer) { void* openglMapBuffer(gnBufferHandle buffer) {
return glMapNamedBuffer(buffer->buffer->id, GL_READ_WRITE); return glMapNamedBuffer(buffer->buffer->id, GL_READ_WRITE);

View File

@@ -9,6 +9,15 @@ gnReturnCode openglCommandPoolAllocateCommandBuffers(gnCommandBufferHandle* comm
pool->commandPool->canBeReallocated[c] = GN_FALSE; pool->commandPool->canBeReallocated[c] = GN_FALSE;
commandBuffers[i]->commandBuffer = &pool->commandPool->commandBuffers[c]; commandBuffers[i]->commandBuffer = &pool->commandPool->commandBuffers[c];
commandBuffers[i]->commandBuffer->commmandRunner = openglCreateCommandRunner(); commandBuffers[i]->commandBuffer->commmandRunner = openglCreateCommandRunner();
// glGenBuffers(1, &commandBuffers[i]->commandBuffer->vertexBuffer);
// glBindBuffer(GL_ARRAY_BUFFER, commandBuffers[i]->commandBuffer->vertexBuffer);
// glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
// gl
// glGenBuffers(1, &commandBuffers[i]->commandBuffer->indexBuffer);
wasAbleToAllocate = GN_TRUE; wasAbleToAllocate = GN_TRUE;
break; break;
} }

View File

@@ -1,10 +1,12 @@
#pragma once #pragma once
#include "glad/glad.h"
#include "core/src/command/command_buffer/gryphn_command_buffer.h" #include "core/src/command/command_buffer/gryphn_command_buffer.h"
#include "commands/commands/opengl_command_runner.h" #include "commands/commands/opengl_command_runner.h"
typedef struct gnPlatformCommandBuffer_t { typedef struct gnPlatformCommandBuffer_t {
int index; int index;
openglCommandRunner commmandRunner; openglCommandRunner commmandRunner;
gnGraphicsPipeline boundGraphicsPipeline;
} gnPlatformCommandBuffer; } gnPlatformCommandBuffer;
gnReturnCode openglCommandPoolAllocateCommandBuffers(gnCommandBufferHandle* commandBuffers, uint32_t count, gnCommandPoolHandle pool); gnReturnCode openglCommandPoolAllocateCommandBuffers(gnCommandBufferHandle* commandBuffers, uint32_t count, gnCommandPoolHandle pool);

View File

@@ -5,6 +5,7 @@
#include "framebuffer/opengl_framebuffer.h" #include "framebuffer/opengl_framebuffer.h"
#include <string.h> #include <string.h>
#include "buffer/opengl_buffer.h" #include "buffer/opengl_buffer.h"
#include "graphics_pipeline/opengl_graphics_pipeline.h"
GN_CPP_FUNCTION void openglBeginRenderPass(gnCommandBuffer buffer, gnRenderPassInfo passInfo) { GN_CPP_FUNCTION void openglBeginRenderPass(gnCommandBuffer buffer, gnRenderPassInfo passInfo) {
gnClearValue* values = (gnClearValue*)malloc(sizeof(gnClearValue*) * passInfo.clearValueCount); gnClearValue* values = (gnClearValue*)malloc(sizeof(gnClearValue*) * passInfo.clearValueCount);
@@ -13,7 +14,7 @@ GN_CPP_FUNCTION void openglBeginRenderPass(gnCommandBuffer buffer, gnRenderPassI
openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([passInfo, values]{ openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([passInfo, values]{
glBindFramebuffer(GL_FRAMEBUFFER, passInfo.framebuffer->framebuffer->framebuffers[0]); glBindFramebuffer(GL_FRAMEBUFFER, passInfo.framebuffer->framebuffer->framebuffers[0]);
glClearColor(values[0].r, values[0].g, values[0].b, values[0].a); glClearColor(values[0].r, values[0].g, values[0].b, values[0].a);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glViewport(passInfo.offset.x, passInfo.offset.y, passInfo.size.x, passInfo.size.y); glViewport(passInfo.offset.x, passInfo.offset.y, passInfo.size.x, passInfo.size.y);
free(values); free(values);
@@ -24,23 +25,57 @@ GN_CPP_FUNCTION void openglEndRenderPass(gnCommandBuffer buffer) {
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
})); }));
} }
GN_CPP_FUNCTION void openglBindGraphicsPipeline(gnCommandBuffer buffer, gnGraphicsPipeline graphicsPipeline); GN_CPP_FUNCTION void openglBindGraphicsPipeline(gnCommandBuffer commandBuffer, gnGraphicsPipeline graphicsPipeline) {
GN_CPP_FUNCTION void openglSetViewport(gnCommandBuffer buffer, gnViewport viewport); gnGraphicsPipeline pipeline = graphicsPipeline;
GN_CPP_FUNCTION void openglSetScissor(gnCommandBuffer buffer, gnScissor scissor); gnCommandBuffer buffer = commandBuffer;
GN_CPP_FUNCTION void openglBindBuffer(gnCommandBufferHandle buffer, gnBufferHandle bufferToBind, gnBufferType type) { openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([buffer, pipeline]{
openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([&]{ buffer->commandBuffer->boundGraphicsPipeline = pipeline;
glBindBuffer(gnBufferTypeToGLEnum(type), bufferToBind->buffer->id);
glBindVertexArray(buffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->vertexArrayObject);
glUseProgram(buffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->program);
})); }));
} }
GN_CPP_FUNCTION void openglDraw(gnCommandBuffer buffer, int vertexCount, int firstVertex, int instanceCount, int firstInstance) { GN_CPP_FUNCTION void openglSetViewport(gnCommandBuffer buffer, gnViewport viewport) {
openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([&]{
}
GN_CPP_FUNCTION void openglSetScissor(gnCommandBuffer buffer, gnScissor scissor) {
}
GN_CPP_FUNCTION void openglBindBuffer(gnCommandBufferHandle buffer, gnBufferHandle bufferToBind, gnBufferType type) {
gnBufferType bType = type;
gnCommandBufferHandle bBuffer = buffer;
gnBufferHandle bBufferToBind = bufferToBind;
openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([bType, bBuffer, bBufferToBind]{
if (bType == GN_VERTEX_BUFFER) {
glVertexArrayVertexBuffer(
bBuffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->vertexArrayObject, 0,
bBufferToBind->buffer->id, 0,
bBuffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->stride
);
} else if (bType == GN_INDEX_BUFFER) {
glVertexArrayElementBuffer(bBuffer->commandBuffer->boundGraphicsPipeline->graphicsPipeline->vertexArrayObject, bBufferToBind->buffer->id);
}
}));
}
GN_CPP_FUNCTION void openglDraw(gnCommandBuffer buffer, int sVertexCount, int sFirstVertex, int sInstanceCount, int sFirstInstance) {
int vertexCount = sVertexCount, firstVertex = sFirstVertex, instanceCount = sInstanceCount, firstInstance = sFirstInstance;
openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([vertexCount, firstVertex, instanceCount, firstInstance]{
glDrawArraysInstancedBaseInstance(GL_TRIANGLES, firstVertex, vertexCount, instanceCount, firstInstance); glDrawArraysInstancedBaseInstance(GL_TRIANGLES, firstVertex, vertexCount, instanceCount, firstInstance);
})); }));
} }
GN_CPP_FUNCTION void openglDrawIndexed(gnCommandBufferHandle buffer, gnIndexType type, int indexCount, int firstIndex, int vertexOffset, int instanceCount, int firstInstance) { // #include "stdio.h"
openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([&]{ GN_CPP_FUNCTION void openglDrawIndexed(gnCommandBufferHandle sBuffer, gnIndexType sType, int sIndexCount, int sFirstIndex, int sVertexOffset, int sInstanceCount, int sFirstInstance) {
glDrawElementsInstancedBaseVertexBaseInstance(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, (void*)(sizeof(GLuint) * firstIndex), instanceCount, vertexOffset, firstInstance); gnCommandBuffer buffer = sBuffer;
gnIndexType type = sType;
int indexCount = sIndexCount, firstIndex = sFirstIndex, vertexOffset = sVertexOffset, instanceCount = sInstanceCount, firstInstance = sFirstInstance;
openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([buffer, type, indexCount, firstIndex, instanceCount, vertexOffset, firstInstance]{
glDrawElementsInstancedBaseVertexBaseInstance(GL_TRIANGLES, indexCount, (type == GN_UINT16) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(sizeof(GLuint) * firstIndex), instanceCount, vertexOffset, firstInstance);
})); }));
} }
GN_CPP_FUNCTION void openglBindUniform(gnCommandBufferHandle buffer, gnUniform uniform, uint32_t set, uint32_t dynamicOffsetCount, uint32_t* dynamicOffsets); GN_CPP_FUNCTION void openglBindUniform(gnCommandBufferHandle buffer, gnUniform uniform, uint32_t set, uint32_t dynamicOffsetCount, uint32_t* dynamicOffsets) {
GN_CPP_FUNCTION void openglBindVertexBytes(gnCommandBufferHandle buffer, gnPushConstantLayout layout, void* data);
}
GN_CPP_FUNCTION void openglPushConstant(gnCommandBufferHandle buffer, gnPushConstantLayout layout, void* data) {
}

View File

@@ -11,4 +11,4 @@ GN_CPP_FUNCTION void openglBindBuffer(gnCommandBufferHandle buffer, gnBufferHand
GN_CPP_FUNCTION void openglDraw(gnCommandBuffer buffer, int vertexCount, int firstVertex, int instanceCount, int firstInstance); GN_CPP_FUNCTION void openglDraw(gnCommandBuffer buffer, int vertexCount, int firstVertex, int instanceCount, int firstInstance);
GN_CPP_FUNCTION void openglDrawIndexed(gnCommandBufferHandle buffer, gnIndexType type, int indexCount, int firstIndex, int vertexOffset, int instanceCount, int firstInstance); GN_CPP_FUNCTION void openglDrawIndexed(gnCommandBufferHandle buffer, gnIndexType type, int indexCount, int firstIndex, int vertexOffset, int instanceCount, int firstInstance);
GN_CPP_FUNCTION void openglBindUniform(gnCommandBufferHandle buffer, gnUniform uniform, uint32_t set, uint32_t dynamicOffsetCount, uint32_t* dynamicOffsets); GN_CPP_FUNCTION void openglBindUniform(gnCommandBufferHandle buffer, gnUniform uniform, uint32_t set, uint32_t dynamicOffsetCount, uint32_t* dynamicOffsets);
GN_CPP_FUNCTION void openglBindVertexBytes(gnCommandBufferHandle buffer, gnPushConstantLayout layout, void* data); GN_CPP_FUNCTION void openglPushConstant(gnCommandBufferHandle buffer, gnPushConstantLayout layout, void* data);

View File

@@ -1,5 +1,8 @@
#include "opengl_graphics_pipeline.h" #include "opengl_graphics_pipeline.h"
#include "shaders/opengl_shader_module.h" #include "shaders/opengl_shader_module.h"
#include "core/src/instance/gryphn_instance.h"
#include "stdio.h"
gnReturnCode openglCreateGraphicsPipeline(gnGraphicsPipeline graphicsPipeline, gnOutputDevice device, gnGraphicsPipelineInfo info) { gnReturnCode openglCreateGraphicsPipeline(gnGraphicsPipeline graphicsPipeline, gnOutputDevice device, gnGraphicsPipelineInfo info) {
graphicsPipeline->graphicsPipeline = malloc(sizeof(gnPlatformGraphicsPipeline)); graphicsPipeline->graphicsPipeline = malloc(sizeof(gnPlatformGraphicsPipeline));
@@ -7,6 +10,40 @@ gnReturnCode openglCreateGraphicsPipeline(gnGraphicsPipeline graphicsPipeline, g
for (int i = 0; i < info.shaderModuleCount; i++) for (int i = 0; i < info.shaderModuleCount; i++)
glAttachShader(graphicsPipeline->graphicsPipeline->program, info.shaderModules[i]->shaderModule->id); glAttachShader(graphicsPipeline->graphicsPipeline->program, info.shaderModules[i]->shaderModule->id);
glLinkProgram(graphicsPipeline->graphicsPipeline->program); glLinkProgram(graphicsPipeline->graphicsPipeline->program);
GLint linked;
glGetProgramiv(graphicsPipeline->graphicsPipeline->program, GL_LINK_STATUS, &linked);
if (!linked) {
GLchar infoLog[512];
glGetProgramInfoLog(graphicsPipeline->graphicsPipeline->program, 512, NULL, infoLog);
gnDebuggerSetErrorMessage(device->instance->debugger, (gnMessageData){
.message = gnCreateString(infoLog)
});
} else {
gnDebuggerSetVerboseMessage(&device->instance->debugger, (gnMessageData){
.message = gnCreateString("Successfully linked program")
});
}
glCreateVertexArrays(1, &graphicsPipeline->graphicsPipeline->vertexArrayObject);
glVertexArrayAttribFormat(graphicsPipeline->graphicsPipeline->vertexArrayObject, 0, 3, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(graphicsPipeline->graphicsPipeline->vertexArrayObject, 0, 0);
// Attribute 1: texcoord (2 floats)
glVertexArrayAttribFormat(graphicsPipeline->graphicsPipeline->vertexArrayObject, 1, 2, GL_FLOAT, GL_FALSE, sizeof(float)*3);
glVertexArrayAttribBinding(graphicsPipeline->graphicsPipeline->vertexArrayObject, 1, 0);
// Attribute 2: color (3 floats)
glVertexArrayAttribFormat(graphicsPipeline->graphicsPipeline->vertexArrayObject, 2, 3, GL_FLOAT, GL_FALSE, sizeof(float)*5);
glVertexArrayAttribBinding(graphicsPipeline->graphicsPipeline->vertexArrayObject, 2, 0);
graphicsPipeline->graphicsPipeline->stride = (sizeof(float) * 8);
// Enable them
glEnableVertexArrayAttrib(graphicsPipeline->graphicsPipeline->vertexArrayObject, 0);
glEnableVertexArrayAttrib(graphicsPipeline->graphicsPipeline->vertexArrayObject, 1);
glEnableVertexArrayAttrib(graphicsPipeline->graphicsPipeline->vertexArrayObject, 2);
return GN_SUCCESS; return GN_SUCCESS;
} }
void openglDestroyGraphicsPipeline(gnGraphicsPipeline graphicsPipeline) { void openglDestroyGraphicsPipeline(gnGraphicsPipeline graphicsPipeline) {

View File

@@ -4,6 +4,9 @@
typedef struct gnPlatformGraphicsPipeline_t { typedef struct gnPlatformGraphicsPipeline_t {
GLuint program; GLuint program;
GLuint vertexArrayObject;
GLsizei stride;
} gnPlatformGraphicsPipeline; } gnPlatformGraphicsPipeline;
gnReturnCode openglCreateGraphicsPipeline(gnGraphicsPipeline graphicsPipeline, gnOutputDevice device, gnGraphicsPipelineInfo info); gnReturnCode openglCreateGraphicsPipeline(gnGraphicsPipeline graphicsPipeline, gnOutputDevice device, gnGraphicsPipelineInfo info);

View File

@@ -7,6 +7,8 @@ gnReturnCode openglPresent(gnOutputDeviceHandle device, gnPresentInfo info) {
for (uint32_t i =0 ; i < info.presentationQueueCount; i++) { for (uint32_t i =0 ; i < info.presentationQueueCount; i++) {
uint32_tArrayListAdd(info.presentationQueues[i]->presentationQueue->avaliableTextures, info.imageIndices[i]); uint32_tArrayListAdd(info.presentationQueues[i]->presentationQueue->avaliableTextures, info.imageIndices[i]);
glBindVertexArray(0);
glUseProgram(device->outputDevice->shaderProgram); glUseProgram(device->outputDevice->shaderProgram);
glBindBuffer(GL_ARRAY_BUFFER, device->outputDevice->buffer); glBindBuffer(GL_ARRAY_BUFFER, device->outputDevice->buffer);
glBindTexture(GL_TEXTURE_2D, GLuintArrayListAt(info.presentationQueues[i]->presentationQueue->textures, info.imageIndices[i])); glBindTexture(GL_TEXTURE_2D, GLuintArrayListAt(info.presentationQueues[i]->presentationQueue->textures, info.imageIndices[i]));