1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/command_line.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "gpu/command_buffer/tests/gl_manager.h"
10 #include "gpu/command_buffer/tests/gl_test_utils.h"
11 #include "gpu/config/gpu_switches.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 const GLenum kCubeMapTextureTargets
[] = {
18 GL_TEXTURE_CUBE_MAP_POSITIVE_X
, GL_TEXTURE_CUBE_MAP_NEGATIVE_X
,
19 GL_TEXTURE_CUBE_MAP_POSITIVE_Y
, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
,
20 GL_TEXTURE_CUBE_MAP_POSITIVE_Z
, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
,
24 // A collection of tests that exercise the cube map texture.
25 class GLCubeMapTextureTest
: public testing::TestWithParam
<GLenum
> {
27 void SetUp() override
{
28 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
29 // ANGLE and NVidia fails ReadPixelsFromIncompleteCubeTexture without this
31 command_line
.AppendSwitchASCII(switches::kGpuDriverBugWorkarounds
,
32 base::IntToString(gpu::FORCE_CUBE_COMPLETE
));
33 gl_
.InitializeWithCommandLine(GLManager::Options(), &command_line
);
34 DCHECK(gl_
.workarounds().force_cube_complete
);
35 for (int i
= 0; i
< 256; i
++) {
36 pixels_
[i
* 4] = 255u;
37 pixels_
[(i
* 4) + 1] = 0;
38 pixels_
[(i
* 4) + 2] = 0;
39 pixels_
[(i
* 4) + 3] = 255u;
42 glGenTextures(1, &texture_
);
43 glBindTexture(GL_TEXTURE_CUBE_MAP
, texture_
);
44 EXPECT_EQ(static_cast<GLenum
>(GL_NO_ERROR
), glGetError());
46 glTexParameterf(GL_TEXTURE_CUBE_MAP
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
47 glTexParameterf(GL_TEXTURE_CUBE_MAP
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
48 glTexParameteri(GL_TEXTURE_CUBE_MAP
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
49 glTexParameteri(GL_TEXTURE_CUBE_MAP
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
50 EXPECT_EQ(static_cast<GLenum
>(GL_NO_ERROR
), glGetError());
52 glGenFramebuffers(1, &framebuffer_id_
);
55 void TearDown() override
{
56 glDeleteTextures(1, &texture_
);
57 glDeleteFramebuffers(1, &framebuffer_id_
);
62 uint8 pixels_
[256 * 4];
63 const int width_
= 16;
65 GLuint framebuffer_id_
;
68 INSTANTIATE_TEST_CASE_P(GLCubeMapTextureTests
,
70 ::testing::ValuesIn(kCubeMapTextureTargets
));
72 TEST_P(GLCubeMapTextureTest
, TexImage2DAfterFBOBinding
) {
73 GLenum cube_map_target
= GetParam();
75 glBindFramebuffer(GL_FRAMEBUFFER
, framebuffer_id_
);
76 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, cube_map_target
,
78 EXPECT_EQ(static_cast<GLenum
>(GL_NO_ERROR
), glGetError());
80 glBindTexture(GL_TEXTURE_CUBE_MAP
, texture_
);
81 // force_gl_finish_after_compositing workaround prevents Nexus 5 crash.
82 // TODO(dshwang): remove the workaround when it's fixed. crbug.com/518889
83 glTexImage2D(cube_map_target
, 0, GL_RGBA
, width_
, width_
, 0, GL_RGBA
,
84 GL_UNSIGNED_BYTE
, pixels_
);
85 EXPECT_EQ(static_cast<GLenum
>(GL_NO_ERROR
), glGetError());
88 TEST_P(GLCubeMapTextureTest
, ReadPixels
) {
89 GLenum cube_map_target
= GetParam();
91 glBindTexture(GL_TEXTURE_CUBE_MAP
, texture_
);
92 EXPECT_EQ(static_cast<GLenum
>(GL_NO_ERROR
), glGetError());
94 // Make a cube texture complete
95 for (unsigned i
= 0; i
< arraysize(kCubeMapTextureTargets
); i
++) {
96 glTexImage2D(kCubeMapTextureTargets
[i
], 0, GL_RGBA
, width_
, width_
, 0,
97 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels_
);
98 EXPECT_EQ(static_cast<GLenum
>(GL_NO_ERROR
), glGetError());
101 glBindFramebuffer(GL_FRAMEBUFFER
, framebuffer_id_
);
102 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, cube_map_target
,
104 EXPECT_EQ(static_cast<GLenum
>(GL_NO_ERROR
), glGetError());
106 // Check that FB is complete.
107 EXPECT_EQ(static_cast<GLenum
>(GL_FRAMEBUFFER_COMPLETE
),
108 glCheckFramebufferStatus(GL_FRAMEBUFFER
));
110 GLTestHelper::CheckPixels(0, 0, width_
, width_
, 0, pixels_
);
111 EXPECT_EQ(static_cast<GLenum
>(GL_NO_ERROR
), glGetError());
115 TEST_P(GLCubeMapTextureTest
, ReadPixelsFromIncompleteCubeTexture
) {
116 GLenum cube_map_target
= GetParam();
118 glBindTexture(GL_TEXTURE_CUBE_MAP
, texture_
);
119 EXPECT_EQ(static_cast<GLenum
>(GL_NO_ERROR
), glGetError());
121 glTexImage2D(cube_map_target
, 0, GL_RGBA
, width_
, width_
, 0, GL_RGBA
,
122 GL_UNSIGNED_BYTE
, pixels_
);
123 EXPECT_EQ(static_cast<GLenum
>(GL_NO_ERROR
), glGetError());
125 glBindFramebuffer(GL_FRAMEBUFFER
, framebuffer_id_
);
126 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, cube_map_target
,
129 // force_gl_finish_after_compositing workaround prevents Nexus 5 crash.
130 // TODO(dshwang): remove the workaround when it's fixed. crbug.com/518889
131 EXPECT_EQ(static_cast<GLenum
>(GL_NO_ERROR
), glGetError());
133 // Check that FB is complete.
134 EXPECT_EQ(static_cast<GLenum
>(GL_FRAMEBUFFER_COMPLETE
),
135 glCheckFramebufferStatus(GL_FRAMEBUFFER
));
136 #if defined(OS_ANDROID)
137 // No way to workaround on Android NVIDIA drivers. Users have to texImage2D
138 // by (POSITIVE_X, NEGATIVE_Z) order only once. If users call texImage2D again
139 // after defining all faces, glReadPixels fails.
140 GLsizei size
= width_
* width_
* 4;
141 scoped_ptr
<uint8
[]> pixels(new uint8
[size
]);
142 glReadPixels(0, 0, width_
, width_
, GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
.get());
144 // Without force_cube_complete workaround,
145 // 1. ANGLE crashes on glReadPixels() from incomplete cube texture.
146 // 2. NVidia fails on glReadPixels() from incomplete cube texture.
147 GLTestHelper::CheckPixels(0, 0, width_
, width_
, 0, pixels_
);
149 EXPECT_EQ(static_cast<GLenum
>(GL_NO_ERROR
), glGetError());