blob: 294d8de884759ba73c69f90dd5ce31bd21b76d84 [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,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600123 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600124 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 Goeltzenleuchterd971b612015-06-17 20:51:59 -0600152
153 /* Now register callback on device */
154 if (0) {
155 if (m_dbgCreateMsgCallback) {
156 err = m_dbgCreateMsgCallback(this->inst,
157 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
158 dbgFunction,
159 userData,
160 &m_devMsgCallback);
161 ASSERT_VK_SUCCESS(err);
162 }
163 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600164 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600165
Tony Barbour6918cd52015-04-09 12:58:51 -0600166 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600167}
168
Tony Barbour6918cd52015-04-09 12:58:51 -0600169void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600170{
Mike Stroyanb050c682015-04-17 12:36:38 -0600171 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
172 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
173 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
174 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
175 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
176 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600177
178 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600179 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600180 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600181 while (!m_renderTargets.empty()) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600182 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
Mike Stroyanb050c682015-04-17 12:36:38 -0600183 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
184 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600185 m_renderTargets.pop_back();
186 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600187
Tony Barbour1c45ce02015-03-27 17:03:18 -0600188 delete m_depthStencil;
189
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600190 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800191 delete m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600192 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600193}
194
Tony Barbour6918cd52015-04-09 12:58:51 -0600195void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600196{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600197 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600198
Tony Barbourd1c35722015-04-16 15:59:00 -0600199 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600200
201 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600202 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600203 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700204 raster.pointSize = 1.0;
205
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600206 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
207 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600208
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600209 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600210 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600211 blend.blendConst[0] = 1.0f;
212 blend.blendConst[1] = 1.0f;
213 blend.blendConst[2] = 1.0f;
214 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600215 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
216 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600217
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600218 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600219 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600220 depthStencil.minDepth = 0.f;
221 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700222 depthStencil.stencilFrontRef = 0;
223 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600224 depthStencil.stencilReadMask = 0xff;
225 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600226
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600227 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
228 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600229
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600230 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600231
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600232 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700233 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
234
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600235 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
236 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600237}
238
Tony Barbour6918cd52015-04-09 12:58:51 -0600239void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600240{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600241 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600242
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600243 VkViewport viewport;
244 VkRect scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600245
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600246 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600247 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700248 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700249 viewport.originX = 0;
250 viewport.originY = 0;
251 viewport.width = 1.f * width;
252 viewport.height = 1.f * height;
253 viewport.minDepth = 0.f;
254 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600255 scissor.extent.width = (int32_t) width;
256 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700257 scissor.offset.x = 0;
258 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700259 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700260 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700261
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600262 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
263 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600264 m_width = width;
265 m_height = height;
266}
267
Tony Barbour6918cd52015-04-09 12:58:51 -0600268void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600269{
270 InitViewport(m_width, m_height);
271}
Tony Barbour6918cd52015-04-09 12:58:51 -0600272void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600273{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700274 InitRenderTarget(1);
275}
276
Tony Barbour6918cd52015-04-09 12:58:51 -0600277void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700278{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600279 InitRenderTarget(targets, NULL);
280}
281
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600282void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600283{
284 InitRenderTarget(1, dsBinding);
285}
286
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600287void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600288{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600289 std::vector<VkAttachmentLoadOp> load_ops;
290 std::vector<VkAttachmentStoreOp> store_ops;
291 std::vector<VkClearColor> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600292
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600293 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800294
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700295 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600296 VkImageObj *img = new VkImageObj(m_device);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600297
298 VkFormatProperties props;
299 size_t size = sizeof(props);
300 VkResult err;
301
302 err = vkGetFormatInfo(m_device->obj(), m_render_target_fmt,
303 VK_FORMAT_INFO_TYPE_PROPERTIES,
304 &size, &props);
305 ASSERT_VK_SUCCESS(err);
306
307 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600308 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600309 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_LINEAR);
310 }
311 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600312 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600313 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL);
314 }
315 else {
316 FAIL() << "Neither Linear nor Optimal allowed for render target";
317 }
318
319 m_renderTargets.push_back(img);
Tony Barbourf52346d2015-01-16 14:27:35 -0700320 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600321 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chris Forbese182fe02015-06-15 09:32:35 +1200322 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 -0600323 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600324 clear_colors.push_back(m_clear_color);
Chia-I Wuecebf752014-12-05 10:45:15 +0800325 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600326
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700327 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600328 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600329 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600330 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700331 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600332 fb_info.pColorAttachments = m_colorBindings;
333 fb_info.pDepthStencilAttachment = dsBinding;
334 fb_info.sampleCount = 1;
335 fb_info.width = (uint32_t)m_width;
336 fb_info.height = (uint32_t)m_height;
337 fb_info.layers = 1;
338
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600339 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600340
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600341 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600342 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600343 rp_info.renderArea.extent.width = (uint32_t) m_width;
344 rp_info.renderArea.extent.height = (uint32_t) m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600345
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700346 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600347 rp_info.pColorFormats = &m_render_target_fmt;
348 rp_info.pColorLayouts = &m_colorBindings[0].layout;
349 rp_info.pColorLoadOps = &load_ops[0];
350 rp_info.pColorStoreOps = &store_ops[0];
351 rp_info.pColorLoadClearValues = &clear_colors[0];
352 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600353 if (dsBinding) {
354 rp_info.depthStencilLayout = dsBinding->layout;
355 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600356 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600357 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600358 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
359 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600360 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600361 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Tony Barbour51a05ce2015-06-03 11:40:51 -0600362 rp_info.sampleCount = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600363 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600364}
365
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600366
367
Tony Barbourd1c35722015-04-16 15:59:00 -0600368VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600369 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800370{
371 init();
372
373 props = gpu().properties();
374 queue_props = &gpu().queue_properties()[0];
375}
376
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600377VkDeviceObj::VkDeviceObj(uint32_t id,
378 VkPhysicalDevice obj,
379 std::vector<VkExtensionProperties> extensions) :
380 vk_testing::Device(obj), id(id)
381{
382 init(extensions);
383
384 props = gpu().properties();
385 queue_props = &gpu().queue_properties()[0];
386}
387
Tony Barbour6918cd52015-04-09 12:58:51 -0600388void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800389{
390 ASSERT_NE(true, graphics_queues().empty());
391 m_queue = graphics_queues()[0]->obj();
392}
393
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600394VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
395 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700396{
Tony Barboure2c58df2014-11-25 13:18:32 -0700397
398}
399
Tony Barbour6918cd52015-04-09 12:58:51 -0600400VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700401{
Chia-I Wu11078b02015-01-04 16:27:24 +0800402 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700403}
Tony Barbour82c39522014-12-04 14:33:33 -0700404
Tony Barbour6918cd52015-04-09 12:58:51 -0600405int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700406{
Chia-I Wu11078b02015-01-04 16:27:24 +0800407 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600408 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600409 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800410 tc.count = 1;
411 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700412
Chia-I Wu11078b02015-01-04 16:27:24 +0800413 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700414}
Tony Barbour82c39522014-12-04 14:33:33 -0700415
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600416int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700417{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600418 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800419 tc.type = type;
420 tc.count = 1;
421 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700422
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800423 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
424 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600425
Chia-I Wu11078b02015-01-04 16:27:24 +0800426 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700427}
Tony Barbour82c39522014-12-04 14:33:33 -0700428
Tony Barbour6918cd52015-04-09 12:58:51 -0600429int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700430{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600431 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600432 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800433 tc.count = 1;
434 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700435
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800436 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600437 tmp.sampler = sampler->obj();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800438 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700439
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800440 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
441 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700442
Chia-I Wu11078b02015-01-04 16:27:24 +0800443 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700444}
Chia-I Wu11078b02015-01-04 16:27:24 +0800445
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500446VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700447{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500448 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700449}
450
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600451VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700452{
Chia-I Wu11078b02015-01-04 16:27:24 +0800453 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700454}
Tony Barboure2c58df2014-11-25 13:18:32 -0700455
Tony Barbour6918cd52015-04-09 12:58:51 -0600456void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700457{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600458 // create VkDescriptorPool
459 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600460 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800461 pool.count = m_type_counts.size();
462 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600463 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700464
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600465 // create VkDescriptorSetLayout
466 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800467 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800468 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800469 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800470 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600471 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800472 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700473 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800474
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600475 // create VkDescriptorSetLayout
476 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600477 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800478 layout.count = bindings.size();
479 layout.pBinding = &bindings[0];
480
Chia-I Wu41126e52015-03-26 15:27:55 +0800481 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600482 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800483 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500484
485 // create VkPipelineLayout
486 VkPipelineLayoutCreateInfo pipeline_layout = {};
487 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
488 pipeline_layout.descriptorSetCount = layouts.size();
489 pipeline_layout.pSetLayouts = NULL;
490
491 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700492
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600493 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500494 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800495
Chia-I Wu41126e52015-03-26 15:27:55 +0800496 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800497 size_t imageSamplerCount = 0;
498 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
499 it != m_writes.end(); it++) {
500 it->destSet = m_set->obj();
501 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
502 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800503 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800504
505 // do the updates
Chia-I Wu11078b02015-01-04 16:27:24 +0800506 clear_sets(*m_set);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800507 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700508}
Tony Barboure2c58df2014-11-25 13:18:32 -0700509
Tony Barbour6918cd52015-04-09 12:58:51 -0600510VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800511{
512 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800513 m_descriptorInfo.imageView = VK_NULL_HANDLE;
514 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800515}
516
Tony Barbour6918cd52015-04-09 12:58:51 -0600517void VkImageObj::ImageMemoryBarrier(
518 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600519 VkImageAspect aspect,
520 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600521 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600522 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
523 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
524 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
525 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600526 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600527 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600528 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
529 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
530 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
531 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
532 VK_MEMORY_INPUT_SHADER_READ_BIT |
533 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
534 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
535 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600536 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600537{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600538 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
539 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600540 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
541 subresourceRange);
542
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600543 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600544
Tony Barbourd1c35722015-04-16 15:59:00 -0600545 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600546
547 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600548 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 -0600549}
550
Tony Barbour6918cd52015-04-09 12:58:51 -0600551void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600552 VkImageAspect aspect,
553 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600554{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600555 VkFlags output_mask, input_mask;
556 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600557 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600558 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
559 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
560 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600561 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600562 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600563 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600564 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
565 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
566 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
567 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
568 VK_MEMORY_INPUT_SHADER_READ_BIT |
569 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
570 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600571 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600572
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800573 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600574 return;
575 }
576
577 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600578 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600579 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
580 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600581 break;
582
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600583 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600584 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
585 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600586 break;
587
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600588 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600589 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
590 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600591 break;
592
Tony Barbourf20f87b2015-04-22 09:02:32 -0600593 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600594 default:
595 output_mask = all_cache_outputs;
596 input_mask = all_cache_inputs;
597 break;
598 }
599
600 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800601 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600602}
603
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600604void VkImageObj::SetLayout(VkImageAspect aspect,
605 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600606{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600607 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600608
609 if (image_layout == m_descriptorInfo.imageLayout) {
610 return;
611 }
612
Tony Barbour6918cd52015-04-09 12:58:51 -0600613 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600614
615 /* Build command buffer to set image layout in the driver */
616 err = cmd_buf.BeginCommandBuffer();
617 assert(!err);
618
619 SetLayout(&cmd_buf, aspect, image_layout);
620
621 err = cmd_buf.EndCommandBuffer();
622 assert(!err);
623
624 cmd_buf.QueueCommandBuffer();
625}
626
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600627bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600628{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600629 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600630 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600631 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600632
Tony Barbour579f7802015-04-03 15:11:43 -0600633 return true;
634}
635
Tony Barbour6918cd52015-04-09 12:58:51 -0600636void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600637 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600638 VkImageTiling requested_tiling,
639 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800640{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600641 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600642 VkFormatProperties image_fmt;
643 VkImageTiling tiling;
644 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600645 size_t size;
646
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800647 mipCount = 0;
648
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600649 uint32_t _w = w;
650 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800651 while( ( _w > 0 ) || ( _h > 0 ) )
652 {
653 _w >>= 1;
654 _h >>= 1;
655 mipCount++;
656 }
657
Tony Barbour579f7802015-04-03 15:11:43 -0600658 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600659 err = vkGetFormatInfo(m_device->obj(), fmt,
Tony Barbourd1c35722015-04-16 15:59:00 -0600660 VK_FORMAT_INFO_TYPE_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600661 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600662 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600663
Tony Barbourd1c35722015-04-16 15:59:00 -0600664 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600665 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600666 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600667 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600668 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600669 } else {
670 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
671 }
672 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600673 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600674 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600675 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600676 } else {
677 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
678 }
679
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600680 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600681 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800682 imageCreateInfo.format = fmt;
683 imageCreateInfo.extent.width = w;
684 imageCreateInfo.extent.height = h;
685 imageCreateInfo.mipLevels = mipCount;
686 imageCreateInfo.tiling = tiling;
687
688 imageCreateInfo.usage = usage;
689
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600690 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800691
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600692 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600693 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600694 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600695 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600696 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800697}
698
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600699VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800700{
701 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600702 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800703}
704
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600705VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800706{
707 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600708 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800709}
710
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600711VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600712{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600713 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600714 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600715 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600716
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600717 /* Build command buffer to copy staging texture to usable texture */
718 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600719 assert(!err);
720
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600721 /* TODO: Can we determine image aspect from image object? */
722 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600723 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600724
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600725 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600726 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600727
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600728 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600729 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600730 copy_region.srcSubresource.arraySlice = 0;
731 copy_region.srcSubresource.mipLevel = 0;
732 copy_region.srcOffset.x = 0;
733 copy_region.srcOffset.y = 0;
734 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600735 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600736 copy_region.destSubresource.arraySlice = 0;
737 copy_region.destSubresource.mipLevel = 0;
738 copy_region.destOffset.x = 0;
739 copy_region.destOffset.y = 0;
740 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600741 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600742
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600743 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600744 src_image.obj(), src_image.layout(),
745 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600746 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600747
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600748 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600749
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600750 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600751
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600752 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600753 assert(!err);
754
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600755 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600756
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600757 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600758}
759
Tony Barbour6918cd52015-04-09 12:58:51 -0600760VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
761 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700762{
763 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600764 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600765 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600766 void *data;
767 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600768 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600769 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600770
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600771 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600772 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600773
774 if (colors == NULL)
775 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700776
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800777 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700778
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600779 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600780 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600781 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600782 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600783 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600784 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600785 view.channels.r = VK_CHANNEL_SWIZZLE_R;
786 view.channels.g = VK_CHANNEL_SWIZZLE_G;
787 view.channels.b = VK_CHANNEL_SWIZZLE_B;
788 view.channels.a = VK_CHANNEL_SWIZZLE_A;
789 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600790 view.subresourceRange.baseMipLevel = 0;
791 view.subresourceRange.mipLevels = 1;
792 view.subresourceRange.baseArraySlice = 0;
793 view.subresourceRange.arraySize = 1;
794 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700795
Tony Barboure2c58df2014-11-25 13:18:32 -0700796 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600797 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700798
799 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800800 view.image = obj();
801 m_textureView.init(*m_device, view);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800802 m_descriptorInfo.imageView = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700803
Tony Barbour5dc515d2015-04-01 17:47:06 -0600804 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700805
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800806 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700807 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800808 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600809 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700810 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600811 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600812 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700813}
Tony Barbour82c39522014-12-04 14:33:33 -0700814
Tony Barbour6918cd52015-04-09 12:58:51 -0600815VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700816{
Tony Barboure2c58df2014-11-25 13:18:32 -0700817 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700818
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600819 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800820 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600821 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
822 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
823 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600824 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600825 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
826 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
827 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800828 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600829 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600830 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800831 samplerCreateInfo.minLod = 0.0;
832 samplerCreateInfo.maxLod = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600833 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700834
Chia-I Wue9864b52014-12-28 16:32:24 +0800835 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700836}
Tony Barboure2c58df2014-11-25 13:18:32 -0700837
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700838/*
839 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
840 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600841VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700842{
843 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700844 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700845
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800846 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700847}
848
Tony Barbour6918cd52015-04-09 12:58:51 -0600849VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600850{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600851 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600852 if (m_commandBuffer) {
853 delete m_commandBuffer;
854 }
855}
856
Tony Barbour6918cd52015-04-09 12:58:51 -0600857VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700858{
Tony Barboure2c58df2014-11-25 13:18:32 -0700859 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800860 m_commandBuffer = 0;
861
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800862 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700863 m_numVertices = constantCount;
864 m_stride = constantSize;
865
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600866 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800867 const size_t allocationSize = constantCount * constantSize;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600868 init(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700869
Chia-I Wua07fee62014-12-28 15:26:08 +0800870 void *pData = map();
871 memcpy(pData, data, allocationSize);
872 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700873
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800874 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600875 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600876 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800877 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600878 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800879 view_info.offset = 0;
880 view_info.range = allocationSize;
881 m_bufferView.init(*m_device, view_info);
882
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800883 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700884}
Tony Barbour82c39522014-12-04 14:33:33 -0700885
Tony Barbourd1c35722015-04-16 15:59:00 -0600886void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700887{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600888 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700889}
890
891
Tony Barbour6918cd52015-04-09 12:58:51 -0600892void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600893 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600894 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600895 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
896 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
897 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
898 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600899 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600900 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600901 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
902 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
903 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
904 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
905 VK_MEMORY_INPUT_SHADER_READ_BIT |
906 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
907 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
908 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700909{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600910 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700911
Tony Barbour38422802014-12-10 14:36:31 -0700912 if (!m_commandBuffer)
913 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600914 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700915
Tony Barbour6918cd52015-04-09 12:58:51 -0600916 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700917 }
918 else
919 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800920 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700921 }
922
Tony Barboure2c58df2014-11-25 13:18:32 -0700923 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600924 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600925 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600926 cmd_buf_info.pNext = NULL;
927 cmd_buf_info.flags = 0;
928
Jon Ashburnc4164b12014-12-31 17:10:47 -0700929 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600930 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700931
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600932 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000933 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600934 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700935
Tony Barbourd1c35722015-04-16 15:59:00 -0600936 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000937
938 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600939 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700940
941 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700942 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600943 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700944
Tony Barboure2c58df2014-11-25 13:18:32 -0700945 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600946 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700947 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600948 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
949 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700950}
951
Tony Barbour6918cd52015-04-09 12:58:51 -0600952VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
953 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700954{
955
956}
957
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600958void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700959{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600960 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700961
962 m_numVertices = numIndexes;
963 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700964 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600965 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700966 m_stride = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -0600967 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700968 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600969 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700970 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600971 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700972 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600973 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700974 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600975 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700976 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800977 default:
978 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600979 m_stride = 2;
980 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800981 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700982 }
983
Chia-I Wua07fee62014-12-28 15:26:08 +0800984 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600985 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
986 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700987
Chia-I Wua07fee62014-12-28 15:26:08 +0800988 void *pData = map();
989 memcpy(pData, data, allocationSize);
990 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700991
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800992 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600993 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600994 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800995 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600996 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700997 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800998 view_info.offset = 0;
999 view_info.range = allocationSize;
1000 m_bufferView.init(*m_device, view_info);
1001
Chia-I Wu9d00ed72015-05-25 16:27:55 +08001002 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001003}
1004
Tony Barbourd1c35722015-04-16 15:59:00 -06001005void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001006{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001007 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001008}
Tony Barboure2c58df2014-11-25 13:18:32 -07001009
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001010VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001011{
1012 return m_indexType;
1013}
1014
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001015VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001016{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001017 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001018 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001019 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001020 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -07001021 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001022 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001023
Tony Barboure2c58df2014-11-25 13:18:32 -07001024 return stageInfo;
1025}
1026
Tony Barbourd1c35722015-04-16 15:59:00 -06001027VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001028{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001029 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001030 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001031 VkShaderCreateInfo createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -07001032 size_t shader_len;
1033
1034 m_stage = stage;
1035 m_device = device;
1036
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001037 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001038 createInfo.pNext = NULL;
1039
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001040 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001041
1042 shader_len = strlen(shader_code);
1043 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1044 createInfo.pCode = malloc(createInfo.codeSize);
1045 createInfo.flags = 0;
1046
Tony Barbourd1c35722015-04-16 15:59:00 -06001047 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001048 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -07001049 ((uint32_t *) createInfo.pCode)[1] = 0;
1050 ((uint32_t *) createInfo.pCode)[2] = stage;
1051 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
1052
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001053 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001054
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001055 // Use Reference GLSL to SPV compiler
1056 framework->GLSLtoSPV(stage, shader_code, spv);
1057 createInfo.pCode = spv.data();
1058 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -07001059 createInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001060 }
Cody Northrop475663c2015-04-15 11:19:06 -06001061
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001062 err = init_try(*m_device, createInfo);
1063
Cody Northrop475663c2015-04-15 11:19:06 -06001064 assert(VK_SUCCESS == err);
Tony Barbourf325bf12014-12-03 15:59:38 -07001065}
Tony Barbour82c39522014-12-04 14:33:33 -07001066
Tony Barbour6918cd52015-04-09 12:58:51 -06001067VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001068{
Tony Barboure2c58df2014-11-25 13:18:32 -07001069 m_device = device;
1070 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
1071 m_vertexBufferCount = 0;
1072
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001073 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1074 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001075 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001076 m_ia_state.disableVertexReuse = VK_FALSE;
1077 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001078 m_ia_state.primitiveRestartIndex = 0;
1079
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001080 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001081 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001082 m_rs_state.depthClipEnable = VK_FALSE;
1083 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
1084 m_rs_state.programPointSize = VK_FALSE;
1085 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1086 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001087 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001088 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1089 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001090
Tony Barboure2c58df2014-11-25 13:18:32 -07001091 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001092 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001093 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001094 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1095 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001096
Tony Barbourf52346d2015-01-16 14:27:35 -07001097 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001098 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1099 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001100 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1101 m_ms_state.samples = 1;
1102 m_ms_state.minSampleShading = 0;
1103 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001104
Tony Barbourd81b8882015-05-15 09:37:57 -06001105 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
1106 m_vp_state.pNext = &m_ms_state;
1107 m_vp_state.viewportCount = 1;
1108 m_vp_state.depthMode = VK_DEPTH_MODE_ZERO_TO_ONE;
1109 m_vp_state.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1110
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001111 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -06001112 m_ds_state.pNext = &m_vp_state,
Tony Barbourd1c35722015-04-16 15:59:00 -06001113 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001114 m_ds_state.depthTestEnable = VK_FALSE;
1115 m_ds_state.depthWriteEnable = VK_FALSE;
1116 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001117 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001118 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1119 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1120 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001121 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001122 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001123 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001124
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001125 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001126 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001127 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001128 att.channelWriteMask = 0xf;
1129 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001130
1131};
1132
Tony Barbour6918cd52015-04-09 12:58:51 -06001133void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001134{
1135 m_shaderObjs.push_back(shader);
1136}
1137
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001138void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001139{
1140 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1141 m_vi_state.attributeCount = count;
1142}
1143
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001144void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001145{
1146 m_vi_state.pVertexBindingDescriptions = vi_binding;
1147 m_vi_state.bindingCount = count;
1148}
1149
Tony Barbour6918cd52015-04-09 12:58:51 -06001150void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001151{
1152 m_vertexBufferObjs.push_back(vertexDataBuffer);
1153 m_vertexBufferBindings.push_back(binding);
1154 m_vertexBufferCount++;
1155}
1156
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001157void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001158{
Tony Barbourf52346d2015-01-16 14:27:35 -07001159 if (binding+1 > m_colorAttachments.size())
1160 {
1161 m_colorAttachments.resize(binding+1);
1162 }
1163 m_colorAttachments[binding] = *att;
1164}
1165
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001166void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001167{
1168 m_ds_state.format = ds_state->format;
1169 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1170 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1171 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001172 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001173 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1174 m_ds_state.back = ds_state->back;
1175 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001176}
1177
Chris Forbes95292b12015-05-25 11:13:26 +12001178VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001179{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001180 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001181 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001182
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001183 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001184
1185 for (int i=0; i<m_shaderObjs.size(); i++)
1186 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001187 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001188 shaderCreateInfo->pNext = head_ptr;
1189 head_ptr = shaderCreateInfo;
1190 }
1191
1192 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1193 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001194 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001195 m_vi_state.pNext = head_ptr;
1196 head_ptr = &m_vi_state;
1197 }
1198
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001199 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1200 info.pNext = head_ptr;
1201 info.flags = 0;
1202 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001203
Tony Barbourf52346d2015-01-16 14:27:35 -07001204 m_cb_state.attachmentCount = m_colorAttachments.size();
1205 m_cb_state.pAttachments = &m_colorAttachments[0];
1206
Chris Forbes95292b12015-05-25 11:13:26 +12001207 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001208}
Chia-I Wu2648d092014-12-29 14:24:14 +08001209
Tony Barbourd1c35722015-04-16 15:59:00 -06001210vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001211{
Tony Barbourd1c35722015-04-16 15:59:00 -06001212 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001213 if (this->mem_refs_.size()) {
1214 mems.reserve(this->mem_refs_.size());
1215 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1216 mems.push_back(this->mem_refs_[i]);
1217 }
1218
1219 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001220}
1221
Tony Barbour6918cd52015-04-09 12:58:51 -06001222VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001223 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001224{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001225 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001226}
Tony Barbour471338d2014-12-10 17:28:39 -07001227
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001228VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001229{
Chia-I Wud28343c2014-12-28 15:12:48 +08001230 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001231}
Tony Barbour471338d2014-12-10 17:28:39 -07001232
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001233VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001234{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001235 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001236 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001237}
1238
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001239VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001240{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001241 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001242 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001243}
1244
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001245VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001246{
1247 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001248 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001249}
1250
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001251VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001252{
Chia-I Wud28343c2014-12-28 15:12:48 +08001253 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001254 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001255}
1256
Tony Barbourd1c35722015-04-16 15:59:00 -06001257void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001258{
Tony Barbourd1c35722015-04-16 15:59:00 -06001259 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001260}
1261
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001262void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001263 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001264{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001265 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001266 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001267 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001268 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1269 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1270 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001271 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001272 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001273
1274 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001275 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001276 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001277 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001278 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001279 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001280 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001281
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001282 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001283 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001284 memory_barrier.outputMask = output_mask;
1285 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001286 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001287 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001288 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001289
Tony Barbourd1c35722015-04-16 15:59:00 -06001290 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001291
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001292 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001293 memory_barrier.image = m_renderTargets[i]->image();
1294 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001295 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001296 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001297
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001298 vkCmdClearColorImage(obj(),
1299 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001300 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001301
Tony Barbour30cc9e82014-12-17 11:53:55 -07001302 }
1303
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001304 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001305 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001306 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001307 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001308 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001309 dsRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001310 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001311 dsRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001312
1313 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001314
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001315 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001316 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001317 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001318 memory_barrier.subresourceRange = dsRange;
1319
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
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001322 vkCmdClearDepthStencil(obj(),
1323 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001324 depth_clear_color, stencil_clear_color,
1325 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001326
1327 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001328 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001329 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001330 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001331 memory_barrier.subresourceRange = dsRange;
Tony Barbourd1c35722015-04-16 15:59:00 -06001332 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001333 }
1334}
1335
Tony Barbour6918cd52015-04-09 12:58:51 -06001336void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001337{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001338 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001339 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001340 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001341 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1342 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1343 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001344 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001345 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001346 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001347 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1348 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1349 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1350 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1351 VK_MEMORY_INPUT_SHADER_READ_BIT |
1352 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1353 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001354 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001355
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001356 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001357 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001358 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001359 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001360 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001361 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001362
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001363 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001364 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001365 memory_barrier.outputMask = output_mask;
1366 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001367 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001368 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001369 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001370
Tony Barbourd1c35722015-04-16 15:59:00 -06001371 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001372
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001373 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001374 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001375 memory_barrier.image = m_renderTargets[i]->image();
1376 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001377 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001378 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001379 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001380}
1381
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001382void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001383{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001384 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001385 renderpass,
1386 framebuffer,
1387 };
1388
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001389 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001390}
1391
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001392void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001393{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001394 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001395}
1396
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001397void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001398{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001399 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001400}
1401
Tony Barbour6918cd52015-04-09 12:58:51 -06001402void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001403{
1404 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001405}
1406
Tony Barbour6918cd52015-04-09 12:58:51 -06001407void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001408{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001409 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001410}
1411
Tony Barbour6918cd52015-04-09 12:58:51 -06001412void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001413{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001414 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001415}
1416
Tony Barbour6918cd52015-04-09 12:58:51 -06001417void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001418{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001419 QueueCommandBuffer(NULL);
1420}
1421
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001422void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001423{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001424 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001425
1426 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001427 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1428 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001429
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001430 err = vkQueueWaitIdle( m_device->m_queue );
1431 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001432
1433 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001434 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001435}
1436
Tony Barbour6918cd52015-04-09 12:58:51 -06001437void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001438{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001439 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001440}
1441
Tony Barbour6918cd52015-04-09 12:58:51 -06001442void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001443{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001444 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001445
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001446 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001447 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropd4c1a502015-04-16 13:41:56 -06001448 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001449}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001450
Tony Barbour6918cd52015-04-09 12:58:51 -06001451void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001452{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001453 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001454}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001455
Tony Barbourd1c35722015-04-16 15:59:00 -06001456void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001457{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001458 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001459}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001460
Tony Barbour6918cd52015-04-09 12:58:51 -06001461VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001462{
1463 m_initialized = false;
1464}
Tony Barbour6918cd52015-04-09 12:58:51 -06001465bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001466{
1467 return m_initialized;
1468}
1469
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001470VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001471{
1472 return &m_depthStencilBindInfo;
1473}
1474
Tony Barbour6918cd52015-04-09 12:58:51 -06001475void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001476{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001477 VkImageCreateInfo image_info;
1478 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001479
1480 m_device = device;
1481 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001482 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001483
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001484 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001485 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001486 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001487 image_info.format = m_depth_stencil_fmt;
1488 image_info.extent.width = width;
1489 image_info.extent.height = height;
1490 image_info.extent.depth = 1;
1491 image_info.mipLevels = 1;
1492 image_info.arraySize = 1;
1493 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001494 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001495 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001496 image_info.flags = 0;
1497 init(*m_device, image_info);
1498
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001499 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001500 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001501 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001502 view_info.mipLevel = 0;
1503 view_info.baseArraySlice = 0;
1504 view_info.arraySize = 1;
1505 view_info.flags = 0;
1506 view_info.image = obj();
1507 m_depthStencilView.init(*m_device, view_info);
1508
1509 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001510 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001511}