Skip to content

Commit

Permalink
gfx: try 3.3c gl backend if 2.1 fails
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Oct 25, 2024
1 parent 5fb9883 commit 3881bd8
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 55 deletions.
15 changes: 10 additions & 5 deletions src/libtrx/gfx/2d/2d_renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

#include <assert.h>

void GFX_2D_Renderer_Init(GFX_2D_RENDERER *renderer)
void GFX_2D_Renderer_Init(
GFX_2D_RENDERER *renderer, const GFX_CONFIG *const config)
{
LOG_INFO("");
assert(renderer);
assert(renderer != NULL);

GFX_GL_Buffer_Init(&renderer->surface_buffer, GL_ARRAY_BUFFER);
GFX_GL_Buffer_Bind(&renderer->surface_buffer);
Expand Down Expand Up @@ -38,9 +39,11 @@ void GFX_2D_Renderer_Init(GFX_2D_RENDERER *renderer)

GFX_GL_Program_Init(&renderer->program);
GFX_GL_Program_AttachShader(
&renderer->program, GL_VERTEX_SHADER, "shaders/2d.glsl");
&renderer->program, GL_VERTEX_SHADER, "shaders/2d.glsl",
config->backend);
GFX_GL_Program_AttachShader(
&renderer->program, GL_FRAGMENT_SHADER, "shaders/2d.glsl");
&renderer->program, GL_FRAGMENT_SHADER, "shaders/2d.glsl",
config->backend);
GFX_GL_Program_Link(&renderer->program);
GFX_GL_Program_FragmentData(&renderer->program, "fragColor");

Expand All @@ -50,7 +53,7 @@ void GFX_2D_Renderer_Init(GFX_2D_RENDERER *renderer)
void GFX_2D_Renderer_Close(GFX_2D_RENDERER *renderer)
{
LOG_INFO("");
assert(renderer);
assert(renderer != NULL);

GFX_GL_VertexArray_Close(&renderer->surface_format);
GFX_GL_Buffer_Close(&renderer->surface_buffer);
Expand All @@ -62,6 +65,7 @@ void GFX_2D_Renderer_Close(GFX_2D_RENDERER *renderer)
void GFX_2D_Renderer_Upload(
GFX_2D_RENDERER *renderer, GFX_2D_SURFACE_DESC *desc, const uint8_t *data)
{
assert(renderer != NULL);
const uint32_t width = desc->width;
const uint32_t height = desc->height;

Expand All @@ -87,6 +91,7 @@ void GFX_2D_Renderer_Upload(

void GFX_2D_Renderer_Render(GFX_2D_RENDERER *renderer)
{
assert(renderer != NULL);
GFX_GL_Program_Bind(&renderer->program);
GFX_GL_Buffer_Bind(&renderer->surface_buffer);
GFX_GL_VertexArray_Bind(&renderer->surface_format);
Expand Down
48 changes: 25 additions & 23 deletions src/libtrx/gfx/3d/3d_renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static void M_SelectTextureImpl(GFX_3D_RENDERER *renderer, int texture_num);

static void M_SelectTextureImpl(GFX_3D_RENDERER *renderer, int texture_num)
{
assert(renderer);
assert(renderer != NULL);

GFX_GL_TEXTURE *texture = NULL;
if (texture_num == GFX_ENV_MAP_TEXTURE) {
Expand All @@ -35,7 +35,7 @@ void GFX_3D_Renderer_Init(
GFX_3D_RENDERER *renderer, const GFX_CONFIG *const config)
{
LOG_INFO("");
assert(renderer);
assert(renderer != NULL);

renderer->config = config;

Expand All @@ -55,9 +55,11 @@ void GFX_3D_Renderer_Init(

GFX_GL_Program_Init(&renderer->program);
GFX_GL_Program_AttachShader(
&renderer->program, GL_VERTEX_SHADER, "shaders/3d.glsl");
&renderer->program, GL_VERTEX_SHADER, "shaders/3d.glsl",
config->backend);
GFX_GL_Program_AttachShader(
&renderer->program, GL_FRAGMENT_SHADER, "shaders/3d.glsl");
&renderer->program, GL_FRAGMENT_SHADER, "shaders/3d.glsl",
config->backend);
GFX_GL_Program_Link(&renderer->program);

renderer->loc_mat_projection =
Expand Down Expand Up @@ -93,7 +95,7 @@ void GFX_3D_Renderer_Init(
void GFX_3D_Renderer_Close(GFX_3D_RENDERER *renderer)
{
LOG_INFO("");
assert(renderer);
assert(renderer != NULL);

GFX_3D_VertexStream_Close(&renderer->vertex_stream);
GFX_GL_Program_Close(&renderer->program);
Expand All @@ -102,7 +104,7 @@ void GFX_3D_Renderer_Close(GFX_3D_RENDERER *renderer)

void GFX_3D_Renderer_RenderBegin(GFX_3D_RENDERER *renderer)
{
assert(renderer);
assert(renderer != NULL);
glEnable(GL_BLEND);

glLineWidth(renderer->config->line_width);
Expand Down Expand Up @@ -145,7 +147,7 @@ void GFX_3D_Renderer_RenderBegin(GFX_3D_RENDERER *renderer)

void GFX_3D_Renderer_RenderEnd(GFX_3D_RENDERER *renderer)
{
assert(renderer);
assert(renderer != NULL);
GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream);

GFX_GL_CheckError();
Expand Down Expand Up @@ -213,8 +215,8 @@ void GFX_3D_Renderer_FillEnvironmentMap(GFX_3D_RENDERER *const renderer)
int GFX_3D_Renderer_RegisterTexturePage(
GFX_3D_RENDERER *renderer, const void *data, int width, int height)
{
assert(renderer);
assert(data);
assert(renderer != NULL);
assert(data != NULL);
GFX_GL_TEXTURE *texture = GFX_GL_Texture_Create(GL_TEXTURE_2D);
GFX_GL_Texture_Load(texture, data, width, height, GL_RGBA, GL_RGBA);

Expand All @@ -236,7 +238,7 @@ int GFX_3D_Renderer_RegisterTexturePage(
bool GFX_3D_Renderer_UnregisterTexturePage(
GFX_3D_RENDERER *renderer, int texture_num)
{
assert(renderer);
assert(renderer != NULL);
assert(texture_num >= 0);
assert(texture_num < GFX_MAX_TEXTURES);

Expand All @@ -260,54 +262,54 @@ bool GFX_3D_Renderer_UnregisterTexturePage(
void GFX_3D_Renderer_RenderPrimStrip(
GFX_3D_RENDERER *renderer, GFX_3D_VERTEX *vertices, int count)
{
assert(renderer);
assert(vertices);
assert(renderer != NULL);
assert(vertices != NULL);
GFX_3D_VertexStream_PushPrimStrip(
&renderer->vertex_stream, vertices, count);
}

void GFX_3D_Renderer_RenderPrimFan(
GFX_3D_RENDERER *renderer, GFX_3D_VERTEX *vertices, int count)
{
assert(renderer);
assert(vertices);
assert(renderer != NULL);
assert(vertices != NULL);
GFX_3D_VertexStream_PushPrimFan(&renderer->vertex_stream, vertices, count);
}

void GFX_3D_Renderer_RenderPrimList(
GFX_3D_RENDERER *renderer, GFX_3D_VERTEX *vertices, int count)
{
assert(renderer);
assert(vertices);
assert(renderer != NULL);
assert(vertices != NULL);
GFX_3D_VertexStream_PushPrimList(&renderer->vertex_stream, vertices, count);
}

void GFX_3D_Renderer_SelectTexture(GFX_3D_RENDERER *renderer, int texture_num)
{
assert(renderer);
assert(renderer != NULL);
GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream);
renderer->selected_texture_num = texture_num;
M_SelectTextureImpl(renderer, texture_num);
}

void GFX_3D_Renderer_RestoreTexture(GFX_3D_RENDERER *renderer)
{
assert(renderer);
assert(renderer != NULL);
M_SelectTextureImpl(renderer, renderer->selected_texture_num);
}

void GFX_3D_Renderer_SetPrimType(
GFX_3D_RENDERER *renderer, GFX_3D_PRIM_TYPE value)
{
assert(renderer);
assert(renderer != NULL);
GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream);
GFX_3D_VertexStream_SetPrimType(&renderer->vertex_stream, value);
}

void GFX_3D_Renderer_SetTextureFilter(
GFX_3D_RENDERER *renderer, GFX_TEXTURE_FILTER filter)
{
assert(renderer);
assert(renderer != NULL);
GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream);
GFX_GL_Sampler_Parameteri(
&renderer->sampler, GL_TEXTURE_MAG_FILTER,
Expand All @@ -323,7 +325,7 @@ void GFX_3D_Renderer_SetTextureFilter(
void GFX_3D_Renderer_SetDepthWritesEnabled(
GFX_3D_RENDERER *const renderer, const bool is_enabled)
{
assert(renderer);
assert(renderer != NULL);
GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream);
if (is_enabled) {
glDepthMask(GL_TRUE);
Expand All @@ -335,7 +337,7 @@ void GFX_3D_Renderer_SetDepthWritesEnabled(
void GFX_3D_Renderer_SetDepthTestEnabled(
GFX_3D_RENDERER *renderer, bool is_enabled)
{
assert(renderer);
assert(renderer != NULL);
GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream);
if (is_enabled) {
glEnable(GL_DEPTH_TEST);
Expand Down Expand Up @@ -366,7 +368,7 @@ void GFX_3D_Renderer_SetBlendingMode(
void GFX_3D_Renderer_SetTexturingEnabled(
GFX_3D_RENDERER *renderer, bool is_enabled)
{
assert(renderer);
assert(renderer != NULL);
GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream);
GFX_GL_Program_Uniform1i(
&renderer->program, renderer->loc_texturing_enabled, is_enabled);
Expand Down
79 changes: 61 additions & 18 deletions src/libtrx/gfx/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "memory.h"

#include <SDL2/SDL_video.h>
#include <assert.h>
#include <string.h>

typedef struct {
Expand All @@ -33,6 +34,9 @@ static GFX_CONTEXT m_Context = { 0 };

static bool M_IsExtensionSupported(const char *name);
static void M_CheckExtensionSupport(const char *name);
static void M_ConfigureBackend(GFX_GL_BACKEND backend);
static SDL_GLContext M_CreateContext(
SDL_Window *window, GFX_GL_BACKEND *out_backend);

static bool M_IsExtensionSupported(const char *name)
{
Expand All @@ -58,6 +62,57 @@ static void M_CheckExtensionSupport(const char *name)
"%s supported: %s", name, M_IsExtensionSupported(name) ? "yes" : "no");
}

static void M_ConfigureBackend(const GFX_GL_BACKEND backend)
{
switch (backend) {
case GFX_GL_21:
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
break;

case GFX_GL_33C:
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(
SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
break;

case GFX_GL_INVALID_BACKEND:
assert(0);
break;
}

m_Context.config.backend = backend;
}

static SDL_GLContext M_CreateContext(
SDL_Window *const window, GFX_GL_BACKEND *const out_backend)
{
const GFX_GL_BACKEND backends_to_try[] = {
// clang-format off
GFX_GL_DEFAULT_BACKEND,
GFX_GL_33C,
GFX_GL_21,
GFX_GL_INVALID_BACKEND, // guard
// clang-format on
};

for (int32_t i = 0; backends_to_try[i] != GFX_GL_INVALID_BACKEND; i++) {
const GFX_GL_BACKEND backend = backends_to_try[i];

M_ConfigureBackend(backend);

SDL_GLContext context = SDL_GL_CreateContext(window);
if (context != NULL) {
*out_backend = backend;
return context;
}
}

return NULL;
}

void GFX_Context_SwitchToWindowViewport(void)
{
glViewport(0, 0, m_Context.window_width, m_Context.window_height);
Expand Down Expand Up @@ -99,21 +154,9 @@ void GFX_Context_SwitchToDisplayViewport(void)
GFX_GL_CheckError();
}

void GFX_Context_SetupEnvironment(void)
GFX_GL_BACKEND GFX_Context_GetBackend(void)
{
switch (GFX_GL_DEFAULT_BACKEND) {
case GFX_GL_21:
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
break;

case GFX_GL_33C:
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(
SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
break;
}
return m_Context.config.backend;
}

void GFX_Context_Attach(void *window_handle)
Expand All @@ -136,8 +179,8 @@ void GFX_Context_Attach(void *window_handle)

m_Context.window_handle = window_handle;

m_Context.context = SDL_GL_CreateContext(m_Context.window_handle);

m_Context.context =
M_CreateContext(window_handle, &m_Context.config.backend);
if (m_Context.context == NULL) {
Shell_ExitSystemFmt("Can't create OpenGL context: %s", SDL_GetError());
}
Expand All @@ -159,7 +202,7 @@ void GFX_Context_Attach(void *window_handle)
}

// Check the availability of non-Core Profile extensions for OpenGL 2.1
if (GFX_GL_DEFAULT_BACKEND == GFX_GL_21) {
if (m_Context.config.backend == GFX_GL_21) {
M_CheckExtensionSupport("GL_ARB_explicit_attrib_location");
M_CheckExtensionSupport("GL_EXT_gpu_shader4");
}
Expand All @@ -171,7 +214,7 @@ void GFX_Context_Attach(void *window_handle)
// VSync defaults to on unless user disabled it in runtime json
SDL_GL_SetSwapInterval(1);

GFX_2D_Renderer_Init(&m_Context.renderer_2d);
GFX_2D_Renderer_Init(&m_Context.renderer_2d, &m_Context.config);
GFX_3D_Renderer_Init(&m_Context.renderer_3d, &m_Context.config);
}

Expand Down
5 changes: 3 additions & 2 deletions src/libtrx/gfx/gl/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ char *GFX_GL_Program_PreprocessShader(
}

void GFX_GL_Program_AttachShader(
GFX_GL_PROGRAM *program, GLenum type, const char *path)
GFX_GL_PROGRAM *program, GLenum type, const char *path,
const GFX_GL_BACKEND backend)
{
assert(program != NULL);
assert(path != NULL);
Expand All @@ -104,7 +105,7 @@ void GFX_GL_Program_AttachShader(
}

char *processed_content =
GFX_GL_Program_PreprocessShader(content, type, GFX_GL_DEFAULT_BACKEND);
GFX_GL_Program_PreprocessShader(content, type, backend);
Memory_FreePointer(&content);
if (!processed_content) {
Shell_ExitSystemFmt("Failed to pre-process shader source: %s", path);
Expand Down
5 changes: 3 additions & 2 deletions src/libtrx/gfx/renderers/fbo_renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ static void M_Init(GFX_RENDERER *const renderer, const GFX_CONFIG *const config)

GFX_GL_Program_Init(&priv->program);
GFX_GL_Program_AttachShader(
&priv->program, GL_VERTEX_SHADER, "shaders/fbo.glsl");
&priv->program, GL_VERTEX_SHADER, "shaders/fbo.glsl", config->backend);
GFX_GL_Program_AttachShader(
&priv->program, GL_FRAGMENT_SHADER, "shaders/fbo.glsl");
&priv->program, GL_FRAGMENT_SHADER, "shaders/fbo.glsl",
config->backend);
GFX_GL_Program_Link(&priv->program);
GFX_GL_Program_FragmentData(&priv->program, "fragColor");

Expand Down
Loading

0 comments on commit 3881bd8

Please sign in to comment.