gl/textures: GL_TEXTURE_LOD_BIAS is the sampler's lodBias

The GL layer dropped a texture's LOD bias with a red line saying Metal samplers have none; Metal has had MTLSamplerDescriptor.lodBias since macOS and iOS 26, the host's minimum on both. The texture now keeps its bias, the sampler cache key holds it, and its sampler gets it clamped to ±15 (the GL_MAX_TEXTURE_LOD_BIAS the layer reports). DOOM 3 sets 0.5 on some images, which now sample half a level coarser, as on the Switch; LÖVE sets −(mipmap sharpness), 0 by default.

Checked: t_gl_imports (DM_GL_STUBS=0 and 1, Metal validation): a two-level texture drawn 1:1 reads level 0 with no bias and level 1 with a bias of 1 (the assertion fails without the lodBias line); test_docs; DOOM 3 from its save into Mars City Hangar on an M6, no red line. Not run: other GL games (only textures with a nonzero bias change), the iPhone.
This commit is contained in:
seedds committed 2026-11-16 16:03:20 +08:00
1 parent 6dad34067c
commit e6a358f5c1
4 files changed
+36 -7

No files matched your search

+1 -1
View File
@@ -21,7 +21,7 @@ No rewriter cut, build-keyed host row or profile switch: everything below is the
- **GL calls it makes** (with `DM_GL_STUBS=1` and `DM_GL_MISSES=1` on the way to Mars City): named uniform blocks (`glGetUniformBlockIndex`, `glUniformBlockBinding`, `glBindBufferRange` of a part of a buffer), two-sided stencil (`glStencilOpSeparate`, `glStencilFuncSeparate`), `glCopyTexImage2D` every frame, `glCompressedTexSubImage2DARB`, `GL_UNPACK_SWAP_BYTES`, `glIsSync`, `glShadeModel`, `glClientActiveTextureARB`, `glPolygonMode(GL_LINE)` once while it puts its state in order. It asks for its compatibility-profile calls (`glBegin`, `glVertex3f`, `glMatrixMode` …) and ARB assembly programs and does not call them here. Each is a general entry under [OpenGL layer](../../../docs/reference/rendering.md#opengl-layer).
- **Resolved: no shadows.** Its shadow volume programs `shadow` and `shadow_skinned` have no fragment shader ([GL programs with a vertex shader alone](../../../docs/reference/rendering.md#gl-programs-with-a-vertex-shader-alone)) and count front and back faces in one draw ([GL two-sided stencil](../../../docs/reference/rendering.md#gl-two-sided-stencil)).
- **Checked, not needed: invariant positions.** Its light passes draw where the depth equals the depth pre-pass; `gl_shaders.py` compiles with `-fpreserve-invariance` but adds no `invariant gl_Position`. Full-resolution shots of the hangar's floor, crates and a skinned marine show no speckling, so no invariance was added (issue #1180 reported misses "in places" without it on its build).
- **Open: `GL_TEXTURE_LOD_BIAS 0.5`** on some textures stays a red line: a Metal sampler has no bias. No difference was seen.
- **Resolved: `GL_TEXTURE_LOD_BIAS 0.5`** on some textures was dropped with a red line, from before Metal samplers had a bias; it is now the sampler's `lodBias`, so those textures sample half a level coarser, as on the Switch ([Smaller GL calls of an id Tech 4 renderer](../../../docs/reference/rendering.md#smaller-gl-calls-of-an-id-tech-4-renderer)).
## Shaders
+5 -4
View File
@@ -302,7 +302,7 @@ typedef struct {
GLenum target; int made, fmt, levels, slevels; uint32_t w, h, d; uint64_t use, gen;
level_t pend[6][MAXLV]; int defined; // defined: bit per level whose face(s) were given
__strong id<MTLTexture> tex; __strong id<MTLSamplerState> smp;
GLint minf, magf, ws, wt, wr, maxlv, cmp, swz[4]; float aniso, border[4]; int smp_dirty;
GLint minf, magf, ws, wt, wr, maxlv, cmp, swz[4]; float aniso, lodbias, border[4]; int smp_dirty; // lodbias: GL_TEXTURE_LOD_BIAS
} tex_t;
NAMES(tex_t, g_texs, g_ntexs, g_captexs)
static tex_t *tex_of(GLuint n) { return n && n < g_ntexs ? g_texs[n] : NULL; }
@@ -592,11 +592,11 @@ static MTLTextureSwizzleChannels tex_swz(const tex_t *t) {
static id<MTLSamplerState> tex_sampler(tex_t *t) {
if (!t->smp_dirty && t->smp) return t->smp;
// sampler states are shared: one per distinct parameter set
typedef struct { GLint minf, magf, ws, wt, wr, lv, cmp; float aniso, b; } skey;
typedef struct { GLint minf, magf, ws, wt, wr, lv, cmp; float aniso, b, bias; } skey;
static struct { skey k; __strong id<MTLSamplerState> s; } cache[256]; static int n;
int mips = t->levels > 1;
skey k = {t->minf, t->magf, t->ws, t->wt, t->target == GL_TEXTURE_CUBE_MAP ? GL_CLAMP_TO_EDGE : t->wr, mips ? (GLint)umin((uint32_t)t->maxlv, (uint32_t)t->levels - 1) : 0, t->cmp, t->aniso,
t->border[3] > 0.5f ? (t->border[0] > 0.5f ? 2.f : 1.f) : 0.f};
t->border[3] > 0.5f ? (t->border[0] > 0.5f ? 2.f : 1.f) : 0.f, fmaxf(-15, fminf(15, t->lodbias))}; // GL clamps the bias to GL_MAX_TEXTURE_LOD_BIAS
for (int i = 0; i < n; i++) if (!memcmp(&cache[i].k, &k, sizeof k)) { t->smp = cache[i].s; t->smp_dirty = 0; return t->smp; }
MTLSamplerDescriptor *d = [MTLSamplerDescriptor new];
d.magFilter = k.magf == GL_NEAREST ? MTLSamplerMinMagFilterNearest : MTLSamplerMinMagFilterLinear;
@@ -608,6 +608,7 @@ static id<MTLSamplerState> tex_sampler(tex_t *t) {
d.sAddressMode = wrap_of(k.ws); d.tAddressMode = wrap_of(k.wt); d.rAddressMode = wrap_of(k.wr);
d.borderColor = k.b == 2.f ? MTLSamplerBorderColorOpaqueWhite : k.b == 1.f ? MTLSamplerBorderColorOpaqueBlack : MTLSamplerBorderColorTransparentBlack;
d.maxAnisotropy = (NSUInteger)fmaxf(1, fminf(16, k.aniso));
d.lodBias = k.bias; // added to the level of detail a sample computes, as GL adds the texture's (Metal 4: macOS and iOS 26, the host's minimum)
if (k.cmp) d.compareFunction = (MTLCompareFunction)(k.cmp - GL_NEVER);
id<MTLSamplerState> s = [g_dev newSamplerStateWithDescriptor:d];
if (n < 256) { cache[n].k = k; cache[n].s = s; n++; } else RED1("more than 256 distinct sampler parameter sets");
@@ -1585,7 +1586,7 @@ static void tex_param(GLenum target, GLenum p, float f, GLint i) {
case GL_TEXTURE_MAX_LEVEL: t->maxlv = i; break; case GL_TEXTURE_MAX_ANISOTROPY: t->aniso = f; break;
case GL_TEXTURE_COMPARE_MODE: t->cmp = i ? (t->cmp ? t->cmp : 0x203) : 0; break; // COMPARE_REF_TO_TEXTURE, the function defaults to LEQUAL
case GL_TEXTURE_COMPARE_FUNC: if (t->cmp) t->cmp = i; break;
case GL_TEXTURE_LOD_BIAS: if (f != 0) RED1("GL_TEXTURE_LOD_BIAS %g: Metal samplers have no bias (the shaders pass theirs)", f); break; // LÖVE sets −(mipmap sharpness), 0 by default
case GL_TEXTURE_LOD_BIAS: t->lodbias = f; break; // LÖVE sets −(mipmap sharpness), 0 by default; DOOM 3 0.5 on some images
case 0x813a: case 0x813b: case 0x813c: break; // MIN_LOD / MAX_LOD / BASE_LEVEL
case GL_TEXTURE_SWIZZLE_R: case GL_TEXTURE_SWIZZLE_R + 1: case GL_TEXTURE_SWIZZLE_R + 2: case GL_TEXTURE_SWIZZLE_A: // fixed once the texture is made (tex_make)
if (t->made && t->swz[p - GL_TEXTURE_SWIZZLE_R] != i) RED1("texture swizzle changed after the texture was made: ignored");
+29 -1
View File
@@ -391,6 +391,34 @@ static void test_doom3(void) {
assert(d[0] == 255 && d[1] == 0 && d[2] == 0 && d[3] == 255 && d[4] == 0 && d[5] == 255 && d[6] == 0 && d[7] == 255);
h_glDeleteTextures(1, &a);
// GL_TEXTURE_LOD_BIAS goes to the texture's sampler: a 4x4 texture drawn 1:1 into a 4x4 target samples level 0 (red), and with a bias of 1 level 1 (green)
static const char *bsrc = "#include <metal_stdlib>\n using namespace metal;\n struct V { float4 p [[position]]; float2 uv; };\n"
"vertex V bv(uint i [[vertex_id]]) { float2 q = float2((i << 1) & 2, i & 2); V o; o.p = float4(q * 2 - 1, 0, 1); o.uv = q; return o; }\n"
"fragment float4 bf(V in [[stage_in]], texture2d<float> t [[texture(0)]], sampler s [[sampler(0)]]) { return t.sample(s, in.uv); }\n";
id<MTLLibrary> blib = [g_dev newLibraryWithSource:@(bsrc) options:nil error:&err]; assert(blib);
MTLRenderPipelineDescriptor *bpd = [MTLRenderPipelineDescriptor new];
bpd.vertexFunction = [blib newFunctionWithName:@"bv"]; bpd.fragmentFunction = [blib newFunctionWithName:@"bf"]; bpd.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA8Unorm;
id<MTLRenderPipelineState> bpso = [g_dev newRenderPipelineStateWithDescriptor:bpd error:&err]; assert(bpso);
uint32_t red[16], green[4]; for (int i = 0; i < 16; i++) red[i] = 0xff0000ffu; for (int i = 0; i < 4; i++) green[i] = 0xff00ff00u;
h_glGenTextures(1, &a); h_glBindTexture(GL_TEXTURE_2D, a);
h_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, red); h_glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, green);
h_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); h_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
ta = tex_of(a); assert(tex_make(ta, 0) && ta->levels == 2);
for (int bias = 0; bias <= 1; bias++) {
h_glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, (float)bias);
MTLTextureDescriptor *td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm width:4 height:4 mipmapped:NO];
td.usage = MTLTextureUsageRenderTarget; td.storageMode = MTLStorageModeShared;
id<MTLTexture> rt = [g_dev newTextureWithDescriptor:td];
MTLRenderPassDescriptor *rp = [MTLRenderPassDescriptor renderPassDescriptor];
rp.colorAttachments[0].texture = rt; rp.colorAttachments[0].loadAction = MTLLoadActionClear; rp.colorAttachments[0].storeAction = MTLStoreActionStore;
id<MTLCommandBuffer> cb = [g_q commandBuffer]; id<MTLRenderCommandEncoder> e = [cb renderCommandEncoderWithDescriptor:rp];
[e setRenderPipelineState:bpso]; [e setFragmentTexture:ta->tex atIndex:0]; [e setFragmentSamplerState:tex_sampler(ta) atIndex:0];
[e drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3]; [e endEncoding]; [cb commit]; [cb waitUntilCompleted];
uint32_t px[16]; [rt getBytes:px bytesPerRow:16 fromRegion:MTLRegionMake2D(0, 0, 4, 4) mipmapLevel:0];
assert(px[5] == (bias ? green[0] : red[0]));
}
h_glDeleteTextures(1, &a);
// Sync queries, wireframe and its polygon offset enable
void *sy = h_glFenceSync(0x9117, 0); assert(h_glIsSync(sy) && !h_glIsSync(NULL)); h_glDeleteSync(sy);
h_glPolygonMode(GL_FRONT_AND_BACK, 0x1b01); assert(S.lines); h_glEnable(0x2a02); assert(S.poffset_line);
@@ -521,6 +549,6 @@ int main(int argc, char **argv) {
assert((h_eglGetProcAddress(missing) != NULL) == stubs);
assert(resolve_import(NULL, missing) == (uint64_t)&unimpl_stubs[0]);
assert(!strcmp(wrap_kind, "STUB"));
printf("GL imports: %u shared entries, shader handle/source, shader and program lifetimes, the error flag, a depth texture's data, vertex formats and bindings, a binding's stride 0 drawn, the uniform buffer target, compressed 2D arrays, DOOM 3's calls (ARB names, extensions, named blocks, two-sided stencil, frame copies, compressed and byte-swapped images, vertex-only programs), wrappers and missing names (DM_GL_STUBS=%d): OK\n", count, stubs);
printf("GL imports: %u shared entries, shader handle/source, shader and program lifetimes, the error flag, a depth texture's data, vertex formats and bindings, a binding's stride 0 drawn, the uniform buffer target, compressed 2D arrays, DOOM 3's calls (ARB names, extensions, named blocks, two-sided stencil, frame copies, compressed and byte-swapped images, vertex-only programs, LOD bias), wrappers and missing names (DM_GL_STUBS=%d): OK\n", count, stubs);
return 0;
}
+1 -1
View File
@@ -2042,7 +2042,7 @@ Digimon Story Cyber Sleuth: Complete Edition (Media.Vision's MVGL engine, SDK 6.
### Smaller GL calls of an id Tech 4 renderer
- `glIsSync` (a sync the layer made and has not deleted), `glShadeModel` (`GL_SMOOTH`; GLSL programs interpolate by their own qualifiers, `GL_FLAT` is a red line), `glClientActiveTexture` (unit 0; no fixed-pipeline arrays), `glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)` (Metal's triangle fill mode, with `GL_POLYGON_OFFSET_LINE` as its offset enable; a masked clear always fills), `GL_MAX_TEXTURE_COORDS` (8), `GL_MAX_UNIFORM_BLOCK_SIZE` (65,536). `GL_TEXTURE_LOD_BIAS` stays a red line: a Metal sampler has no bias.
- `glIsSync` (a sync the layer made and has not deleted), `glShadeModel` (`GL_SMOOTH`; GLSL programs interpolate by their own qualifiers, `GL_FLAT` is a red line), `glClientActiveTexture` (unit 0; no fixed-pipeline arrays), `glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)` (Metal's triangle fill mode, with `GL_POLYGON_OFFSET_LINE` as its offset enable; a masked clear always fills), `GL_MAX_TEXTURE_COORDS` (8), `GL_MAX_UNIFORM_BLOCK_SIZE` (65,536). `GL_TEXTURE_LOD_BIAS` is the texture's sampler's `lodBias` (Metal 4, macOS and iOS 26, the host's minimum), clamped to ±15 as `GL_MAX_TEXTURE_LOD_BIAS` says: the layer used to drop it with a red line, from before Metal had it; `t_gl_imports` draws a two-level texture 1:1 and reads level 1 with a bias of 1.
- **Found on:** [DOOM 3](../../dm/games/010029D00E740000/README.md): it asks for these while it puts its GL state in order and sets a level's sync objects.
### GLSL prefixes in main are no shaders