1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <GLES2/gl2extchromium.h>
#include <stddef.h>
#include <stdint.h>
#include <array>
#include <cmath>
#include <memory>
#include "base/bit_cast.h"
#include "base/containers/heap_array.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "gpu/command_buffer/tests/gl_manager.h"
#include "gpu/command_buffer/tests/gl_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gpu {
class GLReadbackTest : public testing::Test {
protected:
void SetUp() override { gl_.Initialize(GLManager::Options()); }
void TearDown() override { gl_.Destroy(); }
void WaitForQueryCallback(int q, base::OnceClosure cb) {
unsigned int done = 0;
gl_.PerformIdleWork();
glGetQueryObjectuivEXT(q, GL_QUERY_RESULT_AVAILABLE_EXT, &done);
if (done) {
std::move(cb).Run();
} else {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&GLReadbackTest::WaitForQueryCallback,
base::Unretained(this), q, std::move(cb)),
base::Milliseconds(3));
}
}
void WaitForQuery(int q) {
base::RunLoop run_loop;
WaitForQueryCallback(q, run_loop.QuitClosure());
run_loop.Run();
}
GLManager gl_;
};
TEST_F(GLReadbackTest, ReadPixelsWithPBOAndQuery) {
const GLint kBytesPerPixel = 4;
const GLint kWidth = 2;
const GLint kHeight = 2;
GLuint b, q;
glClearColor(0.0, 0.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glGenBuffers(1, &b);
glGenQueriesEXT(1, &q);
glBindBuffer(GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM, b);
glBufferData(GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM,
kWidth * kHeight * kBytesPerPixel, nullptr, GL_STREAM_READ);
glBeginQueryEXT(GL_ASYNC_PIXEL_PACK_COMPLETED_CHROMIUM, q);
glReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glEndQueryEXT(GL_ASYNC_PIXEL_PACK_COMPLETED_CHROMIUM);
glFlush();
WaitForQuery(q);
// TODO(hubbe): Check that glMapBufferCHROMIUM does not block here.
unsigned char *data = static_cast<unsigned char *>(
glMapBufferCHROMIUM(
GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM,
GL_READ_ONLY));
EXPECT_TRUE(data);
EXPECT_EQ(data[0], 0); // red
EXPECT_EQ(data[1], 0); // green
EXPECT_EQ(data[2], 255); // blue
glUnmapBufferCHROMIUM(GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM);
glBindBuffer(GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM, 0);
glDeleteBuffers(1, &b);
glDeleteQueriesEXT(1, &q);
GLTestHelper::CheckGLError("no errors", __LINE__);
}
static float HalfToFloat32(uint16_t value) {
int32_t s = (value >> 15) & 0x00000001;
int32_t e = (value >> 10) & 0x0000001f;
int32_t m = value & 0x000003ff;
if (e == 0) {
if (m == 0) {
uint32_t result = s << 31;
return base::bit_cast<float>(result);
} else {
while (!(m & 0x00000400)) {
m <<= 1;
e -= 1;
}
e += 1;
m &= ~0x00000400;
}
} else if (e == 31) {
if (m == 0) {
uint32_t result = (s << 31) | 0x7f800000;
return base::bit_cast<float>(result);
} else {
uint32_t result = (s << 31) | 0x7f800000 | (m << 13);
return base::bit_cast<float>(result);
}
}
e = e + (127 - 15);
m = m << 13;
uint32_t result = (s << 31) | (e << 23) | m;
return base::bit_cast<float>(result);
}
static GLuint CompileShader(GLenum type, const char *data) {
const char *shaderStrings[1] = { data };
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, shaderStrings, nullptr);
glCompileShader(shader);
GLint compile_status = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
if (compile_status != GL_TRUE) {
glDeleteShader(shader);
shader = 0;
}
return shader;
}
// TODO(zmo): ReadPixels with float type isn't implemented in ANGLE ES2
// backend. crbug.com/607283.
// TODO(zmo): This test also fails on some android devices when the readback
// type is HALF_FLOAT_OES. Likely it's due to a driver bug. crbug.com/607936.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)
#define MAYBE_ReadPixelsFloat DISABLED_ReadPixelsFloat
#else
#define MAYBE_ReadPixelsFloat ReadPixelsFloat
#endif
TEST_F(GLReadbackTest, MAYBE_ReadPixelsFloat) {
const GLsizei kTextureSize = 4;
const std::array<GLfloat, 4> kDrawColor = {-10.9f, 0.5f, 10.5f, 100.12f};
const GLfloat kEpsilon = 0.01f;
struct TestFormat {
GLint format;
GLint type;
uint32_t comp_count;
};
std::array<TestFormat, 4> test_formats;
size_t test_count = 0;
const char *extensions = reinterpret_cast<const char*>(
glGetString(GL_EXTENSIONS));
if (strstr(extensions, "GL_OES_texture_half_float") != nullptr) {
TestFormat rgb16f = {GL_RGB, GL_HALF_FLOAT_OES, 3};
test_formats[test_count++] = rgb16f;
TestFormat rgba16f = {GL_RGBA, GL_HALF_FLOAT_OES, 4};
test_formats[test_count++] = rgba16f;
}
if (strstr(extensions, "GL_OES_texture_float") != nullptr) {
TestFormat rgb32f = {GL_RGB, GL_FLOAT, 3};
test_formats[test_count++] = rgb32f;
TestFormat rgba32f = {GL_RGBA, GL_FLOAT, 4};
test_formats[test_count++] = rgba32f;
}
const char *vs_source =
"precision mediump float;\n"
"attribute vec4 a_position;\n"
"void main() {\n"
" gl_Position = a_position;\n"
"}\n";
GLuint vertex_shader = CompileShader(GL_VERTEX_SHADER, vs_source);
ASSERT_NE(vertex_shader, GLuint(0));
const char *fs_source =
"precision mediump float;\n"
"uniform vec4 u_color;\n"
"void main() {\n"
" gl_FragColor = u_color;\n"
"}\n";
GLuint fragment_shader = CompileShader(GL_FRAGMENT_SHADER, fs_source);
ASSERT_NE(fragment_shader, GLuint(0));
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glDeleteShader(vertex_shader);
glAttachShader(program, fragment_shader);
glDeleteShader(fragment_shader);
glLinkProgram(program);
GLint link_status = 0;
glGetProgramiv(program, GL_LINK_STATUS, &link_status);
if (link_status != GL_TRUE) {
glDeleteProgram(program);
program = 0;
}
ASSERT_NE(program, GLuint(0));
EXPECT_EQ(glGetError(), GLenum(GL_NO_ERROR));
float quad_vertices[] = {
-1.0, -1.0,
1.0, -1.0,
1.0, 1.0,
-1.0, 1.0
};
GLuint vertex_buffer;
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(
GL_ARRAY_BUFFER, sizeof(quad_vertices),
reinterpret_cast<void*>(quad_vertices), GL_STATIC_DRAW);
GLint position_location = glGetAttribLocation(program, "a_position");
glVertexAttribPointer(position_location, 2, GL_FLOAT, GL_FALSE,
2 * sizeof(float), nullptr);
glEnableVertexAttribArray(position_location);
glUseProgram(program);
glUniform4fv(glGetUniformLocation(program, "u_color"), 1, kDrawColor.data());
EXPECT_EQ(glGetError(), GLenum(GL_NO_ERROR));
for (size_t ii = 0; ii < test_count; ++ii) {
GLuint texture_id = 0;
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, test_formats[ii].format, kTextureSize,
kTextureSize, 0, test_formats[ii].format,
test_formats[ii].type, nullptr);
GLuint framebuffer = 0;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_id, 0);
EXPECT_EQ(glGetError(), GLenum(GL_NO_ERROR));
// Make sure this floating point framebuffer is supported
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) {
// Check if this implementation supports reading floats back from this
// framebuffer
GLint read_format = 0;
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &read_format);
GLint read_type = 0;
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &read_type);
EXPECT_EQ(glGetError(), GLenum(GL_NO_ERROR));
if ((read_format == GL_RGB || read_format == GL_RGBA) &&
read_type == test_formats[ii].type) {
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
uint32_t read_comp_count = 0;
switch (read_format) {
case GL_RGB:
read_comp_count = 3;
break;
case GL_RGBA:
read_comp_count = 4;
break;
}
switch (read_type) {
case GL_HALF_FLOAT_OES: {
auto buf = base::HeapArray<GLushort>::Uninit(
kTextureSize * kTextureSize * read_comp_count);
glReadPixels(0, 0, kTextureSize, kTextureSize, read_format,
read_type, buf.data());
EXPECT_EQ(glGetError(), GLenum(GL_NO_ERROR));
for (uint32_t jj = 0; jj < kTextureSize * kTextureSize; ++jj) {
for (uint32_t kk = 0; kk < test_formats[ii].comp_count; ++kk) {
EXPECT_LE(
std::abs(HalfToFloat32(buf[jj * read_comp_count + kk]) -
kDrawColor[kk]),
std::abs(kDrawColor[kk] * kEpsilon));
}
}
break;
}
case GL_FLOAT: {
auto buf = base::HeapArray<GLfloat>::Uninit(
kTextureSize * kTextureSize * read_comp_count);
glReadPixels(0, 0, kTextureSize, kTextureSize, read_format,
read_type, buf.data());
EXPECT_EQ(glGetError(), GLenum(GL_NO_ERROR));
for (uint32_t jj = 0; jj < kTextureSize * kTextureSize; ++jj) {
for (uint32_t kk = 0; kk < test_formats[ii].comp_count; ++kk) {
EXPECT_LE(
std::abs(buf[jj * read_comp_count + kk] - kDrawColor[kk]),
std::abs(kDrawColor[kk] * kEpsilon));
}
}
break;
}
}
}
}
glDeleteFramebuffers(1, &framebuffer);
glDeleteTextures(1, &texture_id);
}
glDeleteBuffers(1, &vertex_buffer);
glDeleteProgram(program);
}
} // namespace gpu
|