Skip to content

Commit

Permalink
support both GLES 3.0 and GL 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
tophyr committed Aug 14, 2024
1 parent 470104c commit c6c777e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions renderer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ cmake_path(SET SHADERS_H NORMALIZE ${GENERATED_HEADERS}/shaders.h)
set(SHADERS
shaders/fragment.glsl
shaders/vertex.glsl
shaders/version_300_es.glsl
shaders/version_150_core.glsl
)
add_custom_command(
OUTPUT ${SHADERS_H}
Expand Down
16 changes: 14 additions & 2 deletions renderer/HardwareOpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ renderer_type Renderer_type = RENDERER_OPENGL;
int WindowGL = 0;

struct Renderer {
Renderer() : shader_{shaders::vertex, shaders::fragment, {
Renderer() : shader_{
std::string{kGlslVersion} + std::string{shaders::vertex},
std::string{kGlslVersion} + std::string{shaders::fragment},
{
vertexAttrib(3, GL_FLOAT, GL_FALSE, &PosColorUV2Vertex::pos, "in_pos"),
vertexAttrib(4, GL_FLOAT, GL_FALSE, &PosColorUV2Vertex::color, "in_color"),
vertexAttrib(2, GL_FLOAT, GL_FALSE, &PosColorUV2Vertex::uv0, "in_uv0"),
vertexAttrib(2, GL_FLOAT, GL_FALSE, &PosColorUV2Vertex::uv1, "in_uv1")
}} {
}} {
shader_.Use();

// these are effectively just constants, for now
Expand Down Expand Up @@ -143,6 +146,13 @@ struct Renderer {
glm::mat4x4 projection_;
GLint texture_enable_{};
ShaderProgram<PosColorUV2Vertex> shader_;

static constexpr auto kGlslVersion =
#if defined(ANDROID)
shaders::version_300_es;
#else
shaders::version_150_core;
#endif
};
std::optional<Renderer> gRenderer;

Expand Down Expand Up @@ -424,8 +434,10 @@ int opengl_Setup(oeApplication *app, int *width, int *height) {
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
#if !defined(ANDROID)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
#endif
Uint32 flags = SDL_WINDOW_OPENGL;

if (fullscreen) {
Expand Down
8 changes: 5 additions & 3 deletions renderer/shaders/fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#version 150 core
// version must be prepended to this file

/*
* Descent 3
Expand All @@ -18,6 +18,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

precision highp float;

in vec4 vertex_color;
in vec2 vertex_uv0;
in vec2 vertex_uv1;
Expand Down Expand Up @@ -50,9 +52,9 @@ void main()
* max(texture(u_texture0, vertex_uv0), vec4(float(!bool((u_texture_enable >> 0) & 1))))
* max(texture(u_texture1, vertex_uv1), vec4(float(!bool((u_texture_enable >> 1) & 1))));

float fog_factor = clamp((u_fog_end - length(vertex_modelview_pos)) * branchless_invert_or_zero(u_fog_end - u_fog_start), 0, 1);
float fog_factor = clamp((u_fog_end - length(vertex_modelview_pos)) * branchless_invert_or_zero(u_fog_end - u_fog_start), 0.0, 1.0);
// out_color is unchanged when fog_factor is 1 (ie, fog distance == u_fog_start). thus, to disable,
// fog_factor must also be 1. invert u_fog_enable (so that it is 1 when disabled) and take the max.
fog_factor = max(fog_factor, float(!u_fog_enable));
out_color = out_color * fog_factor + (1 - fog_factor) * u_fog_color;
out_color = out_color * fog_factor + (1.0 - fog_factor) * u_fog_color;
}
1 change: 1 addition & 0 deletions renderer/shaders/version_150_core.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#version 150 core
1 change: 1 addition & 0 deletions renderer/shaders/version_300_es.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#version 300 es
2 changes: 1 addition & 1 deletion renderer/shaders/vertex.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#version 150 core
// version must be prepended to this file

/*
* Descent 3
Expand Down

0 comments on commit c6c777e

Please sign in to comment.