[MG_Backend, MG_Test] (P7): the wire arm's shader colour blit samples an unscaled blit through its nearest sampler, so a 1:1 GL_LINEAR glBlitFramebuffer into the default framebuffer is an exact copy

This commit is contained in:
2026-09-23 18:59:20 -04:00
parent cfe830cf44
commit 74636c8432
5 changed files with 199 additions and 10 deletions
@@ -52,6 +52,7 @@
#include <MG_Impl/Pipe/SlotAllocator.h>
// P7 wave 2 package B3: rule I's tally for WireDraw.inc's silent draw drops.
#include "WireDeclineTally.h"
#include "WireColorBlitFilter.h"
#include "WireDepthResolveArm.h"
#include "WireDepthResolveProbe.h"
#endif
@@ -340,7 +340,14 @@
alloc.descriptorSetCount = 1;
alloc.pSetLayouts = &cache.descriptorLayout;
VK_VERIFY(vkAllocateDescriptorSets(m_device, &alloc, &descriptorSet));
const VkDescriptorImageInfo sampled{filter == GL_LINEAR ? cache.linear : cache.nearest,
// GL_LINEAR filters only a STRETCHED blit; an unscaled one (mirrored or not, on any of the
// four transforms) is a copy and samples NEAREST, which is exact where linear filtering
// at a quantised coordinate is not (WireColorBlitFilter.h). The format check above still
// asks for SAMPLED_IMAGE_FILTER_LINEAR whenever GL said GL_LINEAR, so which blits reach
// this pass is unchanged.
const VkFilter samplerFilter = WireColorBlitFilterFor({sx0, sy0, sx1, sy1}, {dx0, dy0, dx1, dy1},
filter, transform);
const VkDescriptorImageInfo sampled{samplerFilter == VK_FILTER_LINEAR ? cache.linear : cache.nearest,
sourceView, sourceLayout};
VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
write.dstSet = descriptorSet;
@@ -0,0 +1,66 @@
// MobileGL - MobileGL/MG_Backend/DirectVulkan/Renderer/WireColorBlitFilter.h
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#pragma once
#include <Includes.h>
#include "../VkIncludes.h"
// P7 g3-blit (g3det, gate 3): WHICH SAMPLER THE WIRE ARM'S SHADER COLOUR BLIT READS ITS SOURCE
// THROUGH.
//
// GL applies a blit's `filter` only where the image is STRETCHED (GL 4.6 core 18.3.1). A blit whose
// source and destination rectangles are the same size is a copy, whatever `filter` says, and so is
// a mirrored one. The wire arm's shader blit (WireColorBlit.inc, BlitWireColorImage) draws a quad
// and samples `textureLod(sourceImage, texCoord, 0)` at interpolated coordinates, and it used the
// LINEAR sampler for every GL_LINEAR blit. At 1:1 every fragment centre maps to a texel centre, so
// in exact arithmetic linear filtering reads the same single texel. Adreno 830 quantises the
// coordinate to sub-texel precision: a fragment that lands a fraction off the centre blends in a
// small weight of a neighbour. minecraft-1.21.1-neoforge-create-instancing-in-world ends with a
// 1:1 GL_LINEAR glBlitFramebuffer into FB 0, and on the wire arm 712 high-contrast pixels moved by
// 1 LSB toward a neighbour. The monolith's vkCmdBlitImage copied them exactly
// (~/w7/logs/g3det/VERDICT.md section 3). NEAREST within half a texel of the centre is exact, so an
// unscaled blit takes NEAREST. Lavapipe samples texel centres exactly, so no host picture can see
// the difference; this pure half is what the unit test pins
// (MG_Test/Pipeline/PipelineQuirkTest.cpp, WireColorBlitSamples*).
//
// THE SURFACE TRANSFORM DOES NOT TURN A COPY INTO A STRETCH. BlitWireColorImage applies the
// quarter and half turns in NDC, over a viewport of the native image, and for a quarter turn it
// takes the logical extent as the native extent swapped. A native pixel then has exactly the pitch
// of a logical one, and the rotation maps pixel centres onto pixel centres. So the scale test is on
// GL's own (logical) rectangles for identity and all three rotations. BlitWireColorImage declines
// any other transform before it chooses a sampler; for one, this answers with the filter's own
// mapping, as before this package.
#if MOBILEGL_BUILD_DISAGGREGATED
namespace MobileGL::MG_Backend::DirectVulkan {
// A glBlitFramebuffer rectangle as GL names it: two corners, either order.
struct WireBlitRect {
GLint x0 = 0, y0 = 0, x1 = 0, y1 = 0;
};
// Same extent on both axes, orientation aside. The extents are taken in 64 bits: GL accepts any
// GLint corner, and INT_MAX - INT_MIN does not fit in one.
inline Bool WireBlitIsUnscaled(const WireBlitRect& source, const WireBlitRect& destination) {
const auto extent = [](GLint a, GLint b) {
return std::abs(static_cast<Int64>(b) - static_cast<Int64>(a));
};
return extent(source.x0, source.x1) == extent(destination.x0, destination.x1) &&
extent(source.y0, source.y1) == extent(destination.y0, destination.y1);
}
inline VkFilter WireColorBlitFilterFor(const WireBlitRect& source, const WireBlitRect& destination,
GLenum filter, VkSurfaceTransformFlagBitsKHR transform) {
if (filter != GL_LINEAR) return VK_FILTER_NEAREST;
const Bool expressed = transform == VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR ||
transform == VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR ||
transform == VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR ||
transform == VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
if (expressed && WireBlitIsUnscaled(source, destination)) return VK_FILTER_NEAREST;
return VK_FILTER_LINEAR;
}
} // namespace MobileGL::MG_Backend::DirectVulkan
#endif
@@ -1105,12 +1105,23 @@
// P7 wave 2-B, CONTRACT-P7 §3.2: `default-color-blit-shape@P7` RETIRES, in three
// pieces, because the one Fatal covered three unrelated situations.
//
// The shader blit is here for ROTATION and for nothing else. It can only SAMPLE
// its source - sampler2D, a float result, one of two 2D view types - so an
// RGBA8UI attachment, a 3D slice or an array layer made it return false, and the
// refusal fired even on a surface with NO rotation, where the ordinary
// vkCmdBlitImage arm twenty lines down would have done the blit correctly and
// does handle those shapes.
// The shader blit EXISTS for rotation, because an axis-aligned vkCmdBlitImage cannot
// express a quarter turn. It is still TAKEN FIRST on every transform it can
// express, identity included. That has been the routing since 38919d453: every
// host lane (all identity) and the Redmi pbuffer run their default-framebuffer
// colour blits through it, F1WireScenario.ColorBlitToDefaultHonorsScissorAndReversedRect
// among them. The ordinary vkCmdBlitImage arm below is reached on identity only
// when this pass declines the source. Because identity blits run here, the pass
// must copy exactly wherever GL says the blit is a copy: an unscaled blit samples
// NEAREST whatever `filter` says (WireColorBlitFilter.h, g3-blit; on Adreno 830,
// linear sampling of a 1:1 GL_LINEAR blit moved create-instancing's edge pixels by
// 1 LSB, where the monolith's vkCmdBlitImage is exact).
//
// The pass can only SAMPLE its source - sampler2D, a float result, one of two 2D
// view types - so an RGBA8UI attachment, a 3D slice or an array layer makes it
// return false. That refusal used to fire even on a surface with NO rotation,
// where the vkCmdBlitImage arm below does the blit correctly and does handle
// those shapes.
const auto transform = m_swapchainObject.GetPreTransform();
const Bool knownRotation = transform == VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR ||
transform == VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR ||
@@ -1154,9 +1165,11 @@
static_cast<Uint32>(transform));
return;
}
// Identity: fall through to the ordinary vkCmdBlitImage arm below. It already
// maps GL's bottom row onto the native image's last row for a default target,
// and vkCmdBlitImage takes integer formats and array layers natively. Its 3D
// Identity, and the shader pass above declined this source (an integer format, a
// 3D slice, a view type it cannot sample): fall through to the ordinary
// vkCmdBlitImage arm below. It already maps GL's bottom row onto the native
// image's last row for a default target, and vkCmdBlitImage takes integer
// formats and array layers natively. Its 3D
// addressing was wrong and is fixed with this package - see the srcBaseLayer /
// srcZ note down there; the bug was invisible until now because a 3D source hit
// the Fatal above and never reached that arm.