[Feat] (DirectGLES, MG_Util): normalize the coordinates of a rectangle lookup

A rectangle texture is emulated on an ES 2D texture, and LowerRectImagesForEssl
rewrites the image type in the SPIR-V to match. That is exact only where the lookup
addresses texels directly, which is why the pass declined any module containing a
lookup that takes normalized coordinates - the whole KHR-GL40.texture_gather 2drect
set among them.

The missing half is one divide: a rectangle lookup's coordinate is in texels and the
2D lookup it becomes wants [0,1], so the coordinate has to be divided by the texture's
size. It goes in on the ESSL the transpiler produces, next to the LOD-bias emulation
that already rewrites lookup arguments there, and reads the size back with
textureSize() rather than plumbing a uniform down - the emulated texture is a real ES
2D texture, so the shader can ask it directly.

Only the forms whose argument 1 is the bare coordinate are rewritten - texture,
textureOffset and the three textureGather flavours, which covers the Dref gathers too
because those carry the compare value in a separate argument. texelFetch is
deliberately left alone: its coordinates are integer texels on both targets. The
SPIR-V pass keeps declining everything else, so a projective lookup or a Dref sample
(where the compare value rides in coord.z) still refuses the module instead of
producing something subtly wrong.

Which samplers were declared rectangle is no longer visible in the transpiled source -
they are plain sampler2D by then - so the names come from the frontend program's
reflection.
This commit is contained in:
BZLZHH
2026-08-04 10:38:01 -04:00
parent f38dbf018d
commit 5437947240
4 changed files with 110 additions and 10 deletions
+9
View File
@@ -129,6 +129,15 @@ namespace MobileGL::MG_Backend::DirectGLES {
// all have a zero bias is therefore unaffected. Returns the source unchanged when
// there is nothing to rewrite.
String EmulateTextureLodBias(const String& glslCode);
// GL_TEXTURE_RECTANGLE is emulated on an ES 2D texture and LowerRectImagesForEssl
// rewrites the image type to match, but a rectangle lookup addresses texels
// directly while a 2D one addresses [0,1] - so every lookup that takes normalized
// coordinates has to divide by the texture's size. `rectSamplerNames` is the set of
// samplers the program declared as rectangle; texelFetch is left alone (its
// coordinates are unnormalized on both targets) and so is anything projective,
// which LowerRectImagesForEssl still declines outright.
String NormalizeRectSamplerCoordinates(const String& glslCode,
const Vector<String>& rectSamplerNames);
} // namespace PrgramImpl
namespace Utils {