blob: 5ffc65a644580f3cc7777d7eef03b633a907b2b5 [file] [log] [blame]
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001/*
Karl Schultz6addd812016-02-02 17:17:23 -07002 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06005 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06006 * 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
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06009 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060011 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012 * 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.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060017 *
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060018 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060019 */
20
21#ifndef VKRENDERFRAMEWORK_H
22#define VKRENDERFRAMEWORK_H
23
Michael Lentine83ab3412015-11-03 16:20:30 -080024#ifdef ANDROID
25#include "vktestframeworkandroid.h"
26class VkImageObj;
27#else
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060028#include "vktestframework.h"
Michael Lentine83ab3412015-11-03 16:20:30 -080029#endif
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060030
Tony Barbourb31ae812016-04-11 11:39:43 -060031#include <map>
Michael Lentine83ab3412015-11-03 16:20:30 -080032#include <vector>
Karl Schultzfc8eaf12016-05-06 13:56:42 -060033#include <array>
Michael Lentine83ab3412015-11-03 16:20:30 -080034
35using namespace std;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060036
Karl Schultz6addd812016-02-02 17:17:23 -070037class VkDeviceObj : public vk_testing::Device {
38 public:
Tony Barbourd1c35722015-04-16 15:59:00 -060039 VkDeviceObj(uint32_t id, VkPhysicalDevice obj);
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -060040 VkDeviceObj(uint32_t id, VkPhysicalDevice obj,
41 std::vector<const char *> &layers,
42 std::vector<const char *> &extension_names);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060043
Chia-I Wuf368b602015-07-03 10:41:20 +080044 VkDevice device() { return handle(); }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060045 void get_device_queue();
46
Karl Schultz6addd812016-02-02 17:17:23 -070047 uint32_t id;
48 VkPhysicalDeviceProperties props;
Dustin Graves48458142016-04-29 16:11:55 -060049 std::vector<VkQueueFamilyProperties> queue_props;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060050
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060051 VkQueue m_queue;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060052};
53
Tony Barbourfe3351b2015-07-28 10:17:20 -060054class VkCommandBufferObj;
Tony Barbour0371eb52016-04-05 11:44:03 -060055class VkDepthStencilObj;
Tony Barbourfe3351b2015-07-28 10:17:20 -060056
Karl Schultz6addd812016-02-02 17:17:23 -070057class VkRenderFramework : public VkTestFramework {
58 public:
Tony Barbour6918cd52015-04-09 12:58:51 -060059 VkRenderFramework();
60 ~VkRenderFramework();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060061
Ian Elliott1c32c772016-04-28 14:47:13 -060062 VkInstance instance() { return inst; }
Karl Schultz6addd812016-02-02 17:17:23 -070063 VkDevice device() { return m_device->device(); }
64 VkPhysicalDevice gpu() { return objs[0]; }
65 VkRenderPass renderPass() { return m_renderPass; }
66 VkFramebuffer framebuffer() { return m_framebuffer; }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060067 void InitViewport(float width, float height);
68 void InitViewport();
69 void InitRenderTarget();
70 void InitRenderTarget(uint32_t targets);
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -060071 void InitRenderTarget(VkImageView *dsBinding);
72 void InitRenderTarget(uint32_t targets, VkImageView *dsBinding);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060073 void InitFramework();
Karl Schultz6addd812016-02-02 17:17:23 -070074 void InitFramework(std::vector<const char *> instance_layer_names,
75 std::vector<const char *> device_layer_names,
76 std::vector<const char *> instance_extension_names,
77 std::vector<const char *> device_extension_names,
78 PFN_vkDebugReportCallbackEXT = NULL,
79 void *userData = NULL);
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060080
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060081 void ShutdownFramework();
82 void InitState();
83
Karl Schultz6addd812016-02-02 17:17:23 -070084 const VkRenderPassBeginInfo &renderPassBeginInfo() const {
85 return m_renderPassBeginInfo;
86 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060087
Karl Schultz6addd812016-02-02 17:17:23 -070088 protected:
89 VkApplicationInfo app_info;
90 VkInstance inst;
91 VkPhysicalDevice objs[16];
92 uint32_t gpu_count;
93 VkDeviceObj *m_device;
94 VkCommandPool m_commandPool;
95 VkCommandBufferObj *m_commandBuffer;
96 VkRenderPass m_renderPass;
97 VkFramebuffer m_framebuffer;
98 std::vector<VkViewport> m_viewports;
99 std::vector<VkRect2D> m_scissors;
100 float m_lineWidth;
101 float m_depthBiasConstantFactor;
102 float m_depthBiasClamp;
103 float m_depthBiasSlopeFactor;
104 float m_blendConstants[4];
105 float m_minDepthBounds;
106 float m_maxDepthBounds;
107 uint32_t m_compareMask;
108 uint32_t m_writeMask;
109 uint32_t m_reference;
110 std::vector<VkClearValue> m_renderPassClearValues;
111 VkRenderPassBeginInfo m_renderPassBeginInfo;
112 vector<VkImageObj *> m_renderTargets;
113 float m_width, m_height;
114 VkFormat m_render_target_fmt;
115 VkFormat m_depth_stencil_fmt;
116 VkClearColorValue m_clear_color;
117 bool m_clear_via_load_op;
118 float m_depth_clear_color;
119 uint32_t m_stencil_clear_color;
120 VkDepthStencilObj *m_depthStencil;
121 PFN_vkCreateDebugReportCallbackEXT m_CreateDebugReportCallback;
Courtney Goeltzenleuchter011ccf82015-12-15 09:25:15 -0700122 PFN_vkDestroyDebugReportCallbackEXT m_DestroyDebugReportCallback;
Karl Schultz6addd812016-02-02 17:17:23 -0700123 PFN_vkDebugReportMessageEXT m_DebugReportMessage;
124 VkDebugReportCallbackEXT m_globalMsgCallback;
125 VkDebugReportCallbackEXT m_devMsgCallback;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600126
127 /*
128 * SetUp and TearDown are called by the Google Test framework
129 * to initialize a test framework based on this class.
130 */
131 virtual void SetUp() {
132 this->app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
133 this->app_info.pNext = NULL;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800134 this->app_info.pApplicationName = "base";
135 this->app_info.applicationVersion = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600136 this->app_info.pEngineName = "unittest";
137 this->app_info.engineVersion = 1;
Jon Ashburnc5012ff2016-03-22 13:57:46 -0600138 this->app_info.apiVersion = VK_API_VERSION_1_0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600139
140 InitFramework();
141 }
142
Karl Schultz6addd812016-02-02 17:17:23 -0700143 virtual void TearDown() { ShutdownFramework(); }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600144};
145
Tony Barbour6918cd52015-04-09 12:58:51 -0600146class VkDescriptorSetObj;
147class VkIndexBufferObj;
148class VkConstantBufferObj;
149class VkPipelineObj;
150class VkDescriptorSetObj;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600151
Karl Schultz6addd812016-02-02 17:17:23 -0700152class VkCommandBufferObj : public vk_testing::CommandBuffer {
153 public:
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800154 VkCommandBufferObj(VkDeviceObj *device, VkCommandPool pool);
155 VkCommandBuffer GetBufferHandle();
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600156 VkResult BeginCommandBuffer();
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800157 VkResult BeginCommandBuffer(VkCommandBufferBeginInfo *pInfo);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600158 VkResult EndCommandBuffer();
Karl Schultz6addd812016-02-02 17:17:23 -0700159 void PipelineBarrier(VkPipelineStageFlags src_stages,
160 VkPipelineStageFlags dest_stages,
161 VkDependencyFlags dependencyFlags,
162 uint32_t memoryBarrierCount,
163 const VkMemoryBarrier *pMemoryBarriers,
164 uint32_t bufferMemoryBarrierCount,
165 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
166 uint32_t imageMemoryBarrierCount,
167 const VkImageMemoryBarrier *pImageMemoryBarriers);
Tony Barbour6918cd52015-04-09 12:58:51 -0600168 void AddRenderTarget(VkImageObj *renderTarget);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600169 void AddDepthStencil();
Karl Schultz6addd812016-02-02 17:17:23 -0700170 void ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color,
171 uint32_t stencil_clear_color,
172 VkDepthStencilObj *depthStencilObj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600173 void PrepareAttachments();
Tony Barbour6918cd52015-04-09 12:58:51 -0600174 void BindPipeline(VkPipelineObj &pipeline);
175 void BindDescriptorSet(VkDescriptorSetObj &descriptorSet);
Karl Schultz6addd812016-02-02 17:17:23 -0700176 void BindVertexBuffer(VkConstantBufferObj *vertexBuffer,
177 VkDeviceSize offset, uint32_t binding);
Cody Northropd2ad0342015-08-05 11:15:02 -0600178 void BindIndexBuffer(VkIndexBufferObj *indexBuffer, VkDeviceSize offset);
Chia-I Wu08accc62015-07-07 11:50:03 +0800179 void BeginRenderPass(const VkRenderPassBeginInfo &info);
Chia-I Wu0b50a1c2015-06-26 15:34:39 +0800180 void EndRenderPass();
Karl Schultz6addd812016-02-02 17:17:23 -0700181 void FillBuffer(VkBuffer buffer, VkDeviceSize offset,
182 VkDeviceSize fill_size, uint32_t data);
183 void Draw(uint32_t vertexCount, uint32_t instanceCount,
184 uint32_t firstVertex, uint32_t firstInstance);
185 void DrawIndexed(uint32_t indexCount, uint32_t instanceCount,
186 uint32_t firstIndex, int32_t vertexOffset,
187 uint32_t firstInstance);
Mark Mueller979ba7b2016-06-22 19:40:33 -0600188 void QueueCommandBuffer(bool checkSuccess = true);
189 void QueueCommandBuffer(VkFence fence, bool checkSuccess = true);
Karl Schultz6addd812016-02-02 17:17:23 -0700190 void SetViewport(uint32_t firstViewport, uint32_t viewportCount,
191 const VkViewport *pViewports);
192 void SetScissor(uint32_t firstScissor, uint32_t scissorCount,
193 const VkRect2D *pScissors);
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600194 void SetLineWidth(float lineWidth);
Karl Schultz6addd812016-02-02 17:17:23 -0700195 void SetDepthBias(float depthBiasConstantFactor, float depthBiasClamp,
196 float depthBiasSlopeFactor);
Chia-I Wu2bfb33c2015-10-26 17:24:52 +0800197 void SetBlendConstants(const float blendConstants[4]);
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600198 void SetDepthBounds(float minDepthBounds, float maxDepthBounds);
Chia-I Wu1b99bb22015-10-27 19:25:11 +0800199 void SetStencilReadMask(VkStencilFaceFlags faceMask, uint32_t compareMask);
200 void SetStencilWriteMask(VkStencilFaceFlags faceMask, uint32_t writeMask);
201 void SetStencilReference(VkStencilFaceFlags faceMask, uint32_t reference);
Karl Schultz6addd812016-02-02 17:17:23 -0700202 void UpdateBuffer(VkBuffer buffer, VkDeviceSize dstOffset,
203 VkDeviceSize dataSize, const uint32_t *pData);
204 void CopyImage(VkImage srcImage, VkImageLayout srcImageLayout,
205 VkImage dstImage, VkImageLayout dstImageLayout,
206 uint32_t regionCount, const VkImageCopy *pRegions);
207 void ResolveImage(VkImage srcImage, VkImageLayout srcImageLayout,
208 VkImage dstImage, VkImageLayout dstImageLayout,
209 uint32_t regionCount, const VkImageResolve *pRegions);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600210
Karl Schultz6addd812016-02-02 17:17:23 -0700211 protected:
212 VkDeviceObj *m_device;
213 vector<VkImageObj *> m_renderTargets;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600214};
215
Karl Schultz6addd812016-02-02 17:17:23 -0700216class VkConstantBufferObj : public vk_testing::Buffer {
217 public:
Tony Barbour6918cd52015-04-09 12:58:51 -0600218 VkConstantBufferObj(VkDeviceObj *device);
Karl Schultz6addd812016-02-02 17:17:23 -0700219 VkConstantBufferObj(VkDeviceObj *device, int constantCount,
220 int constantSize, const void *data);
Tony Barbour6918cd52015-04-09 12:58:51 -0600221 ~VkConstantBufferObj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600222 void BufferMemoryBarrier(
Karl Schultz6addd812016-02-02 17:17:23 -0700223 VkFlags srcAccessMask = VK_ACCESS_HOST_WRITE_BIT |
224 VK_ACCESS_SHADER_WRITE_BIT |
225 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
226 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
227 VK_ACCESS_TRANSFER_WRITE_BIT,
228 VkFlags dstAccessMask = VK_ACCESS_HOST_READ_BIT |
229 VK_ACCESS_INDIRECT_COMMAND_READ_BIT |
230 VK_ACCESS_INDEX_READ_BIT |
231 VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT |
232 VK_ACCESS_UNIFORM_READ_BIT |
233 VK_ACCESS_SHADER_READ_BIT |
234 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
235 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
236 VK_ACCESS_MEMORY_READ_BIT);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600237
Karl Schultz6addd812016-02-02 17:17:23 -0700238 void Bind(VkCommandBuffer commandBuffer, VkDeviceSize offset,
239 uint32_t binding);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600240
Karl Schultz6addd812016-02-02 17:17:23 -0700241 VkDescriptorBufferInfo m_descriptorBufferInfo;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600242
Karl Schultz6addd812016-02-02 17:17:23 -0700243 protected:
244 VkDeviceObj *m_device;
245 vk_testing::BufferView m_bufferView;
246 int m_numVertices;
247 int m_stride;
248 vk_testing::CommandPool *m_commandPool;
249 VkCommandBufferObj *m_commandBuffer;
250 vk_testing::Fence m_fence;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600251};
252
Karl Schultz6addd812016-02-02 17:17:23 -0700253class VkIndexBufferObj : public VkConstantBufferObj {
254 public:
Tony Barbour6918cd52015-04-09 12:58:51 -0600255 VkIndexBufferObj(VkDeviceObj *device);
Karl Schultz6addd812016-02-02 17:17:23 -0700256 void CreateAndInitBuffer(int numIndexes, VkIndexType dataFormat,
257 const void *data);
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800258 void Bind(VkCommandBuffer commandBuffer, VkDeviceSize offset);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600259 VkIndexType GetIndexType();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600260
Karl Schultz6addd812016-02-02 17:17:23 -0700261 protected:
262 VkIndexType m_indexType;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600263};
264
Karl Schultz6addd812016-02-02 17:17:23 -0700265class VkImageObj : public vk_testing::Image {
266 public:
Tony Barbour6918cd52015-04-09 12:58:51 -0600267 VkImageObj(VkDeviceObj *dev);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600268 bool IsCompatible(VkFlags usage, VkFlags features);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600269
Karl Schultz6addd812016-02-02 17:17:23 -0700270 public:
271 void init(uint32_t w, uint32_t h, VkFormat fmt, VkFlags usage,
272 VkImageTiling tiling = VK_IMAGE_TILING_LINEAR,
273 VkMemoryPropertyFlags reqs = 0);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600274
275 // void clear( CommandBuffer*, uint32_t[4] );
276
Karl Schultz6addd812016-02-02 17:17:23 -0700277 void layout(VkImageLayout layout) {
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600278 m_descriptorImageInfo.imageLayout = layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600279 }
280
Karl Schultz6addd812016-02-02 17:17:23 -0700281 VkDeviceMemory memory() const { return Image::memory().handle(); }
Chia-I Wu681d7a02015-07-03 13:44:34 +0800282
Karl Schultz6addd812016-02-02 17:17:23 -0700283 void *MapMemory() { return Image::memory().map(); }
Chia-I Wu681d7a02015-07-03 13:44:34 +0800284
Karl Schultz6addd812016-02-02 17:17:23 -0700285 void UnmapMemory() { Image::memory().unmap(); }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600286
Karl Schultz6addd812016-02-02 17:17:23 -0700287 void ImageMemoryBarrier(VkCommandBufferObj *cmd, VkImageAspectFlags aspect,
288 VkFlags output_mask, VkFlags input_mask,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600289 VkImageLayout image_layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600290
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600291 VkResult CopyImage(VkImageObj &src_image);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600292
Karl Schultz6addd812016-02-02 17:17:23 -0700293 VkImage image() const { return handle(); }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600294
Karl Schultz6addd812016-02-02 17:17:23 -0700295 VkImageView targetView(VkFormat format) {
296 if (!m_targetView.initialized()) {
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600297 VkImageViewCreateInfo createView = {};
298 createView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
299 createView.image = handle();
Karl Schultz6addd812016-02-02 17:17:23 -0700300 createView.viewType = VK_IMAGE_VIEW_TYPE_2D;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600301 createView.format = format;
Chia-I Wuab83a0e2015-10-27 19:00:15 +0800302 createView.components.r = VK_COMPONENT_SWIZZLE_R;
303 createView.components.g = VK_COMPONENT_SWIZZLE_G;
304 createView.components.b = VK_COMPONENT_SWIZZLE_B;
305 createView.components.a = VK_COMPONENT_SWIZZLE_A;
Karl Schultz6addd812016-02-02 17:17:23 -0700306 createView.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0,
307 1};
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600308 createView.flags = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600309 m_targetView.init(*m_device, createView);
310 }
Chia-I Wu3158bf32015-07-03 11:49:42 +0800311 return m_targetView.handle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600312 }
313
Tony Barbour9a359b62016-06-16 13:17:35 -0600314 void SetLayout(VkCommandBufferObj *cmd_buf, VkImageAspectFlags aspect,
Karl Schultz6addd812016-02-02 17:17:23 -0700315 VkImageLayout image_layout);
Tony Barbour9a359b62016-06-16 13:17:35 -0600316 void SetLayout(VkImageAspectFlags aspect, VkImageLayout image_layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600317
Karl Schultz6addd812016-02-02 17:17:23 -0700318 VkImageLayout layout() const { return m_descriptorImageInfo.imageLayout; }
319 uint32_t width() const { return extent().width; }
320 uint32_t height() const { return extent().height; }
321 VkDeviceObj *device() const { return m_device; }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600322
Karl Schultz6addd812016-02-02 17:17:23 -0700323 protected:
324 VkDeviceObj *m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600325
Karl Schultz6addd812016-02-02 17:17:23 -0700326 vk_testing::ImageView m_targetView;
327 VkDescriptorImageInfo m_descriptorImageInfo;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600328};
329
Karl Schultz6addd812016-02-02 17:17:23 -0700330class VkTextureObj : public VkImageObj {
331 public:
Tony Barbour6918cd52015-04-09 12:58:51 -0600332 VkTextureObj(VkDeviceObj *device, uint32_t *colors = NULL);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600333
Karl Schultz6addd812016-02-02 17:17:23 -0700334 VkDescriptorImageInfo m_imageInfo;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600335
Karl Schultz6addd812016-02-02 17:17:23 -0700336 protected:
337 VkDeviceObj *m_device;
338 vk_testing::ImageView m_textureView;
339 VkDeviceSize m_rowPitch;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600340};
341
Tony Barbour0371eb52016-04-05 11:44:03 -0600342class VkDepthStencilObj : public VkImageObj {
343 public:
344 VkDepthStencilObj(VkDeviceObj *device);
345 void Init(VkDeviceObj *device, int32_t width, int32_t height,
Tony Barbourabf29962016-06-16 13:27:30 -0600346 VkFormat format, VkImageUsageFlags usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
Tony Barbour0371eb52016-04-05 11:44:03 -0600347 bool Initialized();
348 VkImageView *BindInfo();
349
350 protected:
351 VkDeviceObj *m_device;
352 bool m_initialized;
353 vk_testing::ImageView m_imageView;
354 VkFormat m_depth_stencil_fmt;
355 VkImageView m_attachmentBindInfo;
356};
357
Karl Schultz6addd812016-02-02 17:17:23 -0700358class VkSamplerObj : public vk_testing::Sampler {
359 public:
Tony Barbour6918cd52015-04-09 12:58:51 -0600360 VkSamplerObj(VkDeviceObj *device);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600361
Karl Schultz6addd812016-02-02 17:17:23 -0700362 protected:
363 VkDeviceObj *m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600364};
365
Karl Schultz6addd812016-02-02 17:17:23 -0700366class VkDescriptorSetObj : public vk_testing::DescriptorPool {
367 public:
Tony Barbour6918cd52015-04-09 12:58:51 -0600368 VkDescriptorSetObj(VkDeviceObj *device);
369 ~VkDescriptorSetObj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600370
371 int AppendDummy();
Karl Schultz6addd812016-02-02 17:17:23 -0700372 int AppendBuffer(VkDescriptorType type,
373 VkConstantBufferObj &constantBuffer);
374 int AppendSamplerTexture(VkSamplerObj *sampler, VkTextureObj *texture);
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800375 void CreateVKDescriptorSet(VkCommandBufferObj *commandBuffer);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600376
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600377 VkDescriptorSet GetDescriptorSetHandle() const;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500378 VkPipelineLayout GetPipelineLayout() const;
Tony Barbour26aa42b2016-03-02 15:35:27 -0700379 int GetTypeCounts() {return m_type_counts.size();}
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600380
Karl Schultz6addd812016-02-02 17:17:23 -0700381 protected:
382 VkDeviceObj *m_device;
Tony Barbourb31ae812016-04-11 11:39:43 -0600383 std::vector<VkDescriptorSetLayoutBinding> m_layout_bindings;
384 std::map<VkDescriptorType, int> m_type_counts;
Karl Schultz6addd812016-02-02 17:17:23 -0700385 int m_nextSlot;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600386
Karl Schultz6addd812016-02-02 17:17:23 -0700387 vector<VkDescriptorImageInfo> m_imageSamplerDescriptors;
388 vector<VkWriteDescriptorSet> m_writes;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600389
Karl Schultz6addd812016-02-02 17:17:23 -0700390 vk_testing::DescriptorSetLayout m_layout;
391 vk_testing::PipelineLayout m_pipeline_layout;
Tony Barbour26aa42b2016-03-02 15:35:27 -0700392 vk_testing::DescriptorSet *m_set = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600393};
394
Karl Schultz6addd812016-02-02 17:17:23 -0700395class VkShaderObj : public vk_testing::ShaderModule {
396 public:
397 VkShaderObj(VkDeviceObj *device, const char *shaderText,
Chris Forbes4f9b0e62015-12-30 11:49:04 +1300398 VkShaderStageFlagBits stage, VkRenderFramework *framework,
399 char const *name = "main");
Chia-I Wu28e06912015-10-31 00:31:16 +0800400 VkPipelineShaderStageCreateInfo GetStageCreateInfo() const;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600401
Karl Schultz6addd812016-02-02 17:17:23 -0700402 protected:
403 VkPipelineShaderStageCreateInfo stage_info;
404 VkShaderStageFlagBits m_stage;
Chris Forbes4f9b0e62015-12-30 11:49:04 +1300405 char const *m_name;
Karl Schultz6addd812016-02-02 17:17:23 -0700406 VkDeviceObj *m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600407};
408
Karl Schultz6addd812016-02-02 17:17:23 -0700409class VkPipelineObj : public vk_testing::Pipeline {
410 public:
Tony Barbour6918cd52015-04-09 12:58:51 -0600411 VkPipelineObj(VkDeviceObj *device);
Karl Schultz6addd812016-02-02 17:17:23 -0700412 void AddShader(VkShaderObj *shaderObj);
413 void AddVertexInputAttribs(VkVertexInputAttributeDescription *vi_attrib,
Mark Mueller979ba7b2016-06-22 19:40:33 -0600414 unsigned count);
Karl Schultz6addd812016-02-02 17:17:23 -0700415 void AddVertexInputBindings(VkVertexInputBindingDescription *vi_binding,
Mark Mueller979ba7b2016-06-22 19:40:33 -0600416 unsigned count);
Karl Schultz6addd812016-02-02 17:17:23 -0700417 void AddColorAttachment(uint32_t binding,
418 const VkPipelineColorBlendAttachmentState *att);
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -0600419 void MakeDynamic(VkDynamicState state);
Chia-I Wu08accc62015-07-07 11:50:03 +0800420
Karl Schultz6addd812016-02-02 17:17:23 -0700421 void AddColorAttachment() {
Tony Barbourdd6e32e2015-07-10 15:29:03 -0600422 VkPipelineColorBlendAttachmentState att = {};
Chia-I Wu08accc62015-07-07 11:50:03 +0800423 att.blendEnable = VK_FALSE;
Chia-I Wu1b99bb22015-10-27 19:25:11 +0800424 att.colorWriteMask = 0xf;
Chia-I Wu08accc62015-07-07 11:50:03 +0800425 AddColorAttachment(0, &att);
426 }
427
Tobin Ehlis7a1d2352016-03-28 11:18:19 -0600428 void SetDepthStencil(const VkPipelineDepthStencilStateCreateInfo *);
429 void SetMSAA(const VkPipelineMultisampleStateCreateInfo *ms_state);
Chris Forbesb4cacb62016-04-04 19:15:00 +1200430 void SetInputAssembly(const VkPipelineInputAssemblyStateCreateInfo *ia_state);
431 void SetRasterization(const VkPipelineRasterizationStateCreateInfo *rs_state);
432 void SetTessellation(const VkPipelineTessellationStateCreateInfo *te_state);
Tobin Ehlis7a1d2352016-03-28 11:18:19 -0600433 void SetViewport(const vector<VkViewport> viewports);
434 void SetScissor(const vector<VkRect2D> scissors);
Karl Schultz6addd812016-02-02 17:17:23 -0700435 VkResult CreateVKPipeline(VkPipelineLayout layout,
436 VkRenderPass render_pass);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600437
Karl Schultz6addd812016-02-02 17:17:23 -0700438 protected:
439 VkPipelineVertexInputStateCreateInfo m_vi_state;
440 VkPipelineInputAssemblyStateCreateInfo m_ia_state;
441 VkPipelineRasterizationStateCreateInfo m_rs_state;
442 VkPipelineColorBlendStateCreateInfo m_cb_state;
443 VkPipelineDepthStencilStateCreateInfo m_ds_state;
444 VkPipelineViewportStateCreateInfo m_vp_state;
445 VkPipelineMultisampleStateCreateInfo m_ms_state;
Chris Forbesb4cacb62016-04-04 19:15:00 +1200446 VkPipelineTessellationStateCreateInfo m_te_state;
Karl Schultz6addd812016-02-02 17:17:23 -0700447 vector<VkDynamicState> m_dynamic_state_enables;
448 vector<VkViewport> m_viewports;
449 vector<VkRect2D> m_scissors;
450 VkDeviceObj *m_device;
451 vector<VkShaderObj *> m_shaderObjs;
452 vector<int> m_vertexBufferBindings;
453 vector<VkPipelineColorBlendAttachmentState> m_colorAttachments;
454 int m_vertexBufferCount;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600455};
Mark Mueller979ba7b2016-06-22 19:40:33 -0600456class cMemoryBuffer {
457public:
458 cMemoryBuffer(VkDeviceObj *aVulkanDevice, VkBufferUsageFlags aBufferUsage, VkDeviceSize &aBufferByteCount, const float *aClientData) :
459 AllocateCurrent(false),
460 BoundCurrent(false),
461 CreateCurrent(false),
462 VulkanDevice(aVulkanDevice->device()) {
463
464 VkBufferCreateInfo buffer_create_info = {};
465 buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
466 buffer_create_info.size = aBufferByteCount;
467 buffer_create_info.usage = aBufferUsage;
468
469 VkResult returnValue = vkCreateBuffer(VulkanDevice, &buffer_create_info, nullptr, &VulkanBuffer);
470 assert(!returnValue);
471 CreateCurrent = true;
472
473 VkMemoryRequirements memory_requirements;
474 vkGetBufferMemoryRequirements(VulkanDevice, VulkanBuffer, &memory_requirements);
475
476 VkMemoryAllocateInfo memory_allocate_info = {};
477 memory_allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
478 memory_allocate_info.allocationSize = memory_requirements.size;
479 bool pass = aVulkanDevice->phy().set_memory_type(memory_requirements.memoryTypeBits, &memory_allocate_info, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
480 if (!pass) {
481 vkDestroyBuffer(VulkanDevice, VulkanBuffer, nullptr);
482 return;
483 }
484
485 returnValue = vkAllocateMemory(VulkanDevice, &memory_allocate_info, NULL, &VulkanMemory);
486 assert(!returnValue);
487 AllocateCurrent = true;
488
489 void *mappedMemory;
490 returnValue = vkMapMemory(VulkanDevice, VulkanMemory, 0, memory_allocate_info.allocationSize, 0, &mappedMemory);
491 assert(!returnValue);
492
493 memcpy(mappedMemory, aClientData, static_cast<size_t>(aBufferByteCount));
494
495 vkUnmapMemory(VulkanDevice, VulkanMemory);
496
497 returnValue = vkBindBufferMemory(VulkanDevice, VulkanBuffer, VulkanMemory, 0);
498 assert(!returnValue);
499 BoundCurrent = true;
500 }
501
502 ~cMemoryBuffer() {
503 if (CreateCurrent) {
504 vkDestroyBuffer(VulkanDevice, VulkanBuffer, nullptr);
505 }
506 if (AllocateCurrent) {
507 vkFreeMemory(VulkanDevice, VulkanMemory, nullptr);
508 }
509 }
510
511 const VkBuffer &GetBuffer() {
512 return VulkanBuffer;
513 }
514
515protected:
516 bool AllocateCurrent;
517 bool BoundCurrent;
518 bool CreateCurrent;
519
520 VkBuffer VulkanBuffer;
521 VkDevice VulkanDevice;
522 VkDeviceMemory VulkanMemory;
523
524};
525
526class cVertices {
527public:
528 cVertices(VkDeviceObj *aVulkanDevice, unsigned aAttributeCount, unsigned aBindingCount, unsigned aByteStride, VkDeviceSize aVertexCount, const float *aVerticies) :
529 BoundCurrent(false),
530 AttributeCount(aAttributeCount),
531 BindingCount(aBindingCount),
532 BindId(BindIdGenerator),
533 PipelineVertexInputStateCreateInfo(),
534 VulkanDevice(aVulkanDevice->device()),
535 VulkanMemoryBuffer(aVulkanDevice, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, aVertexCount, aVerticies) {
536 BindIdGenerator++; // NB: This can wrap w/misuse
537 VertexInputAttributeDescription = new VkVertexInputAttributeDescription[AttributeCount];
538 VertexInputBindingDescription = new VkVertexInputBindingDescription[BindingCount];
539
540 PipelineVertexInputStateCreateInfo.pVertexAttributeDescriptions = VertexInputAttributeDescription;
541 PipelineVertexInputStateCreateInfo.vertexAttributeDescriptionCount = AttributeCount;
542 PipelineVertexInputStateCreateInfo.pVertexBindingDescriptions = VertexInputBindingDescription;
543 PipelineVertexInputStateCreateInfo.vertexBindingDescriptionCount = BindingCount;
544 PipelineVertexInputStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
545
546 unsigned i = 0;
547 do {
548 VertexInputAttributeDescription[i].binding = BindId;
549 VertexInputAttributeDescription[i].location = i;
550 VertexInputAttributeDescription[i].format = VK_FORMAT_R32G32B32_SFLOAT;
551 VertexInputAttributeDescription[i].offset = sizeof(float) * aByteStride;
552 i++;
553 } while (AttributeCount < i);
554
555 i = 0;
556 do {
557 VertexInputBindingDescription[i].binding = BindId;
558 VertexInputBindingDescription[i].stride = aByteStride;
559 VertexInputBindingDescription[i].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
560 i++;
561 } while (BindingCount < i);
562 }
563
564 ~cVertices() {
565 if (VertexInputAttributeDescription) {
566 delete[] VertexInputAttributeDescription;
567 }
568 if (VertexInputBindingDescription) {
569 delete[] VertexInputBindingDescription;
570 }
571 }
572
573 void BindVertexBuffers(VkCommandBuffer aCommandBuffer, unsigned aOffsetCount = 0, VkDeviceSize *aOffsetList = nullptr) {
574 VkDeviceSize *offsetList;
575 unsigned offsetCount;
576
577 if (aOffsetCount) {
578 offsetList = aOffsetList;
579 offsetCount = aOffsetCount;
580 }
581 else {
582 offsetList = new VkDeviceSize[1]();
583 offsetCount = 1;
584 }
585
586 vkCmdBindVertexBuffers(aCommandBuffer, BindId, offsetCount, &VulkanMemoryBuffer.GetBuffer(), offsetList);
587 BoundCurrent = true;
588
589 if (!aOffsetCount) {
590 delete [] offsetList;
591 }
592 }
593
594 void AddVertexInputToPipe(VkPipelineObj &aPipelineObj) {
595 aPipelineObj.AddVertexInputAttribs(VertexInputAttributeDescription, AttributeCount);
596 aPipelineObj.AddVertexInputBindings(VertexInputBindingDescription, BindingCount);
597 }
598
599protected:
600 static unsigned BindIdGenerator;
601
602 bool BoundCurrent;
603 unsigned AttributeCount;
604 unsigned BindingCount;
605 uint32_t BindId;
606 VkDeviceMemory DeviceMemory;
607
608 VkPipelineVertexInputStateCreateInfo PipelineVertexInputStateCreateInfo;
609 VkVertexInputAttributeDescription *VertexInputAttributeDescription;
610 VkVertexInputBindingDescription *VertexInputBindingDescription;
611 VkDevice VulkanDevice;
612 cMemoryBuffer VulkanMemoryBuffer;
613};
614
615
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600616#endif // VKRENDERFRAMEWORK_H