blob: a26b6d3f8dbef7c59619f402980eea0bc191ac93 [file] [log] [blame]
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchter5744b972014-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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Ian Elliott338dedb2015-08-21 15:09:33 -060029#include <vk_ext_khr_swapchain.h>
30#include <vk_ext_khr_device_swapchain.h>
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -060031
Jon Ashburn07b309a2015-04-15 11:31:12 -060032#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
Tony Barbourb3744222015-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 Ashburn07b309a2015-04-15 11:31:12 -060038
Tony Barbour01999182015-04-09 12:58:51 -060039VkRenderFramework::VkRenderFramework() :
Tony Barbour1490c912015-07-28 10:17:20 -060040 m_cmdBuffer(),
Tony Barbourbc868f92015-04-09 11:07:02 -060041 m_renderPass(VK_NULL_HANDLE),
42 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -060043 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter7e305322015-03-05 17:26:38 -070044 m_height( 256.0 ), // default window height
Tony Barbour8205d902015-04-16 15:59:00 -060045 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
46 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Tony Barbour6d841032015-07-21 17:00:26 -060047 m_clear_via_load_op( true ),
Courtney Goeltzenleuchter7e305322015-03-05 17:26:38 -070048 m_depth_clear_color( 1.0 ),
Courtney Goeltzenleuchter471cc122015-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 Goeltzenleuchter5744b972014-10-08 08:50:49 -060055{
Tony Barbour17c6ab12015-03-27 17:03:18 -060056
Chia-I Wuc278df82015-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 Goeltzenleuchter7e305322015-03-05 17:26:38 -070060 // clear the back buffer to dark grey
Cody Northrop2563a032015-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 Goeltzenleuchter5744b972014-10-08 08:50:49 -060065}
66
Tony Barbour01999182015-04-09 12:58:51 -060067VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -060068{
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060069
70}
71
Tony Barbour01999182015-04-09 12:58:51 -060072void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060073{
Courtney Goeltzenleuchterf5c61952015-07-06 09:10:47 -060074 std::vector<const char*> instance_layer_names;
75 std::vector<const char*> device_layer_names;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060076 std::vector<const char*> instance_extension_names;
77 std::vector<const char*> device_extension_names;
Courtney Goeltzenleuchterf5c61952015-07-06 09:10:47 -060078 InitFramework(
79 instance_layer_names, device_layer_names,
80 instance_extension_names, device_extension_names);
Tony Barbour950ebc02015-04-23 12:55:36 -060081}
82
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060083void VkRenderFramework::InitFramework(
Courtney Goeltzenleuchterf5c61952015-07-06 09:10:47 -060084 std::vector<const char *> instance_layer_names,
85 std::vector<const char *> device_layer_names,
Courtney Goeltzenleuchter1c7c65d2015-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 Barbour950ebc02015-04-23 12:55:36 -060090{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -060091 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060092 std::vector<VkExtensionProperties> instance_extensions;
93 std::vector<VkExtensionProperties> device_extensions;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060094 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060095
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060096 /* TODO: Verify requested extensions are available */
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060097
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060098 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburn29669a42015-04-04 14:52:07 -060099 instInfo.pNext = NULL;
100 instInfo.pAppInfo = &app_info;
101 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchterf5c61952015-07-06 09:10:47 -0600102 instInfo.layerCount = instance_layer_names.size();
Tony Barbourb0ae8c12015-07-27 09:37:48 -0600103 instInfo.ppEnabledLayerNames = instance_layer_names.data();
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600104 instInfo.extensionCount = instance_extension_names.size();
Tony Barbourb0ae8c12015-07-27 09:37:48 -0600105 instInfo.ppEnabledExtensionNames = instance_extension_names.data();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600106 err = vkCreateInstance(&instInfo, &this->inst);
107 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600108
Jon Ashburn07b309a2015-04-15 11:31:12 -0600109 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
110 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
111 ASSERT_VK_SUCCESS(err);
112 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600113 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter0abdb662015-10-07 13:28:58 -0600114 ASSERT_GE(this->gpu_count, (uint32_t) 1) << "No GPU available";
Tony Barbour0c1bdc62015-04-29 17:34:29 -0600115 if (dbgFunction) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600116 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
117 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
118 if (m_dbgCreateMsgCallback) {
119 err = m_dbgCreateMsgCallback(this->inst,
120 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
121 dbgFunction,
122 userData,
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -0600123 &m_globalMsgCallback);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600124 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter18e8a492015-06-08 15:16:04 -0600125
126 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
127 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600128 }
Tony Barbour0c1bdc62015-04-29 17:34:29 -0600129 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600130
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600131 /* TODO: Verify requested physical device extensions are available */
Courtney Goeltzenleuchter61414de2015-07-07 11:47:33 -0600132 m_device = new VkDeviceObj(0, objs[0], device_layer_names, device_extension_names);
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -0600133
134 /* Now register callback on device */
135 if (0) {
136 if (m_dbgCreateMsgCallback) {
137 err = m_dbgCreateMsgCallback(this->inst,
138 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
139 dbgFunction,
140 userData,
141 &m_devMsgCallback);
142 ASSERT_VK_SUCCESS(err);
143 }
144 }
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600145 m_device->get_device_queue();
Tony Barbour17c6ab12015-03-27 17:03:18 -0600146
Tony Barbour01999182015-04-09 12:58:51 -0600147 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600148}
149
Tony Barbour01999182015-04-09 12:58:51 -0600150void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600151{
Tony Barbour1490c912015-07-28 10:17:20 -0600152 if (m_cmdBuffer)
153 delete m_cmdBuffer;
Cody Northropf02f9f82015-07-09 18:08:05 -0600154 if (m_cmdPool) vkDestroyCommandPool(device(), m_cmdPool);
Tony Barboure84a8d62015-07-10 14:10:27 -0600155 if (m_framebuffer) vkDestroyFramebuffer(device(), m_framebuffer);
156 if (m_renderPass) vkDestroyRenderPass(device(), m_renderPass);
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600157
Courtney Goeltzenleuchter18e8a492015-06-08 15:16:04 -0600158 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
159 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
160
Tobin Ehlis12ee35f2015-03-26 08:23:25 -0600161 while (!m_renderTargets.empty()) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600162 vkDestroyImageView(device(), m_renderTargets.back()->targetView(m_render_target_fmt));
Tony Barboure84a8d62015-07-10 14:10:27 -0600163 vkDestroyImage(device(), m_renderTargets.back()->image());
Mike Stroyan230e6252015-04-17 12:36:38 -0600164 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis12ee35f2015-03-26 08:23:25 -0600165 m_renderTargets.pop_back();
166 }
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600167
Tony Barbour17c6ab12015-03-27 17:03:18 -0600168 delete m_depthStencil;
Tony Barboura024aa02015-07-27 13:31:04 -0600169 while (!m_shader_modules.empty())
170 {
171 delete m_shader_modules.back();
172 m_shader_modules.pop_back();
173 }
Tony Barbour17c6ab12015-03-27 17:03:18 -0600174
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600175 // reset the driver
Chia-I Wu1c9869e2014-12-28 14:27:28 +0800176 delete m_device;
Chris Forbes3f23ffb2015-07-07 11:02:22 +1200177 if (this->inst) vkDestroyInstance(this->inst);
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600178}
179
Tony Barbour01999182015-04-09 12:58:51 -0600180void VkRenderFramework::InitState()
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600181{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600182 VkResult err;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600183
Tony Barbourb3744222015-07-23 10:36:18 -0600184 // Get the list of VkFormat's that are supported:
Ian Elliott338dedb2015-08-21 15:09:33 -0600185 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
Ian Elliottdad13202015-08-07 11:51:12 -0600186 uint32_t formatCount;
Ian Elliott338dedb2015-08-21 15:09:33 -0600187 VkSurfaceDescriptionKHR surface_description;
188 surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Tony Barbourb3744222015-07-23 10:36:18 -0600189 surface_description.pNext = NULL;
Ian Elliott338dedb2015-08-21 15:09:33 -0600190 GET_DEVICE_PROC_ADDR(device(), GetSurfaceFormatsKHR);
191 err = fpGetSurfaceFormatsKHR(device(),
192 (VkSurfaceDescriptionKHR *) &surface_description,
Ian Elliottdad13202015-08-07 11:51:12 -0600193 &formatCount, NULL);
Tony Barbourb3744222015-07-23 10:36:18 -0600194 ASSERT_VK_SUCCESS(err);
Ian Elliott338dedb2015-08-21 15:09:33 -0600195 VkSurfaceFormatKHR *surfFormats =
196 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
197 err = fpGetSurfaceFormatsKHR(device(),
198 (VkSurfaceDescriptionKHR *) &surface_description,
Ian Elliottdad13202015-08-07 11:51:12 -0600199 &formatCount, surfFormats);
Tony Barbourb3744222015-07-23 10:36:18 -0600200 ASSERT_VK_SUCCESS(err);
201 m_render_target_fmt = surfFormats[0].format;
202 free(surfFormats);
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600203
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600204 m_lineWidth = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700205
Chia-I Wufa950c52015-10-26 19:08:09 +0800206 m_depthBiasConstantFactor = 0.0f;
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600207 m_depthBiasClamp = 0.0f;
Chia-I Wufa950c52015-10-26 19:08:09 +0800208 m_depthBiasSlopeFactor = 0.0f;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600209
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600210 m_blendConst[0] = 1.0f;
211 m_blendConst[1] = 1.0f;
212 m_blendConst[2] = 1.0f;
213 m_blendConst[3] = 1.0f;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600214
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600215 m_minDepthBounds = 0.f;
216 m_maxDepthBounds = 1.f;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600217
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600218 m_stencilCompareMask = 0xff;
219 m_stencilWriteMask = 0xff;
220 m_stencilReference = 0;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600221
Cody Northropf02f9f82015-07-09 18:08:05 -0600222 VkCmdPoolCreateInfo cmd_pool_info;
223 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
224 cmd_pool_info.pNext = NULL,
225 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
226 cmd_pool_info.flags = 0,
227 err = vkCreateCommandPool(device(), &cmd_pool_info, &m_cmdPool);
228 assert(!err);
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600229
Tony Barbour1490c912015-07-28 10:17:20 -0600230 m_cmdBuffer = new VkCommandBufferObj(m_device, m_cmdPool);
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600231}
232
Tony Barbour01999182015-04-09 12:58:51 -0600233void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600234{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600235 VkViewport viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +1200236 VkRect2D scissor;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700237 viewport.originX = 0;
238 viewport.originY = 0;
239 viewport.width = 1.f * width;
240 viewport.height = 1.f * height;
241 viewport.minDepth = 0.f;
242 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600243 m_viewports.push_back(viewport);
244
Tony Barbour8bef8ee2015-05-22 09:44:58 -0600245 scissor.extent.width = (int32_t) width;
246 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -0700247 scissor.offset.x = 0;
248 scissor.offset.y = 0;
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600249 m_scissors.push_back(scissor);
Tony Barbourfa6cac72015-01-16 14:27:35 -0700250
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600251 m_width = width;
252 m_height = height;
253}
254
Tony Barbour01999182015-04-09 12:58:51 -0600255void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600256{
257 InitViewport(m_width, m_height);
258}
Tony Barbour01999182015-04-09 12:58:51 -0600259void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600260{
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -0700261 InitRenderTarget(1);
262}
263
Tony Barbour01999182015-04-09 12:58:51 -0600264void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -0700265{
Tony Barbour17c6ab12015-03-27 17:03:18 -0600266 InitRenderTarget(targets, NULL);
267}
268
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600269void VkRenderFramework::InitRenderTarget(VkImageView *dsBinding)
Tony Barbour17c6ab12015-03-27 17:03:18 -0600270{
271 InitRenderTarget(1, dsBinding);
272}
273
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600274void VkRenderFramework::InitRenderTarget(uint32_t targets, VkImageView *dsBinding)
Tony Barbour17c6ab12015-03-27 17:03:18 -0600275{
Chia-I Wuc278df82015-07-07 11:50:03 +0800276 std::vector<VkAttachmentDescription> attachments;
277 std::vector<VkAttachmentReference> color_references;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600278 std::vector<VkImageView> bindings;
Cody Northrop1684adb2015-08-05 11:15:02 -0600279 attachments.reserve(targets + 1); // +1 for dsBinding
Chia-I Wuc278df82015-07-07 11:50:03 +0800280 color_references.reserve(targets);
Cody Northrop1684adb2015-08-05 11:15:02 -0600281 bindings.reserve(targets + 1); // +1 for dsBinding
Tony Barbour17c6ab12015-03-27 17:03:18 -0600282
Chia-I Wuc278df82015-07-07 11:50:03 +0800283 VkAttachmentDescription att = {};
284 att.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION;
285 att.pNext = NULL;
286 att.format = m_render_target_fmt;
287 att.samples = 1;
288 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
289 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
290 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
291 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
292 att.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
293 att.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chia-I Wue7748802014-12-05 10:45:15 +0800294
Chia-I Wuc278df82015-07-07 11:50:03 +0800295 VkAttachmentReference ref = {};
296 ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
297
298 m_renderPassClearValues.clear();
299 VkClearValue clear = {};
300 clear.color = m_clear_color;
301
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600302 VkImageView bind = {};
Chia-I Wuc278df82015-07-07 11:50:03 +0800303
304 for (uint32_t i = 0; i < targets; i++) {
305 attachments.push_back(att);
306
307 ref.attachment = i;
308 color_references.push_back(ref);
309
310 m_renderPassClearValues.push_back(clear);
311
Tony Barbour01999182015-04-09 12:58:51 -0600312 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600313
Tony Barbour25e3b832015-05-20 16:53:31 -0600314 VkFormatProperties props;
Tony Barbour25e3b832015-05-20 16:53:31 -0600315
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600316 vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), m_render_target_fmt, &props);
Tony Barbour25e3b832015-05-20 16:53:31 -0600317
318 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour8bef8ee2015-05-22 09:44:58 -0600319 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboura7ec3ba2015-07-21 17:01:42 -0600320 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
321 VK_IMAGE_TILING_LINEAR);
Tony Barbour25e3b832015-05-20 16:53:31 -0600322 }
323 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour8bef8ee2015-05-22 09:44:58 -0600324 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboura7ec3ba2015-07-21 17:01:42 -0600325 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
326 VK_IMAGE_TILING_OPTIMAL);
Tony Barbour25e3b832015-05-20 16:53:31 -0600327 }
328 else {
329 FAIL() << "Neither Linear nor Optimal allowed for render target";
330 }
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600331
Tony Barbour25e3b832015-05-20 16:53:31 -0600332 m_renderTargets.push_back(img);
Tony Barbourb3744222015-07-23 10:36:18 -0600333 bind = img->targetView(m_render_target_fmt);
Chia-I Wuc278df82015-07-07 11:50:03 +0800334 bindings.push_back(bind);
Chia-I Wue7748802014-12-05 10:45:15 +0800335 }
Tony Barbour25e3b832015-05-20 16:53:31 -0600336
Chia-I Wuc278df82015-07-07 11:50:03 +0800337 VkSubpassDescription subpass = {};
338 subpass.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION;
339 subpass.pNext = NULL;
340 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
341 subpass.flags = 0;
342 subpass.inputCount = 0;
Cody Northrop6de6b0b2015-08-04 11:16:41 -0600343 subpass.pInputAttachments = NULL;
Chia-I Wuc278df82015-07-07 11:50:03 +0800344 subpass.colorCount = targets;
Cody Northrop6de6b0b2015-08-04 11:16:41 -0600345 subpass.pColorAttachments = color_references.data();
346 subpass.pResolveAttachments = NULL;
Chia-I Wuc278df82015-07-07 11:50:03 +0800347
348 if (dsBinding) {
349 att.format = m_depth_stencil_fmt;
Tony Barbour6d841032015-07-21 17:00:26 -0600350 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;;
Chia-I Wuc278df82015-07-07 11:50:03 +0800351 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
352 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
353 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Cody Northropafc718a2015-09-03 16:09:57 -0600354 att.initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
355 att.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Chia-I Wuc278df82015-07-07 11:50:03 +0800356 attachments.push_back(att);
357
Cody Northrop2563a032015-08-25 15:26:38 -0600358 clear.depthStencil.depth = m_depth_clear_color;
359 clear.depthStencil.stencil = m_stencil_clear_color;
Chia-I Wuc278df82015-07-07 11:50:03 +0800360 m_renderPassClearValues.push_back(clear);
361
362 bindings.push_back(*dsBinding);
363
364 subpass.depthStencilAttachment.attachment = targets;
Cody Northropafc718a2015-09-03 16:09:57 -0600365 subpass.depthStencilAttachment.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Chia-I Wuc278df82015-07-07 11:50:03 +0800366 } else {
367 subpass.depthStencilAttachment.attachment = VK_ATTACHMENT_UNUSED;
368 }
369
370 subpass.preserveCount = 0;
Cody Northrop6de6b0b2015-08-04 11:16:41 -0600371 subpass.pPreserveAttachments = NULL;
Chia-I Wuc278df82015-07-07 11:50:03 +0800372
373 VkRenderPassCreateInfo rp_info = {};
374 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
375 rp_info.attachmentCount = attachments.size();
Tony Barbourb0ae8c12015-07-27 09:37:48 -0600376 rp_info.pAttachments = attachments.data();
Chia-I Wuc278df82015-07-07 11:50:03 +0800377 rp_info.subpassCount = 1;
378 rp_info.pSubpasses = &subpass;
379
380 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
381
382 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600383 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600384 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbour901f3bc2015-04-01 17:10:07 -0600385 fb_info.pNext = NULL;
Chia-I Wuc278df82015-07-07 11:50:03 +0800386 fb_info.renderPass = m_renderPass;
387 fb_info.attachmentCount = bindings.size();
Tony Barbourb0ae8c12015-07-27 09:37:48 -0600388 fb_info.pAttachments = bindings.data();
Tony Barbour901f3bc2015-04-01 17:10:07 -0600389 fb_info.width = (uint32_t)m_width;
390 fb_info.height = (uint32_t)m_height;
391 fb_info.layers = 1;
392
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600393 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600394
Chia-I Wuc278df82015-07-07 11:50:03 +0800395 m_renderPassBeginInfo.renderPass = m_renderPass;
396 m_renderPassBeginInfo.framebuffer = m_framebuffer;
Cody Northrop1684adb2015-08-05 11:15:02 -0600397 m_renderPassBeginInfo.renderArea.extent.width = (int32_t) m_width;
398 m_renderPassBeginInfo.renderArea.extent.height = (int32_t) m_height;
Cody Northropc332eef2015-08-04 11:51:03 -0600399 m_renderPassBeginInfo.clearValueCount = m_renderPassClearValues.size();
400 m_renderPassBeginInfo.pClearValues = m_renderPassClearValues.data();
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600401}
402
Mark Lobodzinski15427102015-02-18 16:38:17 -0600403
404
Tony Barbour8205d902015-04-16 15:59:00 -0600405VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600406 vk_testing::Device(obj), id(id)
Chia-I Wu234a0ae2014-12-29 15:23:20 +0800407{
408 init();
409
Chia-I Wuf5fb1092015-07-03 10:32:05 +0800410 props = phy().properties();
Tony Barbourb0ae8c12015-07-27 09:37:48 -0600411 queue_props = phy().queue_properties().data();
Chia-I Wu234a0ae2014-12-29 15:23:20 +0800412}
413
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600414VkDeviceObj::VkDeviceObj(uint32_t id,
Courtney Goeltzenleuchter61414de2015-07-07 11:47:33 -0600415 VkPhysicalDevice obj, std::vector<const char *> &layer_names,
416 std::vector<const char *> &extension_names) :
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600417 vk_testing::Device(obj), id(id)
418{
Courtney Goeltzenleuchter61414de2015-07-07 11:47:33 -0600419 init(layer_names, extension_names);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600420
Chia-I Wuf5fb1092015-07-03 10:32:05 +0800421 props = phy().properties();
Tony Barbourb0ae8c12015-07-27 09:37:48 -0600422 queue_props = phy().queue_properties().data();
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600423}
424
Tony Barbour01999182015-04-09 12:58:51 -0600425void VkDeviceObj::get_device_queue()
Chia-I Wu234a0ae2014-12-29 15:23:20 +0800426{
427 ASSERT_NE(true, graphics_queues().empty());
Chia-I Wuf2862c72015-07-03 10:53:18 +0800428 m_queue = graphics_queues()[0]->handle();
Chia-I Wu234a0ae2014-12-29 15:23:20 +0800429}
430
Tobin Ehlis5b1452f2015-05-27 14:33:27 -0600431VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
432 m_device(device), m_nextSlot(0)
Tony Barbourf43b6982014-11-25 13:18:32 -0700433{
Tony Barbourf43b6982014-11-25 13:18:32 -0700434
435}
436
Tony Barbour01999182015-04-09 12:58:51 -0600437VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barbourf43b6982014-11-25 13:18:32 -0700438{
Chia-I Wuf8385062015-01-04 16:27:24 +0800439 delete m_set;
Tony Barbourf43b6982014-11-25 13:18:32 -0700440}
Tony Barbour408ca622014-12-04 14:33:33 -0700441
Tony Barbour01999182015-04-09 12:58:51 -0600442int VkDescriptorSetObj::AppendDummy()
Tony Barbourf43b6982014-11-25 13:18:32 -0700443{
Chia-I Wuf8385062015-01-04 16:27:24 +0800444 /* request a descriptor but do not update it */
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600445 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600446 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wuf8385062015-01-04 16:27:24 +0800447 tc.count = 1;
448 m_type_counts.push_back(tc);
Tony Barbourf43b6982014-11-25 13:18:32 -0700449
Chia-I Wuf8385062015-01-04 16:27:24 +0800450 return m_nextSlot++;
Tony Barbourf43b6982014-11-25 13:18:32 -0700451}
Tony Barbour408ca622014-12-04 14:33:33 -0700452
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600453int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barbourf43b6982014-11-25 13:18:32 -0700454{
Courtney Goeltzenleuchter2a3e3a32015-09-11 15:29:21 -0600455 assert(type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
456 type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
457 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
458 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600459 VkDescriptorTypeCount tc = {};
Chia-I Wuf8385062015-01-04 16:27:24 +0800460 tc.type = type;
461 tc.count = 1;
462 m_type_counts.push_back(tc);
Tony Barbourf43b6982014-11-25 13:18:32 -0700463
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800464 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600465 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorBufferInfo));
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600466
Chia-I Wuf8385062015-01-04 16:27:24 +0800467 return m_nextSlot++;
Tony Barbourf43b6982014-11-25 13:18:32 -0700468}
Tony Barbour408ca622014-12-04 14:33:33 -0700469
Tony Barbour01999182015-04-09 12:58:51 -0600470int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barbourf43b6982014-11-25 13:18:32 -0700471{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600472 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600473 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wuf8385062015-01-04 16:27:24 +0800474 tc.count = 1;
475 m_type_counts.push_back(tc);
Tony Barbourf43b6982014-11-25 13:18:32 -0700476
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600477 VkDescriptorImageInfo tmp = texture->m_imageInfo;
478 tmp.sampler = sampler->handle();
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800479 m_imageSamplerDescriptors.push_back(tmp);
Tony Barbourf43b6982014-11-25 13:18:32 -0700480
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800481 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600482 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &tmp));
Tony Barbourf43b6982014-11-25 13:18:32 -0700483
Chia-I Wuf8385062015-01-04 16:27:24 +0800484 return m_nextSlot++;
Tony Barbourf43b6982014-11-25 13:18:32 -0700485}
Chia-I Wuf8385062015-01-04 16:27:24 +0800486
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500487VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbour3edb6ac2014-12-17 10:54:03 -0700488{
Chia-I Wudeb99132015-07-03 11:49:42 +0800489 return m_pipeline_layout.handle();
Tony Barbour3edb6ac2014-12-17 10:54:03 -0700490}
491
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600492VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbour3edb6ac2014-12-17 10:54:03 -0700493{
Chia-I Wu55a871f2015-07-03 11:49:42 +0800494 return m_set->handle();
Tony Barbour3edb6ac2014-12-17 10:54:03 -0700495}
Tony Barbourf43b6982014-11-25 13:18:32 -0700496
Tony Barbour01999182015-04-09 12:58:51 -0600497void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barboura5712982014-12-18 17:06:21 -0700498{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600499 // create VkDescriptorPool
500 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600501 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu8d24b3b2015-03-26 13:14:16 +0800502 pool.count = m_type_counts.size();
Courtney Goeltzenleuchterd9e966a2015-09-16 16:12:45 -0600503 pool.maxSets = 1;
Tony Barbourb0ae8c12015-07-27 09:37:48 -0600504 pool.pTypeCount = m_type_counts.data();
Courtney Goeltzenleuchterd9e966a2015-09-16 16:12:45 -0600505 init(*m_device, pool);
Tony Barboura5712982014-12-18 17:06:21 -0700506
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600507 // create VkDescriptorSetLayout
508 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wufc9d9132015-03-26 15:04:41 +0800509 bindings.resize(m_type_counts.size());
Chia-I Wuf8385062015-01-04 16:27:24 +0800510 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wufc9d9132015-03-26 15:04:41 +0800511 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wud3114a22015-05-25 16:22:52 +0800512 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbour8205d902015-04-16 15:59:00 -0600513 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu310eece2015-03-27 12:56:09 +0800514 bindings[i].pImmutableSamplers = NULL;
Tony Barbourf43b6982014-11-25 13:18:32 -0700515 }
Chia-I Wu6ae4cfb2014-12-28 22:32:36 +0800516
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600517 // create VkDescriptorSetLayout
518 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600519 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wufc9d9132015-03-26 15:04:41 +0800520 layout.count = bindings.size();
Tony Barbourb0ae8c12015-07-27 09:37:48 -0600521 layout.pBinding = bindings.data();
Chia-I Wufc9d9132015-03-26 15:04:41 +0800522
Chia-I Wu7732cb22015-03-26 15:27:55 +0800523 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600524 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu7732cb22015-03-26 15:27:55 +0800525 layouts.push_back(&m_layout);
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500526
527 // create VkPipelineLayout
528 VkPipelineLayoutCreateInfo pipeline_layout = {};
529 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
530 pipeline_layout.descriptorSetCount = layouts.size();
531 pipeline_layout.pSetLayouts = NULL;
532
533 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barbourf43b6982014-11-25 13:18:32 -0700534
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600535 // create VkDescriptorSet
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600536 m_set = alloc_sets(*m_device, m_layout);
Chia-I Wuf8385062015-01-04 16:27:24 +0800537
Chia-I Wu7732cb22015-03-26 15:27:55 +0800538 // build the update array
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800539 size_t imageSamplerCount = 0;
540 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
541 it != m_writes.end(); it++) {
Chia-I Wu55a871f2015-07-03 11:49:42 +0800542 it->destSet = m_set->handle();
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800543 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600544 it->pImageInfo = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wuf8385062015-01-04 16:27:24 +0800545 }
Chia-I Wuf8385062015-01-04 16:27:24 +0800546
547 // do the updates
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800548 m_device->update_descriptor_sets(m_writes);
Tony Barbour342ae3b2014-12-03 13:59:18 -0700549}
Tony Barbourf43b6982014-11-25 13:18:32 -0700550
Tony Barbour01999182015-04-09 12:58:51 -0600551VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800552{
553 m_device = dev;
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600554 m_descriptorImageInfo.imageView = VK_NULL_HANDLE;
555 m_descriptorImageInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800556}
557
Tony Barbour01999182015-04-09 12:58:51 -0600558void VkImageObj::ImageMemoryBarrier(
559 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600560 VkImageAspectFlags aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600561 VkFlags output_mask /*=
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600562 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600563 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
564 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
565 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
566 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600567 VkFlags input_mask /*=
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600568 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600569 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
570 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
571 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
572 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
573 VK_MEMORY_INPUT_SHADER_READ_BIT |
574 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
575 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
576 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600577 VkImageLayout image_layout)
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600578{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600579 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
580 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600581 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
582 subresourceRange);
583
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600584 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600585
Tony Barbourc2e987e2015-06-29 16:20:35 -0600586 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
587 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600588
589 // write barrier to the command buffer
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -0600590 vkCmdPipelineBarrier(cmd_buf->handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600591}
592
Tony Barbour01999182015-04-09 12:58:51 -0600593void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600594 VkImageAspectFlagBits aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600595 VkImageLayout image_layout)
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600596{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600597 VkFlags output_mask, input_mask;
598 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600599 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600600 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
601 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
602 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600603 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600604 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600605 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600606 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
607 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
608 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
609 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
610 VK_MEMORY_INPUT_SHADER_READ_BIT |
611 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
612 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600613 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600614
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600615 if (image_layout == m_descriptorImageInfo.imageLayout) {
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600616 return;
617 }
618
619 switch (image_layout) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600620 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600621 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
622 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600623 break;
624
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600625 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600626 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
627 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600628 break;
629
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600630 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600631 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
632 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600633 break;
634
635 default:
636 output_mask = all_cache_outputs;
637 input_mask = all_cache_inputs;
638 break;
639 }
640
641 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600642 m_descriptorImageInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600643}
644
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600645void VkImageObj::SetLayout(VkImageAspectFlagBits aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600646 VkImageLayout image_layout)
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600647{
Tony Barbour22a30862015-04-22 09:02:32 -0600648 VkResult U_ASSERT_ONLY err;
Tony Barbour25e3b832015-05-20 16:53:31 -0600649
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600650 if (image_layout == m_descriptorImageInfo.imageLayout) {
Tony Barbour25e3b832015-05-20 16:53:31 -0600651 return;
652 }
653
Tony Barbour1490c912015-07-28 10:17:20 -0600654 VkCmdPoolCreateInfo cmd_pool_info = {};
655 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
656 cmd_pool_info.pNext = NULL;
657 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
658 cmd_pool_info.flags = 0;
659 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
660 VkCommandBufferObj cmd_buf(m_device, pool.handle());
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600661
662 /* Build command buffer to set image layout in the driver */
663 err = cmd_buf.BeginCommandBuffer();
664 assert(!err);
665
666 SetLayout(&cmd_buf, aspect, image_layout);
667
668 err = cmd_buf.EndCommandBuffer();
669 assert(!err);
670
671 cmd_buf.QueueCommandBuffer();
672}
673
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600674bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600675{
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600676 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbour8205d902015-04-16 15:59:00 -0600677 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600678 return false;
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600679
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600680 return true;
681}
682
Tony Barbour01999182015-04-09 12:58:51 -0600683void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600684 VkFormat fmt, VkFlags usage,
Tony Barbour94310562015-04-22 15:10:33 -0600685 VkImageTiling requested_tiling,
686 VkMemoryPropertyFlags reqs)
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800687{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600688 uint32_t mipCount;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600689 VkFormatProperties image_fmt;
690 VkImageTiling tiling;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600691
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800692 mipCount = 0;
693
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600694 uint32_t _w = w;
695 uint32_t _h = h;
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800696 while( ( _w > 0 ) || ( _h > 0 ) )
697 {
698 _w >>= 1;
699 _h >>= 1;
700 mipCount++;
701 }
702
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600703 vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), fmt, &image_fmt);
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600704
Tony Barbour8205d902015-04-16 15:59:00 -0600705 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600706 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbour8205d902015-04-16 15:59:00 -0600707 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600708 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbour8205d902015-04-16 15:59:00 -0600709 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600710 } else {
711 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
712 }
713 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbour8205d902015-04-16 15:59:00 -0600714 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600715 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbour8205d902015-04-16 15:59:00 -0600716 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600717 } else {
718 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
719 }
720
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600721 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbour8205d902015-04-16 15:59:00 -0600722 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800723 imageCreateInfo.format = fmt;
724 imageCreateInfo.extent.width = w;
725 imageCreateInfo.extent.height = h;
726 imageCreateInfo.mipLevels = mipCount;
727 imageCreateInfo.tiling = tiling;
728
729 imageCreateInfo.usage = usage;
730
Tony Barbour94310562015-04-22 15:10:33 -0600731 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800732
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600733 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600734 SetLayout(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600735 } else {
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600736 SetLayout(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600737 }
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800738}
739
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600740VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour16b71bc2015-04-01 17:47:06 -0600741{
Tony Barbour22a30862015-04-22 09:02:32 -0600742 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600743 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600744
Tony Barbour1490c912015-07-28 10:17:20 -0600745 VkCmdPoolCreateInfo cmd_pool_info = {};
746 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
747 cmd_pool_info.pNext = NULL;
748 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
749 cmd_pool_info.flags = 0;
750 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
751 VkCommandBufferObj cmd_buf(m_device, pool.handle());
752
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600753 /* Build command buffer to copy staging texture to usable texture */
754 err = cmd_buf.BeginCommandBuffer();
Tony Barbour16b71bc2015-04-01 17:47:06 -0600755 assert(!err);
756
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600757 /* TODO: Can we determine image aspect from image object? */
758 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600759 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour16b71bc2015-04-01 17:47:06 -0600760
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600761 dest_image_layout = this->layout();
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600762 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour17c6ab12015-03-27 17:03:18 -0600763
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600764 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600765 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR_BIT;
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -0600766 copy_region.srcSubresource.baseArrayLayer = 0;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600767 copy_region.srcSubresource.mipLevel = 0;
768 copy_region.srcOffset.x = 0;
769 copy_region.srcOffset.y = 0;
770 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600771 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR_BIT;
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -0600772 copy_region.destSubresource.baseArrayLayer = 0;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600773 copy_region.destSubresource.mipLevel = 0;
774 copy_region.destOffset.x = 0;
775 copy_region.destOffset.y = 0;
776 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600777 copy_region.extent = src_image.extent();
Tony Barbour16b71bc2015-04-01 17:47:06 -0600778
Chia-I Wu78c2a352015-07-03 11:49:42 +0800779 vkCmdCopyImage(cmd_buf.handle(),
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800780 src_image.handle(), src_image.layout(),
781 handle(), layout(),
Tony Barbour17c6ab12015-03-27 17:03:18 -0600782 1, &copy_region);
Tony Barbour16b71bc2015-04-01 17:47:06 -0600783
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600784 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR_BIT, src_image_layout);
Tony Barbour16b71bc2015-04-01 17:47:06 -0600785
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600786 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR_BIT, dest_image_layout);
Tony Barbour17c6ab12015-03-27 17:03:18 -0600787
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600788 err = cmd_buf.EndCommandBuffer();
Tony Barbour16b71bc2015-04-01 17:47:06 -0600789 assert(!err);
790
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600791 cmd_buf.QueueCommandBuffer();
Tony Barbour16b71bc2015-04-01 17:47:06 -0600792
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600793 return VK_SUCCESS;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600794}
795
Tony Barbour01999182015-04-09 12:58:51 -0600796VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
797 :VkImageObj(device)
Tony Barbourf43b6982014-11-25 13:18:32 -0700798{
799 m_device = device;
Tony Barbour8205d902015-04-16 15:59:00 -0600800 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbour2f421a02015-04-01 16:38:10 -0600801 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour16b71bc2015-04-01 17:47:06 -0600802 void *data;
803 int32_t x, y;
Tony Barbour01999182015-04-09 12:58:51 -0600804 VkImageObj stagingImage(device);
Tony Barbour94310562015-04-22 15:10:33 -0600805 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600806
Tony Barboura7ec3ba2015-07-21 17:01:42 -0600807 stagingImage.init(16, 16, tex_format, VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600808 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR_BIT, 0, 0));
Tony Barbour2f421a02015-04-01 16:38:10 -0600809
810 if (colors == NULL)
811 colors = tex_colors;
Tony Barbourf43b6982014-11-25 13:18:32 -0700812
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600813 memset(&m_imageInfo,0,sizeof(m_imageInfo));
Tony Barbourf43b6982014-11-25 13:18:32 -0700814
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600815 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600816 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbour901f3bc2015-04-01 17:10:07 -0600817 view.pNext = NULL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600818 view.image = VK_NULL_HANDLE;
Tony Barbour8205d902015-04-16 15:59:00 -0600819 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600820 view.format = tex_format;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600821 view.channels.r = VK_CHANNEL_SWIZZLE_R;
822 view.channels.g = VK_CHANNEL_SWIZZLE_G;
823 view.channels.b = VK_CHANNEL_SWIZZLE_B;
824 view.channels.a = VK_CHANNEL_SWIZZLE_A;
Courtney Goeltzenleuchteraeffeae2015-09-10 17:58:54 -0600825 view.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbour901f3bc2015-04-01 17:10:07 -0600826 view.subresourceRange.baseMipLevel = 0;
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -0600827 view.subresourceRange.numLevels = 1;
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600828 view.subresourceRange.baseArrayLayer = 0;
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -0600829 view.subresourceRange.numLayers = 1;
Tony Barbourf43b6982014-11-25 13:18:32 -0700830
Tony Barbourf43b6982014-11-25 13:18:32 -0700831 /* create image */
Tony Barboura7ec3ba2015-07-21 17:01:42 -0600832 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barbourf43b6982014-11-25 13:18:32 -0700833
834 /* create image view */
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800835 view.image = handle();
Chia-I Wudbec1222014-12-28 15:55:09 +0800836 m_textureView.init(*m_device, view);
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600837 m_imageInfo.imageView = m_textureView.handle();
Tony Barbourf43b6982014-11-25 13:18:32 -0700838
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800839 data = stagingImage.MapMemory();
Tony Barbourf43b6982014-11-25 13:18:32 -0700840
Chia-I Wudbec1222014-12-28 15:55:09 +0800841 for (y = 0; y < extent().height; y++) {
Tony Barbourf43b6982014-11-25 13:18:32 -0700842 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wudbec1222014-12-28 15:55:09 +0800843 for (x = 0; x < extent().width; x++)
Tony Barbour2f421a02015-04-01 16:38:10 -0600844 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barbourf43b6982014-11-25 13:18:32 -0700845 }
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800846 stagingImage.UnmapMemory();
Tony Barbour01999182015-04-09 12:58:51 -0600847 VkImageObj::CopyImage(stagingImage);
Tony Barbourf43b6982014-11-25 13:18:32 -0700848}
Tony Barbour408ca622014-12-04 14:33:33 -0700849
Tony Barbour01999182015-04-09 12:58:51 -0600850VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barbourf43b6982014-11-25 13:18:32 -0700851{
Tony Barbourf43b6982014-11-25 13:18:32 -0700852 m_device = device;
Tony Barbourf43b6982014-11-25 13:18:32 -0700853
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600854 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wua64266a2014-12-28 16:32:24 +0800855 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600856 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
857 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
858 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbour8205d902015-04-16 15:59:00 -0600859 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchter51624412015-09-10 14:08:50 -0600860 samplerCreateInfo.addressModeU = VK_TEX_ADDRESS_MODE_WRAP;
861 samplerCreateInfo.addressModeV = VK_TEX_ADDRESS_MODE_WRAP;
862 samplerCreateInfo.addressModeW = VK_TEX_ADDRESS_MODE_WRAP;
Chia-I Wua64266a2014-12-28 16:32:24 +0800863 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour8bef8ee2015-05-22 09:44:58 -0600864 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600865 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wua64266a2014-12-28 16:32:24 +0800866 samplerCreateInfo.minLod = 0.0;
867 samplerCreateInfo.maxLod = 0.0;
Tony Barbour2c4e7c72015-06-25 16:56:44 -0600868 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Mark Lobodzinski513acdf2015-09-01 15:42:56 -0600869 samplerCreateInfo.unnormalizedCoordinates = VK_FALSE;
Tony Barbourf43b6982014-11-25 13:18:32 -0700870
Chia-I Wua64266a2014-12-28 16:32:24 +0800871 init(*m_device, samplerCreateInfo);
Tony Barbour2cb6bf52014-12-03 15:59:38 -0700872}
Tony Barbourf43b6982014-11-25 13:18:32 -0700873
Courtney Goeltzenleuchterea2db0d2014-12-04 15:18:47 -0700874/*
875 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
876 */
Tony Barbour01999182015-04-09 12:58:51 -0600877VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterea2db0d2014-12-04 15:18:47 -0700878{
879 m_device = device;
Tony Barbour9f47dc72014-12-10 14:36:31 -0700880 m_commandBuffer = 0;
Courtney Goeltzenleuchterea2db0d2014-12-04 15:18:47 -0700881
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600882 memset(&m_descriptorBufferInfo,0,sizeof(m_descriptorBufferInfo));
Courtney Goeltzenleuchterea2db0d2014-12-04 15:18:47 -0700883}
884
Tony Barbour01999182015-04-09 12:58:51 -0600885VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour864c0332015-03-26 12:37:52 -0600886{
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600887 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour864c0332015-03-26 12:37:52 -0600888 if (m_commandBuffer) {
889 delete m_commandBuffer;
Tony Barbour9a3558d2015-07-28 10:23:02 -0600890 delete m_cmdPool;
Tony Barbour864c0332015-03-26 12:37:52 -0600891 }
892}
893
Tony Barbour01999182015-04-09 12:58:51 -0600894VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barbourf43b6982014-11-25 13:18:32 -0700895{
Tony Barbourf43b6982014-11-25 13:18:32 -0700896 m_device = device;
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800897 m_commandBuffer = 0;
898
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600899 memset(&m_descriptorBufferInfo,0,sizeof(m_descriptorBufferInfo));
Tony Barbourf43b6982014-11-25 13:18:32 -0700900 m_numVertices = constantCount;
901 m_stride = constantSize;
902
Tony Barbour94310562015-04-22 15:10:33 -0600903 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800904 const size_t allocationSize = constantCount * constantSize;
Cody Northropd0e7d222015-06-22 14:56:14 -0600905 init_as_src_and_dst(*m_device, allocationSize, reqs);
Tony Barbourf43b6982014-11-25 13:18:32 -0700906
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800907 void *pData = memory().map();
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800908 memcpy(pData, data, allocationSize);
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800909 memory().unmap();
Tony Barbourf43b6982014-11-25 13:18:32 -0700910
Courtney Goeltzenleuchter2a3e3a32015-09-11 15:29:21 -0600911 /*
912 * Constant buffers are going to be used as vertex input buffers
913 * or as shader uniform buffers. So, we'll create the shaderbuffer
914 * descriptor here so it's ready if needed.
915 */
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600916 this->m_descriptorBufferInfo.buffer = handle();
917 this->m_descriptorBufferInfo.offset = 0;
918 this->m_descriptorBufferInfo.range = allocationSize;
Tony Barbourf43b6982014-11-25 13:18:32 -0700919}
Tony Barbour408ca622014-12-04 14:33:33 -0700920
Tony Barbour8205d902015-04-16 15:59:00 -0600921void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter7715bc42014-12-04 15:26:56 -0700922{
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800923 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &handle(), &offset);
Courtney Goeltzenleuchter7715bc42014-12-04 15:26:56 -0700924}
925
926
Tony Barbour01999182015-04-09 12:58:51 -0600927void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600928 VkFlags outputMask /*=
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600929 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600930 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
931 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
932 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
933 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600934 VkFlags inputMask /*=
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600935 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600936 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
937 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
938 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
939 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
940 VK_MEMORY_INPUT_SHADER_READ_BIT |
941 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
942 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
943 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barbourf43b6982014-11-25 13:18:32 -0700944{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600945 VkResult err = VK_SUCCESS;
Tony Barbour9f47dc72014-12-10 14:36:31 -0700946
Tony Barbour9f47dc72014-12-10 14:36:31 -0700947 if (!m_commandBuffer)
948 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600949 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour1490c912015-07-28 10:17:20 -0600950 VkCmdPoolCreateInfo cmd_pool_info = {};
951 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
952 cmd_pool_info.pNext = NULL;
953 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
954 cmd_pool_info.flags = 0;
955 m_cmdPool = new vk_testing::CmdPool(*m_device, cmd_pool_info);
956 m_commandBuffer = new VkCommandBufferObj(m_device, m_cmdPool->handle());
Tony Barbour9f47dc72014-12-10 14:36:31 -0700957 }
958 else
959 {
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800960 m_device->wait(m_fence);
Tony Barbour9f47dc72014-12-10 14:36:31 -0700961 }
962
Tony Barbourf43b6982014-11-25 13:18:32 -0700963 // open the command buffer
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600964 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600965 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbour901f3bc2015-04-01 17:10:07 -0600966 cmd_buf_info.pNext = NULL;
967 cmd_buf_info.flags = 0;
Cody Northrop16898b02015-08-11 11:35:58 -0600968 cmd_buf_info.renderPass = VK_NULL_HANDLE;
969 cmd_buf_info.subpass = 0;
970 cmd_buf_info.framebuffer = VK_NULL_HANDLE;
Tony Barbour901f3bc2015-04-01 17:10:07 -0600971
Jon Ashburn744b8cf2014-12-31 17:10:47 -0700972 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600973 ASSERT_VK_SUCCESS(err);
Tony Barbourf43b6982014-11-25 13:18:32 -0700974
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600975 VkBufferMemoryBarrier memory_barrier =
Mike Stroyan55658c22014-12-04 11:08:39 +0000976 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600977 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barbourf43b6982014-11-25 13:18:32 -0700978
Tony Barbourc2e987e2015-06-29 16:20:35 -0600979 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
980 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyan55658c22014-12-04 11:08:39 +0000981
982 // write barrier to the command buffer
Tony Barbourc2e987e2015-06-29 16:20:35 -0600983 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barbourf43b6982014-11-25 13:18:32 -0700984
985 // finish recording the command buffer
Tony Barboure8f14162014-12-10 17:28:39 -0700986 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600987 ASSERT_VK_SUCCESS(err);
Tony Barbourf43b6982014-11-25 13:18:32 -0700988
Tony Barbourf43b6982014-11-25 13:18:32 -0700989 // submit the command buffer to the universal queue
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600990 VkCmdBuffer bufferArray[1];
Tony Barboure8f14162014-12-10 17:28:39 -0700991 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchter1a748e92015-10-27 11:22:14 -0600992 VkSubmitInfo submit_info;
993 submit_info.waitSemCount = 0;
994 submit_info.pWaitSemaphores = NULL;
995 submit_info.cmdBufferCount = 1;
996 submit_info.pCommandBuffers = bufferArray;
997 submit_info.signalSemCount = 0;
998 submit_info.pSignalSemaphores = NULL;
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600999
1000 err = vkQueueSubmit(m_device->m_queue, 1, &submit_info, m_fence.handle());
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001001 ASSERT_VK_SUCCESS(err);
Tony Barbourf43b6982014-11-25 13:18:32 -07001002}
1003
Tony Barbour01999182015-04-09 12:58:51 -06001004VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
1005 : VkConstantBufferObj(device)
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001006{
1007
1008}
1009
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001010void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001011{
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001012 m_numVertices = numIndexes;
1013 m_indexType = indexType;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001014 switch (indexType) {
Tony Barbour8205d902015-04-16 15:59:00 -06001015 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001016 m_stride = 2;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001017 break;
Tony Barbour8205d902015-04-16 15:59:00 -06001018 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001019 m_stride = 4;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001020 break;
Chia-I Wubbdeacc2014-12-15 23:50:11 +08001021 default:
1022 assert(!"unknown index type");
Tony Barbour22a30862015-04-22 09:02:32 -06001023 m_stride = 2;
Chia-I Wubbdeacc2014-12-15 23:50:11 +08001024 break;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001025 }
1026
Chia-I Wu67ee00f2014-12-28 15:26:08 +08001027 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour94310562015-04-22 15:10:33 -06001028 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northropd0e7d222015-06-22 14:56:14 -06001029 init_as_src_and_dst(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001030
Chia-I Wuf4aed6c2015-07-03 13:44:34 +08001031 void *pData = memory().map();
Chia-I Wu67ee00f2014-12-28 15:26:08 +08001032 memcpy(pData, data, allocationSize);
Chia-I Wuf4aed6c2015-07-03 13:44:34 +08001033 memory().unmap();
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001034
Courtney Goeltzenleuchtere4099742015-10-22 15:31:56 -06001035 // set up the descriptor for the constant buffer
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001036 this->m_descriptorBufferInfo.buffer = handle();
1037 this->m_descriptorBufferInfo.offset = 0;
1038 this->m_descriptorBufferInfo.range = allocationSize;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001039}
1040
Tony Barbour8205d902015-04-16 15:59:00 -06001041void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001042{
Chia-I Wuf4aed6c2015-07-03 13:44:34 +08001043 vkCmdBindIndexBuffer(cmdBuffer, handle(), offset, m_indexType);
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001044}
Tony Barbourf43b6982014-11-25 13:18:32 -07001045
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001046VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbour140a59f2014-12-17 10:57:58 -07001047{
1048 return m_indexType;
1049}
1050
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001051VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barbourf43b6982014-11-25 13:18:32 -07001052{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001053 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001054 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wub48eddb2015-07-03 11:49:42 +08001055 stageInfo->shader = handle();
Tony Barbourf43b6982014-11-25 13:18:32 -07001056
Tony Barbourf43b6982014-11-25 13:18:32 -07001057 return stageInfo;
1058}
1059
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001060VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStageFlagBits stage, VkRenderFramework *framework)
Tony Barbourf43b6982014-11-25 13:18:32 -07001061{
Tony Barbour9687cb12015-07-14 13:34:05 -06001062 VkResult U_ASSERT_ONLY err = VK_SUCCESS;
Cody Northropacfb0492015-03-17 15:55:58 -06001063 std::vector<unsigned int> spv;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001064 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001065 VkShaderModuleCreateInfo moduleCreateInfo;
Tony Barboura024aa02015-07-27 13:31:04 -06001066 vk_testing::ShaderModule *module = new vk_testing::ShaderModule();
Tony Barbourf43b6982014-11-25 13:18:32 -07001067 size_t shader_len;
1068
1069 m_stage = stage;
1070 m_device = device;
1071
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001072 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1073 moduleCreateInfo.pNext = NULL;
1074
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001075 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barbourf43b6982014-11-25 13:18:32 -07001076 createInfo.pNext = NULL;
1077
Cody Northrop1cfbd172015-06-03 16:49:20 -06001078 if (framework->m_use_glsl) {
Tony Barbourf43b6982014-11-25 13:18:32 -07001079
1080 shader_len = strlen(shader_code);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001081 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
Chia-I Wu036b1612015-10-26 19:22:06 +08001082 moduleCreateInfo.pCode = (uint32_t *) malloc(moduleCreateInfo.codeSize);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001083 moduleCreateInfo.flags = 0;
Tony Barbourf43b6982014-11-25 13:18:32 -07001084
Tony Barbour8205d902015-04-16 15:59:00 -06001085 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001086 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1087 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1088 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1089 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001090
Cody Northrop1cfbd172015-06-03 16:49:20 -06001091 } else {
Tony Barbourf43b6982014-11-25 13:18:32 -07001092
Cody Northropacfb0492015-03-17 15:55:58 -06001093 // Use Reference GLSL to SPV compiler
1094 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001095 moduleCreateInfo.pCode = spv.data();
1096 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1097 moduleCreateInfo.flags = 0;
Chia-I Wuc2c31082014-12-29 14:14:03 +08001098 }
Cody Northropa44c2ff2015-04-15 11:19:06 -06001099
Tony Barboura024aa02015-07-27 13:31:04 -06001100 err = module->init_try(*m_device, moduleCreateInfo);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001101 assert(VK_SUCCESS == err);
Cody Northrop1cfbd172015-06-03 16:49:20 -06001102
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001103 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1104 createInfo.pNext = NULL;
Tony Barboura024aa02015-07-27 13:31:04 -06001105 createInfo.module = module->handle();
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001106 createInfo.pName = "main";
1107 createInfo.flags = 0;
Cody Northrop87ae5e12015-08-24 15:11:10 -06001108 createInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001109
1110 err = init_try(*m_device, createInfo);
Cody Northropa44c2ff2015-04-15 11:19:06 -06001111 assert(VK_SUCCESS == err);
Tony Barboura024aa02015-07-27 13:31:04 -06001112 framework->m_shader_modules.push_back(module);
Tony Barbour2cb6bf52014-12-03 15:59:38 -07001113}
Tony Barbour408ca622014-12-04 14:33:33 -07001114
Tony Barbour01999182015-04-09 12:58:51 -06001115VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barbourf43b6982014-11-25 13:18:32 -07001116{
Tony Barbourf43b6982014-11-25 13:18:32 -07001117 m_device = device;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001118
1119 m_vi_state.pNext = VK_NULL_HANDLE;
1120 m_vi_state.bindingCount = 0;
1121 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1122 m_vi_state.attributeCount = 0;
1123 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1124
Tony Barbourf43b6982014-11-25 13:18:32 -07001125 m_vertexBufferCount = 0;
1126
Tony Barboure307f582015-07-10 15:29:03 -06001127 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001128 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbour8205d902015-04-16 15:59:00 -06001129 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001130 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barbourf43b6982014-11-25 13:18:32 -07001131
Tony Barboure307f582015-07-10 15:29:03 -06001132 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001133 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterc0f9fa72015-10-15 12:57:38 -06001134 m_rs_state.depthClampEnable = VK_TRUE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001135 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Tony Barbour8205d902015-04-16 15:59:00 -06001136 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter28846ae2015-04-30 17:44:12 -06001137 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1138 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Cody Northropf5bd2252015-08-17 11:10:49 -06001139 m_rs_state.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter2f5deb52015-09-30 16:16:57 -06001140 m_rs_state.lineWidth = 1.0f;
Chia-I Wufa950c52015-10-26 19:08:09 +08001141 m_rs_state.depthBiasConstantFactor = 0.0f;
Courtney Goeltzenleuchter2f5deb52015-09-30 16:16:57 -06001142 m_rs_state.depthBiasClamp = 0.0f;
Chia-I Wufa950c52015-10-26 19:08:09 +08001143 m_rs_state.depthBiasSlopeFactor = 0.0f;
Tony Barbourf43b6982014-11-25 13:18:32 -07001144
Tony Barbourf43b6982014-11-25 13:18:32 -07001145 memset(&m_cb_state,0,sizeof(m_cb_state));
Tony Barboure307f582015-07-10 15:29:03 -06001146 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001147 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001148 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Courtney Goeltzenleuchter2f5deb52015-09-30 16:16:57 -06001149 m_cb_state.blendConst[0] = 1.0f;
1150 m_cb_state.blendConst[1] = 1.0f;
1151 m_cb_state.blendConst[2] = 1.0f;
1152 m_cb_state.blendConst[3] = 1.0f;
Tony Barbourf43b6982014-11-25 13:18:32 -07001153
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001154 m_ms_state.pNext = VK_NULL_HANDLE;
Tony Barboure307f582015-07-10 15:29:03 -06001155 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001156 m_ms_state.pSampleMask = NULL;
Courtney Goeltzenleuchterc8f54f72015-10-15 17:59:39 -06001157 m_ms_state.alphaToCoverageEnable = VK_FALSE;
Courtney Goeltzenleuchterd4a39bf2015-10-23 15:47:29 -06001158 m_ms_state.alphaToOneEnable = VK_FALSE;
Tony Barboure094edf2015-06-26 10:18:34 -06001159 m_ms_state.rasterSamples = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001160 m_ms_state.minSampleShading = 0;
1161 m_ms_state.sampleShadingEnable = 0;
Tony Barbourf43b6982014-11-25 13:18:32 -07001162
Tony Barboure307f582015-07-10 15:29:03 -06001163 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001164 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbour5e245132015-05-15 09:37:57 -06001165 m_vp_state.viewportCount = 1;
Courtney Goeltzenleuchter2f5deb52015-09-30 16:16:57 -06001166 m_vp_state.scissorCount = 1;
Tony Barbour3c2a7d32015-10-02 13:33:51 -06001167 m_vp_state.pViewports = NULL;
1168 m_vp_state.pScissors = NULL;
Tony Barbour5e245132015-05-15 09:37:57 -06001169
Tony Barboure307f582015-07-10 15:29:03 -06001170 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001171 m_ds_state.pNext = VK_NULL_HANDLE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001172 m_ds_state.depthTestEnable = VK_FALSE;
1173 m_ds_state.depthWriteEnable = VK_FALSE;
Cody Northrope4bc6942015-08-26 10:01:32 -06001174 m_ds_state.depthBoundsTestEnable = VK_FALSE;
Tony Barbour8205d902015-04-16 15:59:00 -06001175 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001176 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1177 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1178 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001179 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001180 m_ds_state.stencilTestEnable = VK_FALSE;
Courtney Goeltzenleuchter2f5deb52015-09-30 16:16:57 -06001181 m_ds_state.back.stencilCompareMask = 0xff;
1182 m_ds_state.back.stencilWriteMask = 0xff;
1183 m_ds_state.back.stencilReference = 0;
1184 m_ds_state.minDepthBounds = 0.f;
1185 m_ds_state.maxDepthBounds = 1.f;
1186
Tony Barbourfa6cac72015-01-16 14:27:35 -07001187 m_ds_state.front = m_ds_state.back;
Tony Barbourf43b6982014-11-25 13:18:32 -07001188};
1189
Tony Barbour01999182015-04-09 12:58:51 -06001190void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barbourf43b6982014-11-25 13:18:32 -07001191{
1192 m_shaderObjs.push_back(shader);
1193}
1194
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001195void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barbourf43b6982014-11-25 13:18:32 -07001196{
1197 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1198 m_vi_state.attributeCount = count;
1199}
1200
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001201void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barbourf43b6982014-11-25 13:18:32 -07001202{
1203 m_vi_state.pVertexBindingDescriptions = vi_binding;
1204 m_vi_state.bindingCount = count;
1205}
1206
Tony Barboure307f582015-07-10 15:29:03 -06001207void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineColorBlendAttachmentState *att)
Chia-I Wue7748802014-12-05 10:45:15 +08001208{
Tony Barbourfa6cac72015-01-16 14:27:35 -07001209 if (binding+1 > m_colorAttachments.size())
1210 {
1211 m_colorAttachments.resize(binding+1);
1212 }
1213 m_colorAttachments[binding] = *att;
1214}
1215
Tony Barboure307f582015-07-10 15:29:03 -06001216void VkPipelineObj::SetDepthStencil(VkPipelineDepthStencilStateCreateInfo *ds_state)
Tony Barbourfa6cac72015-01-16 14:27:35 -07001217{
Tony Barbourfa6cac72015-01-16 14:27:35 -07001218 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1219 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
Cody Northrope4bc6942015-08-26 10:01:32 -06001220 m_ds_state.depthBoundsTestEnable = ds_state->depthBoundsTestEnable;
Tony Barbour8205d902015-04-16 15:59:00 -06001221 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001222 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1223 m_ds_state.back = ds_state->back;
1224 m_ds_state.front = ds_state->front;
Chia-I Wue7748802014-12-05 10:45:15 +08001225}
1226
Tobin Ehlisa88e2f52015-10-02 11:00:56 -06001227void VkPipelineObj::SetViewport(vector<VkViewport> viewports)
1228{
1229 m_viewports = viewports;
1230 // If we explicitly set a null viewport, pass it through to create info
Cody Northrop3e67d152015-10-27 13:42:57 -06001231 // but preserve viewportCount because it musn't change
1232 if (m_viewports.size() == 0) {
1233 m_vp_state.pViewports = nullptr;
Tobin Ehlisa88e2f52015-10-02 11:00:56 -06001234 }
1235}
1236
1237void VkPipelineObj::SetScissor(vector<VkRect2D> scissors)
1238{
1239 m_scissors = scissors;
1240 // If we explicitly set a null scissors, pass it through to create info
Cody Northrop3e67d152015-10-27 13:42:57 -06001241 // but preserve viewportCount because it musn't change
1242 if (m_scissors.size() == 0) {
1243 m_vp_state.pScissors = nullptr;
Tobin Ehlisa88e2f52015-10-02 11:00:56 -06001244 }
1245}
1246
Courtney Goeltzenleuchter2f5deb52015-09-30 16:16:57 -06001247void VkPipelineObj::MakeDynamic(VkDynamicState state)
1248{
1249 /* Only add a state once */
1250 for (auto it = m_dynamic_state_enables.begin(); it != m_dynamic_state_enables.end(); it++) {
1251 if ((*it) == state) return;
1252 }
1253 m_dynamic_state_enables.push_back(state);
1254}
1255
Tony Barbour875e7462015-08-06 09:27:11 -06001256void VkPipelineObj::SetMSAA(VkPipelineMultisampleStateCreateInfo *ms_state)
1257{
1258 memcpy(&m_ms_state, ms_state, sizeof(VkPipelineMultisampleStateCreateInfo));
1259}
1260
Tony Barboured132432015-08-04 16:23:11 -06001261VkResult VkPipelineObj::CreateVKPipeline(VkPipelineLayout layout, VkRenderPass render_pass)
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001262{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001263 VkGraphicsPipelineCreateInfo info = {};
Courtney Goeltzenleuchter2f5deb52015-09-30 16:16:57 -06001264 VkPipelineDynamicStateCreateInfo dsci = {};
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001265
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001266 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001267
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001268 info.stageCount = m_shaderObjs.size();
1269 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1270
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001271 for (int i=0; i<m_shaderObjs.size(); i++)
1272 {
Chia-I Wuf8385062015-01-04 16:27:24 +08001273 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001274 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001275 }
1276
Tony Barbour73c22f42015-07-27 09:36:24 -06001277 if (m_vi_state.attributeCount && m_vi_state.bindingCount) {
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001278 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barbour73c22f42015-07-27 09:36:24 -06001279 info.pVertexInputState = &m_vi_state;
1280 } else {
1281 info.pVertexInputState = NULL;
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001282 }
1283
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001284 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001285 info.pNext = NULL;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001286 info.flags = 0;
Tony Barboured132432015-08-04 16:23:11 -06001287 info.layout = layout;
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001288
Tony Barbourfa6cac72015-01-16 14:27:35 -07001289 m_cb_state.attachmentCount = m_colorAttachments.size();
Tony Barbourb0ae8c12015-07-27 09:37:48 -06001290 m_cb_state.pAttachments = m_colorAttachments.data();
Tony Barbourfa6cac72015-01-16 14:27:35 -07001291
Courtney Goeltzenleuchter2f5deb52015-09-30 16:16:57 -06001292 if (m_viewports.size() > 0) {
1293 m_vp_state.viewportCount = m_viewports.size();
1294 m_vp_state.pViewports = m_viewports.data();
1295 } else {
1296 MakeDynamic(VK_DYNAMIC_STATE_VIEWPORT);
1297 }
1298
1299 if (m_scissors.size() > 0) {
1300 m_vp_state.scissorCount = m_scissors.size();
1301 m_vp_state.pScissors = m_scissors.data();
1302 } else {
1303 MakeDynamic(VK_DYNAMIC_STATE_SCISSOR);
1304 }
1305
1306 if (m_dynamic_state_enables.size() > 0) {
1307 dsci.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1308 dsci.dynamicStateCount = m_dynamic_state_enables.size();
1309 dsci.pDynamicStates = m_dynamic_state_enables.data();
1310 info.pDynamicState = &dsci;
1311 }
1312
Chia-I Wuc278df82015-07-07 11:50:03 +08001313 info.renderPass = render_pass;
1314 info.subpass = 0;
Tony Barboure307f582015-07-10 15:29:03 -06001315 info.pTessellationState = NULL;
Tony Barboure307f582015-07-10 15:29:03 -06001316 info.pInputAssemblyState = &m_ia_state;
1317 info.pViewportState = &m_vp_state;
1318 info.pRasterState = &m_rs_state;
1319 info.pMultisampleState = &m_ms_state;
1320 info.pDepthStencilState = &m_ds_state;
1321 info.pColorBlendState = &m_cb_state;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001322
Chris Forbesdc2188c2015-05-25 11:13:26 +12001323 return init_try(*m_device, info);
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001324}
Chia-I Wuf5d81872014-12-29 14:24:14 +08001325
Tony Barbour1490c912015-07-28 10:17:20 -06001326VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device, VkCmdPool pool)
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001327{
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001328 m_device = device;
Courtney Goeltzenleuchterec561272015-07-13 12:57:11 -06001329
Tony Barbour1490c912015-07-28 10:17:20 -06001330 init(*device, vk_testing::CmdBuffer::create_info(pool));
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001331}
Tony Barboure8f14162014-12-10 17:28:39 -07001332
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001333VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001334{
Chia-I Wu78c2a352015-07-03 11:49:42 +08001335 return handle();
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001336}
Tony Barboure8f14162014-12-10 17:28:39 -07001337
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001338VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barboure8f14162014-12-10 17:28:39 -07001339{
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001340 begin(pInfo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001341 return VK_SUCCESS;
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001342}
1343
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001344VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001345{
1346 begin();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001347 return VK_SUCCESS;
Tony Barboure8f14162014-12-10 17:28:39 -07001348}
1349
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001350VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barboure8f14162014-12-10 17:28:39 -07001351{
Chia-I Wuae0564f2014-12-28 15:12:48 +08001352 end();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001353 return VK_SUCCESS;
Tony Barboure8f14162014-12-10 17:28:39 -07001354}
1355
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -06001356void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, VkBool32 byRegion, uint32_t memBarrierCount, const void* const* ppMemBarriers)
Tony Barboure8f14162014-12-10 17:28:39 -07001357{
Chia-I Wu78c2a352015-07-03 11:49:42 +08001358 vkCmdPipelineBarrier(handle(), src_stages, dest_stages, byRegion, memBarrierCount, ppMemBarriers);
Tony Barboure8f14162014-12-10 17:28:39 -07001359}
1360
Chris Forbese3105972015-06-24 14:34:53 +12001361void VkCommandBufferObj::ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour01999182015-04-09 12:58:51 -06001362 VkDepthStencilObj *depthStencilObj)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001363{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001364 uint32_t i;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001365 const VkFlags output_mask =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001366 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001367 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1368 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1369 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001370 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001371 const VkFlags input_mask = 0;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001372
1373 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001374 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchteraeffeae2015-09-10 17:58:54 -06001375 srRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001376 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -06001377 srRange.numLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -06001378 srRange.baseArrayLayer = 0;
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -06001379 srRange.numLayers = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001380
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001381 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001382 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyan55658c22014-12-04 11:08:39 +00001383 memory_barrier.outputMask = output_mask;
1384 memory_barrier.inputMask = input_mask;
Mark Lobodzinski4e97c452015-07-01 15:18:26 -06001385 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyan55658c22014-12-04 11:08:39 +00001386 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001387 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyan55658c22014-12-04 11:08:39 +00001388
Tony Barbourc2e987e2015-06-29 16:20:35 -06001389 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1390 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyan55658c22014-12-04 11:08:39 +00001391
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -07001392 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyan55658c22014-12-04 11:08:39 +00001393 memory_barrier.image = m_renderTargets[i]->image();
1394 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -06001395 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyan55658c22014-12-04 11:08:39 +00001396 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001397
Chia-I Wu78c2a352015-07-03 11:49:42 +08001398 vkCmdClearColorImage(handle(),
Mark Lobodzinski4e97c452015-07-01 15:18:26 -06001399 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterda4a99e2015-04-23 17:49:22 -06001400 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001401
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001402 }
1403
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001404 if (depthStencilObj)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001405 {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001406 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchteraeffeae2015-09-10 17:58:54 -06001407 dsRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001408 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -06001409 dsRange.numLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -06001410 dsRange.baseArrayLayer = 0;
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -06001411 dsRange.numLayers = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001412
1413 // prepare the depth buffer for clear
Mike Stroyan55658c22014-12-04 11:08:39 +00001414
Cody Northropf110c6e2015-08-04 10:47:08 -06001415 memory_barrier.oldLayout = memory_barrier.newLayout;
Mark Lobodzinski4e97c452015-07-01 15:18:26 -06001416 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuf4aed6c2015-07-03 13:44:34 +08001417 memory_barrier.image = depthStencilObj->handle();
Mike Stroyan55658c22014-12-04 11:08:39 +00001418 memory_barrier.subresourceRange = dsRange;
1419
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -06001420 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001421
Courtney Goeltzenleuchter315ad992015-09-15 18:03:22 -06001422 VkClearDepthStencilValue clear_value = {
1423 depth_clear_color,
1424 stencil_clear_color
1425 };
Chia-I Wu78c2a352015-07-03 11:49:42 +08001426 vkCmdClearDepthStencilImage(handle(),
Chia-I Wuf4aed6c2015-07-03 13:44:34 +08001427 depthStencilObj->handle(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchter315ad992015-09-15 18:03:22 -06001428 &clear_value,
Chris Forbes2951d7d2015-06-22 17:21:59 +12001429 1, &dsRange);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001430
1431 // prepare depth buffer for rendering
Chia-I Wuf4aed6c2015-07-03 13:44:34 +08001432 memory_barrier.image = depthStencilObj->handle();
Cody Northropf110c6e2015-08-04 10:47:08 -06001433 memory_barrier.newLayout = memory_barrier.oldLayout;
Mark Lobodzinski4e97c452015-07-01 15:18:26 -06001434 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyan55658c22014-12-04 11:08:39 +00001435 memory_barrier.subresourceRange = dsRange;
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -06001436 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001437 }
1438}
1439
Tony Barboure389b882015-07-20 13:00:10 -06001440void VkCommandBufferObj::FillBuffer(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize fill_size, uint32_t data)
1441{
1442 vkCmdFillBuffer( handle(), buffer, offset, fill_size, data);
1443}
1444
Mike Stroyan43909d82015-09-25 13:39:21 -06001445void VkCommandBufferObj::UpdateBuffer(VkBuffer buffer, VkDeviceSize destOffset, VkDeviceSize dataSize, const uint32_t *pData)
1446{
1447 vkCmdUpdateBuffer(handle(), buffer, destOffset, dataSize, pData);
1448}
1449
1450void VkCommandBufferObj::CopyImage(VkImage srcImage, VkImageLayout srcImageLayout, VkImage destImage, VkImageLayout destImageLayout, uint32_t regionCount, const VkImageCopy* pRegions)
1451{
1452 vkCmdCopyImage(handle(), srcImage, srcImageLayout, destImage, destImageLayout, regionCount, pRegions);
1453}
1454
1455void VkCommandBufferObj::ResolveImage(VkImage srcImage, VkImageLayout srcImageLayout, VkImage destImage, VkImageLayout destImageLayout, uint32_t regionCount, const VkImageResolve* pRegions)
1456{
1457 vkCmdResolveImage(handle(), srcImage, srcImageLayout, destImage, destImageLayout, regionCount, pRegions);
1458}
1459
Tony Barbour01999182015-04-09 12:58:51 -06001460void VkCommandBufferObj::PrepareAttachments()
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001461{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001462 uint32_t i;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001463 const VkFlags output_mask =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001464 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001465 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1466 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1467 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001468 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001469 const VkFlags input_mask =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001470 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001471 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1472 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1473 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1474 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1475 VK_MEMORY_INPUT_SHADER_READ_BIT |
1476 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1477 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001478 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyan55658c22014-12-04 11:08:39 +00001479
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001480 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchteraeffeae2015-09-10 17:58:54 -06001481 srRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001482 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -06001483 srRange.numLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -06001484 srRange.baseArrayLayer = 0;
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -06001485 srRange.numLayers = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001486
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001487 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001488 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyan55658c22014-12-04 11:08:39 +00001489 memory_barrier.outputMask = output_mask;
1490 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001491 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyan55658c22014-12-04 11:08:39 +00001492 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001493 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyan55658c22014-12-04 11:08:39 +00001494
Tony Barbourc2e987e2015-06-29 16:20:35 -06001495 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1496 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyan55658c22014-12-04 11:08:39 +00001497
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -07001498 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001499 {
Mike Stroyan55658c22014-12-04 11:08:39 +00001500 memory_barrier.image = m_renderTargets[i]->image();
1501 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -06001502 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyan55658c22014-12-04 11:08:39 +00001503 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001504 }
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001505}
1506
Chia-I Wuc278df82015-07-07 11:50:03 +08001507void VkCommandBufferObj::BeginRenderPass(const VkRenderPassBeginInfo &info)
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001508{
Chia-I Wu78c2a352015-07-03 11:49:42 +08001509 vkCmdBeginRenderPass( handle(), &info, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001510}
1511
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001512void VkCommandBufferObj::EndRenderPass()
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001513{
Chia-I Wu78c2a352015-07-03 11:49:42 +08001514 vkCmdEndRenderPass(handle());
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001515}
1516
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -06001517void VkCommandBufferObj::SetViewport(
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -06001518 uint32_t viewportCount,
1519 const VkViewport* pViewports)
1520{
1521 vkCmdSetViewport( handle(), viewportCount, pViewports);
1522}
1523
1524void VkCommandBufferObj::SetScissor(
1525 uint32_t scissorCount,
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -06001526 const VkRect2D* pScissors)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001527{
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -06001528 vkCmdSetScissor( handle(), scissorCount, pScissors);
Tony Barboure84a8d62015-07-10 14:10:27 -06001529}
1530
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -06001531void VkCommandBufferObj::SetLineWidth(float lineWidth)
Tony Barboure84a8d62015-07-10 14:10:27 -06001532{
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -06001533 vkCmdSetLineWidth( handle(), lineWidth);
Cody Northropf5bd2252015-08-17 11:10:49 -06001534}
1535
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -06001536void VkCommandBufferObj::SetDepthBias(
Chia-I Wufa950c52015-10-26 19:08:09 +08001537 float depthBiasConstantFactor,
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -06001538 float depthBiasClamp,
Chia-I Wufa950c52015-10-26 19:08:09 +08001539 float depthBiasSlopeFactor)
Cody Northropf5bd2252015-08-17 11:10:49 -06001540{
Chia-I Wufa950c52015-10-26 19:08:09 +08001541 vkCmdSetDepthBias( handle(), depthBiasConstantFactor, depthBiasClamp, depthBiasSlopeFactor);
Tony Barboure84a8d62015-07-10 14:10:27 -06001542}
1543
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -06001544void VkCommandBufferObj::SetBlendConstants(
1545 const float blendConst[4])
Tony Barboure84a8d62015-07-10 14:10:27 -06001546{
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -06001547 vkCmdSetBlendConstants( handle(), blendConst);
Tony Barboure84a8d62015-07-10 14:10:27 -06001548}
1549
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -06001550void VkCommandBufferObj::SetDepthBounds(
1551 float minDepthBounds,
1552 float maxDepthBounds)
Tony Barboure84a8d62015-07-10 14:10:27 -06001553{
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -06001554 vkCmdSetDepthBounds( handle(), minDepthBounds, maxDepthBounds);
Cody Northrop2605cb02015-08-18 15:21:16 -06001555}
1556
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -06001557void VkCommandBufferObj::SetStencilReadMask(
1558 VkStencilFaceFlags faceMask,
1559 uint32_t stencilCompareMask)
Cody Northrop2605cb02015-08-18 15:21:16 -06001560{
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -06001561 vkCmdSetStencilCompareMask( handle(), faceMask, stencilCompareMask);
1562}
1563
1564void VkCommandBufferObj::SetStencilWriteMask(
1565 VkStencilFaceFlags faceMask,
1566 uint32_t stencilWriteMask)
1567{
1568 vkCmdSetStencilWriteMask( handle(), faceMask, stencilWriteMask);
1569}
1570
1571void VkCommandBufferObj::SetStencilReference(
1572 VkStencilFaceFlags faceMask,
1573 uint32_t stencilReference)
1574{
1575 vkCmdSetStencilReference( handle(), faceMask, stencilReference);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001576}
1577
Tony Barbour01999182015-04-09 12:58:51 -06001578void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001579{
1580 m_renderTargets.push_back(renderTarget);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001581}
1582
Courtney Goeltzenleuchter4ff11cc2015-09-23 12:31:50 -06001583void VkCommandBufferObj::DrawIndexed(uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001584{
Courtney Goeltzenleuchter4ff11cc2015-09-23 12:31:50 -06001585 vkCmdDrawIndexed(handle(), indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001586}
1587
Courtney Goeltzenleuchter4ff11cc2015-09-23 12:31:50 -06001588void VkCommandBufferObj::Draw(uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001589{
Courtney Goeltzenleuchter4ff11cc2015-09-23 12:31:50 -06001590 vkCmdDraw(handle(), vertexCount, instanceCount, firstVertex, firstInstance);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001591}
1592
Tony Barbour01999182015-04-09 12:58:51 -06001593void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001594{
Tony Barboure84a8d62015-07-10 14:10:27 -06001595 VkFence nullFence = { VK_NULL_HANDLE };
1596 QueueCommandBuffer(nullFence);
Tony Barbour67324f72015-04-08 10:11:29 -06001597}
1598
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001599void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour67324f72015-04-08 10:11:29 -06001600{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001601 VkResult err = VK_SUCCESS;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001602
1603 // submit the command buffer to the universal queue
Courtney Goeltzenleuchter1a748e92015-10-27 11:22:14 -06001604 VkSubmitInfo submit_info;
1605 submit_info.waitSemCount = 0;
1606 submit_info.pWaitSemaphores = NULL;
1607 submit_info.cmdBufferCount = 1;
1608 submit_info.pCommandBuffers = &handle();
1609 submit_info.signalSemCount = 0;
1610 submit_info.pSignalSemaphores = NULL;
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -06001611
1612 err = vkQueueSubmit( m_device->m_queue, 1, &submit_info, fence );
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001613 ASSERT_VK_SUCCESS( err );
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001614
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001615 err = vkQueueWaitIdle( m_device->m_queue );
1616 ASSERT_VK_SUCCESS( err );
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001617
1618 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001619 vkDeviceWaitIdle(m_device->device());
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001620}
1621
Tony Barbour01999182015-04-09 12:58:51 -06001622void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001623{
Chia-I Wu78c2a352015-07-03 11:49:42 +08001624 vkCmdBindPipeline( handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.handle() );
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001625}
1626
Tony Barbour01999182015-04-09 12:58:51 -06001627void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001628{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001629 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu862c5572015-03-28 15:23:55 +08001630
Chia-I Wu714df452015-01-01 07:55:04 +08001631 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wu78c2a352015-07-03 11:49:42 +08001632 vkCmdBindDescriptorSets(handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northrop1a01b1d2015-04-16 13:41:56 -06001633 0, 1, &set_obj, 0, NULL );
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001634}
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001635
Cody Northrop1684adb2015-08-05 11:15:02 -06001636void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, VkDeviceSize offset)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001637{
Chia-I Wuf4aed6c2015-07-03 13:44:34 +08001638 vkCmdBindIndexBuffer(handle(), indexBuffer->handle(), offset, indexBuffer->GetIndexType());
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001639}
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001640
Tony Barbour8205d902015-04-16 15:59:00 -06001641void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001642{
Chia-I Wuf4aed6c2015-07-03 13:44:34 +08001643 vkCmdBindVertexBuffers(handle(), binding, 1, &vertexBuffer->handle(), &offset);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001644}
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001645
Tony Barbour01999182015-04-09 12:58:51 -06001646VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour17c6ab12015-03-27 17:03:18 -06001647{
1648 m_initialized = false;
1649}
Tony Barbour01999182015-04-09 12:58:51 -06001650bool VkDepthStencilObj::Initialized()
Tony Barbour17c6ab12015-03-27 17:03:18 -06001651{
1652 return m_initialized;
1653}
1654
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001655VkImageView* VkDepthStencilObj::BindInfo()
Tony Barbour17c6ab12015-03-27 17:03:18 -06001656{
Chia-I Wuc278df82015-07-07 11:50:03 +08001657 return &m_attachmentBindInfo;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001658}
1659
Chia-I Wuc278df82015-07-07 11:50:03 +08001660void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height, VkFormat format)
Tony Barbour17c6ab12015-03-27 17:03:18 -06001661{
Cody Northropafc718a2015-09-03 16:09:57 -06001662 VkImageCreateInfo image_info = {};
1663 VkImageViewCreateInfo view_info = {};
Tony Barbour17c6ab12015-03-27 17:03:18 -06001664
1665 m_device = device;
1666 m_initialized = true;
Chia-I Wuc278df82015-07-07 11:50:03 +08001667 m_depth_stencil_fmt = format;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001668
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001669 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001670 image_info.pNext = NULL;
Tony Barbour8205d902015-04-16 15:59:00 -06001671 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001672 image_info.format = m_depth_stencil_fmt;
1673 image_info.extent.width = width;
1674 image_info.extent.height = height;
1675 image_info.extent.depth = 1;
1676 image_info.mipLevels = 1;
Courtney Goeltzenleuchter2ebc2342015-10-21 17:57:31 -06001677 image_info.arrayLayers = 1;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001678 image_info.samples = 1;
Tony Barbour8205d902015-04-16 15:59:00 -06001679 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterc3b8eea2015-09-10 14:14:11 -06001680 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001681 image_info.flags = 0;
Tony Barbour9a3558d2015-07-28 10:23:02 -06001682 image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1683 image_info.queueFamilyCount = 0;
1684 image_info.pQueueFamilyIndices = NULL;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001685 init(*m_device, image_info);
1686
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001687 view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001688 view_info.pNext = NULL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001689 view_info.image = VK_NULL_HANDLE;
Courtney Goeltzenleuchteraeffeae2015-09-10 17:58:54 -06001690 view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001691 view_info.subresourceRange.baseMipLevel = 0;
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -06001692 view_info.subresourceRange.numLevels = 1;
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -06001693 view_info.subresourceRange.baseArrayLayer = 0;
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -06001694 view_info.subresourceRange.numLayers = 1;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001695 view_info.flags = 0;
Tony Barbour9a3558d2015-07-28 10:23:02 -06001696 view_info.format = m_depth_stencil_fmt;
Chia-I Wuf4aed6c2015-07-03 13:44:34 +08001697 view_info.image = handle();
Cody Northropafc718a2015-09-03 16:09:57 -06001698 view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001699 m_imageView.init(*m_device, view_info);
Tony Barbour17c6ab12015-03-27 17:03:18 -06001700
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001701 m_attachmentBindInfo = m_imageView.handle();
Tony Barbour17c6ab12015-03-27 17:03:18 -06001702}