blob: 96c6efc58427ad581272fa035bdc69b194204b5c [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 ),
Courtney Goeltzenleuchterb6c9aca2015-06-08 16:19:37 -060046 m_stencil_clear_color( 0 ),
47 m_depthStencil( NULL ),
48 m_dbgCreateMsgCallback( VK_NULL_HANDLE ),
49 m_dbgDestroyMsgCallback( VK_NULL_HANDLE ),
50 m_globalMsgCallback( VK_NULL_HANDLE ),
51 m_devMsgCallback( VK_NULL_HANDLE )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060052{
Tony Barbour1c45ce02015-03-27 17:03:18 -060053
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070054 // clear the back buffer to dark grey
55 m_clear_color.color.rawColor[0] = 64;
56 m_clear_color.color.rawColor[1] = 64;
57 m_clear_color.color.rawColor[2] = 64;
58 m_clear_color.color.rawColor[3] = 0;
59 m_clear_color.useRawValue = true;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060060}
61
Tony Barbour6918cd52015-04-09 12:58:51 -060062VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060063{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060064
65}
66
Tony Barbour6918cd52015-04-09 12:58:51 -060067void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060068{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060069 std::vector<const char*> instance_extension_names;
70 std::vector<const char*> device_extension_names;
71 InitFramework(instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060072}
73
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060074void VkRenderFramework::InitFramework(
75 std::vector<const char *> instance_extension_names,
76 std::vector<const char *> device_extension_names,
77 PFN_vkDbgMsgCallback dbgFunction,
78 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060079{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060080 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060081 std::vector<VkExtensionProperties> instance_extensions;
82 std::vector<VkExtensionProperties> device_extensions;
83 uint32_t extCount = 0;
84 size_t extSize = sizeof(extCount);
85 VkResult U_ASSERT_ONLY err;
86 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
87 assert(!err);
88
89 VkExtensionProperties extProp;
90 extSize = sizeof(VkExtensionProperties);
91 bool32_t extFound;
92
93 for (uint32_t i = 0; i < instance_extension_names.size(); i++) {
94 extFound = 0;
95 for (uint32_t j = 0; j < extCount; j++) {
96 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, j, &extSize, &extProp);
97 assert(!err);
98 if (!strcmp(instance_extension_names[i], extProp.name)) {
99 instance_extensions.push_back(extProp);
100 extFound = 1;
101 }
102 }
103 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << instance_extension_names[i] << " which is necessary to pass this test";
104 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600105 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -0600106 instInfo.pNext = NULL;
107 instInfo.pAppInfo = &app_info;
108 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600109 instInfo.extensionCount = instance_extensions.size();
110 instInfo.pEnabledExtensions = (instance_extensions.size()) ? &instance_extensions[0] : NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600111 err = vkCreateInstance(&instInfo, &this->inst);
112 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600113
Jon Ashburn83a64252015-04-15 11:31:12 -0600114 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
115 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
116 ASSERT_VK_SUCCESS(err);
117 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600118 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700119 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600120 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600121 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
122 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
123 if (m_dbgCreateMsgCallback) {
124 err = m_dbgCreateMsgCallback(this->inst,
125 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
126 dbgFunction,
127 userData,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600128 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600129 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600130
131 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
132 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600133 }
Tony Barbour15524c32015-04-29 17:34:29 -0600134 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600135
136 extSize = sizeof(extCount);
137 err = vkGetPhysicalDeviceExtensionInfo(objs[0], VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
138 assert(!err);
139
140 extSize = sizeof(VkExtensionProperties);
141 for (uint32_t i = 0; i < device_extension_names.size(); i++) {
142 extFound = 0;
143 for (uint32_t j = 0; j < extCount; j++) {
144 err = vkGetPhysicalDeviceExtensionInfo(objs[0], VK_EXTENSION_INFO_TYPE_PROPERTIES, j, &extSize, &extProp);
145 assert(!err);
146 if (!strcmp(device_extension_names[i], extProp.name)) {
147 device_extensions.push_back(extProp);
148 extFound = 1;
149 }
150 }
151 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << device_extension_names[i] << " which is necessary to pass this test";
152 }
153 /* TODO: Testing unenabled extension */
154
155// PFN_vkDbgSetObjectName obj_name = (PFN_vkDbgSetObjectName) vkGetInstanceProcAddr(this->inst,
156// "vkDbgSetObjectName");
157// ASSERT_NE(obj_name, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
158// obj_name()
159 m_device = new VkDeviceObj(0, objs[0], device_extensions);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600160
161 /* Now register callback on device */
162 if (0) {
163 if (m_dbgCreateMsgCallback) {
164 err = m_dbgCreateMsgCallback(this->inst,
165 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
166 dbgFunction,
167 userData,
168 &m_devMsgCallback);
169 ASSERT_VK_SUCCESS(err);
170 }
171 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600172 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600173
Tony Barbour6918cd52015-04-09 12:58:51 -0600174 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600175}
176
Tony Barbour6918cd52015-04-09 12:58:51 -0600177void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600178{
Mike Stroyanb050c682015-04-17 12:36:38 -0600179 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
180 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
181 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
182 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
183 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
184 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600185
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600186 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
187 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
188
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600189 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600190 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600191 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600192 while (!m_renderTargets.empty()) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600193 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
Mike Stroyanb050c682015-04-17 12:36:38 -0600194 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
195 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600196 m_renderTargets.pop_back();
197 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600198
Tony Barbour1c45ce02015-03-27 17:03:18 -0600199 delete m_depthStencil;
200
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600201 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800202 delete m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600203 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600204}
205
Tony Barbour6918cd52015-04-09 12:58:51 -0600206void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600207{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600208 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600209
Tony Barbourd1c35722015-04-16 15:59:00 -0600210 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600211
212 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600213 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600214 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700215
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600216 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
217 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600218
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600219 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600220 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600221 blend.blendConst[0] = 1.0f;
222 blend.blendConst[1] = 1.0f;
223 blend.blendConst[2] = 1.0f;
224 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600225 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
226 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600227
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600228 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600229 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski365feea2015-06-12 11:14:17 -0600230 depthStencil.minDepthBounds = 0.f;
231 depthStencil.maxDepthBounds = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700232 depthStencil.stencilFrontRef = 0;
233 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600234 depthStencil.stencilReadMask = 0xff;
235 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600236
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600237 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
238 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600239
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600240 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600241
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600242 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700243 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
244
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600245 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
246 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600247}
248
Tony Barbour6918cd52015-04-09 12:58:51 -0600249void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600250{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600251 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600252
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600253 VkViewport viewport;
254 VkRect scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600255
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600256 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600257 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700258 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700259 viewport.originX = 0;
260 viewport.originY = 0;
261 viewport.width = 1.f * width;
262 viewport.height = 1.f * height;
263 viewport.minDepth = 0.f;
264 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600265 scissor.extent.width = (int32_t) width;
266 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700267 scissor.offset.x = 0;
268 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700269 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700270 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700271
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600272 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
273 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600274 m_width = width;
275 m_height = height;
276}
277
Tony Barbour6918cd52015-04-09 12:58:51 -0600278void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600279{
280 InitViewport(m_width, m_height);
281}
Tony Barbour6918cd52015-04-09 12:58:51 -0600282void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600283{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700284 InitRenderTarget(1);
285}
286
Tony Barbour6918cd52015-04-09 12:58:51 -0600287void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700288{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600289 InitRenderTarget(targets, NULL);
290}
291
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600292void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600293{
294 InitRenderTarget(1, dsBinding);
295}
296
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600297void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600298{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600299 std::vector<VkAttachmentLoadOp> load_ops;
300 std::vector<VkAttachmentStoreOp> store_ops;
301 std::vector<VkClearColor> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600302
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600303 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800304
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700305 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600306 VkImageObj *img = new VkImageObj(m_device);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600307
308 VkFormatProperties props;
309 size_t size = sizeof(props);
310 VkResult err;
311
312 err = vkGetFormatInfo(m_device->obj(), m_render_target_fmt,
313 VK_FORMAT_INFO_TYPE_PROPERTIES,
314 &size, &props);
315 ASSERT_VK_SUCCESS(err);
316
317 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600318 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600319 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_LINEAR);
320 }
321 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600322 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600323 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL);
324 }
325 else {
326 FAIL() << "Neither Linear nor Optimal allowed for render target";
327 }
328
329 m_renderTargets.push_back(img);
Tony Barbourf52346d2015-01-16 14:27:35 -0700330 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600331 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chris Forbese182fe02015-06-15 09:32:35 +1200332 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 -0600333 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600334 clear_colors.push_back(m_clear_color);
Chia-I Wuecebf752014-12-05 10:45:15 +0800335 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600336
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700337 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600338 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600339 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600340 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700341 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600342 fb_info.pColorAttachments = m_colorBindings;
343 fb_info.pDepthStencilAttachment = dsBinding;
344 fb_info.sampleCount = 1;
345 fb_info.width = (uint32_t)m_width;
346 fb_info.height = (uint32_t)m_height;
347 fb_info.layers = 1;
348
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600349 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600350
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600351 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600352 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600353 rp_info.renderArea.extent.width = (uint32_t) m_width;
354 rp_info.renderArea.extent.height = (uint32_t) m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600355
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700356 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600357 rp_info.pColorFormats = &m_render_target_fmt;
358 rp_info.pColorLayouts = &m_colorBindings[0].layout;
359 rp_info.pColorLoadOps = &load_ops[0];
360 rp_info.pColorStoreOps = &store_ops[0];
361 rp_info.pColorLoadClearValues = &clear_colors[0];
362 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600363 if (dsBinding) {
364 rp_info.depthStencilLayout = dsBinding->layout;
365 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600366 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600367 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600368 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
369 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600370 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600371 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Tony Barbour51a05ce2015-06-03 11:40:51 -0600372 rp_info.sampleCount = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600373 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600374}
375
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600376
377
Tony Barbourd1c35722015-04-16 15:59:00 -0600378VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600379 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800380{
381 init();
382
383 props = gpu().properties();
384 queue_props = &gpu().queue_properties()[0];
385}
386
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600387VkDeviceObj::VkDeviceObj(uint32_t id,
388 VkPhysicalDevice obj,
389 std::vector<VkExtensionProperties> extensions) :
390 vk_testing::Device(obj), id(id)
391{
392 init(extensions);
393
394 props = gpu().properties();
395 queue_props = &gpu().queue_properties()[0];
396}
397
Tony Barbour6918cd52015-04-09 12:58:51 -0600398void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800399{
400 ASSERT_NE(true, graphics_queues().empty());
401 m_queue = graphics_queues()[0]->obj();
402}
403
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600404VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
405 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700406{
Tony Barboure2c58df2014-11-25 13:18:32 -0700407
408}
409
Tony Barbour6918cd52015-04-09 12:58:51 -0600410VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700411{
Chia-I Wu11078b02015-01-04 16:27:24 +0800412 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700413}
Tony Barbour82c39522014-12-04 14:33:33 -0700414
Tony Barbour6918cd52015-04-09 12:58:51 -0600415int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700416{
Chia-I Wu11078b02015-01-04 16:27:24 +0800417 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600418 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600419 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800420 tc.count = 1;
421 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700422
Chia-I Wu11078b02015-01-04 16:27:24 +0800423 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700424}
Tony Barbour82c39522014-12-04 14:33:33 -0700425
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600426int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700427{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600428 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800429 tc.type = type;
430 tc.count = 1;
431 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700432
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800433 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
434 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600435
Chia-I Wu11078b02015-01-04 16:27:24 +0800436 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700437}
Tony Barbour82c39522014-12-04 14:33:33 -0700438
Tony Barbour6918cd52015-04-09 12:58:51 -0600439int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700440{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600441 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600442 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800443 tc.count = 1;
444 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700445
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800446 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600447 tmp.sampler = sampler->obj();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800448 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700449
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800450 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
451 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700452
Chia-I Wu11078b02015-01-04 16:27:24 +0800453 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700454}
Chia-I Wu11078b02015-01-04 16:27:24 +0800455
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500456VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700457{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500458 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700459}
460
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600461VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700462{
Chia-I Wu11078b02015-01-04 16:27:24 +0800463 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700464}
Tony Barboure2c58df2014-11-25 13:18:32 -0700465
Tony Barbour6918cd52015-04-09 12:58:51 -0600466void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700467{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600468 // create VkDescriptorPool
469 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600470 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800471 pool.count = m_type_counts.size();
472 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600473 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700474
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600475 // create VkDescriptorSetLayout
476 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800477 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800478 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800479 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800480 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600481 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800482 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700483 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800484
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600485 // create VkDescriptorSetLayout
486 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600487 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800488 layout.count = bindings.size();
489 layout.pBinding = &bindings[0];
490
Chia-I Wu41126e52015-03-26 15:27:55 +0800491 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600492 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800493 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500494
495 // create VkPipelineLayout
496 VkPipelineLayoutCreateInfo pipeline_layout = {};
497 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
498 pipeline_layout.descriptorSetCount = layouts.size();
499 pipeline_layout.pSetLayouts = NULL;
500
501 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700502
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600503 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500504 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800505
Chia-I Wu41126e52015-03-26 15:27:55 +0800506 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800507 size_t imageSamplerCount = 0;
508 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
509 it != m_writes.end(); it++) {
510 it->destSet = m_set->obj();
511 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
512 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800513 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800514
515 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800516 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700517}
Tony Barboure2c58df2014-11-25 13:18:32 -0700518
Tony Barbour6918cd52015-04-09 12:58:51 -0600519VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800520{
521 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800522 m_descriptorInfo.imageView = VK_NULL_HANDLE;
523 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800524}
525
Tony Barbour6918cd52015-04-09 12:58:51 -0600526void VkImageObj::ImageMemoryBarrier(
527 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600528 VkImageAspect aspect,
529 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600530 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600531 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
532 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
533 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
534 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600535 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600536 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600537 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
538 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
539 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
540 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
541 VK_MEMORY_INPUT_SHADER_READ_BIT |
542 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
543 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
544 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600545 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600546{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600547 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
548 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600549 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
550 subresourceRange);
551
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600552 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600553
Tony Barbourd1c35722015-04-16 15:59:00 -0600554 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600555
556 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600557 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 -0600558}
559
Tony Barbour6918cd52015-04-09 12:58:51 -0600560void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600561 VkImageAspect aspect,
562 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600563{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600564 VkFlags output_mask, input_mask;
565 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600566 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600567 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
568 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
569 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600570 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600571 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600572 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600573 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
574 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
575 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
576 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
577 VK_MEMORY_INPUT_SHADER_READ_BIT |
578 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
579 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600580 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600581
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800582 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600583 return;
584 }
585
586 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600587 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600588 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
589 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600590 break;
591
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600592 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600593 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
594 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600595 break;
596
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600597 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600598 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
599 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600600 break;
601
Tony Barbourf20f87b2015-04-22 09:02:32 -0600602 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600603 default:
604 output_mask = all_cache_outputs;
605 input_mask = all_cache_inputs;
606 break;
607 }
608
609 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800610 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600611}
612
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600613void VkImageObj::SetLayout(VkImageAspect aspect,
614 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600615{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600616 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600617
618 if (image_layout == m_descriptorInfo.imageLayout) {
619 return;
620 }
621
Tony Barbour6918cd52015-04-09 12:58:51 -0600622 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600623
624 /* Build command buffer to set image layout in the driver */
625 err = cmd_buf.BeginCommandBuffer();
626 assert(!err);
627
628 SetLayout(&cmd_buf, aspect, image_layout);
629
630 err = cmd_buf.EndCommandBuffer();
631 assert(!err);
632
633 cmd_buf.QueueCommandBuffer();
634}
635
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600636bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600637{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600638 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600639 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600640 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600641
Tony Barbour579f7802015-04-03 15:11:43 -0600642 return true;
643}
644
Tony Barbour6918cd52015-04-09 12:58:51 -0600645void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600646 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600647 VkImageTiling requested_tiling,
648 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800649{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600650 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600651 VkFormatProperties image_fmt;
652 VkImageTiling tiling;
653 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600654 size_t size;
655
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800656 mipCount = 0;
657
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600658 uint32_t _w = w;
659 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800660 while( ( _w > 0 ) || ( _h > 0 ) )
661 {
662 _w >>= 1;
663 _h >>= 1;
664 mipCount++;
665 }
666
Tony Barbour579f7802015-04-03 15:11:43 -0600667 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600668 err = vkGetFormatInfo(m_device->obj(), fmt,
Tony Barbourd1c35722015-04-16 15:59:00 -0600669 VK_FORMAT_INFO_TYPE_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600670 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600671 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600672
Tony Barbourd1c35722015-04-16 15:59:00 -0600673 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600674 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 if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600677 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600678 } else {
679 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
680 }
681 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600682 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600683 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600684 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600685 } else {
686 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
687 }
688
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600689 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600690 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800691 imageCreateInfo.format = fmt;
692 imageCreateInfo.extent.width = w;
693 imageCreateInfo.extent.height = h;
694 imageCreateInfo.mipLevels = mipCount;
695 imageCreateInfo.tiling = tiling;
696
697 imageCreateInfo.usage = usage;
698
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600699 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800700
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600701 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600702 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600703 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600704 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600705 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800706}
707
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600708VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800709{
710 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600711 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800712}
713
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600714VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800715{
716 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600717 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800718}
719
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600720VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600721{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600722 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600723 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600724 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600725
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600726 /* Build command buffer to copy staging texture to usable texture */
727 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600728 assert(!err);
729
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600730 /* TODO: Can we determine image aspect from image object? */
731 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600732 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600733
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600734 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600735 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600736
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600737 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600738 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600739 copy_region.srcSubresource.arraySlice = 0;
740 copy_region.srcSubresource.mipLevel = 0;
741 copy_region.srcOffset.x = 0;
742 copy_region.srcOffset.y = 0;
743 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600744 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600745 copy_region.destSubresource.arraySlice = 0;
746 copy_region.destSubresource.mipLevel = 0;
747 copy_region.destOffset.x = 0;
748 copy_region.destOffset.y = 0;
749 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600750 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600751
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600752 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600753 src_image.obj(), src_image.layout(),
754 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600755 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600756
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600757 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600758
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600759 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600760
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600761 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600762 assert(!err);
763
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600764 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600765
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600766 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600767}
768
Tony Barbour6918cd52015-04-09 12:58:51 -0600769VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
770 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700771{
772 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600773 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600774 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600775 void *data;
776 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600777 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600778 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600779
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600780 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600781 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600782
783 if (colors == NULL)
784 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700785
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800786 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700787
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600788 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600789 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600790 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600791 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600792 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600793 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600794 view.channels.r = VK_CHANNEL_SWIZZLE_R;
795 view.channels.g = VK_CHANNEL_SWIZZLE_G;
796 view.channels.b = VK_CHANNEL_SWIZZLE_B;
797 view.channels.a = VK_CHANNEL_SWIZZLE_A;
798 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600799 view.subresourceRange.baseMipLevel = 0;
800 view.subresourceRange.mipLevels = 1;
801 view.subresourceRange.baseArraySlice = 0;
802 view.subresourceRange.arraySize = 1;
803 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700804
Tony Barboure2c58df2014-11-25 13:18:32 -0700805 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600806 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700807
808 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800809 view.image = obj();
810 m_textureView.init(*m_device, view);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800811 m_descriptorInfo.imageView = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700812
Tony Barbour5dc515d2015-04-01 17:47:06 -0600813 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700814
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800815 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700816 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800817 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600818 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700819 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600820 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600821 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700822}
Tony Barbour82c39522014-12-04 14:33:33 -0700823
Tony Barbour6918cd52015-04-09 12:58:51 -0600824VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700825{
Tony Barboure2c58df2014-11-25 13:18:32 -0700826 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700827
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600828 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800829 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600830 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
831 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
832 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600833 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600834 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
835 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
836 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800837 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600838 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600839 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800840 samplerCreateInfo.minLod = 0.0;
841 samplerCreateInfo.maxLod = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600842 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700843
Chia-I Wue9864b52014-12-28 16:32:24 +0800844 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700845}
Tony Barboure2c58df2014-11-25 13:18:32 -0700846
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700847/*
848 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
849 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600850VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700851{
852 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700853 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700854
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800855 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700856}
857
Tony Barbour6918cd52015-04-09 12:58:51 -0600858VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600859{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600860 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600861 if (m_commandBuffer) {
862 delete m_commandBuffer;
863 }
864}
865
Tony Barbour6918cd52015-04-09 12:58:51 -0600866VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700867{
Tony Barboure2c58df2014-11-25 13:18:32 -0700868 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800869 m_commandBuffer = 0;
870
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800871 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700872 m_numVertices = constantCount;
873 m_stride = constantSize;
874
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600875 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800876 const size_t allocationSize = constantCount * constantSize;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600877 init(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700878
Chia-I Wua07fee62014-12-28 15:26:08 +0800879 void *pData = map();
880 memcpy(pData, data, allocationSize);
881 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700882
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800883 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600884 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600885 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800886 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600887 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800888 view_info.offset = 0;
889 view_info.range = allocationSize;
890 m_bufferView.init(*m_device, view_info);
891
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800892 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700893}
Tony Barbour82c39522014-12-04 14:33:33 -0700894
Tony Barbourd1c35722015-04-16 15:59:00 -0600895void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700896{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600897 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700898}
899
900
Tony Barbour6918cd52015-04-09 12:58:51 -0600901void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600902 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600903 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600904 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
905 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
906 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
907 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600908 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600909 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600910 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
911 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
912 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
913 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
914 VK_MEMORY_INPUT_SHADER_READ_BIT |
915 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
916 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
917 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700918{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600919 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700920
Tony Barbour38422802014-12-10 14:36:31 -0700921 if (!m_commandBuffer)
922 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600923 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700924
Tony Barbour6918cd52015-04-09 12:58:51 -0600925 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700926 }
927 else
928 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800929 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700930 }
931
Tony Barboure2c58df2014-11-25 13:18:32 -0700932 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600933 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600934 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600935 cmd_buf_info.pNext = NULL;
936 cmd_buf_info.flags = 0;
937
Jon Ashburnc4164b12014-12-31 17:10:47 -0700938 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600939 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700940
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600941 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000942 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600943 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700944
Tony Barbourd1c35722015-04-16 15:59:00 -0600945 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000946
947 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600948 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700949
950 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700951 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600952 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700953
Tony Barboure2c58df2014-11-25 13:18:32 -0700954 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600955 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700956 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600957 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
958 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700959}
960
Tony Barbour6918cd52015-04-09 12:58:51 -0600961VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
962 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700963{
964
965}
966
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600967void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700968{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600969 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700970
971 m_numVertices = numIndexes;
972 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700973 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600974 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700975 m_stride = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -0600976 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700977 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600978 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700979 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600980 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700981 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600982 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700983 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600984 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700985 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800986 default:
987 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600988 m_stride = 2;
989 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800990 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700991 }
992
Chia-I Wua07fee62014-12-28 15:26:08 +0800993 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600994 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
995 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700996
Chia-I Wua07fee62014-12-28 15:26:08 +0800997 void *pData = map();
998 memcpy(pData, data, allocationSize);
999 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001000
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001001 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001002 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001003 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001004 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -06001005 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001006 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001007 view_info.offset = 0;
1008 view_info.range = allocationSize;
1009 m_bufferView.init(*m_device, view_info);
1010
Chia-I Wu9d00ed72015-05-25 16:27:55 +08001011 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001012}
1013
Tony Barbourd1c35722015-04-16 15:59:00 -06001014void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001015{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001016 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001017}
Tony Barboure2c58df2014-11-25 13:18:32 -07001018
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001019VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001020{
1021 return m_indexType;
1022}
1023
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001024VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001025{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001026 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001027 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001028 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001029 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -07001030 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001031 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001032
Tony Barboure2c58df2014-11-25 13:18:32 -07001033 return stageInfo;
1034}
1035
Tony Barbourd1c35722015-04-16 15:59:00 -06001036VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001037{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001038 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001039 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001040 VkShaderCreateInfo createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -07001041 size_t shader_len;
1042
1043 m_stage = stage;
1044 m_device = device;
1045
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001046 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001047 createInfo.pNext = NULL;
1048
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001049 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001050
1051 shader_len = strlen(shader_code);
1052 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1053 createInfo.pCode = malloc(createInfo.codeSize);
1054 createInfo.flags = 0;
1055
Tony Barbourd1c35722015-04-16 15:59:00 -06001056 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001057 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -07001058 ((uint32_t *) createInfo.pCode)[1] = 0;
1059 ((uint32_t *) createInfo.pCode)[2] = stage;
1060 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
1061
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001062 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001063
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001064 // Use Reference GLSL to SPV compiler
1065 framework->GLSLtoSPV(stage, shader_code, spv);
1066 createInfo.pCode = spv.data();
1067 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -07001068 createInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001069 }
Cody Northrop475663c2015-04-15 11:19:06 -06001070
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001071 err = init_try(*m_device, createInfo);
1072
Cody Northrop475663c2015-04-15 11:19:06 -06001073 assert(VK_SUCCESS == err);
Tony Barbourf325bf12014-12-03 15:59:38 -07001074}
Tony Barbour82c39522014-12-04 14:33:33 -07001075
Tony Barbour6918cd52015-04-09 12:58:51 -06001076VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001077{
Tony Barboure2c58df2014-11-25 13:18:32 -07001078 m_device = device;
1079 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
1080 m_vertexBufferCount = 0;
1081
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001082 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1083 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001084 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001085 m_ia_state.disableVertexReuse = VK_FALSE;
1086 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001087 m_ia_state.primitiveRestartIndex = 0;
1088
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001089 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001090 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001091 m_rs_state.depthClipEnable = VK_FALSE;
1092 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001093 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1094 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001095 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001096 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1097 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001098
Tony Barboure2c58df2014-11-25 13:18:32 -07001099 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001100 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001101 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001102 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1103 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001104
Tony Barbourf52346d2015-01-16 14:27:35 -07001105 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001106 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1107 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001108 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1109 m_ms_state.samples = 1;
1110 m_ms_state.minSampleShading = 0;
1111 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001112
Tony Barbourd81b8882015-05-15 09:37:57 -06001113 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
1114 m_vp_state.pNext = &m_ms_state;
1115 m_vp_state.viewportCount = 1;
1116 m_vp_state.depthMode = VK_DEPTH_MODE_ZERO_TO_ONE;
1117 m_vp_state.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1118
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001119 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -06001120 m_ds_state.pNext = &m_vp_state,
Tony Barbourd1c35722015-04-16 15:59:00 -06001121 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001122 m_ds_state.depthTestEnable = VK_FALSE;
1123 m_ds_state.depthWriteEnable = VK_FALSE;
1124 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001125 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001126 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1127 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1128 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001129 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001130 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001131 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001132
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001133 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001134 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001135 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001136 att.channelWriteMask = 0xf;
1137 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001138
1139};
1140
Tony Barbour6918cd52015-04-09 12:58:51 -06001141void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001142{
1143 m_shaderObjs.push_back(shader);
1144}
1145
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001146void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001147{
1148 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1149 m_vi_state.attributeCount = count;
1150}
1151
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001152void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001153{
1154 m_vi_state.pVertexBindingDescriptions = vi_binding;
1155 m_vi_state.bindingCount = count;
1156}
1157
Tony Barbour6918cd52015-04-09 12:58:51 -06001158void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001159{
1160 m_vertexBufferObjs.push_back(vertexDataBuffer);
1161 m_vertexBufferBindings.push_back(binding);
1162 m_vertexBufferCount++;
1163}
1164
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001165void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001166{
Tony Barbourf52346d2015-01-16 14:27:35 -07001167 if (binding+1 > m_colorAttachments.size())
1168 {
1169 m_colorAttachments.resize(binding+1);
1170 }
1171 m_colorAttachments[binding] = *att;
1172}
1173
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001174void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001175{
1176 m_ds_state.format = ds_state->format;
1177 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1178 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1179 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001180 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001181 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1182 m_ds_state.back = ds_state->back;
1183 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001184}
1185
Chris Forbes95292b12015-05-25 11:13:26 +12001186VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001187{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001188 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001189 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001190
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001191 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001192
1193 for (int i=0; i<m_shaderObjs.size(); i++)
1194 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001195 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001196 shaderCreateInfo->pNext = head_ptr;
1197 head_ptr = shaderCreateInfo;
1198 }
1199
1200 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1201 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001202 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001203 m_vi_state.pNext = head_ptr;
1204 head_ptr = &m_vi_state;
1205 }
1206
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001207 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1208 info.pNext = head_ptr;
1209 info.flags = 0;
1210 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001211
Tony Barbourf52346d2015-01-16 14:27:35 -07001212 m_cb_state.attachmentCount = m_colorAttachments.size();
1213 m_cb_state.pAttachments = &m_colorAttachments[0];
1214
Chris Forbes95292b12015-05-25 11:13:26 +12001215 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001216}
Chia-I Wu2648d092014-12-29 14:24:14 +08001217
Tony Barbourd1c35722015-04-16 15:59:00 -06001218vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001219{
Tony Barbourd1c35722015-04-16 15:59:00 -06001220 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001221 if (this->mem_refs_.size()) {
1222 mems.reserve(this->mem_refs_.size());
1223 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1224 mems.push_back(this->mem_refs_[i]);
1225 }
1226
1227 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001228}
1229
Tony Barbour6918cd52015-04-09 12:58:51 -06001230VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001231 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001232{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001233 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001234}
Tony Barbour471338d2014-12-10 17:28:39 -07001235
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001236VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001237{
Chia-I Wud28343c2014-12-28 15:12:48 +08001238 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001239}
Tony Barbour471338d2014-12-10 17:28:39 -07001240
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001241VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001242{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001243 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001244 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001245}
1246
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001247VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001248{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001249 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001250 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001251}
1252
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001253VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001254{
1255 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001256 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001257}
1258
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001259VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001260{
Chia-I Wud28343c2014-12-28 15:12:48 +08001261 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001262 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001263}
1264
Tony Barbourd1c35722015-04-16 15:59:00 -06001265void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001266{
Tony Barbourd1c35722015-04-16 15:59:00 -06001267 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001268}
1269
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001270void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001271 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001272{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001273 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001274 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001275 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001276 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1277 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1278 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001279 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001280 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001281
1282 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001283 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001284 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001285 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001286 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001287 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001288 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001289
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001290 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001291 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001292 memory_barrier.outputMask = output_mask;
1293 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001294 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001295 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001296 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001297
Tony Barbourd1c35722015-04-16 15:59:00 -06001298 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001299
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001300 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001301 memory_barrier.image = m_renderTargets[i]->image();
1302 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001303 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001304 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001305
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001306 vkCmdClearColorImage(obj(),
1307 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001308 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001309
Tony Barbour30cc9e82014-12-17 11:53:55 -07001310 }
1311
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001312 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001313 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001314 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001315 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001316 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001317 dsRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001318 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001319 dsRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001320
1321 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001322
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001323 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001324 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001325 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001326 memory_barrier.subresourceRange = dsRange;
1327
Tony Barbourd1c35722015-04-16 15:59:00 -06001328 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001329
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001330 vkCmdClearDepthStencil(obj(),
1331 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001332 depth_clear_color, stencil_clear_color,
1333 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001334
1335 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001336 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001337 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001338 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001339 memory_barrier.subresourceRange = dsRange;
Tony Barbourd1c35722015-04-16 15:59:00 -06001340 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001341 }
1342}
1343
Tony Barbour6918cd52015-04-09 12:58:51 -06001344void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001345{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001346 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001347 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001348 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001349 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1350 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1351 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001352 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001353 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001354 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001355 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1356 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1357 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1358 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1359 VK_MEMORY_INPUT_SHADER_READ_BIT |
1360 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1361 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001362 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001363
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001364 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001365 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001366 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001367 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001368 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001369 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001370
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001371 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001372 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001373 memory_barrier.outputMask = output_mask;
1374 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001375 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001376 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001377 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001378
Tony Barbourd1c35722015-04-16 15:59:00 -06001379 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001380
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001381 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001382 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001383 memory_barrier.image = m_renderTargets[i]->image();
1384 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001385 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001386 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001387 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001388}
1389
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001390void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001391{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001392 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001393 renderpass,
1394 framebuffer,
1395 };
1396
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001397 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001398}
1399
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001400void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001401{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001402 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001403}
1404
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001405void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001406{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001407 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001408}
1409
Tony Barbour6918cd52015-04-09 12:58:51 -06001410void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001411{
1412 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001413}
1414
Tony Barbour6918cd52015-04-09 12:58:51 -06001415void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001416{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001417 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001418}
1419
Tony Barbour6918cd52015-04-09 12:58:51 -06001420void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001421{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001422 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001423}
1424
Tony Barbour6918cd52015-04-09 12:58:51 -06001425void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001426{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001427 QueueCommandBuffer(NULL);
1428}
1429
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001430void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001431{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001432 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001433
1434 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001435 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1436 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001437
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001438 err = vkQueueWaitIdle( m_device->m_queue );
1439 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001440
1441 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001442 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001443}
1444
Tony Barbour6918cd52015-04-09 12:58:51 -06001445void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001446{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001447 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001448}
1449
Tony Barbour6918cd52015-04-09 12:58:51 -06001450void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001451{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001452 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001453
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001454 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001455 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropd4c1a502015-04-16 13:41:56 -06001456 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001457}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001458
Tony Barbour6918cd52015-04-09 12:58:51 -06001459void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001460{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001461 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001462}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001463
Tony Barbourd1c35722015-04-16 15:59:00 -06001464void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001465{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001466 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001467}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001468
Tony Barbour6918cd52015-04-09 12:58:51 -06001469VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001470{
1471 m_initialized = false;
1472}
Tony Barbour6918cd52015-04-09 12:58:51 -06001473bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001474{
1475 return m_initialized;
1476}
1477
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001478VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001479{
1480 return &m_depthStencilBindInfo;
1481}
1482
Tony Barbour6918cd52015-04-09 12:58:51 -06001483void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001484{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001485 VkImageCreateInfo image_info;
1486 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001487
1488 m_device = device;
1489 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001490 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001491
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001492 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001493 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001494 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001495 image_info.format = m_depth_stencil_fmt;
1496 image_info.extent.width = width;
1497 image_info.extent.height = height;
1498 image_info.extent.depth = 1;
1499 image_info.mipLevels = 1;
1500 image_info.arraySize = 1;
1501 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001502 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001503 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001504 image_info.flags = 0;
1505 init(*m_device, image_info);
1506
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001507 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001508 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001509 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001510 view_info.mipLevel = 0;
1511 view_info.baseArraySlice = 0;
1512 view_info.arraySize = 1;
1513 view_info.flags = 0;
1514 view_info.image = obj();
1515 m_depthStencilView.init(*m_device, view_info);
1516
1517 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001518 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001519}