mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-16 14:33:24 +09:00
[Merge] (P11): dev@c60639db - the Espryt image-bindable re-mint reads GPU levels back on the monolith arm too, partial writes into converted or reshaped levels upload only their region on every arm, Magma orders an upload after the recording's writes and queued clears, and Magma's eglSwapInterval reaches the swapchain
This commit is contained in:
@@ -114,6 +114,8 @@ add_executable(MobileGLIntegrationTest
|
||||
Scenarios/NonCoreImageFormatScenario.cpp
|
||||
Scenarios/ImageSizeAfterRespecScenario.cpp
|
||||
Scenarios/EmptyTextureLevelScenario.cpp
|
||||
Scenarios/ImageBindableRemintScenario.cpp
|
||||
Scenarios/PartialUploadOverGpuContentScenario.cpp
|
||||
Scenarios/SsboDeclarationFormScenario.cpp
|
||||
Scenarios/Glsl420DeclarationScenario.cpp
|
||||
Scenarios/IoBlockNameCollisionScenario.cpp
|
||||
|
||||
@@ -0,0 +1,516 @@
|
||||
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/ImageBindableRemintScenario.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 - THE FIRST glBindImageTexture OF A TEXTURE THE GPU HAS ALREADY WRITTEN.
|
||||
//
|
||||
// ES only allows immutable storage on an image unit, and a few formats need a wider carrier on
|
||||
// top (TextureImpl::GetImageBindableStorageWidening), so Espryt re-mints a texture the first time
|
||||
// it is image-bound after it already has driver storage (RequireImageBindableStorage). The new
|
||||
// storage has to hold what the old one held. The re-mint used to fill it from the frontend's
|
||||
// shadow - the CLIENT's copy of each level - and the shadow never sees a GPU write: a render or
|
||||
// clear into the level, a glGenerateMipmap, a null-data definition only the GPU ever filled. Every
|
||||
// such level came back as the bytes the application last uploaded, or zeroes. That is the
|
||||
// ~26.9k-pixel iris-derivative difference: an RGBA16F render target, first image-bound after it
|
||||
// was rendered into.
|
||||
//
|
||||
// Every case gives a texture its content by one means, lets the backend create its storage (a
|
||||
// sampling draw, or the FBO attach of a GPU clear), writes it on the GPU where the case says so,
|
||||
// and then binds it to an image unit for the first time: a compute imageLoad copies the level into
|
||||
// a destination that was image-bound before it was ever synced (so the destination itself never
|
||||
// re-mints), and the destination is read back through an FBO. The expectation is the GL answer.
|
||||
//
|
||||
// The controls are the other half: levels the GPU never wrote (including formats whose driver
|
||||
// readback may be refused), levels the driver does not have yet because they were defined after
|
||||
// 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 <cstdint>
|
||||
#include <cstdlib>
|
||||
#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 kEdge = 8;
|
||||
|
||||
constexpr const char* kVS = R"(#version 430 core
|
||||
void main() {
|
||||
vec2 positions[3] = vec2[3](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
|
||||
gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0);
|
||||
}
|
||||
)";
|
||||
constexpr const char* kSampleFS = R"(#version 430 core
|
||||
uniform sampler2D u_tex;
|
||||
out vec4 o_color;
|
||||
void main() { o_color = texelFetch(u_tex, ivec2(gl_FragCoord.xy) % 4, 0); }
|
||||
)";
|
||||
|
||||
// Copies an image into an RGBA8 image with imageLoad; a signed-normalized source is mapped
|
||||
// to [0, 1] so it survives the unsigned destination.
|
||||
std::string CopyCS(const char* srcFormat, bool snorm) {
|
||||
std::string s = "#version 430 core\nlayout(local_size_x = 8, local_size_y = 8) in;\n";
|
||||
s += std::string("layout(") + srcFormat + ", binding = 0) readonly uniform image2D u_src;\n";
|
||||
s += "layout(rgba8, binding = 1) writeonly uniform image2D u_dst;\n";
|
||||
s += "void main() { ivec2 p = ivec2(gl_GlobalInvocationID.xy); vec4 v = imageLoad(u_src, p);\n";
|
||||
if (snorm) s += " v = v * 0.5 + 0.5;\n";
|
||||
s += " imageStore(u_dst, p, v); }\n";
|
||||
return s;
|
||||
}
|
||||
|
||||
std::vector<GLubyte> Pattern(int edge, GLubyte magic) {
|
||||
std::vector<GLubyte> t(static_cast<std::size_t>(edge * edge) * 4u);
|
||||
for (int y = 0; y < edge; ++y)
|
||||
for (int x = 0; x < edge; ++x) {
|
||||
const std::size_t at = static_cast<std::size_t>((y * edge + x) * 4);
|
||||
t[at + 0] = magic;
|
||||
t[at + 1] = static_cast<GLubyte>(16 * (x + 1));
|
||||
t[at + 2] = static_cast<GLubyte>(16 * (y + 1));
|
||||
t[at + 3] = 0xFF;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
std::vector<GLubyte> Solid(int edge, GLubyte r, GLubyte g, GLubyte b, GLubyte a) {
|
||||
std::vector<GLubyte> t(static_cast<std::size_t>(edge * edge) * 4u);
|
||||
for (std::size_t i = 0; i < t.size(); i += 4) {
|
||||
t[i] = r;
|
||||
t[i + 1] = g;
|
||||
t[i + 2] = b;
|
||||
t[i + 3] = a;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
const GLfloat kGreen[4] = {0.0f, 1.0f, 0.0f, 1.0f};
|
||||
|
||||
class ImageBindableRemintScenario : public ScenarioTest {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
ScenarioTest::SetUp();
|
||||
if (!Ready()) return;
|
||||
std::string error;
|
||||
m_sampleProgram = CompileProgram(kVS, kSampleFS, &error);
|
||||
ASSERT_NE(m_sampleProgram, 0u) << error;
|
||||
m_scratch = MakeColorFbo(4, 4);
|
||||
glGenVertexArrays(1, &m_vao);
|
||||
FirstGLError();
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
if (!Ready()) return;
|
||||
glUseProgram(0);
|
||||
glBindImageTexture(0, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
|
||||
glBindImageTexture(1, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
|
||||
if (m_sampleProgram) glDeleteProgram(m_sampleProgram);
|
||||
if (m_vao) glDeleteVertexArrays(1, &m_vao);
|
||||
if (!m_textures.empty()) glDeleteTextures(static_cast<GLsizei>(m_textures.size()), m_textures.data());
|
||||
if (m_fbo) glDeleteFramebuffers(1, &m_fbo);
|
||||
BindDefaultFramebuffer();
|
||||
DestroyColorFbo(m_scratch);
|
||||
glViewport(0, 0, Gl().Width(), Gl().Height());
|
||||
FirstGLError();
|
||||
}
|
||||
|
||||
GLuint NewTexture() {
|
||||
GLuint t = 0;
|
||||
glGenTextures(1, &t);
|
||||
m_textures.push_back(t);
|
||||
return t;
|
||||
}
|
||||
|
||||
static void Nearest(GLenum target, GLuint tex, int maxLevel) {
|
||||
glBindTexture(target, tex);
|
||||
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevel);
|
||||
glBindTexture(target, 0);
|
||||
}
|
||||
|
||||
// A draw that samples the texture: the backend now holds storage for it.
|
||||
void SyncBySampling(GLuint tex) {
|
||||
BindFbo(m_scratch);
|
||||
glUseProgram(m_sampleProgram);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
glUniform1i(glGetUniformLocation(m_sampleProgram, "u_tex"), 0);
|
||||
glBindVertexArray(m_vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glBindVertexArray(0);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glUseProgram(0);
|
||||
BindDefaultFramebuffer();
|
||||
glFinish();
|
||||
}
|
||||
|
||||
// A GPU write the frontend's shadow never sees. A layer >= 0 names one layer of an
|
||||
// array texture.
|
||||
void GpuClear(GLuint tex, int level, const GLfloat* color, int layer = -1) {
|
||||
if (!m_fbo) glGenFramebuffers(1, &m_fbo);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
|
||||
if (layer >= 0) {
|
||||
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, level, layer);
|
||||
} else {
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, level);
|
||||
}
|
||||
EXPECT_EQ(glCheckFramebufferStatus(GL_FRAMEBUFFER), GLenum(GL_FRAMEBUFFER_COMPLETE))
|
||||
<< "the clear target is incomplete";
|
||||
glClearBufferfv(GL_COLOR, 0, color);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
|
||||
BindDefaultFramebuffer();
|
||||
}
|
||||
|
||||
// The first image binding of (src, level[, layer]): an imageLoad copy into a fresh RGBA8
|
||||
// destination, read back through an FBO.
|
||||
std::vector<GLubyte> ImageCopy(GLuint src, int level, GLenum unitFormat, const char* glslFormat,
|
||||
bool snorm = false, int layer = 0) {
|
||||
const GLuint dst = NewTexture();
|
||||
glBindTexture(GL_TEXTURE_2D, dst);
|
||||
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge, kEdge);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glBindImageTexture(1, dst, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8);
|
||||
glBindImageTexture(0, src, level, GL_FALSE, layer, GL_READ_ONLY, unitFormat);
|
||||
EXPECT_EQ(FirstGLError(), 0u) << "glBindImageTexture";
|
||||
const std::string cs = CopyCS(glslFormat, snorm);
|
||||
const char* csp = cs.c_str();
|
||||
const GLuint shader = glCreateShader(GL_COMPUTE_SHADER);
|
||||
glShaderSource(shader, 1, &csp, nullptr);
|
||||
glCompileShader(shader);
|
||||
const GLuint program = glCreateProgram();
|
||||
glAttachShader(program, shader);
|
||||
glLinkProgram(program);
|
||||
glDeleteShader(shader);
|
||||
GLint linked = GL_FALSE;
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &linked);
|
||||
EXPECT_EQ(linked, GL_TRUE) << "the copy program did not link";
|
||||
glUseProgram(program);
|
||||
glDispatchCompute(1, 1, 1);
|
||||
glMemoryBarrier(GL_ALL_BARRIER_BITS);
|
||||
glUseProgram(0);
|
||||
glDeleteProgram(program);
|
||||
glBindImageTexture(0, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
|
||||
glBindImageTexture(1, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
|
||||
if (!m_fbo) glGenFramebuffers(1, &m_fbo);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dst, 0);
|
||||
std::vector<GLubyte> out(static_cast<std::size_t>(kEdge * kEdge) * 4u, 0xA5);
|
||||
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
||||
glReadPixels(0, 0, kEdge, kEdge, GL_RGBA, GL_UNSIGNED_BYTE, out.data());
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
|
||||
BindDefaultFramebuffer();
|
||||
EXPECT_EQ(FirstGLError(), 0u) << "copy/readback";
|
||||
return out;
|
||||
}
|
||||
|
||||
// The edge x edge corner of `got` against an RGBA8 expectation, the first `channels`
|
||||
// channels only, within 2 codes (a normalized value's round trip).
|
||||
static void ExpectImage(const std::vector<GLubyte>& got, int edge, const std::vector<GLubyte>& want,
|
||||
int channels = 4) {
|
||||
int bad = 0;
|
||||
std::ostringstream first;
|
||||
for (int y = 0; y < edge; ++y)
|
||||
for (int x = 0; x < edge; ++x) {
|
||||
const std::size_t g = static_cast<std::size_t>((y * kEdge + x) * 4);
|
||||
const std::size_t w = static_cast<std::size_t>((y * edge + x) * 4);
|
||||
bool texelBad = false;
|
||||
for (int c = 0; c < channels; ++c)
|
||||
texelBad |= std::abs(int(got[g + c]) - int(want[w + c])) > 2;
|
||||
if (texelBad && bad++ == 0) {
|
||||
first << "(" << x << "," << y << ") got " << int(got[g]) << "," << int(got[g + 1]) << ","
|
||||
<< int(got[g + 2]) << "," << int(got[g + 3]) << " want " << int(want[w]) << ","
|
||||
<< int(want[w + 1]) << "," << int(want[w + 2]) << "," << int(want[w + 3]);
|
||||
}
|
||||
}
|
||||
EXPECT_EQ(bad, 0) << bad << " of " << edge * edge << " texels differ; first " << first.str();
|
||||
}
|
||||
|
||||
GLuint m_sampleProgram = 0, m_vao = 0, m_fbo = 0;
|
||||
ColorFbo m_scratch{};
|
||||
std::vector<GLuint> m_textures;
|
||||
};
|
||||
|
||||
// Control: the shadow and the driver agree.
|
||||
TEST_F(ImageBindableRemintScenario, UploadedLevelKeepsItsUpload) {
|
||||
if (!Ready()) return;
|
||||
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);
|
||||
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, p);
|
||||
}
|
||||
|
||||
TEST_F(ImageBindableRemintScenario, GpuClearOverAnUploadSurvives) {
|
||||
if (!Ready()) return;
|
||||
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);
|
||||
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, Solid(kEdge, 0, 255, 0, 255));
|
||||
}
|
||||
|
||||
// Immutable in a core image format: the storage is already what an image unit needs.
|
||||
TEST_F(ImageBindableRemintScenario, GpuClearOverAnImmutableUploadSurvives) {
|
||||
if (!Ready()) return;
|
||||
const GLuint t = NewTexture();
|
||||
const auto p = Pattern(kEdge, 0x40);
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge, kEdge);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RGBA, GL_UNSIGNED_BYTE, p.data());
|
||||
Nearest(GL_TEXTURE_2D, t, 0);
|
||||
SyncBySampling(t);
|
||||
GpuClear(t, 0, kGreen);
|
||||
SyncBySampling(t);
|
||||
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, Solid(kEdge, 0, 255, 0, 255));
|
||||
}
|
||||
|
||||
// A null-data definition only the GPU ever filled: the shadow holds zeroes.
|
||||
TEST_F(ImageBindableRemintScenario, GpuClearOfANullDefinitionSurvives) {
|
||||
if (!Ready()) return;
|
||||
const GLuint t = NewTexture();
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
Nearest(GL_TEXTURE_2D, t, 0);
|
||||
SyncBySampling(t);
|
||||
GpuClear(t, 0, kGreen);
|
||||
SyncBySampling(t);
|
||||
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, Solid(kEdge, 0, 255, 0, 255));
|
||||
}
|
||||
|
||||
// A client write still pending when the bind comes lands on top of the GPU's content,
|
||||
// and only inside its own box.
|
||||
TEST_F(ImageBindableRemintScenario, PendingSubImageLandsOnTheGpuClear) {
|
||||
if (!Ready()) return;
|
||||
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);
|
||||
const auto blue = Solid(2, 0, 0, 255, 255);
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 2, 2, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, blue.data());
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
auto want = Solid(kEdge, 0, 255, 0, 255);
|
||||
for (int y = 2; y < 4; ++y)
|
||||
for (int x = 2; x < 4; ++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;
|
||||
}
|
||||
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, want);
|
||||
}
|
||||
|
||||
TEST_F(ImageBindableRemintScenario, GeneratedMipLevelSurvives) {
|
||||
if (!Ready()) return;
|
||||
const GLuint t = NewTexture();
|
||||
const auto s = Solid(kEdge, 200, 100, 50, 255);
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, s.data());
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
SyncBySampling(t);
|
||||
ExpectImage(ImageCopy(t, 1, GL_RGBA8, "rgba8"), kEdge / 2, Solid(kEdge / 2, 200, 100, 50, 255));
|
||||
}
|
||||
|
||||
TEST_F(ImageBindableRemintScenario, GpuClearOfAnUploadedMipLevelSurvives) {
|
||||
if (!Ready()) return;
|
||||
const GLuint t = NewTexture();
|
||||
const auto s0 = Solid(kEdge, 200, 100, 50, 255);
|
||||
const auto s1 = Solid(kEdge / 2, 10, 20, 30, 255);
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, s0.data());
|
||||
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge / 2, kEdge / 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, s1.data());
|
||||
Nearest(GL_TEXTURE_2D, t, 1);
|
||||
SyncBySampling(t);
|
||||
GpuClear(t, 1, kGreen);
|
||||
SyncBySampling(t);
|
||||
ExpectImage(ImageCopy(t, 1, GL_RGBA8, "rgba8"), kEdge / 2, Solid(kEdge / 2, 0, 255, 0, 255));
|
||||
}
|
||||
|
||||
// Control: a level defined (null data) after the last sync is not on the driver yet, so
|
||||
// there is nothing to read back for it; the bound level 0 must still be the upload.
|
||||
TEST_F(ImageBindableRemintScenario, LevelDefinedAfterTheSyncDoesNotDisturbTheOthers) {
|
||||
if (!Ready()) return;
|
||||
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);
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge / 2, kEdge / 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, p);
|
||||
}
|
||||
|
||||
// Control: the same late level, partly uploaded - the shadow is the only copy of it.
|
||||
TEST_F(ImageBindableRemintScenario, LevelDefinedAndUploadedAfterTheSyncKeepsTheUpload) {
|
||||
if (!Ready()) return;
|
||||
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);
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge / 2, kEdge / 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
const auto blue = Solid(2, 0, 0, 255, 255);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 1, 0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, blue.data());
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
ExpectImage(ImageCopy(t, 1, GL_RGBA8, "rgba8"), 2, blue);
|
||||
}
|
||||
|
||||
// GL_RG8 is not an ESSL image format, so the re-mint also WIDENS the storage (to RGBA8)
|
||||
// and cannot keep the old allocation even though it is immutable.
|
||||
TEST_F(ImageBindableRemintScenario, GpuClearSurvivesAWideningRemint) {
|
||||
if (!Ready()) return;
|
||||
const GLuint t = NewTexture();
|
||||
std::vector<GLubyte> rg(static_cast<std::size_t>(kEdge * kEdge) * 2u, 0x33);
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG8, kEdge, kEdge);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RG, GL_UNSIGNED_BYTE, rg.data());
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
||||
Nearest(GL_TEXTURE_2D, t, 0);
|
||||
SyncBySampling(t);
|
||||
const GLfloat c[4] = {0.5f, 0.25f, 0.0f, 1.0f};
|
||||
GpuClear(t, 0, c);
|
||||
SyncBySampling(t);
|
||||
ExpectImage(ImageCopy(t, 0, GL_RG8, "rg8"), kEdge, Solid(kEdge, 128, 64, 0, 255), 2);
|
||||
}
|
||||
|
||||
// Control: widened, uploaded only.
|
||||
TEST_F(ImageBindableRemintScenario, UploadSurvivesAWideningRemint) {
|
||||
if (!Ready()) return;
|
||||
const GLuint t = NewTexture();
|
||||
std::vector<GLubyte> rg(static_cast<std::size_t>(kEdge * kEdge) * 2u);
|
||||
for (std::size_t i = 0; i < rg.size(); i += 2) {
|
||||
rg[i] = 0x33;
|
||||
rg[i + 1] = 0x99;
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG8, kEdge, kEdge);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RG, GL_UNSIGNED_BYTE, rg.data());
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
||||
Nearest(GL_TEXTURE_2D, t, 0);
|
||||
SyncBySampling(t);
|
||||
ExpectImage(ImageCopy(t, 0, GL_RG8, "rg8"), kEdge, Solid(kEdge, 0x33, 0x99, 0, 255), 2);
|
||||
}
|
||||
|
||||
// Control: a signed-normalized level (not colour-renderable on every ES driver, so its
|
||||
// readback may be refused and the shadow used instead), uploaded only.
|
||||
TEST_F(ImageBindableRemintScenario, SnormUploadSurvives) {
|
||||
if (!Ready()) return;
|
||||
const GLuint t = NewTexture();
|
||||
std::vector<GLbyte> s(static_cast<std::size_t>(kEdge * kEdge) * 4u);
|
||||
for (std::size_t i = 0; i < s.size(); i += 4) {
|
||||
s[i] = 127;
|
||||
s[i + 1] = 0;
|
||||
s[i + 2] = -127;
|
||||
s[i + 3] = 127;
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8_SNORM, kEdge, kEdge, 0, GL_RGBA, GL_BYTE, s.data());
|
||||
Nearest(GL_TEXTURE_2D, t, 0);
|
||||
SyncBySampling(t);
|
||||
ExpectImage(ImageCopy(t, 0, GL_RGBA8_SNORM, "rgba8_snorm", true), kEdge, Solid(kEdge, 255, 128, 0, 255),
|
||||
3);
|
||||
}
|
||||
|
||||
// Control: RGBA16_SNORM is desktop-only as an image format, so this re-mint widens too.
|
||||
TEST_F(ImageBindableRemintScenario, Snorm16UploadSurvivesAWideningRemint) {
|
||||
if (!Ready()) return;
|
||||
const GLuint t = NewTexture();
|
||||
std::vector<GLshort> s(static_cast<std::size_t>(kEdge * kEdge) * 4u);
|
||||
for (std::size_t i = 0; i < s.size(); i += 4) {
|
||||
s[i] = 32767;
|
||||
s[i + 1] = 0;
|
||||
s[i + 2] = -32767;
|
||||
s[i + 3] = 32767;
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA16_SNORM, kEdge, kEdge);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RGBA, GL_SHORT, s.data());
|
||||
Nearest(GL_TEXTURE_2D, t, 0);
|
||||
SyncBySampling(t);
|
||||
ExpectImage(ImageCopy(t, 0, GL_RGBA16_SNORM, "rgba16_snorm", true), kEdge,
|
||||
Solid(kEdge, 255, 128, 0, 255), 3);
|
||||
}
|
||||
|
||||
// An array texture is read back one layer at a time: the GPU-cleared layer and the
|
||||
// uploaded one both have to come through. The clear's FBO attach is what gives the
|
||||
// texture its driver storage here.
|
||||
TEST_F(ImageBindableRemintScenario, GpuClearOfOneArrayLayerSurvivesBesideTheUploadedLayer) {
|
||||
if (!Ready()) return;
|
||||
const GLuint t = NewTexture();
|
||||
const auto p = Pattern(kEdge, 0x40);
|
||||
std::vector<GLubyte> layers(p);
|
||||
layers.insert(layers.end(), p.begin(), p.end());
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, t);
|
||||
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, kEdge, kEdge, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
layers.data());
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
|
||||
Nearest(GL_TEXTURE_2D_ARRAY, t, 0);
|
||||
GpuClear(t, 0, kGreen, /*layer=*/1);
|
||||
glFinish();
|
||||
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8", false, /*layer=*/1), kEdge, Solid(kEdge, 0, 255, 0, 255));
|
||||
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8", false, /*layer=*/0), kEdge, p);
|
||||
}
|
||||
|
||||
// A view owns no storage, so its first image binding is the VIEWED texture's transition,
|
||||
// and the GPU's texels there have to come through it. Needs glTextureView, which Espryt
|
||||
// only has where the ES driver does (not on llvmpipe).
|
||||
TEST_F(ImageBindableRemintScenario, GpuClearSurvivesTheFirstImageBindingOfAView) {
|
||||
if (!Ready()) return;
|
||||
const GLuint owner = NewTexture();
|
||||
const auto p = Pattern(kEdge, 0x40);
|
||||
glBindTexture(GL_TEXTURE_2D, owner);
|
||||
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge, kEdge);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RGBA, GL_UNSIGNED_BYTE, p.data());
|
||||
Nearest(GL_TEXTURE_2D, owner, 0);
|
||||
SyncBySampling(owner);
|
||||
GpuClear(owner, 0, kGreen);
|
||||
SyncBySampling(owner);
|
||||
const GLuint view = NewTexture();
|
||||
FirstGLError();
|
||||
glTextureView(view, GL_TEXTURE_2D, owner, GL_RGBA8, 0, 1, 0, 1);
|
||||
if (const GLenum error = FirstGLError()) {
|
||||
GTEST_SKIP() << "glTextureView is unavailable on backend " << Gl().BackendName() << " ("
|
||||
<< GLErrorName(error) << ")";
|
||||
}
|
||||
Nearest(GL_TEXTURE_2D, view, 0);
|
||||
SyncBySampling(view);
|
||||
ExpectImage(ImageCopy(view, 0, GL_RGBA8, "rgba8"), kEdge, Solid(kEdge, 0, 255, 0, 255));
|
||||
}
|
||||
} // namespace
|
||||
} // namespace MGITest
|
||||
@@ -0,0 +1,428 @@
|
||||
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/PartialUploadOverGpuContentScenario.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 SMALL glTexSubImage INTO A LEVEL THE GPU HAS WRITTEN.
|
||||
//
|
||||
// Espryt keeps a CPU shadow of every level and syncs a pending client write by uploading the
|
||||
// region it covers (SyncMipmapsToBackend's sub-rect path). The shadow never sees a GPU write, so
|
||||
// only the region may go up: anything more puts the shadow's stale texels over what the GPU drew.
|
||||
// The region upload used to be skipped whenever the shadow's bytes had to be CONVERTED on the way
|
||||
// to the driver - a three-channel format the driver cannot render is stored four-channel and every
|
||||
// upload is repacked - and whenever the ES upload is shaped differently from the shadow (a 1D
|
||||
// array, stored as a 2D array one texel high): those levels went up WHOLE, and a 2x2 write erased
|
||||
// the rest of the level.
|
||||
//
|
||||
// Every case uploads a level, lets the GPU clear it, writes one 2x2 box, and reads the level back:
|
||||
// the clear outside the box, the write inside. The formats are picked by what they cost the ES
|
||||
// upload, which is per driver - on Mesa llvmpipe GL_SRGB8, GL_RGB8_SNORM and GL_RGB8UI are stored
|
||||
// four-channel (see ThreeChannelAttachmentScenario) while GL_RGB16F is native; ES has no renderable
|
||||
// three-channel format at all, so on a phone all four are converted. GL_RGBA8 is the control that
|
||||
// never converts. A format a backend cannot attach is skipped, not failed.
|
||||
//
|
||||
// The second half is about ORDER, with GL_RGBA8. Magma queues a clear until the texture's next
|
||||
// use and records texel uploads into a batch submitted ahead of the frame's recorded commands, so
|
||||
// a write issued after a clear of (or a draw into) the level reached the image FIRST and the
|
||||
// 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.
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#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 kEdge = 8;
|
||||
constexpr int kBoxAt = 2;
|
||||
constexpr int kBoxEdge = 2;
|
||||
|
||||
constexpr const char* kVS = R"(#version 430 core
|
||||
void main() {
|
||||
vec2 positions[3] = vec2[3](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
|
||||
gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
// How a value in [0, 1] is spelled in the client data, and how the level is cleared and
|
||||
// read back. Integer levels spell 1 as kIntegerOne.
|
||||
enum class Encoding { Unorm8, Snorm8, Float32, Uint8 };
|
||||
constexpr unsigned kIntegerOne = 9;
|
||||
|
||||
struct Format {
|
||||
GLenum internalFormat;
|
||||
GLenum clientFormat;
|
||||
GLenum clientType;
|
||||
int components;
|
||||
Encoding encoding;
|
||||
};
|
||||
|
||||
std::vector<std::uint8_t> Fill(const Format& f, int texels, float r, float g, float b) {
|
||||
const float rgba[4] = {r, g, b, 1.0f};
|
||||
const std::size_t componentBytes = f.encoding == Encoding::Float32 ? 4u : 1u;
|
||||
std::vector<std::uint8_t> out(static_cast<std::size_t>(texels) * f.components * componentBytes);
|
||||
for (int t = 0; t < texels; ++t) {
|
||||
for (int c = 0; c < f.components; ++c) {
|
||||
std::uint8_t* dst = out.data() + (static_cast<std::size_t>(t) * f.components + c) * componentBytes;
|
||||
const float v = rgba[c];
|
||||
switch (f.encoding) {
|
||||
case Encoding::Unorm8: *dst = static_cast<std::uint8_t>(std::lround(v * 255.0f)); break;
|
||||
case Encoding::Snorm8:
|
||||
*dst = static_cast<std::uint8_t>(static_cast<std::int8_t>(std::lround(v * 127.0f)));
|
||||
break;
|
||||
case Encoding::Float32: std::memcpy(dst, &v, sizeof(v)); break;
|
||||
case Encoding::Uint8: *dst = static_cast<std::uint8_t>(std::lround(v * kIntegerOne)); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
class PartialUploadOverGpuContentScenario : public ScenarioTest {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
ScenarioTest::SetUp();
|
||||
if (!Ready()) return;
|
||||
m_scratch = MakeColorFbo(4, 4);
|
||||
glGenVertexArrays(1, &m_vao);
|
||||
glGenFramebuffers(1, &m_fbo);
|
||||
FirstGLError();
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
if (!Ready()) return;
|
||||
glUseProgram(0);
|
||||
for (GLuint p : m_programs) glDeleteProgram(p);
|
||||
if (!m_textures.empty()) glDeleteTextures(static_cast<GLsizei>(m_textures.size()), m_textures.data());
|
||||
if (m_vao) glDeleteVertexArrays(1, &m_vao);
|
||||
if (m_fbo) glDeleteFramebuffers(1, &m_fbo);
|
||||
BindDefaultFramebuffer();
|
||||
DestroyColorFbo(m_scratch);
|
||||
glViewport(0, 0, Gl().Width(), Gl().Height());
|
||||
FirstGLError();
|
||||
}
|
||||
|
||||
GLuint NewTexture(GLenum target) {
|
||||
GLuint t = 0;
|
||||
glGenTextures(1, &t);
|
||||
m_textures.push_back(t);
|
||||
glBindTexture(target, t);
|
||||
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 0);
|
||||
glBindTexture(target, 0);
|
||||
return t;
|
||||
}
|
||||
|
||||
// A draw that samples the texture, which is what syncs its pending writes to the
|
||||
// driver (an FBO attach alone may find the framebuffer already synced).
|
||||
void SyncBySampling(GLenum target, GLuint tex, const char* samplerType, const char* coord) {
|
||||
std::string fs = std::string("#version 430 core\nuniform ") + samplerType +
|
||||
" u_tex;\nout vec4 o_color;\nvoid main() { o_color = vec4(texelFetch(u_tex, " + coord +
|
||||
", 0)); }\n";
|
||||
std::string error;
|
||||
const GLuint program = CompileProgram(kVS, fs.c_str(), &error);
|
||||
ASSERT_NE(program, 0u) << error;
|
||||
m_programs.push_back(program);
|
||||
BindFbo(m_scratch);
|
||||
glUseProgram(program);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(target, tex);
|
||||
glUniform1i(glGetUniformLocation(program, "u_tex"), 0);
|
||||
glBindVertexArray(m_vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glBindVertexArray(0);
|
||||
glBindTexture(target, 0);
|
||||
glUseProgram(0);
|
||||
BindDefaultFramebuffer();
|
||||
glFinish();
|
||||
EXPECT_EQ(FirstGLError(), 0u) << "sampling sync";
|
||||
}
|
||||
|
||||
// Attaches (tex, layer) to the scratch framebuffer; false when this backend cannot.
|
||||
bool Attach(GLuint tex, int layer) {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
|
||||
if (layer >= 0) {
|
||||
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, 0, layer);
|
||||
} else {
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
|
||||
}
|
||||
return glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
|
||||
}
|
||||
|
||||
void Detach() {
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
|
||||
BindDefaultFramebuffer();
|
||||
}
|
||||
|
||||
// The GPU write the shadow never sees: the whole level (or layer) to green.
|
||||
void GpuClearGreen(const Format& f) {
|
||||
if (f.encoding == Encoding::Uint8) {
|
||||
const GLuint green[4] = {0u, kIntegerOne, 0u, 1u};
|
||||
glClearBufferuiv(GL_COLOR, 0, green);
|
||||
} else {
|
||||
const GLfloat green[4] = {0.0f, 1.0f, 0.0f, 1.0f};
|
||||
glClearBufferfv(GL_COLOR, 0, green);
|
||||
}
|
||||
}
|
||||
|
||||
// The attached image's rgb, in units of "one", width x height texels.
|
||||
std::vector<float> ReadRgb(const Format& f, int width, int height) {
|
||||
std::vector<float> rgb(static_cast<std::size_t>(width * height) * 3u);
|
||||
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
||||
if (f.encoding == Encoding::Uint8) {
|
||||
std::vector<GLuint> texels(static_cast<std::size_t>(width * height) * 4u, 0xA5A5A5A5u);
|
||||
glReadPixels(0, 0, width, height, GL_RGBA_INTEGER, GL_UNSIGNED_INT, texels.data());
|
||||
for (int i = 0; i < width * height; ++i)
|
||||
for (int c = 0; c < 3; ++c)
|
||||
rgb[i * 3 + c] = static_cast<float>(texels[i * 4 + c]) / kIntegerOne;
|
||||
} else {
|
||||
std::vector<GLfloat> texels(static_cast<std::size_t>(width * height) * 4u, -7.0f);
|
||||
glReadPixels(0, 0, width, height, GL_RGBA, GL_FLOAT, texels.data());
|
||||
for (int i = 0; i < width * height; ++i)
|
||||
for (int c = 0; c < 3; ++c) rgb[i * 3 + c] = texels[i * 4 + c];
|
||||
}
|
||||
EXPECT_EQ(FirstGLError(), 0u) << "readback";
|
||||
return rgb;
|
||||
}
|
||||
|
||||
// Green everywhere but blue inside [boxX, boxX + boxW) x [boxY, boxY + boxH).
|
||||
static void ExpectClearAroundBox(const std::vector<float>& rgb, int width, int height, int boxX, int boxY,
|
||||
int boxW, int boxH) {
|
||||
int bad = 0;
|
||||
std::ostringstream first;
|
||||
for (int y = 0; y < height; ++y)
|
||||
for (int x = 0; x < width; ++x) {
|
||||
const bool inBox = x >= boxX && x < boxX + boxW && y >= boxY && y < boxY + boxH;
|
||||
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 * width + x) * 3u];
|
||||
bool texelBad = false;
|
||||
for (int c = 0; c < 3; ++c) texelBad |= std::fabs(got[c] - want[c]) > 0.02f;
|
||||
if (texelBad && 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 " << width * height << " texels wrong; first " << first.str()
|
||||
<< " (red outside the box: the stale shadow went back up; green inside it: the "
|
||||
"write landed before the clear or draw)";
|
||||
}
|
||||
|
||||
// Upload red, sync, clear green on the GPU, sync, write a blue 2x2 box, sync, read.
|
||||
void Run2D(const Format& f) {
|
||||
const GLuint t = NewTexture(GL_TEXTURE_2D);
|
||||
const bool integer = f.encoding == Encoding::Uint8;
|
||||
const char* sampler = integer ? "usampler2D" : "sampler2D";
|
||||
const auto red = Fill(f, kEdge * kEdge, 1.0f, 0.0f, 0.0f);
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, static_cast<GLint>(f.internalFormat), kEdge, kEdge, 0, f.clientFormat,
|
||||
f.clientType, red.data());
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
ASSERT_EQ(FirstGLError(), 0u) << "definition";
|
||||
SyncBySampling(GL_TEXTURE_2D, t, sampler, "ivec2(0)");
|
||||
if (!Attach(t, -1)) {
|
||||
Detach();
|
||||
GTEST_SKIP() << "the format does not attach on backend " << Gl().BackendName();
|
||||
}
|
||||
GpuClearGreen(f);
|
||||
Detach();
|
||||
SyncBySampling(GL_TEXTURE_2D, t, sampler, "ivec2(0)");
|
||||
const auto blue = Fill(f, kBoxEdge * kBoxEdge, 0.0f, 0.0f, 1.0f);
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, kBoxAt, kBoxAt, kBoxEdge, kBoxEdge, f.clientFormat, f.clientType,
|
||||
blue.data());
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
||||
SyncBySampling(GL_TEXTURE_2D, t, sampler, "ivec2(0)");
|
||||
ASSERT_TRUE(Attach(t, -1));
|
||||
const auto rgb = ReadRgb(f, kEdge, kEdge);
|
||||
Detach();
|
||||
ExpectClearAroundBox(rgb, kEdge, kEdge, kBoxAt, kBoxAt, kBoxEdge, kBoxEdge);
|
||||
}
|
||||
|
||||
// Building blocks of the RGBA8 ordering cases: the same texels, issued in the orders
|
||||
// a backend that defers work may get wrong.
|
||||
static Format Rgba8() { return {GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 4, Encoding::Unorm8}; }
|
||||
|
||||
GLuint NewRedRgba8() {
|
||||
const GLuint t = NewTexture(GL_TEXTURE_2D);
|
||||
const auto red = Fill(Rgba8(), kEdge * kEdge, 1.0f, 0.0f, 0.0f);
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, red.data());
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
EXPECT_EQ(FirstGLError(), 0u) << "definition";
|
||||
return t;
|
||||
}
|
||||
|
||||
void SampleRgba8(GLuint t) { SyncBySampling(GL_TEXTURE_2D, t, "sampler2D", "ivec2(0)"); }
|
||||
|
||||
void ClearRgba8Green(GLuint t) {
|
||||
ASSERT_TRUE(Attach(t, -1));
|
||||
GpuClearGreen(Rgba8());
|
||||
Detach();
|
||||
}
|
||||
|
||||
// A draw that writes green over the whole level.
|
||||
void DrawRgba8Green(GLuint t) {
|
||||
std::string error;
|
||||
const GLuint program = CompileProgram(
|
||||
kVS, "#version 430 core\nout vec4 o_color;\nvoid main() { o_color = vec4(0.0, 1.0, 0.0, 1.0); }\n",
|
||||
&error);
|
||||
ASSERT_NE(program, 0u) << error;
|
||||
m_programs.push_back(program);
|
||||
ASSERT_TRUE(Attach(t, -1));
|
||||
glViewport(0, 0, kEdge, kEdge);
|
||||
glUseProgram(program);
|
||||
glBindVertexArray(m_vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glBindVertexArray(0);
|
||||
glUseProgram(0);
|
||||
Detach();
|
||||
glViewport(0, 0, Gl().Width(), Gl().Height());
|
||||
}
|
||||
|
||||
void WriteRgba8BlueBox(GLuint t) {
|
||||
const auto blue = Fill(Rgba8(), kBoxEdge * kBoxEdge, 0.0f, 0.0f, 1.0f);
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, kBoxAt, kBoxAt, kBoxEdge, kBoxEdge, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
blue.data());
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
// Green everywhere, and blue in the box when `boxWins`.
|
||||
void ExpectRgba8(GLuint t, bool boxWins) {
|
||||
ASSERT_TRUE(Attach(t, -1));
|
||||
const auto rgb = ReadRgb(Rgba8(), kEdge, kEdge);
|
||||
Detach();
|
||||
ExpectClearAroundBox(rgb, kEdge, kEdge, kBoxAt, kBoxAt, boxWins ? kBoxEdge : 0, boxWins ? kBoxEdge : 0);
|
||||
}
|
||||
|
||||
GLuint m_vao = 0, m_fbo = 0;
|
||||
ColorFbo m_scratch{};
|
||||
std::vector<GLuint> m_programs;
|
||||
std::vector<GLuint> m_textures;
|
||||
};
|
||||
|
||||
// Control: the shadow's bytes go to the driver as they are.
|
||||
TEST_F(PartialUploadOverGpuContentScenario, Rgba8SubImageKeepsTheGpuClear) {
|
||||
if (!Ready()) return;
|
||||
Run2D({GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 4, Encoding::Unorm8});
|
||||
}
|
||||
|
||||
TEST_F(PartialUploadOverGpuContentScenario, Srgb8SubImageKeepsTheGpuClear) {
|
||||
if (!Ready()) return;
|
||||
Run2D({GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE, 3, Encoding::Unorm8});
|
||||
}
|
||||
|
||||
TEST_F(PartialUploadOverGpuContentScenario, Rgb8SnormSubImageKeepsTheGpuClear) {
|
||||
if (!Ready()) return;
|
||||
Run2D({GL_RGB8_SNORM, GL_RGB, GL_BYTE, 3, Encoding::Snorm8});
|
||||
}
|
||||
|
||||
TEST_F(PartialUploadOverGpuContentScenario, Rgb16fSubImageKeepsTheGpuClear) {
|
||||
if (!Ready()) return;
|
||||
Run2D({GL_RGB16F, GL_RGB, GL_FLOAT, 3, Encoding::Float32});
|
||||
}
|
||||
|
||||
TEST_F(PartialUploadOverGpuContentScenario, Rgb8uiSubImageKeepsTheGpuClear) {
|
||||
if (!Ready()) return;
|
||||
Run2D({GL_RGB8UI, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, 3, Encoding::Uint8});
|
||||
}
|
||||
|
||||
// A 1D array's ES image is a 2D array one texel high with the layers in depth, so the
|
||||
// upload is shaped differently from the shadow (layers as rows) even with no conversion.
|
||||
TEST_F(PartialUploadOverGpuContentScenario, OneDimensionalArraySubImageKeepsTheGpuClearedLayer) {
|
||||
if (!Ready()) return;
|
||||
constexpr int kLayers = 2;
|
||||
const Format f{GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 4, Encoding::Unorm8};
|
||||
const GLuint t = NewTexture(GL_TEXTURE_1D_ARRAY);
|
||||
const auto red = Fill(f, kEdge * kLayers, 1.0f, 0.0f, 0.0f);
|
||||
glBindTexture(GL_TEXTURE_1D_ARRAY, t);
|
||||
glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, kEdge, kLayers, 0, GL_RGBA, GL_UNSIGNED_BYTE, red.data());
|
||||
glBindTexture(GL_TEXTURE_1D_ARRAY, 0);
|
||||
ASSERT_EQ(FirstGLError(), 0u) << "definition";
|
||||
SyncBySampling(GL_TEXTURE_1D_ARRAY, t, "sampler1DArray", "ivec2(0)");
|
||||
if (!Attach(t, 1)) {
|
||||
Detach();
|
||||
GTEST_SKIP() << "a 1D array layer does not attach on backend " << Gl().BackendName();
|
||||
}
|
||||
GpuClearGreen(f);
|
||||
Detach();
|
||||
SyncBySampling(GL_TEXTURE_1D_ARRAY, t, "sampler1DArray", "ivec2(0)");
|
||||
const auto blue = Fill(f, kBoxEdge, 0.0f, 0.0f, 1.0f);
|
||||
glBindTexture(GL_TEXTURE_1D_ARRAY, t);
|
||||
glTexSubImage2D(GL_TEXTURE_1D_ARRAY, 0, kBoxAt, 1, kBoxEdge, 1, GL_RGBA, GL_UNSIGNED_BYTE, blue.data());
|
||||
glBindTexture(GL_TEXTURE_1D_ARRAY, 0);
|
||||
SyncBySampling(GL_TEXTURE_1D_ARRAY, t, "sampler1DArray", "ivec2(0)");
|
||||
ASSERT_TRUE(Attach(t, 1));
|
||||
const auto layer1 = ReadRgb(f, kEdge, 1);
|
||||
Detach();
|
||||
ExpectClearAroundBox(layer1, kEdge, 1, kBoxAt, 0, kBoxEdge, 1);
|
||||
}
|
||||
|
||||
// The orders below matter to a backend that defers GPU work (Magma queues clears and runs
|
||||
// texel uploads in a batch ahead of the frame's recorded commands); every one is plain GL.
|
||||
|
||||
// The clear is still queued when the write arrives: it has to reach the level first.
|
||||
TEST_F(PartialUploadOverGpuContentScenario, SubImageAfterAQueuedClearLandsOnTheClear) {
|
||||
if (!Ready()) return;
|
||||
const GLuint t = NewRedRgba8();
|
||||
SampleRgba8(t);
|
||||
ClearRgba8Green(t);
|
||||
WriteRgba8BlueBox(t);
|
||||
SampleRgba8(t);
|
||||
ExpectRgba8(t, true);
|
||||
}
|
||||
|
||||
// Same, with the readback as the first thing to sync the write.
|
||||
TEST_F(PartialUploadOverGpuContentScenario, SubImageAfterAQueuedClearLandsOnTheClearForAReadback) {
|
||||
if (!Ready()) return;
|
||||
const GLuint t = NewRedRgba8();
|
||||
SampleRgba8(t);
|
||||
ClearRgba8Green(t);
|
||||
WriteRgba8BlueBox(t);
|
||||
ExpectRgba8(t, true);
|
||||
}
|
||||
|
||||
// The other order: the write is still pending when the clear arrives, and the clear wins.
|
||||
TEST_F(PartialUploadOverGpuContentScenario, ClearAfterAPendingSubImageWins) {
|
||||
if (!Ready()) return;
|
||||
const GLuint t = NewRedRgba8();
|
||||
SampleRgba8(t);
|
||||
WriteRgba8BlueBox(t);
|
||||
ClearRgba8Green(t);
|
||||
SampleRgba8(t);
|
||||
ExpectRgba8(t, false);
|
||||
}
|
||||
|
||||
// A draw into the level instead of a clear.
|
||||
TEST_F(PartialUploadOverGpuContentScenario, SubImageAfterADrawIntoTheLevelLandsOnTheDraw) {
|
||||
if (!Ready()) return;
|
||||
const GLuint t = NewRedRgba8();
|
||||
SampleRgba8(t);
|
||||
DrawRgba8Green(t);
|
||||
WriteRgba8BlueBox(t);
|
||||
SampleRgba8(t);
|
||||
ExpectRgba8(t, true);
|
||||
}
|
||||
} // namespace
|
||||
} // namespace MGITest
|
||||
Reference in New Issue
Block a user