• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2020 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // VulkanFramebufferTest:
7 //   Tests to validate our Vulkan framebuffer image allocation.
8 //
9 
10 #include "test_utils/ANGLETest.h"
11 #include "test_utils/angle_test_instantiate.h"
12 // 'None' is defined as 'struct None {};' in
13 // third_party/googletest/src/googletest/include/gtest/internal/gtest-type-util.h.
14 // But 'None' is also defined as a numeric constant 0L in <X11/X.h>.
15 // So we need to include ANGLETest.h first to avoid this conflict.
16 
17 #include "libANGLE/Context.h"
18 #include "libANGLE/angletypes.h"
19 #include "libANGLE/renderer/vulkan/ContextVk.h"
20 #include "libANGLE/renderer/vulkan/ProgramVk.h"
21 #include "libANGLE/renderer/vulkan/TextureVk.h"
22 #include "test_utils/gl_raii.h"
23 #include "util/EGLWindow.h"
24 #include "util/shader_utils.h"
25 
26 using namespace angle;
27 
28 namespace
29 {
30 
31 class VulkanFramebufferTest : public ANGLETest
32 {
33   protected:
hackANGLE() const34     rx::ContextVk *hackANGLE() const
35     {
36         // Hack the angle!
37         const gl::Context *context = static_cast<gl::Context *>(getEGLWindow()->getContext());
38         return rx::GetImplAs<rx::ContextVk>(context);
39     }
40 
hackTexture(GLuint handle) const41     rx::TextureVk *hackTexture(GLuint handle) const
42     {
43         // Hack the angle!
44         const gl::Context *context = static_cast<gl::Context *>(getEGLWindow()->getContext());
45         const gl::Texture *texture = context->getTexture({handle});
46         return rx::vk::GetImpl(texture);
47     }
48 };
49 
50 // Test that framebuffer can be created from a mip-incomplete texture, and that its allocation only
51 // includes the framebuffer's attached mip.
TEST_P(VulkanFramebufferTest,TextureAttachmentMipIncomplete)52 TEST_P(VulkanFramebufferTest, TextureAttachmentMipIncomplete)
53 {
54     GLFramebuffer framebuffer;
55     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
56 
57     GLTexture texture;
58     glBindTexture(GL_TEXTURE_2D, texture);
59 
60     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 100, 100, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
61     glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
62     glTexImage2D(GL_TEXTURE_2D, 2, GL_RGBA8, 5, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
63 
64     // Set framebuffer to mip 0.  Framebuffer should be complete, and make the texture allocate
65     // an image of only 1 level.
66     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
67     EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
68 
69     glClearColor(0, 0, 0, 1.0f);
70     glClear(GL_COLOR_BUFFER_BIT);
71     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
72 
73     // http://anglebug.com/4686: The Vulkan backend is allocating three mips of sizes 100x100,
74     // 50x50 and 25x25 instead of one mip of size 100x100.
75     ANGLE_SKIP_TEST_IF(IsVulkan());
76 
77     rx::TextureVk *textureVk = hackTexture(texture);
78     EXPECT_EQ(textureVk->getImage().getLevelCount(), 1u);
79 }
80 
81 // Test ensure that a R4G4B4A4 format sample only will actually uses R4G4B4A4 format instead of
82 // R8G8B8A8.
TEST_P(VulkanFramebufferTest,R4G4B4A4TextureSampleOnlyActuallyUses444Format)83 TEST_P(VulkanFramebufferTest, R4G4B4A4TextureSampleOnlyActuallyUses444Format)
84 {
85     rx::ContextVk *contextVk = hackANGLE();
86     rx::RendererVk *renderer = contextVk->getRenderer();
87     angle::FormatID formatID = angle::FormatID::R4G4B4A4_UNORM;
88 
89     // Check if R4G4B4A4_UNORM is supported format.
90     bool isTexturable = renderer->hasImageFormatFeatureBits(
91         formatID,
92         VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT);
93     ANGLE_SKIP_TEST_IF(!isTexturable);
94 
95     static constexpr GLsizei kTexWidth  = 100;
96     static constexpr GLsizei kTexHeight = 100;
97     GLTexture texture;
98     glBindTexture(GL_TEXTURE_2D, texture);
99 
100     const GLushort u16Color = 0xF00F;  // red
101     std::vector<GLushort> pixels(kTexWidth * kTexHeight, u16Color);
102     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, kTexWidth, kTexHeight, 0, GL_RGBA,
103                  GL_UNSIGNED_SHORT_4_4_4_4, pixels.data());
104     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
105     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
106     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Texture2DLod(), essl3_shaders::fs::Texture2DLod());
107     glUseProgram(program);
108     GLint textureLocation = glGetUniformLocation(program, essl3_shaders::Texture2DUniform());
109     ASSERT_NE(-1, textureLocation);
110     GLint lodLocation = glGetUniformLocation(program, essl3_shaders::LodUniform());
111     ASSERT_NE(-1, lodLocation);
112     glActiveTexture(GL_TEXTURE0);
113     glBindTexture(GL_TEXTURE_2D, texture);
114     glClearColor(0, 1, 0, 1);
115     glClear(GL_COLOR_BUFFER_BIT);
116     glUniform1f(lodLocation, 0);
117     drawQuad(program, essl3_shaders::PositionAttrib(), 0.5f);
118     EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, 255, 0, 0, 255);
119     ASSERT_GL_NO_ERROR();
120 
121     rx::TextureVk *textureVk = hackTexture(texture);
122     EXPECT_EQ(textureVk->getImage().getActualFormatID(), formatID);
123 }
124 
125 ANGLE_INSTANTIATE_TEST(VulkanFramebufferTest, ES3_VULKAN());
126 
127 }  // anonymous namespace
128