blob: 7c3fa4a78c6bf5cb07aa8200094d1892fbeb81ed [file] [log] [blame]
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -060029
Jon Ashburn07b309a2015-04-15 11:31:12 -060030#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
31
Tony Barbour01999182015-04-09 12:58:51 -060032VkRenderFramework::VkRenderFramework() :
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060033 m_cmdBuffer( VK_NULL_HANDLE ),
Tony Barbourbc868f92015-04-09 11:07:02 -060034 m_renderPass(VK_NULL_HANDLE),
35 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchter9cc421e2015-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 Goeltzenleuchtere5409342014-10-08 14:26:40 -060040 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter7e305322015-03-05 17:26:38 -070041 m_height( 256.0 ), // default window height
Tony Barbour8205d902015-04-16 15:59:00 -060042 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
43 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Chris Forbes23e6db62015-06-15 09:32:35 +120044 m_clear_via_load_op( false ),
Courtney Goeltzenleuchter7e305322015-03-05 17:26:38 -070045 m_depth_clear_color( 1.0 ),
Courtney Goeltzenleuchter471cc122015-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 Goeltzenleuchter5744b972014-10-08 08:50:49 -060052{
Tony Barbour17c6ab12015-03-27 17:03:18 -060053
Courtney Goeltzenleuchter7e305322015-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 Goeltzenleuchter5744b972014-10-08 08:50:49 -060060}
61
Tony Barbour01999182015-04-09 12:58:51 -060062VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -060063{
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060064
65}
66
Tony Barbour01999182015-04-09 12:58:51 -060067void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060068{
Courtney Goeltzenleuchter1c7c65d2015-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 Barbour950ebc02015-04-23 12:55:36 -060072}
73
Courtney Goeltzenleuchter1c7c65d2015-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 Barbour950ebc02015-04-23 12:55:36 -060079{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -060080 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchter1c7c65d2015-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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600105 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburn29669a42015-04-04 14:52:07 -0600106 instInfo.pNext = NULL;
107 instInfo.pAppInfo = &app_info;
108 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600109 instInfo.extensionCount = instance_extensions.size();
110 instInfo.pEnabledExtensions = (instance_extensions.size()) ? &instance_extensions[0] : NULL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600111 err = vkCreateInstance(&instInfo, &this->inst);
112 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600113
Jon Ashburn07b309a2015-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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600118 ASSERT_VK_SUCCESS(err);
Jon Ashburn19733c92014-11-26 11:06:49 -0700119 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour0c1bdc62015-04-29 17:34:29 -0600120 if (dbgFunction) {
Courtney Goeltzenleuchter1c7c65d2015-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 Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -0600128 &m_globalMsgCallback);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600129 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter18e8a492015-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 Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600133 }
Tony Barbour0c1bdc62015-04-29 17:34:29 -0600134 }
Courtney Goeltzenleuchter1c7c65d2015-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 Goeltzenleuchter23b5f8d2015-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 Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600172 m_device->get_device_queue();
Tony Barbour17c6ab12015-03-27 17:03:18 -0600173
Tony Barbour01999182015-04-09 12:58:51 -0600174 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600175}
176
Tony Barbour01999182015-04-09 12:58:51 -0600177void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600178{
Mike Stroyan230e6252015-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 Goeltzenleuchter5744b972014-10-08 08:50:49 -0600185
Courtney Goeltzenleuchter18e8a492015-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 Goeltzenleuchter5744b972014-10-08 08:50:49 -0600189 if (m_stateViewport) {
Mike Stroyan230e6252015-04-17 12:36:38 -0600190 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600191 }
Tobin Ehlis12ee35f2015-03-26 08:23:25 -0600192 while (!m_renderTargets.empty()) {
Mike Stroyan230e6252015-04-17 12:36:38 -0600193 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
Mike Stroyan230e6252015-04-17 12:36:38 -0600194 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
195 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis12ee35f2015-03-26 08:23:25 -0600196 m_renderTargets.pop_back();
197 }
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600198
Tony Barbour17c6ab12015-03-27 17:03:18 -0600199 delete m_depthStencil;
200
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600201 // reset the driver
Chia-I Wu1c9869e2014-12-28 14:27:28 +0800202 delete m_device;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600203 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600204}
205
Tony Barbour01999182015-04-09 12:58:51 -0600206void VkRenderFramework::InitState()
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600207{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600208 VkResult err;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600209
Tony Barbour8205d902015-04-16 15:59:00 -0600210 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600211
212 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600213 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600214 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700215
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600216 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
217 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600218
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600219 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600220 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Tony Barbour5e245132015-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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600225 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
226 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600227
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600228 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600229 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski4405fbf2015-06-12 11:14:17 -0600230 depthStencil.minDepthBounds = 0.f;
231 depthStencil.maxDepthBounds = 1.f;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700232 depthStencil.stencilFrontRef = 0;
233 depthStencil.stencilBackRef = 0;
Tony Barbour5e245132015-05-15 09:37:57 -0600234 depthStencil.stencilReadMask = 0xff;
235 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600236
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600237 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
238 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600239
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600240 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600241
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600242 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700243 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
244
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600245 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
246 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600247}
248
Tony Barbour01999182015-04-09 12:58:51 -0600249void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600250{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600251 VkResult err;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600252
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600253 VkViewport viewport;
254 VkRect scissor;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600255
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600256 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600257 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -0700258 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourfa6cac72015-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 Barbour8bef8ee2015-05-22 09:44:58 -0600265 scissor.extent.width = (int32_t) width;
266 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -0700267 scissor.offset.x = 0;
268 scissor.offset.y = 0;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700269 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -0700270 viewportCreate.pScissors = &scissor;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700271
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600272 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
273 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600274 m_width = width;
275 m_height = height;
276}
277
Tony Barbour01999182015-04-09 12:58:51 -0600278void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600279{
280 InitViewport(m_width, m_height);
281}
Tony Barbour01999182015-04-09 12:58:51 -0600282void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600283{
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -0700284 InitRenderTarget(1);
285}
286
Tony Barbour01999182015-04-09 12:58:51 -0600287void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -0700288{
Tony Barbour17c6ab12015-03-27 17:03:18 -0600289 InitRenderTarget(targets, NULL);
290}
291
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600292void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour17c6ab12015-03-27 17:03:18 -0600293{
294 InitRenderTarget(1, dsBinding);
295}
296
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600297void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour17c6ab12015-03-27 17:03:18 -0600298{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600299 std::vector<VkAttachmentLoadOp> load_ops;
300 std::vector<VkAttachmentStoreOp> store_ops;
301 std::vector<VkClearColor> clear_colors;
Tony Barbour17c6ab12015-03-27 17:03:18 -0600302
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600303 uint32_t i;
Chia-I Wue7748802014-12-05 10:45:15 +0800304
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -0700305 for (i = 0; i < targets; i++) {
Tony Barbour01999182015-04-09 12:58:51 -0600306 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600307
Tony Barbour25e3b832015-05-20 16:53:31 -0600308 VkFormatProperties props;
Tony Barbour25e3b832015-05-20 16:53:31 -0600309 VkResult err;
310
Chris Forbesd7576302015-06-21 22:55:02 +1200311 err = vkGetPhysicalDeviceFormatInfo(m_device->gpu().obj(), m_render_target_fmt, &props);
Tony Barbour25e3b832015-05-20 16:53:31 -0600312 ASSERT_VK_SUCCESS(err);
313
314 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour8bef8ee2015-05-22 09:44:58 -0600315 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour25e3b832015-05-20 16:53:31 -0600316 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_LINEAR);
317 }
318 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour8bef8ee2015-05-22 09:44:58 -0600319 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour25e3b832015-05-20 16:53:31 -0600320 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL);
321 }
322 else {
323 FAIL() << "Neither Linear nor Optimal allowed for render target";
324 }
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600325
Tony Barbour25e3b832015-05-20 16:53:31 -0600326 m_renderTargets.push_back(img);
Tony Barbourfa6cac72015-01-16 14:27:35 -0700327 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600328 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chris Forbes23e6db62015-06-15 09:32:35 +1200329 load_ops.push_back(m_clear_via_load_op ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600330 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600331 clear_colors.push_back(m_clear_color);
Chia-I Wue7748802014-12-05 10:45:15 +0800332 }
Tony Barbour25e3b832015-05-20 16:53:31 -0600333
Jeremy Hayese0c3b222015-01-14 16:17:08 -0700334 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600335 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600336 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbour901f3bc2015-04-01 17:10:07 -0600337 fb_info.pNext = NULL;
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -0700338 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbour901f3bc2015-04-01 17:10:07 -0600339 fb_info.pColorAttachments = m_colorBindings;
340 fb_info.pDepthStencilAttachment = dsBinding;
341 fb_info.sampleCount = 1;
342 fb_info.width = (uint32_t)m_width;
343 fb_info.height = (uint32_t)m_height;
344 fb_info.layers = 1;
345
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600346 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600347
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600348 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600349 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Tony Barbour8bef8ee2015-05-22 09:44:58 -0600350 rp_info.renderArea.extent.width = (uint32_t) m_width;
351 rp_info.renderArea.extent.height = (uint32_t) m_height;
Tony Barbour17c6ab12015-03-27 17:03:18 -0600352
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -0700353 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600354 rp_info.pColorFormats = &m_render_target_fmt;
355 rp_info.pColorLayouts = &m_colorBindings[0].layout;
356 rp_info.pColorLoadOps = &load_ops[0];
357 rp_info.pColorStoreOps = &store_ops[0];
358 rp_info.pColorLoadClearValues = &clear_colors[0];
359 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour17c6ab12015-03-27 17:03:18 -0600360 if (dsBinding) {
361 rp_info.depthStencilLayout = dsBinding->layout;
362 }
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600363 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600364 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600365 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
366 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600367 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600368 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Tony Barbourb689eef2015-06-03 11:40:51 -0600369 rp_info.sampleCount = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600370 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600371}
372
Mark Lobodzinski15427102015-02-18 16:38:17 -0600373
374
Tony Barbour8205d902015-04-16 15:59:00 -0600375VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600376 vk_testing::Device(obj), id(id)
Chia-I Wu234a0ae2014-12-29 15:23:20 +0800377{
378 init();
379
380 props = gpu().properties();
381 queue_props = &gpu().queue_properties()[0];
382}
383
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600384VkDeviceObj::VkDeviceObj(uint32_t id,
385 VkPhysicalDevice obj,
386 std::vector<VkExtensionProperties> extensions) :
387 vk_testing::Device(obj), id(id)
388{
389 init(extensions);
390
391 props = gpu().properties();
392 queue_props = &gpu().queue_properties()[0];
393}
394
Tony Barbour01999182015-04-09 12:58:51 -0600395void VkDeviceObj::get_device_queue()
Chia-I Wu234a0ae2014-12-29 15:23:20 +0800396{
397 ASSERT_NE(true, graphics_queues().empty());
398 m_queue = graphics_queues()[0]->obj();
399}
400
Tobin Ehlis5b1452f2015-05-27 14:33:27 -0600401VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
402 m_device(device), m_nextSlot(0)
Tony Barbourf43b6982014-11-25 13:18:32 -0700403{
Tony Barbourf43b6982014-11-25 13:18:32 -0700404
405}
406
Tony Barbour01999182015-04-09 12:58:51 -0600407VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barbourf43b6982014-11-25 13:18:32 -0700408{
Chia-I Wuf8385062015-01-04 16:27:24 +0800409 delete m_set;
Tony Barbourf43b6982014-11-25 13:18:32 -0700410}
Tony Barbour408ca622014-12-04 14:33:33 -0700411
Tony Barbour01999182015-04-09 12:58:51 -0600412int VkDescriptorSetObj::AppendDummy()
Tony Barbourf43b6982014-11-25 13:18:32 -0700413{
Chia-I Wuf8385062015-01-04 16:27:24 +0800414 /* request a descriptor but do not update it */
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600415 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600416 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wuf8385062015-01-04 16:27:24 +0800417 tc.count = 1;
418 m_type_counts.push_back(tc);
Tony Barbourf43b6982014-11-25 13:18:32 -0700419
Chia-I Wuf8385062015-01-04 16:27:24 +0800420 return m_nextSlot++;
Tony Barbourf43b6982014-11-25 13:18:32 -0700421}
Tony Barbour408ca622014-12-04 14:33:33 -0700422
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600423int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barbourf43b6982014-11-25 13:18:32 -0700424{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600425 VkDescriptorTypeCount tc = {};
Chia-I Wuf8385062015-01-04 16:27:24 +0800426 tc.type = type;
427 tc.count = 1;
428 m_type_counts.push_back(tc);
Tony Barbourf43b6982014-11-25 13:18:32 -0700429
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800430 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
431 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600432
Chia-I Wuf8385062015-01-04 16:27:24 +0800433 return m_nextSlot++;
Tony Barbourf43b6982014-11-25 13:18:32 -0700434}
Tony Barbour408ca622014-12-04 14:33:33 -0700435
Tony Barbour01999182015-04-09 12:58:51 -0600436int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barbourf43b6982014-11-25 13:18:32 -0700437{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600438 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600439 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wuf8385062015-01-04 16:27:24 +0800440 tc.count = 1;
441 m_type_counts.push_back(tc);
Tony Barbourf43b6982014-11-25 13:18:32 -0700442
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800443 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Courtney Goeltzenleuchterd38dcc92015-04-09 11:43:10 -0600444 tmp.sampler = sampler->obj();
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800445 m_imageSamplerDescriptors.push_back(tmp);
Tony Barbourf43b6982014-11-25 13:18:32 -0700446
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800447 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
448 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barbourf43b6982014-11-25 13:18:32 -0700449
Chia-I Wuf8385062015-01-04 16:27:24 +0800450 return m_nextSlot++;
Tony Barbourf43b6982014-11-25 13:18:32 -0700451}
Chia-I Wuf8385062015-01-04 16:27:24 +0800452
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500453VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbour3edb6ac2014-12-17 10:54:03 -0700454{
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500455 return m_pipeline_layout.obj();
Tony Barbour3edb6ac2014-12-17 10:54:03 -0700456}
457
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600458VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbour3edb6ac2014-12-17 10:54:03 -0700459{
Chia-I Wuf8385062015-01-04 16:27:24 +0800460 return m_set->obj();
Tony Barbour3edb6ac2014-12-17 10:54:03 -0700461}
Tony Barbourf43b6982014-11-25 13:18:32 -0700462
Tony Barbour01999182015-04-09 12:58:51 -0600463void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barboura5712982014-12-18 17:06:21 -0700464{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600465 // create VkDescriptorPool
466 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600467 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu8d24b3b2015-03-26 13:14:16 +0800468 pool.count = m_type_counts.size();
469 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600470 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barboura5712982014-12-18 17:06:21 -0700471
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600472 // create VkDescriptorSetLayout
473 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wufc9d9132015-03-26 15:04:41 +0800474 bindings.resize(m_type_counts.size());
Chia-I Wuf8385062015-01-04 16:27:24 +0800475 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wufc9d9132015-03-26 15:04:41 +0800476 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wud3114a22015-05-25 16:22:52 +0800477 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbour8205d902015-04-16 15:59:00 -0600478 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu310eece2015-03-27 12:56:09 +0800479 bindings[i].pImmutableSamplers = NULL;
Tony Barbourf43b6982014-11-25 13:18:32 -0700480 }
Chia-I Wu6ae4cfb2014-12-28 22:32:36 +0800481
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600482 // create VkDescriptorSetLayout
483 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600484 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wufc9d9132015-03-26 15:04:41 +0800485 layout.count = bindings.size();
486 layout.pBinding = &bindings[0];
487
Chia-I Wu7732cb22015-03-26 15:27:55 +0800488 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600489 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu7732cb22015-03-26 15:27:55 +0800490 layouts.push_back(&m_layout);
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500491
492 // create VkPipelineLayout
493 VkPipelineLayoutCreateInfo pipeline_layout = {};
494 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
495 pipeline_layout.descriptorSetCount = layouts.size();
496 pipeline_layout.pSetLayouts = NULL;
497
498 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barbourf43b6982014-11-25 13:18:32 -0700499
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600500 // create VkDescriptorSet
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500501 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wuf8385062015-01-04 16:27:24 +0800502
Chia-I Wu7732cb22015-03-26 15:27:55 +0800503 // build the update array
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800504 size_t imageSamplerCount = 0;
505 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
506 it != m_writes.end(); it++) {
507 it->destSet = m_set->obj();
508 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
509 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wuf8385062015-01-04 16:27:24 +0800510 }
Chia-I Wuf8385062015-01-04 16:27:24 +0800511
512 // do the updates
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800513 m_device->update_descriptor_sets(m_writes);
Tony Barbour342ae3b2014-12-03 13:59:18 -0700514}
Tony Barbourf43b6982014-11-25 13:18:32 -0700515
Tony Barbour01999182015-04-09 12:58:51 -0600516VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800517{
518 m_device = dev;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800519 m_descriptorInfo.imageView = VK_NULL_HANDLE;
520 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800521}
522
Tony Barbour01999182015-04-09 12:58:51 -0600523void VkImageObj::ImageMemoryBarrier(
524 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600525 VkImageAspect aspect,
526 VkFlags output_mask /*=
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600527 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600528 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
529 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
530 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
531 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600532 VkFlags input_mask /*=
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600533 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600534 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
535 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
536 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
537 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
538 VK_MEMORY_INPUT_SHADER_READ_BIT |
539 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
540 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
541 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600542 VkImageLayout image_layout)
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600543{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600544 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
545 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600546 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
547 subresourceRange);
548
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600549 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600550
Tony Barbour8205d902015-04-16 15:59:00 -0600551 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600552
553 // write barrier to the command buffer
Tony Barbour8205d902015-04-16 15:59:00 -0600554 vkCmdPipelineBarrier(cmd_buf->obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, pipe_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600555}
556
Tony Barbour01999182015-04-09 12:58:51 -0600557void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600558 VkImageAspect aspect,
559 VkImageLayout image_layout)
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600560{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600561 VkFlags output_mask, input_mask;
562 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600563 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600564 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
565 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
566 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600567 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600568 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600569 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600570 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
571 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
572 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
573 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
574 VK_MEMORY_INPUT_SHADER_READ_BIT |
575 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
576 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600577 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600578
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800579 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600580 return;
581 }
582
583 switch (image_layout) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600584 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600585 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
586 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600587 break;
588
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600589 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600590 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
591 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600592 break;
593
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600594 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600595 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
596 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600597 break;
598
Tony Barbour22a30862015-04-22 09:02:32 -0600599 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600600 default:
601 output_mask = all_cache_outputs;
602 input_mask = all_cache_inputs;
603 break;
604 }
605
606 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800607 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600608}
609
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600610void VkImageObj::SetLayout(VkImageAspect aspect,
611 VkImageLayout image_layout)
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600612{
Tony Barbour22a30862015-04-22 09:02:32 -0600613 VkResult U_ASSERT_ONLY err;
Tony Barbour25e3b832015-05-20 16:53:31 -0600614
615 if (image_layout == m_descriptorInfo.imageLayout) {
616 return;
617 }
618
Tony Barbour01999182015-04-09 12:58:51 -0600619 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600620
621 /* Build command buffer to set image layout in the driver */
622 err = cmd_buf.BeginCommandBuffer();
623 assert(!err);
624
625 SetLayout(&cmd_buf, aspect, image_layout);
626
627 err = cmd_buf.EndCommandBuffer();
628 assert(!err);
629
630 cmd_buf.QueueCommandBuffer();
631}
632
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600633bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600634{
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600635 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbour8205d902015-04-16 15:59:00 -0600636 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600637 return false;
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600638
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600639 return true;
640}
641
Tony Barbour01999182015-04-09 12:58:51 -0600642void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600643 VkFormat fmt, VkFlags usage,
Tony Barbour94310562015-04-22 15:10:33 -0600644 VkImageTiling requested_tiling,
645 VkMemoryPropertyFlags reqs)
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800646{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600647 uint32_t mipCount;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600648 VkFormatProperties image_fmt;
649 VkImageTiling tiling;
650 VkResult err;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600651
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800652 mipCount = 0;
653
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600654 uint32_t _w = w;
655 uint32_t _h = h;
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800656 while( ( _w > 0 ) || ( _h > 0 ) )
657 {
658 _w >>= 1;
659 _h >>= 1;
660 mipCount++;
661 }
662
Chris Forbesd7576302015-06-21 22:55:02 +1200663 err = vkGetPhysicalDeviceFormatInfo(m_device->gpu().obj(), fmt, &image_fmt);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600664 ASSERT_VK_SUCCESS(err);
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600665
Tony Barbour8205d902015-04-16 15:59:00 -0600666 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600667 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbour8205d902015-04-16 15:59:00 -0600668 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600669 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbour8205d902015-04-16 15:59:00 -0600670 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600671 } else {
672 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
673 }
674 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbour8205d902015-04-16 15:59:00 -0600675 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600676 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbour8205d902015-04-16 15:59:00 -0600677 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600678 } else {
679 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
680 }
681
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600682 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbour8205d902015-04-16 15:59:00 -0600683 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800684 imageCreateInfo.format = fmt;
685 imageCreateInfo.extent.width = w;
686 imageCreateInfo.extent.height = h;
687 imageCreateInfo.mipLevels = mipCount;
688 imageCreateInfo.tiling = tiling;
689
690 imageCreateInfo.usage = usage;
691
Tony Barbour94310562015-04-22 15:10:33 -0600692 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800693
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600694 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600695 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600696 } else {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600697 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600698 }
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800699}
700
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600701VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800702{
703 *ptr = map();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600704 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800705}
706
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600707VkResult VkImageObj::UnmapMemory()
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800708{
709 unmap();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600710 return VK_SUCCESS;
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800711}
712
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600713VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour16b71bc2015-04-01 17:47:06 -0600714{
Tony Barbour22a30862015-04-22 09:02:32 -0600715 VkResult U_ASSERT_ONLY err;
Tony Barbour01999182015-04-09 12:58:51 -0600716 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600717 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600718
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600719 /* Build command buffer to copy staging texture to usable texture */
720 err = cmd_buf.BeginCommandBuffer();
Tony Barbour16b71bc2015-04-01 17:47:06 -0600721 assert(!err);
722
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600723 /* TODO: Can we determine image aspect from image object? */
724 src_image_layout = src_image.layout();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600725 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour16b71bc2015-04-01 17:47:06 -0600726
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600727 dest_image_layout = this->layout();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600728 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour17c6ab12015-03-27 17:03:18 -0600729
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600730 VkImageCopy copy_region = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600731 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600732 copy_region.srcSubresource.arraySlice = 0;
733 copy_region.srcSubresource.mipLevel = 0;
734 copy_region.srcOffset.x = 0;
735 copy_region.srcOffset.y = 0;
736 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600737 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600738 copy_region.destSubresource.arraySlice = 0;
739 copy_region.destSubresource.mipLevel = 0;
740 copy_region.destOffset.x = 0;
741 copy_region.destOffset.y = 0;
742 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600743 copy_region.extent = src_image.extent();
Tony Barbour16b71bc2015-04-01 17:47:06 -0600744
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600745 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600746 src_image.obj(), src_image.layout(),
747 obj(), layout(),
Tony Barbour17c6ab12015-03-27 17:03:18 -0600748 1, &copy_region);
Tony Barbour16b71bc2015-04-01 17:47:06 -0600749
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600750 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour16b71bc2015-04-01 17:47:06 -0600751
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600752 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour17c6ab12015-03-27 17:03:18 -0600753
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600754 err = cmd_buf.EndCommandBuffer();
Tony Barbour16b71bc2015-04-01 17:47:06 -0600755 assert(!err);
756
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600757 cmd_buf.QueueCommandBuffer();
Tony Barbour16b71bc2015-04-01 17:47:06 -0600758
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600759 return VK_SUCCESS;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600760}
761
Tony Barbour01999182015-04-09 12:58:51 -0600762VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
763 :VkImageObj(device)
Tony Barbourf43b6982014-11-25 13:18:32 -0700764{
765 m_device = device;
Tony Barbour8205d902015-04-16 15:59:00 -0600766 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbour2f421a02015-04-01 16:38:10 -0600767 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour16b71bc2015-04-01 17:47:06 -0600768 void *data;
769 int32_t x, y;
Tony Barbour01999182015-04-09 12:58:51 -0600770 VkImageObj stagingImage(device);
Tony Barbour94310562015-04-22 15:10:33 -0600771 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600772
Tony Barbour94310562015-04-22 15:10:33 -0600773 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600774 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbour2f421a02015-04-01 16:38:10 -0600775
776 if (colors == NULL)
777 colors = tex_colors;
Tony Barbourf43b6982014-11-25 13:18:32 -0700778
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800779 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barbourf43b6982014-11-25 13:18:32 -0700780
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600781 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600782 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbour901f3bc2015-04-01 17:10:07 -0600783 view.pNext = NULL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600784 view.image = VK_NULL_HANDLE;
Tony Barbour8205d902015-04-16 15:59:00 -0600785 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600786 view.format = tex_format;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600787 view.channels.r = VK_CHANNEL_SWIZZLE_R;
788 view.channels.g = VK_CHANNEL_SWIZZLE_G;
789 view.channels.b = VK_CHANNEL_SWIZZLE_B;
790 view.channels.a = VK_CHANNEL_SWIZZLE_A;
791 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour901f3bc2015-04-01 17:10:07 -0600792 view.subresourceRange.baseMipLevel = 0;
793 view.subresourceRange.mipLevels = 1;
794 view.subresourceRange.baseArraySlice = 0;
795 view.subresourceRange.arraySize = 1;
796 view.minLod = 0.0f;
Tony Barbourf43b6982014-11-25 13:18:32 -0700797
Tony Barbourf43b6982014-11-25 13:18:32 -0700798 /* create image */
Tony Barbour8205d902015-04-16 15:59:00 -0600799 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barbourf43b6982014-11-25 13:18:32 -0700800
801 /* create image view */
Chia-I Wudbec1222014-12-28 15:55:09 +0800802 view.image = obj();
803 m_textureView.init(*m_device, view);
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800804 m_descriptorInfo.imageView = m_textureView.obj();
Tony Barbourf43b6982014-11-25 13:18:32 -0700805
Tony Barbour16b71bc2015-04-01 17:47:06 -0600806 data = stagingImage.map();
Tony Barbourf43b6982014-11-25 13:18:32 -0700807
Chia-I Wudbec1222014-12-28 15:55:09 +0800808 for (y = 0; y < extent().height; y++) {
Tony Barbourf43b6982014-11-25 13:18:32 -0700809 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wudbec1222014-12-28 15:55:09 +0800810 for (x = 0; x < extent().width; x++)
Tony Barbour2f421a02015-04-01 16:38:10 -0600811 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barbourf43b6982014-11-25 13:18:32 -0700812 }
Tony Barbour16b71bc2015-04-01 17:47:06 -0600813 stagingImage.unmap();
Tony Barbour01999182015-04-09 12:58:51 -0600814 VkImageObj::CopyImage(stagingImage);
Tony Barbourf43b6982014-11-25 13:18:32 -0700815}
Tony Barbour408ca622014-12-04 14:33:33 -0700816
Tony Barbour01999182015-04-09 12:58:51 -0600817VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barbourf43b6982014-11-25 13:18:32 -0700818{
Tony Barbourf43b6982014-11-25 13:18:32 -0700819 m_device = device;
Tony Barbourf43b6982014-11-25 13:18:32 -0700820
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600821 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wua64266a2014-12-28 16:32:24 +0800822 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600823 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
824 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
825 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbour8205d902015-04-16 15:59:00 -0600826 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600827 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
828 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
829 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wua64266a2014-12-28 16:32:24 +0800830 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour8bef8ee2015-05-22 09:44:58 -0600831 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600832 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wua64266a2014-12-28 16:32:24 +0800833 samplerCreateInfo.minLod = 0.0;
834 samplerCreateInfo.maxLod = 0.0;
Tony Barbour8205d902015-04-16 15:59:00 -0600835 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barbourf43b6982014-11-25 13:18:32 -0700836
Chia-I Wua64266a2014-12-28 16:32:24 +0800837 init(*m_device, samplerCreateInfo);
Tony Barbour2cb6bf52014-12-03 15:59:38 -0700838}
Tony Barbourf43b6982014-11-25 13:18:32 -0700839
Courtney Goeltzenleuchterea2db0d2014-12-04 15:18:47 -0700840/*
841 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
842 */
Tony Barbour01999182015-04-09 12:58:51 -0600843VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterea2db0d2014-12-04 15:18:47 -0700844{
845 m_device = device;
Tony Barbour9f47dc72014-12-10 14:36:31 -0700846 m_commandBuffer = 0;
Courtney Goeltzenleuchterea2db0d2014-12-04 15:18:47 -0700847
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800848 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchterea2db0d2014-12-04 15:18:47 -0700849}
850
Tony Barbour01999182015-04-09 12:58:51 -0600851VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour864c0332015-03-26 12:37:52 -0600852{
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600853 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour864c0332015-03-26 12:37:52 -0600854 if (m_commandBuffer) {
855 delete m_commandBuffer;
856 }
857}
858
Tony Barbour01999182015-04-09 12:58:51 -0600859VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barbourf43b6982014-11-25 13:18:32 -0700860{
Tony Barbourf43b6982014-11-25 13:18:32 -0700861 m_device = device;
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800862 m_commandBuffer = 0;
863
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800864 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barbourf43b6982014-11-25 13:18:32 -0700865 m_numVertices = constantCount;
866 m_stride = constantSize;
867
Tony Barbour94310562015-04-22 15:10:33 -0600868 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800869 const size_t allocationSize = constantCount * constantSize;
Tony Barbour94310562015-04-22 15:10:33 -0600870 init(*m_device, allocationSize, reqs);
Tony Barbourf43b6982014-11-25 13:18:32 -0700871
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800872 void *pData = map();
873 memcpy(pData, data, allocationSize);
874 unmap();
Tony Barbourf43b6982014-11-25 13:18:32 -0700875
Chia-I Wu714df452015-01-01 07:55:04 +0800876 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600877 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600878 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +0800879 view_info.buffer = obj();
Tony Barbour8205d902015-04-16 15:59:00 -0600880 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +0800881 view_info.offset = 0;
882 view_info.range = allocationSize;
883 m_bufferView.init(*m_device, view_info);
884
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800885 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Tony Barbourf43b6982014-11-25 13:18:32 -0700886}
Tony Barbour408ca622014-12-04 14:33:33 -0700887
Tony Barbour8205d902015-04-16 15:59:00 -0600888void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter7715bc42014-12-04 15:26:56 -0700889{
Courtney Goeltzenleuchter46962942015-04-16 13:38:46 -0600890 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter7715bc42014-12-04 15:26:56 -0700891}
892
893
Tony Barbour01999182015-04-09 12:58:51 -0600894void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600895 VkFlags outputMask /*=
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600896 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600897 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
898 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
899 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
900 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600901 VkFlags inputMask /*=
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600902 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600903 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
904 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
905 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
906 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
907 VK_MEMORY_INPUT_SHADER_READ_BIT |
908 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
909 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
910 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barbourf43b6982014-11-25 13:18:32 -0700911{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600912 VkResult err = VK_SUCCESS;
Tony Barbour9f47dc72014-12-10 14:36:31 -0700913
Tony Barbour9f47dc72014-12-10 14:36:31 -0700914 if (!m_commandBuffer)
915 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600916 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour9f47dc72014-12-10 14:36:31 -0700917
Tony Barbour01999182015-04-09 12:58:51 -0600918 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour9f47dc72014-12-10 14:36:31 -0700919 }
920 else
921 {
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800922 m_device->wait(m_fence);
Tony Barbour9f47dc72014-12-10 14:36:31 -0700923 }
924
Tony Barbourf43b6982014-11-25 13:18:32 -0700925 // open the command buffer
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600926 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600927 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbour901f3bc2015-04-01 17:10:07 -0600928 cmd_buf_info.pNext = NULL;
929 cmd_buf_info.flags = 0;
930
Jon Ashburn744b8cf2014-12-31 17:10:47 -0700931 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600932 ASSERT_VK_SUCCESS(err);
Tony Barbourf43b6982014-11-25 13:18:32 -0700933
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600934 VkBufferMemoryBarrier memory_barrier =
Mike Stroyan55658c22014-12-04 11:08:39 +0000935 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600936 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barbourf43b6982014-11-25 13:18:32 -0700937
Tony Barbour8205d902015-04-16 15:59:00 -0600938 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyan55658c22014-12-04 11:08:39 +0000939
940 // write barrier to the command buffer
Tony Barbour8205d902015-04-16 15:59:00 -0600941 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbourf43b6982014-11-25 13:18:32 -0700942
943 // finish recording the command buffer
Tony Barboure8f14162014-12-10 17:28:39 -0700944 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600945 ASSERT_VK_SUCCESS(err);
Tony Barbourf43b6982014-11-25 13:18:32 -0700946
Tony Barbourf43b6982014-11-25 13:18:32 -0700947 // submit the command buffer to the universal queue
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600948 VkCmdBuffer bufferArray[1];
Tony Barboure8f14162014-12-10 17:28:39 -0700949 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600950 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
951 ASSERT_VK_SUCCESS(err);
Tony Barbourf43b6982014-11-25 13:18:32 -0700952}
953
Tony Barbour01999182015-04-09 12:58:51 -0600954VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
955 : VkConstantBufferObj(device)
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700956{
957
958}
959
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600960void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700961{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600962 VkFormat viewFormat;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700963
964 m_numVertices = numIndexes;
965 m_indexType = indexType;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700966 switch (indexType) {
Tony Barbour8205d902015-04-16 15:59:00 -0600967 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700968 m_stride = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600969 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700970 break;
Tony Barbour8205d902015-04-16 15:59:00 -0600971 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700972 m_stride = 2;
Tony Barbour8205d902015-04-16 15:59:00 -0600973 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700974 break;
Tony Barbour8205d902015-04-16 15:59:00 -0600975 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700976 m_stride = 4;
Tony Barbour8205d902015-04-16 15:59:00 -0600977 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700978 break;
Chia-I Wubbdeacc2014-12-15 23:50:11 +0800979 default:
980 assert(!"unknown index type");
Tony Barbour22a30862015-04-22 09:02:32 -0600981 m_stride = 2;
982 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wubbdeacc2014-12-15 23:50:11 +0800983 break;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700984 }
985
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800986 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour94310562015-04-22 15:10:33 -0600987 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
988 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700989
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800990 void *pData = map();
991 memcpy(pData, data, allocationSize);
992 unmap();
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700993
Chia-I Wu714df452015-01-01 07:55:04 +0800994 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600995 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600996 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +0800997 view_info.buffer = obj();
Tony Barbour8205d902015-04-16 15:59:00 -0600998 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700999 view_info.format = viewFormat;
Chia-I Wu714df452015-01-01 07:55:04 +08001000 view_info.offset = 0;
1001 view_info.range = allocationSize;
1002 m_bufferView.init(*m_device, view_info);
1003
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001004 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001005}
1006
Tony Barbour8205d902015-04-16 15:59:00 -06001007void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001008{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001009 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -07001010}
Tony Barbourf43b6982014-11-25 13:18:32 -07001011
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001012VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbour140a59f2014-12-17 10:57:58 -07001013{
1014 return m_indexType;
1015}
1016
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001017VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barbourf43b6982014-11-25 13:18:32 -07001018{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001019 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001020 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1021 stageInfo->stage = m_stage;
1022 stageInfo->shader = obj();
1023 stageInfo->linkConstBufferCount = 0;
1024 stageInfo->pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barbourf43b6982014-11-25 13:18:32 -07001025
Tony Barbourf43b6982014-11-25 13:18:32 -07001026 return stageInfo;
1027}
1028
Tony Barbour8205d902015-04-16 15:59:00 -06001029VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barbourf43b6982014-11-25 13:18:32 -07001030{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001031 VkResult err = VK_SUCCESS;
Cody Northropacfb0492015-03-17 15:55:58 -06001032 std::vector<unsigned int> spv;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001033 VkShaderCreateInfo createInfo;
Tony Barbourf43b6982014-11-25 13:18:32 -07001034 size_t shader_len;
1035
1036 m_stage = stage;
1037 m_device = device;
1038
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001039 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barbourf43b6982014-11-25 13:18:32 -07001040 createInfo.pNext = NULL;
1041
Cody Northrop1cfbd172015-06-03 16:49:20 -06001042 if (framework->m_use_glsl) {
Tony Barbourf43b6982014-11-25 13:18:32 -07001043
1044 shader_len = strlen(shader_code);
1045 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1046 createInfo.pCode = malloc(createInfo.codeSize);
1047 createInfo.flags = 0;
1048
Tony Barbour8205d902015-04-16 15:59:00 -06001049 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northropacfb0492015-03-17 15:55:58 -06001050 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barbourf43b6982014-11-25 13:18:32 -07001051 ((uint32_t *) createInfo.pCode)[1] = 0;
1052 ((uint32_t *) createInfo.pCode)[2] = stage;
1053 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
1054
Cody Northrop1cfbd172015-06-03 16:49:20 -06001055 } else {
Tony Barbourf43b6982014-11-25 13:18:32 -07001056
Cody Northropacfb0492015-03-17 15:55:58 -06001057 // Use Reference GLSL to SPV compiler
1058 framework->GLSLtoSPV(stage, shader_code, spv);
1059 createInfo.pCode = spv.data();
1060 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barbourf43b6982014-11-25 13:18:32 -07001061 createInfo.flags = 0;
Chia-I Wuc2c31082014-12-29 14:14:03 +08001062 }
Cody Northropa44c2ff2015-04-15 11:19:06 -06001063
Cody Northrop1cfbd172015-06-03 16:49:20 -06001064 err = init_try(*m_device, createInfo);
1065
Cody Northropa44c2ff2015-04-15 11:19:06 -06001066 assert(VK_SUCCESS == err);
Tony Barbour2cb6bf52014-12-03 15:59:38 -07001067}
Tony Barbour408ca622014-12-04 14:33:33 -07001068
Tony Barbour01999182015-04-09 12:58:51 -06001069VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barbourf43b6982014-11-25 13:18:32 -07001070{
Tony Barbourf43b6982014-11-25 13:18:32 -07001071 m_device = device;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001072
1073 m_vi_state.pNext = VK_NULL_HANDLE;
1074 m_vi_state.bindingCount = 0;
1075 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1076 m_vi_state.attributeCount = 0;
1077 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1078
Tony Barbourf43b6982014-11-25 13:18:32 -07001079 m_vertexBufferCount = 0;
1080
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001081 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1082 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbour8205d902015-04-16 15:59:00 -06001083 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001084 m_ia_state.disableVertexReuse = VK_FALSE;
1085 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barbourf43b6982014-11-25 13:18:32 -07001086 m_ia_state.primitiveRestartIndex = 0;
1087
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001088 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001089 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001090 m_rs_state.depthClipEnable = VK_FALSE;
1091 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001092 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1093 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbour8205d902015-04-16 15:59:00 -06001094 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter28846ae2015-04-30 17:44:12 -06001095 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1096 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barbourf43b6982014-11-25 13:18:32 -07001097
Tony Barbourf43b6982014-11-25 13:18:32 -07001098 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001099 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001100 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001101 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1102 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barbourf43b6982014-11-25 13:18:32 -07001103
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001104 m_ms_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001105 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1106 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001107 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1108 m_ms_state.samples = 1;
1109 m_ms_state.minSampleShading = 0;
1110 m_ms_state.sampleShadingEnable = 0;
Tony Barbourf43b6982014-11-25 13:18:32 -07001111
Tony Barbour5e245132015-05-15 09:37:57 -06001112 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001113 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbour5e245132015-05-15 09:37:57 -06001114 m_vp_state.viewportCount = 1;
1115 m_vp_state.depthMode = VK_DEPTH_MODE_ZERO_TO_ONE;
1116 m_vp_state.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1117
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001118 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001119 m_ds_state.pNext = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -06001120 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001121 m_ds_state.depthTestEnable = VK_FALSE;
1122 m_ds_state.depthWriteEnable = VK_FALSE;
1123 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbour8205d902015-04-16 15:59:00 -06001124 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001125 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1126 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1127 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001128 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001129 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001130 m_ds_state.front = m_ds_state.back;
Tony Barbourf43b6982014-11-25 13:18:32 -07001131
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001132 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001133 att.blendEnable = VK_FALSE;
Tony Barbour8205d902015-04-16 15:59:00 -06001134 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001135 att.channelWriteMask = 0xf;
1136 AddColorAttachment(0, &att);
Tony Barbourf43b6982014-11-25 13:18:32 -07001137
1138};
1139
Tony Barbour01999182015-04-09 12:58:51 -06001140void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barbourf43b6982014-11-25 13:18:32 -07001141{
1142 m_shaderObjs.push_back(shader);
1143}
1144
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001145void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barbourf43b6982014-11-25 13:18:32 -07001146{
1147 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1148 m_vi_state.attributeCount = count;
1149}
1150
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001151void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barbourf43b6982014-11-25 13:18:32 -07001152{
1153 m_vi_state.pVertexBindingDescriptions = vi_binding;
1154 m_vi_state.bindingCount = count;
1155}
1156
Tony Barbour01999182015-04-09 12:58:51 -06001157void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barbourf43b6982014-11-25 13:18:32 -07001158{
1159 m_vertexBufferObjs.push_back(vertexDataBuffer);
1160 m_vertexBufferBindings.push_back(binding);
1161 m_vertexBufferCount++;
1162}
1163
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001164void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wue7748802014-12-05 10:45:15 +08001165{
Tony Barbourfa6cac72015-01-16 14:27:35 -07001166 if (binding+1 > m_colorAttachments.size())
1167 {
1168 m_colorAttachments.resize(binding+1);
1169 }
1170 m_colorAttachments[binding] = *att;
1171}
1172
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001173void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourfa6cac72015-01-16 14:27:35 -07001174{
1175 m_ds_state.format = ds_state->format;
1176 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1177 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1178 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbour8205d902015-04-16 15:59:00 -06001179 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001180 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1181 m_ds_state.back = ds_state->back;
1182 m_ds_state.front = ds_state->front;
Chia-I Wue7748802014-12-05 10:45:15 +08001183}
1184
Chris Forbesdc2188c2015-05-25 11:13:26 +12001185VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001186{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001187 VkGraphicsPipelineCreateInfo info = {};
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001188
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001189 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001190
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001191 info.stageCount = m_shaderObjs.size();
1192 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1193
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001194 for (int i=0; i<m_shaderObjs.size(); i++)
1195 {
Chia-I Wuf8385062015-01-04 16:27:24 +08001196 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001197 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001198 }
1199
1200 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1201 {
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001202 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001203 }
1204
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001205 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001206 info.pNext = NULL;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001207 info.flags = 0;
1208 info.layout = descriptorSet.GetPipelineLayout();
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001209
Tony Barbourfa6cac72015-01-16 14:27:35 -07001210 m_cb_state.attachmentCount = m_colorAttachments.size();
1211 m_cb_state.pAttachments = &m_colorAttachments[0];
1212
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001213 info.pTessState = NULL;
1214 info.pVertexInputState = &m_vi_state;
1215 info.pIaState = &m_ia_state;
1216 info.pVpState = &m_vp_state;
1217 info.pRsState = &m_rs_state;
1218 info.pMsState = &m_ms_state;
1219 info.pDsState = &m_ds_state;
1220 info.pCbState = &m_cb_state;
1221
Chris Forbesdc2188c2015-05-25 11:13:26 +12001222 return init_try(*m_device, info);
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001223}
Chia-I Wuf5d81872014-12-29 14:24:14 +08001224
Tony Barbour8205d902015-04-16 15:59:00 -06001225vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001226{
Tony Barbour8205d902015-04-16 15:59:00 -06001227 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001228 if (this->mem_refs_.size()) {
1229 mems.reserve(this->mem_refs_.size());
1230 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1231 mems.push_back(this->mem_refs_[i]);
1232 }
1233
1234 return mems;
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001235}
1236
Tony Barbour01999182015-04-09 12:58:51 -06001237VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001238 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001239{
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001240 m_device = device;
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001241}
Tony Barboure8f14162014-12-10 17:28:39 -07001242
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001243VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001244{
Chia-I Wuae0564f2014-12-28 15:12:48 +08001245 return obj();
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001246}
Tony Barboure8f14162014-12-10 17:28:39 -07001247
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001248VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barboure8f14162014-12-10 17:28:39 -07001249{
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001250 begin(pInfo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001251 return VK_SUCCESS;
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001252}
1253
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001254VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001255{
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001256 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001257 return VK_SUCCESS;
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001258}
1259
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001260VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001261{
1262 begin();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001263 return VK_SUCCESS;
Tony Barboure8f14162014-12-10 17:28:39 -07001264}
1265
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001266VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barboure8f14162014-12-10 17:28:39 -07001267{
Chia-I Wuae0564f2014-12-28 15:12:48 +08001268 end();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001269 return VK_SUCCESS;
Tony Barboure8f14162014-12-10 17:28:39 -07001270}
1271
Tony Barbour8205d902015-04-16 15:59:00 -06001272void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barboure8f14162014-12-10 17:28:39 -07001273{
Tony Barbour8205d902015-04-16 15:59:00 -06001274 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barboure8f14162014-12-10 17:28:39 -07001275}
1276
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001277void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour01999182015-04-09 12:58:51 -06001278 VkDepthStencilObj *depthStencilObj)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001279{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001280 uint32_t i;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001281 const VkFlags output_mask =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001282 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001283 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1284 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1285 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001286 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001287 const VkFlags input_mask = 0;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001288
1289 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001290 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001291 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001292 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterdac96cf2015-06-04 11:35:43 -06001293 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001294 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterdac96cf2015-06-04 11:35:43 -06001295 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001296
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001297 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001298 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyan55658c22014-12-04 11:08:39 +00001299 memory_barrier.outputMask = output_mask;
1300 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001301 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyan55658c22014-12-04 11:08:39 +00001302 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001303 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyan55658c22014-12-04 11:08:39 +00001304
Tony Barbour8205d902015-04-16 15:59:00 -06001305 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyan55658c22014-12-04 11:08:39 +00001306
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -07001307 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyan55658c22014-12-04 11:08:39 +00001308 memory_barrier.image = m_renderTargets[i]->image();
1309 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbour8205d902015-04-16 15:59:00 -06001310 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyan55658c22014-12-04 11:08:39 +00001311 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001312
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001313 vkCmdClearColorImage(obj(),
1314 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterda4a99e2015-04-23 17:49:22 -06001315 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001316
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001317 }
1318
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001319 if (depthStencilObj)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001320 {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001321 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001322 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001323 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchterdac96cf2015-06-04 11:35:43 -06001324 dsRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001325 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchterdac96cf2015-06-04 11:35:43 -06001326 dsRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001327
1328 // prepare the depth buffer for clear
Mike Stroyan55658c22014-12-04 11:08:39 +00001329
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001330 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001331 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001332 memory_barrier.image = depthStencilObj->obj();
Mike Stroyan55658c22014-12-04 11:08:39 +00001333 memory_barrier.subresourceRange = dsRange;
1334
Tony Barbour8205d902015-04-16 15:59:00 -06001335 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001336
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001337 vkCmdClearDepthStencil(obj(),
1338 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter7e305322015-03-05 17:26:38 -07001339 depth_clear_color, stencil_clear_color,
1340 1, &dsRange);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001341
1342 // prepare depth buffer for rendering
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001343 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001344 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001345 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyan55658c22014-12-04 11:08:39 +00001346 memory_barrier.subresourceRange = dsRange;
Tony Barbour8205d902015-04-16 15:59:00 -06001347 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001348 }
1349}
1350
Tony Barbour01999182015-04-09 12:58:51 -06001351void VkCommandBufferObj::PrepareAttachments()
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001352{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001353 uint32_t i;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001354 const VkFlags output_mask =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001355 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001356 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1357 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1358 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001359 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001360 const VkFlags input_mask =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001361 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001362 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1363 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1364 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1365 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1366 VK_MEMORY_INPUT_SHADER_READ_BIT |
1367 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1368 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001369 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyan55658c22014-12-04 11:08:39 +00001370
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001371 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001372 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001373 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterdac96cf2015-06-04 11:35:43 -06001374 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001375 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterdac96cf2015-06-04 11:35:43 -06001376 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001377
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001378 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001379 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyan55658c22014-12-04 11:08:39 +00001380 memory_barrier.outputMask = output_mask;
1381 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001382 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyan55658c22014-12-04 11:08:39 +00001383 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001384 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyan55658c22014-12-04 11:08:39 +00001385
Tony Barbour8205d902015-04-16 15:59:00 -06001386 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyan55658c22014-12-04 11:08:39 +00001387
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -07001388 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001389 {
Mike Stroyan55658c22014-12-04 11:08:39 +00001390 memory_barrier.image = m_renderTargets[i]->image();
1391 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbour8205d902015-04-16 15:59:00 -06001392 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyan55658c22014-12-04 11:08:39 +00001393 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001394 }
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001395}
1396
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001397void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001398{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001399 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001400 renderpass,
1401 framebuffer,
1402 };
1403
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001404 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001405}
1406
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001407void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001408{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001409 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001410}
1411
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001412void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001413{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001414 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001415}
1416
Tony Barbour01999182015-04-09 12:58:51 -06001417void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001418{
1419 m_renderTargets.push_back(renderTarget);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001420}
1421
Tony Barbour01999182015-04-09 12:58:51 -06001422void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001423{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001424 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001425}
1426
Tony Barbour01999182015-04-09 12:58:51 -06001427void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001428{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001429 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001430}
1431
Tony Barbour01999182015-04-09 12:58:51 -06001432void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001433{
Tony Barbour67324f72015-04-08 10:11:29 -06001434 QueueCommandBuffer(NULL);
1435}
1436
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001437void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour67324f72015-04-08 10:11:29 -06001438{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001439 VkResult err = VK_SUCCESS;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001440
1441 // submit the command buffer to the universal queue
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001442 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1443 ASSERT_VK_SUCCESS( err );
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001444
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001445 err = vkQueueWaitIdle( m_device->m_queue );
1446 ASSERT_VK_SUCCESS( err );
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001447
1448 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001449 vkDeviceWaitIdle(m_device->device());
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001450}
1451
Tony Barbour01999182015-04-09 12:58:51 -06001452void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001453{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001454 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001455}
1456
Tony Barbour01999182015-04-09 12:58:51 -06001457void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001458{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001459 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu862c5572015-03-28 15:23:55 +08001460
Chia-I Wu714df452015-01-01 07:55:04 +08001461 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Mark Lobodzinskia65c4632015-06-15 13:21:21 -06001462 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northrop1a01b1d2015-04-16 13:41:56 -06001463 0, 1, &set_obj, 0, NULL );
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001464}
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001465
Tony Barbour01999182015-04-09 12:58:51 -06001466void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001467{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001468 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001469}
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001470
Tony Barbour8205d902015-04-16 15:59:00 -06001471void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001472{
Courtney Goeltzenleuchter46962942015-04-16 13:38:46 -06001473 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001474}
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001475
Tony Barbour01999182015-04-09 12:58:51 -06001476VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour17c6ab12015-03-27 17:03:18 -06001477{
1478 m_initialized = false;
1479}
Tony Barbour01999182015-04-09 12:58:51 -06001480bool VkDepthStencilObj::Initialized()
Tony Barbour17c6ab12015-03-27 17:03:18 -06001481{
1482 return m_initialized;
1483}
1484
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001485VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour17c6ab12015-03-27 17:03:18 -06001486{
1487 return &m_depthStencilBindInfo;
1488}
1489
Tony Barbour01999182015-04-09 12:58:51 -06001490void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour17c6ab12015-03-27 17:03:18 -06001491{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001492 VkImageCreateInfo image_info;
1493 VkDepthStencilViewCreateInfo view_info;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001494
1495 m_device = device;
1496 m_initialized = true;
Tony Barbour8205d902015-04-16 15:59:00 -06001497 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001498
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001499 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001500 image_info.pNext = NULL;
Tony Barbour8205d902015-04-16 15:59:00 -06001501 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001502 image_info.format = m_depth_stencil_fmt;
1503 image_info.extent.width = width;
1504 image_info.extent.height = height;
1505 image_info.extent.depth = 1;
1506 image_info.mipLevels = 1;
1507 image_info.arraySize = 1;
1508 image_info.samples = 1;
Tony Barbour8205d902015-04-16 15:59:00 -06001509 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001510 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001511 image_info.flags = 0;
1512 init(*m_device, image_info);
1513
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001514 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001515 view_info.pNext = NULL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001516 view_info.image = VK_NULL_HANDLE;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001517 view_info.mipLevel = 0;
1518 view_info.baseArraySlice = 0;
1519 view_info.arraySize = 1;
1520 view_info.flags = 0;
1521 view_info.image = obj();
1522 m_depthStencilView.init(*m_device, view_info);
1523
1524 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001525 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001526}