Skip to content

Commit 175301f

Browse files
Share defined OpenGL textures with NVN
1 parent 4c24f51 commit 175301f

7 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎dm/gfx/gl/hle_gl.m‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
// Environment: DM_GL_LOG=1 logs object creation; DM_GL_CAPTURE=<dir> writes every shader source the game compiles (<key>.vert / <key>.frag, read by gl_shaders.py).
2020
#include "../../host/hle.h"
2121
#include "../nvn/nvn_mtl.h"
22+
#include "../nvn/nvn_tex.h"
23+
#include <nvn.h>
2224
#include "../nvn/host_msl.h"
2325
#import <Metal/Metal.h>
2426
#include <CommonCrypto/CommonDigest.h>
@@ -601,6 +603,20 @@ static int tex_make(tex_t *t, int rt) {
601603
OLOG("texture %ux%u %s, %d levels%s", t->w, t->h, m->name, lv, faces == 6 ? ", cube" : "");
602604
return 1;
603605
}
606+
// Borrow a defined 2D image for an NVN texture. The NVN serial table retains the same Metal object.
607+
void *gl_host_texture_2d(uint32_t name, nvn_texdesc *desc) {
608+
tex_t *t = tex_of(name);
609+
if (!t || t->target != GL_TEXTURE_2D || !tex_make(t, 1) || !t->tex) return NULL;
610+
int format;
611+
switch (t->tex.pixelFormat) {
612+
case MTLPixelFormatRGBA8Unorm: format = NVN_FORMAT_RGBA8; break;
613+
case MTLPixelFormatRGBA8Unorm_sRGB: format = NVN_FORMAT_RGBA8_SRGB; break;
614+
default: return NULL;
615+
}
616+
*desc = (nvn_texdesc){.target = NVN_TEXTURE_TARGET_2D, .format = format,
617+
.width = t->w, .height = t->h, .depth = 1, .levels = t->levels};
618+
return (__bridge void *)t->tex;
619+
}
604620
// Write one level's (or a part's) bytes into a made texture, in command order: a CPU write when no command buffer that may still read it is pending,
605621
// else a blit from transient memory (in this frame's command buffer, after ending its render pass, when this frame used it; in a command buffer of its own otherwise).
606622
// face: the cube face or array layer; z: the slice of a 3D texture

‎dm/gfx/nvn/hle_nvn.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ NVN(nvnTextureBuilderSetDefaults, void, NVNtextureBuilder *b) {
327327
TB(b)->d.target = NVN_TEXTURE_TARGET_2D; TB(b)->d.width = TB(b)->d.height = TB(b)->d.depth = TB(b)->d.levels = 1;
328328
}
329329
NVN(nvnTextureBuilderSetDevice, void, NVNtextureBuilder *b, NVNdevice *d) { TB(b)->dev = d; }
330+
NVN(nvnTextureBuilderSetGLTextureName, void, NVNtextureBuilder *b, uint32_t name) { TB(b)->gl_name = name; }
331+
NVN(nvnTextureBuilderGetGLTextureName, uint32_t, const NVNtextureBuilder *b) { return ((const tex_builder_t *)b)->gl_name; }
330332
NVN(nvnTextureBuilderSetFlags, void, NVNtextureBuilder *b, int f) { TB(b)->d.flags = f; }
331333
NVN(nvnTextureBuilderSetTarget, void, NVNtextureBuilder *b, NVNtextureTarget t) { TB(b)->d.target = t; }
332334
NVN(nvnTextureBuilderSetWidth, void, NVNtextureBuilder *b, int v) { TB(b)->d.width = v; }
@@ -493,12 +495,30 @@ int nvn_flushed(const void *p, uint64_t n) { // whether the CPU flushed any of
493495
pthread_mutex_unlock(&g_fl_mu);
494496
return r;
495497
}
498+
extern void *gl_host_texture_2d(uint32_t name, nvn_texdesc *desc) __attribute__((weak_import));
496499
NVN(nvnTextureInitialize, NVNboolean, NVNtexture *t, const NVNtextureBuilder *b) {
497500
const tex_builder_t *s = (const tex_builder_t *)b; tex_t *o = TX(t);
501+
nvn_texdesc gl_desc;
502+
void *gl_tex = NULL;
503+
if (s->gl_name) {
504+
gl_tex = gl_host_texture_2d ? gl_host_texture_2d(s->gl_name, &gl_desc) : NULL;
505+
if (!gl_tex) {
506+
static _Atomic int warned;
507+
if (!atomic_exchange(&warned, 1)) LOG("[RED] NVN cannot import GL texture %u (not a defined RGBA8 2D texture)", s->gl_name);
508+
return 0;
509+
}
510+
}
498511
// Re-initializing a texture object that is still live (without a Finalize first): release the host texture of the old serial first. SMBW re-initializes its transient render targets on the same object every frame and never Finalizes them;
499512
// previously every init created a new private MTLTexture and the old one stayed in the serial table forever, so GPU memory grew about 13 MB per frame (killed by the memory watchdog at 24 GB)
500513
if (o->magic == MAGIC('T', 'E', 'X', 0) && o->serial) { static uint64_t n; if (!n++) LOG("[nvn] TextureInitialize on a texture object that is still live (no Finalize): releasing the old host texture (first case ser=%llu)", (unsigned long long)o->serial); mtl_tex_release(o); }
501514
memset(o, 0, sizeof *o);
515+
if (gl_tex) {
516+
o->magic = MAGIC('T', 'E', 'X', 0); o->d = gl_desc;
517+
nvn_tex_layout(&o->d, &o->L);
518+
o->serial = nvn_new_serial(); mtl_tex_init(o); mtl_tex_import_shared(o->serial, gl_tex);
519+
OLOG("texture %p shares GL name %u (%ux%u)", (void *)t, s->gl_name, o->d.width, o->d.height);
520+
return 1;
521+
}
502522
o->magic = MAGIC('T', 'E', 'X', 0); o->d = s->d; o->pool = s->pool; o->off = s->off;
503523
o->has_layout = s->has_layout; memcpy(o->layout, s->layout, 8); o->set_swz = s->set_swz; o->stencil = s->stencil; o->swz = s->swz;
504524
nvn_tex_layout(&o->d, &o->L);

‎dm/gfx/nvn/nvn_mtl.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ void mtl_xv_window_tex(uint64_t serial); // Register a window (swapchain) text
5050
void *mtl_device(void);
5151
void *mtl_device_classic(void); // the same for a layer that draws only on classic queues: a device without Metal 4 (a virtual Mac) starts too, and its presents are dropped
5252
uint64_t mtl_ext_tex(void *tex);
53+
void mtl_tex_import_shared(uint64_t serial, void *tex); // Retain a GL texture for the lifetime of an NVN serial
5354
uint64_t mtl_present_ext(uint64_t serial, double period, void *ev, uint64_t ev_val, const int crop[4]); // crop: x, y, w, h of the texture to show (NULL = all of it)
5455
void mtl_present_wait(uint64_t ticket); // as in nvn_obj.h: wait until that present has been handed to the GPU

‎dm/gfx/nvn/nvn_mtl.m‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12331,6 +12331,10 @@ static void present_drop_for_suspend(void) {
1233112331
void *mtl_device(void) { mtl_init(); return (__bridge void *)g_dev; }
1233212332
void *mtl_device_classic(void) { g_classic_ok = 1; return mtl_device(); } // the GL and Vulkan layers: a device without Metal 4 is taken too, with presents dropped (mtl_init)
1233312333
// An external producer's texture (the Vulkan layer's swapchain images): a serial the present path resolves (serial_put), resident for the Metal 4 queue, never collected by the cross-submit ledger
12334+
void mtl_tex_import_shared(uint64_t serial, void *tex) {
12335+
id<MTLTexture> t = (__bridge id<MTLTexture>)tex;
12336+
res_add(t); serial_put(serial, t);
12337+
}
1233412338
uint64_t mtl_ext_tex(void *tex) {
1233512339
static _Atomic uint64_t n; id<MTLTexture> t = (__bridge id<MTLTexture>)tex; uint64_t ser = (1ull << 62) + atomic_fetch_add(&n, 1) + 1;
1233612340
res_add(t); serial_put(ser, t); mtl_xv_window_tex(ser); return ser;

‎dm/gfx/nvn/nvn_mtl_legacy.inc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10034,6 +10034,10 @@ uint64_t mtl_present(const tex_t *t, double period, const int crop[4]) {
1003410034
// Bridge used by the Vulkan/OpenGL front ends that are linked into the common
1003510035
// host. Z-A itself uses NVN, but the symbols remain part of the shared host.
1003610036
void *mtl_device(void) { mtl_init(); return (__bridge void *)g_dev; }
10037+
void mtl_tex_import_shared(uint64_t serial, void *tex) {
10038+
id<MTLTexture> t = (__bridge id<MTLTexture>)tex;
10039+
res_add(t); serial_put(serial, t);
10040+
}
1003710041
uint64_t mtl_ext_tex(void *tex) {
1003810042
static _Atomic uint64_t n;
1003910043
id<MTLTexture> t = (__bridge id<MTLTexture>)tex;

‎dm/gfx/nvn/nvn_obj.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ typedef struct {
2929
NVNdevice *dev; nvn_texdesc d; pool_t *pool; int64_t off;
3030
uint8_t has_layout, layout[8];
3131
const void *pkg_data; // TextureBuilderSetPackagedTextureData: copied into the storage at TextureInitialize (13 Sentinels)
32+
uint32_t gl_name;
3233
uint8_t set_swz : 1, stencil : 1; uint16_t swz; // TextureBuilderSetSwizzle: r | g << 3 | b << 6 | a << 9 (NVN and Metal channel enums have the same values: 0 zero 1 one 2 R 3 G 4 B 5 A);
3334
// stencil: TextureBuilderSetDepthStencilMode STENCIL (the texture reads the stencil of its depth-stencil storage, as a stencil-mode view does)
3435
} tex_builder_t; FITS(tex_builder_t, NVNtextureBuilder);

‎dm/gfx/tests/t_gl_imports.m‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,37 @@ int main(int argc, char **argv) {
123123
assert(h_glClientWaitSync(out, 0, 0) == GL_ALREADY_SIGNALED);
124124
h_glWaitSync(out, 0, UINT64_MAX);
125125
h_glDeleteSync(out); assert(!h_glGetError());
126+
if (getenv("DM_TEST_GL_NVNTEX")) {
127+
dev_init_once();
128+
assert(g_dev);
129+
NVNtextureBuilder tb = {0};
130+
NVNtexture tex = {0};
131+
void (*defaults)(NVNtextureBuilder *) = nvn_lookup("nvnTextureBuilderSetDefaults");
132+
void (*set_name)(NVNtextureBuilder *, uint32_t) = nvn_lookup("nvnTextureBuilderSetGLTextureName");
133+
uint32_t (*get_name)(const NVNtextureBuilder *) = nvn_lookup("nvnTextureBuilderGetGLTextureName");
134+
NVNboolean (*init_tex)(NVNtexture *, const NVNtextureBuilder *) = nvn_lookup("nvnTextureInitialize");
135+
void (*fini_tex)(NVNtexture *) = nvn_lookup("nvnTextureFinalize");
136+
int (*width)(const NVNtexture *) = nvn_lookup("nvnTextureGetWidth");
137+
int (*height)(const NVNtexture *) = nvn_lookup("nvnTextureGetHeight");
138+
defaults(&tb);
139+
assert(!get_name(&tb));
140+
set_name(&tb, 0xf123);
141+
assert(!init_tex(&tex, &tb)); // undefined texture is never reported as an initialized NVN image
142+
GLuint name = 0;
143+
h_glGenTextures(1, &name);
144+
h_glBindTexture(GL_TEXTURE_2D, name);
145+
h_glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 32, 16);
146+
set_name(&tb, name);
147+
assert(get_name(&tb) == name);
148+
assert(init_tex(&tex, &tb) && width(&tex) == 32 && height(&tex) == 16);
149+
id<MTLTexture> image = tex_of(name)->tex;
150+
assert(image && image.width == 32 && image.height == 16);
151+
fini_tex(&tex);
152+
assert(tex_of(name)->tex == image);
153+
h_glDeleteTextures(1, &name);
154+
defaults(&tb);
155+
assert(!get_name(&tb));
156+
}
126157
const char *missing = "glMissingFunctionForTest";
127158
assert(!hle_implements(missing));
128159
assert((h_eglGetProcAddress(missing) != NULL) == stubs);

0 commit comments

Comments
 (0)