Skip to content

Commit f691081

Browse files
Fix Storm 4 startup on the OpenGL ES path
1 parent df98483 commit f691081

1 file changed

Lines changed: 42 additions & 9 deletions

File tree

‎dm/gfx/gl/hle_gl.m‎

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ static uint32_t name_new(void ***tab, uint32_t *n, uint32_t *cap, size_t sz) {
311311
static rb_t *rb_of(GLuint n) { return n && n < g_nrbs ? g_rbs[n] : NULL; }
312312

313313
typedef struct { int kind; GLuint name; int level, face; } att_t; // kind 0 none, 1 texture, 2 renderbuffer; face: a cube face, an array layer or a 3D slice
314-
typedef struct { att_t col, depth, stencil; uint64_t gen; } fb_t;
314+
typedef struct { att_t col, depth, stencil; uint64_t gen; int bound; } fb_t;
315315
NAMES(fb_t, g_fbs, g_nfbs, g_capfbs)
316316
static fb_t *fb_of(GLuint n) { return n && n < g_nfbs ? g_fbs[n] : NULL; }
317317

@@ -1228,6 +1228,12 @@ static void cap_set(GLenum cap, int on) {
12281228
GL(glPopGroupMarkerEXT, void, void) {}
12291229
GL(glObjectLabel, void, GLenum id, GLuint name, GLsizei len, const GLchar *label) { (void)id; (void)name; (void)len; (void)label; }
12301230

1231+
// Keep the API identity with the current EGL handle, including when a game
1232+
// makes its context current on a different thread.
1233+
enum { EGL_CONTEXT_DESKTOP = 2, EGL_CONTEXT_ES3 = 5 };
1234+
static _Thread_local struct { EGLDisplay display; EGLContext context; EGLSurface draw, read; } g_egl_current;
1235+
static int gl_is_es3(void) { return (uintptr_t)g_egl_current.context == EGL_CONTEXT_ES3; }
1236+
12311237
// ---------- Queries ----------
12321238
static const char *const g_exts[] = {"GL_ARB_ES3_2_compatibility", "GL_ARB_ES3_compatibility", "GL_ARB_texture_compression_bptc", "GL_ARB_texture_compression_rgtc",
12331239
"GL_EXT_texture_compression_s3tc", "GL_EXT_texture_filter_anisotropic", "GL_ARB_vertex_array_object", "GL_ARB_direct_state_access", "GL_ARB_half_float_vertex",
@@ -1237,8 +1243,8 @@ static void cap_set(GLenum cap, int on) {
12371243
switch (n) {
12381244
case GL_VENDOR: return "NVIDIA Corporation";
12391245
case GL_RENDERER: return "NVIDIA Tegra X1 (nvgpu)/integrated (Metal)";
1240-
case GL_VERSION: return "4.5.0 NVIDIA 349.00";
1241-
case GL_SHADING_LANGUAGE_VERSION: return "4.50 NVIDIA";
1246+
case GL_VERSION: return gl_is_es3() ? "OpenGL ES 3.0" : "4.5.0 NVIDIA 349.00";
1247+
case GL_SHADING_LANGUAGE_VERSION: return gl_is_es3() ? "OpenGL ES GLSL ES 3.00" : "4.50 NVIDIA";
12421248
case GL_EXTENSIONS: { // the compatibility profile's one string
12431249
static char all[1024]; if (!all[0]) for (int i = 0; i < NEXTS; i++) { strlcat(all, g_exts[i], sizeof all); strlcat(all, " ", sizeof all); }
12441250
return all;
@@ -1250,9 +1256,9 @@ static void cap_set(GLenum cap, int on) {
12501256
GL(glGetIntegerv, void, GLenum p, GLint *v) {
12511257
switch (p) {
12521258
case GL_NUM_EXTENSIONS: *v = NEXTS; return;
1253-
case GL_MAJOR_VERSION: *v = 4; return;
1254-
case GL_MINOR_VERSION: *v = 5; return;
1255-
case GL_CONTEXT_PROFILE_MASK: *v = 1; return; // CONTEXT_CORE_PROFILE_BIT: what the games asked for (Digimon a 4.5 core context); LÖVE takes its core paths (the file header)
1259+
case GL_MAJOR_VERSION: *v = gl_is_es3() ? 3 : 4; return;
1260+
case GL_MINOR_VERSION: *v = gl_is_es3() ? 0 : 5; return;
1261+
case GL_CONTEXT_PROFILE_MASK: *v = gl_is_es3() ? 0 : 1; return; // CONTEXT_CORE_PROFILE_BIT: what the games asked for (Digimon a 4.5 core context); LÖVE takes its core paths (the file header)
12561262
case GL_CONTEXT_FLAGS: *v = 0; return;
12571263
case GL_MAX_TEXTURE_SIZE: case GL_MAX_RENDERBUFFER_SIZE: case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *v = 16384; return;
12581264
case GL_MAX_3D_TEXTURE_SIZE: case GL_MAX_ARRAY_TEXTURE_LAYERS: *v = 2048; return;
@@ -1434,6 +1440,20 @@ int gl_host_wait_fence(uint64_t serial, uint64_t timeout_ns) {
14341440
pass_end();
14351441
[cb_get() encodeWaitForEvent:event value:serial];
14361442
}
1443+
GL(glGetSynciv, void, void *sync, GLenum pname, GLsizei size, GLsizei *length, GLint *values) {
1444+
uint64_t serial; int from_nvn; GLint value;
1445+
if (size < 0) { if (!g_gl_error) g_gl_error = 0x501; return; }
1446+
if (!gl_sync_info(sync, &serial, &from_nvn)) return;
1447+
switch (pname) {
1448+
case 0x9112: value = 0x9116; break; // OBJECT_TYPE: SYNC_FENCE
1449+
case 0x9113: value = 0x9117; break; // SYNC_CONDITION: GPU_COMMANDS_COMPLETE
1450+
case 0x9114: value = (!serial || (from_nvn ? mtl_wait(serial, 0) : g_ev.signaledValue >= serial)) ? 0x9119 : 0x9118; break;
1451+
case 0x9115: value = 0; break; // SYNC_FLAGS
1452+
default: if (!g_gl_error) g_gl_error = 0x500; return; // GL_INVALID_ENUM
1453+
}
1454+
if (size > 0) values[0] = value;
1455+
if (length) *length = size > 0;
1456+
}
14371457
GLB(glClientWaitSync, GLenum, void *sy, GLbitfield flags, uint64_t timeout_ns) {
14381458
uint64_t serial; int from_nvn;
14391459
if (!gl_sync_info(sy, &serial, &from_nvn)) return 0x911d; // GL_WAIT_FAILED
@@ -1465,6 +1485,11 @@ int gl_host_wait_fence(uint64_t serial, uint64_t timeout_ns) {
14651485
GL(glVertexAttrib4fv, void, GLuint i, const GLfloat *v) { if (i < 16) memcpy(S.va[i], v, sizeof S.va[i]); }
14661486

14671487
// ---------- Textures ----------
1488+
// Sampler zero selects the parameters on the bound texture, the only
1489+
// sampling state this layer supports. Reject names it cannot create.
1490+
GL(glBindSampler, void, GLuint unit, GLuint sampler) {
1491+
if ((unit >= NUNITS || sampler) && !g_gl_error) g_gl_error = 0x501; // GL_INVALID_VALUE
1492+
}
14681493
GL(glGenTextures, void, GLsizei n, GLuint *out) { for (GLsizei i = 0; i < n; i++) out[i] = name_new((void ***)&g_texs, &g_ntexs, &g_captexs, sizeof(tex_t)); }
14691494
GL(glDeleteTextures, void, GLsizei n, const GLuint *t) {
14701495
for (GLsizei i = 0; i < n; i++) {
@@ -1629,7 +1654,9 @@ static void mip_impl(GLenum target) {
16291654
GLB(glDeleteFramebuffers, void, GLsizei n, const GLuint *f) {
16301655
for (GLsizei i = 0; i < n; i++) { fb_t *x = fb_of(f[i]); if (!x) continue; if (P.open && P.fb == f[i]) pass_end(); if (g_pend_clear.fb == f[i]) g_pend_clear.bits = 0; free(x); g_fbs[f[i]] = NULL; if (S.fb == f[i]) S.fb = 0; }
16311656
}
1657+
GL(glIsFramebuffer, uint32_t, GLuint f) { fb_t *fb = fb_of(f); return fb && fb->bound; }
16321658
GLB(glBindFramebuffer, void, GLenum target, GLuint f) {
1659+
fb_t *fb = fb_of(f); if (fb) fb->bound = 1;
16331660
if (target != GL_DRAW_FRAMEBUFFER) S.read_fb = f; // GL_FRAMEBUFFER binds both
16341661
if (target == GL_READ_FRAMEBUFFER || f == S.fb) return;
16351662
{ static int on = -1; if (on < 0) on = getenv("DM_GL_FBLOG") != NULL; if (on) { uint32_t bw, bh; fb_size(f, &bw, &bh); GLOG("bind framebuffer %u %ux%u (was %u) after %llu draws", f, bw, bh, S.fb, (unsigned long long)g_n_draw); } }
@@ -1777,6 +1804,7 @@ static void sh_capture(const shader_t *s) {
17771804
GL(glCreateShader, GLuint, GLenum type) {
17781805
GLuint n = name_new((void ***)&g_sps, &g_nsps, &g_capsps, sizeof(sp_t)); g_sps[n]->s.type = type; return n;
17791806
}
1807+
GL(glIsProgram, uint32_t, GLuint pr) { return prog_of(pr) != NULL; }
17801808
GL(glCreateProgram, GLuint, void) {
17811809
GLuint n = name_new((void ***)&g_sps, &g_nsps, &g_capsps, sizeof(sp_t)); g_sps[n]->is_prog = 1; return n;
17821810
}
@@ -2023,7 +2051,6 @@ static void frame_end(void) {
20232051
EGL(eglGetDisplay, EGLDisplay, void *native) { (void)native; dev_init(); return (EGLDisplay)&g_egl; }
20242052
// EGL current bindings belong to the calling thread, including an unbound
20252053
// thread. Keep the handles from MakeCurrent rather than inventing a display.
2026-
static _Thread_local struct { EGLDisplay display; EGLContext context; EGLSurface draw, read; } g_egl_current;
20272054
static _Thread_local EGLint g_egl_error;
20282055
EGL(eglGetCurrentDisplay, EGLDisplay, void) { return g_egl_current.display; }
20292056
EGL(eglGetCurrentContext, EGLContext, void) { return g_egl_current.context; }
@@ -2077,8 +2104,14 @@ static void frame_end(void) {
20772104
(void)d; (void)cfg;
20782105
int major = 0, minor = 0; for (int i = 0; attr && attr[i] != 0x3038 && i < 32; i += 2) { if (attr[i] == 0x3098) major = attr[i + 1]; else if (attr[i] == 0x30fb) minor = attr[i + 1]; }
20792106
if (share) RED1("shared EGL contexts are not implemented (one context)");
2080-
GLOG("EGL context: OpenGL %d.%d", major, minor);
2081-
return (EGLContext)(uintptr_t)2;
2107+
// Storm 4's Scaleform loader uses the default ES API and parses
2108+
// "OpenGL ES %d.%d". A desktop version leaves its glGetIntegerv dispatch
2109+
// slot empty, so a zero texture limit leads to an undersized decode buffer.
2110+
// Restrict the implicit ES context to the verified NSO; desktop callers
2111+
// still use the existing GL 4.5 identity and state.
2112+
int es3 = major == 3 && minor == 0 && !strcmp(dm_main_build(), "d3016fc0c0402dfb70fba6b826ea772f");
2113+
GLOG("EGL context: OpenGL%s %d.%d", es3 ? " ES" : "", major, minor);
2114+
return (EGLContext)(uintptr_t)(es3 ? EGL_CONTEXT_ES3 : EGL_CONTEXT_DESKTOP);
20822115
}
20832116
EGL(eglCreateWindowSurface, EGLSurface, EGLDisplay d, EGLConfig cfg, void *win, const EGLint *attr) {
20842117
(void)d; (void)cfg; (void)win; (void)attr; dev_init();

0 commit comments

Comments
 (0)