1*03ce13f7SAndroid Build Coastguard Worker // Copyright 2021 The SwiftShader Authors. All Rights Reserved.
2*03ce13f7SAndroid Build Coastguard Worker //
3*03ce13f7SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*03ce13f7SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*03ce13f7SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*03ce13f7SAndroid Build Coastguard Worker //
7*03ce13f7SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
8*03ce13f7SAndroid Build Coastguard Worker //
9*03ce13f7SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*03ce13f7SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*03ce13f7SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*03ce13f7SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*03ce13f7SAndroid Build Coastguard Worker // limitations under the License.
14*03ce13f7SAndroid Build Coastguard Worker
15*03ce13f7SAndroid Build Coastguard Worker #ifndef DRAW_TESTER_HPP_
16*03ce13f7SAndroid Build Coastguard Worker #define DRAW_TESTER_HPP_
17*03ce13f7SAndroid Build Coastguard Worker
18*03ce13f7SAndroid Build Coastguard Worker #include "Framebuffer.hpp"
19*03ce13f7SAndroid Build Coastguard Worker #include "Image.hpp"
20*03ce13f7SAndroid Build Coastguard Worker #include "Swapchain.hpp"
21*03ce13f7SAndroid Build Coastguard Worker #include "Util.hpp"
22*03ce13f7SAndroid Build Coastguard Worker #include "VulkanTester.hpp"
23*03ce13f7SAndroid Build Coastguard Worker #include "Window.hpp"
24*03ce13f7SAndroid Build Coastguard Worker
25*03ce13f7SAndroid Build Coastguard Worker #include <functional>
26*03ce13f7SAndroid Build Coastguard Worker #include <memory>
27*03ce13f7SAndroid Build Coastguard Worker
28*03ce13f7SAndroid Build Coastguard Worker enum class Multisample
29*03ce13f7SAndroid Build Coastguard Worker {
30*03ce13f7SAndroid Build Coastguard Worker False,
31*03ce13f7SAndroid Build Coastguard Worker True
32*03ce13f7SAndroid Build Coastguard Worker };
33*03ce13f7SAndroid Build Coastguard Worker
34*03ce13f7SAndroid Build Coastguard Worker class DrawTester : public VulkanTester
35*03ce13f7SAndroid Build Coastguard Worker {
36*03ce13f7SAndroid Build Coastguard Worker public:
37*03ce13f7SAndroid Build Coastguard Worker using ThisType = DrawTester;
38*03ce13f7SAndroid Build Coastguard Worker
39*03ce13f7SAndroid Build Coastguard Worker DrawTester(Multisample multisample = Multisample::False);
40*03ce13f7SAndroid Build Coastguard Worker ~DrawTester();
41*03ce13f7SAndroid Build Coastguard Worker
42*03ce13f7SAndroid Build Coastguard Worker void initialize();
43*03ce13f7SAndroid Build Coastguard Worker void renderFrame();
44*03ce13f7SAndroid Build Coastguard Worker void show();
45*03ce13f7SAndroid Build Coastguard Worker
46*03ce13f7SAndroid Build Coastguard Worker /////////////////////////
47*03ce13f7SAndroid Build Coastguard Worker // Hooks
48*03ce13f7SAndroid Build Coastguard Worker /////////////////////////
49*03ce13f7SAndroid Build Coastguard Worker
50*03ce13f7SAndroid Build Coastguard Worker // Called from prepareVertices.
51*03ce13f7SAndroid Build Coastguard Worker // Callback may call tester.addVertexBuffer() from this function.
52*03ce13f7SAndroid Build Coastguard Worker void onCreateVertexBuffers(std::function<void(ThisType &tester)> callback);
53*03ce13f7SAndroid Build Coastguard Worker
54*03ce13f7SAndroid Build Coastguard Worker // Called from createGraphicsPipeline.
55*03ce13f7SAndroid Build Coastguard Worker // Callback must return vector of DescriptorSetLayoutBindings for which a DescriptorSetLayout
56*03ce13f7SAndroid Build Coastguard Worker // will be created and stored in this->descriptorSetLayout.
57*03ce13f7SAndroid Build Coastguard Worker void onCreateDescriptorSetLayouts(std::function<std::vector<vk::DescriptorSetLayoutBinding>(ThisType &tester)> callback);
58*03ce13f7SAndroid Build Coastguard Worker
59*03ce13f7SAndroid Build Coastguard Worker // Called from createGraphicsPipeline.
60*03ce13f7SAndroid Build Coastguard Worker // Callback should call tester.createShaderModule() and return the result.
61*03ce13f7SAndroid Build Coastguard Worker void onCreateVertexShader(std::function<vk::ShaderModule(ThisType &tester)> callback);
62*03ce13f7SAndroid Build Coastguard Worker
63*03ce13f7SAndroid Build Coastguard Worker // Called from createGraphicsPipeline.
64*03ce13f7SAndroid Build Coastguard Worker // Callback should call tester.createShaderModule() and return the result.
65*03ce13f7SAndroid Build Coastguard Worker void onCreateFragmentShader(std::function<vk::ShaderModule(ThisType &tester)> callback);
66*03ce13f7SAndroid Build Coastguard Worker
67*03ce13f7SAndroid Build Coastguard Worker // Called from createCommandBuffers.
68*03ce13f7SAndroid Build Coastguard Worker // Callback may create resources (tester.addImage, tester.addSampler, etc.), and make sure to
69*03ce13f7SAndroid Build Coastguard Worker // call tester.device().updateDescriptorSets.
70*03ce13f7SAndroid Build Coastguard Worker void onUpdateDescriptorSet(std::function<void(ThisType &tester, vk::CommandPool &commandPool, vk::DescriptorSet &descriptorSet)> callback);
71*03ce13f7SAndroid Build Coastguard Worker
72*03ce13f7SAndroid Build Coastguard Worker /////////////////////////
73*03ce13f7SAndroid Build Coastguard Worker // Resource Management
74*03ce13f7SAndroid Build Coastguard Worker /////////////////////////
75*03ce13f7SAndroid Build Coastguard Worker
76*03ce13f7SAndroid Build Coastguard Worker // Call from doCreateFragmentShader()
77*03ce13f7SAndroid Build Coastguard Worker vk::ShaderModule createShaderModule(const char *glslSource, EShLanguage glslLanguage);
78*03ce13f7SAndroid Build Coastguard Worker
79*03ce13f7SAndroid Build Coastguard Worker // Call from doCreateVertexBuffers()
80*03ce13f7SAndroid Build Coastguard Worker template<typename VertexType>
addVertexBuffer(VertexType * vertexBufferData,size_t vertexBufferDataSize,std::vector<vk::VertexInputAttributeDescription> inputAttributes)81*03ce13f7SAndroid Build Coastguard Worker void addVertexBuffer(VertexType *vertexBufferData, size_t vertexBufferDataSize, std::vector<vk::VertexInputAttributeDescription> inputAttributes)
82*03ce13f7SAndroid Build Coastguard Worker {
83*03ce13f7SAndroid Build Coastguard Worker addVertexBuffer(vertexBufferData, vertexBufferDataSize, sizeof(VertexType), std::move(inputAttributes));
84*03ce13f7SAndroid Build Coastguard Worker }
85*03ce13f7SAndroid Build Coastguard Worker
86*03ce13f7SAndroid Build Coastguard Worker template<typename T>
87*03ce13f7SAndroid Build Coastguard Worker struct Resource
88*03ce13f7SAndroid Build Coastguard Worker {
89*03ce13f7SAndroid Build Coastguard Worker size_t id;
90*03ce13f7SAndroid Build Coastguard Worker T &obj;
91*03ce13f7SAndroid Build Coastguard Worker };
92*03ce13f7SAndroid Build Coastguard Worker
93*03ce13f7SAndroid Build Coastguard Worker template<typename... Args>
addImage(Args &&...args)94*03ce13f7SAndroid Build Coastguard Worker Resource<Image> addImage(Args &&...args)
95*03ce13f7SAndroid Build Coastguard Worker {
96*03ce13f7SAndroid Build Coastguard Worker images.emplace_back(std::make_unique<Image>(std::forward<Args>(args)...));
97*03ce13f7SAndroid Build Coastguard Worker return { images.size() - 1, *images.back() };
98*03ce13f7SAndroid Build Coastguard Worker }
99*03ce13f7SAndroid Build Coastguard Worker
getImageById(size_t id)100*03ce13f7SAndroid Build Coastguard Worker Image &getImageById(size_t id)
101*03ce13f7SAndroid Build Coastguard Worker {
102*03ce13f7SAndroid Build Coastguard Worker return *images[id].get();
103*03ce13f7SAndroid Build Coastguard Worker }
104*03ce13f7SAndroid Build Coastguard Worker
addSampler(const vk::SamplerCreateInfo & samplerCreateInfo)105*03ce13f7SAndroid Build Coastguard Worker Resource<vk::Sampler> addSampler(const vk::SamplerCreateInfo &samplerCreateInfo)
106*03ce13f7SAndroid Build Coastguard Worker {
107*03ce13f7SAndroid Build Coastguard Worker auto sampler = device.createSampler(samplerCreateInfo);
108*03ce13f7SAndroid Build Coastguard Worker samplers.push_back(sampler);
109*03ce13f7SAndroid Build Coastguard Worker return { samplers.size() - 1, samplers.back() };
110*03ce13f7SAndroid Build Coastguard Worker }
111*03ce13f7SAndroid Build Coastguard Worker
getSamplerById(size_t id)112*03ce13f7SAndroid Build Coastguard Worker vk::Sampler &getSamplerById(size_t id)
113*03ce13f7SAndroid Build Coastguard Worker {
114*03ce13f7SAndroid Build Coastguard Worker return samplers[id];
115*03ce13f7SAndroid Build Coastguard Worker }
116*03ce13f7SAndroid Build Coastguard Worker
117*03ce13f7SAndroid Build Coastguard Worker private:
118*03ce13f7SAndroid Build Coastguard Worker void createSynchronizationPrimitives();
119*03ce13f7SAndroid Build Coastguard Worker void createCommandBuffers(vk::RenderPass renderPass);
120*03ce13f7SAndroid Build Coastguard Worker void prepareVertices();
121*03ce13f7SAndroid Build Coastguard Worker void createFramebuffers(vk::RenderPass renderPass);
122*03ce13f7SAndroid Build Coastguard Worker vk::RenderPass createRenderPass(vk::Format colorFormat);
123*03ce13f7SAndroid Build Coastguard Worker vk::Pipeline createGraphicsPipeline(vk::RenderPass renderPass);
124*03ce13f7SAndroid Build Coastguard Worker void addVertexBuffer(void *vertexBufferData, size_t vertexBufferDataSize, size_t vertexSize, std::vector<vk::VertexInputAttributeDescription> inputAttributes);
125*03ce13f7SAndroid Build Coastguard Worker
126*03ce13f7SAndroid Build Coastguard Worker struct Hook
127*03ce13f7SAndroid Build Coastguard Worker {
__anone820d3f80102DrawTester::Hook128*03ce13f7SAndroid Build Coastguard Worker std::function<void(ThisType &tester)> createVertexBuffers = [](auto &) {};
__anone820d3f80202DrawTester::Hook129*03ce13f7SAndroid Build Coastguard Worker std::function<std::vector<vk::DescriptorSetLayoutBinding>(ThisType &tester)> createDescriptorSetLayout = [](auto &) { return std::vector<vk::DescriptorSetLayoutBinding>{}; };
__anone820d3f80302DrawTester::Hook130*03ce13f7SAndroid Build Coastguard Worker std::function<vk::ShaderModule(ThisType &tester)> createVertexShader = [](auto &) { return vk::ShaderModule{}; };
__anone820d3f80402DrawTester::Hook131*03ce13f7SAndroid Build Coastguard Worker std::function<vk::ShaderModule(ThisType &tester)> createFragmentShader = [](auto &) { return vk::ShaderModule{}; };
__anone820d3f80502DrawTester::Hook132*03ce13f7SAndroid Build Coastguard Worker std::function<void(ThisType &tester, vk::CommandPool &commandPool, vk::DescriptorSet &descriptorSet)> updateDescriptorSet = [](auto &, auto &, auto &) {};
133*03ce13f7SAndroid Build Coastguard Worker } hooks;
134*03ce13f7SAndroid Build Coastguard Worker
135*03ce13f7SAndroid Build Coastguard Worker const vk::Extent2D windowSize = { 1280, 720 };
136*03ce13f7SAndroid Build Coastguard Worker const bool multisample;
137*03ce13f7SAndroid Build Coastguard Worker
138*03ce13f7SAndroid Build Coastguard Worker std::unique_ptr<Window> window;
139*03ce13f7SAndroid Build Coastguard Worker std::unique_ptr<Swapchain> swapchain;
140*03ce13f7SAndroid Build Coastguard Worker
141*03ce13f7SAndroid Build Coastguard Worker vk::RenderPass renderPass; // Owning handle
142*03ce13f7SAndroid Build Coastguard Worker std::vector<std::unique_ptr<Framebuffer>> framebuffers;
143*03ce13f7SAndroid Build Coastguard Worker uint32_t currentFrameBuffer = 0;
144*03ce13f7SAndroid Build Coastguard Worker
145*03ce13f7SAndroid Build Coastguard Worker struct VertexBuffer
146*03ce13f7SAndroid Build Coastguard Worker {
147*03ce13f7SAndroid Build Coastguard Worker vk::Buffer buffer; // Owning handle
148*03ce13f7SAndroid Build Coastguard Worker vk::DeviceMemory memory; // Owning handle
149*03ce13f7SAndroid Build Coastguard Worker
150*03ce13f7SAndroid Build Coastguard Worker vk::VertexInputBindingDescription inputBinding;
151*03ce13f7SAndroid Build Coastguard Worker std::vector<vk::VertexInputAttributeDescription> inputAttributes;
152*03ce13f7SAndroid Build Coastguard Worker vk::PipelineVertexInputStateCreateInfo inputState;
153*03ce13f7SAndroid Build Coastguard Worker
154*03ce13f7SAndroid Build Coastguard Worker uint32_t numVertices = 0;
155*03ce13f7SAndroid Build Coastguard Worker } vertices;
156*03ce13f7SAndroid Build Coastguard Worker
157*03ce13f7SAndroid Build Coastguard Worker vk::DescriptorSetLayout descriptorSetLayout; // Owning handle
158*03ce13f7SAndroid Build Coastguard Worker vk::PipelineLayout pipelineLayout; // Owning handle
159*03ce13f7SAndroid Build Coastguard Worker vk::Pipeline pipeline; // Owning handle
160*03ce13f7SAndroid Build Coastguard Worker
161*03ce13f7SAndroid Build Coastguard Worker vk::Semaphore presentCompleteSemaphore; // Owning handle
162*03ce13f7SAndroid Build Coastguard Worker vk::Semaphore renderCompleteSemaphore; // Owning handle
163*03ce13f7SAndroid Build Coastguard Worker std::vector<vk::Fence> waitFences; // Owning handles
164*03ce13f7SAndroid Build Coastguard Worker
165*03ce13f7SAndroid Build Coastguard Worker vk::CommandPool commandPool; // Owning handle
166*03ce13f7SAndroid Build Coastguard Worker vk::DescriptorPool descriptorPool; // Owning handle
167*03ce13f7SAndroid Build Coastguard Worker
168*03ce13f7SAndroid Build Coastguard Worker // Resources
169*03ce13f7SAndroid Build Coastguard Worker std::vector<std::unique_ptr<Image>> images;
170*03ce13f7SAndroid Build Coastguard Worker std::vector<vk::Sampler> samplers; // Owning handles
171*03ce13f7SAndroid Build Coastguard Worker
172*03ce13f7SAndroid Build Coastguard Worker std::vector<vk::CommandBuffer> commandBuffers; // Owning handles
173*03ce13f7SAndroid Build Coastguard Worker };
174*03ce13f7SAndroid Build Coastguard Worker
onCreateVertexBuffers(std::function<void (ThisType & tester)> callback)175*03ce13f7SAndroid Build Coastguard Worker inline void DrawTester::onCreateVertexBuffers(std::function<void(ThisType &tester)> callback)
176*03ce13f7SAndroid Build Coastguard Worker {
177*03ce13f7SAndroid Build Coastguard Worker hooks.createVertexBuffers = std::move(callback);
178*03ce13f7SAndroid Build Coastguard Worker }
179*03ce13f7SAndroid Build Coastguard Worker
onCreateDescriptorSetLayouts(std::function<std::vector<vk::DescriptorSetLayoutBinding> (ThisType & tester)> callback)180*03ce13f7SAndroid Build Coastguard Worker inline void DrawTester::onCreateDescriptorSetLayouts(std::function<std::vector<vk::DescriptorSetLayoutBinding>(ThisType &tester)> callback)
181*03ce13f7SAndroid Build Coastguard Worker {
182*03ce13f7SAndroid Build Coastguard Worker hooks.createDescriptorSetLayout = std::move(callback);
183*03ce13f7SAndroid Build Coastguard Worker }
184*03ce13f7SAndroid Build Coastguard Worker
onCreateVertexShader(std::function<vk::ShaderModule (ThisType & tester)> callback)185*03ce13f7SAndroid Build Coastguard Worker inline void DrawTester::onCreateVertexShader(std::function<vk::ShaderModule(ThisType &tester)> callback)
186*03ce13f7SAndroid Build Coastguard Worker {
187*03ce13f7SAndroid Build Coastguard Worker hooks.createVertexShader = std::move(callback);
188*03ce13f7SAndroid Build Coastguard Worker }
189*03ce13f7SAndroid Build Coastguard Worker
onCreateFragmentShader(std::function<vk::ShaderModule (ThisType & tester)> callback)190*03ce13f7SAndroid Build Coastguard Worker inline void DrawTester::onCreateFragmentShader(std::function<vk::ShaderModule(ThisType &tester)> callback)
191*03ce13f7SAndroid Build Coastguard Worker {
192*03ce13f7SAndroid Build Coastguard Worker hooks.createFragmentShader = std::move(callback);
193*03ce13f7SAndroid Build Coastguard Worker }
194*03ce13f7SAndroid Build Coastguard Worker
onUpdateDescriptorSet(std::function<void (ThisType & tester,vk::CommandPool & commandPool,vk::DescriptorSet & descriptorSet)> callback)195*03ce13f7SAndroid Build Coastguard Worker inline void DrawTester::onUpdateDescriptorSet(std::function<void(ThisType &tester, vk::CommandPool &commandPool, vk::DescriptorSet &descriptorSet)> callback)
196*03ce13f7SAndroid Build Coastguard Worker {
197*03ce13f7SAndroid Build Coastguard Worker hooks.updateDescriptorSet = std::move(callback);
198*03ce13f7SAndroid Build Coastguard Worker }
199*03ce13f7SAndroid Build Coastguard Worker
200*03ce13f7SAndroid Build Coastguard Worker #endif // DRAW_TESTER_HPP_
201