• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2018 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 // VulkanPipelineCachePerf:
7 //   Performance benchmark for the Vulkan Pipeline cache.
8 
9 #include "ANGLEPerfTest.h"
10 
11 #include "libANGLE/renderer/vulkan/vk_cache_utils.h"
12 #include "libANGLE/renderer/vulkan/vk_helpers.h"
13 #include "libANGLE/renderer/vulkan/vk_renderer.h"
14 #include "util/random_utils.h"
15 
16 using namespace rx;
17 
18 namespace
19 {
20 constexpr unsigned int kIterationsPerStep = 100;
21 
22 struct Params
23 {
24     bool withDynamicState = false;
25 };
26 
27 class VulkanPipelineCachePerfTest : public ANGLEPerfTest,
28                                     public ::testing::WithParamInterface<Params>
29 {
30   public:
31     VulkanPipelineCachePerfTest();
32     ~VulkanPipelineCachePerfTest();
33 
34     void SetUp() override;
35     void step() override;
36 
37     GraphicsPipelineCache<GraphicsPipelineDescCompleteHash> mCache;
38     angle::RNG mRNG;
39 
40     std::vector<vk::GraphicsPipelineDesc> mCacheHits;
41     std::vector<vk::GraphicsPipelineDesc> mCacheMisses;
42     size_t mMissIndex = 0;
43 
44   private:
45     void randomizeDesc(vk::GraphicsPipelineDesc *desc);
46 };
47 
VulkanPipelineCachePerfTest()48 VulkanPipelineCachePerfTest::VulkanPipelineCachePerfTest()
49     : ANGLEPerfTest("VulkanPipelineCachePerf", "", "", kIterationsPerStep), mRNG(0x12345678u)
50 {}
51 
~VulkanPipelineCachePerfTest()52 VulkanPipelineCachePerfTest::~VulkanPipelineCachePerfTest()
53 {
54     mCache.reset();
55 }
56 
SetUp()57 void VulkanPipelineCachePerfTest::SetUp()
58 {
59     ANGLEPerfTest::SetUp();
60 
61     // Insert a number of random pipeline states.
62     for (int pipelineCount = 0; pipelineCount < 100; ++pipelineCount)
63     {
64         vk::Pipeline pipeline;
65         vk::GraphicsPipelineDesc desc;
66         randomizeDesc(&desc);
67 
68         if (pipelineCount < 10)
69         {
70             mCacheHits.push_back(desc);
71         }
72         mCache.populate(desc, std::move(pipeline), nullptr);
73     }
74 
75     for (int missCount = 0; missCount < 10000; ++missCount)
76     {
77         vk::GraphicsPipelineDesc desc;
78         randomizeDesc(&desc);
79         mCacheMisses.push_back(desc);
80     }
81 }
82 
randomizeDesc(vk::GraphicsPipelineDesc * desc)83 void VulkanPipelineCachePerfTest::randomizeDesc(vk::GraphicsPipelineDesc *desc)
84 {
85     std::vector<uint8_t> bytes(sizeof(vk::GraphicsPipelineDesc));
86     FillVectorWithRandomUBytes(&mRNG, &bytes);
87     memcpy(desc, bytes.data(), sizeof(vk::GraphicsPipelineDesc));
88 
89     desc->setSupportsDynamicStateForTest(GetParam().withDynamicState);
90 }
91 
step()92 void VulkanPipelineCachePerfTest::step()
93 {
94     vk::RenderPass rp;
95     vk::PipelineLayout pl;
96     vk::PipelineCache pc;
97     vk::PipelineCacheAccess spc;
98     vk::RefCounted<vk::ShaderModule> vsRefCounted;
99     vk::RefCounted<vk::ShaderModule> fsRefCounted;
100     vk::ShaderModuleMap ssm;
101     const vk::GraphicsPipelineDesc *desc = nullptr;
102     vk::PipelineHelper *result           = nullptr;
103 
104     // The Vulkan handle types are difficult to cast to without #ifdefs.
105     VkShaderModule vs = (VkShaderModule)1;
106     VkShaderModule fs = (VkShaderModule)2;
107 
108     vsRefCounted.get().setHandle(vs);
109     fsRefCounted.get().setHandle(fs);
110 
111     ssm[gl::ShaderType::Vertex].set(&vsRefCounted);
112     ssm[gl::ShaderType::Fragment].set(&fsRefCounted);
113 
114     spc.init(&pc, nullptr);
115 
116     vk::SpecializationConstants defaultSpecConsts{};
117 
118     for (unsigned int iteration = 0; iteration < kIterationsPerStep; ++iteration)
119     {
120         for (const auto &hit : mCacheHits)
121         {
122             if (!mCache.getPipeline(hit, &desc, &result))
123             {
124                 (void)mCache.createPipeline(VK_NULL_HANDLE, &spc, rp, pl, ssm, defaultSpecConsts,
125                                             PipelineSource::Draw, hit, &desc, &result);
126             }
127         }
128     }
129 
130     for (int missCount = 0; missCount < 20 && mMissIndex < mCacheMisses.size();
131          ++missCount, ++mMissIndex)
132     {
133         const auto &miss = mCacheMisses[mMissIndex];
134         if (!mCache.getPipeline(miss, &desc, &result))
135         {
136             (void)mCache.createPipeline(VK_NULL_HANDLE, &spc, rp, pl, ssm, defaultSpecConsts,
137                                         PipelineSource::Draw, miss, &desc, &result);
138         }
139     }
140 
141     vsRefCounted.get().setHandle(VK_NULL_HANDLE);
142     fsRefCounted.get().setHandle(VK_NULL_HANDLE);
143 }
144 
145 }  // anonymous namespace
146 
147 // Test performance of pipeline hash and look up in Vulkan
TEST_P(VulkanPipelineCachePerfTest,Run)148 TEST_P(VulkanPipelineCachePerfTest, Run)
149 {
150     run();
151 }
152 
153 INSTANTIATE_TEST_SUITE_P(,
154                          VulkanPipelineCachePerfTest,
155                          ::testing::ValuesIn(std::vector<Params>{{Params{false}, Params{true}}}));
156