Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try OpenGL 3.3C first, and fall back to 2.1 only if it fails #1747

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/tr1/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased](https://github.com/LostArtefacts/TRX/compare/tr1-4.5.1...develop) - ××××-××-××
- added support for wading, similar to TR2+ (#1537)
- added the ability to pause during cutscenes (#1673)
- changed OpenGL backend to use version 3.3, with fallback to 2.1 if initialization fails (#1738)
- fixed missing pushblock SFX in Natla's Mines (#1714)
- fixed crash reports not working in certain circumstances (#1738)
- fixed missing trapdoor triggers in City of Khamoon (#1744)
Expand Down Expand Up @@ -45,7 +46,6 @@
- added `/exit` command (#1462)
- added reflections to Midas Hand death animation and savegame crystals (#154)
- added an option to use PS1 tinted savegame crystals (#1506)
- changed the target change functionality from the look key to a new, standalone key (default `z` on keyboard, `left analog click` on controller) (#1503)
- fixed `/play`, `/load`, `/demo` and similar commands not working in stats, credits, cinematics and fmvs (#1477)
- fixed console commands being able to interfere with demos, cutscenes and the title screen (#1489, regression from 3.0)
- fixed reopening the compass not resetting its needle (#1472, regression from 4.0)
Expand All @@ -69,6 +69,7 @@
- fixed Bacon Lara re-spawning after saving and loading (#1500, regression from 0.7)
- fixed config JSON not sanitizing some numeric values (#1515)
- fixed potential crashes in custom levels if hybrid creature objects are not present in the level (#1444)
- changed the target change functionality from the look key to a new, standalone key (default `z` on keyboard, `left analog click` on controller) (#1503)
- changed `/heal` console command to also extinguish Lara
- changed `/tp` console command to look for the closest place to teleport to when targeting items (#1484)
- changed `/set` console command output to always use fully-qualified option names
Expand Down
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
30 changes: 13 additions & 17 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 Down Expand Up @@ -99,16 +100,23 @@ void GFX_Context_SwitchToDisplayViewport(void)
GFX_GL_CheckError();
}

void GFX_Context_Attach(void *window_handle)
bool GFX_Context_Attach(void *window_handle, GFX_GL_BACKEND backend)
{
const char *shading_ver;

if (m_Context.window_handle) {
return;
LOG_ERROR("Context already attached");
return false;
}

LOG_INFO("Attaching to window %p", window_handle);
m_Context.context = SDL_GL_CreateContext(window_handle);
if (m_Context.context == NULL) {
LOG_ERROR("Can't create OpenGL context: %s", SDL_GetError());
return false;
}

m_Context.config.backend = backend;
m_Context.config.line_width = 1;
m_Context.config.enable_wireframe = false;
m_Context.render_mode = -1;
Expand All @@ -119,19 +127,6 @@ void GFX_Context_Attach(void *window_handle)

m_Context.window_handle = window_handle;

if (GFX_GL_DEFAULT_BACKEND == 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);
}

m_Context.context = SDL_GL_CreateContext(m_Context.window_handle);

if (m_Context.context == NULL) {
Shell_ExitSystemFmt("Can't create OpenGL context: %s", SDL_GetError());
}

if (SDL_GL_MakeCurrent(m_Context.window_handle, m_Context.context)) {
Shell_ExitSystemFmt(
"Can't activate OpenGL context: %s", SDL_GetError());
Expand All @@ -149,7 +144,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 @@ -161,8 +156,9 @@ 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);
return true;
}

void GFX_Context_Detach(void)
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
3 changes: 2 additions & 1 deletion src/libtrx/include/libtrx/gfx/2d/2d_renderer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "../config.h"
#include "../gl/buffer.h"
#include "../gl/program.h"
#include "../gl/sampler.h"
Expand All @@ -20,7 +21,7 @@ typedef struct {
GFX_GL_PROGRAM program;
} GFX_2D_RENDERER;

void GFX_2D_Renderer_Init(GFX_2D_RENDERER *renderer);
void GFX_2D_Renderer_Init(GFX_2D_RENDERER *renderer, const GFX_CONFIG *config);
void GFX_2D_Renderer_Close(GFX_2D_RENDERER *renderer);

void GFX_2D_Renderer_Upload(
Expand Down
1 change: 1 addition & 0 deletions src/libtrx/include/libtrx/gfx/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef enum {
} GFX_RENDER_MODE;

typedef enum {
GFX_GL_INVALID_BACKEND,
GFX_GL_21,
GFX_GL_33C,
} GFX_GL_BACKEND;
2 changes: 2 additions & 0 deletions src/libtrx/include/libtrx/gfx/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <stdint.h>

typedef struct {
GFX_GL_BACKEND backend;

GFX_TEXTURE_FILTER display_filter;
bool enable_wireframe;
int32_t line_width;
Expand Down
2 changes: 1 addition & 1 deletion src/libtrx/include/libtrx/gfx/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <stdbool.h>
#include <stdint.h>

void GFX_Context_Attach(void *window_handle);
bool GFX_Context_Attach(void *window_handle, GFX_GL_BACKEND backend);
void GFX_Context_Detach(void);

void GFX_Context_SetDisplayFilter(GFX_TEXTURE_FILTER filter);
Expand Down
Loading