Skip to content

Commit

Permalink
Add shaders for OpenGL ES (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
clementgallet committed Oct 26, 2021
1 parent a52c479 commit 9fd9946
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
### Added

* Implement SteamInternal_FindOrCreateUserInterface()
* Add shaders for OpenGL ES (#441)

### Changed
### Fixed
Expand Down
15 changes: 15 additions & 0 deletions src/library/eglwrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ DEFINE_ORIG_POINTER(eglGetProcAddress)
DEFINE_ORIG_POINTER(eglMakeCurrent)
DEFINE_ORIG_POINTER(eglSwapBuffers)
DEFINE_ORIG_POINTER(eglSwapInterval)
DEFINE_ORIG_POINTER(eglBindAPI)
DEFINE_ORIG_POINTER(eglCreateContext)

/* If the game uses the eglGetProcAddress functions to access to a function
Expand All @@ -61,6 +62,7 @@ static void* store_orig_and_return_my_symbol(const char* symbol, void* real_poin
STORE_RETURN_SYMBOL(eglMakeCurrent)
STORE_RETURN_SYMBOL(eglSwapBuffers)
STORE_RETURN_SYMBOL(eglSwapInterval)
STORE_RETURN_SYMBOL(eglBindAPI)
STORE_RETURN_SYMBOL(eglCreateContext)

return real_pointer;
Expand Down Expand Up @@ -101,6 +103,18 @@ EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGL
return ret;
}

static EGLenum bindAPI = EGL_OPENGL_ES_API;

EGLBoolean eglBindAPI(EGLenum api)
{
debuglogstdio(LCF_OGL, "%s call with API %d", __func__, api);
LINK_NAMESPACE(eglBindAPI, "EGL");

bindAPI = api;

return orig::eglBindAPI(api);
}

EGLBoolean eglSwapBuffers( EGLDisplay dpy, EGLSurface surface )
{
LINK_NAMESPACE(eglSwapBuffers, "EGL");
Expand All @@ -114,6 +128,7 @@ EGLBoolean eglSwapBuffers( EGLDisplay dpy, EGLSurface surface )
#ifdef LIBTAS_ENABLE_HUD
// static RenderHUD_GL renderHUD;
static RenderHUD_GL renderHUD;
renderHUD.setGLES(bindAPI == EGL_OPENGL_ES_API);
frameBoundary([&] () {orig::eglSwapBuffers(dpy, surface);}, renderHUD);
#else
frameBoundary([&] () {orig::eglSwapBuffers(dpy, surface);});
Expand Down
2 changes: 2 additions & 0 deletions src/library/eglwrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ OVERRIDE EGLBoolean eglSwapBuffers( EGLDisplay dpy, EGLSurface surface );
/* Set the VSync value */
OVERRIDE EGLBoolean eglSwapInterval (EGLDisplay dpy, EGLint interval);

OVERRIDE EGLBoolean eglBindAPI(EGLenum api);

OVERRIDE EGLContext eglCreateContext (EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list);

}
Expand Down
46 changes: 42 additions & 4 deletions src/library/renderhud/RenderHUD_GL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ RenderHUD_GL::~RenderHUD_GL() {
fini();
}

void RenderHUD_GL::init()
void RenderHUD_GL::init(bool stateGLES)
{
if (texture == 0) {
LINK_NAMESPACE(glGenTextures, "GL");
Expand Down Expand Up @@ -189,7 +189,26 @@ void RenderHUD_GL::init()
TexCoord = aTexCoord;
}
)";
orig::glShaderSource(vertexShaderID, 1, &vertexShaderSource , NULL);

const char *vertexShaderSourceES = R"(
#version 100
attribute vec3 aPos;
attribute vec2 aTexCoord;
varying vec2 TexCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
TexCoord = aTexCoord;
}
)";

if (stateGLES) {
orig::glShaderSource(vertexShaderID, 1, &vertexShaderSourceES , NULL);
}
else {
orig::glShaderSource(vertexShaderID, 1, &vertexShaderSource , NULL);
}
orig::glCompileShader(vertexShaderID);


Expand All @@ -212,7 +231,26 @@ void RenderHUD_GL::init()
gl_FragColor = texture2D(ourTexture, TexCoord);
}
)";
orig::glShaderSource(fragmentShaderID, 1, &fragmentShaderSource , NULL);

const char *fragmentShaderSourceES = R"(
#version 100
precision mediump float;
uniform sampler2D ourTexture;
varying vec2 TexCoord;
void main()
{
gl_FragColor = texture2D(ourTexture, TexCoord);
}
)";

if (stateGLES) {
orig::glShaderSource(fragmentShaderID, 1, &fragmentShaderSourceES , NULL);
}
else {
orig::glShaderSource(fragmentShaderID, 1, &fragmentShaderSource , NULL);
}

orig::glCompileShader(fragmentShaderID);

/* Check the fragment shader */
Expand Down Expand Up @@ -273,7 +311,7 @@ void RenderHUD_GL::fini()

void RenderHUD_GL::renderSurface(std::unique_ptr<SurfaceARGB> surf, int x, int y)
{
RenderHUD_GL::init();
RenderHUD_GL::init(isGLES);

LINK_NAMESPACE(glEnable, "GL");
LINK_NAMESPACE(glDisable, "GL");
Expand Down
7 changes: 6 additions & 1 deletion src/library/renderhud/RenderHUD_GL.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ class RenderHUD_GL : public RenderHUD_Base_MacOS
~RenderHUD_GL();

/* Initialize texture and fbo */
static void init();
static void init(bool stateGLES);

/* Deallocate texture and fbo */
static void fini();

/* Switch renderer to OpenGL ES */
void setGLES(bool stateGLES) {isGLES = stateGLES;}

void renderSurface(std::unique_ptr<SurfaceARGB> surf, int x, int y);
private:
static GLuint texture;
Expand All @@ -63,6 +66,8 @@ class RenderHUD_GL : public RenderHUD_Base_MacOS

static float vertices[20];
static GLuint indices[6];

bool isGLES = false;
};
}

Expand Down

0 comments on commit 9fd9946

Please sign in to comment.