Compare commits

...

3 Commits

Author SHA1 Message Date
Shylie
65c6de85ba Rework testing code 2024-05-27 13:07:50 -04:00
Shylie
12f7d22009 Convert from char* to char[] 2024-05-27 11:47:00 -04:00
Shylie
2861b78681 Update to allow GL_ARB_shader_viewport_layer_array as well. 2024-05-27 11:42:17 -04:00
5 changed files with 85 additions and 34 deletions

View File

@ -1,22 +1,23 @@
/*
OpenGL loader generated by glad 0.1.36 on Sun May 26 16:07:25 2024.
OpenGL loader generated by glad 0.1.36 on Mon May 27 15:25:59 2024.
Language/Generator: C/C++
Specification: gl
APIs: gl=4.5
Profile: core
Extensions:
GL_AMD_vertex_shader_layer
GL_AMD_vertex_shader_layer,
GL_ARB_shader_viewport_layer_array
Loader: True
Local files: False
Omit khrplatform: False
Reproducible: False
Commandline:
--profile="core" --api="gl=4.5" --generator="c" --spec="gl" --extensions="GL_AMD_vertex_shader_layer"
--profile="core" --api="gl=4.5" --generator="c" --spec="gl" --extensions="GL_AMD_vertex_shader_layer,GL_ARB_shader_viewport_layer_array"
Online:
https://glad.dav1d.de/#profile=core&language=c&specification=gl&loader=on&api=gl%3D4.5&extensions=GL_AMD_vertex_shader_layer
https://glad.dav1d.de/#profile=core&language=c&specification=gl&loader=on&api=gl%3D4.5&extensions=GL_AMD_vertex_shader_layer&extensions=GL_ARB_shader_viewport_layer_array
*/
#include <stdio.h>
@ -969,6 +970,7 @@ PFNGLVIEWPORTINDEXEDFPROC glad_glViewportIndexedf = NULL;
PFNGLVIEWPORTINDEXEDFVPROC glad_glViewportIndexedfv = NULL;
PFNGLWAITSYNCPROC glad_glWaitSync = NULL;
int GLAD_GL_AMD_vertex_shader_layer = 0;
int GLAD_GL_ARB_shader_viewport_layer_array = 0;
static void load_GL_VERSION_1_0(GLADloadproc load) {
if(!GLAD_GL_VERSION_1_0) return;
glad_glCullFace = (PFNGLCULLFACEPROC)load("glCullFace");
@ -1725,6 +1727,7 @@ static void load_GL_VERSION_4_5(GLADloadproc load) {
static int find_extensionsGL(void) {
if (!get_exts()) return 0;
GLAD_GL_AMD_vertex_shader_layer = has_ext("GL_AMD_vertex_shader_layer");
GLAD_GL_ARB_shader_viewport_layer_array = has_ext("GL_ARB_shader_viewport_layer_array");
free_exts();
return 1;
}

View File

@ -1,22 +1,23 @@
/*
OpenGL loader generated by glad 0.1.36 on Sun May 26 16:07:25 2024.
OpenGL loader generated by glad 0.1.36 on Mon May 27 15:25:59 2024.
Language/Generator: C/C++
Specification: gl
APIs: gl=4.5
Profile: core
Extensions:
GL_AMD_vertex_shader_layer
GL_AMD_vertex_shader_layer,
GL_ARB_shader_viewport_layer_array
Loader: True
Local files: False
Omit khrplatform: False
Reproducible: False
Commandline:
--profile="core" --api="gl=4.5" --generator="c" --spec="gl" --extensions="GL_AMD_vertex_shader_layer"
--profile="core" --api="gl=4.5" --generator="c" --spec="gl" --extensions="GL_AMD_vertex_shader_layer,GL_ARB_shader_viewport_layer_array"
Online:
https://glad.dav1d.de/#profile=core&language=c&specification=gl&loader=on&api=gl%3D4.5&extensions=GL_AMD_vertex_shader_layer
https://glad.dav1d.de/#profile=core&language=c&specification=gl&loader=on&api=gl%3D4.5&extensions=GL_AMD_vertex_shader_layer&extensions=GL_ARB_shader_viewport_layer_array
*/
@ -3652,6 +3653,10 @@ GLAPI PFNGLTEXTUREBARRIERPROC glad_glTextureBarrier;
#define GL_AMD_vertex_shader_layer 1
GLAPI int GLAD_GL_AMD_vertex_shader_layer;
#endif
#ifndef GL_ARB_shader_viewport_layer_array
#define GL_ARB_shader_viewport_layer_array 1
GLAPI int GLAD_GL_ARB_shader_viewport_layer_array;
#endif
#ifdef __cplusplus
}

View File

@ -63,6 +63,8 @@ namespace glerminal
unsigned int m_sprites_texture;
unsigned int m_framebuffer;
unsigned int m_framebuffer_backing_texture;
unsigned int m_screen_framebuffer;
unsigned int m_screen_framebuffer_backing_texture;
unsigned int m_layer_colors_buffer;
unsigned int m_layer_scales_buffer;
unsigned int m_screen_size_uniform_location;
@ -85,8 +87,8 @@ namespace glerminal
#ifdef GLERMINAL_OPENGL_DEBUG_CONTEXT
mutable std::ofstream m_log;
void log(GLenum type, GLuint id, GLenum severity, const char* message) const;
#endif
void log(GLenum type, GLuint id, GLenum severity, const char* message) const;
void init_glfw();
void init_gl();

View File

@ -53,8 +53,9 @@ namespace
"}";
// note: AMD_vertex_shader_layer support required
constexpr char VERTEX_SHADER_AMD_SOURCE[] =
constexpr char VERTEX_SHADER_ARB_SOURCE[] =
"#version 450 core\n"
"#extension GL_ARB_shader_viewport_layer_array : enable\n"
"#extension GL_AMD_vertex_shader_layer : enable\n"
"layout (location = 0) in vec2 position;\n"
"layout (location = 1) in vec2 offset;\n"
@ -81,7 +82,7 @@ namespace
"}";
constexpr const char* VERTEX_SHADER_SOURCE_PTR = VERTEX_SHADER_SOURCE;
constexpr const char* VERTEX_SHADER_AMD_SOURCE_PTR = VERTEX_SHADER_AMD_SOURCE;
constexpr const char* VERTEX_SHADER_ARB_SOURCE_PTR = VERTEX_SHADER_ARB_SOURCE;
constexpr char GEOMETRY_SHADER_SOURCE[] =
"#version 450 core\n"
@ -128,7 +129,7 @@ namespace
"}";
// note: AMD_vertex_shader_layer support required
constexpr char FRAGMENT_SHADER_AMD_SOURCE[] =
constexpr char FRAGMENT_SHADER_ARB_SOURCE[] =
"#version 450 core\n"
"in VS_OUT {\n"
" flat int sprite;\n"
@ -142,7 +143,7 @@ namespace
"}";
constexpr const char* FRAGMENT_SHADER_SOURCE_PTR = FRAGMENT_SHADER_SOURCE;
constexpr const char* FRAGMENT_SHADER_AMD_SOURCE_PTR = FRAGMENT_SHADER_AMD_SOURCE;
constexpr const char* FRAGMENT_SHADER_ARB_SOURCE_PTR = FRAGMENT_SHADER_ARB_SOURCE;
constexpr char SCREEN_VERTEX_SHADER_SOURCE[] =
"#version 450 core\n"
@ -156,7 +157,7 @@ namespace
constexpr const char* SCREEN_VERTEX_SHADER_SOURCE_PTR = SCREEN_VERTEX_SHADER_SOURCE;
constexpr char* SCREEN_FRAGMENT_SHADER_SOURCE =
constexpr char SCREEN_FRAGMENT_SHADER_SOURCE[] =
"#version 450 core\n"
"in vec2 texcoord;\n"
"layout (binding = 1) uniform sampler2DArray " LAYERS_UNIFORM_NAME ";\n"
@ -266,11 +267,13 @@ namespace glerminal
glDrawArraysInstanced(GL_TRIANGLES, 0, 6, GRID_AREA * LAYER_COUNT);
glUseProgram(m_screen_program);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, m_screen_framebuffer);
glBindVertexArray(m_screen_vao);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBlitNamedFramebuffer(m_screen_framebuffer, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glfwSwapBuffers(m_window);
}
@ -363,12 +366,12 @@ namespace glerminal
}
}
#ifdef GLERMINAL_OPENGL_DEBUG_CONTEXT
void glerminal::log(GLenum type, GLuint id, GLenum severity, const char* message) const
{
#ifdef GLERMINAL_OPENGL_DEBUG_CONTEXT
glDebugMessageInsert(GL_DEBUG_SOURCE_THIRD_PARTY, type, id, severity, -1, message);
}
#endif
}
void glerminal::init_gl()
{
@ -494,11 +497,12 @@ namespace glerminal
// -- setup shader program --
// test for features
const bool vertex_shader_layer_supported = glfwExtensionSupported("GL_AMD_vertex_shader_layer");
const bool vertex_shader_layer_supported =
glfwExtensionSupported("GL_ARB_shader_viewport_layer_array") || glfwExtensionSupported("GL_AMD_vertex_shader_layer");
// compile
const unsigned int vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &(vertex_shader_layer_supported ? VERTEX_SHADER_AMD_SOURCE_PTR : VERTEX_SHADER_SOURCE_PTR), nullptr);
glShaderSource(vertex_shader, 1, &(vertex_shader_layer_supported ? VERTEX_SHADER_ARB_SOURCE_PTR : VERTEX_SHADER_SOURCE_PTR), nullptr);
glCompileShader(vertex_shader);
const unsigned int geometry_shader = glCreateShader(GL_GEOMETRY_SHADER);
@ -506,12 +510,12 @@ namespace glerminal
glCompileShader(geometry_shader);
const unsigned int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &(vertex_shader_layer_supported ? FRAGMENT_SHADER_AMD_SOURCE_PTR : FRAGMENT_SHADER_SOURCE_PTR), nullptr);
glShaderSource(fragment_shader, 1, &(vertex_shader_layer_supported ? FRAGMENT_SHADER_ARB_SOURCE_PTR : FRAGMENT_SHADER_SOURCE_PTR), nullptr);
glCompileShader(fragment_shader);
constexpr int INFO_LOG_SIZE = 512;
int success;
char info_log[INFO_LOG_SIZE];
char info_log[INFO_LOG_SIZE + 1] = {};
// verify compile
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
@ -523,7 +527,9 @@ namespace glerminal
glDeleteShader(geometry_shader);
glDeleteShader(fragment_shader);
throw std::runtime_error("Could not compile vertex shader.");
log(GL_DEBUG_TYPE_ERROR, 0, 0, "Could not compile vertex shader.");
log(GL_DEBUG_TYPE_ERROR, 0, 0, info_log);
throw std::runtime_error("");
}
glGetShaderiv(geometry_shader, GL_COMPILE_STATUS, &success);
@ -535,7 +541,9 @@ namespace glerminal
glDeleteShader(geometry_shader);
glDeleteShader(fragment_shader);
throw std::runtime_error("Could not compile geometry shader.");
log(GL_DEBUG_TYPE_ERROR, 1, 0, "Could not compile geometry shader.");
log(GL_DEBUG_TYPE_ERROR, 1, 0, info_log);
throw std::runtime_error("");
}
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
@ -547,7 +555,9 @@ namespace glerminal
glDeleteShader(geometry_shader);
glDeleteShader(fragment_shader);
throw std::runtime_error("Could not compile fragment shader.");
log(GL_DEBUG_TYPE_ERROR, 2, 0, "Could not compile fragment shader.");
log(GL_DEBUG_TYPE_ERROR, 2, 0, info_log);
throw std::runtime_error("");
}
// link
@ -572,7 +582,9 @@ namespace glerminal
glDeleteProgram(m_program);
throw std::runtime_error("Could not link shader program.");
log(GL_DEBUG_TYPE_ERROR, 3, 0, "Could not link shader program.");
log(GL_DEBUG_TYPE_ERROR, 3, 0, info_log);
throw std::runtime_error("");
}
// setup uniforms
@ -599,7 +611,9 @@ namespace glerminal
glDeleteShader(screen_vertex_shader);
glDeleteShader(screen_fragment_shader);
throw std::runtime_error("Could not compile screen vertex shader.");
log(GL_DEBUG_TYPE_ERROR, 4, 0, "Could not compile screen vertex shader.");
log(GL_DEBUG_TYPE_ERROR, 4, 0, info_log);
throw std::runtime_error("");
}
glGetShaderiv(screen_fragment_shader, GL_COMPILE_STATUS, &success);
@ -610,7 +624,9 @@ namespace glerminal
glDeleteShader(screen_vertex_shader);
glDeleteShader(screen_fragment_shader);
throw std::runtime_error("Could not compile screen fragment shader.");
log(GL_DEBUG_TYPE_ERROR, 5, 0, "Could not compile screen fragment shader.");
log(GL_DEBUG_TYPE_ERROR, 5, 0, info_log);
throw std::runtime_error("");
}
// link
@ -627,10 +643,15 @@ namespace glerminal
{
glDeleteProgram(m_screen_program);
throw std::runtime_error("Could not link screen shader program.");
log(GL_DEBUG_TYPE_ERROR, 6, 0, "Could not link screen shader program.");
log(GL_DEBUG_TYPE_ERROR, 6, 0, info_log);
throw std::runtime_error("");
}
// setup uniforms later
// setup uniforms for screen shader
glUseProgram(m_screen_program);
glUniform1i(glGetUniformLocation(m_screen_program, LAYER_COUNT_UNIFORM_NAME), LAYER_COUNT);
// -- setup textures --
glGenTextures(1, &m_sprites_texture);
@ -670,10 +691,24 @@ namespace glerminal
glBindTextureUnit(1, m_framebuffer_backing_texture);
// setup uniforms for screen shader
glUseProgram(m_screen_program);
// -- setup screen framebuffer --
glGenFramebuffers(1, &m_screen_framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_screen_framebuffer);
glUniform1i(glGetUniformLocation(m_screen_program, LAYER_COUNT_UNIFORM_NAME), LAYER_COUNT);
glGenTextures(1, &m_screen_framebuffer_backing_texture);
glBindTexture(GL_TEXTURE_2D, m_screen_framebuffer_backing_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, SCREEN_WIDTH, SCREEN_HEIGHT);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_screen_framebuffer_backing_texture, 0);
}
void glerminal::deinit_glfw()
@ -685,6 +720,8 @@ namespace glerminal
void glerminal::deinit_gl()
{
glDeleteFramebuffers(1, &m_screen_framebuffer);
glDeleteTextures(1, &m_screen_framebuffer_backing_texture);
glDeleteFramebuffers(1, &m_framebuffer);
glDeleteTextures(1, &m_framebuffer_backing_texture);
glDeleteTextures(1, &m_sprites_texture);

View File

@ -13,8 +13,12 @@ namespace
void glerminal_test_save_image()
{
glReadBuffer(GL_LEFT);
glReadPixels(0, 0, GRID_WIDTH * CELL_SCALE * 8, GRID_HEIGHT * CELL_SCALE * 8, GL_RGB, GL_UNSIGNED_BYTE, pixels);
// -- DIRTY HACK --
//
// GL_TEXTURE_2D is not rebound after setting up screen framebuffer
// This code will break if this behavior changes
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
stbi_flip_vertically_on_write(true);
stbi_write_png("image.png", GRID_WIDTH * CELL_SCALE * 8, GRID_HEIGHT * CELL_SCALE * 8, 3, pixels, 1280 * 3);