From 16d2e7b8fc1463789342d7ea84195e7289c87a45 Mon Sep 17 00:00:00 2001 From: Gregory Wells Date: Tue, 19 Aug 2025 16:50:16 -0400 Subject: [PATCH] SRGB in OpenGL --- .../src/commands/commands/opengl_commands.cpp | 13 +++++++++++-- projects/apis/opengl/src/present/opengl_present.c | 4 ++++ .../presentation_queue/opengl_presentation_queue.c | 1 + .../presentation_queue/opengl_presentation_queue.h | 1 + .../src/renderpass/opengl_render_pass_descriptor.c | 4 ++-- .../src/renderpass/opengl_render_pass_descriptor.h | 2 +- projects/apis/opengl/src/surface/opengl_surface.c | 4 ++-- projects/apis/opengl/src/textures/opengl_texture.c | 2 ++ 8 files changed, 24 insertions(+), 7 deletions(-) diff --git a/projects/apis/opengl/src/commands/commands/opengl_commands.cpp b/projects/apis/opengl/src/commands/commands/opengl_commands.cpp index b51191f..f6d6611 100644 --- a/projects/apis/opengl/src/commands/commands/opengl_commands.cpp +++ b/projects/apis/opengl/src/commands/commands/opengl_commands.cpp @@ -6,23 +6,29 @@ #include #include "buffer/opengl_buffer.h" #include "graphics_pipeline/opengl_graphics_pipeline.h" +#include "renderpass/opengl_render_pass_descriptor.h" -GN_CPP_FUNCTION void openglBeginRenderPass(gnCommandBuffer buffer, gnRenderPassInfo passInfo) { +GN_CPP_FUNCTION void openglBeginRenderPass(gnCommandBuffer buffer, gnRenderPassInfo sPassInfo) { + gnRenderPassInfo passInfo = sPassInfo; gnClearValue* values = (gnClearValue*)malloc(sizeof(gnClearValue*) * passInfo.clearValueCount); memcpy(values, passInfo.clearValues, sizeof(gnClearValue*) * passInfo.clearValueCount); openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function([passInfo, values]{ glBindFramebuffer(GL_FRAMEBUFFER, passInfo.framebuffer->framebuffer->framebuffers[0]); + if (passInfo.renderPassDescriptor->renderPassDescriptor->subpasses[0].colorAttachments[0].format == GL_SRGB8_ALPHA8) glEnable(GL_FRAMEBUFFER_SRGB); glClearColor(values[0].r, values[0].g, values[0].b, values[0].a); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glViewport(passInfo.offset.x, passInfo.offset.y, passInfo.size.x, passInfo.size.y); + + free(values); })); } GN_CPP_FUNCTION void openglEndRenderPass(gnCommandBuffer buffer) { openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function([]{ glBindFramebuffer(GL_FRAMEBUFFER, 0); + glDisable(GL_FRAMEBUFFER_SRGB); })); } GN_CPP_FUNCTION void openglBindGraphicsPipeline(gnCommandBuffer commandBuffer, gnGraphicsPipeline graphicsPipeline) { @@ -74,7 +80,10 @@ GN_CPP_FUNCTION void openglDrawIndexed(gnCommandBufferHandle sBuffer, gnIndexTyp })); } GN_CPP_FUNCTION void openglBindUniform(gnCommandBufferHandle buffer, gnUniform uniform, uint32_t set, uint32_t dynamicOffsetCount, uint32_t* dynamicOffsets) { - + openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function([]{ + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, 5); + })); } GN_CPP_FUNCTION void openglPushConstant(gnCommandBufferHandle buffer, gnPushConstantLayout layout, void* data) { diff --git a/projects/apis/opengl/src/present/opengl_present.c b/projects/apis/opengl/src/present/opengl_present.c index 594f03a..18987fb 100644 --- a/projects/apis/opengl/src/present/opengl_present.c +++ b/projects/apis/opengl/src/present/opengl_present.c @@ -9,6 +9,8 @@ gnReturnCode openglPresent(gnOutputDeviceHandle device, gnPresentInfo info) { glBindVertexArray(0); + if (info.presentationQueues[i]->presentationQueue->format == GL_SRGB8_ALPHA8) glEnable(GL_FRAMEBUFFER_SRGB); + glUseProgram(device->outputDevice->shaderProgram); glBindBuffer(GL_ARRAY_BUFFER, device->outputDevice->buffer); glBindTexture(GL_TEXTURE_2D, GLuintArrayListAt(info.presentationQueues[i]->presentationQueue->textures, info.imageIndices[i])); @@ -18,6 +20,8 @@ gnReturnCode openglPresent(gnOutputDeviceHandle device, gnPresentInfo info) { glBindTexture(GL_TEXTURE_2D, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); swapBuffers(info.presentationQueues[i]->info.surface); + + glDisable(GL_FRAMEBUFFER_SRGB); } // for (uint32_t i = 0; i < info.presentationQueueCount; i++) { diff --git a/projects/apis/opengl/src/presentation_queue/opengl_presentation_queue.c b/projects/apis/opengl/src/presentation_queue/opengl_presentation_queue.c index 2427897..a09d053 100644 --- a/projects/apis/opengl/src/presentation_queue/opengl_presentation_queue.c +++ b/projects/apis/opengl/src/presentation_queue/opengl_presentation_queue.c @@ -9,6 +9,7 @@ gnReturnCode createOpenGLPresentationQueue(gnPresentationQueueHandle presentatio presentationQueue->images = malloc(sizeof(gnTexture) * presentationInfo.minImageCount); presentationQueue->presentationQueue->textures = GLuintArrayListCreate(); presentationQueue->presentationQueue->avaliableTextures = uint32_tArrayListCreate(); + presentationQueue->presentationQueue->format = glGryphnFormatToOpenGLInternalFormat(presentationInfo.format.format); for (int i = 0; i < presentationInfo.minImageCount; i++) { presentationQueue->images[i] = malloc(sizeof(struct gnTexture_t)); presentationQueue->images[i]->texture = malloc(sizeof(gnPlatformTexture)); diff --git a/projects/apis/opengl/src/presentation_queue/opengl_presentation_queue.h b/projects/apis/opengl/src/presentation_queue/opengl_presentation_queue.h index 14c5d38..9e2cde9 100644 --- a/projects/apis/opengl/src/presentation_queue/opengl_presentation_queue.h +++ b/projects/apis/opengl/src/presentation_queue/opengl_presentation_queue.h @@ -7,6 +7,7 @@ GN_ARRAY_LIST_HEADER(GLuint); typedef struct gnPlatformPresentationQueue_t { GLuintArrayList textures; uint32_tArrayList avaliableTextures; + GLenum format; } gnPlatformPresentationQueue_t; gnReturnCode createOpenGLPresentationQueue(gnPresentationQueueHandle presentationQueue, gnOutputDeviceHandle device, gnPresentationQueueInfo presentationInfo); diff --git a/projects/apis/opengl/src/renderpass/opengl_render_pass_descriptor.c b/projects/apis/opengl/src/renderpass/opengl_render_pass_descriptor.c index 7a34a09..e409539 100644 --- a/projects/apis/opengl/src/renderpass/opengl_render_pass_descriptor.c +++ b/projects/apis/opengl/src/renderpass/opengl_render_pass_descriptor.c @@ -26,7 +26,7 @@ gnReturnCode openglCreateRenderPass(gnRenderPassDescriptor renderPass, gnDevice .storeOperation = info.attachmentInfos[attachmentIndex].storeOperation, .attachmentIndex = attachmentIndex, .resolveAttachmentIndex = resolveAttachmentIndex, - .format = glGryphnFormatToOpenGLFormat(info.attachmentInfos[attachmentIndex].format) + .format = glGryphnFormatToOpenGLInternalFormat(info.attachmentInfos[attachmentIndex].format) }; } @@ -34,7 +34,7 @@ gnReturnCode openglCreateRenderPass(gnRenderPassDescriptor renderPass, gnDevice uint32_t depthAttachmentIndex = info.subpassInfos[i].depthAttachment->index; renderPass->renderPassDescriptor->subpasses[i].depthAttachment = (gnDepthAttachment){ .index = depthAttachmentIndex, - .format = glGryphnFormatToOpenGLFormat(info.attachmentInfos[depthAttachmentIndex].format), + .format = glGryphnFormatToOpenGLInternalFormat(info.attachmentInfos[depthAttachmentIndex].format), .loadOperation = info.attachmentInfos[depthAttachmentIndex].loadOperation, .storeOperation = info.attachmentInfos[depthAttachmentIndex].storeOperation, }; diff --git a/projects/apis/opengl/src/renderpass/opengl_render_pass_descriptor.h b/projects/apis/opengl/src/renderpass/opengl_render_pass_descriptor.h index 36d7111..5c0ec8d 100644 --- a/projects/apis/opengl/src/renderpass/opengl_render_pass_descriptor.h +++ b/projects/apis/opengl/src/renderpass/opengl_render_pass_descriptor.h @@ -8,7 +8,7 @@ typedef struct glColorAttachment { gnLoadOperation loadOperation; gnStoreOperation storeOperation; - GLint format; + GLenum format; uint32_t attachmentIndex; int resolveAttachmentIndex; // -1 = no attachment } glColorAttachment; diff --git a/projects/apis/opengl/src/surface/opengl_surface.c b/projects/apis/opengl/src/surface/opengl_surface.c index eb0bf33..7dca31c 100644 --- a/projects/apis/opengl/src/surface/opengl_surface.c +++ b/projects/apis/opengl/src/surface/opengl_surface.c @@ -109,11 +109,11 @@ gnSurfaceDetails genOpenGLSurfaceDetails( surfaceDetails.formatCount = 1; surfaceDetails.formats = malloc(sizeof(gnSurfaceFormat) * 2); surfaceDetails.formats[0] = (gnSurfaceFormat){ - .format = GN_FORMAT_RGBA8, + .format = GN_FORMAT_RGBA8_SRGB, .colorSpace = GN_COLOR_SPACE_SRGB_NONLINEAR }; surfaceDetails.formats[1] = (gnSurfaceFormat){ - .format = GN_FORMAT_RGBA8_SRGB, + .format = GN_FORMAT_RGBA8, .colorSpace = GN_COLOR_SPACE_SRGB_NONLINEAR }; diff --git a/projects/apis/opengl/src/textures/opengl_texture.c b/projects/apis/opengl/src/textures/opengl_texture.c index 8663472..1ca2f12 100644 --- a/projects/apis/opengl/src/textures/opengl_texture.c +++ b/projects/apis/opengl/src/textures/opengl_texture.c @@ -18,7 +18,9 @@ gnReturnCode openglCreateTexture(gnTexture texture, gnDevice device, const gnTex ); return GN_SUCCESS; } +#include "stdio.h" void openglTextureData(gnTextureHandle texture, void* pixelData) { + printf("OpenGL id: %u\n", texture->texture->id); glTextureSubImage2D( texture->texture->id, 0,