SRGB in OpenGL

This commit is contained in:
Gregory Wells
2025-08-19 16:50:16 -04:00
parent 7f6ec430de
commit 16d2e7b8fc
8 changed files with 24 additions and 7 deletions

View File

@@ -6,23 +6,29 @@
#include <string.h> #include <string.h>
#include "buffer/opengl_buffer.h" #include "buffer/opengl_buffer.h"
#include "graphics_pipeline/opengl_graphics_pipeline.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); gnClearValue* values = (gnClearValue*)malloc(sizeof(gnClearValue*) * passInfo.clearValueCount);
memcpy(values, passInfo.clearValues, sizeof(gnClearValue*) * passInfo.clearValueCount); memcpy(values, passInfo.clearValues, sizeof(gnClearValue*) * passInfo.clearValueCount);
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]);
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); glClearColor(values[0].r, values[0].g, values[0].b, values[0].a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_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);
})); }));
} }
GN_CPP_FUNCTION void openglEndRenderPass(gnCommandBuffer buffer) { GN_CPP_FUNCTION void openglEndRenderPass(gnCommandBuffer buffer) {
openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([]{ openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([]{
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_FRAMEBUFFER_SRGB);
})); }));
} }
GN_CPP_FUNCTION void openglBindGraphicsPipeline(gnCommandBuffer commandBuffer, gnGraphicsPipeline graphicsPipeline) { 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) { GN_CPP_FUNCTION void openglBindUniform(gnCommandBufferHandle buffer, gnUniform uniform, uint32_t set, uint32_t dynamicOffsetCount, uint32_t* dynamicOffsets) {
openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([]{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 5);
}));
} }
GN_CPP_FUNCTION void openglPushConstant(gnCommandBufferHandle buffer, gnPushConstantLayout layout, void* data) { GN_CPP_FUNCTION void openglPushConstant(gnCommandBufferHandle buffer, gnPushConstantLayout layout, void* data) {

View File

@@ -9,6 +9,8 @@ gnReturnCode openglPresent(gnOutputDeviceHandle device, gnPresentInfo info) {
glBindVertexArray(0); glBindVertexArray(0);
if (info.presentationQueues[i]->presentationQueue->format == GL_SRGB8_ALPHA8) glEnable(GL_FRAMEBUFFER_SRGB);
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]));
@@ -18,6 +20,8 @@ gnReturnCode openglPresent(gnOutputDeviceHandle device, gnPresentInfo info) {
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
swapBuffers(info.presentationQueues[i]->info.surface); swapBuffers(info.presentationQueues[i]->info.surface);
glDisable(GL_FRAMEBUFFER_SRGB);
} }
// for (uint32_t i = 0; i < info.presentationQueueCount; i++) { // for (uint32_t i = 0; i < info.presentationQueueCount; i++) {

View File

@@ -9,6 +9,7 @@ gnReturnCode createOpenGLPresentationQueue(gnPresentationQueueHandle presentatio
presentationQueue->images = malloc(sizeof(gnTexture) * presentationInfo.minImageCount); presentationQueue->images = malloc(sizeof(gnTexture) * presentationInfo.minImageCount);
presentationQueue->presentationQueue->textures = GLuintArrayListCreate(); presentationQueue->presentationQueue->textures = GLuintArrayListCreate();
presentationQueue->presentationQueue->avaliableTextures = uint32_tArrayListCreate(); presentationQueue->presentationQueue->avaliableTextures = uint32_tArrayListCreate();
presentationQueue->presentationQueue->format = glGryphnFormatToOpenGLInternalFormat(presentationInfo.format.format);
for (int i = 0; i < presentationInfo.minImageCount; i++) { for (int i = 0; i < presentationInfo.minImageCount; i++) {
presentationQueue->images[i] = malloc(sizeof(struct gnTexture_t)); presentationQueue->images[i] = malloc(sizeof(struct gnTexture_t));
presentationQueue->images[i]->texture = malloc(sizeof(gnPlatformTexture)); presentationQueue->images[i]->texture = malloc(sizeof(gnPlatformTexture));

View File

@@ -7,6 +7,7 @@ GN_ARRAY_LIST_HEADER(GLuint);
typedef struct gnPlatformPresentationQueue_t { typedef struct gnPlatformPresentationQueue_t {
GLuintArrayList textures; GLuintArrayList textures;
uint32_tArrayList avaliableTextures; uint32_tArrayList avaliableTextures;
GLenum format;
} gnPlatformPresentationQueue_t; } gnPlatformPresentationQueue_t;
gnReturnCode createOpenGLPresentationQueue(gnPresentationQueueHandle presentationQueue, gnOutputDeviceHandle device, gnPresentationQueueInfo presentationInfo); gnReturnCode createOpenGLPresentationQueue(gnPresentationQueueHandle presentationQueue, gnOutputDeviceHandle device, gnPresentationQueueInfo presentationInfo);

View File

@@ -26,7 +26,7 @@ gnReturnCode openglCreateRenderPass(gnRenderPassDescriptor renderPass, gnDevice
.storeOperation = info.attachmentInfos[attachmentIndex].storeOperation, .storeOperation = info.attachmentInfos[attachmentIndex].storeOperation,
.attachmentIndex = attachmentIndex, .attachmentIndex = attachmentIndex,
.resolveAttachmentIndex = resolveAttachmentIndex, .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; uint32_t depthAttachmentIndex = info.subpassInfos[i].depthAttachment->index;
renderPass->renderPassDescriptor->subpasses[i].depthAttachment = (gnDepthAttachment){ renderPass->renderPassDescriptor->subpasses[i].depthAttachment = (gnDepthAttachment){
.index = depthAttachmentIndex, .index = depthAttachmentIndex,
.format = glGryphnFormatToOpenGLFormat(info.attachmentInfos[depthAttachmentIndex].format), .format = glGryphnFormatToOpenGLInternalFormat(info.attachmentInfos[depthAttachmentIndex].format),
.loadOperation = info.attachmentInfos[depthAttachmentIndex].loadOperation, .loadOperation = info.attachmentInfos[depthAttachmentIndex].loadOperation,
.storeOperation = info.attachmentInfos[depthAttachmentIndex].storeOperation, .storeOperation = info.attachmentInfos[depthAttachmentIndex].storeOperation,
}; };

View File

@@ -8,7 +8,7 @@ typedef struct glColorAttachment {
gnLoadOperation loadOperation; gnLoadOperation loadOperation;
gnStoreOperation storeOperation; gnStoreOperation storeOperation;
GLint format; GLenum format;
uint32_t attachmentIndex; uint32_t attachmentIndex;
int resolveAttachmentIndex; // -1 = no attachment int resolveAttachmentIndex; // -1 = no attachment
} glColorAttachment; } glColorAttachment;

View File

@@ -109,11 +109,11 @@ gnSurfaceDetails genOpenGLSurfaceDetails(
surfaceDetails.formatCount = 1; surfaceDetails.formatCount = 1;
surfaceDetails.formats = malloc(sizeof(gnSurfaceFormat) * 2); surfaceDetails.formats = malloc(sizeof(gnSurfaceFormat) * 2);
surfaceDetails.formats[0] = (gnSurfaceFormat){ surfaceDetails.formats[0] = (gnSurfaceFormat){
.format = GN_FORMAT_RGBA8, .format = GN_FORMAT_RGBA8_SRGB,
.colorSpace = GN_COLOR_SPACE_SRGB_NONLINEAR .colorSpace = GN_COLOR_SPACE_SRGB_NONLINEAR
}; };
surfaceDetails.formats[1] = (gnSurfaceFormat){ surfaceDetails.formats[1] = (gnSurfaceFormat){
.format = GN_FORMAT_RGBA8_SRGB, .format = GN_FORMAT_RGBA8,
.colorSpace = GN_COLOR_SPACE_SRGB_NONLINEAR .colorSpace = GN_COLOR_SPACE_SRGB_NONLINEAR
}; };

View File

@@ -18,7 +18,9 @@ gnReturnCode openglCreateTexture(gnTexture texture, gnDevice device, const gnTex
); );
return GN_SUCCESS; return GN_SUCCESS;
} }
#include "stdio.h"
void openglTextureData(gnTextureHandle texture, void* pixelData) { void openglTextureData(gnTextureHandle texture, void* pixelData) {
printf("OpenGL id: %u\n", texture->texture->id);
glTextureSubImage2D( glTextureSubImage2D(
texture->texture->id, texture->texture->id,
0, 0,