blob: a658bbee4f1f9be160b5afa8f53564d510067c46 [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"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Jon Ashburn83a64252015-04-15 11:31:12 -060030#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
31
Tony Barbour6918cd52015-04-09 12:58:51 -060032VkRenderFramework::VkRenderFramework() :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060033 m_cmdBuffer( VK_NULL_HANDLE ),
Tony Barbourf77fd652015-04-09 11:07:02 -060034 m_renderPass(VK_NULL_HANDLE),
35 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060036 m_stateRaster( VK_NULL_HANDLE ),
37 m_colorBlend( VK_NULL_HANDLE ),
38 m_stateViewport( VK_NULL_HANDLE ),
39 m_stateDepthStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060040 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070041 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060042 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
43 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Chris Forbese182fe02015-06-15 09:32:35 +120044 m_clear_via_load_op( false ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070045 m_depth_clear_color( 1.0 ),
46 m_stencil_clear_color( 0 )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060047{
Tony Barbour1c45ce02015-03-27 17:03:18 -060048
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070049 // clear the back buffer to dark grey
50 m_clear_color.color.rawColor[0] = 64;
51 m_clear_color.color.rawColor[1] = 64;
52 m_clear_color.color.rawColor[2] = 64;
53 m_clear_color.color.rawColor[3] = 0;
54 m_clear_color.useRawValue = true;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060055}
56
Tony Barbour6918cd52015-04-09 12:58:51 -060057VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060058{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060059
60}
61
Tony Barbour6918cd52015-04-09 12:58:51 -060062void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060063{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060064 std::vector<const char*> instance_extension_names;
65 std::vector<const char*> device_extension_names;
66 InitFramework(instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060067}
68
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060069void VkRenderFramework::InitFramework(
70 std::vector<const char *> instance_extension_names,
71 std::vector<const char *> device_extension_names,
72 PFN_vkDbgMsgCallback dbgFunction,
73 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060074{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060075 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060076 std::vector<VkExtensionProperties> instance_extensions;
77 std::vector<VkExtensionProperties> device_extensions;
78 uint32_t extCount = 0;
79 size_t extSize = sizeof(extCount);
80 VkResult U_ASSERT_ONLY err;
81 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
82 assert(!err);
83
84 VkExtensionProperties extProp;
85 extSize = sizeof(VkExtensionProperties);
86 bool32_t extFound;
87
88 for (uint32_t i = 0; i < instance_extension_names.size(); i++) {
89 extFound = 0;
90 for (uint32_t j = 0; j < extCount; j++) {
91 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, j, &extSize, &extProp);
92 assert(!err);
93 if (!strcmp(instance_extension_names[i], extProp.name)) {
94 instance_extensions.push_back(extProp);
95 extFound = 1;
96 }
97 }
98 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << instance_extension_names[i] << " which is necessary to pass this test";
99 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600100 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -0600101 instInfo.pNext = NULL;
102 instInfo.pAppInfo = &app_info;
103 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600104 instInfo.extensionCount = instance_extensions.size();
105 instInfo.pEnabledExtensions = (instance_extensions.size()) ? &instance_extensions[0] : NULL;
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);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700114 ASSERT_GE(this->gpu_count, 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,
123 &m_msgCallback);
124 ASSERT_VK_SUCCESS(err);
125 }
Tony Barbour15524c32015-04-29 17:34:29 -0600126 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600127
128 extSize = sizeof(extCount);
129 err = vkGetPhysicalDeviceExtensionInfo(objs[0], VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
130 assert(!err);
131
132 extSize = sizeof(VkExtensionProperties);
133 for (uint32_t i = 0; i < device_extension_names.size(); i++) {
134 extFound = 0;
135 for (uint32_t j = 0; j < extCount; j++) {
136 err = vkGetPhysicalDeviceExtensionInfo(objs[0], VK_EXTENSION_INFO_TYPE_PROPERTIES, j, &extSize, &extProp);
137 assert(!err);
138 if (!strcmp(device_extension_names[i], extProp.name)) {
139 device_extensions.push_back(extProp);
140 extFound = 1;
141 }
142 }
143 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << device_extension_names[i] << " which is necessary to pass this test";
144 }
145 /* TODO: Testing unenabled extension */
146
147// PFN_vkDbgSetObjectName obj_name = (PFN_vkDbgSetObjectName) vkGetInstanceProcAddr(this->inst,
148// "vkDbgSetObjectName");
149// ASSERT_NE(obj_name, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
150// obj_name()
151 m_device = new VkDeviceObj(0, objs[0], device_extensions);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600152 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600153
Tony Barbour6918cd52015-04-09 12:58:51 -0600154 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600155}
156
Tony Barbour6918cd52015-04-09 12:58:51 -0600157void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600158{
Mike Stroyanb050c682015-04-17 12:36:38 -0600159 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
160 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
161 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
162 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
163 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
164 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600165
166 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600167 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600168 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600169 while (!m_renderTargets.empty()) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600170 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
Mike Stroyanb050c682015-04-17 12:36:38 -0600171 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
172 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600173 m_renderTargets.pop_back();
174 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600175
Tony Barbour1c45ce02015-03-27 17:03:18 -0600176 delete m_depthStencil;
177
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600178 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800179 delete m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600180 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600181}
182
Tony Barbour6918cd52015-04-09 12:58:51 -0600183void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600184{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600185 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600186
Tony Barbourd1c35722015-04-16 15:59:00 -0600187 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600188
189 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600190 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600191 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700192 raster.pointSize = 1.0;
193
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600194 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
195 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600196
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600197 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600198 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600199 blend.blendConst[0] = 1.0f;
200 blend.blendConst[1] = 1.0f;
201 blend.blendConst[2] = 1.0f;
202 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600203 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
204 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600205
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600206 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600207 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600208 depthStencil.minDepth = 0.f;
209 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700210 depthStencil.stencilFrontRef = 0;
211 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600212 depthStencil.stencilReadMask = 0xff;
213 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600214
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600215 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
216 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600217
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600218 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600219
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600220 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700221 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
222
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600223 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
224 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600225}
226
Tony Barbour6918cd52015-04-09 12:58:51 -0600227void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600228{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600229 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600230
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600231 VkViewport viewport;
232 VkRect scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600233
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600234 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600235 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700236 viewportCreate.viewportAndScissorCount = 1;
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;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600243 scissor.extent.width = (int32_t) width;
244 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700245 scissor.offset.x = 0;
246 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700247 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700248 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700249
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600250 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
251 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600252 m_width = width;
253 m_height = height;
254}
255
Tony Barbour6918cd52015-04-09 12:58:51 -0600256void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600257{
258 InitViewport(m_width, m_height);
259}
Tony Barbour6918cd52015-04-09 12:58:51 -0600260void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600261{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700262 InitRenderTarget(1);
263}
264
Tony Barbour6918cd52015-04-09 12:58:51 -0600265void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700266{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600267 InitRenderTarget(targets, NULL);
268}
269
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600270void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600271{
272 InitRenderTarget(1, dsBinding);
273}
274
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600275void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600276{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600277 std::vector<VkAttachmentLoadOp> load_ops;
278 std::vector<VkAttachmentStoreOp> store_ops;
279 std::vector<VkClearColor> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600280
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600281 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800282
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700283 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600284 VkImageObj *img = new VkImageObj(m_device);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600285
286 VkFormatProperties props;
287 size_t size = sizeof(props);
288 VkResult err;
289
290 err = vkGetFormatInfo(m_device->obj(), m_render_target_fmt,
291 VK_FORMAT_INFO_TYPE_PROPERTIES,
292 &size, &props);
293 ASSERT_VK_SUCCESS(err);
294
295 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600296 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600297 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_LINEAR);
298 }
299 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600300 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600301 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL);
302 }
303 else {
304 FAIL() << "Neither Linear nor Optimal allowed for render target";
305 }
306
307 m_renderTargets.push_back(img);
Tony Barbourf52346d2015-01-16 14:27:35 -0700308 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600309 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chris Forbese182fe02015-06-15 09:32:35 +1200310 load_ops.push_back(m_clear_via_load_op ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600311 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600312 clear_colors.push_back(m_clear_color);
Chia-I Wuecebf752014-12-05 10:45:15 +0800313 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600314
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700315 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600316 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600317 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600318 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700319 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600320 fb_info.pColorAttachments = m_colorBindings;
321 fb_info.pDepthStencilAttachment = dsBinding;
322 fb_info.sampleCount = 1;
323 fb_info.width = (uint32_t)m_width;
324 fb_info.height = (uint32_t)m_height;
325 fb_info.layers = 1;
326
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600327 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600328
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600329 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600330 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600331 rp_info.renderArea.extent.width = (uint32_t) m_width;
332 rp_info.renderArea.extent.height = (uint32_t) m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600333
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700334 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600335 rp_info.pColorFormats = &m_render_target_fmt;
336 rp_info.pColorLayouts = &m_colorBindings[0].layout;
337 rp_info.pColorLoadOps = &load_ops[0];
338 rp_info.pColorStoreOps = &store_ops[0];
339 rp_info.pColorLoadClearValues = &clear_colors[0];
340 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600341 if (dsBinding) {
342 rp_info.depthStencilLayout = dsBinding->layout;
343 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600344 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600345 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600346 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
347 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600348 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600349 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Tony Barbour51a05ce2015-06-03 11:40:51 -0600350 rp_info.sampleCount = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600351 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600352}
353
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600354
355
Tony Barbourd1c35722015-04-16 15:59:00 -0600356VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600357 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800358{
359 init();
360
361 props = gpu().properties();
362 queue_props = &gpu().queue_properties()[0];
363}
364
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600365VkDeviceObj::VkDeviceObj(uint32_t id,
366 VkPhysicalDevice obj,
367 std::vector<VkExtensionProperties> extensions) :
368 vk_testing::Device(obj), id(id)
369{
370 init(extensions);
371
372 props = gpu().properties();
373 queue_props = &gpu().queue_properties()[0];
374}
375
Tony Barbour6918cd52015-04-09 12:58:51 -0600376void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800377{
378 ASSERT_NE(true, graphics_queues().empty());
379 m_queue = graphics_queues()[0]->obj();
380}
381
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600382VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
383 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700384{
Tony Barboure2c58df2014-11-25 13:18:32 -0700385
386}
387
Tony Barbour6918cd52015-04-09 12:58:51 -0600388VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700389{
Chia-I Wu11078b02015-01-04 16:27:24 +0800390 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700391}
Tony Barbour82c39522014-12-04 14:33:33 -0700392
Tony Barbour6918cd52015-04-09 12:58:51 -0600393int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700394{
Chia-I Wu11078b02015-01-04 16:27:24 +0800395 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600396 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600397 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800398 tc.count = 1;
399 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700400
Chia-I Wu11078b02015-01-04 16:27:24 +0800401 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700402}
Tony Barbour82c39522014-12-04 14:33:33 -0700403
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600404int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700405{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600406 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800407 tc.type = type;
408 tc.count = 1;
409 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700410
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800411 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
412 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600413
Chia-I Wu11078b02015-01-04 16:27:24 +0800414 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700415}
Tony Barbour82c39522014-12-04 14:33:33 -0700416
Tony Barbour6918cd52015-04-09 12:58:51 -0600417int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700418{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600419 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600420 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800421 tc.count = 1;
422 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700423
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800424 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600425 tmp.sampler = sampler->obj();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800426 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700427
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800428 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
429 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700430
Chia-I Wu11078b02015-01-04 16:27:24 +0800431 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700432}
Chia-I Wu11078b02015-01-04 16:27:24 +0800433
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500434VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700435{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500436 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700437}
438
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600439VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700440{
Chia-I Wu11078b02015-01-04 16:27:24 +0800441 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700442}
Tony Barboure2c58df2014-11-25 13:18:32 -0700443
Tony Barbour6918cd52015-04-09 12:58:51 -0600444void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700445{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600446 // create VkDescriptorPool
447 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600448 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800449 pool.count = m_type_counts.size();
450 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600451 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700452
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600453 // create VkDescriptorSetLayout
454 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800455 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800456 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800457 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800458 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600459 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800460 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700461 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800462
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600463 // create VkDescriptorSetLayout
464 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600465 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800466 layout.count = bindings.size();
467 layout.pBinding = &bindings[0];
468
Chia-I Wu41126e52015-03-26 15:27:55 +0800469 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600470 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800471 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500472
473 // create VkPipelineLayout
474 VkPipelineLayoutCreateInfo pipeline_layout = {};
475 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
476 pipeline_layout.descriptorSetCount = layouts.size();
477 pipeline_layout.pSetLayouts = NULL;
478
479 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700480
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600481 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500482 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800483
Chia-I Wu41126e52015-03-26 15:27:55 +0800484 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800485 size_t imageSamplerCount = 0;
486 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
487 it != m_writes.end(); it++) {
488 it->destSet = m_set->obj();
489 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
490 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800491 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800492
493 // do the updates
Chia-I Wu11078b02015-01-04 16:27:24 +0800494 clear_sets(*m_set);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800495 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700496}
Tony Barboure2c58df2014-11-25 13:18:32 -0700497
Tony Barbour6918cd52015-04-09 12:58:51 -0600498VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800499{
500 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800501 m_descriptorInfo.imageView = VK_NULL_HANDLE;
502 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800503}
504
Tony Barbour6918cd52015-04-09 12:58:51 -0600505void VkImageObj::ImageMemoryBarrier(
506 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600507 VkImageAspect aspect,
508 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600509 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600510 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
511 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
512 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
513 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600514 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600515 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600516 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
517 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
518 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
519 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
520 VK_MEMORY_INPUT_SHADER_READ_BIT |
521 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
522 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
523 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600524 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600525{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600526 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
527 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600528 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
529 subresourceRange);
530
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600531 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600532
Tony Barbourd1c35722015-04-16 15:59:00 -0600533 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600534
535 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600536 vkCmdPipelineBarrier(cmd_buf->obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, pipe_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600537}
538
Tony Barbour6918cd52015-04-09 12:58:51 -0600539void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600540 VkImageAspect aspect,
541 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600542{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600543 VkFlags output_mask, input_mask;
544 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600545 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600546 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
547 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
548 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600549 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600550 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600551 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600552 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
553 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
554 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
555 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
556 VK_MEMORY_INPUT_SHADER_READ_BIT |
557 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
558 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600559 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600560
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800561 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600562 return;
563 }
564
565 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600566 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600567 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
568 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600569 break;
570
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600571 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600572 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
573 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600574 break;
575
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600576 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600577 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
578 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600579 break;
580
Tony Barbourf20f87b2015-04-22 09:02:32 -0600581 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600582 default:
583 output_mask = all_cache_outputs;
584 input_mask = all_cache_inputs;
585 break;
586 }
587
588 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800589 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600590}
591
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600592void VkImageObj::SetLayout(VkImageAspect aspect,
593 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600594{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600595 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600596
597 if (image_layout == m_descriptorInfo.imageLayout) {
598 return;
599 }
600
Tony Barbour6918cd52015-04-09 12:58:51 -0600601 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600602
603 /* Build command buffer to set image layout in the driver */
604 err = cmd_buf.BeginCommandBuffer();
605 assert(!err);
606
607 SetLayout(&cmd_buf, aspect, image_layout);
608
609 err = cmd_buf.EndCommandBuffer();
610 assert(!err);
611
612 cmd_buf.QueueCommandBuffer();
613}
614
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600615bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600616{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600617 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600618 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600619 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600620
Tony Barbour579f7802015-04-03 15:11:43 -0600621 return true;
622}
623
Tony Barbour6918cd52015-04-09 12:58:51 -0600624void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600625 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600626 VkImageTiling requested_tiling,
627 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800628{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600629 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600630 VkFormatProperties image_fmt;
631 VkImageTiling tiling;
632 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600633 size_t size;
634
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800635 mipCount = 0;
636
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600637 uint32_t _w = w;
638 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800639 while( ( _w > 0 ) || ( _h > 0 ) )
640 {
641 _w >>= 1;
642 _h >>= 1;
643 mipCount++;
644 }
645
Tony Barbour579f7802015-04-03 15:11:43 -0600646 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600647 err = vkGetFormatInfo(m_device->obj(), fmt,
Tony Barbourd1c35722015-04-16 15:59:00 -0600648 VK_FORMAT_INFO_TYPE_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600649 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600650 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600651
Tony Barbourd1c35722015-04-16 15:59:00 -0600652 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600653 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600654 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600655 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600656 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600657 } else {
658 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
659 }
660 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600661 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600662 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600663 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600664 } else {
665 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
666 }
667
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600668 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600669 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800670 imageCreateInfo.format = fmt;
671 imageCreateInfo.extent.width = w;
672 imageCreateInfo.extent.height = h;
673 imageCreateInfo.mipLevels = mipCount;
674 imageCreateInfo.tiling = tiling;
675
676 imageCreateInfo.usage = usage;
677
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600678 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800679
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600680 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600681 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600682 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600683 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600684 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800685}
686
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600687VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800688{
689 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600690 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800691}
692
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600693VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800694{
695 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600696 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800697}
698
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600699VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600700{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600701 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600702 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600703 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600704
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600705 /* Build command buffer to copy staging texture to usable texture */
706 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600707 assert(!err);
708
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600709 /* TODO: Can we determine image aspect from image object? */
710 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600711 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600712
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600713 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600714 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600715
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600716 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600717 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600718 copy_region.srcSubresource.arraySlice = 0;
719 copy_region.srcSubresource.mipLevel = 0;
720 copy_region.srcOffset.x = 0;
721 copy_region.srcOffset.y = 0;
722 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600723 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600724 copy_region.destSubresource.arraySlice = 0;
725 copy_region.destSubresource.mipLevel = 0;
726 copy_region.destOffset.x = 0;
727 copy_region.destOffset.y = 0;
728 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600729 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600730
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600731 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600732 src_image.obj(), src_image.layout(),
733 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600734 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600735
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600736 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600737
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600738 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600739
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600740 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600741 assert(!err);
742
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600743 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600744
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600745 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600746}
747
Tony Barbour6918cd52015-04-09 12:58:51 -0600748VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
749 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700750{
751 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600752 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600753 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600754 void *data;
755 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600756 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600757 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600758
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600759 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600760 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600761
762 if (colors == NULL)
763 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700764
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800765 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700766
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600767 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600768 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600769 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600770 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600771 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600772 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600773 view.channels.r = VK_CHANNEL_SWIZZLE_R;
774 view.channels.g = VK_CHANNEL_SWIZZLE_G;
775 view.channels.b = VK_CHANNEL_SWIZZLE_B;
776 view.channels.a = VK_CHANNEL_SWIZZLE_A;
777 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600778 view.subresourceRange.baseMipLevel = 0;
779 view.subresourceRange.mipLevels = 1;
780 view.subresourceRange.baseArraySlice = 0;
781 view.subresourceRange.arraySize = 1;
782 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700783
Tony Barboure2c58df2014-11-25 13:18:32 -0700784 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600785 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700786
787 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800788 view.image = obj();
789 m_textureView.init(*m_device, view);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800790 m_descriptorInfo.imageView = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700791
Tony Barbour5dc515d2015-04-01 17:47:06 -0600792 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700793
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800794 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700795 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800796 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600797 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700798 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600799 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600800 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700801}
Tony Barbour82c39522014-12-04 14:33:33 -0700802
Tony Barbour6918cd52015-04-09 12:58:51 -0600803VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700804{
Tony Barboure2c58df2014-11-25 13:18:32 -0700805 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700806
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600807 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800808 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600809 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
810 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
811 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600812 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600813 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
814 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
815 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800816 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600817 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600818 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800819 samplerCreateInfo.minLod = 0.0;
820 samplerCreateInfo.maxLod = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600821 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700822
Chia-I Wue9864b52014-12-28 16:32:24 +0800823 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700824}
Tony Barboure2c58df2014-11-25 13:18:32 -0700825
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700826/*
827 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
828 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600829VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700830{
831 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700832 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700833
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800834 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700835}
836
Tony Barbour6918cd52015-04-09 12:58:51 -0600837VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600838{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600839 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600840 if (m_commandBuffer) {
841 delete m_commandBuffer;
842 }
843}
844
Tony Barbour6918cd52015-04-09 12:58:51 -0600845VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700846{
Tony Barboure2c58df2014-11-25 13:18:32 -0700847 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800848 m_commandBuffer = 0;
849
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800850 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700851 m_numVertices = constantCount;
852 m_stride = constantSize;
853
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600854 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800855 const size_t allocationSize = constantCount * constantSize;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600856 init(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700857
Chia-I Wua07fee62014-12-28 15:26:08 +0800858 void *pData = map();
859 memcpy(pData, data, allocationSize);
860 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700861
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800862 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600863 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600864 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800865 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600866 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800867 view_info.offset = 0;
868 view_info.range = allocationSize;
869 m_bufferView.init(*m_device, view_info);
870
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800871 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700872}
Tony Barbour82c39522014-12-04 14:33:33 -0700873
Tony Barbourd1c35722015-04-16 15:59:00 -0600874void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700875{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600876 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700877}
878
879
Tony Barbour6918cd52015-04-09 12:58:51 -0600880void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600881 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600882 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600883 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
884 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
885 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
886 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600887 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600888 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600889 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
890 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
891 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
892 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
893 VK_MEMORY_INPUT_SHADER_READ_BIT |
894 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
895 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
896 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700897{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600898 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700899
Tony Barbour38422802014-12-10 14:36:31 -0700900 if (!m_commandBuffer)
901 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600902 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700903
Tony Barbour6918cd52015-04-09 12:58:51 -0600904 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700905 }
906 else
907 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800908 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700909 }
910
Tony Barboure2c58df2014-11-25 13:18:32 -0700911 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600912 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600913 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600914 cmd_buf_info.pNext = NULL;
915 cmd_buf_info.flags = 0;
916
Jon Ashburnc4164b12014-12-31 17:10:47 -0700917 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600918 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700919
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600920 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000921 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600922 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700923
Tony Barbourd1c35722015-04-16 15:59:00 -0600924 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000925
926 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600927 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700928
929 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700930 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600931 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700932
Tony Barboure2c58df2014-11-25 13:18:32 -0700933 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600934 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700935 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600936 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
937 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700938}
939
Tony Barbour6918cd52015-04-09 12:58:51 -0600940VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
941 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700942{
943
944}
945
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600946void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700947{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600948 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700949
950 m_numVertices = numIndexes;
951 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700952 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600953 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700954 m_stride = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -0600955 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700956 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600957 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700958 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600959 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700960 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600961 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700962 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600963 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700964 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800965 default:
966 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600967 m_stride = 2;
968 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800969 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700970 }
971
Chia-I Wua07fee62014-12-28 15:26:08 +0800972 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600973 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
974 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700975
Chia-I Wua07fee62014-12-28 15:26:08 +0800976 void *pData = map();
977 memcpy(pData, data, allocationSize);
978 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700979
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800980 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600981 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600982 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800983 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600984 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700985 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800986 view_info.offset = 0;
987 view_info.range = allocationSize;
988 m_bufferView.init(*m_device, view_info);
989
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800990 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700991}
992
Tony Barbourd1c35722015-04-16 15:59:00 -0600993void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700994{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600995 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700996}
Tony Barboure2c58df2014-11-25 13:18:32 -0700997
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600998VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -0700999{
1000 return m_indexType;
1001}
1002
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001003VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001004{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001005 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001006 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001007 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001008 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -07001009 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001010 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001011
Tony Barboure2c58df2014-11-25 13:18:32 -07001012 return stageInfo;
1013}
1014
Tony Barbourd1c35722015-04-16 15:59:00 -06001015VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001016{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001017 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001018 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001019 VkShaderCreateInfo createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -07001020 size_t shader_len;
1021
1022 m_stage = stage;
1023 m_device = device;
1024
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001025 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001026 createInfo.pNext = NULL;
1027
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001028 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001029
1030 shader_len = strlen(shader_code);
1031 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1032 createInfo.pCode = malloc(createInfo.codeSize);
1033 createInfo.flags = 0;
1034
Tony Barbourd1c35722015-04-16 15:59:00 -06001035 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001036 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -07001037 ((uint32_t *) createInfo.pCode)[1] = 0;
1038 ((uint32_t *) createInfo.pCode)[2] = stage;
1039 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
1040
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001041 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001042
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001043 // Use Reference GLSL to SPV compiler
1044 framework->GLSLtoSPV(stage, shader_code, spv);
1045 createInfo.pCode = spv.data();
1046 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -07001047 createInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001048 }
Cody Northrop475663c2015-04-15 11:19:06 -06001049
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001050 err = init_try(*m_device, createInfo);
1051
Cody Northrop475663c2015-04-15 11:19:06 -06001052 assert(VK_SUCCESS == err);
Tony Barbourf325bf12014-12-03 15:59:38 -07001053}
Tony Barbour82c39522014-12-04 14:33:33 -07001054
Tony Barbour6918cd52015-04-09 12:58:51 -06001055VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001056{
Tony Barboure2c58df2014-11-25 13:18:32 -07001057 m_device = device;
1058 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
1059 m_vertexBufferCount = 0;
1060
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001061 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1062 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001063 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001064 m_ia_state.disableVertexReuse = VK_FALSE;
1065 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001066 m_ia_state.primitiveRestartIndex = 0;
1067
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001068 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001069 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001070 m_rs_state.depthClipEnable = VK_FALSE;
1071 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
1072 m_rs_state.programPointSize = VK_FALSE;
1073 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1074 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001075 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001076 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1077 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001078
Tony Barboure2c58df2014-11-25 13:18:32 -07001079 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001080 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001081 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001082 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1083 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001084
Tony Barbourf52346d2015-01-16 14:27:35 -07001085 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001086 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1087 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001088 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1089 m_ms_state.samples = 1;
1090 m_ms_state.minSampleShading = 0;
1091 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001092
Tony Barbourd81b8882015-05-15 09:37:57 -06001093 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
1094 m_vp_state.pNext = &m_ms_state;
1095 m_vp_state.viewportCount = 1;
1096 m_vp_state.depthMode = VK_DEPTH_MODE_ZERO_TO_ONE;
1097 m_vp_state.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1098
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001099 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -06001100 m_ds_state.pNext = &m_vp_state,
Tony Barbourd1c35722015-04-16 15:59:00 -06001101 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001102 m_ds_state.depthTestEnable = VK_FALSE;
1103 m_ds_state.depthWriteEnable = VK_FALSE;
1104 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001105 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001106 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1107 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1108 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001109 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001110 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001111 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001112
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001113 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001114 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001115 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001116 att.channelWriteMask = 0xf;
1117 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001118
1119};
1120
Tony Barbour6918cd52015-04-09 12:58:51 -06001121void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001122{
1123 m_shaderObjs.push_back(shader);
1124}
1125
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001126void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001127{
1128 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1129 m_vi_state.attributeCount = count;
1130}
1131
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001132void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001133{
1134 m_vi_state.pVertexBindingDescriptions = vi_binding;
1135 m_vi_state.bindingCount = count;
1136}
1137
Tony Barbour6918cd52015-04-09 12:58:51 -06001138void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001139{
1140 m_vertexBufferObjs.push_back(vertexDataBuffer);
1141 m_vertexBufferBindings.push_back(binding);
1142 m_vertexBufferCount++;
1143}
1144
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001145void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001146{
Tony Barbourf52346d2015-01-16 14:27:35 -07001147 if (binding+1 > m_colorAttachments.size())
1148 {
1149 m_colorAttachments.resize(binding+1);
1150 }
1151 m_colorAttachments[binding] = *att;
1152}
1153
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001154void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001155{
1156 m_ds_state.format = ds_state->format;
1157 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1158 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1159 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001160 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001161 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1162 m_ds_state.back = ds_state->back;
1163 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001164}
1165
Chris Forbes95292b12015-05-25 11:13:26 +12001166VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001167{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001168 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001169 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001170
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001171 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001172
1173 for (int i=0; i<m_shaderObjs.size(); i++)
1174 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001175 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001176 shaderCreateInfo->pNext = head_ptr;
1177 head_ptr = shaderCreateInfo;
1178 }
1179
1180 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1181 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001182 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001183 m_vi_state.pNext = head_ptr;
1184 head_ptr = &m_vi_state;
1185 }
1186
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001187 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1188 info.pNext = head_ptr;
1189 info.flags = 0;
1190 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001191
Tony Barbourf52346d2015-01-16 14:27:35 -07001192 m_cb_state.attachmentCount = m_colorAttachments.size();
1193 m_cb_state.pAttachments = &m_colorAttachments[0];
1194
Chris Forbes95292b12015-05-25 11:13:26 +12001195 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001196}
Chia-I Wu2648d092014-12-29 14:24:14 +08001197
Tony Barbourd1c35722015-04-16 15:59:00 -06001198vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001199{
Tony Barbourd1c35722015-04-16 15:59:00 -06001200 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001201 if (this->mem_refs_.size()) {
1202 mems.reserve(this->mem_refs_.size());
1203 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1204 mems.push_back(this->mem_refs_[i]);
1205 }
1206
1207 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001208}
1209
Tony Barbour6918cd52015-04-09 12:58:51 -06001210VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001211 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001212{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001213 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001214}
Tony Barbour471338d2014-12-10 17:28:39 -07001215
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001216VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001217{
Chia-I Wud28343c2014-12-28 15:12:48 +08001218 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001219}
Tony Barbour471338d2014-12-10 17:28:39 -07001220
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001221VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001222{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001223 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001224 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001225}
1226
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001227VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001228{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001229 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001230 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001231}
1232
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001233VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001234{
1235 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001236 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001237}
1238
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001239VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001240{
Chia-I Wud28343c2014-12-28 15:12:48 +08001241 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001242 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001243}
1244
Tony Barbourd1c35722015-04-16 15:59:00 -06001245void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001246{
Tony Barbourd1c35722015-04-16 15:59:00 -06001247 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001248}
1249
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001250void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001251 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001252{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001253 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001254 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001255 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001256 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1257 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1258 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001259 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001260 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001261
1262 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001263 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001264 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001265 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001266 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001267 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001268 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001269
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001270 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001271 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001272 memory_barrier.outputMask = output_mask;
1273 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001274 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001275 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001276 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001277
Tony Barbourd1c35722015-04-16 15:59:00 -06001278 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001279
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001280 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001281 memory_barrier.image = m_renderTargets[i]->image();
1282 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001283 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001284 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001285
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001286 vkCmdClearColorImage(obj(),
1287 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001288 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001289
Tony Barbour30cc9e82014-12-17 11:53:55 -07001290 }
1291
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001292 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001293 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001294 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001295 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001296 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001297 dsRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001298 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001299 dsRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001300
1301 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001302
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001303 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001304 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001305 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001306 memory_barrier.subresourceRange = dsRange;
1307
Tony Barbourd1c35722015-04-16 15:59:00 -06001308 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001309
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001310 vkCmdClearDepthStencil(obj(),
1311 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001312 depth_clear_color, stencil_clear_color,
1313 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001314
1315 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001316 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001317 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001318 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001319 memory_barrier.subresourceRange = dsRange;
Tony Barbourd1c35722015-04-16 15:59:00 -06001320 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001321 }
1322}
1323
Tony Barbour6918cd52015-04-09 12:58:51 -06001324void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001325{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001326 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001327 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001328 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001329 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1330 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1331 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001332 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001333 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001334 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001335 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1336 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1337 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1338 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1339 VK_MEMORY_INPUT_SHADER_READ_BIT |
1340 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1341 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001342 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001343
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001344 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001345 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001346 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001347 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001348 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001349 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001350
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001351 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001352 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001353 memory_barrier.outputMask = output_mask;
1354 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001355 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001356 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001357 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001358
Tony Barbourd1c35722015-04-16 15:59:00 -06001359 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001360
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001361 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001362 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001363 memory_barrier.image = m_renderTargets[i]->image();
1364 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001365 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001366 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001367 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001368}
1369
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001370void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001371{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001372 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001373 renderpass,
1374 framebuffer,
1375 };
1376
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001377 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001378}
1379
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001380void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001381{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001382 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001383}
1384
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001385void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001386{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001387 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001388}
1389
Tony Barbour6918cd52015-04-09 12:58:51 -06001390void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001391{
1392 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001393}
1394
Tony Barbour6918cd52015-04-09 12:58:51 -06001395void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001396{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001397 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001398}
1399
Tony Barbour6918cd52015-04-09 12:58:51 -06001400void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001401{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001402 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001403}
1404
Tony Barbour6918cd52015-04-09 12:58:51 -06001405void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001406{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001407 QueueCommandBuffer(NULL);
1408}
1409
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001410void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001411{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001412 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001413
1414 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001415 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1416 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001417
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001418 err = vkQueueWaitIdle( m_device->m_queue );
1419 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001420
1421 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001422 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001423}
1424
Tony Barbour6918cd52015-04-09 12:58:51 -06001425void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001426{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001427 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001428}
1429
Tony Barbour6918cd52015-04-09 12:58:51 -06001430void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001431{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001432 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001433
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001434 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001435 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropd4c1a502015-04-16 13:41:56 -06001436 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001437}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001438
Tony Barbour6918cd52015-04-09 12:58:51 -06001439void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001440{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001441 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001442}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001443
Tony Barbourd1c35722015-04-16 15:59:00 -06001444void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001445{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001446 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001447}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001448
Tony Barbour6918cd52015-04-09 12:58:51 -06001449VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001450{
1451 m_initialized = false;
1452}
Tony Barbour6918cd52015-04-09 12:58:51 -06001453bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001454{
1455 return m_initialized;
1456}
1457
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001458VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001459{
1460 return &m_depthStencilBindInfo;
1461}
1462
Tony Barbour6918cd52015-04-09 12:58:51 -06001463void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001464{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001465 VkImageCreateInfo image_info;
1466 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001467
1468 m_device = device;
1469 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001470 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001471
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001472 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001473 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001474 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001475 image_info.format = m_depth_stencil_fmt;
1476 image_info.extent.width = width;
1477 image_info.extent.height = height;
1478 image_info.extent.depth = 1;
1479 image_info.mipLevels = 1;
1480 image_info.arraySize = 1;
1481 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001482 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001483 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001484 image_info.flags = 0;
1485 init(*m_device, image_info);
1486
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001487 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001488 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001489 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001490 view_info.mipLevel = 0;
1491 view_info.baseArraySlice = 0;
1492 view_info.arraySize = 1;
1493 view_info.flags = 0;
1494 view_info.image = obj();
1495 m_depthStencilView.init(*m_device, view_info);
1496
1497 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001498 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001499}