render fullscreen texcoord quad

This commit is contained in:
Gregory Wells
2025-08-18 23:22:09 -04:00
parent 453f7b70db
commit 740cf1e628
7 changed files with 114 additions and 28 deletions

View File

@@ -7,16 +7,16 @@
GN_CPP_FUNCTION void openglBeginRenderPass(gnCommandBuffer buffer, gnRenderPassInfo passInfo) {
const char* func_name = "begin render pass";
openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([passInfo]{
glBindFramebuffer(GL_FRAMEBUFFER, passInfo.framebuffer->framebuffer->framebuffers[0]);
glClearColor(passInfo.clearValues[0].r, passInfo.clearValues[0].g, passInfo.clearValues[0].b, passInfo.clearValues[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);
// glBindFramebuffer(GL_FRAMEBUFFER, passInfo.framebuffer->framebuffer->framebuffers[0]);
// glClearColor(passInfo.clearValues[0].r, passInfo.clearValues[0].g, passInfo.clearValues[0].b, passInfo.clearValues[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);
}));
}
GN_CPP_FUNCTION void openglEndRenderPass(gnCommandBuffer buffer) {
openglCommandRunnerBindFunction(buffer->commandBuffer->commmandRunner, std::function<void()>([]{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// glBindFramebuffer(GL_FRAMEBUFFER, 0);
}));
}
GN_CPP_FUNCTION void openglBindGraphicsPipeline(gnCommandBuffer buffer, gnGraphicsPipeline graphicsPipeline);

View File

@@ -1,22 +1,23 @@
const char * vertex_shader_source =
"#version 450 core"
""
"layout(location = 0) in vec2 inPosition;"
"layout(location = 1) in vec2 inTexcoord;"
""
"out vec2 texcoord;"
""
"void main() {"
" gl_Position = vec4(inPosition, 0.0, 1.0);"
" texcoord = inTexcoord;"
"}" ;
"#version 450 core\n"
"\n"
"layout(location = 0) in vec2 inPosition;\n"
"layout(location = 1) in vec2 inTexcoord;\n"
"\n"
"out vec2 texcoord;\n"
"\n"
"void main() {\n"
" gl_Position = vec4(inPosition, 0.0, 1.0);\n"
" texcoord = inTexcoord;\n"
"}\n" ;
const char * fragment_shader_source =
"#version 450 core"
""
"out vec4 FragColor;"
"layout(binding = 0) uniform sampler2D tex;"
"in vec2 texcoord;"
""
"void main() {"
" FragColor = texture(tex, texcoord);"
"}" ;
"#version 450 core\n"
"\n"
"out vec4 FragColor;\n"
"layout(binding = 0) uniform sampler2D tex;\n"
"in vec2 texcoord;\n"
"\n"
"void main() {\n"
" //FragColor = texture(tex, texcoord);\n"
" FragColor = vec4(texcoord, 0.0, 1.0);\n"
"}\n" ;

View File

@@ -2,23 +2,30 @@
#include "opengl_output_device.h"
#include "glsl_shader.glsl"
#include "stdlib.h"
#include "core/src/instance/gryphn_instance.h"
gnReturnCode createOpenGLOutputDevice(gnInstanceHandle instance, gnOutputDeviceHandle device, gnOutputDeviceInfo deviceInfo) {
int success;
char infoLog[512];
device->outputDevice = malloc(sizeof(gnPlatformOutputDevice));
float vertices[] = {
-1.0f, 1.0f, 0.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 1.0f, 1.0f,
};
glCreateBuffers(1, &device->outputDevice->buffer);
glBindBuffer(GL_ARRAY_BUFFER, device->outputDevice->buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, (void*)0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, (void*)(sizeof(float) * 2));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void*)(sizeof(float) * 2));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
@@ -27,15 +34,37 @@ gnReturnCode createOpenGLOutputDevice(gnInstanceHandle instance, gnOutputDeviceH
glShaderSource(vertexShader, 1, &vertex_shader_source, NULL);
glCompileShader(vertexShader);
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
.message = gnCreateString(infoLog)
});
}
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragment_shader_source, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
.message = gnCreateString(infoLog)
});
}
device->outputDevice->shaderProgram = glCreateProgram();
glAttachShader(device->outputDevice->shaderProgram, vertexShader);
glAttachShader(device->outputDevice->shaderProgram, fragmentShader);
glLinkProgram(device->outputDevice->shaderProgram);
glGetProgramiv(device->outputDevice->shaderProgram, GL_LINK_STATUS, &success);
if(!success) {
glGetProgramInfoLog(device->outputDevice->shaderProgram, 512, NULL, infoLog);
gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
.message = gnCreateString(infoLog)
});
}
return GN_SUCCESS;
}

View File

@@ -0,0 +1,47 @@
#include "opengl_present.h"
#include "presentation_queue/opengl_presentation_queue.h"
#include "device/opengl_output_device.h"
#include "surface/opengl_surface.h"
gnReturnCode openglPresent(gnOutputDeviceHandle device, gnPresentInfo info) {
for (uint32_t i =0 ; i < info.presentationQueueCount; i++) {
uint32_tArrayListAdd(info.presentationQueues[i]->presentationQueue->avaliableTextures, info.imageIndices[i]);
glUseProgram(device->outputDevice->shaderProgram);
glBindBuffer(GL_ARRAY_BUFFER, device->outputDevice->buffer);
glDrawArrays(GL_TRIANGLES, 0, 6);
glUseProgram(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
swapBuffers(info.presentationQueues[i]->info.surface);
// id<MTLBlitCommandEncoder> blit = [commandBuffer blitCommandEncoder];
// [blit copyFromTexture:info.presentationQueues[i]->images[info.imageIndices[i]]->texture->texture
// sourceSlice:0
// sourceLevel:0
// sourceOrigin:(MTLOrigin){0, 0, 0}
// sourceSize:(MTLSize){info.presentationQueues[i]->info.imageSize.x, info.presentationQueues[i]->info.imageSize.y, 1}
// toTexture:drawable.texture
// destinationSlice:0
// destinationLevel:0
// destinationOrigin:(MTLOrigin){0, 0, 0}];
// [blit endEncoding];
// [drawable texture];
// [commandBuffer presentDrawable:drawable];
// [commandBuffer commit];
// device->outputDevice->executingCommandBuffer = commandBuffer;
}
// [device->outputDevice->executingCommandBuffer waitUntilCompleted];
// for (uint32_t i = 0; i < info.presentationQueueCount; i++) {
// if (info.presentationQueues[i]->info.imageSize.x != info.presentationQueues[i]->info.surface->windowSurface->layer.drawableSize.width ||
// info.presentationQueues[i]->info.imageSize.y != info.presentationQueues[i]->info.surface->windowSurface->layer.drawableSize.height) {
// return GN_SUBOPTIMAL_PRESENTATION_QUEUE;
// }
// }
return GN_SUCCESS;
}

View File

@@ -0,0 +1,4 @@
#pragma once
#include "core/src/present/gryphn_present.h"
gnReturnCode openglPresent(gnOutputDeviceHandle device, gnPresentInfo info);

View File

@@ -89,6 +89,10 @@ gnUInt2 getWindowSize(gnPlatformWindowSurface* surface) {
return (gnUInt2){ attr.width, attr.height };
}
void swapBuffers(gnWindowSurface surface) {
glXSwapBuffers(surface->windowSurface->display, surface->windowSurface->window);
}
#endif
#ifdef GN_WINFDOW_WAYLAND

View File

@@ -15,6 +15,7 @@ gnReturnCode createGLXContext(gnWindowSurfaceHandle windowSurface, gnInstanceHan
#endif
gnUInt2 getWindowSize(gnPlatformWindowSurface* surface);
void swapBuffers(gnWindowSurface surface);
gnSurfaceDetails genOpenGLSurfaceDetails(gnWindowSurfaceHandle windowSurface, gnPhysicalDevice device);
void destroyOpenGLSurface(gnWindowSurface surface);