blob: cc38ba8f903962c0e2508d2393cdb469f87e2484 [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Ian Elliott7e40db92015-08-21 15:09:33 -060029#include <vk_ext_khr_swapchain.h>
30#include <vk_ext_khr_device_swapchain.h>
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060031
Jon Ashburn83a64252015-04-15 11:31:12 -060032#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
Tony Barbour6a3faf02015-07-23 10:36:18 -060033#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
34{ \
35 fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
36 assert(fp##entrypoint != NULL); \
37}
Jon Ashburn83a64252015-04-15 11:31:12 -060038
Tony Barbour6918cd52015-04-09 12:58:51 -060039VkRenderFramework::VkRenderFramework() :
Tony Barbourfe3351b2015-07-28 10:17:20 -060040 m_cmdBuffer(),
Tony Barbourf77fd652015-04-09 11:07:02 -060041 m_renderPass(VK_NULL_HANDLE),
42 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060043 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070044 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060045 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
46 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Tony Barbour71bd4b32015-07-21 17:00:26 -060047 m_clear_via_load_op( true ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070048 m_depth_clear_color( 1.0 ),
Courtney Goeltzenleuchterb6c9aca2015-06-08 16:19:37 -060049 m_stencil_clear_color( 0 ),
50 m_depthStencil( NULL ),
51 m_dbgCreateMsgCallback( VK_NULL_HANDLE ),
52 m_dbgDestroyMsgCallback( VK_NULL_HANDLE ),
53 m_globalMsgCallback( VK_NULL_HANDLE ),
54 m_devMsgCallback( VK_NULL_HANDLE )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060055{
Tony Barbour1c45ce02015-03-27 17:03:18 -060056
Chia-I Wu08accc62015-07-07 11:50:03 +080057 memset(&m_renderPassBeginInfo, 0, sizeof(m_renderPassBeginInfo));
58 m_renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
59
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070060 // clear the back buffer to dark grey
Cody Northrop85789d52015-08-25 15:26:38 -060061 m_clear_color.float32[0] = 0.25f;
62 m_clear_color.float32[1] = 0.25f;
63 m_clear_color.float32[2] = 0.25f;
64 m_clear_color.float32[3] = 0.0f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060065}
66
Tony Barbour6918cd52015-04-09 12:58:51 -060067VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060068{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060069
70}
71
Tony Barbour6918cd52015-04-09 12:58:51 -060072void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060073{
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060074 std::vector<const char*> instance_layer_names;
75 std::vector<const char*> device_layer_names;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060076 std::vector<const char*> instance_extension_names;
77 std::vector<const char*> device_extension_names;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060078 InitFramework(
79 instance_layer_names, device_layer_names,
80 instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060081}
82
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060083void VkRenderFramework::InitFramework(
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060084 std::vector<const char *> instance_layer_names,
85 std::vector<const char *> device_layer_names,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060086 std::vector<const char *> instance_extension_names,
87 std::vector<const char *> device_extension_names,
88 PFN_vkDbgMsgCallback dbgFunction,
89 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060090{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060091 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060092 std::vector<VkExtensionProperties> instance_extensions;
93 std::vector<VkExtensionProperties> device_extensions;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060094 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060095
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -060096 /* TODO: Verify requested extensions are available */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060097
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060098 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060099 instInfo.pNext = NULL;
100 instInfo.pAppInfo = &app_info;
101 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600102 instInfo.layerCount = instance_layer_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600103 instInfo.ppEnabledLayerNames = instance_layer_names.data();
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600104 instInfo.extensionCount = instance_extension_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600105 instInfo.ppEnabledExtensionNames = instance_extension_names.data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600106 err = vkCreateInstance(&instInfo, &this->inst);
107 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600108
Jon Ashburn83a64252015-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 Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600113 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -0600114 ASSERT_GE(this->gpu_count, (uint32_t) 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600115 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-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 Goeltzenleuchterd971b612015-06-17 20:51:59 -0600123 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600124 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-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 Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600128 }
Tony Barbour15524c32015-04-29 17:34:29 -0600129 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600130
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600131 /* TODO: Verify requested physical device extensions are available */
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600132 m_device = new VkDeviceObj(0, objs[0], device_layer_names, device_extension_names);
Courtney Goeltzenleuchterd971b612015-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 Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600145 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600146
Tony Barbour6918cd52015-04-09 12:58:51 -0600147 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600148}
149
Tony Barbour6918cd52015-04-09 12:58:51 -0600150void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600151{
Tony Barbourfe3351b2015-07-28 10:17:20 -0600152 if (m_cmdBuffer)
153 delete m_cmdBuffer;
Cody Northrope62183e2015-07-09 18:08:05 -0600154 if (m_cmdPool) vkDestroyCommandPool(device(), m_cmdPool);
Tony Barbour67e99152015-07-10 14:10:27 -0600155 if (m_framebuffer) vkDestroyFramebuffer(device(), m_framebuffer);
156 if (m_renderPass) vkDestroyRenderPass(device(), m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600157
Courtney Goeltzenleuchter7e46b622015-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 Ehlis976fc162015-03-26 08:23:25 -0600161 while (!m_renderTargets.empty()) {
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600162 vkDestroyImageView(device(), m_renderTargets.back()->targetView(m_render_target_fmt));
Tony Barbour67e99152015-07-10 14:10:27 -0600163 vkDestroyImage(device(), m_renderTargets.back()->image());
Mike Stroyanb050c682015-04-17 12:36:38 -0600164 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600165 m_renderTargets.pop_back();
166 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600167
Tony Barbour1c45ce02015-03-27 17:03:18 -0600168 delete m_depthStencil;
Tony Barbour823e6ca2015-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 Barbour1c45ce02015-03-27 17:03:18 -0600174
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600175 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800176 delete m_device;
Chris Forbesbe2fae62015-07-07 11:02:22 +1200177 if (this->inst) vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600178}
179
Tony Barbour6918cd52015-04-09 12:58:51 -0600180void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600181{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600182 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600183
Tony Barbour6a3faf02015-07-23 10:36:18 -0600184 // Get the list of VkFormat's that are supported:
Ian Elliott7e40db92015-08-21 15:09:33 -0600185 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
Ian Elliott8b139792015-08-07 11:51:12 -0600186 uint32_t formatCount;
Ian Elliott7e40db92015-08-21 15:09:33 -0600187 VkSurfaceDescriptionKHR surface_description;
188 surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Tony Barbour6a3faf02015-07-23 10:36:18 -0600189 surface_description.pNext = NULL;
Ian Elliott7e40db92015-08-21 15:09:33 -0600190 GET_DEVICE_PROC_ADDR(device(), GetSurfaceFormatsKHR);
191 err = fpGetSurfaceFormatsKHR(device(),
192 (VkSurfaceDescriptionKHR *) &surface_description,
Ian Elliott8b139792015-08-07 11:51:12 -0600193 &formatCount, NULL);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600194 ASSERT_VK_SUCCESS(err);
Ian Elliott7e40db92015-08-21 15:09:33 -0600195 VkSurfaceFormatKHR *surfFormats =
196 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
197 err = fpGetSurfaceFormatsKHR(device(),
198 (VkSurfaceDescriptionKHR *) &surface_description,
Ian Elliott8b139792015-08-07 11:51:12 -0600199 &formatCount, surfFormats);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600200 ASSERT_VK_SUCCESS(err);
201 m_render_target_fmt = surfFormats[0].format;
202 free(surfFormats);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600203
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600204 m_lineWidth = 1.0f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700205
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600206 m_depthBias = 0.0f;
207 m_depthBiasClamp = 0.0f;
208 m_slopeScaledDepthBias = 0.0f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600209
Courtney Goeltzenleuchter49c73082015-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 Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600214
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600215 m_minDepthBounds = 0.f;
216 m_maxDepthBounds = 1.f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600217
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600218 m_stencilCompareMask = 0xff;
219 m_stencilWriteMask = 0xff;
220 m_stencilReference = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600221
Cody Northrope62183e2015-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 Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600229
Tony Barbourfe3351b2015-07-28 10:17:20 -0600230 m_cmdBuffer = new VkCommandBufferObj(m_device, m_cmdPool);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600231}
232
Tony Barbour6918cd52015-04-09 12:58:51 -0600233void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600234{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600235 VkViewport viewport;
Chris Forbesd9be82b2015-06-22 17:21:59 +1200236 VkRect2D scissor;
Tony Barbourf52346d2015-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 Goeltzenleuchter49c73082015-09-17 15:06:17 -0600243 m_viewports.push_back(viewport);
244
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600245 scissor.extent.width = (int32_t) width;
246 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700247 scissor.offset.x = 0;
248 scissor.offset.y = 0;
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600249 m_scissors.push_back(scissor);
Tony Barbourf52346d2015-01-16 14:27:35 -0700250
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600251 m_width = width;
252 m_height = height;
253}
254
Tony Barbour6918cd52015-04-09 12:58:51 -0600255void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600256{
257 InitViewport(m_width, m_height);
258}
Tony Barbour6918cd52015-04-09 12:58:51 -0600259void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600260{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700261 InitRenderTarget(1);
262}
263
Tony Barbour6918cd52015-04-09 12:58:51 -0600264void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700265{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600266 InitRenderTarget(targets, NULL);
267}
268
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600269void VkRenderFramework::InitRenderTarget(VkImageView *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600270{
271 InitRenderTarget(1, dsBinding);
272}
273
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600274void VkRenderFramework::InitRenderTarget(uint32_t targets, VkImageView *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600275{
Chia-I Wu08accc62015-07-07 11:50:03 +0800276 std::vector<VkAttachmentDescription> attachments;
277 std::vector<VkAttachmentReference> color_references;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600278 std::vector<VkImageView> bindings;
Cody Northropd2ad0342015-08-05 11:15:02 -0600279 attachments.reserve(targets + 1); // +1 for dsBinding
Chia-I Wu08accc62015-07-07 11:50:03 +0800280 color_references.reserve(targets);
Cody Northropd2ad0342015-08-05 11:15:02 -0600281 bindings.reserve(targets + 1); // +1 for dsBinding
Tony Barbour1c45ce02015-03-27 17:03:18 -0600282
Chia-I Wu08accc62015-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 Wuecebf752014-12-05 10:45:15 +0800294
Chia-I Wu08accc62015-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 Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600302 VkImageView bind = {};
Chia-I Wu08accc62015-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 Barbour6918cd52015-04-09 12:58:51 -0600312 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600313
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600314 VkFormatProperties props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600315 VkResult err;
316
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600317 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), m_render_target_fmt, &props);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600318 ASSERT_VK_SUCCESS(err);
319
320 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600321 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600322 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
323 VK_IMAGE_TILING_LINEAR);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600324 }
325 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600326 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600327 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
328 VK_IMAGE_TILING_OPTIMAL);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600329 }
330 else {
331 FAIL() << "Neither Linear nor Optimal allowed for render target";
332 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600333
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600334 m_renderTargets.push_back(img);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600335 bind = img->targetView(m_render_target_fmt);
Chia-I Wu08accc62015-07-07 11:50:03 +0800336 bindings.push_back(bind);
Chia-I Wuecebf752014-12-05 10:45:15 +0800337 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600338
Chia-I Wu08accc62015-07-07 11:50:03 +0800339 VkSubpassDescription subpass = {};
340 subpass.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION;
341 subpass.pNext = NULL;
342 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
343 subpass.flags = 0;
344 subpass.inputCount = 0;
Cody Northropa505dda2015-08-04 11:16:41 -0600345 subpass.pInputAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800346 subpass.colorCount = targets;
Cody Northropa505dda2015-08-04 11:16:41 -0600347 subpass.pColorAttachments = color_references.data();
348 subpass.pResolveAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800349
350 if (dsBinding) {
351 att.format = m_depth_stencil_fmt;
Tony Barbour71bd4b32015-07-21 17:00:26 -0600352 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;;
Chia-I Wu08accc62015-07-07 11:50:03 +0800353 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
354 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
355 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Cody Northrop78e71932015-09-03 16:09:57 -0600356 att.initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
357 att.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800358 attachments.push_back(att);
359
Cody Northrop85789d52015-08-25 15:26:38 -0600360 clear.depthStencil.depth = m_depth_clear_color;
361 clear.depthStencil.stencil = m_stencil_clear_color;
Chia-I Wu08accc62015-07-07 11:50:03 +0800362 m_renderPassClearValues.push_back(clear);
363
364 bindings.push_back(*dsBinding);
365
366 subpass.depthStencilAttachment.attachment = targets;
Cody Northrop78e71932015-09-03 16:09:57 -0600367 subpass.depthStencilAttachment.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800368 } else {
369 subpass.depthStencilAttachment.attachment = VK_ATTACHMENT_UNUSED;
370 }
371
372 subpass.preserveCount = 0;
Cody Northropa505dda2015-08-04 11:16:41 -0600373 subpass.pPreserveAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800374
375 VkRenderPassCreateInfo rp_info = {};
376 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
377 rp_info.attachmentCount = attachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600378 rp_info.pAttachments = attachments.data();
Chia-I Wu08accc62015-07-07 11:50:03 +0800379 rp_info.subpassCount = 1;
380 rp_info.pSubpasses = &subpass;
381
382 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
383
384 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600385 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600386 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600387 fb_info.pNext = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800388 fb_info.renderPass = m_renderPass;
389 fb_info.attachmentCount = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600390 fb_info.pAttachments = bindings.data();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600391 fb_info.width = (uint32_t)m_width;
392 fb_info.height = (uint32_t)m_height;
393 fb_info.layers = 1;
394
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600395 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600396
Chia-I Wu08accc62015-07-07 11:50:03 +0800397 m_renderPassBeginInfo.renderPass = m_renderPass;
398 m_renderPassBeginInfo.framebuffer = m_framebuffer;
Cody Northropd2ad0342015-08-05 11:15:02 -0600399 m_renderPassBeginInfo.renderArea.extent.width = (int32_t) m_width;
400 m_renderPassBeginInfo.renderArea.extent.height = (int32_t) m_height;
Cody Northrop23dd89d2015-08-04 11:51:03 -0600401 m_renderPassBeginInfo.clearValueCount = m_renderPassClearValues.size();
402 m_renderPassBeginInfo.pClearValues = m_renderPassClearValues.data();
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600403}
404
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600405
406
Tony Barbourd1c35722015-04-16 15:59:00 -0600407VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600408 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800409{
410 init();
411
Chia-I Wu999f0482015-07-03 10:32:05 +0800412 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600413 queue_props = phy().queue_properties().data();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800414}
415
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600416VkDeviceObj::VkDeviceObj(uint32_t id,
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600417 VkPhysicalDevice obj, std::vector<const char *> &layer_names,
418 std::vector<const char *> &extension_names) :
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600419 vk_testing::Device(obj), id(id)
420{
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600421 init(layer_names, extension_names);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600422
Chia-I Wu999f0482015-07-03 10:32:05 +0800423 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600424 queue_props = phy().queue_properties().data();
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600425}
426
Tony Barbour6918cd52015-04-09 12:58:51 -0600427void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800428{
429 ASSERT_NE(true, graphics_queues().empty());
Chia-I Wudf12ffd2015-07-03 10:53:18 +0800430 m_queue = graphics_queues()[0]->handle();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800431}
432
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600433VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
434 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700435{
Tony Barboure2c58df2014-11-25 13:18:32 -0700436
437}
438
Tony Barbour6918cd52015-04-09 12:58:51 -0600439VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700440{
Chia-I Wu11078b02015-01-04 16:27:24 +0800441 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700442}
Tony Barbour82c39522014-12-04 14:33:33 -0700443
Tony Barbour6918cd52015-04-09 12:58:51 -0600444int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700445{
Chia-I Wu11078b02015-01-04 16:27:24 +0800446 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600447 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600448 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800449 tc.count = 1;
450 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700451
Chia-I Wu11078b02015-01-04 16:27:24 +0800452 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700453}
Tony Barbour82c39522014-12-04 14:33:33 -0700454
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600455int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700456{
Courtney Goeltzenleuchter6eb1aeb2015-09-11 15:29:21 -0600457 assert(type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
458 type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
459 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
460 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600461 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800462 tc.type = type;
463 tc.count = 1;
464 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700465
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800466 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
467 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600468
Chia-I Wu11078b02015-01-04 16:27:24 +0800469 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700470}
Tony Barbour82c39522014-12-04 14:33:33 -0700471
Tony Barbour6918cd52015-04-09 12:58:51 -0600472int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700473{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600474 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600475 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800476 tc.count = 1;
477 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700478
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800479 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Chia-I Wu8c721c62015-07-03 11:49:42 +0800480 tmp.sampler = sampler->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800481 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700482
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800483 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
484 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700485
Chia-I Wu11078b02015-01-04 16:27:24 +0800486 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700487}
Chia-I Wu11078b02015-01-04 16:27:24 +0800488
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500489VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700490{
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800491 return m_pipeline_layout.handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700492}
493
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600494VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700495{
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800496 return m_set->handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700497}
Tony Barboure2c58df2014-11-25 13:18:32 -0700498
Tony Barbour6918cd52015-04-09 12:58:51 -0600499void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700500{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600501 // create VkDescriptorPool
502 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600503 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800504 pool.count = m_type_counts.size();
Courtney Goeltzenleuchterfe908d32015-09-16 16:12:45 -0600505 pool.poolUsage = VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT;
506 pool.maxSets = 1;
Tony Barbour482c6092015-07-27 09:37:48 -0600507 pool.pTypeCount = m_type_counts.data();
Courtney Goeltzenleuchterfe908d32015-09-16 16:12:45 -0600508 init(*m_device, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700509
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600510 // create VkDescriptorSetLayout
511 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800512 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800513 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800514 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800515 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600516 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800517 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700518 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800519
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600520 // create VkDescriptorSetLayout
521 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600522 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800523 layout.count = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600524 layout.pBinding = bindings.data();
Chia-I Wub41393e2015-03-26 15:04:41 +0800525
Chia-I Wu41126e52015-03-26 15:27:55 +0800526 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600527 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800528 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500529
530 // create VkPipelineLayout
531 VkPipelineLayoutCreateInfo pipeline_layout = {};
532 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
533 pipeline_layout.descriptorSetCount = layouts.size();
534 pipeline_layout.pSetLayouts = NULL;
535
536 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700537
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600538 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500539 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800540
Chia-I Wu41126e52015-03-26 15:27:55 +0800541 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800542 size_t imageSamplerCount = 0;
543 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
544 it != m_writes.end(); it++) {
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800545 it->destSet = m_set->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800546 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
547 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800548 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800549
550 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800551 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700552}
Tony Barboure2c58df2014-11-25 13:18:32 -0700553
Tony Barbour6918cd52015-04-09 12:58:51 -0600554VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800555{
556 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800557 m_descriptorInfo.imageView = VK_NULL_HANDLE;
558 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800559}
560
Tony Barbour6918cd52015-04-09 12:58:51 -0600561void VkImageObj::ImageMemoryBarrier(
562 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600563 VkImageAspect aspect,
564 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600565 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600566 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
567 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
568 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
569 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600570 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600571 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600572 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
573 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
574 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
575 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
576 VK_MEMORY_INPUT_SHADER_READ_BIT |
577 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
578 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
579 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600580 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600581{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600582 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
583 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600584 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
585 subresourceRange);
586
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600587 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600588
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600589 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
590 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600591
592 // write barrier to the command buffer
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -0600593 vkCmdPipelineBarrier(cmd_buf->handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600594}
595
Tony Barbour6918cd52015-04-09 12:58:51 -0600596void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600597 VkImageAspect aspect,
598 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600599{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600600 VkFlags output_mask, input_mask;
601 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600602 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600603 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
604 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
605 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600606 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600607 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600608 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600609 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
610 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
611 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
612 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
613 VK_MEMORY_INPUT_SHADER_READ_BIT |
614 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
615 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600616 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600617
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800618 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600619 return;
620 }
621
622 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600623 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600624 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
625 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600626 break;
627
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600628 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600629 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
630 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600631 break;
632
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600633 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600634 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
635 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600636 break;
637
638 default:
639 output_mask = all_cache_outputs;
640 input_mask = all_cache_inputs;
641 break;
642 }
643
644 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800645 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600646}
647
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600648void VkImageObj::SetLayout(VkImageAspect aspect,
649 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600650{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600651 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600652
653 if (image_layout == m_descriptorInfo.imageLayout) {
654 return;
655 }
656
Tony Barbourfe3351b2015-07-28 10:17:20 -0600657 VkCmdPoolCreateInfo cmd_pool_info = {};
658 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
659 cmd_pool_info.pNext = NULL;
660 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
661 cmd_pool_info.flags = 0;
662 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
663 VkCommandBufferObj cmd_buf(m_device, pool.handle());
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600664
665 /* Build command buffer to set image layout in the driver */
666 err = cmd_buf.BeginCommandBuffer();
667 assert(!err);
668
669 SetLayout(&cmd_buf, aspect, image_layout);
670
671 err = cmd_buf.EndCommandBuffer();
672 assert(!err);
673
674 cmd_buf.QueueCommandBuffer();
675}
676
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600677bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600678{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600679 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600680 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600681 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600682
Tony Barbour579f7802015-04-03 15:11:43 -0600683 return true;
684}
685
Tony Barbour6918cd52015-04-09 12:58:51 -0600686void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600687 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600688 VkImageTiling requested_tiling,
689 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800690{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600691 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600692 VkFormatProperties image_fmt;
693 VkImageTiling tiling;
694 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600695
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800696 mipCount = 0;
697
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600698 uint32_t _w = w;
699 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800700 while( ( _w > 0 ) || ( _h > 0 ) )
701 {
702 _w >>= 1;
703 _h >>= 1;
704 mipCount++;
705 }
706
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600707 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), fmt, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600708 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600709
Tony Barbourd1c35722015-04-16 15:59:00 -0600710 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600711 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600712 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600713 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600714 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600715 } else {
716 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
717 }
718 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600719 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600720 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600721 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600722 } else {
723 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
724 }
725
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600726 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600727 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800728 imageCreateInfo.format = fmt;
729 imageCreateInfo.extent.width = w;
730 imageCreateInfo.extent.height = h;
731 imageCreateInfo.mipLevels = mipCount;
732 imageCreateInfo.tiling = tiling;
733
734 imageCreateInfo.usage = usage;
735
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600736 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800737
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600738 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600739 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600740 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600741 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600742 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800743}
744
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600745VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600746{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600747 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600748 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600749
Tony Barbourfe3351b2015-07-28 10:17:20 -0600750 VkCmdPoolCreateInfo cmd_pool_info = {};
751 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
752 cmd_pool_info.pNext = NULL;
753 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
754 cmd_pool_info.flags = 0;
755 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
756 VkCommandBufferObj cmd_buf(m_device, pool.handle());
757
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600758 /* Build command buffer to copy staging texture to usable texture */
759 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600760 assert(!err);
761
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600762 /* TODO: Can we determine image aspect from image object? */
763 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600764 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600765
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600766 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600767 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600768
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600769 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600770 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600771 copy_region.srcSubresource.arrayLayer = 0;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600772 copy_region.srcSubresource.mipLevel = 0;
773 copy_region.srcOffset.x = 0;
774 copy_region.srcOffset.y = 0;
775 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600776 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600777 copy_region.destSubresource.arrayLayer = 0;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600778 copy_region.destSubresource.mipLevel = 0;
779 copy_region.destOffset.x = 0;
780 copy_region.destOffset.y = 0;
781 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600782 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600783
Chia-I Wube2b9172015-07-03 11:49:42 +0800784 vkCmdCopyImage(cmd_buf.handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +0800785 src_image.handle(), src_image.layout(),
786 handle(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600787 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600788
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600789 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600790
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600791 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600792
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600793 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600794 assert(!err);
795
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600796 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600797
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600798 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600799}
800
Tony Barbour6918cd52015-04-09 12:58:51 -0600801VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
802 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700803{
804 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600805 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600806 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600807 void *data;
808 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600809 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600810 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600811
Tony Barboure65788f2015-07-21 17:01:42 -0600812 stagingImage.init(16, 16, tex_format, VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600813 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600814
815 if (colors == NULL)
816 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700817
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800818 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700819
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600820 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600821 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600822 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600823 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600824 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600825 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600826 view.channels.r = VK_CHANNEL_SWIZZLE_R;
827 view.channels.g = VK_CHANNEL_SWIZZLE_G;
828 view.channels.b = VK_CHANNEL_SWIZZLE_B;
829 view.channels.a = VK_CHANNEL_SWIZZLE_A;
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -0600830 view.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600831 view.subresourceRange.baseMipLevel = 0;
832 view.subresourceRange.mipLevels = 1;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600833 view.subresourceRange.baseArrayLayer = 0;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600834 view.subresourceRange.arraySize = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700835
Tony Barboure2c58df2014-11-25 13:18:32 -0700836 /* create image */
Tony Barboure65788f2015-07-21 17:01:42 -0600837 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700838
839 /* create image view */
Chia-I Wu681d7a02015-07-03 13:44:34 +0800840 view.image = handle();
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800841 m_textureView.init(*m_device, view);
Chia-I Wu3158bf32015-07-03 11:49:42 +0800842 m_descriptorInfo.imageView = m_textureView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700843
Chia-I Wu681d7a02015-07-03 13:44:34 +0800844 data = stagingImage.MapMemory();
Tony Barboure2c58df2014-11-25 13:18:32 -0700845
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800846 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700847 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800848 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600849 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700850 }
Chia-I Wu681d7a02015-07-03 13:44:34 +0800851 stagingImage.UnmapMemory();
Tony Barbour6918cd52015-04-09 12:58:51 -0600852 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700853}
Tony Barbour82c39522014-12-04 14:33:33 -0700854
Tony Barbour6918cd52015-04-09 12:58:51 -0600855VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700856{
Tony Barboure2c58df2014-11-25 13:18:32 -0700857 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700858
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600859 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800860 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600861 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
862 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
863 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600864 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchter97953352015-09-10 14:08:50 -0600865 samplerCreateInfo.addressModeU = VK_TEX_ADDRESS_MODE_WRAP;
866 samplerCreateInfo.addressModeV = VK_TEX_ADDRESS_MODE_WRAP;
867 samplerCreateInfo.addressModeW = VK_TEX_ADDRESS_MODE_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800868 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600869 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600870 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800871 samplerCreateInfo.minLod = 0.0;
872 samplerCreateInfo.maxLod = 0.0;
Tony Barbour26b17f82015-06-25 16:56:44 -0600873 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Mark Lobodzinski52ac6582015-09-01 15:42:56 -0600874 samplerCreateInfo.unnormalizedCoordinates = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700875
Chia-I Wue9864b52014-12-28 16:32:24 +0800876 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700877}
Tony Barboure2c58df2014-11-25 13:18:32 -0700878
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700879/*
880 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
881 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600882VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700883{
884 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700885 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700886
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800887 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700888}
889
Tony Barbour6918cd52015-04-09 12:58:51 -0600890VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600891{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600892 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600893 if (m_commandBuffer) {
894 delete m_commandBuffer;
Tony Barbour3d83f492015-07-28 10:23:02 -0600895 delete m_cmdPool;
Tony Barbour044703b2015-03-26 12:37:52 -0600896 }
897}
898
Tony Barbour6918cd52015-04-09 12:58:51 -0600899VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700900{
Tony Barboure2c58df2014-11-25 13:18:32 -0700901 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800902 m_commandBuffer = 0;
903
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800904 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700905 m_numVertices = constantCount;
906 m_stride = constantSize;
907
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600908 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800909 const size_t allocationSize = constantCount * constantSize;
Cody Northrop7fb43862015-06-22 14:56:14 -0600910 init_as_src_and_dst(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700911
Chia-I Wu681d7a02015-07-03 13:44:34 +0800912 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +0800913 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800914 memory().unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700915
Courtney Goeltzenleuchter6eb1aeb2015-09-11 15:29:21 -0600916 /*
917 * Constant buffers are going to be used as vertex input buffers
918 * or as shader uniform buffers. So, we'll create the shaderbuffer
919 * descriptor here so it's ready if needed.
920 */
Courtney Goeltzenleuchterb46f2272015-09-14 14:14:21 -0600921 this->m_descriptorInfo.bufferInfo.buffer = handle();
922 this->m_descriptorInfo.bufferInfo.offset = 0;
923 this->m_descriptorInfo.bufferInfo.range = allocationSize;
Tony Barboure2c58df2014-11-25 13:18:32 -0700924}
Tony Barbour82c39522014-12-04 14:33:33 -0700925
Tony Barbourd1c35722015-04-16 15:59:00 -0600926void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700927{
Chia-I Wu681d7a02015-07-03 13:44:34 +0800928 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &handle(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700929}
930
931
Tony Barbour6918cd52015-04-09 12:58:51 -0600932void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600933 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600934 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600935 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
936 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
937 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
938 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600939 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600940 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600941 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
942 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
943 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
944 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
945 VK_MEMORY_INPUT_SHADER_READ_BIT |
946 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
947 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
948 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700949{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600950 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700951
Tony Barbour38422802014-12-10 14:36:31 -0700952 if (!m_commandBuffer)
953 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600954 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbourfe3351b2015-07-28 10:17:20 -0600955 VkCmdPoolCreateInfo cmd_pool_info = {};
956 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
957 cmd_pool_info.pNext = NULL;
958 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
959 cmd_pool_info.flags = 0;
960 m_cmdPool = new vk_testing::CmdPool(*m_device, cmd_pool_info);
961 m_commandBuffer = new VkCommandBufferObj(m_device, m_cmdPool->handle());
Tony Barbour38422802014-12-10 14:36:31 -0700962 }
963 else
964 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800965 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700966 }
967
Tony Barboure2c58df2014-11-25 13:18:32 -0700968 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600969 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600970 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600971 cmd_buf_info.pNext = NULL;
972 cmd_buf_info.flags = 0;
Cody Northropcc09b9e2015-08-11 11:35:58 -0600973 cmd_buf_info.renderPass = VK_NULL_HANDLE;
974 cmd_buf_info.subpass = 0;
975 cmd_buf_info.framebuffer = VK_NULL_HANDLE;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600976
Jon Ashburnc4164b12014-12-31 17:10:47 -0700977 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600978 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700979
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600980 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000981 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600982 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700983
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600984 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
985 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000986
987 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600988 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700989
990 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700991 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600992 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700993
Tony Barboure2c58df2014-11-25 13:18:32 -0700994 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600995 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700996 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wud9e8e822015-07-03 11:45:55 +0800997 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.handle() );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600998 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700999}
1000
Tony Barbour6918cd52015-04-09 12:58:51 -06001001VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
1002 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001003{
1004
1005}
1006
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001007void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001008{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001009 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001010
1011 m_numVertices = numIndexes;
1012 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001013 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -06001014 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001015 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -06001016 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001017 break;
Tony Barbourd1c35722015-04-16 15:59:00 -06001018 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001019 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -06001020 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001021 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001022 default:
1023 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -06001024 m_stride = 2;
1025 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001026 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001027 }
1028
Chia-I Wua07fee62014-12-28 15:26:08 +08001029 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -06001030 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northrop7fb43862015-06-22 14:56:14 -06001031 init_as_src_and_dst(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001032
Chia-I Wu681d7a02015-07-03 13:44:34 +08001033 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +08001034 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +08001035 memory().unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001036
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001037 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001038 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001039 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001040 view_info.buffer = handle();
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001041 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001042 view_info.offset = 0;
1043 view_info.range = allocationSize;
1044 m_bufferView.init(*m_device, view_info);
1045
Chia-I Wu3158bf32015-07-03 11:49:42 +08001046 this->m_descriptorInfo.bufferView = m_bufferView.handle();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001047}
1048
Tony Barbourd1c35722015-04-16 15:59:00 -06001049void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001050{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001051 vkCmdBindIndexBuffer(cmdBuffer, handle(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001052}
Tony Barboure2c58df2014-11-25 13:18:32 -07001053
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001054VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001055{
1056 return m_indexType;
1057}
1058
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001059VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001060{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001061 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001062 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1063 stageInfo->stage = m_stage;
Chia-I Wu4d0c7922015-07-03 11:49:42 +08001064 stageInfo->shader = handle();
Tony Barboure2c58df2014-11-25 13:18:32 -07001065
Tony Barboure2c58df2014-11-25 13:18:32 -07001066 return stageInfo;
1067}
1068
Tony Barbourd1c35722015-04-16 15:59:00 -06001069VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001070{
Tony Barbourb5d2c942015-07-14 13:34:05 -06001071 VkResult U_ASSERT_ONLY err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001072 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001073 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001074 VkShaderModuleCreateInfo moduleCreateInfo;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001075 vk_testing::ShaderModule *module = new vk_testing::ShaderModule();
Tony Barboure2c58df2014-11-25 13:18:32 -07001076 size_t shader_len;
1077
1078 m_stage = stage;
1079 m_device = device;
1080
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001081 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1082 moduleCreateInfo.pNext = NULL;
1083
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001084 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001085 createInfo.pNext = NULL;
1086
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001087 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001088
1089 shader_len = strlen(shader_code);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001090 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1091 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1092 moduleCreateInfo.flags = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001093
Tony Barbourd1c35722015-04-16 15:59:00 -06001094 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001095 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1096 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1097 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1098 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barboure2c58df2014-11-25 13:18:32 -07001099
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001100 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001101
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001102 // Use Reference GLSL to SPV compiler
1103 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001104 moduleCreateInfo.pCode = spv.data();
1105 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1106 moduleCreateInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001107 }
Cody Northrop475663c2015-04-15 11:19:06 -06001108
Tony Barbour823e6ca2015-07-27 13:31:04 -06001109 err = module->init_try(*m_device, moduleCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001110 assert(VK_SUCCESS == err);
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001111
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001112 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1113 createInfo.pNext = NULL;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001114 createInfo.module = module->handle();
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001115 createInfo.pName = "main";
1116 createInfo.flags = 0;
Cody Northropfd4bcfd2015-08-24 15:11:10 -06001117 createInfo.stage = stage;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001118
1119 err = init_try(*m_device, createInfo);
Cody Northrop475663c2015-04-15 11:19:06 -06001120 assert(VK_SUCCESS == err);
Tony Barbour823e6ca2015-07-27 13:31:04 -06001121 framework->m_shader_modules.push_back(module);
Tony Barbourf325bf12014-12-03 15:59:38 -07001122}
Tony Barbour82c39522014-12-04 14:33:33 -07001123
Tony Barbour6918cd52015-04-09 12:58:51 -06001124VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001125{
Tony Barboure2c58df2014-11-25 13:18:32 -07001126 m_device = device;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001127
1128 m_vi_state.pNext = VK_NULL_HANDLE;
1129 m_vi_state.bindingCount = 0;
1130 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1131 m_vi_state.attributeCount = 0;
1132 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1133
Tony Barboure2c58df2014-11-25 13:18:32 -07001134 m_vertexBufferCount = 0;
1135
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001136 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001137 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001138 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001139 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001140
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001141 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001142 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchtera4c4ae02015-10-15 12:57:38 -06001143 m_rs_state.depthClampEnable = VK_TRUE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001144 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001145 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001146 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1147 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Cody Northrop12365112015-08-17 11:10:49 -06001148 m_rs_state.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001149 m_rs_state.lineWidth = 1.0f;
1150 m_rs_state.depthBias = 0.0f;
1151 m_rs_state.depthBiasClamp = 0.0f;
1152 m_rs_state.slopeScaledDepthBias = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -07001153
Tony Barboure2c58df2014-11-25 13:18:32 -07001154 memset(&m_cb_state,0,sizeof(m_cb_state));
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001155 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001156 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001157 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1158 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001159 m_cb_state.blendConst[0] = 1.0f;
1160 m_cb_state.blendConst[1] = 1.0f;
1161 m_cb_state.blendConst[2] = 1.0f;
1162 m_cb_state.blendConst[3] = 1.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -07001163
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001164 m_ms_state.pNext = VK_NULL_HANDLE;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001165 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop02e61ed2015-08-04 14:34:54 -06001166 m_ms_state.pSampleMask = NULL;
Tony Barbourdfd533a2015-06-26 10:18:34 -06001167 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001168 m_ms_state.minSampleShading = 0;
1169 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001170
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001171 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001172 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001173 m_vp_state.viewportCount = 1;
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001174 m_vp_state.scissorCount = 1;
Tony Barbour414dd9e2015-10-02 13:33:51 -06001175 m_vp_state.pViewports = NULL;
1176 m_vp_state.pScissors = NULL;
Tony Barbourd81b8882015-05-15 09:37:57 -06001177
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001178 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001179 m_ds_state.pNext = VK_NULL_HANDLE,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001180 m_ds_state.depthTestEnable = VK_FALSE;
1181 m_ds_state.depthWriteEnable = VK_FALSE;
Cody Northrop271ba752015-08-26 10:01:32 -06001182 m_ds_state.depthBoundsTestEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001183 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001184 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1185 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1186 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001187 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001188 m_ds_state.stencilTestEnable = VK_FALSE;
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001189 m_ds_state.back.stencilCompareMask = 0xff;
1190 m_ds_state.back.stencilWriteMask = 0xff;
1191 m_ds_state.back.stencilReference = 0;
1192 m_ds_state.minDepthBounds = 0.f;
1193 m_ds_state.maxDepthBounds = 1.f;
1194
Tony Barbourf52346d2015-01-16 14:27:35 -07001195 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001196};
1197
Tony Barbour6918cd52015-04-09 12:58:51 -06001198void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001199{
1200 m_shaderObjs.push_back(shader);
1201}
1202
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001203void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001204{
1205 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1206 m_vi_state.attributeCount = count;
1207}
1208
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001209void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001210{
1211 m_vi_state.pVertexBindingDescriptions = vi_binding;
1212 m_vi_state.bindingCount = count;
1213}
1214
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001215void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineColorBlendAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001216{
Tony Barbourf52346d2015-01-16 14:27:35 -07001217 if (binding+1 > m_colorAttachments.size())
1218 {
1219 m_colorAttachments.resize(binding+1);
1220 }
1221 m_colorAttachments[binding] = *att;
1222}
1223
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001224void VkPipelineObj::SetDepthStencil(VkPipelineDepthStencilStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001225{
Tony Barbourf52346d2015-01-16 14:27:35 -07001226 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1227 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
Cody Northrop271ba752015-08-26 10:01:32 -06001228 m_ds_state.depthBoundsTestEnable = ds_state->depthBoundsTestEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001229 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001230 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1231 m_ds_state.back = ds_state->back;
1232 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001233}
1234
Tobin Ehlisd332f282015-10-02 11:00:56 -06001235void VkPipelineObj::SetViewport(vector<VkViewport> viewports)
1236{
1237 m_viewports = viewports;
1238 // If we explicitly set a null viewport, pass it through to create info
1239 if (!m_viewports.size()) {
1240 m_vp_state.viewportCount = m_viewports.size();
1241 m_vp_state.pViewports = m_viewports.data();
1242 }
1243}
1244
1245void VkPipelineObj::SetScissor(vector<VkRect2D> scissors)
1246{
1247 m_scissors = scissors;
1248 // If we explicitly set a null scissors, pass it through to create info
1249 if (!m_scissors.size()) {
1250 m_vp_state.scissorCount = m_scissors.size();
1251 m_vp_state.pScissors = m_scissors.data();
1252 }
1253}
1254
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001255void VkPipelineObj::MakeDynamic(VkDynamicState state)
1256{
1257 /* Only add a state once */
1258 for (auto it = m_dynamic_state_enables.begin(); it != m_dynamic_state_enables.end(); it++) {
1259 if ((*it) == state) return;
1260 }
1261 m_dynamic_state_enables.push_back(state);
1262}
1263
Tony Barbour0ec8cd52015-08-06 09:27:11 -06001264void VkPipelineObj::SetMSAA(VkPipelineMultisampleStateCreateInfo *ms_state)
1265{
1266 memcpy(&m_ms_state, ms_state, sizeof(VkPipelineMultisampleStateCreateInfo));
1267}
1268
Tony Barbour5781e8f2015-08-04 16:23:11 -06001269VkResult VkPipelineObj::CreateVKPipeline(VkPipelineLayout layout, VkRenderPass render_pass)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001270{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001271 VkGraphicsPipelineCreateInfo info = {};
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001272 VkPipelineDynamicStateCreateInfo dsci = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001273
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001274 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001275
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001276 info.stageCount = m_shaderObjs.size();
1277 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1278
Tony Barbour976e1cf2014-12-17 11:57:31 -07001279 for (int i=0; i<m_shaderObjs.size(); i++)
1280 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001281 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001282 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001283 }
1284
Tony Barboure815fd72015-07-27 09:36:24 -06001285 if (m_vi_state.attributeCount && m_vi_state.bindingCount) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001286 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barboure815fd72015-07-27 09:36:24 -06001287 info.pVertexInputState = &m_vi_state;
1288 } else {
1289 info.pVertexInputState = NULL;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001290 }
1291
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001292 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001293 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001294 info.flags = 0;
Tony Barbour5781e8f2015-08-04 16:23:11 -06001295 info.layout = layout;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001296
Tony Barbourf52346d2015-01-16 14:27:35 -07001297 m_cb_state.attachmentCount = m_colorAttachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -06001298 m_cb_state.pAttachments = m_colorAttachments.data();
Tony Barbourf52346d2015-01-16 14:27:35 -07001299
Courtney Goeltzenleuchter507ba972015-09-30 16:16:57 -06001300 if (m_viewports.size() > 0) {
1301 m_vp_state.viewportCount = m_viewports.size();
1302 m_vp_state.pViewports = m_viewports.data();
1303 } else {
1304 MakeDynamic(VK_DYNAMIC_STATE_VIEWPORT);
1305 }
1306
1307 if (m_scissors.size() > 0) {
1308 m_vp_state.scissorCount = m_scissors.size();
1309 m_vp_state.pScissors = m_scissors.data();
1310 } else {
1311 MakeDynamic(VK_DYNAMIC_STATE_SCISSOR);
1312 }
1313
1314 if (m_dynamic_state_enables.size() > 0) {
1315 dsci.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1316 dsci.dynamicStateCount = m_dynamic_state_enables.size();
1317 dsci.pDynamicStates = m_dynamic_state_enables.data();
1318 info.pDynamicState = &dsci;
1319 }
1320
Chia-I Wu08accc62015-07-07 11:50:03 +08001321 info.renderPass = render_pass;
1322 info.subpass = 0;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001323 info.pTessellationState = NULL;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001324 info.pInputAssemblyState = &m_ia_state;
1325 info.pViewportState = &m_vp_state;
1326 info.pRasterState = &m_rs_state;
1327 info.pMultisampleState = &m_ms_state;
1328 info.pDepthStencilState = &m_ds_state;
1329 info.pColorBlendState = &m_cb_state;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001330
Chris Forbes95292b12015-05-25 11:13:26 +12001331 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001332}
Chia-I Wu2648d092014-12-29 14:24:14 +08001333
Tony Barbourfe3351b2015-07-28 10:17:20 -06001334VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device, VkCmdPool pool)
Tony Barbour6d047bf2014-12-10 14:34:45 -07001335{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001336 m_device = device;
Courtney Goeltzenleuchter0c8385b2015-07-13 12:57:11 -06001337
Tony Barbourfe3351b2015-07-28 10:17:20 -06001338 init(*device, vk_testing::CmdBuffer::create_info(pool));
Tony Barbour6d047bf2014-12-10 14:34:45 -07001339}
Tony Barbour471338d2014-12-10 17:28:39 -07001340
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001341VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001342{
Chia-I Wube2b9172015-07-03 11:49:42 +08001343 return handle();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001344}
Tony Barbour471338d2014-12-10 17:28:39 -07001345
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001346VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001347{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001348 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001349 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001350}
1351
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001352VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001353{
1354 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001355 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001356}
1357
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001358VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001359{
Chia-I Wud28343c2014-12-28 15:12:48 +08001360 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001361 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001362}
1363
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001364void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, VkBool32 byRegion, uint32_t memBarrierCount, const void* const* ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001365{
Chia-I Wube2b9172015-07-03 11:49:42 +08001366 vkCmdPipelineBarrier(handle(), src_stages, dest_stages, byRegion, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001367}
1368
Chris Forbesf0796e12015-06-24 14:34:53 +12001369void VkCommandBufferObj::ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001370 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001371{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001372 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001373 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001374 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001375 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1376 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1377 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001378 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001379 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001380
1381 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001382 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001383 srRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001384 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter13dbde02015-08-28 16:47:15 -06001385 srRange.mipLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001386 srRange.baseArrayLayer = 0;
1387 srRange.arraySize = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001388
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001389 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001390 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001391 memory_barrier.outputMask = output_mask;
1392 memory_barrier.inputMask = input_mask;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001393 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001394 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001395 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001396
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001397 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1398 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001399
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001400 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001401 memory_barrier.image = m_renderTargets[i]->image();
1402 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001403 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001404 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001405
Chia-I Wube2b9172015-07-03 11:49:42 +08001406 vkCmdClearColorImage(handle(),
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001407 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001408 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001409
Tony Barbour30cc9e82014-12-17 11:53:55 -07001410 }
1411
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001412 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001413 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001414 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001415 dsRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001416 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter13dbde02015-08-28 16:47:15 -06001417 dsRange.mipLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001418 dsRange.baseArrayLayer = 0;
1419 dsRange.arraySize = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001420
1421 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001422
Cody Northrop3cc85e92015-08-04 10:47:08 -06001423 memory_barrier.oldLayout = memory_barrier.newLayout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001424 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001425 memory_barrier.image = depthStencilObj->handle();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001426 memory_barrier.subresourceRange = dsRange;
1427
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001428 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001429
Courtney Goeltzenleuchter45df9e12015-09-15 18:03:22 -06001430 VkClearDepthStencilValue clear_value = {
1431 depth_clear_color,
1432 stencil_clear_color
1433 };
Chia-I Wube2b9172015-07-03 11:49:42 +08001434 vkCmdClearDepthStencilImage(handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +08001435 depthStencilObj->handle(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchter45df9e12015-09-15 18:03:22 -06001436 &clear_value,
Chris Forbesd9be82b2015-06-22 17:21:59 +12001437 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001438
1439 // prepare depth buffer for rendering
Chia-I Wu681d7a02015-07-03 13:44:34 +08001440 memory_barrier.image = depthStencilObj->handle();
Cody Northrop3cc85e92015-08-04 10:47:08 -06001441 memory_barrier.newLayout = memory_barrier.oldLayout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001442 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001443 memory_barrier.subresourceRange = dsRange;
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001444 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001445 }
1446}
1447
Tony Barbourc1eb1a52015-07-20 13:00:10 -06001448void VkCommandBufferObj::FillBuffer(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize fill_size, uint32_t data)
1449{
1450 vkCmdFillBuffer( handle(), buffer, offset, fill_size, data);
1451}
1452
Mike Stroyana3082432015-09-25 13:39:21 -06001453void VkCommandBufferObj::UpdateBuffer(VkBuffer buffer, VkDeviceSize destOffset, VkDeviceSize dataSize, const uint32_t *pData)
1454{
1455 vkCmdUpdateBuffer(handle(), buffer, destOffset, dataSize, pData);
1456}
1457
1458void VkCommandBufferObj::CopyImage(VkImage srcImage, VkImageLayout srcImageLayout, VkImage destImage, VkImageLayout destImageLayout, uint32_t regionCount, const VkImageCopy* pRegions)
1459{
1460 vkCmdCopyImage(handle(), srcImage, srcImageLayout, destImage, destImageLayout, regionCount, pRegions);
1461}
1462
1463void VkCommandBufferObj::ResolveImage(VkImage srcImage, VkImageLayout srcImageLayout, VkImage destImage, VkImageLayout destImageLayout, uint32_t regionCount, const VkImageResolve* pRegions)
1464{
1465 vkCmdResolveImage(handle(), srcImage, srcImageLayout, destImage, destImageLayout, regionCount, pRegions);
1466}
1467
Tony Barbour6918cd52015-04-09 12:58:51 -06001468void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001469{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001470 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001471 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001472 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001473 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1474 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1475 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001476 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001477 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001478 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001479 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1480 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1481 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1482 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1483 VK_MEMORY_INPUT_SHADER_READ_BIT |
1484 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1485 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001486 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001487
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001488 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001489 srRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001490 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter13dbde02015-08-28 16:47:15 -06001491 srRange.mipLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001492 srRange.baseArrayLayer = 0;
1493 srRange.arraySize = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001494
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001495 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001496 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001497 memory_barrier.outputMask = output_mask;
1498 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001499 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001500 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001501 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001502
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001503 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1504 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001505
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001506 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001507 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001508 memory_barrier.image = m_renderTargets[i]->image();
1509 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001510 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001511 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001512 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001513}
1514
Chia-I Wu08accc62015-07-07 11:50:03 +08001515void VkCommandBufferObj::BeginRenderPass(const VkRenderPassBeginInfo &info)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001516{
Chia-I Wube2b9172015-07-03 11:49:42 +08001517 vkCmdBeginRenderPass( handle(), &info, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001518}
1519
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001520void VkCommandBufferObj::EndRenderPass()
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001521{
Chia-I Wube2b9172015-07-03 11:49:42 +08001522 vkCmdEndRenderPass(handle());
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001523}
1524
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001525void VkCommandBufferObj::SetViewport(
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -06001526 uint32_t viewportCount,
1527 const VkViewport* pViewports)
1528{
1529 vkCmdSetViewport( handle(), viewportCount, pViewports);
1530}
1531
1532void VkCommandBufferObj::SetScissor(
1533 uint32_t scissorCount,
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001534 const VkRect2D* pScissors)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001535{
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -06001536 vkCmdSetScissor( handle(), scissorCount, pScissors);
Tony Barbour67e99152015-07-10 14:10:27 -06001537}
1538
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001539void VkCommandBufferObj::SetLineWidth(float lineWidth)
Tony Barbour67e99152015-07-10 14:10:27 -06001540{
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001541 vkCmdSetLineWidth( handle(), lineWidth);
Cody Northrop12365112015-08-17 11:10:49 -06001542}
1543
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001544void VkCommandBufferObj::SetDepthBias(
1545 float depthBias,
1546 float depthBiasClamp,
1547 float slopeScaledDepthBias)
Cody Northrop12365112015-08-17 11:10:49 -06001548{
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001549 vkCmdSetDepthBias( handle(), depthBias, depthBiasClamp, slopeScaledDepthBias);
Tony Barbour67e99152015-07-10 14:10:27 -06001550}
1551
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001552void VkCommandBufferObj::SetBlendConstants(
1553 const float blendConst[4])
Tony Barbour67e99152015-07-10 14:10:27 -06001554{
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001555 vkCmdSetBlendConstants( handle(), blendConst);
Tony Barbour67e99152015-07-10 14:10:27 -06001556}
1557
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001558void VkCommandBufferObj::SetDepthBounds(
1559 float minDepthBounds,
1560 float maxDepthBounds)
Tony Barbour67e99152015-07-10 14:10:27 -06001561{
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001562 vkCmdSetDepthBounds( handle(), minDepthBounds, maxDepthBounds);
Cody Northrop82485a82015-08-18 15:21:16 -06001563}
1564
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001565void VkCommandBufferObj::SetStencilReadMask(
1566 VkStencilFaceFlags faceMask,
1567 uint32_t stencilCompareMask)
Cody Northrop82485a82015-08-18 15:21:16 -06001568{
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -06001569 vkCmdSetStencilCompareMask( handle(), faceMask, stencilCompareMask);
1570}
1571
1572void VkCommandBufferObj::SetStencilWriteMask(
1573 VkStencilFaceFlags faceMask,
1574 uint32_t stencilWriteMask)
1575{
1576 vkCmdSetStencilWriteMask( handle(), faceMask, stencilWriteMask);
1577}
1578
1579void VkCommandBufferObj::SetStencilReference(
1580 VkStencilFaceFlags faceMask,
1581 uint32_t stencilReference)
1582{
1583 vkCmdSetStencilReference( handle(), faceMask, stencilReference);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001584}
1585
Tony Barbour6918cd52015-04-09 12:58:51 -06001586void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001587{
1588 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001589}
1590
Courtney Goeltzenleuchter08c26372015-09-23 12:31:50 -06001591void VkCommandBufferObj::DrawIndexed(uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001592{
Courtney Goeltzenleuchter08c26372015-09-23 12:31:50 -06001593 vkCmdDrawIndexed(handle(), indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001594}
1595
Courtney Goeltzenleuchter08c26372015-09-23 12:31:50 -06001596void VkCommandBufferObj::Draw(uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001597{
Courtney Goeltzenleuchter08c26372015-09-23 12:31:50 -06001598 vkCmdDraw(handle(), vertexCount, instanceCount, firstVertex, firstInstance);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001599}
1600
Tony Barbour6918cd52015-04-09 12:58:51 -06001601void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001602{
Tony Barbour67e99152015-07-10 14:10:27 -06001603 VkFence nullFence = { VK_NULL_HANDLE };
1604 QueueCommandBuffer(nullFence);
Tony Barbour09b7ac32015-04-08 10:11:29 -06001605}
1606
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001607void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001608{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001609 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001610
1611 // submit the command buffer to the universal queue
Chia-I Wube2b9172015-07-03 11:49:42 +08001612 err = vkQueueSubmit( m_device->m_queue, 1, &handle(), fence );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001613 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001614
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001615 err = vkQueueWaitIdle( m_device->m_queue );
1616 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001617
1618 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001619 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001620}
1621
Tony Barbour6918cd52015-04-09 12:58:51 -06001622void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001623{
Chia-I Wube2b9172015-07-03 11:49:42 +08001624 vkCmdBindPipeline( handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.handle() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001625}
1626
Tony Barbour6918cd52015-04-09 12:58:51 -06001627void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001628{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001629 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001630
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001631 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wube2b9172015-07-03 11:49:42 +08001632 vkCmdBindDescriptorSets(handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northropd4c1a502015-04-16 13:41:56 -06001633 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001634}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001635
Cody Northropd2ad0342015-08-05 11:15:02 -06001636void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, VkDeviceSize offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001637{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001638 vkCmdBindIndexBuffer(handle(), indexBuffer->handle(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001639}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001640
Tony Barbourd1c35722015-04-16 15:59:00 -06001641void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001642{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001643 vkCmdBindVertexBuffers(handle(), binding, 1, &vertexBuffer->handle(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001644}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001645
Tony Barbour6918cd52015-04-09 12:58:51 -06001646VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001647{
1648 m_initialized = false;
1649}
Tony Barbour6918cd52015-04-09 12:58:51 -06001650bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001651{
1652 return m_initialized;
1653}
1654
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001655VkImageView* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001656{
Chia-I Wu08accc62015-07-07 11:50:03 +08001657 return &m_attachmentBindInfo;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001658}
1659
Chia-I Wu08accc62015-07-07 11:50:03 +08001660void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height, VkFormat format)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001661{
Cody Northrop78e71932015-09-03 16:09:57 -06001662 VkImageCreateInfo image_info = {};
1663 VkImageViewCreateInfo view_info = {};
Tony Barbour1c45ce02015-03-27 17:03:18 -06001664
1665 m_device = device;
1666 m_initialized = true;
Chia-I Wu08accc62015-07-07 11:50:03 +08001667 m_depth_stencil_fmt = format;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001668
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001669 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001670 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001671 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-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;
1677 image_info.arraySize = 1;
1678 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001679 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchter660f0ca2015-09-10 14:14:11 -06001680 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001681 image_info.flags = 0;
Tony Barbour3d83f492015-07-28 10:23:02 -06001682 image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1683 image_info.queueFamilyCount = 0;
1684 image_info.pQueueFamilyIndices = NULL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001685 init(*m_device, image_info);
1686
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001687 view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001688 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001689 view_info.image = VK_NULL_HANDLE;
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001690 view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001691 view_info.subresourceRange.baseMipLevel = 0;
1692 view_info.subresourceRange.mipLevels = 1;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001693 view_info.subresourceRange.baseArrayLayer = 0;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001694 view_info.subresourceRange.arraySize = 1;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001695 view_info.flags = 0;
Tony Barbour3d83f492015-07-28 10:23:02 -06001696 view_info.format = m_depth_stencil_fmt;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001697 view_info.image = handle();
Cody Northrop78e71932015-09-03 16:09:57 -06001698 view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001699 m_imageView.init(*m_device, view_info);
Tony Barbour1c45ce02015-03-27 17:03:18 -06001700
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001701 m_attachmentBindInfo = m_imageView.handle();
Tony Barbour1c45ce02015-03-27 17:03:18 -06001702}