1 /*
2 * Copyright (c) 2015-2019 The Khronos Group Inc.
3 * Copyright (c) 2015-2019 Valve Corporation
4 * Copyright (c) 2015-2019 LunarG, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
19 * Author: Dave Houlton <daveh@lunarg.com>
20 */
21
22 #ifndef VKRENDERFRAMEWORK_H
23 #define VKRENDERFRAMEWORK_H
24
25 #ifdef ANDROID
26 #include "vktestframeworkandroid.h"
27 class VkImageObj;
28 #else
29 #include "vktestframework.h"
30 #endif
31
32 #include <algorithm>
33 #include <array>
34 #include <map>
35 #include <memory>
36 #include <vector>
37
38 using namespace std;
39
40 using vk_testing::MakeVkHandles;
41
42 template <class Dst, class Src>
MakeTestbindingHandles(const std::vector<Src * > & v)43 std::vector<Dst *> MakeTestbindingHandles(const std::vector<Src *> &v) {
44 std::vector<Dst *> handles;
45 handles.reserve(v.size());
46 std::transform(v.begin(), v.end(), std::back_inserter(handles), [](const Src *o) { return static_cast<Dst *>(o); });
47 return handles;
48 }
49
50 typedef vk_testing::Queue VkQueueObj;
51 class VkDeviceObj : public vk_testing::Device {
52 public:
53 VkDeviceObj(uint32_t id, VkPhysicalDevice obj);
54 VkDeviceObj(uint32_t id, VkPhysicalDevice obj, std::vector<const char *> &extension_names,
55 VkPhysicalDeviceFeatures *features = nullptr, void *create_device_pnext = nullptr);
56
57 uint32_t QueueFamilyMatching(VkQueueFlags with, VkQueueFlags without, bool all_bits = true);
QueueFamilyWithoutCapabilities(VkQueueFlags capabilities)58 uint32_t QueueFamilyWithoutCapabilities(VkQueueFlags capabilities) {
59 // an all_bits match with 0 matches all
60 return QueueFamilyMatching(VkQueueFlags(0), capabilities, true /* all_bits with */);
61 }
62
device()63 VkDevice device() { return handle(); }
64 void SetDeviceQueue();
65 VkQueueObj *GetDefaultQueue();
66
67 uint32_t id;
68 VkPhysicalDeviceProperties props;
69 std::vector<VkQueueFamilyProperties> queue_props;
70
71 VkQueue m_queue;
72 };
73
74 class VkCommandPoolObj;
75 class VkCommandBufferObj;
76 class VkDepthStencilObj;
77
78 class VkRenderFramework : public VkTestFramework {
79 public:
80 VkRenderFramework();
81 ~VkRenderFramework();
82
instance()83 VkInstance instance() { return inst; }
device()84 VkDevice device() { return m_device->device(); }
DeviceObj()85 VkDeviceObj *DeviceObj() const { return m_device; }
86 VkPhysicalDevice gpu();
renderPass()87 VkRenderPass renderPass() { return m_renderPass; }
RenderPassInfo()88 const VkRenderPassCreateInfo &RenderPassInfo() const { return renderPass_info_; };
framebuffer()89 VkFramebuffer framebuffer() { return m_framebuffer; }
90 void InitViewport(float width, float height);
91 void InitViewport();
92 void InitRenderTarget();
93 void InitRenderTarget(uint32_t targets);
94 void InitRenderTarget(VkImageView *dsBinding);
95 void InitRenderTarget(uint32_t targets, VkImageView *dsBinding);
96 void DestroyRenderTarget();
97 void InitFramework(PFN_vkDebugReportCallbackEXT = NULL, void *userData = NULL, void *instance_pnext = NULL);
98
99 void ShutdownFramework();
100 void GetPhysicalDeviceFeatures(VkPhysicalDeviceFeatures *features);
101 void GetPhysicalDeviceProperties(VkPhysicalDeviceProperties *props);
102 void InitState(VkPhysicalDeviceFeatures *features = nullptr, void *create_device_pnext = nullptr,
103 const VkCommandPoolCreateFlags flags = 0);
104
renderPassBeginInfo()105 const VkRenderPassBeginInfo &renderPassBeginInfo() const { return m_renderPassBeginInfo; }
106
107 bool InstanceLayerSupported(const char *name, uint32_t specVersion = 0, uint32_t implementationVersion = 0);
108 bool EnableDeviceProfileLayer();
109 bool InstanceExtensionSupported(const char *name, uint32_t specVersion = 0);
110 bool InstanceExtensionEnabled(const char *name);
111 bool DeviceExtensionSupported(VkPhysicalDevice dev, const char *layer, const char *name, uint32_t specVersion = 0);
112 bool DeviceExtensionEnabled(const char *name);
113 bool DeviceIsMockICD();
114 bool DeviceCanDraw();
115
116 protected:
117 VkApplicationInfo app_info;
118 VkInstance inst;
119 VkPhysicalDevice objs[16];
120 uint32_t gpu_count;
121 VkDeviceObj *m_device;
122 VkCommandPoolObj *m_commandPool;
123 VkCommandBufferObj *m_commandBuffer;
124 VkRenderPass m_renderPass;
125 VkRenderPassCreateInfo renderPass_info_ = {};
126 VkFramebuffer m_framebuffer;
127 std::vector<VkViewport> m_viewports;
128 std::vector<VkRect2D> m_scissors;
129 float m_lineWidth;
130 float m_depthBiasConstantFactor;
131 float m_depthBiasClamp;
132 float m_depthBiasSlopeFactor;
133 float m_blendConstants[4];
134 float m_minDepthBounds;
135 float m_maxDepthBounds;
136 uint32_t m_compareMask;
137 uint32_t m_writeMask;
138 uint32_t m_reference;
139 bool m_addRenderPassSelfDependency;
140 std::vector<VkClearValue> m_renderPassClearValues;
141 VkRenderPassBeginInfo m_renderPassBeginInfo;
142 vector<std::unique_ptr<VkImageObj>> m_renderTargets;
143 float m_width, m_height;
144 VkFormat m_render_target_fmt;
145 VkFormat m_depth_stencil_fmt;
146 VkClearColorValue m_clear_color;
147 bool m_clear_via_load_op;
148 float m_depth_clear_color;
149 uint32_t m_stencil_clear_color;
150 VkDepthStencilObj *m_depthStencil;
151 PFN_vkCreateDebugReportCallbackEXT m_CreateDebugReportCallback;
152 PFN_vkDestroyDebugReportCallbackEXT m_DestroyDebugReportCallback;
153 PFN_vkDebugReportMessageEXT m_DebugReportMessage;
154 VkDebugReportCallbackEXT m_globalMsgCallback;
155 VkDebugReportCallbackEXT m_devMsgCallback;
156
157 std::vector<const char *> m_instance_layer_names;
158 std::vector<const char *> m_instance_extension_names;
159 std::vector<const char *> m_device_extension_names;
160
161 /*
162 * SetUp and TearDown are called by the Google Test framework
163 * to initialize a test framework based on this class.
164 */
SetUp()165 virtual void SetUp() {
166 this->app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
167 this->app_info.pNext = NULL;
168 this->app_info.pApplicationName = "base";
169 this->app_info.applicationVersion = 1;
170 this->app_info.pEngineName = "unittest";
171 this->app_info.engineVersion = 1;
172 this->app_info.apiVersion = VK_API_VERSION_1_0;
173
174 InitFramework();
175 }
176
TearDown()177 virtual void TearDown() { ShutdownFramework(); }
178 };
179
180 class VkDescriptorSetObj;
181 class VkConstantBufferObj;
182 class VkPipelineObj;
183 class VkDescriptorSetObj;
184 typedef vk_testing::Fence VkFenceObj;
185 typedef vk_testing::Buffer VkBufferObj;
186
187 class VkCommandPoolObj : public vk_testing::CommandPool {
188 public:
189 VkCommandPoolObj(VkDeviceObj *device, uint32_t queue_family_index, VkCommandPoolCreateFlags flags = 0);
190 };
191
192 class VkCommandBufferObj : public vk_testing::CommandBuffer {
193 public:
194 VkCommandBufferObj(VkDeviceObj *device, VkCommandPoolObj *pool, VkCommandBufferLevel level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
195 VkQueueObj *queue = nullptr);
196 void PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, VkDependencyFlags dependencyFlags,
197 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
198 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
199 const VkImageMemoryBarrier *pImageMemoryBarriers);
200 void ClearAllBuffers(const vector<std::unique_ptr<VkImageObj>> &color_objs, VkClearColorValue clear_color,
201 VkDepthStencilObj *depth_stencil_obj, float depth_clear_value, uint32_t stencil_clear_value);
202 void PrepareAttachments(const vector<std::unique_ptr<VkImageObj>> &color_atts, VkDepthStencilObj *depth_stencil_att);
203 void BindDescriptorSet(VkDescriptorSetObj &descriptorSet);
204 void BindIndexBuffer(VkBufferObj *indexBuffer, VkDeviceSize offset, VkIndexType indexType);
205 void BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding);
206 void BeginRenderPass(const VkRenderPassBeginInfo &info);
207 void EndRenderPass();
208 void FillBuffer(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize fill_size, uint32_t data);
209 void Draw(uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance);
210 void DrawIndexed(uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t vertexOffset,
211 uint32_t firstInstance);
212 void QueueCommandBuffer(bool checkSuccess = true);
213 void QueueCommandBuffer(const VkFenceObj &fence, bool checkSuccess = true);
214 void SetViewport(uint32_t firstViewport, uint32_t viewportCount, const VkViewport *pViewports);
215 void SetStencilReference(VkStencilFaceFlags faceMask, uint32_t reference);
216 void UpdateBuffer(VkBuffer buffer, VkDeviceSize dstOffset, VkDeviceSize dataSize, const void *pData);
217 void CopyImage(VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage, VkImageLayout dstImageLayout,
218 uint32_t regionCount, const VkImageCopy *pRegions);
219 void ResolveImage(VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage, VkImageLayout dstImageLayout,
220 uint32_t regionCount, const VkImageResolve *pRegions);
221 void ClearColorImage(VkImage image, VkImageLayout imageLayout, const VkClearColorValue *pColor, uint32_t rangeCount,
222 const VkImageSubresourceRange *pRanges);
223 void ClearDepthStencilImage(VkImage image, VkImageLayout imageLayout, const VkClearDepthStencilValue *pColor,
224 uint32_t rangeCount, const VkImageSubresourceRange *pRanges);
225
226 protected:
227 VkDeviceObj *m_device;
228 VkQueueObj *m_queue;
229 };
230
231 class VkConstantBufferObj : public VkBufferObj {
232 public:
233 VkConstantBufferObj(VkDeviceObj *device,
234 VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
235 VkConstantBufferObj(VkDeviceObj *device, VkDeviceSize size, const void *data,
236 VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
237
238 VkDescriptorBufferInfo m_descriptorBufferInfo;
239
240 protected:
241 VkDeviceObj *m_device;
242 };
243
244 class VkRenderpassObj {
245 public:
246 VkRenderpassObj(VkDeviceObj *device);
247 ~VkRenderpassObj();
handle()248 VkRenderPass handle() { return m_renderpass; }
249
250 protected:
251 VkRenderPass m_renderpass;
252 VkDevice device;
253 };
254
255 class VkImageObj : public vk_testing::Image {
256 public:
257 VkImageObj(VkDeviceObj *dev);
258 bool IsCompatible(VkImageUsageFlags usages, VkFormatFeatureFlags features);
259
260 public:
261 void Init(uint32_t const width, uint32_t const height, uint32_t const mipLevels, VkFormat const format, VkFlags const usage,
262 VkImageTiling const tiling = VK_IMAGE_TILING_LINEAR, VkMemoryPropertyFlags const reqs = 0,
263 const std::vector<uint32_t> *queue_families = nullptr);
264
265 void init(const VkImageCreateInfo *create_info);
266
267 void InitNoLayout(uint32_t const width, uint32_t const height, uint32_t const mipLevels, VkFormat const format,
268 VkFlags const usage, VkImageTiling tiling = VK_IMAGE_TILING_LINEAR, VkMemoryPropertyFlags reqs = 0,
269 const std::vector<uint32_t> *queue_families = nullptr);
270
271 // void clear( CommandBuffer*, uint32_t[4] );
272
Layout(VkImageLayout const layout)273 void Layout(VkImageLayout const layout) { m_descriptorImageInfo.imageLayout = layout; }
274
memory()275 VkDeviceMemory memory() const { return Image::memory().handle(); }
276
MapMemory()277 void *MapMemory() { return Image::memory().map(); }
278
UnmapMemory()279 void UnmapMemory() { Image::memory().unmap(); }
280
281 void ImageMemoryBarrier(VkCommandBufferObj *cmd, VkImageAspectFlags aspect, VkFlags output_mask, VkFlags input_mask,
282 VkImageLayout image_layout);
283
284 VkResult CopyImage(VkImageObj &src_image);
285
286 VkResult CopyImageOut(VkImageObj &dst_image);
287
288 std::array<std::array<uint32_t, 16>, 16> Read();
289
image()290 VkImage image() const { return handle(); }
291
targetView(VkFormat format)292 VkImageView targetView(VkFormat format) {
293 if (!m_targetView.initialized()) {
294 VkImageViewCreateInfo createView = {};
295 createView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
296 createView.image = handle();
297 createView.viewType = VK_IMAGE_VIEW_TYPE_2D;
298 createView.format = format;
299 createView.components.r = VK_COMPONENT_SWIZZLE_R;
300 createView.components.g = VK_COMPONENT_SWIZZLE_G;
301 createView.components.b = VK_COMPONENT_SWIZZLE_B;
302 createView.components.a = VK_COMPONENT_SWIZZLE_A;
303 createView.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
304 createView.flags = 0;
305 m_targetView.init(*m_device, createView);
306 }
307 return m_targetView.handle();
308 }
309
310 void SetLayout(VkCommandBufferObj *cmd_buf, VkImageAspectFlags aspect, VkImageLayout image_layout);
311 void SetLayout(VkImageAspectFlags aspect, VkImageLayout image_layout);
312
Layout()313 VkImageLayout Layout() const { return m_descriptorImageInfo.imageLayout; }
width()314 uint32_t width() const { return extent().width; }
height()315 uint32_t height() const { return extent().height; }
device()316 VkDeviceObj *device() const { return m_device; }
317
318 protected:
319 VkDeviceObj *m_device;
320
321 vk_testing::ImageView m_targetView;
322 VkDescriptorImageInfo m_descriptorImageInfo;
323 };
324
325 class VkTextureObj : public VkImageObj {
326 public:
327 VkTextureObj(VkDeviceObj *device, uint32_t *colors = NULL);
328
DescriptorImageInfo()329 const VkDescriptorImageInfo &DescriptorImageInfo() const { return m_descriptorImageInfo; }
330
331 protected:
332 VkDeviceObj *m_device;
333 vk_testing::ImageView m_textureView;
334 };
335
336 class VkDepthStencilObj : public VkImageObj {
337 public:
338 VkDepthStencilObj(VkDeviceObj *device);
339 void Init(VkDeviceObj *device, int32_t width, int32_t height, VkFormat format,
340 VkImageUsageFlags usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
341 bool Initialized();
342 VkImageView *BindInfo();
343
344 VkFormat Format() const;
345
346 protected:
347 VkDeviceObj *m_device;
348 bool m_initialized;
349 vk_testing::ImageView m_imageView;
350 VkFormat m_depth_stencil_fmt;
351 VkImageView m_attachmentBindInfo;
352 };
353
354 class VkSamplerObj : public vk_testing::Sampler {
355 public:
356 VkSamplerObj(VkDeviceObj *device);
357
358 protected:
359 VkDeviceObj *m_device;
360 };
361
362 class VkDescriptorSetLayoutObj : public vk_testing::DescriptorSetLayout {
363 public:
364 VkDescriptorSetLayoutObj() = default;
365 VkDescriptorSetLayoutObj(const VkDeviceObj *device,
366 const std::vector<VkDescriptorSetLayoutBinding> &descriptor_set_bindings = {},
367 VkDescriptorSetLayoutCreateFlags flags = 0);
368
369 // Move constructor and move assignment operator for Visual Studio 2013
VkDescriptorSetLayoutObj(VkDescriptorSetLayoutObj && src)370 VkDescriptorSetLayoutObj(VkDescriptorSetLayoutObj &&src) : DescriptorSetLayout(std::move(src)){};
371 VkDescriptorSetLayoutObj &operator=(VkDescriptorSetLayoutObj &&src) {
372 DescriptorSetLayout::operator=(std::move(src));
373 return *this;
374 }
375 };
376
377 class VkDescriptorSetObj : public vk_testing::DescriptorPool {
378 public:
379 VkDescriptorSetObj(VkDeviceObj *device);
380 ~VkDescriptorSetObj();
381
382 int AppendDummy();
383 int AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer);
384 int AppendSamplerTexture(VkSamplerObj *sampler, VkTextureObj *texture);
385 void CreateVKDescriptorSet(VkCommandBufferObj *commandBuffer);
386
387 VkDescriptorSet GetDescriptorSetHandle() const;
388 VkPipelineLayout GetPipelineLayout() const;
389
390 protected:
391 VkDeviceObj *m_device;
392 std::vector<VkDescriptorSetLayoutBinding> m_layout_bindings;
393 std::map<VkDescriptorType, int> m_type_counts;
394 int m_nextSlot;
395
396 vector<VkDescriptorImageInfo> m_imageSamplerDescriptors;
397 vector<VkWriteDescriptorSet> m_writes;
398
399 vk_testing::DescriptorSetLayout m_layout;
400 vk_testing::PipelineLayout m_pipeline_layout;
401 vk_testing::DescriptorSet *m_set = NULL;
402 };
403
404 class VkShaderObj : public vk_testing::ShaderModule {
405 public:
406 VkShaderObj(VkDeviceObj *device, const char *shaderText, VkShaderStageFlagBits stage, VkRenderFramework *framework,
407 char const *name = "main", bool debug = false);
408 VkShaderObj(VkDeviceObj *device, const std::string spv_source, VkShaderStageFlagBits stage, VkRenderFramework *framework,
409 char const *name = "main");
410 VkPipelineShaderStageCreateInfo const &GetStageCreateInfo() const;
411
412 protected:
413 VkPipelineShaderStageCreateInfo m_stage_info;
414 VkDeviceObj *m_device;
415 };
416
417 class VkPipelineLayoutObj : public vk_testing::PipelineLayout {
418 public:
419 VkPipelineLayoutObj() = default;
420 VkPipelineLayoutObj(VkDeviceObj *device, const std::vector<const VkDescriptorSetLayoutObj *> &descriptor_layouts = {},
421 const std::vector<VkPushConstantRange> &push_constant_ranges = {});
422
423 // Move constructor and move assignment operator for Visual Studio 2013
VkPipelineLayoutObj(VkPipelineLayoutObj && src)424 VkPipelineLayoutObj(VkPipelineLayoutObj &&src) : PipelineLayout(std::move(src)) {}
425 VkPipelineLayoutObj &operator=(VkPipelineLayoutObj &&src) {
426 PipelineLayout::operator=(std::move(src));
427 return *this;
428 }
429
430 void Reset();
431 };
432
433 class VkPipelineObj : public vk_testing::Pipeline {
434 public:
435 VkPipelineObj(VkDeviceObj *device);
436 void AddShader(VkShaderObj *shaderObj);
437 void AddShader(VkPipelineShaderStageCreateInfo const &createInfo);
438 void AddVertexInputAttribs(VkVertexInputAttributeDescription *vi_attrib, uint32_t count);
439 void AddVertexInputBindings(VkVertexInputBindingDescription *vi_binding, uint32_t count);
440 void AddColorAttachment(uint32_t binding, const VkPipelineColorBlendAttachmentState &att);
441 void MakeDynamic(VkDynamicState state);
442
443 void AddDefaultColorAttachment(VkColorComponentFlags writeMask = 0xf /*=R|G|B|A*/) {
444 VkPipelineColorBlendAttachmentState att = {};
445 att.blendEnable = VK_FALSE;
446 att.colorWriteMask = writeMask;
447 AddColorAttachment(0, att);
448 }
449
450 void SetDepthStencil(const VkPipelineDepthStencilStateCreateInfo *);
451 void SetMSAA(const VkPipelineMultisampleStateCreateInfo *ms_state);
452 void SetInputAssembly(const VkPipelineInputAssemblyStateCreateInfo *ia_state);
453 void SetRasterization(const VkPipelineRasterizationStateCreateInfo *rs_state);
454 void SetTessellation(const VkPipelineTessellationStateCreateInfo *te_state);
455 void SetViewport(const vector<VkViewport> viewports);
456 void SetScissor(const vector<VkRect2D> scissors);
457
458 void InitGraphicsPipelineCreateInfo(VkGraphicsPipelineCreateInfo *gp_ci);
459
460 VkResult CreateVKPipeline(VkPipelineLayout layout, VkRenderPass render_pass, VkGraphicsPipelineCreateInfo *gp_ci = nullptr);
461
462 protected:
463 VkPipelineVertexInputStateCreateInfo m_vi_state;
464 VkPipelineInputAssemblyStateCreateInfo m_ia_state;
465 VkPipelineRasterizationStateCreateInfo m_rs_state;
466 VkPipelineColorBlendStateCreateInfo m_cb_state;
467 VkPipelineDepthStencilStateCreateInfo const *m_ds_state;
468 VkPipelineViewportStateCreateInfo m_vp_state;
469 VkPipelineMultisampleStateCreateInfo m_ms_state;
470 VkPipelineTessellationStateCreateInfo const *m_te_state;
471 VkPipelineDynamicStateCreateInfo m_pd_state;
472 vector<VkDynamicState> m_dynamic_state_enables;
473 vector<VkViewport> m_viewports;
474 vector<VkRect2D> m_scissors;
475 VkDeviceObj *m_device;
476 vector<VkPipelineShaderStageCreateInfo> m_shaderStages;
477 vector<VkPipelineColorBlendAttachmentState> m_colorAttachments;
478 };
479
480 #endif // VKRENDERFRAMEWORK_H
481