[Merge] (P11): dev@c14283ee - scattered sub-image writes upload exactly their texels, Magma submits a frame's recording every 16384 draws with backpressure, and Magma grows descriptor pools geometrically and trims a quiet slot whole; the handle arm notes driver-side writes by handle and the wire arm splits recordings and keeps the trim's epoch peak across descriptor rewinds

This commit is contained in:
2026-09-29 12:11:37 -04:00
30 changed files with 2417 additions and 95 deletions
@@ -158,6 +158,8 @@ add_executable(MobileGLIntegrationTest
Scenarios/ClearTexImageUndefinedLevelZeroScenario.cpp
Scenarios/RenderbufferBlendFormatScenario.cpp
Scenarios/DualSourceBlendScenario.cpp
Scenarios/OversizedRecordingSplitScenario.cpp
Scenarios/DescriptorPoolGrowthScenario.cpp
Scenarios/PipeVerifyArmingScenario.cpp
Scenarios/PoisonOmissionScenario.cpp
Scenarios/HandleRecycleScenario.cpp
@@ -753,6 +755,12 @@ mgl_itest_join_environment(MGL_ITEST_VULKAN_PRIMGEN_REROUTE_ENVIRONMENT
"MOBILEGL_BACKEND_TYPE=DirectVulkan" "MOBILEGL_MAGMA_PRIMGEN_QUERY_REROUTE=1"
"MOBILEGL_LOG_FILE_PATH=${CMAKE_CURRENT_BINARY_DIR}/primgen-query-reroute.log"
${MGL_ITEST_VULKAN_ENV})
# The command-buffer draw budget pinned far below one frame's draws, with the log path for the
# split's latched MGLOG_I - the same arming reasoning as the two entries above.
mgl_itest_join_environment(MGL_ITEST_VULKAN_SPLIT_RECORDING_ENVIRONMENT
"MOBILEGL_BACKEND_TYPE=DirectVulkan" "MOBILEGL_MAGMA_MAX_DRAWS_PER_COMMAND_BUFFER=64"
"MOBILEGL_LOG_FILE_PATH=${CMAKE_CURRENT_BINARY_DIR}/oversized-recording-split.log"
${MGL_ITEST_VULKAN_ENV})
# The point-size demotion pinned on, per backend, with a per-lane log file for the arming
# assertion - the same MOBILEGL_LOG_FILE_PATH reasoning as the UnlocatedIoBlocks lane above.
# Two lanes because the demotion runs in the SHARED phase-B chain and each backend then
@@ -1052,6 +1060,24 @@ gtest_discover_tests(MobileGLIntegrationTest
ENVIRONMENT "${MGL_ITEST_VULKAN_PRIMGEN_REROUTE_ENVIRONMENT}"
)
# OversizedRecordingSplitScenario with the command-buffer draw budget PINNED to 64. The ambient
# registrations run its cases under the default budget, which never splits them; this entry
# splits every case at least once between the draws that leave state (depth, stencil, a blend
# destination, multisample samples, an open query, an active capture) and the draws that
# consume it, so the results prove a split carries all of it, and the arming assertion (the
# renderer's latched MGLOG_I) proves the split happened at all - on a renderer that records the
# whole frame into one command buffer, it is the assertion that goes red. DirectVulkan only:
# the budget steers nothing on DirectGLES, whose driver flushes on its own.
gtest_discover_tests(MobileGLIntegrationTest
TEST_PREFIX "DirectVulkan.SplitRecording."
TEST_FILTER "OversizedRecordingSplitScenario.*"
DISCOVERY_TIMEOUT 30
PROPERTIES
LABELS integration-gpu
TIMEOUT ${MGL_ITEST_TIMEOUT}
ENVIRONMENT "${MGL_ITEST_VULKAN_SPLIT_RECORDING_ENVIRONMENT}"
)
# The packed16 copy scenarios again, with the 8-bit storage widening PINNED ON. The ambient
# registrations above cover the narrow storage - on every CI driver the widening's POST
# probe finds no field-order mirror, so Auto keeps the native 16-bit path - which means the
@@ -0,0 +1,346 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/DescriptorPoolGrowthScenario.cpp
// 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
//
// Scenario - ONE FRAME THAT NEEDS THOUSANDS OF DISTINCT DESCRIPTOR SETS.
//
// DirectVulkan takes a draw's descriptor set from a per-frame-slot cache and adds a pool to the
// slot when the cache runs dry (UniformManager). Two things went wrong with a burst like
// Minecraft rd12's loading frame (15,016 sets in one slot):
// - Growth was linear. The next pool was sized at twice the slot's FIRST pool of the flavour,
// which is always its oldest and smallest, so every grown pool came out the same size: the
// loading slot ended with 59 pools of 256 sets.
// - The burst was kept forever. The cache only rewinds, so the slot held all 15k sets (and the
// pools behind them) for the life of the process, while steady frames used a few dozen.
// Now each pool doubles the slot's largest one (capped), and a slot whose pools stay under a
// quarter full for MOBILEGL_MAGMA_DESCRIPTOR_TRIM_FRAMES frames is handed back whole and re-grows.
//
// Each of kDraws draws paints its own pixel of a 64x64 target and samples one of kTextures
// textures, a different one from each of the previous draws' so neither the renderer's per-draw
// descriptor reuse memo (four entries) nor its dynamic-offset-only rebind can let two draws share
// a set: one frame needs kDraws sets. Every pixel must hold its own draw's texture colour, on both
// backends. On DirectVulkan the cases also read the pools through GetDescriptorPoolCensus - which
// only the desktop build can reach, since the integration harness links the static library there
// and the hidden-visibility shipping library on Android.
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <sstream>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/ScenarioFixture.h"
#if !defined(__ANDROID__)
#include <MG_Backend/DirectVulkan/DescriptorPoolCensus.h>
#endif
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
constexpr int kSide = 64;
constexpr int kDraws = kSide * kSide; // well under the 16384-draw command-buffer split budget
// More than the descriptor reuse memo's four entries, so the texture a draw samples is never
// one of the last four draws'.
constexpr int kTextures = 8;
// The base pool size (VulkanRenderer's kDescriptorSetsPerFrame) and the growth cap
// (UniformManager::kMaxDescriptorPoolSets).
constexpr std::uint64_t kBaseSets = 64;
constexpr std::uint64_t kMaxPoolSets = 4096;
constexpr const char* kVS = R"(#version 330 core
in vec2 aPos;
uniform int uIndex;
void main() {
vec2 cell = vec2(float(uIndex % 64), float(uIndex / 64));
gl_Position = vec4((cell + aPos) / 32.0 - 1.0, 0.0, 1.0);
}
)";
constexpr const char* kFS = R"(#version 330 core
uniform sampler2D uTex;
out vec4 o_color;
void main() { o_color = texture(uTex, vec2(0.5)); }
)";
// Texture t's single texel; every channel differs between any two textures.
Rgba8 TextureColor(int t) {
return Rgba8{static_cast<std::uint8_t>(16 + 32 * t), static_cast<std::uint8_t>(240 - 32 * t),
static_cast<std::uint8_t>(8 + 29 * t), 255};
}
std::string Describe(const Rgba8& c) {
std::ostringstream os;
os << "rgba(" << static_cast<int>(c.r) << ", " << static_cast<int>(c.g) << ", " << static_cast<int>(c.b)
<< ", " << static_cast<int>(c.a) << ")";
return os.str();
}
unsigned long EnvUnsigned(const char* name, unsigned long fallback) {
const char* value = std::getenv(name);
if (value == nullptr || *value == '\0') return fallback;
char* end = nullptr;
const unsigned long parsed = std::strtoul(value, &end, 10);
return (end != nullptr && *end == '\0') ? parsed : fallback;
}
// Pools a geometric policy adds for n sets from an empty flavour: 64, 128, ... up to the cap,
// then one per cap. The old first-pool rule needed n/128 or n/256 of them.
std::uint64_t GeometricPoolBound(std::uint64_t n) {
std::uint64_t pools = 0, capacity = 0, size = kBaseSets;
while (capacity < n) {
capacity += size;
++pools;
size = std::min(size * 2, kMaxPoolSets);
}
return pools;
}
struct Census {
bool available = false;
std::uint64_t pools = 0;
std::uint64_t capacitySets = 0;
std::uint64_t cachedSets = 0;
std::uint64_t slotTrims = 0;
};
Census ReadCensus() {
Census out;
#if !defined(__ANDROID__)
const auto census = MobileGL::MG_Backend::DirectVulkan::GetDescriptorPoolCensus();
out.available = census.available;
out.pools = census.pools;
out.capacitySets = census.capacitySets;
out.cachedSets = census.cachedSets;
out.slotTrims = census.slotTrims;
#endif
return out;
}
std::string Describe(const Census& c) {
std::ostringstream os;
os << "pools=" << c.pools << " capacity=" << c.capacitySets << " cachedSets=" << c.cachedSets
<< " slotTrims=" << c.slotTrims;
return os.str();
}
::testing::AssertionResult EveryPixelHoldsItsDrawsTexture(const Image& image, const char* when) {
int wrong = 0;
int firstX = -1, firstY = -1;
Rgba8 firstSeen{};
for (int y = 0; y < kSide; ++y) {
for (int x = 0; x < kSide; ++x) {
const Rgba8 expected = TextureColor((y * kSide + x) % kTextures);
const Rgba8 seen = image.At(x, y);
if (seen != expected) {
if (wrong == 0) {
firstX = x;
firstY = y;
firstSeen = seen;
}
++wrong;
}
}
}
if (wrong == 0) return ::testing::AssertionSuccess();
const int draw = firstY * kSide + firstX;
return ::testing::AssertionFailure()
<< when << ": " << wrong << " of " << kDraws << " pixels do not hold their own draw's texture. "
<< "First: (" << firstX << ", " << firstY << ") = " << Describe(firstSeen) << ", drawn by draw "
<< draw << " with texture " << (draw % kTextures) << " = "
<< Describe(TextureColor(draw % kTextures)) << " (a pixel no draw reached holds 0,0,0,0)";
}
class DescriptorPoolGrowthScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
static const float kVertices[] = {0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f};
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(kVertices), kVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), nullptr);
m_textures.resize(kTextures);
glGenTextures(kTextures, m_textures.data());
for (int t = 0; t < kTextures; ++t) {
const Rgba8 c = TextureColor(t);
const std::uint8_t texel[4] = {c.r, c.g, c.b, c.a};
glBindTexture(GL_TEXTURE_2D, m_textures[t]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texel);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
glBindTexture(GL_TEXTURE_2D, 0);
}
void TearDown() override {
if (Ready()) {
glUseProgram(0);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
BindDefaultFramebuffer();
if (m_program != 0) glDeleteProgram(m_program);
if (!m_textures.empty()) glDeleteTextures(static_cast<GLsizei>(m_textures.size()), m_textures.data());
if (m_vbo != 0) glDeleteBuffers(1, &m_vbo);
if (m_vao != 0) glDeleteVertexArrays(1, &m_vao);
DestroyColorFbo(m_target);
}
ScenarioTest::TearDown();
}
::testing::AssertionResult Prepare() {
std::string error;
m_program = CompileProgram(kVS, kFS, &error);
if (m_program == 0) return ::testing::AssertionFailure() << error;
m_indexLocation = glGetUniformLocation(m_program, "uIndex");
m_target = MakeColorFbo(kSide, kSide);
if (m_target.fbo == 0) return ::testing::AssertionFailure() << "MakeColorFbo failed";
glUseProgram(m_program);
glUniform1i(glGetUniformLocation(m_program, "uTex"), 0);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(m_vao);
BindFbo(m_target);
glDisable(GL_SCISSOR_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
return ::testing::AssertionSuccess();
}
// Draw i paints pixel (i % 64, i / 64) with texture i % kTextures.
void Draw(int i) {
glBindTexture(GL_TEXTURE_2D, m_textures[i % kTextures]);
glUniform1i(m_indexLocation, i);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
// The burst: every pixel, one draw and one descriptor set each.
void DrawBurst() {
ClearTo(0.0f, 0.0f, 0.0f, 0.0f);
for (int i = 0; i < kDraws; ++i) Draw(i);
}
static bool IsVulkan() { return Gl().BackendName() == std::string("DirectVulkan"); }
GLuint m_vao = 0;
GLuint m_vbo = 0;
GLuint m_program = 0;
GLint m_indexLocation = -1;
std::vector<GLuint> m_textures;
ColorFbo m_target;
};
} // namespace
// One frame, kDraws distinct sets. Every pixel lands, and the frame slot grows O(log N) pools
// for it, not N / 128.
TEST_F(DescriptorPoolGrowthScenario, AFrameOfDistinctSetsLandsWholeAndGrowsItsPoolsGeometrically) {
if (!Ready()) return;
ASSERT_TRUE(Prepare());
const Census before = ReadCensus();
DrawBurst();
const Image image = ReadPixels(kSide, kSide);
const Census after = ReadCensus();
EXPECT_TRUE(EveryPixelHoldsItsDrawsTexture(image, "one frame of distinct descriptor sets"));
EXPECT_EQ(FirstGLError(), 0u);
RecordProperty("census_checked", (IsVulkan() && after.available) ? "yes" : "no");
if (!IsVulkan() || !after.available) return;
// Falsifiability first: had the renderer's memos collapsed the draws onto shared sets, the
// slot would never have needed to grow and the bound below would pass on any policy.
ASSERT_GE(after.cachedSets, before.cachedSets + kDraws)
<< "the frame did not allocate one descriptor set per draw, so it does not exercise growth. Before: "
<< Describe(before) << "; after: " << Describe(after);
const std::uint64_t added = after.pools - before.pools;
RecordProperty("census_before", Describe(before));
RecordProperty("census_after", Describe(after));
EXPECT_LE(added, GeometricPoolBound(kDraws + 1))
<< "a frame needing " << kDraws << " descriptor sets added " << added << " pools; doubling the slot's "
<< "largest pool needs at most " << GeometricPoolBound(kDraws + 1) << ". Before: " << Describe(before)
<< "; after: " << Describe(after);
}
// Burst, then quiet frames until the renderer hands the burst back, then the burst again while
// the quiet frames are still in flight. Nothing waits between frames except where noted, so the
// trim runs with other frame slots' work outstanding.
TEST_F(DescriptorPoolGrowthScenario, AGivenBackBurstLeavesNoStaleSetBehind) {
if (!Ready()) return;
ASSERT_TRUE(Prepare());
const unsigned long trimFrames = EnvUnsigned("MOBILEGL_MAGMA_DESCRIPTOR_TRIM_FRAMES", 120);
const unsigned long framesInFlight = EnvUnsigned("MOBILEGL_MAGMA_FRAMESINFLIGHT", 3);
// The trim fires once trimFrames quiet frames have followed the burst, at the burst slot's next
// turn; past this many frames it is not coming.
const unsigned long quietFrameLimit = trimFrames + 2 * framesInFlight + 16;
DrawBurst();
Gl().EndFrame();
const Census afterBurst = ReadCensus();
// Quiet frames: one draw each, the same pixel and texture the burst gave draw 0.
const bool observe = IsVulkan() && afterBurst.available && trimFrames != 0;
unsigned long quietFrames = 0;
Census afterQuiet = afterBurst;
for (; quietFrames < quietFrameLimit; ++quietFrames) {
Draw(0);
Gl().EndFrame();
if (observe) {
afterQuiet = ReadCensus();
if (afterQuiet.slotTrims > afterBurst.slotTrims) {
++quietFrames;
break;
}
}
}
// The burst again, straight away. The slot it lands in re-grows from its base pool.
DrawBurst();
const Image image = ReadPixels(kSide, kSide);
const Census afterSecondBurst = ReadCensus();
EXPECT_TRUE(EveryPixelHoldsItsDrawsTexture(image, "the burst drawn after the first one was given back"));
EXPECT_EQ(FirstGLError(), 0u);
RecordProperty("census_checked", observe ? "yes" : "no");
if (!observe) return;
RecordProperty("census_after_burst", Describe(afterBurst));
RecordProperty("quiet_frames", std::to_string(quietFrames));
RecordProperty("census_after_quiet", Describe(afterQuiet));
RecordProperty("census_after_second_burst", Describe(afterSecondBurst));
ASSERT_GE(afterBurst.cachedSets, static_cast<std::uint64_t>(kDraws))
<< "the burst did not allocate one descriptor set per draw: " << Describe(afterBurst);
EXPECT_GT(afterQuiet.slotTrims, afterBurst.slotTrims)
<< quietFrames << " frames of one draw each followed a " << kDraws << "-set frame, but no frame slot was "
<< "trimmed (MOBILEGL_MAGMA_DESCRIPTOR_TRIM_FRAMES=" << trimFrames << "). After the burst: "
<< Describe(afterBurst) << "; after the quiet frames: " << Describe(afterQuiet);
EXPECT_LE(afterQuiet.cachedSets + kDraws / 2, afterBurst.cachedSets)
<< "the trim did not give the burst's sets back. After the burst: " << Describe(afterBurst)
<< "; after the quiet frames: " << Describe(afterQuiet);
EXPECT_LT(afterQuiet.pools, afterBurst.pools)
<< "the trim did not give the burst's pools back. After the burst: " << Describe(afterBurst)
<< "; after the quiet frames: " << Describe(afterQuiet);
EXPECT_GE(afterSecondBurst.cachedSets, afterQuiet.cachedSets + kDraws)
<< "the second burst was not served by freshly allocated sets: " << Describe(afterSecondBurst);
}
} // namespace MGITest
@@ -29,6 +29,7 @@
// the last sync, and a pending client write that has to land on top of GPU content. Magma never
// re-mints from a shadow (it copies image to image) and is the control backend.
#include <array>
#include <cstdint>
#include <cstdlib>
#include <sstream>
@@ -248,6 +249,35 @@ void main() { o_color = texelFetch(u_tex, ivec2(gl_FragCoord.xy) % 4, 0); }
EXPECT_EQ(bad, 0) << bad << " of " << edge * edge << " texels differ; first " << first.str();
}
// One glTexSubImage of opaque blue per {x, y, w, h} box, left pending.
static void WriteBlueBoxes(GLuint tex, const std::vector<std::array<int, 4>>& boxes) {
glBindTexture(GL_TEXTURE_2D, tex);
for (const auto& b : boxes) {
std::vector<GLubyte> blue(static_cast<std::size_t>(b[2] * b[3]) * 4u, 0);
for (std::size_t i = 0; i < blue.size(); i += 4) {
blue[i + 2] = 255;
blue[i + 3] = 255;
}
glTexSubImage2D(GL_TEXTURE_2D, 0, b[0], b[1], b[2], b[3], GL_RGBA, GL_UNSIGNED_BYTE, blue.data());
}
glBindTexture(GL_TEXTURE_2D, 0);
}
// kEdge x kEdge green, blue inside the boxes.
static std::vector<GLubyte> GreenWithBlueBoxes(const std::vector<std::array<int, 4>>& boxes) {
auto want = Solid(kEdge, 0, 255, 0, 255);
for (const auto& b : boxes)
for (int y = b[1]; y < b[1] + b[3]; ++y)
for (int x = b[0]; x < b[0] + b[2]; ++x) {
const std::size_t at = static_cast<std::size_t>((y * kEdge + x) * 4);
want[at] = 0;
want[at + 1] = 0;
want[at + 2] = 255;
want[at + 3] = 255;
}
return want;
}
GLuint m_sampleProgram = 0, m_vao = 0, m_fbo = 0;
ColorFbo m_scratch{};
std::vector<GLuint> m_textures;
@@ -334,6 +364,43 @@ void main() { o_color = texelFetch(u_tex, ivec2(gl_FragCoord.xy) % 4, 0); }
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, want);
}
// Several pending writes: only their texels land on the GPU's clear, never the gaps between
// them. The re-mint merged the union box of the shadow over the readback whenever the rect
// list was withheld, and kept the shadow outright when that box spanned the level.
// Two 3x7 columns with a one-texel gap: 42 of their union's 49 texels, past the 3/4 at which
// the storage withholds its rect list.
TEST_F(ImageBindableRemintScenario, PendingSubImagesFillingThreeQuartersOfTheirUnionLandOnTheGpuClear) {
if (!Ready()) return;
const std::vector<std::array<int, 4>> boxes = {{0, 0, 3, 7}, {4, 0, 3, 7}};
const GLuint t = NewTexture();
const auto p = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, p.data());
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
GpuClear(t, 0, kGreen);
SyncBySampling(t);
WriteBlueBoxes(t, boxes);
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, GreenWithBlueBoxes(boxes));
}
// Three quadrants: the union box is the whole level; the fourth quadrant is the GPU's.
TEST_F(ImageBindableRemintScenario, PendingSubImagesWhoseUnionSpansTheLevelLandOnTheGpuClear) {
if (!Ready()) return;
const std::vector<std::array<int, 4>> boxes = {{0, 0, 4, 4}, {4, 0, 4, 4}, {0, 4, 4, 4}};
const GLuint t = NewTexture();
const auto p = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, p.data());
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
GpuClear(t, 0, kGreen);
SyncBySampling(t);
WriteBlueBoxes(t, boxes);
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, GreenWithBlueBoxes(boxes));
}
TEST_F(ImageBindableRemintScenario, GeneratedMipLevelSurvives) {
if (!Ready()) return;
const GLuint t = NewTexture();
@@ -0,0 +1,772 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/OversizedRecordingSplitScenario.cpp
// 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
//
// Scenario - A FRAME WITH MORE DRAWS THAN ONE COMMAND BUFFER MAY HOLD IS SPLIT, AND LOSES NOTHING.
//
// DirectVulkan records a frame into one command buffer, and used to submit it only at Present.
// A driver keeps a command buffer's GPU memory until the buffer is freed, and the Adreno 830
// driver maps that memory into the process in 16 KiB chunks. Minecraft rd12's loading frame
// records 981,654 draws (3.1M commands) into one buffer. It grew to ~40k such mappings and
// aborted inside the driver's own calloc once vm.max_map_count (65530) was spent. The renderer
// now submits the open buffer after MOBILEGL_MAGMA_MAX_DRAWS_PER_COMMAND_BUFFER draws and goes
// on recording into a fresh one (VulkanRenderer::SplitOversizedRecording).
//
// The first case writes each pixel of a 32x32 target with its own draw and its own uniform
// value, so it pins that no draw is lost and no per-draw state is left behind on either side of
// a split. The rest put GL state that one draw leaves for a later one ACROSS the split: a depth
// occluder, a stencil mark, a blend destination, the samples of a multisample target, an
// occlusion or primitives-generated query that is still open, and a transform-feedback capture
// that is still active. Each draws its "before" part, then kFillers draws, then its "after"
// part. kFillers is more than twice the SplitRecording lane's budget of 64, so at least one
// split lands between the two parts wherever the recording's count stood.
//
// The ambient registrations run the same draws unsplit, because the default budget is far above
// any of these frames; that is the control. The DirectVulkan.SplitRecording. entry pins the
// budget to 64, and there every case also asserts the renderer's latched MGLOG_I - the one
// assertion that is red on a renderer that never splits.
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/ScenarioFixture.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
constexpr int kSide = 32;
constexpr int kDraws = kSide * kSide;
// More than twice the SplitRecording lane's budget (64): wherever the count stands when
// they start, at least one split lands among them.
constexpr int kFillers = 150;
// Draws inside the query and capture spans: also more than twice the budget.
constexpr int kSpanDraws = 200;
// aPos spans the unit square; the uniform moves it onto cell (i % 32, i / 32) of a
// 32x32 grid, which is exactly one pixel of a 32x32 target.
constexpr const char* kVS = R"(#version 330 core
in vec2 aPos;
uniform int uIndex;
void main() {
vec2 cell = vec2(float(uIndex % 32), float(uIndex / 32));
gl_Position = vec4((cell + aPos) / 16.0 - 1.0, 0.0, 1.0);
}
)";
// Multiples of 8/255 survive an RGBA8 round trip exactly, so the readback bytes are
// (8x, 8y, 255, 255) for the pixel at (x, y) and nothing else can produce them.
constexpr const char* kFS = R"(#version 330 core
uniform int uIndex;
out vec4 o_color;
void main() {
o_color = vec4(float(uIndex % 32) * 8.0 / 255.0, float(uIndex / 32) * 8.0 / 255.0, 1.0, 1.0);
}
)";
// The state cases: the unit-square vertices (or a triangle of them) stretched over an
// NDC rectangle at a given depth, in a uniform colour. A zero rectangle makes a
// zero-area primitive - a draw that reaches the renderer and rasterizes nothing.
constexpr const char* kRectVS = R"(#version 330 core
in vec2 aPos;
uniform vec4 uRect;
uniform float uDepth;
void main() { gl_Position = vec4(mix(uRect.xy, uRect.zw, aPos), uDepth, 1.0); }
)";
constexpr const char* kRectFS = R"(#version 330 core
uniform vec4 uColor;
out vec4 o_color;
void main() { o_color = uColor; }
)";
// The capture case: one point per draw, carrying 2 * uIndex + 1.
constexpr const char* kCaptureVS = R"(#version 330 core
uniform int uIndex;
out float vs_value;
void main() {
vs_value = float(uIndex) * 2.0 + 1.0;
gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
}
)";
constexpr const char* kCaptureFS = R"(#version 330 core
out vec4 o_color;
void main() { o_color = vec4(1.0); }
)";
constexpr float kCapturePoison = -12345.0f;
// The budget the process was launched with, or 0 when it is unset or not a number.
unsigned long PinnedDrawBudget() {
const char* value = std::getenv("MOBILEGL_MAGMA_MAX_DRAWS_PER_COMMAND_BUFFER");
if (value == nullptr || *value == '\0') return 0;
char* end = nullptr;
const unsigned long parsed = std::strtoul(value, &end, 10);
return (end != nullptr && *end == '\0') ? parsed : 0;
}
// The library log, for the arming assertion. Same machinery and reasoning as
// UnlocatedIoBlockScenario: MOBILEGL_LOG_FILE_PATH is read at log-init, every process in
// the lane appends to the file, and only bytes appended after the snapshot count.
std::filesystem::path LibraryLogPath() {
const char* path = std::getenv("MOBILEGL_LOG_FILE_PATH");
return (path != nullptr && *path != '\0') ? std::filesystem::path(path) : std::filesystem::path();
}
std::uintmax_t LibraryLogSize() {
std::error_code ec;
const std::filesystem::path path = LibraryLogPath();
if (path.empty()) return 0;
const std::uintmax_t size = std::filesystem::file_size(path, ec);
return ec ? 0 : size;
}
std::string LibraryLogSince(std::uintmax_t offset) {
const std::filesystem::path path = LibraryLogPath();
if (path.empty()) return {};
std::ifstream file(path, std::ios::binary);
if (!file.good()) return {};
file.seekg(static_cast<std::streamoff>(offset));
return std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
}
std::string Describe(const Rgba8& c) {
std::ostringstream os;
os << "rgba(" << static_cast<int>(c.r) << ", " << static_cast<int>(c.g) << ", " << static_cast<int>(c.b)
<< ", " << static_cast<int>(c.a) << ")";
return os.str();
}
::testing::AssertionResult EveryPixelHoldsItsOwnDraw(const Image& image) {
int wrong = 0;
int firstX = -1, firstY = -1;
Rgba8 firstSeen{};
for (int y = 0; y < kSide; ++y) {
for (int x = 0; x < kSide; ++x) {
const Rgba8 expected{static_cast<std::uint8_t>(8 * x), static_cast<std::uint8_t>(8 * y), 255, 255};
const Rgba8 seen = image.At(x, y);
if (seen != expected) {
if (wrong == 0) {
firstX = x;
firstY = y;
firstSeen = seen;
}
++wrong;
}
}
}
if (wrong == 0) {
return ::testing::AssertionSuccess();
}
return ::testing::AssertionFailure()
<< wrong << " of " << kDraws << " pixels do not hold their own draw's colour. First: (" << firstX
<< ", " << firstY << ") = " << firstSeen << ", drawn by draw " << (firstY * kSide + firstX)
<< " (a pixel no draw reached still holds the clear colour 0,0,0,0)";
}
// Every pixel of columns [x0, x1] (all rows) is `expected`, each channel within `tolerance`.
::testing::AssertionResult ColumnsAre(const Image& image, int x0, int x1, const Rgba8& expected,
int tolerance, const char* what) {
int wrong = 0;
int firstX = -1, firstY = -1;
Rgba8 firstSeen{};
for (int y = 0; y < kSide; ++y) {
for (int x = x0; x <= x1; ++x) {
const Rgba8 seen = image.At(x, y);
const bool close = std::abs(seen.r - expected.r) <= tolerance &&
std::abs(seen.g - expected.g) <= tolerance &&
std::abs(seen.b - expected.b) <= tolerance &&
std::abs(seen.a - expected.a) <= tolerance;
if (!close) {
if (wrong == 0) {
firstX = x;
firstY = y;
firstSeen = seen;
}
++wrong;
}
}
}
if (wrong == 0) return ::testing::AssertionSuccess();
return ::testing::AssertionFailure()
<< what << ": " << wrong << " pixels of columns " << x0 << ".." << x1 << " are not "
<< Describe(expected) << "; first (" << firstX << ", " << firstY << ") = " << Describe(firstSeen);
}
class OversizedRecordingSplitScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
// Taken before the case draws anything, so the line the arming check looks for
// can only have been written by this case. The renderer latches it at its first
// split, and ctest gives every case a process of its own.
m_logBefore = LibraryLogSize();
// 0..3: the unit square as a strip. 4..6: its lower-left triangle. 7..9: its
// upper-right triangle - the two share the diagonal, so together they cover every
// sample of the square exactly once.
static const float kVertices[] = {0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f};
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(kVertices), kVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), nullptr);
}
void TearDown() override {
if (Ready()) {
glDisable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST);
glDisable(GL_BLEND);
glDisable(GL_RASTERIZER_DISCARD);
glDepthMask(GL_TRUE);
glStencilMask(0xFF);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glUseProgram(0);
glBindVertexArray(0);
BindDefaultFramebuffer();
if (m_rectProgram != 0) glDeleteProgram(m_rectProgram);
if (m_vbo != 0) glDeleteBuffers(1, &m_vbo);
if (m_vao != 0) glDeleteVertexArrays(1, &m_vao);
for (GLuint rbo : m_renderbuffers) glDeleteRenderbuffers(1, &rbo);
for (GLuint fbo : m_framebuffers) glDeleteFramebuffers(1, &fbo);
for (ColorFbo& target : m_colorTargets) DestroyColorFbo(target);
}
ScenarioTest::TearDown();
}
// In the SplitRecording lane (budget pinned below what every case draws, DirectVulkan,
// a log to read), a split must have happened during this case. Elsewhere this case is
// the unsplit control and there is nothing to arm.
void ExpectTheRecordingWasSplitWhereTheBudgetIsPinned() {
const unsigned long budget = PinnedDrawBudget();
const bool splitExpected = budget > 0 && budget * 2 < static_cast<unsigned long>(kFillers) &&
Gl().BackendName() == std::string("DirectVulkan") &&
!LibraryLogPath().empty();
RecordProperty("arming_checked", splitExpected ? "yes" : "no");
if (!splitExpected) return;
const std::string appended = LibraryLogSince(m_logBefore);
EXPECT_NE(appended.find("draws into one command buffer; submitting it"), std::string::npos)
<< "MOBILEGL_MAGMA_MAX_DRAWS_PER_COMMAND_BUFFER=" << budget << " and this case recorded more "
<< "than " << kFillers << " draws in one frame, but DirectVulkan never reported splitting the "
"recording. A renderer that does not split grows a single command buffer without bound "
"(rd12 on Adreno: ~40k driver mappings, then SIGABRT). See "
"VulkanRenderer::SplitOversizedRecording. Log appended by this case:\n"
<< appended;
}
bool BuildRectProgram(std::string* error) {
m_rectProgram = CompileProgram(kRectVS, kRectFS, error);
if (m_rectProgram == 0) return false;
m_rectLoc = glGetUniformLocation(m_rectProgram, "uRect");
m_depthLoc = glGetUniformLocation(m_rectProgram, "uDepth");
m_colorLoc = glGetUniformLocation(m_rectProgram, "uColor");
glUseProgram(m_rectProgram);
glBindVertexArray(m_vao);
return true;
}
// The rectangle [x0, x1] x [y0, y1] in NDC; `first`/`count` pick the strip or a triangle.
void DrawRect(float x0, float y0, float x1, float y1, float depth, float r, float g, float b, float a,
GLenum mode = GL_TRIANGLE_STRIP, GLint first = 0, GLsizei count = 4) {
glUniform4f(m_rectLoc, x0, y0, x1, y1);
glUniform1f(m_depthLoc, depth);
glUniform4f(m_colorLoc, r, g, b, a);
glDrawArrays(mode, first, count);
}
// One pixel of the 32x32 grid, by index.
void DrawPixel(int index, float r, float g, float b, float a) {
const float x = static_cast<float>(index % kSide);
const float y = static_cast<float>(index / kSide);
DrawRect(x / 16.0f - 1.0f, y / 16.0f - 1.0f, (x + 1.0f) / 16.0f - 1.0f, (y + 1.0f) / 16.0f - 1.0f, 0.0f,
r, g, b, a);
}
// Draws that reach the renderer and rasterize nothing: zero-area strips.
void DrawFillers(int count) {
for (int i = 0; i < count; ++i) {
DrawRect(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
}
}
// A kSide x kSide RGBA8 target, optionally with a DEPTH24_STENCIL8 renderbuffer.
// Returns fbo 0 when the driver will not complete it.
ColorFbo MakeTarget(bool withDepthStencil) {
ColorFbo target = MakeColorFbo(kSide, kSide);
if (target.fbo == 0) return target;
m_colorTargets.push_back(target);
if (withDepthStencil) {
GLuint rbo = 0;
glGenRenderbuffers(1, &rbo);
m_renderbuffers.push_back(rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSide, kSide);
glBindFramebuffer(GL_FRAMEBUFFER, target.fbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
target.fbo = 0;
}
}
return target;
}
Image ReadTarget(const ColorFbo& target) {
glBindFramebuffer(GL_FRAMEBUFFER, target.fbo);
return ReadPixels(kSide, kSide);
}
std::uintmax_t m_logBefore = 0;
GLuint m_vao = 0;
GLuint m_vbo = 0;
GLuint m_rectProgram = 0;
GLint m_rectLoc = -1;
GLint m_depthLoc = -1;
GLint m_colorLoc = -1;
std::vector<ColorFbo> m_colorTargets;
std::vector<GLuint> m_renderbuffers;
std::vector<GLuint> m_framebuffers;
};
} // namespace
// kDraws single-pixel quads into a fresh target, one uniform value per draw, with no
// readback, flush or rebind in between. Every pixel must hold its own draw's colour.
TEST_F(OversizedRecordingSplitScenario, EveryDrawOfTheFrameLandsAcrossCommandBufferSplits) {
if (!Ready()) return;
std::string error;
const unsigned int program = CompileProgram(kVS, kFS, &error);
ASSERT_NE(program, 0u) << error;
ColorFbo target = MakeTarget(false);
ASSERT_NE(target.fbo, 0u) << "MakeColorFbo failed";
BindFbo(target);
glDisable(GL_SCISSOR_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
ClearTo(0.0f, 0.0f, 0.0f, 0.0f);
glUseProgram(program);
glBindVertexArray(m_vao);
const GLint indexLocation = glGetUniformLocation(program, "uIndex");
for (int i = 0; i < kDraws; ++i) {
glUniform1i(indexLocation, i);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
const Image image = ReadPixels(kSide, kSide);
glUseProgram(0);
glDeleteProgram(program);
EXPECT_TRUE(EveryPixelHoldsItsOwnDraw(image));
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (a) An occluder drawn before the split must still fail the depth test of a draw after it:
// the depth attachment's contents have to survive the render pass ending and re-beginning.
TEST_F(OversizedRecordingSplitScenario, ADepthOccluderDrawnBeforeTheSplitHidesADrawAfterIt) {
if (!Ready()) return;
std::string error;
ASSERT_TRUE(BuildRectProgram(&error)) << error;
ColorFbo target = MakeTarget(true);
if (target.fbo == 0) GTEST_SKIP() << "no complete RGBA8 + DEPTH24_STENCIL8 target on this driver";
BindFbo(target);
glClearDepth(1.0);
ClearTo(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
DrawRect(-1.0f, -1.0f, 0.0f, 1.0f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f); // left half, near, red
DrawFillers(kFillers);
DrawRect(-1.0f, -1.0f, 1.0f, 1.0f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f); // everything, far, green
const Image image = ReadTarget(target);
EXPECT_TRUE(ColumnsAre(image, 0, kSide / 2 - 1, Rgba8{255, 0, 0, 255}, 0,
"the near occluder drawn before the split must hide the far draw after it"));
EXPECT_TRUE(ColumnsAre(image, kSide / 2, kSide - 1, Rgba8{0, 255, 0, 255}, 0,
"where nothing occludes, the far draw after the split must land"));
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (b) A stencil mark made before the split must gate a draw after it.
TEST_F(OversizedRecordingSplitScenario, AStencilMarkMadeBeforeTheSplitGatesADrawAfterIt) {
if (!Ready()) return;
std::string error;
ASSERT_TRUE(BuildRectProgram(&error)) << error;
ColorFbo target = MakeTarget(true);
if (target.fbo == 0) GTEST_SKIP() << "no complete RGBA8 + DEPTH24_STENCIL8 target on this driver";
BindFbo(target);
glDisable(GL_DEPTH_TEST);
glStencilMask(0xFF);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
ClearTo(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
DrawRect(-1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f); // left half: stencil 1, blue
glStencilFunc(GL_EQUAL, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
DrawFillers(kFillers);
DrawRect(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f); // everything, green, where stencil == 1
const Image image = ReadTarget(target);
EXPECT_TRUE(ColumnsAre(image, 0, kSide / 2 - 1, Rgba8{0, 255, 0, 255}, 0,
"where the mark was made before the split, the draw after it must pass"));
EXPECT_TRUE(ColumnsAre(image, kSide / 2, kSide - 1, Rgba8{0, 0, 0, 255}, 0,
"where no mark was made, the draw after the split must be stencilled out"));
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (c) Additive blending onto pixels written before the split: every one of kSpanDraws
// full-target draws adds exactly 1/255 of red onto a target whose green was written first,
// so the result counts every draw on both sides of every split.
TEST_F(OversizedRecordingSplitScenario, BlendingAccumulatesOntoPixelsWrittenBeforeTheSplit) {
if (!Ready()) return;
std::string error;
ASSERT_TRUE(BuildRectProgram(&error)) << error;
ColorFbo target = MakeTarget(false);
ASSERT_NE(target.fbo, 0u) << "MakeColorFbo failed";
BindFbo(target);
glDisable(GL_DEPTH_TEST);
ClearTo(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_ONE, GL_ONE);
DrawRect(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 55.0f / 255.0f, 0.0f, 0.0f);
for (int i = 0; i < kSpanDraws; ++i) {
DrawRect(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f / 255.0f, 0.0f, 0.0f, 0.0f);
}
const Image image = ReadTarget(target);
EXPECT_TRUE(ColumnsAre(image, 0, kSide - 1, Rgba8{static_cast<std::uint8_t>(kSpanDraws), 55, 0, 0}, 1,
"every additive draw, and the green written before any split, must be in the sum"));
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (d) A multisample target drawn on both sides of the split, then resolved. Before the
// split a triangle covers the lower-left half of the target; after it, the complementary
// triangle covers the upper-right half. On the diagonal each pixel's samples are shared
// between the two, so only a target that kept every SAMPLE across the split resolves them to
// red + green = 255. One that was resolved or re-broadcast at the split gives about 190
// there, and one whose samples were dropped gives black or garbage.
TEST_F(OversizedRecordingSplitScenario, AMultisampleTargetKeepsItsSamplesAcrossTheSplit) {
if (!Ready()) return;
std::string error;
ASSERT_TRUE(BuildRectProgram(&error)) << error;
GLint maxSamples = 0;
glGetIntegerv(GL_MAX_SAMPLES, &maxSamples);
if (maxSamples < 2) {
GTEST_SKIP() << "GL_MAX_SAMPLES is " << maxSamples << "; this needs a multisample renderbuffer";
}
const GLsizei samples = std::min<GLint>(4, maxSamples);
GLuint msFbo = 0, msRbo = 0;
glGenFramebuffers(1, &msFbo);
m_framebuffers.push_back(msFbo);
glGenRenderbuffers(1, &msRbo);
m_renderbuffers.push_back(msRbo);
glBindRenderbuffer(GL_RENDERBUFFER, msRbo);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_RGBA8, kSide, kSide);
glBindFramebuffer(GL_FRAMEBUFFER, msFbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msRbo);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
GTEST_SKIP() << "no complete " << samples << "x multisample RGBA8 renderbuffer on this driver";
}
ColorFbo resolved = MakeTarget(false);
ASSERT_NE(resolved.fbo, 0u) << "MakeColorFbo failed";
glBindFramebuffer(GL_FRAMEBUFFER, msFbo);
glViewport(0, 0, kSide, kSide);
glDisable(GL_DEPTH_TEST);
ClearTo(0.0f, 0.0f, 0.0f, 1.0f);
DrawRect(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, GL_TRIANGLES, 4, 3); // lower-left, red
DrawFillers(kFillers);
DrawRect(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, GL_TRIANGLES, 7, 3); // upper-right, green
glBindFramebuffer(GL_READ_FRAMEBUFFER, msFbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, resolved.fbo);
glBlitFramebuffer(0, 0, kSide, kSide, 0, 0, kSide, kSide, GL_COLOR_BUFFER_BIT, GL_NEAREST);
const Image image = ReadTarget(resolved);
int wrong = 0;
std::string first;
for (int y = 0; y < kSide && wrong < 1000; ++y) {
for (int x = 0; x < kSide; ++x) {
const Rgba8 seen = image.At(x, y);
bool ok = false;
// The diagonal x + y = kSide in pixel units passes through the pixels whose
// x + y == kSide - 1; every other pixel lies wholly on one side of it.
if (x + y < kSide - 1) {
ok = seen == Rgba8{255, 0, 0, 255};
} else if (x + y > kSide - 1) {
ok = seen == Rgba8{0, 255, 0, 255};
} else {
ok = seen.b == 0 && seen.a == 255 && seen.r > 0 && seen.g > 0 &&
std::abs(static_cast<int>(seen.r) + static_cast<int>(seen.g) - 255) <= 3;
}
if (!ok) {
if (wrong == 0) {
std::ostringstream os;
os << "(" << x << ", " << y << ") = " << Describe(seen);
first = os.str();
}
++wrong;
}
}
}
EXPECT_EQ(wrong, 0) << wrong << " resolved pixels are wrong; first " << first
<< ". Lower-left of the diagonal must be red, upper-right green, and a pixel on it "
"red + green = 255 with both present (its samples were split between a draw before "
"the command-buffer split and one after it)";
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (e) Occlusion queries open across the split. GL_SAMPLES_PASSED counts kSpanDraws
// single-pixel draws on both sides of it; GL_ANY_SAMPLES_PASSED is answered by the one draw
// made before the split, the fillers after it pass nothing - with a fillers-only control
// that must answer false.
TEST_F(OversizedRecordingSplitScenario, OcclusionQueriesOpenAcrossTheSplitCountBothSides) {
if (!Ready()) return;
std::string error;
ASSERT_TRUE(BuildRectProgram(&error)) << error;
ColorFbo target = MakeTarget(false);
ASSERT_NE(target.fbo, 0u) << "MakeColorFbo failed";
BindFbo(target);
glDisable(GL_DEPTH_TEST);
ClearTo(0.0f, 0.0f, 0.0f, 1.0f);
GLuint queries[3] = {};
glGenQueries(3, queries);
FirstGLError();
// Any samples: control first (fillers only), then one real pixel before the split.
glBeginQuery(GL_ANY_SAMPLES_PASSED, queries[0]);
const unsigned int anyBeginError = FirstGLError();
if (anyBeginError != 0u) {
glDeleteQueries(3, queries);
GTEST_SKIP() << Gl().BackendName() << " does not accept GL_ANY_SAMPLES_PASSED ("
<< GLErrorName(anyBeginError) << ")";
}
DrawFillers(kFillers);
glEndQuery(GL_ANY_SAMPLES_PASSED);
glBeginQuery(GL_ANY_SAMPLES_PASSED, queries[1]);
DrawPixel(0, 1.0f, 1.0f, 1.0f, 1.0f);
DrawFillers(kFillers);
glEndQuery(GL_ANY_SAMPLES_PASSED);
GLuint anyControl = 7, anyAcross = 7;
glGetQueryObjectuiv(queries[0], GL_QUERY_RESULT, &anyControl);
glGetQueryObjectuiv(queries[1], GL_QUERY_RESULT, &anyAcross);
EXPECT_EQ(anyControl, static_cast<GLuint>(GL_FALSE)) << "fillers alone rasterize nothing";
EXPECT_EQ(anyAcross, static_cast<GLuint>(GL_TRUE))
<< "the only passing draw was made before the split; the query that spans it lost that answer";
// Samples passed: kSpanDraws distinct single-sample pixels, spanning at least one split.
glBeginQuery(GL_SAMPLES_PASSED, queries[2]);
const unsigned int samplesBeginError = FirstGLError();
if (samplesBeginError == 0u) {
for (int i = 0; i < kSpanDraws; ++i) {
DrawPixel(i, 1.0f, 1.0f, 1.0f, 1.0f);
}
glEndQuery(GL_SAMPLES_PASSED);
GLuint samplesPassed = 0;
glGetQueryObjectuiv(queries[2], GL_QUERY_RESULT, &samplesPassed);
if (Gl().BackendName() == std::string("DirectGLES")) {
// OpenGL ES has no sample-counting occlusion query, only ANY_SAMPLES_PASSED*, so
// DirectGLES answers GL_SAMPLES_PASSED with a 0/1 approximation (DirectGLES.cpp,
// the occlusion-query result path). The exact count is not this backend's to give.
RecordProperty("samples_passed", "0/1 approximation on OpenGL ES; exact count not checked");
EXPECT_EQ(samplesPassed, 1u) << "samples passed, so the approximation must read 1";
} else {
EXPECT_EQ(samplesPassed, static_cast<GLuint>(kSpanDraws))
<< "each of the " << kSpanDraws << " draws covers exactly one single-sample pixel";
}
} else {
RecordProperty("samples_passed", std::string("not accepted: ") + GLErrorName(samplesBeginError));
}
glDeleteQueries(3, queries);
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (e) GL_PRIMITIVES_GENERATED open across the split: kSpanDraws strips of two triangles.
// A one-draw query first establishes that this stack counts at all.
TEST_F(OversizedRecordingSplitScenario, APrimitivesGeneratedQueryOpenAcrossTheSplitCountsBothSides) {
if (!Ready()) return;
std::string error;
ASSERT_TRUE(BuildRectProgram(&error)) << error;
ColorFbo target = MakeTarget(false);
ASSERT_NE(target.fbo, 0u) << "MakeColorFbo failed";
BindFbo(target);
glDisable(GL_DEPTH_TEST);
ClearTo(0.0f, 0.0f, 0.0f, 1.0f);
GLuint queries[2] = {};
glGenQueries(2, queries);
FirstGLError();
glBeginQuery(GL_PRIMITIVES_GENERATED, queries[0]);
const unsigned int beginError = FirstGLError();
if (beginError != 0u) {
glDeleteQueries(2, queries);
GTEST_SKIP() << Gl().BackendName() << " does not accept GL_PRIMITIVES_GENERATED ("
<< GLErrorName(beginError) << ")";
}
DrawPixel(0, 1.0f, 1.0f, 1.0f, 1.0f);
glEndQuery(GL_PRIMITIVES_GENERATED);
GLuint control = 0;
glGetQueryObjectuiv(queries[0], GL_QUERY_RESULT, &control);
if (control == 0u) {
glDeleteQueries(2, queries);
GTEST_SKIP() << "GL_PRIMITIVES_GENERATED reads 0 for a single unsplit strip on " << Gl().BackendName()
<< " (" << Gl().RendererString() << "): nothing this stack counts, so nothing to carry";
}
ASSERT_EQ(control, 2u) << "one strip of four vertices is two triangles";
glBeginQuery(GL_PRIMITIVES_GENERATED, queries[1]);
for (int i = 0; i < kSpanDraws; ++i) {
DrawPixel(i, 1.0f, 1.0f, 1.0f, 1.0f);
}
glEndQuery(GL_PRIMITIVES_GENERATED);
GLuint generated = 0;
glGetQueryObjectuiv(queries[1], GL_QUERY_RESULT, &generated);
EXPECT_EQ(generated, static_cast<GLuint>(2 * kSpanDraws))
<< kSpanDraws << " strips of two triangles each, drawn across at least one split";
glDeleteQueries(2, queries);
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (f) Transform feedback active across the split: kSpanDraws one-point draws, draw i
// capturing 2i + 1, must append in order into one buffer with nothing past the end, and
// GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN must count every one.
TEST_F(OversizedRecordingSplitScenario, ATransformFeedbackCaptureActiveAcrossTheSplitAppendsEveryDraw) {
if (!Ready()) return;
auto compile = [](GLenum stage, const char* source, std::string* log) -> GLuint {
const GLuint shader = glCreateShader(stage);
glShaderSource(shader, 1, &source, nullptr);
glCompileShader(shader);
GLint ok = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
if (!ok) {
char buffer[2048] = {};
glGetShaderInfoLog(shader, sizeof(buffer) - 1, nullptr, buffer);
*log = buffer;
glDeleteShader(shader);
return 0;
}
return shader;
};
std::string log;
const GLuint vs = compile(GL_VERTEX_SHADER, kCaptureVS, &log);
ASSERT_NE(vs, 0u) << log;
const GLuint fs = compile(GL_FRAGMENT_SHADER, kCaptureFS, &log);
ASSERT_NE(fs, 0u) << log;
const GLuint program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
const char* varyings[] = {"vs_value"};
glTransformFeedbackVaryings(program, 1, varyings, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
GLint linked = 0;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked) {
char buffer[2048] = {};
glGetProgramInfoLog(program, sizeof(buffer) - 1, nullptr, buffer);
glDeleteProgram(program);
FAIL() << "capture program failed to link: " << buffer;
}
constexpr int kSlack = 4;
std::vector<float> poison(kSpanDraws + kSlack, kCapturePoison);
GLuint buffer = 0;
glGenBuffers(1, &buffer);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, buffer);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, static_cast<GLsizeiptr>(poison.size() * sizeof(float)),
poison.data(), GL_STATIC_COPY);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, buffer);
GLuint written = 0;
glGenQueries(1, &written);
glUseProgram(program);
glBindVertexArray(m_vao);
const GLint indexLocation = glGetUniformLocation(program, "uIndex");
glEnable(GL_RASTERIZER_DISCARD);
glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, written);
glBeginTransformFeedback(GL_POINTS);
for (int i = 0; i < kSpanDraws; ++i) {
glUniform1i(indexLocation, i);
glDrawArrays(GL_POINTS, 0, 1);
}
glEndTransformFeedback();
glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
glDisable(GL_RASTERIZER_DISCARD);
glUseProgram(0);
GLuint primitivesWritten = 0;
glGetQueryObjectuiv(written, GL_QUERY_RESULT, &primitivesWritten);
std::vector<float> captured(poison.size(), 0.0f);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, buffer);
glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0, static_cast<GLsizeiptr>(captured.size() * sizeof(float)),
captured.data());
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
glDeleteQueries(1, &written);
glDeleteBuffers(1, &buffer);
glDeleteProgram(program);
EXPECT_EQ(primitivesWritten, static_cast<GLuint>(kSpanDraws));
int wrong = 0;
std::string first;
for (int i = 0; i < static_cast<int>(captured.size()); ++i) {
const float expected = i < kSpanDraws ? static_cast<float>(2 * i + 1) : kCapturePoison;
if (captured[i] != expected) {
if (wrong == 0) {
std::ostringstream os;
os << "record " << i << " = " << captured[i] << ", expected " << expected;
first = os.str();
}
++wrong;
}
}
EXPECT_EQ(wrong, 0) << wrong << " of " << captured.size() << " records are wrong; first " << first
<< " (records past " << kSpanDraws << " must keep the poison)";
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
} // namespace MGITest
@@ -30,6 +30,15 @@
// earlier clear or draw landed over it: the box read back green. Each order is covered - the
// clear already recorded, still queued, synced by a draw or by the readback, the reverse order
// in which the clear must win, and a draw instead of a clear.
//
// The last part is about SCATTER: several boxes written between two syncs, with the GPU's clear in
// the gaps between them. Everything a backend has to go on is the union box and the storage's
// rect list, and both describe more than the writes - the box spans the gaps, the list merges
// touching rects into their bounding box, folds once it holds 96, and is withheld once its rects
// fill 3/4 of the box; a box spanning the level went up as the whole level. Espryt also uploaded
// the union box whenever its unpack ring was in use. Each case below reaches one of those: two
// distant boxes, a hundred of them, boxes filling more than 3/4 of their union, and boxes whose
// union spans the level.
#include <cmath>
#include <cstdint>
@@ -377,6 +386,60 @@ void main() {
ExpectClearAroundBox(rgb, kEdge, kEdge, kBoxAt, kBoxAt, boxWins ? kBoxEdge : 0, boxWins ? kBoxEdge : 0);
}
struct Box {
int x, y, w, h;
};
// An edge x edge RGBA8 level uploaded red, cleared green on the GPU, then written blue in
// every box by one glTexSubImage each, with no sync in between; read back after a sync.
// Blue inside the boxes, green everywhere else.
void RunScatteredWrites(int edge, const std::vector<Box>& boxes) {
const GLuint t = NewTexture(GL_TEXTURE_2D);
const auto red = Fill(Rgba8(), edge * edge, 1.0f, 0.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, t);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, edge, edge, 0, GL_RGBA, GL_UNSIGNED_BYTE, red.data());
glBindTexture(GL_TEXTURE_2D, 0);
ASSERT_EQ(FirstGLError(), 0u) << "definition";
SampleRgba8(t);
ClearRgba8Green(t);
SampleRgba8(t);
glBindTexture(GL_TEXTURE_2D, t);
for (const Box& box : boxes) {
const auto blue = Fill(Rgba8(), box.w * box.h, 0.0f, 0.0f, 1.0f);
glTexSubImage2D(GL_TEXTURE_2D, 0, box.x, box.y, box.w, box.h, GL_RGBA, GL_UNSIGNED_BYTE,
blue.data());
}
glBindTexture(GL_TEXTURE_2D, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
ASSERT_EQ(FirstGLError(), 0u) << "writes";
SampleRgba8(t);
ASSERT_TRUE(Attach(t, -1));
const auto rgb = ReadRgb(Rgba8(), edge, edge);
Detach();
int bad = 0, stale = 0;
std::ostringstream first;
for (int y = 0; y < edge; ++y)
for (int x = 0; x < edge; ++x) {
bool inBox = false;
for (const Box& box : boxes)
inBox |= x >= box.x && x < box.x + box.w && y >= box.y && y < box.y + box.h;
const float want[3] = {0.0f, inBox ? 0.0f : 1.0f, inBox ? 1.0f : 0.0f};
const float* got = &rgb[static_cast<std::size_t>(y * edge + x) * 3u];
bool texelBad = false;
for (int c = 0; c < 3; ++c) texelBad |= std::fabs(got[c] - want[c]) > 0.02f;
if (!texelBad) continue;
stale += got[0] > 0.5f;
if (bad++ == 0) {
first << "(" << x << "," << y << ") got " << got[0] << "," << got[1] << "," << got[2]
<< " want " << want[0] << "," << want[1] << "," << want[2];
}
}
EXPECT_EQ(bad, 0) << bad << " of " << edge * edge << " texels wrong, " << stale
<< " of them red - the stale shadow went up outside the writes; first "
<< first.str();
}
GLuint m_vao = 0, m_fbo = 0;
ColorFbo m_scratch{};
std::vector<GLuint> m_programs;
@@ -503,5 +566,35 @@ void main() {
SampleRgba8(t);
ExpectRgba8(t, true);
}
// Two boxes in opposite corners: their union box is 7x7, of which the writes are 8 texels.
TEST_F(PartialUploadOverGpuContentScenario, TwoDistantSubImagesKeepTheGpuClearBetweenThem) {
if (!Ready()) return;
RunScatteredWrites(kEdge, {{0, 0, 2, 2}, {5, 5, 2, 2}});
}
// A hundred 2x2 boxes two texels apart: more than the 96 rects the storage keeps, so its
// list folds neighbours together, gaps and all.
TEST_F(PartialUploadOverGpuContentScenario, AHundredSubImagesKeepTheGpuClearBetweenThem) {
if (!Ready()) return;
std::vector<Box> boxes;
for (int j = 0; j < 10; ++j)
for (int i = 0; i < 10; ++i) boxes.push_back({4 * i + 1, 4 * j + 1, 2, 2});
RunScatteredWrites(40, boxes);
}
// Four 4x4 boxes around a one-texel cross: 64 of their union's 81 texels, past the 3/4 at
// which the storage stops handing its rect list out.
TEST_F(PartialUploadOverGpuContentScenario, SubImagesFillingThreeQuartersOfTheirUnionKeepTheGpuClearBetweenThem) {
if (!Ready()) return;
RunScatteredWrites(16, {{2, 2, 4, 4}, {7, 2, 4, 4}, {2, 7, 4, 4}, {7, 7, 4, 4}});
}
// Three quadrants: the union box is the whole level, and the list merges the touching
// boxes into it; the fourth quadrant is still the GPU's.
TEST_F(PartialUploadOverGpuContentScenario, SubImagesWhoseUnionSpansTheLevelKeepTheGpuClearInTheGap) {
if (!Ready()) return;
RunScatteredWrites(kEdge, {{0, 0, 4, 4}, {4, 0, 4, 4}, {0, 4, 4, 4}});
}
} // namespace
} // namespace MGITest