Hash :
6c1021ec
        
        Author :
  
        
        Date :
2024-11-22T16:48:45
        
      
Vulkan: Switch DescriptorSetLayout to use AtomicSharedPtr SharedPtr has better semantics and safer to use. This CL removes direct exposure of RefCounted object and also allows me to delete BindingPointer class in later CL. Bug: angleproject:372268711 Change-Id: I08a0dff3efcf794be843a4a548b9f2609bb9a5e1 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6044328 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Yuxin Hu <yuxinhu@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
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
//
// Copyright 2020 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// VulkanDescriptorSetTest:
//   Various tests related for Vulkan descriptor sets.
//
#include "test_utils/ANGLETest.h"
#include "test_utils/gl_raii.h"
#include "libANGLE/Context.h"
#include "libANGLE/Display.h"
#include "libANGLE/angletypes.h"
#include "libANGLE/renderer/vulkan/ContextVk.h"
#include "libANGLE/renderer/vulkan/ProgramVk.h"
#include "libANGLE/renderer/vulkan/vk_helpers.h"
using namespace angle;
namespace
{
class VulkanDescriptorSetTest : public ANGLETest<>
{
  protected:
    VulkanDescriptorSetTest() {}
    void testSetUp() override
    {
        mMaxSetsPerPool = rx::vk::DynamicDescriptorPool::GetMaxSetsPerPoolForTesting();
        mMaxSetsPerPoolMultiplier =
            rx::vk::DynamicDescriptorPool::GetMaxSetsPerPoolMultiplierForTesting();
    }
    void testTearDown() override
    {
        rx::vk::DynamicDescriptorPool::SetMaxSetsPerPoolForTesting(mMaxSetsPerPool);
        rx::vk::DynamicDescriptorPool::SetMaxSetsPerPoolMultiplierForTesting(
            mMaxSetsPerPoolMultiplier);
    }
    static constexpr uint32_t kMaxSetsForTesting           = 1;
    static constexpr uint32_t kMaxSetsMultiplierForTesting = 1;
    void limitMaxSets()
    {
        rx::vk::DynamicDescriptorPool::SetMaxSetsPerPoolForTesting(kMaxSetsForTesting);
        rx::vk::DynamicDescriptorPool::SetMaxSetsPerPoolMultiplierForTesting(
            kMaxSetsMultiplierForTesting);
    }
  private:
    uint32_t mMaxSetsPerPool;
    uint32_t mMaxSetsPerPoolMultiplier;
};
// Test atomic counter read.
TEST_P(VulkanDescriptorSetTest, AtomicCounterReadLimitedDescriptorPool)
{
    // Skipping test while we work on enabling atomic counter buffer support in th D3D renderer.
    // http://anglebug.com/42260658
    ANGLE_SKIP_TEST_IF(IsD3D11());
    // Must be before program creation to limit the descriptor pool sizes when creating the pipeline
    // layout.
    limitMaxSets();
    constexpr char kFS[] =
        "#version 310 es\n"
        "precision highp float;\n"
        "layout(binding = 0, offset = 4) uniform atomic_uint ac;\n"
        "out highp vec4 my_color;\n"
        "void main()\n"
        "{\n"
        "    my_color = vec4(0.0);\n"
        "    uint a1 = atomicCounter(ac);\n"
        "    if (a1 == 3u) my_color = vec4(1.0);\n"
        "}\n";
    ANGLE_GL_PROGRAM(program, essl31_shaders::vs::Simple(), kFS);
    glUseProgram(program);
    // The initial value of counter 'ac' is 3u.
    unsigned int bufferData[3] = {11u, 3u, 1u};
    GLBuffer atomicCounterBuffer;
    glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, atomicCounterBuffer);
    glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0, atomicCounterBuffer);
    for (int i = 0; i < 5; ++i)
    {
        glBufferData(GL_ATOMIC_COUNTER_BUFFER, sizeof(bufferData), bufferData, GL_STATIC_DRAW);
        drawQuad(program, essl31_shaders::PositionAttrib(), 0.0f);
        ASSERT_GL_NO_ERROR();
        EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::white);
    }
}
class VulkanDescriptorSetLayoutDescTest : public ANGLETest<>
{
  protected:
    VulkanDescriptorSetLayoutDescTest() {}
    void testSetUp() override { ANGLETest::testSetUp(); }
    void testTearDown() override { ANGLETest::testTearDown(); }
    gl::Context *hackContext() const
    {
        egl::Display *display   = static_cast<egl::Display *>(getEGLWindow()->getDisplay());
        gl::ContextID contextID = {
            static_cast<GLuint>(reinterpret_cast<uintptr_t>(getEGLWindow()->getContext()))};
        return display->getContext(contextID);
    }
    rx::ContextVk *hackANGLE() const
    {
        // Hack the angle!
        return rx::GetImplAs<rx::ContextVk>(hackContext());
    }
    struct DescriptorSetBinding
    {
        uint32_t bindingIndex;
        VkDescriptorType type;
        uint32_t bindingCount;
        VkShaderStageFlagBits shaderStage;
    };
    const std::array<DescriptorSetBinding, 12> mBindings = {{
        {0, VK_DESCRIPTOR_TYPE_SAMPLER, 1, VK_SHADER_STAGE_VERTEX_BIT},
        {1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
        {2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT},
        {3, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
        {4, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1, VK_SHADER_STAGE_VERTEX_BIT},
        {5, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
        {6, VK_DESCRIPTOR_TYPE_SAMPLER, 1, VK_SHADER_STAGE_VERTEX_BIT},
        {7, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
        {8, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT},
        {9, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
        {10, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1, VK_SHADER_STAGE_VERTEX_BIT},
        {11, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
    }};
    void addBindings(const std::vector<uint32_t> &bindingIndices,
                     rx::vk::DescriptorSetLayoutDesc *desc)
    {
        for (uint32_t index : bindingIndices)
        {
            ASSERT(index < mBindings.size());
            const DescriptorSetBinding &binding = mBindings[index];
            desc->addBinding(binding.bindingIndex, binding.type, binding.bindingCount,
                             binding.shaderStage, nullptr);
        }
    }
    rx::vk::DescriptorSetLayoutDesc mDescriptorSetLayoutDesc;
    rx::DescriptorSetLayoutCache mDescriptorSetLayoutCache;
};
// Test basic interaction between DescriptorSetLayoutDesc and DescriptorSetLayoutCache
TEST_P(VulkanDescriptorSetLayoutDescTest, Basic)
{
    const std::vector<uint32_t> bindingsPattern1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    const std::vector<uint32_t> bindingsPattern2 = {0, 1};
    const std::vector<uint32_t> bindingsPattern3 = {0, 1, 5, 9};
    angle::Result result;
    rx::ContextVk *contextVk = hackANGLE();
    rx::vk::DescriptorSetLayoutPtr descriptorSetLayout;
    mDescriptorSetLayoutDesc = {};
    addBindings(bindingsPattern1, &mDescriptorSetLayoutDesc);
    result = mDescriptorSetLayoutCache.getDescriptorSetLayout(contextVk, mDescriptorSetLayoutDesc,
                                                              &descriptorSetLayout);
    EXPECT_EQ(result, angle::Result::Continue);
    EXPECT_EQ(mDescriptorSetLayoutCache.getCacheMissCount(), 1u);
    mDescriptorSetLayoutDesc = {};
    addBindings(bindingsPattern2, &mDescriptorSetLayoutDesc);
    result = mDescriptorSetLayoutCache.getDescriptorSetLayout(contextVk, mDescriptorSetLayoutDesc,
                                                              &descriptorSetLayout);
    EXPECT_EQ(result, angle::Result::Continue);
    EXPECT_EQ(mDescriptorSetLayoutCache.getCacheMissCount(), 2u);
    mDescriptorSetLayoutDesc = {};
    addBindings(bindingsPattern3, &mDescriptorSetLayoutDesc);
    size_t reusedDescHash = mDescriptorSetLayoutDesc.hash();
    result = mDescriptorSetLayoutCache.getDescriptorSetLayout(contextVk, mDescriptorSetLayoutDesc,
                                                              &descriptorSetLayout);
    EXPECT_EQ(result, angle::Result::Continue);
    EXPECT_EQ(mDescriptorSetLayoutCache.getCacheMissCount(), 3u);
    rx::vk::DescriptorSetLayoutDesc desc;
    addBindings(bindingsPattern3, &desc);
    size_t newDescHash = desc.hash();
    EXPECT_EQ(reusedDescHash, newDescHash);
    result =
        mDescriptorSetLayoutCache.getDescriptorSetLayout(contextVk, desc, &descriptorSetLayout);
    EXPECT_EQ(result, angle::Result::Continue);
    EXPECT_EQ(mDescriptorSetLayoutCache.getCacheHitCount(), 1u);
    EXPECT_EQ(mDescriptorSetLayoutCache.getCacheMissCount(), 3u);
    descriptorSetLayout.reset();
    mDescriptorSetLayoutCache.destroy(contextVk->getRenderer());
}
ANGLE_INSTANTIATE_TEST(VulkanDescriptorSetTest, ES31_VULKAN(), ES31_VULKAN_SWIFTSHADER());
ANGLE_INSTANTIATE_TEST(VulkanDescriptorSetLayoutDescTest, ES31_VULKAN());
}  // namespace