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/RendererVk.h"
12 #include "libANGLE/renderer/vulkan/vk_cache_utils.h"
13 #include "libANGLE/renderer/vulkan/vk_helpers.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 // Insert a number of random pipeline states.
60 for (int pipelineCount = 0; pipelineCount < 100; ++pipelineCount)
61 {
62 vk::Pipeline pipeline;
63 vk::GraphicsPipelineDesc desc;
64 randomizeDesc(&desc);
65
66 if (pipelineCount < 10)
67 {
68 mCacheHits.push_back(desc);
69 }
70 mCache.populate(desc, std::move(pipeline));
71 }
72
73 for (int missCount = 0; missCount < 10000; ++missCount)
74 {
75 vk::GraphicsPipelineDesc desc;
76 randomizeDesc(&desc);
77 mCacheMisses.push_back(desc);
78 }
79 }
80
randomizeDesc(vk::GraphicsPipelineDesc * desc)81 void VulkanPipelineCachePerfTest::randomizeDesc(vk::GraphicsPipelineDesc *desc)
82 {
83 std::vector<uint8_t> bytes(sizeof(vk::GraphicsPipelineDesc));
84 FillVectorWithRandomUBytes(&mRNG, &bytes);
85 memcpy(desc, bytes.data(), sizeof(vk::GraphicsPipelineDesc));
86
87 desc->setSupportsDynamicStateForTest(GetParam().withDynamicState);
88 }
89
step()90 void VulkanPipelineCachePerfTest::step()
91 {
92 vk::RenderPass rp;
93 vk::PipelineLayout pl;
94 vk::PipelineCache pc;
95 vk::PipelineCacheAccess spc;
96 vk::RefCounted<vk::ShaderModule> vsRefCounted;
97 vk::RefCounted<vk::ShaderModule> fsRefCounted;
98 vk::ShaderModuleMap ssm;
99 const vk::GraphicsPipelineDesc *desc = nullptr;
100 vk::PipelineHelper *result = nullptr;
101
102 // The Vulkan handle types are difficult to cast to without #ifdefs.
103 VkShaderModule vs = (VkShaderModule)1;
104 VkShaderModule fs = (VkShaderModule)2;
105
106 vsRefCounted.get().setHandle(vs);
107 fsRefCounted.get().setHandle(fs);
108
109 ssm[gl::ShaderType::Vertex].set(&vsRefCounted);
110 ssm[gl::ShaderType::Fragment].set(&fsRefCounted);
111
112 spc.init(&pc, nullptr);
113
114 vk::SpecializationConstants defaultSpecConsts{};
115
116 for (unsigned int iteration = 0; iteration < kIterationsPerStep; ++iteration)
117 {
118 for (const auto &hit : mCacheHits)
119 {
120 if (!mCache.getPipeline(hit, &desc, &result))
121 {
122 (void)mCache.createPipeline(VK_NULL_HANDLE, &spc, rp, pl, ssm, defaultSpecConsts,
123 PipelineSource::Draw, hit, &desc, &result);
124 }
125 }
126 }
127
128 for (int missCount = 0; missCount < 20 && mMissIndex < mCacheMisses.size();
129 ++missCount, ++mMissIndex)
130 {
131 const auto &miss = mCacheMisses[mMissIndex];
132 if (!mCache.getPipeline(miss, &desc, &result))
133 {
134 (void)mCache.createPipeline(VK_NULL_HANDLE, &spc, rp, pl, ssm, defaultSpecConsts,
135 PipelineSource::Draw, miss, &desc, &result);
136 }
137 }
138
139 vsRefCounted.get().setHandle(VK_NULL_HANDLE);
140 fsRefCounted.get().setHandle(VK_NULL_HANDLE);
141 }
142
143 } // anonymous namespace
144
145 // Test performance of pipeline hash and look up in Vulkan
TEST_P(VulkanPipelineCachePerfTest,Run)146 TEST_P(VulkanPipelineCachePerfTest, Run)
147 {
148 run();
149 }
150
151 INSTANTIATE_TEST_SUITE_P(,
152 VulkanPipelineCachePerfTest,
153 ::testing::ValuesIn(std::vector<Params>{{Params{false}, Params{true}}}));
154