blob: d571d8ff7a6759fc51bfe64cfdfc529d1ba799d3 [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Ian Elliott7e40db92015-08-21 15:09:33 -060029#include <vk_ext_khr_swapchain.h>
30#include <vk_ext_khr_device_swapchain.h>
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060031
Jon Ashburn83a64252015-04-15 11:31:12 -060032#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
Tony Barbour6a3faf02015-07-23 10:36:18 -060033#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
34{ \
35 fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
36 assert(fp##entrypoint != NULL); \
37}
Jon Ashburn83a64252015-04-15 11:31:12 -060038
Tony Barbour6918cd52015-04-09 12:58:51 -060039VkRenderFramework::VkRenderFramework() :
Tony Barbourfe3351b2015-07-28 10:17:20 -060040 m_cmdBuffer(),
Tony Barbourf77fd652015-04-09 11:07:02 -060041 m_renderPass(VK_NULL_HANDLE),
42 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060043 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070044 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060045 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
46 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Tony Barbour71bd4b32015-07-21 17:00:26 -060047 m_clear_via_load_op( true ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070048 m_depth_clear_color( 1.0 ),
Courtney Goeltzenleuchterb6c9aca2015-06-08 16:19:37 -060049 m_stencil_clear_color( 0 ),
50 m_depthStencil( NULL ),
51 m_dbgCreateMsgCallback( VK_NULL_HANDLE ),
52 m_dbgDestroyMsgCallback( VK_NULL_HANDLE ),
53 m_globalMsgCallback( VK_NULL_HANDLE ),
54 m_devMsgCallback( VK_NULL_HANDLE )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060055{
Tony Barbour1c45ce02015-03-27 17:03:18 -060056
Chia-I Wu08accc62015-07-07 11:50:03 +080057 memset(&m_renderPassBeginInfo, 0, sizeof(m_renderPassBeginInfo));
58 m_renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
59
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070060 // clear the back buffer to dark grey
Cody Northrop85789d52015-08-25 15:26:38 -060061 m_clear_color.float32[0] = 0.25f;
62 m_clear_color.float32[1] = 0.25f;
63 m_clear_color.float32[2] = 0.25f;
64 m_clear_color.float32[3] = 0.0f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060065}
66
Tony Barbour6918cd52015-04-09 12:58:51 -060067VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060068{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060069
70}
71
Tony Barbour6918cd52015-04-09 12:58:51 -060072void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060073{
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060074 std::vector<const char*> instance_layer_names;
75 std::vector<const char*> device_layer_names;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060076 std::vector<const char*> instance_extension_names;
77 std::vector<const char*> device_extension_names;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060078 InitFramework(
79 instance_layer_names, device_layer_names,
80 instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060081}
82
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060083void VkRenderFramework::InitFramework(
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060084 std::vector<const char *> instance_layer_names,
85 std::vector<const char *> device_layer_names,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060086 std::vector<const char *> instance_extension_names,
87 std::vector<const char *> device_extension_names,
88 PFN_vkDbgMsgCallback dbgFunction,
89 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060090{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060091 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060092 std::vector<VkExtensionProperties> instance_extensions;
93 std::vector<VkExtensionProperties> device_extensions;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060094 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060095
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -060096 /* TODO: Verify requested extensions are available */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060097
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060098 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060099 instInfo.pNext = NULL;
100 instInfo.pAppInfo = &app_info;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800101 instInfo.enabledLayerNameCount = instance_layer_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600102 instInfo.ppEnabledLayerNames = instance_layer_names.data();
Chia-I Wud50a7d72015-10-26 20:48:51 +0800103 instInfo.enabledExtensionNameCount = instance_extension_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600104 instInfo.ppEnabledExtensionNames = instance_extension_names.data();
Chia-I Wuf7458c52015-10-26 21:10:41 +0800105 err = vkCreateInstance(&instInfo, NULL, &this->inst);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600106 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600107
Jon Ashburn83a64252015-04-15 11:31:12 -0600108 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
109 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
110 ASSERT_VK_SUCCESS(err);
111 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600112 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -0600113 ASSERT_GE(this->gpu_count, (uint32_t) 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600114 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600115 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
116 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
117 if (m_dbgCreateMsgCallback) {
118 err = m_dbgCreateMsgCallback(this->inst,
119 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
120 dbgFunction,
121 userData,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600122 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600123 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600124
125 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
126 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600127 }
Tony Barbour15524c32015-04-29 17:34:29 -0600128 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600129
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600130 /* TODO: Verify requested physical device extensions are available */
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600131 m_device = new VkDeviceObj(0, objs[0], device_layer_names, device_extension_names);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600132
133 /* Now register callback on device */
134 if (0) {
135 if (m_dbgCreateMsgCallback) {
136 err = m_dbgCreateMsgCallback(this->inst,
137 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
138 dbgFunction,
139 userData,
140 &m_devMsgCallback);
141 ASSERT_VK_SUCCESS(err);
142 }
143 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600144 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600145
Tony Barbour6918cd52015-04-09 12:58:51 -0600146 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600147}
148
Tony Barbour6918cd52015-04-09 12:58:51 -0600149void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600150{
Tony Barbourfe3351b2015-07-28 10:17:20 -0600151 if (m_cmdBuffer)
152 delete m_cmdBuffer;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800153 if (m_cmdPool) vkDestroyCommandPool(device(), m_cmdPool, NULL);
154 if (m_framebuffer) vkDestroyFramebuffer(device(), m_framebuffer, NULL);
155 if (m_renderPass) vkDestroyRenderPass(device(), m_renderPass, NULL);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600156
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600157 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
158 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
159
Tobin Ehlis976fc162015-03-26 08:23:25 -0600160 while (!m_renderTargets.empty()) {
Chia-I Wuf7458c52015-10-26 21:10:41 +0800161 vkDestroyImageView(device(), m_renderTargets.back()->targetView(m_render_target_fmt), NULL);
162 vkDestroyImage(device(), m_renderTargets.back()->image(), NULL);
163 vkFreeMemory(device(), m_renderTargets.back()->memory(), NULL);
Tobin Ehlis976fc162015-03-26 08:23:25 -0600164 m_renderTargets.pop_back();
165 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600166
Tony Barbour1c45ce02015-03-27 17:03:18 -0600167 delete m_depthStencil;
Tony Barbour823e6ca2015-07-27 13:31:04 -0600168 while (!m_shader_modules.empty())
169 {
170 delete m_shader_modules.back();
171 m_shader_modules.pop_back();
172 }
Tony Barbour1c45ce02015-03-27 17:03:18 -0600173
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600174 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800175 delete m_device;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800176 if (this->inst) vkDestroyInstance(this->inst, NULL);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600177}
178
Tony Barbour6918cd52015-04-09 12:58:51 -0600179void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600180{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600181 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600182
Tony Barbour6a3faf02015-07-23 10:36:18 -0600183 // Get the list of VkFormat's that are supported:
Ian Elliott7e40db92015-08-21 15:09:33 -0600184 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
Ian Elliott8b139792015-08-07 11:51:12 -0600185 uint32_t formatCount;
Ian Elliott7e40db92015-08-21 15:09:33 -0600186 VkSurfaceDescriptionKHR surface_description;
187 surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Tony Barbour6a3faf02015-07-23 10:36:18 -0600188 surface_description.pNext = NULL;
Ian Elliott7e40db92015-08-21 15:09:33 -0600189 GET_DEVICE_PROC_ADDR(device(), GetSurfaceFormatsKHR);
190 err = fpGetSurfaceFormatsKHR(device(),
191 (VkSurfaceDescriptionKHR *) &surface_description,
Ian Elliott8b139792015-08-07 11:51:12 -0600192 &formatCount, NULL);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600193 ASSERT_VK_SUCCESS(err);
Ian Elliott7e40db92015-08-21 15:09:33 -0600194 VkSurfaceFormatKHR *surfFormats =
195 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
196 err = fpGetSurfaceFormatsKHR(device(),
197 (VkSurfaceDescriptionKHR *) &surface_description,
Ian Elliott8b139792015-08-07 11:51:12 -0600198 &formatCount, surfFormats);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600199 ASSERT_VK_SUCCESS(err);
200 m_render_target_fmt = surfFormats[0].format;
201 free(surfFormats);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600202
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600203 m_lineWidth = 1.0f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700204
Chia-I Wud8c946a2015-10-26 19:08:09 +0800205 m_depthBiasConstantFactor = 0.0f;
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600206 m_depthBiasClamp = 0.0f;
Chia-I Wud8c946a2015-10-26 19:08:09 +0800207 m_depthBiasSlopeFactor = 0.0f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600208
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600209 m_blendConst[0] = 1.0f;
210 m_blendConst[1] = 1.0f;
211 m_blendConst[2] = 1.0f;
212 m_blendConst[3] = 1.0f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600213
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600214 m_minDepthBounds = 0.f;
215 m_maxDepthBounds = 1.f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600216
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600217 m_stencilCompareMask = 0xff;
218 m_stencilWriteMask = 0xff;
219 m_stencilReference = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600220
Cody Northrope62183e2015-07-09 18:08:05 -0600221 VkCmdPoolCreateInfo cmd_pool_info;
222 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
223 cmd_pool_info.pNext = NULL,
224 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
225 cmd_pool_info.flags = 0,
Chia-I Wuf7458c52015-10-26 21:10:41 +0800226 err = vkCreateCommandPool(device(), &cmd_pool_info, NULL, &m_cmdPool);
Cody Northrope62183e2015-07-09 18:08:05 -0600227 assert(!err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600228
Tony Barbourfe3351b2015-07-28 10:17:20 -0600229 m_cmdBuffer = new VkCommandBufferObj(m_device, m_cmdPool);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600230}
231
Tony Barbour6918cd52015-04-09 12:58:51 -0600232void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600233{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600234 VkViewport viewport;
Chris Forbesd9be82b2015-06-22 17:21:59 +1200235 VkRect2D scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700236 viewport.originX = 0;
237 viewport.originY = 0;
238 viewport.width = 1.f * width;
239 viewport.height = 1.f * height;
240 viewport.minDepth = 0.f;
241 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600242 m_viewports.push_back(viewport);
243
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600244 scissor.extent.width = (int32_t) width;
245 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700246 scissor.offset.x = 0;
247 scissor.offset.y = 0;
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600248 m_scissors.push_back(scissor);
Tony Barbourf52346d2015-01-16 14:27:35 -0700249
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600250 m_width = width;
251 m_height = height;
252}
253
Tony Barbour6918cd52015-04-09 12:58:51 -0600254void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600255{
256 InitViewport(m_width, m_height);
257}
Tony Barbour6918cd52015-04-09 12:58:51 -0600258void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600259{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700260 InitRenderTarget(1);
261}
262
Tony Barbour6918cd52015-04-09 12:58:51 -0600263void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700264{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600265 InitRenderTarget(targets, NULL);
266}
267
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600268void VkRenderFramework::InitRenderTarget(VkImageView *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600269{
270 InitRenderTarget(1, dsBinding);
271}
272
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600273void VkRenderFramework::InitRenderTarget(uint32_t targets, VkImageView *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600274{
Chia-I Wu08accc62015-07-07 11:50:03 +0800275 std::vector<VkAttachmentDescription> attachments;
276 std::vector<VkAttachmentReference> color_references;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600277 std::vector<VkImageView> bindings;
Cody Northropd2ad0342015-08-05 11:15:02 -0600278 attachments.reserve(targets + 1); // +1 for dsBinding
Chia-I Wu08accc62015-07-07 11:50:03 +0800279 color_references.reserve(targets);
Cody Northropd2ad0342015-08-05 11:15:02 -0600280 bindings.reserve(targets + 1); // +1 for dsBinding
Tony Barbour1c45ce02015-03-27 17:03:18 -0600281
Chia-I Wu08accc62015-07-07 11:50:03 +0800282 VkAttachmentDescription att = {};
283 att.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION;
284 att.pNext = NULL;
285 att.format = m_render_target_fmt;
286 att.samples = 1;
287 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
288 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
289 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
290 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
291 att.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
292 att.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chia-I Wuecebf752014-12-05 10:45:15 +0800293
Chia-I Wu08accc62015-07-07 11:50:03 +0800294 VkAttachmentReference ref = {};
295 ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
296
297 m_renderPassClearValues.clear();
298 VkClearValue clear = {};
299 clear.color = m_clear_color;
300
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600301 VkImageView bind = {};
Chia-I Wu08accc62015-07-07 11:50:03 +0800302
303 for (uint32_t i = 0; i < targets; i++) {
304 attachments.push_back(att);
305
306 ref.attachment = i;
307 color_references.push_back(ref);
308
309 m_renderPassClearValues.push_back(clear);
310
Tony Barbour6918cd52015-04-09 12:58:51 -0600311 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600312
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600313 VkFormatProperties props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600314
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600315 vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), m_render_target_fmt, &props);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600316
317 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600318 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600319 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
320 VK_IMAGE_TILING_LINEAR);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600321 }
322 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600323 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600324 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
325 VK_IMAGE_TILING_OPTIMAL);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600326 }
327 else {
328 FAIL() << "Neither Linear nor Optimal allowed for render target";
329 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600330
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600331 m_renderTargets.push_back(img);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600332 bind = img->targetView(m_render_target_fmt);
Chia-I Wu08accc62015-07-07 11:50:03 +0800333 bindings.push_back(bind);
Chia-I Wuecebf752014-12-05 10:45:15 +0800334 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600335
Chia-I Wu08accc62015-07-07 11:50:03 +0800336 VkSubpassDescription subpass = {};
337 subpass.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION;
338 subpass.pNext = NULL;
339 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
340 subpass.flags = 0;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800341 subpass.inputAttachmentCount = 0;
Cody Northropa505dda2015-08-04 11:16:41 -0600342 subpass.pInputAttachments = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800343 subpass.colorAttachmentCount = targets;
Cody Northropa505dda2015-08-04 11:16:41 -0600344 subpass.pColorAttachments = color_references.data();
345 subpass.pResolveAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800346
347 if (dsBinding) {
348 att.format = m_depth_stencil_fmt;
Tony Barbour71bd4b32015-07-21 17:00:26 -0600349 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;;
Chia-I Wu08accc62015-07-07 11:50:03 +0800350 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
351 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
352 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Cody Northrop78e71932015-09-03 16:09:57 -0600353 att.initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
354 att.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800355 attachments.push_back(att);
356
Cody Northrop85789d52015-08-25 15:26:38 -0600357 clear.depthStencil.depth = m_depth_clear_color;
358 clear.depthStencil.stencil = m_stencil_clear_color;
Chia-I Wu08accc62015-07-07 11:50:03 +0800359 m_renderPassClearValues.push_back(clear);
360
361 bindings.push_back(*dsBinding);
362
363 subpass.depthStencilAttachment.attachment = targets;
Cody Northrop78e71932015-09-03 16:09:57 -0600364 subpass.depthStencilAttachment.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800365 } else {
366 subpass.depthStencilAttachment.attachment = VK_ATTACHMENT_UNUSED;
367 }
368
Chia-I Wud50a7d72015-10-26 20:48:51 +0800369 subpass.preserveAttachmentCount = 0;
Cody Northropa505dda2015-08-04 11:16:41 -0600370 subpass.pPreserveAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800371
372 VkRenderPassCreateInfo rp_info = {};
373 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
374 rp_info.attachmentCount = attachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600375 rp_info.pAttachments = attachments.data();
Chia-I Wu08accc62015-07-07 11:50:03 +0800376 rp_info.subpassCount = 1;
377 rp_info.pSubpasses = &subpass;
378
Chia-I Wuf7458c52015-10-26 21:10:41 +0800379 vkCreateRenderPass(device(), &rp_info, NULL, &m_renderPass);
Chia-I Wu08accc62015-07-07 11:50:03 +0800380
381 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600382 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600383 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600384 fb_info.pNext = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800385 fb_info.renderPass = m_renderPass;
386 fb_info.attachmentCount = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600387 fb_info.pAttachments = bindings.data();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600388 fb_info.width = (uint32_t)m_width;
389 fb_info.height = (uint32_t)m_height;
390 fb_info.layers = 1;
391
Chia-I Wuf7458c52015-10-26 21:10:41 +0800392 vkCreateFramebuffer(device(), &fb_info, NULL, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600393
Chia-I Wu08accc62015-07-07 11:50:03 +0800394 m_renderPassBeginInfo.renderPass = m_renderPass;
395 m_renderPassBeginInfo.framebuffer = m_framebuffer;
Cody Northropd2ad0342015-08-05 11:15:02 -0600396 m_renderPassBeginInfo.renderArea.extent.width = (int32_t) m_width;
397 m_renderPassBeginInfo.renderArea.extent.height = (int32_t) m_height;
Cody Northrop23dd89d2015-08-04 11:51:03 -0600398 m_renderPassBeginInfo.clearValueCount = m_renderPassClearValues.size();
399 m_renderPassBeginInfo.pClearValues = m_renderPassClearValues.data();
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600400}
401
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600402
403
Tony Barbourd1c35722015-04-16 15:59:00 -0600404VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600405 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800406{
407 init();
408
Chia-I Wu999f0482015-07-03 10:32:05 +0800409 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600410 queue_props = phy().queue_properties().data();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800411}
412
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600413VkDeviceObj::VkDeviceObj(uint32_t id,
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600414 VkPhysicalDevice obj, std::vector<const char *> &layer_names,
415 std::vector<const char *> &extension_names) :
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600416 vk_testing::Device(obj), id(id)
417{
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600418 init(layer_names, extension_names);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600419
Chia-I Wu999f0482015-07-03 10:32:05 +0800420 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600421 queue_props = phy().queue_properties().data();
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600422}
423
Tony Barbour6918cd52015-04-09 12:58:51 -0600424void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800425{
426 ASSERT_NE(true, graphics_queues().empty());
Chia-I Wudf12ffd2015-07-03 10:53:18 +0800427 m_queue = graphics_queues()[0]->handle();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800428}
429
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600430VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
431 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700432{
Tony Barboure2c58df2014-11-25 13:18:32 -0700433
434}
435
Tony Barbour6918cd52015-04-09 12:58:51 -0600436VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700437{
Chia-I Wu11078b02015-01-04 16:27:24 +0800438 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700439}
Tony Barbour82c39522014-12-04 14:33:33 -0700440
Tony Barbour6918cd52015-04-09 12:58:51 -0600441int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700442{
Chia-I Wu11078b02015-01-04 16:27:24 +0800443 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600444 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600445 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800446 tc.descriptorCount = 1;
Chia-I Wu11078b02015-01-04 16:27:24 +0800447 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700448
Chia-I Wu11078b02015-01-04 16:27:24 +0800449 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700450}
Tony Barbour82c39522014-12-04 14:33:33 -0700451
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600452int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700453{
Courtney Goeltzenleuchter6eb1aeb2015-09-11 15:29:21 -0600454 assert(type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
455 type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
456 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
457 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600458 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800459 tc.type = type;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800460 tc.descriptorCount = 1;
Chia-I Wu11078b02015-01-04 16:27:24 +0800461 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700462
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800463 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600464 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorBufferInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600465
Chia-I Wu11078b02015-01-04 16:27:24 +0800466 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700467}
Tony Barbour82c39522014-12-04 14:33:33 -0700468
Tony Barbour6918cd52015-04-09 12:58:51 -0600469int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700470{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600471 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600472 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800473 tc.descriptorCount = 1;
Chia-I Wu11078b02015-01-04 16:27:24 +0800474 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700475
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600476 VkDescriptorImageInfo tmp = texture->m_imageInfo;
477 tmp.sampler = sampler->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800478 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700479
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800480 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600481 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &tmp));
Tony Barboure2c58df2014-11-25 13:18:32 -0700482
Chia-I Wu11078b02015-01-04 16:27:24 +0800483 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700484}
Chia-I Wu11078b02015-01-04 16:27:24 +0800485
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500486VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700487{
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800488 return m_pipeline_layout.handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700489}
490
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600491VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700492{
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800493 return m_set->handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700494}
Tony Barboure2c58df2014-11-25 13:18:32 -0700495
Tony Barbour6918cd52015-04-09 12:58:51 -0600496void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700497{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600498 // create VkDescriptorPool
499 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600500 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800501 pool.typeCount = m_type_counts.size();
Courtney Goeltzenleuchterfe908d32015-09-16 16:12:45 -0600502 pool.maxSets = 1;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800503 pool.pTypeCounts = m_type_counts.data();
Courtney Goeltzenleuchterfe908d32015-09-16 16:12:45 -0600504 init(*m_device, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700505
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600506 // create VkDescriptorSetLayout
507 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800508 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800509 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800510 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800511 bindings[i].arraySize = m_type_counts[i].descriptorCount;
Tony Barbourd1c35722015-04-16 15:59:00 -0600512 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800513 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700514 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800515
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600516 // create VkDescriptorSetLayout
517 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600518 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800519 layout.bindingCount = bindings.size();
520 layout.pBindings = bindings.data();
Chia-I Wub41393e2015-03-26 15:04:41 +0800521
Chia-I Wu41126e52015-03-26 15:27:55 +0800522 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600523 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800524 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500525
526 // create VkPipelineLayout
527 VkPipelineLayoutCreateInfo pipeline_layout = {};
528 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800529 pipeline_layout.setLayoutCount = layouts.size();
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500530 pipeline_layout.pSetLayouts = NULL;
531
532 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700533
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600534 // create VkDescriptorSet
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600535 m_set = alloc_sets(*m_device, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800536
Chia-I Wu41126e52015-03-26 15:27:55 +0800537 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800538 size_t imageSamplerCount = 0;
539 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
540 it != m_writes.end(); it++) {
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800541 it->destSet = m_set->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800542 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600543 it->pImageInfo = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800544 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800545
546 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800547 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700548}
Tony Barboure2c58df2014-11-25 13:18:32 -0700549
Tony Barbour6918cd52015-04-09 12:58:51 -0600550VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800551{
552 m_device = dev;
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600553 m_descriptorImageInfo.imageView = VK_NULL_HANDLE;
554 m_descriptorImageInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800555}
556
Tony Barbour6918cd52015-04-09 12:58:51 -0600557void VkImageObj::ImageMemoryBarrier(
558 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600559 VkImageAspectFlags aspect,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600560 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600561 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600562 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
563 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
564 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
565 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600566 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600567 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600568 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
569 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
570 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
571 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
572 VK_MEMORY_INPUT_SHADER_READ_BIT |
573 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
574 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
575 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600576 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600577{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600578 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
579 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600580 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
581 subresourceRange);
582
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600583 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600584
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600585 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
586 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600587
588 // write barrier to the command buffer
Chia-I Wu53534662015-10-26 17:08:33 +0800589 vkCmdPipelineBarrier(cmd_buf->handle(), src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600590}
591
Tony Barbour6918cd52015-04-09 12:58:51 -0600592void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600593 VkImageAspectFlagBits aspect,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600594 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600595{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600596 VkFlags output_mask, input_mask;
597 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600598 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600599 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
600 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
601 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600602 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600603 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600604 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600605 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
606 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
607 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
608 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
609 VK_MEMORY_INPUT_SHADER_READ_BIT |
610 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
611 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600612 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600613
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600614 if (image_layout == m_descriptorImageInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600615 return;
616 }
617
618 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600619 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600620 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
621 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600622 break;
623
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600624 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600625 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
626 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600627 break;
628
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600629 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600630 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
631 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600632 break;
633
634 default:
635 output_mask = all_cache_outputs;
636 input_mask = all_cache_inputs;
637 break;
638 }
639
640 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600641 m_descriptorImageInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600642}
643
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600644void VkImageObj::SetLayout(VkImageAspectFlagBits aspect,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600645 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600646{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600647 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600648
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600649 if (image_layout == m_descriptorImageInfo.imageLayout) {
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600650 return;
651 }
652
Tony Barbourfe3351b2015-07-28 10:17:20 -0600653 VkCmdPoolCreateInfo cmd_pool_info = {};
654 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
655 cmd_pool_info.pNext = NULL;
656 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
657 cmd_pool_info.flags = 0;
658 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
659 VkCommandBufferObj cmd_buf(m_device, pool.handle());
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600660
661 /* Build command buffer to set image layout in the driver */
662 err = cmd_buf.BeginCommandBuffer();
663 assert(!err);
664
665 SetLayout(&cmd_buf, aspect, image_layout);
666
667 err = cmd_buf.EndCommandBuffer();
668 assert(!err);
669
670 cmd_buf.QueueCommandBuffer();
671}
672
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600673bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600674{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600675 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600676 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600677 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600678
Tony Barbour579f7802015-04-03 15:11:43 -0600679 return true;
680}
681
Tony Barbour6918cd52015-04-09 12:58:51 -0600682void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600683 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600684 VkImageTiling requested_tiling,
685 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800686{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600687 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600688 VkFormatProperties image_fmt;
689 VkImageTiling tiling;
Tony Barbour579f7802015-04-03 15:11:43 -0600690
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800691 mipCount = 0;
692
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600693 uint32_t _w = w;
694 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800695 while( ( _w > 0 ) || ( _h > 0 ) )
696 {
697 _w >>= 1;
698 _h >>= 1;
699 mipCount++;
700 }
701
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600702 vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), fmt, &image_fmt);
Tony Barbour579f7802015-04-03 15:11:43 -0600703
Tony Barbourd1c35722015-04-16 15:59:00 -0600704 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600705 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600706 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600707 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600708 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600709 } else {
710 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
711 }
712 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600713 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600714 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600715 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600716 } else {
717 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
718 }
719
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600720 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600721 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800722 imageCreateInfo.format = fmt;
723 imageCreateInfo.extent.width = w;
724 imageCreateInfo.extent.height = h;
725 imageCreateInfo.mipLevels = mipCount;
726 imageCreateInfo.tiling = tiling;
727
728 imageCreateInfo.usage = usage;
729
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600730 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800731
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600732 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600733 SetLayout(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600734 } else {
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600735 SetLayout(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600736 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800737}
738
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600739VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600740{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600741 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600742 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600743
Tony Barbourfe3351b2015-07-28 10:17:20 -0600744 VkCmdPoolCreateInfo cmd_pool_info = {};
745 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
746 cmd_pool_info.pNext = NULL;
747 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
748 cmd_pool_info.flags = 0;
749 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
750 VkCommandBufferObj cmd_buf(m_device, pool.handle());
751
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600752 /* Build command buffer to copy staging texture to usable texture */
753 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600754 assert(!err);
755
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600756 /* TODO: Can we determine image aspect from image object? */
757 src_image_layout = src_image.layout();
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600758 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600759
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600760 dest_image_layout = this->layout();
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600761 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600762
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600763 VkImageCopy copy_region = {};
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600764 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR_BIT;
Courtney Goeltzenleuchter8367ce02015-10-16 09:46:00 -0600765 copy_region.srcSubresource.baseArrayLayer = 0;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600766 copy_region.srcSubresource.mipLevel = 0;
767 copy_region.srcOffset.x = 0;
768 copy_region.srcOffset.y = 0;
769 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600770 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR_BIT;
Courtney Goeltzenleuchter8367ce02015-10-16 09:46:00 -0600771 copy_region.destSubresource.baseArrayLayer = 0;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600772 copy_region.destSubresource.mipLevel = 0;
773 copy_region.destOffset.x = 0;
774 copy_region.destOffset.y = 0;
775 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600776 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600777
Chia-I Wube2b9172015-07-03 11:49:42 +0800778 vkCmdCopyImage(cmd_buf.handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +0800779 src_image.handle(), src_image.layout(),
780 handle(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600781 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600782
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600783 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR_BIT, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600784
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600785 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR_BIT, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600786
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600787 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600788 assert(!err);
789
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600790 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600791
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600792 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600793}
794
Tony Barbour6918cd52015-04-09 12:58:51 -0600795VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
796 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700797{
798 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600799 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600800 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600801 void *data;
802 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600803 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600804 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600805
Tony Barboure65788f2015-07-21 17:01:42 -0600806 stagingImage.init(16, 16, tex_format, VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600807 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR_BIT, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600808
809 if (colors == NULL)
810 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700811
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600812 memset(&m_imageInfo,0,sizeof(m_imageInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700813
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600814 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600815 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600816 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600817 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600818 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600819 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600820 view.channels.r = VK_CHANNEL_SWIZZLE_R;
821 view.channels.g = VK_CHANNEL_SWIZZLE_G;
822 view.channels.b = VK_CHANNEL_SWIZZLE_B;
823 view.channels.a = VK_CHANNEL_SWIZZLE_A;
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -0600824 view.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600825 view.subresourceRange.baseMipLevel = 0;
Courtney Goeltzenleuchter8367ce02015-10-16 09:46:00 -0600826 view.subresourceRange.numLevels = 1;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600827 view.subresourceRange.baseArrayLayer = 0;
Courtney Goeltzenleuchter8367ce02015-10-16 09:46:00 -0600828 view.subresourceRange.numLayers = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700829
Tony Barboure2c58df2014-11-25 13:18:32 -0700830 /* create image */
Tony Barboure65788f2015-07-21 17:01:42 -0600831 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700832
833 /* create image view */
Chia-I Wu681d7a02015-07-03 13:44:34 +0800834 view.image = handle();
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800835 m_textureView.init(*m_device, view);
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600836 m_imageInfo.imageView = m_textureView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700837
Chia-I Wu681d7a02015-07-03 13:44:34 +0800838 data = stagingImage.MapMemory();
Tony Barboure2c58df2014-11-25 13:18:32 -0700839
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800840 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700841 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800842 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600843 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700844 }
Chia-I Wu681d7a02015-07-03 13:44:34 +0800845 stagingImage.UnmapMemory();
Tony Barbour6918cd52015-04-09 12:58:51 -0600846 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700847}
Tony Barbour82c39522014-12-04 14:33:33 -0700848
Tony Barbour6918cd52015-04-09 12:58:51 -0600849VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700850{
Tony Barboure2c58df2014-11-25 13:18:32 -0700851 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700852
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600853 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800854 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600855 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
Chia-I Wub99df442015-10-26 16:49:32 +0800856 samplerCreateInfo.magFilter = VK_FILTER_NEAREST;
857 samplerCreateInfo.minFilter = VK_FILTER_NEAREST;
858 samplerCreateInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_BASE;
859 samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_WRAP;
860 samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_WRAP;
861 samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800862 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600863 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600864 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800865 samplerCreateInfo.minLod = 0.0;
866 samplerCreateInfo.maxLod = 0.0;
Tony Barbour26b17f82015-06-25 16:56:44 -0600867 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Mark Lobodzinski52ac6582015-09-01 15:42:56 -0600868 samplerCreateInfo.unnormalizedCoordinates = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700869
Chia-I Wue9864b52014-12-28 16:32:24 +0800870 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700871}
Tony Barboure2c58df2014-11-25 13:18:32 -0700872
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700873/*
874 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
875 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600876VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700877{
878 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700879 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700880
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600881 memset(&m_descriptorBufferInfo,0,sizeof(m_descriptorBufferInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700882}
883
Tony Barbour6918cd52015-04-09 12:58:51 -0600884VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600885{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600886 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600887 if (m_commandBuffer) {
888 delete m_commandBuffer;
Tony Barbour3d83f492015-07-28 10:23:02 -0600889 delete m_cmdPool;
Tony Barbour044703b2015-03-26 12:37:52 -0600890 }
891}
892
Tony Barbour6918cd52015-04-09 12:58:51 -0600893VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700894{
Tony Barboure2c58df2014-11-25 13:18:32 -0700895 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800896 m_commandBuffer = 0;
897
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600898 memset(&m_descriptorBufferInfo,0,sizeof(m_descriptorBufferInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700899 m_numVertices = constantCount;
900 m_stride = constantSize;
901
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600902 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800903 const size_t allocationSize = constantCount * constantSize;
Cody Northrop7fb43862015-06-22 14:56:14 -0600904 init_as_src_and_dst(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700905
Chia-I Wu681d7a02015-07-03 13:44:34 +0800906 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +0800907 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800908 memory().unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700909
Courtney Goeltzenleuchter6eb1aeb2015-09-11 15:29:21 -0600910 /*
911 * Constant buffers are going to be used as vertex input buffers
912 * or as shader uniform buffers. So, we'll create the shaderbuffer
913 * descriptor here so it's ready if needed.
914 */
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -0600915 this->m_descriptorBufferInfo.buffer = handle();
916 this->m_descriptorBufferInfo.offset = 0;
917 this->m_descriptorBufferInfo.range = allocationSize;
Tony Barboure2c58df2014-11-25 13:18:32 -0700918}
Tony Barbour82c39522014-12-04 14:33:33 -0700919
Tony Barbourd1c35722015-04-16 15:59:00 -0600920void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700921{
Chia-I Wu681d7a02015-07-03 13:44:34 +0800922 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &handle(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700923}
924
925
Tony Barbour6918cd52015-04-09 12:58:51 -0600926void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600927 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600928 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600929 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
930 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
931 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
932 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600933 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600934 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600935 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
936 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
937 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
938 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
939 VK_MEMORY_INPUT_SHADER_READ_BIT |
940 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
941 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
942 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700943{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600944 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700945
Tony Barbour38422802014-12-10 14:36:31 -0700946 if (!m_commandBuffer)
947 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600948 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbourfe3351b2015-07-28 10:17:20 -0600949 VkCmdPoolCreateInfo cmd_pool_info = {};
950 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
951 cmd_pool_info.pNext = NULL;
952 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
953 cmd_pool_info.flags = 0;
954 m_cmdPool = new vk_testing::CmdPool(*m_device, cmd_pool_info);
955 m_commandBuffer = new VkCommandBufferObj(m_device, m_cmdPool->handle());
Tony Barbour38422802014-12-10 14:36:31 -0700956 }
957 else
958 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800959 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700960 }
961
Tony Barboure2c58df2014-11-25 13:18:32 -0700962 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600963 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600964 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600965 cmd_buf_info.pNext = NULL;
966 cmd_buf_info.flags = 0;
Cody Northropcc09b9e2015-08-11 11:35:58 -0600967 cmd_buf_info.renderPass = VK_NULL_HANDLE;
968 cmd_buf_info.subpass = 0;
969 cmd_buf_info.framebuffer = VK_NULL_HANDLE;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600970
Jon Ashburnc4164b12014-12-31 17:10:47 -0700971 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600972 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700973
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600974 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000975 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600976 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700977
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600978 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
979 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000980
981 // write barrier to the command buffer
Chia-I Wu53534662015-10-26 17:08:33 +0800982 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, 0, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700983
984 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700985 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600986 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700987
Tony Barboure2c58df2014-11-25 13:18:32 -0700988 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600989 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700990 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600991 VkSubmitInfo submit_info;
Chia-I Wuf9be13c2015-10-26 20:37:06 +0800992 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
993 submit_info.pNext = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800994 submit_info.waitSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600995 submit_info.pWaitSemaphores = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800996 submit_info.commandBufferCount = 1;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600997 submit_info.pCommandBuffers = bufferArray;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800998 submit_info.signalSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600999 submit_info.pSignalSemaphores = NULL;
Courtney Goeltzenleuchter646b9072015-10-20 18:04:07 -06001000
1001 err = vkQueueSubmit(m_device->m_queue, 1, &submit_info, m_fence.handle());
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001002 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001003}
1004
Tony Barbour6918cd52015-04-09 12:58:51 -06001005VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
1006 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001007{
1008
1009}
1010
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001011void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001012{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001013 m_numVertices = numIndexes;
1014 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001015 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -06001016 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001017 m_stride = 2;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001018 break;
Tony Barbourd1c35722015-04-16 15:59:00 -06001019 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001020 m_stride = 4;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001021 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001022 default:
1023 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -06001024 m_stride = 2;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001025 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001026 }
1027
Chia-I Wua07fee62014-12-28 15:26:08 +08001028 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -06001029 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northrop7fb43862015-06-22 14:56:14 -06001030 init_as_src_and_dst(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001031
Chia-I Wu681d7a02015-07-03 13:44:34 +08001032 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +08001033 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +08001034 memory().unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001035
Courtney Goeltzenleuchteraa132e72015-10-22 15:31:56 -06001036 // set up the descriptor for the constant buffer
Courtney Goeltzenleuchter4cb6d922015-10-23 13:38:14 -06001037 this->m_descriptorBufferInfo.buffer = handle();
1038 this->m_descriptorBufferInfo.offset = 0;
1039 this->m_descriptorBufferInfo.range = allocationSize;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001040}
1041
Tony Barbourd1c35722015-04-16 15:59:00 -06001042void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001043{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001044 vkCmdBindIndexBuffer(cmdBuffer, handle(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001045}
Tony Barboure2c58df2014-11-25 13:18:32 -07001046
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001047VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001048{
1049 return m_indexType;
1050}
1051
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001052VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001053{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001054 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001055 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu4d0c7922015-07-03 11:49:42 +08001056 stageInfo->shader = handle();
Tony Barboure2c58df2014-11-25 13:18:32 -07001057
Tony Barboure2c58df2014-11-25 13:18:32 -07001058 return stageInfo;
1059}
1060
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001061VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStageFlagBits stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001062{
Tony Barbourb5d2c942015-07-14 13:34:05 -06001063 VkResult U_ASSERT_ONLY err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001064 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001065 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001066 VkShaderModuleCreateInfo moduleCreateInfo;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001067 vk_testing::ShaderModule *module = new vk_testing::ShaderModule();
Tony Barboure2c58df2014-11-25 13:18:32 -07001068 size_t shader_len;
1069
1070 m_stage = stage;
1071 m_device = device;
1072
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001073 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1074 moduleCreateInfo.pNext = NULL;
1075
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001076 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001077 createInfo.pNext = NULL;
1078
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001079 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001080
1081 shader_len = strlen(shader_code);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001082 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
Chia-I Wu8094f192015-10-26 19:22:06 +08001083 moduleCreateInfo.pCode = (uint32_t *) malloc(moduleCreateInfo.codeSize);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001084 moduleCreateInfo.flags = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001085
Tony Barbourd1c35722015-04-16 15:59:00 -06001086 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001087 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1088 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1089 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1090 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barboure2c58df2014-11-25 13:18:32 -07001091
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001092 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001093
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001094 // Use Reference GLSL to SPV compiler
1095 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001096 moduleCreateInfo.pCode = spv.data();
1097 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1098 moduleCreateInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001099 }
Cody Northrop475663c2015-04-15 11:19:06 -06001100
Tony Barbour823e6ca2015-07-27 13:31:04 -06001101 err = module->init_try(*m_device, moduleCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001102 assert(VK_SUCCESS == err);
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001103
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001104 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1105 createInfo.pNext = NULL;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001106 createInfo.module = module->handle();
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001107 createInfo.pName = "main";
1108 createInfo.flags = 0;
Cody Northropfd4bcfd2015-08-24 15:11:10 -06001109 createInfo.stage = stage;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001110
1111 err = init_try(*m_device, createInfo);
Cody Northrop475663c2015-04-15 11:19:06 -06001112 assert(VK_SUCCESS == err);
Tony Barbour823e6ca2015-07-27 13:31:04 -06001113 framework->m_shader_modules.push_back(module);
Tony Barbourf325bf12014-12-03 15:59:38 -07001114}
Tony Barbour82c39522014-12-04 14:33:33 -07001115
Tony Barbour6918cd52015-04-09 12:58:51 -06001116VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001117{
Tony Barboure2c58df2014-11-25 13:18:32 -07001118 m_device = device;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001119
1120 m_vi_state.pNext = VK_NULL_HANDLE;
Chia-I Wud50a7d72015-10-26 20:48:51 +08001121 m_vi_state.vertexBindingDescriptionCount = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001122 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
Chia-I Wud50a7d72015-10-26 20:48:51 +08001123 m_vi_state.vertexAttributeDescriptionCount = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001124 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1125
Tony Barboure2c58df2014-11-25 13:18:32 -07001126 m_vertexBufferCount = 0;
1127
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001128 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001129 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001130 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001131 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001132
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001133 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001134 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchtera4c4ae02015-10-15 12:57:38 -06001135 m_rs_state.depthClampEnable = VK_TRUE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001136 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001137 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001138 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1139 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Cody Northrop12365112015-08-17 11:10:49 -06001140 m_rs_state.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001141 m_rs_state.lineWidth = 1.0f;
Chia-I Wud8c946a2015-10-26 19:08:09 +08001142 m_rs_state.depthBiasConstantFactor = 0.0f;
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001143 m_rs_state.depthBiasClamp = 0.0f;
Chia-I Wud8c946a2015-10-26 19:08:09 +08001144 m_rs_state.depthBiasSlopeFactor = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -07001145
Tony Barboure2c58df2014-11-25 13:18:32 -07001146 memset(&m_cb_state,0,sizeof(m_cb_state));
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001147 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001148 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001149 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001150 m_cb_state.blendConst[0] = 1.0f;
1151 m_cb_state.blendConst[1] = 1.0f;
1152 m_cb_state.blendConst[2] = 1.0f;
1153 m_cb_state.blendConst[3] = 1.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -07001154
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001155 m_ms_state.pNext = VK_NULL_HANDLE;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001156 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop02e61ed2015-08-04 14:34:54 -06001157 m_ms_state.pSampleMask = NULL;
Courtney Goeltzenleuchter6fabf912015-10-15 17:59:39 -06001158 m_ms_state.alphaToCoverageEnable = VK_FALSE;
Courtney Goeltzenleuchter2b9aaa62015-10-23 15:47:29 -06001159 m_ms_state.alphaToOneEnable = VK_FALSE;
Tony Barbourdfd533a2015-06-26 10:18:34 -06001160 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001161 m_ms_state.minSampleShading = 0;
1162 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001163
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001164 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001165 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001166 m_vp_state.viewportCount = 1;
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001167 m_vp_state.scissorCount = 1;
Tony Barbour414dd9e2015-10-02 13:33:51 -06001168 m_vp_state.pViewports = NULL;
1169 m_vp_state.pScissors = NULL;
Tony Barbourd81b8882015-05-15 09:37:57 -06001170
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001171 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001172 m_ds_state.pNext = VK_NULL_HANDLE,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001173 m_ds_state.depthTestEnable = VK_FALSE;
1174 m_ds_state.depthWriteEnable = VK_FALSE;
Cody Northrop271ba752015-08-26 10:01:32 -06001175 m_ds_state.depthBoundsTestEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001176 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001177 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1178 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1179 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001180 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001181 m_ds_state.stencilTestEnable = VK_FALSE;
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001182 m_ds_state.back.stencilCompareMask = 0xff;
1183 m_ds_state.back.stencilWriteMask = 0xff;
1184 m_ds_state.back.stencilReference = 0;
1185 m_ds_state.minDepthBounds = 0.f;
1186 m_ds_state.maxDepthBounds = 1.f;
1187
Tony Barbourf52346d2015-01-16 14:27:35 -07001188 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001189};
1190
Tony Barbour6918cd52015-04-09 12:58:51 -06001191void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001192{
1193 m_shaderObjs.push_back(shader);
1194}
1195
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001196void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001197{
1198 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
Chia-I Wud50a7d72015-10-26 20:48:51 +08001199 m_vi_state.vertexAttributeDescriptionCount = count;
Tony Barboure2c58df2014-11-25 13:18:32 -07001200}
1201
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001202void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001203{
1204 m_vi_state.pVertexBindingDescriptions = vi_binding;
Chia-I Wud50a7d72015-10-26 20:48:51 +08001205 m_vi_state.vertexBindingDescriptionCount = count;
Tony Barboure2c58df2014-11-25 13:18:32 -07001206}
1207
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001208void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineColorBlendAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001209{
Tony Barbourf52346d2015-01-16 14:27:35 -07001210 if (binding+1 > m_colorAttachments.size())
1211 {
1212 m_colorAttachments.resize(binding+1);
1213 }
1214 m_colorAttachments[binding] = *att;
1215}
1216
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001217void VkPipelineObj::SetDepthStencil(VkPipelineDepthStencilStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001218{
Tony Barbourf52346d2015-01-16 14:27:35 -07001219 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1220 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
Cody Northrop271ba752015-08-26 10:01:32 -06001221 m_ds_state.depthBoundsTestEnable = ds_state->depthBoundsTestEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001222 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001223 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1224 m_ds_state.back = ds_state->back;
1225 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001226}
1227
Tobin Ehlisd332f282015-10-02 11:00:56 -06001228void VkPipelineObj::SetViewport(vector<VkViewport> viewports)
1229{
1230 m_viewports = viewports;
1231 // If we explicitly set a null viewport, pass it through to create info
Cody Northropb2b4f042015-10-27 13:42:57 -06001232 // but preserve viewportCount because it musn't change
1233 if (m_viewports.size() == 0) {
1234 m_vp_state.pViewports = nullptr;
Tobin Ehlisd332f282015-10-02 11:00:56 -06001235 }
1236}
1237
1238void VkPipelineObj::SetScissor(vector<VkRect2D> scissors)
1239{
1240 m_scissors = scissors;
1241 // If we explicitly set a null scissors, pass it through to create info
Cody Northropb2b4f042015-10-27 13:42:57 -06001242 // but preserve viewportCount because it musn't change
1243 if (m_scissors.size() == 0) {
1244 m_vp_state.pScissors = nullptr;
Tobin Ehlisd332f282015-10-02 11:00:56 -06001245 }
1246}
1247
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001248void VkPipelineObj::MakeDynamic(VkDynamicState state)
1249{
1250 /* Only add a state once */
1251 for (auto it = m_dynamic_state_enables.begin(); it != m_dynamic_state_enables.end(); it++) {
1252 if ((*it) == state) return;
1253 }
1254 m_dynamic_state_enables.push_back(state);
1255}
1256
Tony Barbour0ec8cd52015-08-06 09:27:11 -06001257void VkPipelineObj::SetMSAA(VkPipelineMultisampleStateCreateInfo *ms_state)
1258{
1259 memcpy(&m_ms_state, ms_state, sizeof(VkPipelineMultisampleStateCreateInfo));
1260}
1261
Tony Barbour5781e8f2015-08-04 16:23:11 -06001262VkResult VkPipelineObj::CreateVKPipeline(VkPipelineLayout layout, VkRenderPass render_pass)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001263{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001264 VkGraphicsPipelineCreateInfo info = {};
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001265 VkPipelineDynamicStateCreateInfo dsci = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001266
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001267 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001268
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001269 info.stageCount = m_shaderObjs.size();
1270 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1271
Tony Barbour976e1cf2014-12-17 11:57:31 -07001272 for (int i=0; i<m_shaderObjs.size(); i++)
1273 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001274 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001275 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001276 }
1277
Chia-I Wud50a7d72015-10-26 20:48:51 +08001278 if (m_vi_state.vertexAttributeDescriptionCount && m_vi_state.vertexBindingDescriptionCount) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001279 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barboure815fd72015-07-27 09:36:24 -06001280 info.pVertexInputState = &m_vi_state;
1281 } else {
1282 info.pVertexInputState = NULL;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001283 }
1284
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001285 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001286 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001287 info.flags = 0;
Tony Barbour5781e8f2015-08-04 16:23:11 -06001288 info.layout = layout;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001289
Tony Barbourf52346d2015-01-16 14:27:35 -07001290 m_cb_state.attachmentCount = m_colorAttachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -06001291 m_cb_state.pAttachments = m_colorAttachments.data();
Tony Barbourf52346d2015-01-16 14:27:35 -07001292
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001293 if (m_viewports.size() > 0) {
1294 m_vp_state.viewportCount = m_viewports.size();
1295 m_vp_state.pViewports = m_viewports.data();
1296 } else {
1297 MakeDynamic(VK_DYNAMIC_STATE_VIEWPORT);
1298 }
1299
1300 if (m_scissors.size() > 0) {
1301 m_vp_state.scissorCount = m_scissors.size();
1302 m_vp_state.pScissors = m_scissors.data();
1303 } else {
1304 MakeDynamic(VK_DYNAMIC_STATE_SCISSOR);
1305 }
1306
1307 if (m_dynamic_state_enables.size() > 0) {
1308 dsci.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1309 dsci.dynamicStateCount = m_dynamic_state_enables.size();
1310 dsci.pDynamicStates = m_dynamic_state_enables.data();
1311 info.pDynamicState = &dsci;
1312 }
1313
Chia-I Wu08accc62015-07-07 11:50:03 +08001314 info.renderPass = render_pass;
1315 info.subpass = 0;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001316 info.pTessellationState = NULL;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001317 info.pInputAssemblyState = &m_ia_state;
1318 info.pViewportState = &m_vp_state;
1319 info.pRasterState = &m_rs_state;
1320 info.pMultisampleState = &m_ms_state;
1321 info.pDepthStencilState = &m_ds_state;
1322 info.pColorBlendState = &m_cb_state;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001323
Chris Forbes95292b12015-05-25 11:13:26 +12001324 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001325}
Chia-I Wu2648d092014-12-29 14:24:14 +08001326
Tony Barbourfe3351b2015-07-28 10:17:20 -06001327VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device, VkCmdPool pool)
Tony Barbour6d047bf2014-12-10 14:34:45 -07001328{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001329 m_device = device;
Courtney Goeltzenleuchter0c8385b2015-07-13 12:57:11 -06001330
Tony Barbourfe3351b2015-07-28 10:17:20 -06001331 init(*device, vk_testing::CmdBuffer::create_info(pool));
Tony Barbour6d047bf2014-12-10 14:34:45 -07001332}
Tony Barbour471338d2014-12-10 17:28:39 -07001333
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001334VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001335{
Chia-I Wube2b9172015-07-03 11:49:42 +08001336 return handle();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001337}
Tony Barbour471338d2014-12-10 17:28:39 -07001338
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001339VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001340{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001341 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001342 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001343}
1344
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001345VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001346{
1347 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001348 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001349}
1350
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001351VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001352{
Chia-I Wud28343c2014-12-28 15:12:48 +08001353 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001354 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001355}
1356
Chia-I Wu53534662015-10-26 17:08:33 +08001357void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, VkDependencyFlags dependencyFlags, uint32_t memBarrierCount, const void* const* ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001358{
Chia-I Wu53534662015-10-26 17:08:33 +08001359 vkCmdPipelineBarrier(handle(), src_stages, dest_stages, dependencyFlags, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001360}
1361
Chris Forbesf0796e12015-06-24 14:34:53 +12001362void VkCommandBufferObj::ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001363 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001364{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001365 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001366 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001367 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001368 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1369 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1370 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001371 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001372 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001373
1374 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001375 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001376 srRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001377 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter8367ce02015-10-16 09:46:00 -06001378 srRange.numLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001379 srRange.baseArrayLayer = 0;
Courtney Goeltzenleuchter8367ce02015-10-16 09:46:00 -06001380 srRange.numLayers = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001381
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001382 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001383 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001384 memory_barrier.outputMask = output_mask;
1385 memory_barrier.inputMask = input_mask;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001386 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001387 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001388 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001389
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001390 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1391 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001392
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001393 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001394 memory_barrier.image = m_renderTargets[i]->image();
1395 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Chia-I Wu53534662015-10-26 17:08:33 +08001396 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001397 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001398
Chia-I Wube2b9172015-07-03 11:49:42 +08001399 vkCmdClearColorImage(handle(),
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001400 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001401 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001402
Tony Barbour30cc9e82014-12-17 11:53:55 -07001403 }
1404
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001405 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001406 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001407 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001408 dsRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001409 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter8367ce02015-10-16 09:46:00 -06001410 dsRange.numLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001411 dsRange.baseArrayLayer = 0;
Courtney Goeltzenleuchter8367ce02015-10-16 09:46:00 -06001412 dsRange.numLayers = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001413
1414 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001415
Cody Northrop3cc85e92015-08-04 10:47:08 -06001416 memory_barrier.oldLayout = memory_barrier.newLayout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001417 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001418 memory_barrier.image = depthStencilObj->handle();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001419 memory_barrier.subresourceRange = dsRange;
1420
Chia-I Wu53534662015-10-26 17:08:33 +08001421 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001422
Courtney Goeltzenleuchter45df9e12015-09-15 18:03:22 -06001423 VkClearDepthStencilValue clear_value = {
1424 depth_clear_color,
1425 stencil_clear_color
1426 };
Chia-I Wube2b9172015-07-03 11:49:42 +08001427 vkCmdClearDepthStencilImage(handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +08001428 depthStencilObj->handle(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchter45df9e12015-09-15 18:03:22 -06001429 &clear_value,
Chris Forbesd9be82b2015-06-22 17:21:59 +12001430 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001431
1432 // prepare depth buffer for rendering
Chia-I Wu681d7a02015-07-03 13:44:34 +08001433 memory_barrier.image = depthStencilObj->handle();
Cody Northrop3cc85e92015-08-04 10:47:08 -06001434 memory_barrier.newLayout = memory_barrier.oldLayout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001435 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001436 memory_barrier.subresourceRange = dsRange;
Chia-I Wu53534662015-10-26 17:08:33 +08001437 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001438 }
1439}
1440
Tony Barbourc1eb1a52015-07-20 13:00:10 -06001441void VkCommandBufferObj::FillBuffer(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize fill_size, uint32_t data)
1442{
1443 vkCmdFillBuffer( handle(), buffer, offset, fill_size, data);
1444}
1445
Mike Stroyana3082432015-09-25 13:39:21 -06001446void VkCommandBufferObj::UpdateBuffer(VkBuffer buffer, VkDeviceSize destOffset, VkDeviceSize dataSize, const uint32_t *pData)
1447{
1448 vkCmdUpdateBuffer(handle(), buffer, destOffset, dataSize, pData);
1449}
1450
1451void VkCommandBufferObj::CopyImage(VkImage srcImage, VkImageLayout srcImageLayout, VkImage destImage, VkImageLayout destImageLayout, uint32_t regionCount, const VkImageCopy* pRegions)
1452{
1453 vkCmdCopyImage(handle(), srcImage, srcImageLayout, destImage, destImageLayout, regionCount, pRegions);
1454}
1455
1456void VkCommandBufferObj::ResolveImage(VkImage srcImage, VkImageLayout srcImageLayout, VkImage destImage, VkImageLayout destImageLayout, uint32_t regionCount, const VkImageResolve* pRegions)
1457{
1458 vkCmdResolveImage(handle(), srcImage, srcImageLayout, destImage, destImageLayout, regionCount, pRegions);
1459}
1460
Tony Barbour6918cd52015-04-09 12:58:51 -06001461void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001462{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001463 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001464 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001465 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001466 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1467 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1468 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001469 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001470 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001471 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001472 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1473 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1474 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1475 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1476 VK_MEMORY_INPUT_SHADER_READ_BIT |
1477 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1478 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001479 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001480
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001481 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001482 srRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001483 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter8367ce02015-10-16 09:46:00 -06001484 srRange.numLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001485 srRange.baseArrayLayer = 0;
Courtney Goeltzenleuchter8367ce02015-10-16 09:46:00 -06001486 srRange.numLayers = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001487
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001488 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001489 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001490 memory_barrier.outputMask = output_mask;
1491 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001492 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001493 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001494 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001495
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001496 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1497 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001498
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001499 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001500 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001501 memory_barrier.image = m_renderTargets[i]->image();
1502 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Chia-I Wu53534662015-10-26 17:08:33 +08001503 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001504 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001505 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001506}
1507
Chia-I Wu08accc62015-07-07 11:50:03 +08001508void VkCommandBufferObj::BeginRenderPass(const VkRenderPassBeginInfo &info)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001509{
Chia-I Wube2b9172015-07-03 11:49:42 +08001510 vkCmdBeginRenderPass( handle(), &info, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001511}
1512
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001513void VkCommandBufferObj::EndRenderPass()
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001514{
Chia-I Wube2b9172015-07-03 11:49:42 +08001515 vkCmdEndRenderPass(handle());
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001516}
1517
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001518void VkCommandBufferObj::SetViewport(
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -06001519 uint32_t viewportCount,
1520 const VkViewport* pViewports)
1521{
1522 vkCmdSetViewport( handle(), viewportCount, pViewports);
1523}
1524
1525void VkCommandBufferObj::SetScissor(
1526 uint32_t scissorCount,
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001527 const VkRect2D* pScissors)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001528{
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -06001529 vkCmdSetScissor( handle(), scissorCount, pScissors);
Tony Barbour67e99152015-07-10 14:10:27 -06001530}
1531
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001532void VkCommandBufferObj::SetLineWidth(float lineWidth)
Tony Barbour67e99152015-07-10 14:10:27 -06001533{
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001534 vkCmdSetLineWidth( handle(), lineWidth);
Cody Northrop12365112015-08-17 11:10:49 -06001535}
1536
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001537void VkCommandBufferObj::SetDepthBias(
Chia-I Wud8c946a2015-10-26 19:08:09 +08001538 float depthBiasConstantFactor,
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001539 float depthBiasClamp,
Chia-I Wud8c946a2015-10-26 19:08:09 +08001540 float depthBiasSlopeFactor)
Cody Northrop12365112015-08-17 11:10:49 -06001541{
Chia-I Wud8c946a2015-10-26 19:08:09 +08001542 vkCmdSetDepthBias( handle(), depthBiasConstantFactor, depthBiasClamp, depthBiasSlopeFactor);
Tony Barbour67e99152015-07-10 14:10:27 -06001543}
1544
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001545void VkCommandBufferObj::SetBlendConstants(
1546 const float blendConst[4])
Tony Barbour67e99152015-07-10 14:10:27 -06001547{
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001548 vkCmdSetBlendConstants( handle(), blendConst);
Tony Barbour67e99152015-07-10 14:10:27 -06001549}
1550
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001551void VkCommandBufferObj::SetDepthBounds(
1552 float minDepthBounds,
1553 float maxDepthBounds)
Tony Barbour67e99152015-07-10 14:10:27 -06001554{
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001555 vkCmdSetDepthBounds( handle(), minDepthBounds, maxDepthBounds);
Cody Northrop82485a82015-08-18 15:21:16 -06001556}
1557
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001558void VkCommandBufferObj::SetStencilReadMask(
1559 VkStencilFaceFlags faceMask,
1560 uint32_t stencilCompareMask)
Cody Northrop82485a82015-08-18 15:21:16 -06001561{
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001562 vkCmdSetStencilCompareMask( handle(), faceMask, stencilCompareMask);
1563}
1564
1565void VkCommandBufferObj::SetStencilWriteMask(
1566 VkStencilFaceFlags faceMask,
1567 uint32_t stencilWriteMask)
1568{
1569 vkCmdSetStencilWriteMask( handle(), faceMask, stencilWriteMask);
1570}
1571
1572void VkCommandBufferObj::SetStencilReference(
1573 VkStencilFaceFlags faceMask,
1574 uint32_t stencilReference)
1575{
1576 vkCmdSetStencilReference( handle(), faceMask, stencilReference);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001577}
1578
Tony Barbour6918cd52015-04-09 12:58:51 -06001579void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001580{
1581 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001582}
1583
Courtney Goeltzenleuchter08c26372015-09-23 12:31:50 -06001584void VkCommandBufferObj::DrawIndexed(uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001585{
Courtney Goeltzenleuchter08c26372015-09-23 12:31:50 -06001586 vkCmdDrawIndexed(handle(), indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001587}
1588
Courtney Goeltzenleuchter08c26372015-09-23 12:31:50 -06001589void VkCommandBufferObj::Draw(uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001590{
Courtney Goeltzenleuchter08c26372015-09-23 12:31:50 -06001591 vkCmdDraw(handle(), vertexCount, instanceCount, firstVertex, firstInstance);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001592}
1593
Tony Barbour6918cd52015-04-09 12:58:51 -06001594void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001595{
Tony Barbour67e99152015-07-10 14:10:27 -06001596 VkFence nullFence = { VK_NULL_HANDLE };
1597 QueueCommandBuffer(nullFence);
Tony Barbour09b7ac32015-04-08 10:11:29 -06001598}
1599
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001600void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001601{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001602 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001603
1604 // submit the command buffer to the universal queue
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -06001605 VkSubmitInfo submit_info;
Chia-I Wuf9be13c2015-10-26 20:37:06 +08001606 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1607 submit_info.pNext = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +08001608 submit_info.waitSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -06001609 submit_info.pWaitSemaphores = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +08001610 submit_info.commandBufferCount = 1;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -06001611 submit_info.pCommandBuffers = &handle();
Chia-I Wud50a7d72015-10-26 20:48:51 +08001612 submit_info.signalSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -06001613 submit_info.pSignalSemaphores = NULL;
Courtney Goeltzenleuchter646b9072015-10-20 18:04:07 -06001614
1615 err = vkQueueSubmit( m_device->m_queue, 1, &submit_info, fence );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001616 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001617
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001618 err = vkQueueWaitIdle( m_device->m_queue );
1619 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001620
1621 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001622 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001623}
1624
Tony Barbour6918cd52015-04-09 12:58:51 -06001625void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001626{
Chia-I Wube2b9172015-07-03 11:49:42 +08001627 vkCmdBindPipeline( handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.handle() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001628}
1629
Tony Barbour6918cd52015-04-09 12:58:51 -06001630void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001631{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001632 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001633
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001634 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wube2b9172015-07-03 11:49:42 +08001635 vkCmdBindDescriptorSets(handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northropd4c1a502015-04-16 13:41:56 -06001636 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001637}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001638
Cody Northropd2ad0342015-08-05 11:15:02 -06001639void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, VkDeviceSize offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001640{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001641 vkCmdBindIndexBuffer(handle(), indexBuffer->handle(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001642}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001643
Tony Barbourd1c35722015-04-16 15:59:00 -06001644void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001645{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001646 vkCmdBindVertexBuffers(handle(), binding, 1, &vertexBuffer->handle(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001647}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001648
Tony Barbour6918cd52015-04-09 12:58:51 -06001649VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001650{
1651 m_initialized = false;
1652}
Tony Barbour6918cd52015-04-09 12:58:51 -06001653bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001654{
1655 return m_initialized;
1656}
1657
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001658VkImageView* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001659{
Chia-I Wu08accc62015-07-07 11:50:03 +08001660 return &m_attachmentBindInfo;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001661}
1662
Chia-I Wu08accc62015-07-07 11:50:03 +08001663void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height, VkFormat format)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001664{
Cody Northrop78e71932015-09-03 16:09:57 -06001665 VkImageCreateInfo image_info = {};
1666 VkImageViewCreateInfo view_info = {};
Tony Barbour1c45ce02015-03-27 17:03:18 -06001667
1668 m_device = device;
1669 m_initialized = true;
Chia-I Wu08accc62015-07-07 11:50:03 +08001670 m_depth_stencil_fmt = format;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001671
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001672 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001673 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001674 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001675 image_info.format = m_depth_stencil_fmt;
1676 image_info.extent.width = width;
1677 image_info.extent.height = height;
1678 image_info.extent.depth = 1;
1679 image_info.mipLevels = 1;
Courtney Goeltzenleuchter5d2aed42015-10-21 17:57:31 -06001680 image_info.arrayLayers = 1;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001681 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001682 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchter660f0ca2015-09-10 14:14:11 -06001683 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001684 image_info.flags = 0;
Tony Barbour3d83f492015-07-28 10:23:02 -06001685 image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
Chia-I Wud50a7d72015-10-26 20:48:51 +08001686 image_info.queueFamilyIndexCount = 0;
Tony Barbour3d83f492015-07-28 10:23:02 -06001687 image_info.pQueueFamilyIndices = NULL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001688 init(*m_device, image_info);
1689
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001690 view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001691 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001692 view_info.image = VK_NULL_HANDLE;
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001693 view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001694 view_info.subresourceRange.baseMipLevel = 0;
Courtney Goeltzenleuchter8367ce02015-10-16 09:46:00 -06001695 view_info.subresourceRange.numLevels = 1;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001696 view_info.subresourceRange.baseArrayLayer = 0;
Courtney Goeltzenleuchter8367ce02015-10-16 09:46:00 -06001697 view_info.subresourceRange.numLayers = 1;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001698 view_info.flags = 0;
Tony Barbour3d83f492015-07-28 10:23:02 -06001699 view_info.format = m_depth_stencil_fmt;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001700 view_info.image = handle();
Cody Northrop78e71932015-09-03 16:09:57 -06001701 view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001702 m_imageView.init(*m_device, view_info);
Tony Barbour1c45ce02015-03-27 17:03:18 -06001703
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001704 m_attachmentBindInfo = m_imageView.handle();
Tony Barbour1c45ce02015-03-27 17:03:18 -06001705}