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/Display.h"
19 #include "libANGLE/angletypes.h"
20 #include "libANGLE/renderer/vulkan/ContextVk.h"
21 #include "libANGLE/renderer/vulkan/ProgramVk.h"
22 #include "libANGLE/renderer/vulkan/TextureVk.h"
23 #include "test_utils/gl_raii.h"
24 #include "util/EGLWindow.h"
25 #include "util/shader_utils.h"
26 
27 using namespace angle;
28 
29 namespace
30 {
31 
32 class VulkanFramebufferTest : public ANGLETest<>
33 {
34   protected:
hackContext() const35     gl::Context *hackContext() const
36     {
37         egl::Display *display   = static_cast<egl::Display *>(getEGLWindow()->getDisplay());
38         gl::ContextID contextID = {
39             static_cast<GLuint>(reinterpret_cast<uintptr_t>(getEGLWindow()->getContext()))};
40         return display->getContext(contextID);
41     }
42 
hackANGLE() const43     rx::ContextVk *hackANGLE() const
44     {
45         // Hack the angle!
46         return rx::GetImplAs<rx::ContextVk>(hackContext());
47     }
48 
hackTexture(GLuint handle) const49     rx::TextureVk *hackTexture(GLuint handle) const
50     {
51         // Hack the angle!
52         const gl::Context *context = hackContext();
53         const gl::Texture *texture = context->getTexture({handle});
54         return rx::vk::GetImpl(texture);
55     }
56 };
57 
58 // Test that framebuffer can be created from a mip-incomplete texture, and that its allocation only
59 // includes the framebuffer's attached mip.
TEST_P(VulkanFramebufferTest,TextureAttachmentMipIncomplete)60 TEST_P(VulkanFramebufferTest, TextureAttachmentMipIncomplete)
61 {
62     GLFramebuffer framebuffer;
63     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
64 
65     GLTexture texture;
66     glBindTexture(GL_TEXTURE_2D, texture);
67 
68     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 100, 100, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
69     glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
70     glTexImage2D(GL_TEXTURE_2D, 2, GL_RGBA8, 5, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
71 
72     // Set framebuffer to mip 0.  Framebuffer should be complete, and make the texture allocate
73     // an image of only 1 level.
74     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
75     EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
76 
77     glClearColor(0, 0, 0, 1.0f);
78     glClear(GL_COLOR_BUFFER_BIT);
79     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
80 
81     // http://anglebug.com/4686: The Vulkan backend is allocating three mips of sizes 100x100,
82     // 50x50 and 25x25 instead of one mip of size 100x100.
83     ANGLE_SKIP_TEST_IF(IsVulkan());
84 
85     rx::TextureVk *textureVk = hackTexture(texture);
86     EXPECT_EQ(textureVk->getImage().getLevelCount(), 1u);
87 }
88 
89 // Test ensure that a R4G4B4A4 format sample only will actually uses R4G4B4A4 format instead of
90 // R8G8B8A8.
TEST_P(VulkanFramebufferTest,R4G4B4A4TextureSampleOnlyActuallyUses444Format)91 TEST_P(VulkanFramebufferTest, R4G4B4A4TextureSampleOnlyActuallyUses444Format)
92 {
93     rx::ContextVk *contextVk = hackANGLE();
94     rx::RendererVk *renderer = contextVk->getRenderer();
95     angle::FormatID formatID = angle::FormatID::R4G4B4A4_UNORM;
96 
97     // Check if R4G4B4A4_UNORM is supported format.
98     bool isTexturable = renderer->hasImageFormatFeatureBits(
99         formatID,
100         VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT);
101     ANGLE_SKIP_TEST_IF(!isTexturable);
102 
103     static constexpr GLsizei kTexWidth  = 100;
104     static constexpr GLsizei kTexHeight = 100;
105     GLTexture texture;
106     glBindTexture(GL_TEXTURE_2D, texture);
107 
108     const GLushort u16Color = 0xF00F;  // red
109     std::vector<GLushort> pixels(kTexWidth * kTexHeight, u16Color);
110     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, kTexWidth, kTexHeight, 0, GL_RGBA,
111                  GL_UNSIGNED_SHORT_4_4_4_4, pixels.data());
112     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
113     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
114     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Texture2DLod(), essl3_shaders::fs::Texture2DLod());
115     glUseProgram(program);
116     GLint textureLocation = glGetUniformLocation(program, essl3_shaders::Texture2DUniform());
117     ASSERT_NE(-1, textureLocation);
118     GLint lodLocation = glGetUniformLocation(program, essl3_shaders::LodUniform());
119     ASSERT_NE(-1, lodLocation);
120     glActiveTexture(GL_TEXTURE0);
121     glBindTexture(GL_TEXTURE_2D, texture);
122     glClearColor(0, 1, 0, 1);
123     glClear(GL_COLOR_BUFFER_BIT);
124     glUniform1f(lodLocation, 0);
125     drawQuad(program, essl3_shaders::PositionAttrib(), 0.5f);
126     EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, 255, 0, 0, 255);
127     ASSERT_GL_NO_ERROR();
128 
129     rx::TextureVk *textureVk = hackTexture(texture);
130     EXPECT_EQ(textureVk->getImage().getActualFormatID(), formatID);
131 }
132 
133 ANGLE_INSTANTIATE_TEST(VulkanFramebufferTest, ES3_VULKAN());
134 
135 }  // anonymous namespace
136