Skip to content

Commit 2eddd56

Browse files
Handle framebuffer attachment queries before Naruto battles
1 parent d4c8ed4 commit 2eddd56

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

‎dm/gfx/gl/hle_gl.m‎

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@
114114
GL_FLOAT_VEC2 = 0x8b50, GL_FLOAT_VEC3, GL_FLOAT_VEC4, GL_INT_VEC2, GL_INT_VEC3, GL_INT_VEC4, GL_BOOL, GL_FLOAT_MAT2 = 0x8b5a, GL_FLOAT_MAT3, GL_FLOAT_MAT4, GL_SAMPLER_2D = 0x8b5e,
115115
GL_SAMPLER_3D, GL_SAMPLER_CUBE, GL_SAMPLER_2D_SHADOW = 0x8b62, GL_TEXTURE_2D_ARRAY = 0x8c1a, GL_TEXTURE_BINDING_2D_ARRAY = 0x8c1d,
116116
GL_READ_FRAMEBUFFER = 0x8ca8, GL_DRAW_FRAMEBUFFER, GL_READ_FRAMEBUFFER_BINDING, GL_RENDERBUFFER_SAMPLES, GL_FRAMEBUFFER_COMPLETE = 0x8cd5, GL_MAX_COLOR_ATTACHMENTS = 0x8cdf,
117+
GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = 0x8cd0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
118+
GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL, GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE, GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER,
119+
GL_FRAMEBUFFER_DEFAULT = 0x8218, GL_TEXTURE = 0x1702,
120+
GL_RENDERBUFFER_DEPTH_SIZE = 0x8d54, GL_RENDERBUFFER_STENCIL_SIZE,
117121
GL_RENDERBUFFER_WIDTH = 0x8d42, GL_RENDERBUFFER_HEIGHT, GL_RENDERBUFFER_INTERNAL_FORMAT, GL_FRAMEBUFFER_SRGB = 0x8db9, GL_SAMPLER_2D_ARRAY = 0x8dc1,
118122
GL_UNSIGNED_INT_VEC2 = 0x8dc6, GL_UNSIGNED_INT_VEC3, GL_UNSIGNED_INT_VEC4, GL_TEXTURE_SWIZZLE_R = 0x8e42, GL_TEXTURE_SWIZZLE_A = 0x8e45,
119123
GL_ALREADY_SIGNALED = 0x911a, GL_TIMEOUT_EXPIRED, GL_CONDITION_SATISFIED, GL_CONTEXT_PROFILE_MASK = 0x9126,
@@ -314,7 +318,7 @@ static uint32_t name_new(void ***tab, uint32_t *n, uint32_t *cap, size_t sz) {
314318
NAMES(tex_t, g_texs, g_ntexs, g_captexs)
315319
static tex_t *tex_of(GLuint n) { return n && n < g_ntexs ? g_texs[n] : NULL; }
316320

317-
typedef struct { int fmt; uint32_t w, h; __strong id<MTLTexture> tex; uint64_t gen; } rb_t;
321+
typedef struct { int fmt; uint32_t w, h; __strong id<MTLTexture> tex; uint64_t gen; GLenum internal_fmt; } rb_t;
318322
NAMES(rb_t, g_rbs, g_nrbs, g_caprbs)
319323
static rb_t *rb_of(GLuint n) { return n && n < g_nrbs ? g_rbs[n] : NULL; }
320324

@@ -1695,6 +1699,16 @@ static void mip_impl(GLenum target) {
16951699
case GL_RENDERBUFFER_SAMPLES: *v = 0; return;
16961700
case GL_RENDERBUFFER_WIDTH: *v = r ? (GLint)r->w : 0; return;
16971701
case GL_RENDERBUFFER_HEIGHT: *v = r ? (GLint)r->h : 0; return;
1702+
case GL_RENDERBUFFER_INTERNAL_FORMAT: *v = r ? (GLint)r->internal_fmt : 0; return;
1703+
case GL_RENDERBUFFER_DEPTH_SIZE:
1704+
switch (r ? r->internal_fmt : 0) {
1705+
case GL_DEPTH_COMPONENT16: *v = 16; return;
1706+
case GL_DEPTH_COMPONENT24: case GL_DEPTH24_STENCIL8: *v = 24; return;
1707+
case GL_DEPTH_COMPONENT32: case GL_DEPTH_COMPONENT32F: case GL_DEPTH32F_STENCIL8: *v = 32; return;
1708+
default: *v = 0; return;
1709+
}
1710+
case GL_RENDERBUFFER_STENCIL_SIZE:
1711+
*v = r && (r->internal_fmt == GL_STENCIL_INDEX8 || r->internal_fmt == GL_DEPTH24_STENCIL8 || r->internal_fmt == GL_DEPTH32F_STENCIL8) ? 8 : 0; return;
16981712
}
16991713
RED1("glGetRenderbufferParameteriv(%#x) not implemented", p); *v = 0;
17001714
}
@@ -1705,7 +1719,7 @@ static void mip_impl(GLenum target) {
17051719
@autoreleasepool {
17061720
MTLTextureDescriptor *td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:g_fmt[f].pf width:w height:h mipmapped:NO];
17071721
td.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead; td.storageMode = MTLStorageModePrivate;
1708-
r->tex = [g_dev newTextureWithDescriptor:td]; r->fmt = f; r->w = w; r->h = h; r->gen++;
1722+
r->tex = [g_dev newTextureWithDescriptor:td]; r->fmt = f; r->w = w; r->h = h; r->gen++; r->internal_fmt = ifmt;
17091723
}
17101724
OLOG("renderbuffer %u: %dx%d %s", S.rb, w, h, g_fmt[f].name);
17111725
}
@@ -1730,6 +1744,47 @@ static void fb_attach(GLenum target, GLenum attachment, int kind, GLuint name, i
17301744
GL(glFramebufferTextureLayer, void, GLenum target, GLenum att, GLuint t, GLint level, GLint layer) { fb_attach(target, att, 1, t, level, layer); }
17311745
GL(glFramebufferTexture3D, void, GLenum target, GLenum att, GLenum textarget, GLuint t, GLint level, GLint z) { (void)textarget; fb_attach(target, att, 1, t, level, z); }
17321746
GL(glFramebufferRenderbuffer, void, GLenum target, GLenum att, GLenum rbt, GLuint r) { (void)rbt; fb_attach(target, att, 2, r, 0, 0); }
1747+
// Report the attachment actually bound; Naruto checks stencil and depth before a battle.
1748+
GL(glGetFramebufferAttachmentParameteriv, void, GLenum target, GLenum attachment, GLenum p, GLint *v) {
1749+
GLenum error = 0x500; // GL_INVALID_ENUM
1750+
if (!v || (target != GL_FRAMEBUFFER && target != GL_DRAW_FRAMEBUFFER && target != GL_READ_FRAMEBUFFER)) goto fail;
1751+
GLuint name = target == GL_READ_FRAMEBUFFER ? S.read_fb : S.fb;
1752+
fb_t *f = fb_of(name);
1753+
const att_t *a;
1754+
if (!name) {
1755+
if (attachment != GL_BACK && attachment != GL_DEPTH && attachment != GL_STENCIL) goto fail;
1756+
if (p == GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE) { *v = GL_FRAMEBUFFER_DEFAULT; return; }
1757+
error = 0x502; goto fail; // GL_INVALID_OPERATION
1758+
}
1759+
if (!f) { error = 0x502; goto fail; }
1760+
if (attachment == GL_COLOR_ATTACHMENT0) a = &f->col;
1761+
else if (attachment == GL_DEPTH_ATTACHMENT) a = &f->depth;
1762+
else if (attachment == GL_STENCIL_ATTACHMENT) a = &f->stencil;
1763+
else if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1764+
if (f->depth.kind != f->stencil.kind || f->depth.name != f->stencil.name ||
1765+
f->depth.level != f->stencil.level || f->depth.face != f->stencil.face) { error = 0x502; goto fail; }
1766+
a = &f->depth;
1767+
} else goto fail;
1768+
if (p == GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE) {
1769+
*v = a->kind == 1 ? GL_TEXTURE : a->kind == 2 ? GL_RENDERBUFFER : 0; return;
1770+
}
1771+
if (p == GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME) { *v = (GLint)a->name; return; }
1772+
if (a->kind == 1) {
1773+
tex_t *t = tex_of(a->name);
1774+
switch (p) {
1775+
case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL: *v = a->level; return;
1776+
case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
1777+
*v = t && t->target == GL_TEXTURE_CUBE_MAP ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + a->face : 0; return;
1778+
case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER:
1779+
*v = t && (t->target == GL_TEXTURE_2D_ARRAY || t->target == GL_TEXTURE_3D) ? a->face : 0; return;
1780+
}
1781+
} else error = 0x502;
1782+
fail:
1783+
if (!g_gl_error) g_gl_error = error;
1784+
}
1785+
GL(glGetFramebufferAttachmentParameterivEXT, void, GLenum target, GLenum attachment, GLenum p, GLint *v) {
1786+
h_glGetFramebufferAttachmentParameteriv(target, attachment, p, v);
1787+
}
17331788
// A framebuffer's colour image: the texture, its level and its slice (a cube face or an array layer) or 3D slice
17341789
static id<MTLTexture> fb_color(GLuint fb, int *level, int *slice) {
17351790
*level = *slice = 0;

‎dm/gfx/tests/t_gl_imports.m‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ int main(int argc, char **argv) {
6262
GLsizei len = -1; GLchar log[2] = {'?', 0}; info(sh, sizeof log, &len, log); assert(!len && !log[0]);
6363
void (*delete)(GLuint) = import("glDeleteShader"); delete(sh); assert(!shader_of(sh));
6464

65+
// Naruto queries an empty stencil slot and then a combined depth/stencil
66+
// renderbuffer while preparing a fight. Both loader spellings must work.
67+
void (*query)(GLenum, GLenum, GLenum, GLint *) = import("glGetFramebufferAttachmentParameteriv");
68+
void (*query_ext)(GLenum, GLenum, GLenum, GLint *) = h_eglGetProcAddress("glGetFramebufferAttachmentParameterivEXT");
69+
assert(query_ext == import("glGetFramebufferAttachmentParameterivEXT"));
70+
GLuint fbo, rb, depth_tex;
71+
h_glGenFramebuffers(1, &fbo);
72+
h_glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
73+
query(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value);
74+
assert(value == 0 && h_glGetError() == 0);
75+
h_glGenRenderbuffers(1, &rb);
76+
h_glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb);
77+
query_ext(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value);
78+
assert(value == GL_RENDERBUFFER);
79+
query(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &value);
80+
assert((GLuint)value == rb);
81+
query(GL_READ_FRAMEBUFFER, GL_DEPTH, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value);
82+
assert(value == GL_FRAMEBUFFER_DEFAULT);
83+
h_glGenTextures(1, &depth_tex);
84+
h_glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_tex, 2);
85+
query(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL, &value);
86+
assert(value == 2);
87+
query(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value);
88+
assert(value == 2 && h_glGetError() == 0x502);
89+
h_glDeleteFramebuffers(1, &fbo);
90+
h_glDeleteRenderbuffers(1, &rb);
91+
h_glDeleteTextures(1, &depth_tex);
92+
6593
unsigned long sz;
6694
const hle_ent *sec = (const hle_ent *)getsectiondata(&_mh_execute_header, "__DATA", "swhle", &sz);
6795
unsigned count = 0;

0 commit comments

Comments
 (0)