The Android/Quest CMakeLists.txt patch previously reimplemented desktop
build / build (push) Successful in 1m8s
build / build (push) Successful in 1m8s
immediate-mode GL (glBegin/glVertex3f/glEnd and friends) from scratch on top of GLES, since GLES has none of it. That code compiled but was never actually exercised - no on-screen verification was possible without real Quest hardware - making it the biggest unverified risk in the port. Replace it with GL4ES (MIT-licensed, prebuilt into the build image), a mature desktop-GL-on-GLES translation library used by other Quest/Android game ports for exactly this problem. This cuts 04-android-opengl-es-render.patch by half, deleting the hand-written vertex-accumulation emulation entirely; alpha test, point sprites, and point size stay on their existing GLES-native fixes rather than trusting GL4ES's shakier custom-shader interop for those. Bump build-image/VERSION to 3 - a fresh image with GL4ES cross-compiled for arm64-v8a was built and make apk verified successfully against it.
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
--- a/CMakeLists.txt
|
||||
+++ b/CMakeLists.txt
|
||||
@@ -46,9 +46,15 @@
|
||||
@@ -46,9 +46,17 @@
|
||||
|
||||
add_compile_options(-fsigned-char -fno-strict-aliasing)
|
||||
|
||||
-# Find OpenGL
|
||||
+# Find OpenGL. Android has no desktop GL/GLX for CMake's FindOpenGL module
|
||||
+# to find - just link the NDK's own GLESv2/EGL libraries directly.
|
||||
+# to find - link the prebuilt GL4ES instead (translates this file's
|
||||
+# desktop-style GL calls into GLES/EGL; it links GLESv2/EGL itself, so
|
||||
+# the engine doesn't need to link them directly).
|
||||
if(ENABLE_OPENGL)
|
||||
- find_package(OpenGL REQUIRED)
|
||||
+ if(ANDROID)
|
||||
+ set(OPENGL_INCLUDE_DIRS "")
|
||||
+ set(OPENGL_LIBRARIES GLESv2 EGL)
|
||||
+ set(OPENGL_INCLUDE_DIRS /opt/prebuilt/android/gl4es/include)
|
||||
+ set(OPENGL_LIBRARIES /opt/prebuilt/android/gl4es/lib/libGL.so)
|
||||
+ else()
|
||||
+ find_package(OpenGL REQUIRED)
|
||||
+ endif()
|
||||
|
||||
@@ -1,153 +1,55 @@
|
||||
--- a/src/MacSrc/OpenGL.cc
|
||||
+++ b/src/MacSrc/OpenGL.cc
|
||||
@@ -7,6 +7,11 @@
|
||||
#define GLEW_STATIC 1
|
||||
#include <SDL.h>
|
||||
#include <GL/glew.h>
|
||||
+#elif defined(__ANDROID__)
|
||||
+// GLES 3.2 (see Shock.c's context creation) - none of the desktop GL
|
||||
+// headers below exist on Android; EGL only ever gives out GLES contexts.
|
||||
+#include <SDL.h>
|
||||
+#include <GLES3/gl32.h>
|
||||
#else
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
#ifdef __APPLE__
|
||||
@@ -20,6 +25,112 @@
|
||||
@@ -20,6 +20,24 @@
|
||||
#include <SDL_opengl.h>
|
||||
#endif
|
||||
|
||||
+#ifdef __ANDROID__
|
||||
+// GLES has no immediate-mode rendering (glBegin/glVertex3f/glEnd, used
|
||||
+// throughout this file below) at all. These macros transparently replace
|
||||
+// it with an equivalent that accumulates vertex data into arrays and
|
||||
+// issues one glDrawArrays call at glEnd() time - every actual call site
|
||||
+// below (all unchanged) looks identical on both platforms. "position",
|
||||
+// bound to attribute location 0 in CreateShader() below, replaces
|
||||
+// gl_Vertex (a desktop-only builtin android/gles-shaders/main.vert can't
|
||||
+// use either).
|
||||
+#define glBegin questshock_glBegin
|
||||
+#define glVertex3f questshock_glVertex3f
|
||||
+#define glEnd questshock_glEnd
|
||||
+#define glVertexAttrib1f questshock_glVertexAttrib1f
|
||||
+#define glVertexAttrib2f questshock_glVertexAttrib2f
|
||||
+// GL4ES's glPointSize()/custom-shader interop goes through the same kind
|
||||
+// of shader-source rewrite as its alpha-test emulation (see
|
||||
+// init_opengl() below) - avoid relying on it. GLES point size instead
|
||||
+// comes from a "pointSize" uniform, written to gl_PointSize in
|
||||
+// android/gles-shaders/main.vert. opengl_begin_stars() (the only
|
||||
+// glPointSize() call site) now binds the star shader first, so
|
||||
+// GL_CURRENT_PROGRAM here is always correct.
|
||||
+#define glPointSize questshock_glPointSize
|
||||
+
|
||||
+// Generous enough for both the largest opengl_draw_poly() polygon and a
|
||||
+// full frame's worth of opengl_draw_star() points accumulated between one
|
||||
+// opengl_begin_stars()/opengl_end_stars() pair.
|
||||
+static const int QUESTSHOCK_MAX_IM_VERTS = 4096;
|
||||
+static float questshock_im_pos[QUESTSHOCK_MAX_IM_VERTS * 3];
|
||||
+static float questshock_im_tc[QUESTSHOCK_MAX_IM_VERTS * 2];
|
||||
+static float questshock_im_light[QUESTSHOCK_MAX_IM_VERTS];
|
||||
+static int questshock_im_count;
|
||||
+static GLenum questshock_im_mode;
|
||||
+static float questshock_im_cur_tc[2];
|
||||
+static float questshock_im_cur_light;
|
||||
+static float questshock_point_size = 1.0f;
|
||||
+
|
||||
+// GLES has no glPointSize() at all - point size is instead read from
|
||||
+// gl_PointSize, set by a "pointSize" uniform in
|
||||
+// android/gles-shaders/main.vert. Stashed here and applied in
|
||||
+// questshock_glEnd() below, since opengl_begin_stars() calls this before
|
||||
+// glUseProgram(starShaderProgram...) - the star shader isn't the current
|
||||
+// program yet at this point.
|
||||
+static void questshock_glPointSize(GLfloat size) { questshock_point_size = size; }
|
||||
+
|
||||
+static void questshock_glBegin(GLenum mode) {
|
||||
+ questshock_im_mode = mode;
|
||||
+ questshock_im_count = 0;
|
||||
+ questshock_im_cur_tc[0] = questshock_im_cur_tc[1] = 0.0f;
|
||||
+ questshock_im_cur_light = 0.0f;
|
||||
+}
|
||||
+
|
||||
+static void questshock_glVertexAttrib1f(GLuint index, GLfloat v) {
|
||||
+ // Only ever called for "light" at these call sites.
|
||||
+ (void)index;
|
||||
+ questshock_im_cur_light = v;
|
||||
+}
|
||||
+
|
||||
+static void questshock_glVertexAttrib2f(GLuint index, GLfloat a, GLfloat b) {
|
||||
+ // Only ever called for "texcoords" at these call sites.
|
||||
+ (void)index;
|
||||
+ questshock_im_cur_tc[0] = a;
|
||||
+ questshock_im_cur_tc[1] = b;
|
||||
+}
|
||||
+
|
||||
+static void questshock_glVertex3f(GLfloat x, GLfloat y, GLfloat z) {
|
||||
+ if (questshock_im_count >= QUESTSHOCK_MAX_IM_VERTS)
|
||||
+ return;
|
||||
+ questshock_im_pos[questshock_im_count * 3 + 0] = x;
|
||||
+ questshock_im_pos[questshock_im_count * 3 + 1] = y;
|
||||
+ questshock_im_pos[questshock_im_count * 3 + 2] = z;
|
||||
+ questshock_im_tc[questshock_im_count * 2 + 0] = questshock_im_cur_tc[0];
|
||||
+ questshock_im_tc[questshock_im_count * 2 + 1] = questshock_im_cur_tc[1];
|
||||
+ questshock_im_light[questshock_im_count] = questshock_im_cur_light;
|
||||
+ questshock_im_count++;
|
||||
+}
|
||||
+
|
||||
+static void questshock_glEnd() {
|
||||
+static void questshock_glPointSize(GLfloat size) {
|
||||
+ GLint program = 0;
|
||||
+ glGetIntegerv(GL_CURRENT_PROGRAM, &program);
|
||||
+ GLint posAttrib = glGetAttribLocation(program, "position");
|
||||
+ GLint tcAttrib = glGetAttribLocation(program, "texcoords");
|
||||
+ GLint lightAttrib = glGetAttribLocation(program, "light");
|
||||
+
|
||||
+ if (questshock_im_mode == GL_POINTS) {
|
||||
+ GLint pointSizeUniform = glGetUniformLocation(program, "pointSize");
|
||||
+ if (pointSizeUniform >= 0)
|
||||
+ glUniform1f(pointSizeUniform, questshock_point_size);
|
||||
+ }
|
||||
+
|
||||
+ if (posAttrib >= 0) {
|
||||
+ glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, questshock_im_pos);
|
||||
+ glEnableVertexAttribArray(posAttrib);
|
||||
+ }
|
||||
+ if (tcAttrib >= 0) {
|
||||
+ glVertexAttribPointer(tcAttrib, 2, GL_FLOAT, GL_FALSE, 0, questshock_im_tc);
|
||||
+ glEnableVertexAttribArray(tcAttrib);
|
||||
+ }
|
||||
+ if (lightAttrib >= 0) {
|
||||
+ glVertexAttribPointer(lightAttrib, 1, GL_FLOAT, GL_FALSE, 0, questshock_im_light);
|
||||
+ glEnableVertexAttribArray(lightAttrib);
|
||||
+ }
|
||||
+
|
||||
+ glDrawArrays(questshock_im_mode, 0, questshock_im_count);
|
||||
+
|
||||
+ if (posAttrib >= 0)
|
||||
+ glDisableVertexAttribArray(posAttrib);
|
||||
+ if (tcAttrib >= 0)
|
||||
+ glDisableVertexAttribArray(tcAttrib);
|
||||
+ if (lightAttrib >= 0)
|
||||
+ glDisableVertexAttribArray(lightAttrib);
|
||||
+ GLint loc = glGetUniformLocation(program, "pointSize");
|
||||
+ if (loc >= 0)
|
||||
+ glUniform1f(loc, size);
|
||||
+}
|
||||
+#endif // __ANDROID__
|
||||
+
|
||||
extern "C" {
|
||||
#include "mainloop.h"
|
||||
#include "map.h"
|
||||
@@ -229,6 +340,12 @@
|
||||
@@ -229,6 +247,13 @@
|
||||
GLuint shaderProgram = glCreateProgram();
|
||||
glAttachShader(shaderProgram, vertShader);
|
||||
glAttachShader(shaderProgram, fragShader);
|
||||
+#ifdef __ANDROID__
|
||||
+ // Must be bound before linking. Matches questshock_glEnd()'s
|
||||
+ // glGetAttribLocation(program, "position") above and
|
||||
+ // Must be bound before linking. GL4ES routes glVertex3f() (used
|
||||
+ // throughout this file below) to attribute location 0 of whatever
|
||||
+ // shader is currently bound - matches
|
||||
+ // android/gles-shaders/main.vert's "position" attribute.
|
||||
+ glBindAttribLocation(shaderProgram, 0, "position");
|
||||
+#endif
|
||||
glLinkProgram(shaderProgram);
|
||||
glUseProgram(shaderProgram);
|
||||
|
||||
@@ -313,10 +430,17 @@
|
||||
@@ -313,10 +338,18 @@
|
||||
|
||||
glEnable(GL_CULL_FACE);
|
||||
glEnable(GL_BLEND);
|
||||
+#ifndef __ANDROID__
|
||||
+ // GLES has no fixed-function pipeline: no alpha test (emulated with a
|
||||
+ // discard in android/gles-shaders/texture.frag instead), and point
|
||||
+ // sprites are always implicitly on for GL_POINTS rendering (via
|
||||
+ // gl_PointCoord in android/gles-shaders/star.frag) - no enum/enable
|
||||
+ // for either exists in GLES at all.
|
||||
+ // GL4ES emulates both of these (alpha test, point sprites) for a
|
||||
+ // custom shader by rewriting its source under the hood - a rougher
|
||||
+ // edge of the library. Skip relying on that: alpha test is instead
|
||||
+ // a discard in android/gles-shaders/texture.frag, and point-sprite
|
||||
+ // rasterization for GL_POINTS works automatically in GLES with no
|
||||
+ // enable call at all (see android/gles-shaders/star.frag).
|
||||
glEnable(GL_ALPHA_TEST);
|
||||
glEnable(GL_POINT_SPRITE);
|
||||
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
@@ -157,3 +59,17 @@
|
||||
|
||||
CreateShader("main.vert", "texture.frag", &textureShaderProgram);
|
||||
CreateShader("main.vert", "color.frag", &colorShaderProgram);
|
||||
@@ -908,9 +941,12 @@
|
||||
void opengl_begin_stars() {
|
||||
SDL_GL_MakeCurrent(window, context);
|
||||
|
||||
+ // Bind the star shader before setting the point size: on Android,
|
||||
+ // questshock_glPointSize() (see the __ANDROID__ block above) needs
|
||||
+ // it to already be GL_CURRENT_PROGRAM.
|
||||
+ glUseProgram(starShaderProgram.shaderProgram);
|
||||
glPointSize(1.5 * (render_width / 320.0));
|
||||
|
||||
- glUseProgram(starShaderProgram.shaderProgram);
|
||||
glUniformMatrix4fv(starShaderProgram.uniView, 1, false, IdentityMatrix);
|
||||
glUniformMatrix4fv(starShaderProgram.uniProj, 1, false, IdentityMatrix);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user