blob: 9cf2ba5088ad3b0fb3128bba607fdbba74854ab3 [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Jon Ashburn83a64252015-04-15 11:31:12 -060030#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
31
Tony Barbour6918cd52015-04-09 12:58:51 -060032VkRenderFramework::VkRenderFramework() :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060033 m_cmdBuffer( VK_NULL_HANDLE ),
Tony Barbourf77fd652015-04-09 11:07:02 -060034 m_renderPass(VK_NULL_HANDLE),
35 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060036 m_stateRaster( VK_NULL_HANDLE ),
37 m_colorBlend( VK_NULL_HANDLE ),
38 m_stateViewport( VK_NULL_HANDLE ),
39 m_stateDepthStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060040 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070041 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060042 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
43 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Chris Forbese182fe02015-06-15 09:32:35 +120044 m_clear_via_load_op( false ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070045 m_depth_clear_color( 1.0 ),
46 m_stencil_clear_color( 0 )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060047{
Tony Barbour1c45ce02015-03-27 17:03:18 -060048
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070049 // clear the back buffer to dark grey
50 m_clear_color.color.rawColor[0] = 64;
51 m_clear_color.color.rawColor[1] = 64;
52 m_clear_color.color.rawColor[2] = 64;
53 m_clear_color.color.rawColor[3] = 0;
54 m_clear_color.useRawValue = true;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060055}
56
Tony Barbour6918cd52015-04-09 12:58:51 -060057VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060058{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060059
60}
61
Tony Barbour6918cd52015-04-09 12:58:51 -060062void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060063{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060064 std::vector<const char*> instance_extension_names;
65 std::vector<const char*> device_extension_names;
66 InitFramework(instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060067}
68
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060069void VkRenderFramework::InitFramework(
70 std::vector<const char *> instance_extension_names,
71 std::vector<const char *> device_extension_names,
72 PFN_vkDbgMsgCallback dbgFunction,
73 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060074{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060075 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060076 std::vector<VkExtensionProperties> instance_extensions;
77 std::vector<VkExtensionProperties> device_extensions;
78 uint32_t extCount = 0;
79 size_t extSize = sizeof(extCount);
80 VkResult U_ASSERT_ONLY err;
81 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
82 assert(!err);
83
84 VkExtensionProperties extProp;
85 extSize = sizeof(VkExtensionProperties);
86 bool32_t extFound;
87
88 for (uint32_t i = 0; i < instance_extension_names.size(); i++) {
89 extFound = 0;
90 for (uint32_t j = 0; j < extCount; j++) {
91 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, j, &extSize, &extProp);
92 assert(!err);
93 if (!strcmp(instance_extension_names[i], extProp.name)) {
94 instance_extensions.push_back(extProp);
95 extFound = 1;
96 }
97 }
98 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << instance_extension_names[i] << " which is necessary to pass this test";
99 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600100 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -0600101 instInfo.pNext = NULL;
102 instInfo.pAppInfo = &app_info;
103 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600104 instInfo.extensionCount = instance_extensions.size();
105 instInfo.pEnabledExtensions = (instance_extensions.size()) ? &instance_extensions[0] : NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600106 err = vkCreateInstance(&instInfo, &this->inst);
107 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600108
Jon Ashburn83a64252015-04-15 11:31:12 -0600109 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
110 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
111 ASSERT_VK_SUCCESS(err);
112 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600113 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700114 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600115 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600116 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
117 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
118 if (m_dbgCreateMsgCallback) {
119 err = m_dbgCreateMsgCallback(this->inst,
120 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
121 dbgFunction,
122 userData,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600123 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600124 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600125
126 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
127 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600128 }
Tony Barbour15524c32015-04-29 17:34:29 -0600129 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600130
131 extSize = sizeof(extCount);
132 err = vkGetPhysicalDeviceExtensionInfo(objs[0], VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
133 assert(!err);
134
135 extSize = sizeof(VkExtensionProperties);
136 for (uint32_t i = 0; i < device_extension_names.size(); i++) {
137 extFound = 0;
138 for (uint32_t j = 0; j < extCount; j++) {
139 err = vkGetPhysicalDeviceExtensionInfo(objs[0], VK_EXTENSION_INFO_TYPE_PROPERTIES, j, &extSize, &extProp);
140 assert(!err);
141 if (!strcmp(device_extension_names[i], extProp.name)) {
142 device_extensions.push_back(extProp);
143 extFound = 1;
144 }
145 }
146 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << device_extension_names[i] << " which is necessary to pass this test";
147 }
148 /* TODO: Testing unenabled extension */
149
150// PFN_vkDbgSetObjectName obj_name = (PFN_vkDbgSetObjectName) vkGetInstanceProcAddr(this->inst,
151// "vkDbgSetObjectName");
152// ASSERT_NE(obj_name, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
153// obj_name()
154 m_device = new VkDeviceObj(0, objs[0], device_extensions);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600155
156 /* Now register callback on device */
157 if (0) {
158 if (m_dbgCreateMsgCallback) {
159 err = m_dbgCreateMsgCallback(this->inst,
160 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
161 dbgFunction,
162 userData,
163 &m_devMsgCallback);
164 ASSERT_VK_SUCCESS(err);
165 }
166 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600167 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600168
Tony Barbour6918cd52015-04-09 12:58:51 -0600169 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600170}
171
Tony Barbour6918cd52015-04-09 12:58:51 -0600172void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600173{
Mike Stroyanb050c682015-04-17 12:36:38 -0600174 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
175 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
176 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
177 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
178 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
179 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600180
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600181 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
182 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
183
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600184 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600185 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600186 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600187 while (!m_renderTargets.empty()) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600188 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
Mike Stroyanb050c682015-04-17 12:36:38 -0600189 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
190 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600191 m_renderTargets.pop_back();
192 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600193
Tony Barbour1c45ce02015-03-27 17:03:18 -0600194 delete m_depthStencil;
195
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600196 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800197 delete m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600198 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600199}
200
Tony Barbour6918cd52015-04-09 12:58:51 -0600201void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600202{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600203 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600204
Tony Barbourd1c35722015-04-16 15:59:00 -0600205 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600206
207 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600208 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600209 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700210 raster.pointSize = 1.0;
211
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600212 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
213 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600214
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600215 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600216 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600217 blend.blendConst[0] = 1.0f;
218 blend.blendConst[1] = 1.0f;
219 blend.blendConst[2] = 1.0f;
220 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600221 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
222 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600223
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600224 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600225 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600226 depthStencil.minDepth = 0.f;
227 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700228 depthStencil.stencilFrontRef = 0;
229 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600230 depthStencil.stencilReadMask = 0xff;
231 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600232
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600233 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
234 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600235
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600236 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600237
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600238 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700239 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
240
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600241 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
242 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600243}
244
Tony Barbour6918cd52015-04-09 12:58:51 -0600245void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600246{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600247 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600248
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600249 VkViewport viewport;
250 VkRect scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600251
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600252 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600253 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700254 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700255 viewport.originX = 0;
256 viewport.originY = 0;
257 viewport.width = 1.f * width;
258 viewport.height = 1.f * height;
259 viewport.minDepth = 0.f;
260 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600261 scissor.extent.width = (int32_t) width;
262 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700263 scissor.offset.x = 0;
264 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700265 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700266 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700267
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600268 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
269 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600270 m_width = width;
271 m_height = height;
272}
273
Tony Barbour6918cd52015-04-09 12:58:51 -0600274void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600275{
276 InitViewport(m_width, m_height);
277}
Tony Barbour6918cd52015-04-09 12:58:51 -0600278void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600279{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700280 InitRenderTarget(1);
281}
282
Tony Barbour6918cd52015-04-09 12:58:51 -0600283void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700284{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600285 InitRenderTarget(targets, NULL);
286}
287
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600288void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600289{
290 InitRenderTarget(1, dsBinding);
291}
292
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600293void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600294{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600295 std::vector<VkAttachmentLoadOp> load_ops;
296 std::vector<VkAttachmentStoreOp> store_ops;
297 std::vector<VkClearColor> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600298
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600299 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800300
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700301 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600302 VkImageObj *img = new VkImageObj(m_device);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600303
304 VkFormatProperties props;
305 size_t size = sizeof(props);
306 VkResult err;
307
308 err = vkGetFormatInfo(m_device->obj(), m_render_target_fmt,
309 VK_FORMAT_INFO_TYPE_PROPERTIES,
310 &size, &props);
311 ASSERT_VK_SUCCESS(err);
312
313 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600314 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600315 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_LINEAR);
316 }
317 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600318 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600319 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL);
320 }
321 else {
322 FAIL() << "Neither Linear nor Optimal allowed for render target";
323 }
324
325 m_renderTargets.push_back(img);
Tony Barbourf52346d2015-01-16 14:27:35 -0700326 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600327 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chris Forbese182fe02015-06-15 09:32:35 +1200328 load_ops.push_back(m_clear_via_load_op ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600329 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600330 clear_colors.push_back(m_clear_color);
Chia-I Wuecebf752014-12-05 10:45:15 +0800331 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600332
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700333 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600334 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600335 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600336 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700337 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600338 fb_info.pColorAttachments = m_colorBindings;
339 fb_info.pDepthStencilAttachment = dsBinding;
340 fb_info.sampleCount = 1;
341 fb_info.width = (uint32_t)m_width;
342 fb_info.height = (uint32_t)m_height;
343 fb_info.layers = 1;
344
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600345 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600346
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600347 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600348 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600349 rp_info.renderArea.extent.width = (uint32_t) m_width;
350 rp_info.renderArea.extent.height = (uint32_t) m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600351
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700352 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600353 rp_info.pColorFormats = &m_render_target_fmt;
354 rp_info.pColorLayouts = &m_colorBindings[0].layout;
355 rp_info.pColorLoadOps = &load_ops[0];
356 rp_info.pColorStoreOps = &store_ops[0];
357 rp_info.pColorLoadClearValues = &clear_colors[0];
358 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600359 if (dsBinding) {
360 rp_info.depthStencilLayout = dsBinding->layout;
361 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600362 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600363 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600364 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
365 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600366 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600367 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Tony Barbour51a05ce2015-06-03 11:40:51 -0600368 rp_info.sampleCount = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600369 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600370}
371
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600372
373
Tony Barbourd1c35722015-04-16 15:59:00 -0600374VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600375 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800376{
377 init();
378
379 props = gpu().properties();
380 queue_props = &gpu().queue_properties()[0];
381}
382
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600383VkDeviceObj::VkDeviceObj(uint32_t id,
384 VkPhysicalDevice obj,
385 std::vector<VkExtensionProperties> extensions) :
386 vk_testing::Device(obj), id(id)
387{
388 init(extensions);
389
390 props = gpu().properties();
391 queue_props = &gpu().queue_properties()[0];
392}
393
Tony Barbour6918cd52015-04-09 12:58:51 -0600394void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800395{
396 ASSERT_NE(true, graphics_queues().empty());
397 m_queue = graphics_queues()[0]->obj();
398}
399
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600400VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
401 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700402{
Tony Barboure2c58df2014-11-25 13:18:32 -0700403
404}
405
Tony Barbour6918cd52015-04-09 12:58:51 -0600406VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700407{
Chia-I Wu11078b02015-01-04 16:27:24 +0800408 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700409}
Tony Barbour82c39522014-12-04 14:33:33 -0700410
Tony Barbour6918cd52015-04-09 12:58:51 -0600411int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700412{
Chia-I Wu11078b02015-01-04 16:27:24 +0800413 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600414 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600415 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800416 tc.count = 1;
417 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700418
Chia-I Wu11078b02015-01-04 16:27:24 +0800419 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700420}
Tony Barbour82c39522014-12-04 14:33:33 -0700421
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600422int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700423{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600424 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800425 tc.type = type;
426 tc.count = 1;
427 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700428
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800429 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
430 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600431
Chia-I Wu11078b02015-01-04 16:27:24 +0800432 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700433}
Tony Barbour82c39522014-12-04 14:33:33 -0700434
Tony Barbour6918cd52015-04-09 12:58:51 -0600435int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700436{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600437 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600438 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800439 tc.count = 1;
440 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700441
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800442 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600443 tmp.sampler = sampler->obj();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800444 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700445
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800446 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
447 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700448
Chia-I Wu11078b02015-01-04 16:27:24 +0800449 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700450}
Chia-I Wu11078b02015-01-04 16:27:24 +0800451
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500452VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700453{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500454 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700455}
456
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600457VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700458{
Chia-I Wu11078b02015-01-04 16:27:24 +0800459 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700460}
Tony Barboure2c58df2014-11-25 13:18:32 -0700461
Tony Barbour6918cd52015-04-09 12:58:51 -0600462void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700463{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600464 // create VkDescriptorPool
465 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600466 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800467 pool.count = m_type_counts.size();
468 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600469 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700470
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600471 // create VkDescriptorSetLayout
472 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800473 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800474 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800475 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800476 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600477 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800478 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700479 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800480
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600481 // create VkDescriptorSetLayout
482 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600483 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800484 layout.count = bindings.size();
485 layout.pBinding = &bindings[0];
486
Chia-I Wu41126e52015-03-26 15:27:55 +0800487 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600488 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800489 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500490
491 // create VkPipelineLayout
492 VkPipelineLayoutCreateInfo pipeline_layout = {};
493 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
494 pipeline_layout.descriptorSetCount = layouts.size();
495 pipeline_layout.pSetLayouts = NULL;
496
497 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700498
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600499 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500500 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800501
Chia-I Wu41126e52015-03-26 15:27:55 +0800502 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800503 size_t imageSamplerCount = 0;
504 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
505 it != m_writes.end(); it++) {
506 it->destSet = m_set->obj();
507 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
508 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800509 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800510
511 // do the updates
Chia-I Wu11078b02015-01-04 16:27:24 +0800512 clear_sets(*m_set);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800513 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700514}
Tony Barboure2c58df2014-11-25 13:18:32 -0700515
Tony Barbour6918cd52015-04-09 12:58:51 -0600516VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800517{
518 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800519 m_descriptorInfo.imageView = VK_NULL_HANDLE;
520 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800521}
522
Tony Barbour6918cd52015-04-09 12:58:51 -0600523void VkImageObj::ImageMemoryBarrier(
524 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600525 VkImageAspect aspect,
526 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600527 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-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 Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600532 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600533 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-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 Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600542 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600543{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600544 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
545 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600546 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
547 subresourceRange);
548
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600549 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600550
Tony Barbourd1c35722015-04-16 15:59:00 -0600551 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600552
553 // write barrier to the command buffer
Tony Barbourd1c35722015-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 Goeltzenleuchter97b75232015-04-07 17:13:38 -0600555}
556
Tony Barbour6918cd52015-04-09 12:58:51 -0600557void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600558 VkImageAspect aspect,
559 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600560{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600561 VkFlags output_mask, input_mask;
562 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600563 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-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 Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600567 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600568 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600569 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-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 Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600577 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600578
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800579 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600580 return;
581 }
582
583 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600584 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-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 Goeltzenleuchter97b75232015-04-07 17:13:38 -0600587 break;
588
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600589 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-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 Goeltzenleuchter97b75232015-04-07 17:13:38 -0600592 break;
593
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600594 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-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 Goeltzenleuchter97b75232015-04-07 17:13:38 -0600597 break;
598
Tony Barbourf20f87b2015-04-22 09:02:32 -0600599 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-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 Wu9d00ed72015-05-25 16:27:55 +0800607 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600608}
609
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600610void VkImageObj::SetLayout(VkImageAspect aspect,
611 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600612{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600613 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600614
615 if (image_layout == m_descriptorInfo.imageLayout) {
616 return;
617 }
618
Tony Barbour6918cd52015-04-09 12:58:51 -0600619 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-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 Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600633bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600634{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600635 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600636 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600637 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600638
Tony Barbour579f7802015-04-03 15:11:43 -0600639 return true;
640}
641
Tony Barbour6918cd52015-04-09 12:58:51 -0600642void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600643 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600644 VkImageTiling requested_tiling,
645 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800646{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600647 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600648 VkFormatProperties image_fmt;
649 VkImageTiling tiling;
650 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600651 size_t size;
652
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800653 mipCount = 0;
654
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600655 uint32_t _w = w;
656 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800657 while( ( _w > 0 ) || ( _h > 0 ) )
658 {
659 _w >>= 1;
660 _h >>= 1;
661 mipCount++;
662 }
663
Tony Barbour579f7802015-04-03 15:11:43 -0600664 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600665 err = vkGetFormatInfo(m_device->obj(), fmt,
Tony Barbourd1c35722015-04-16 15:59:00 -0600666 VK_FORMAT_INFO_TYPE_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600667 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600668 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600669
Tony Barbourd1c35722015-04-16 15:59:00 -0600670 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600671 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600672 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600673 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600674 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600675 } else {
676 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
677 }
678 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600679 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600680 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600681 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600682 } else {
683 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
684 }
685
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600686 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600687 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800688 imageCreateInfo.format = fmt;
689 imageCreateInfo.extent.width = w;
690 imageCreateInfo.extent.height = h;
691 imageCreateInfo.mipLevels = mipCount;
692 imageCreateInfo.tiling = tiling;
693
694 imageCreateInfo.usage = usage;
695
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600696 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800697
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600698 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600699 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600700 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600701 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600702 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800703}
704
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600705VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800706{
707 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600708 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800709}
710
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600711VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800712{
713 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600714 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800715}
716
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600717VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600718{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600719 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600720 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600721 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600722
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600723 /* Build command buffer to copy staging texture to usable texture */
724 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600725 assert(!err);
726
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600727 /* TODO: Can we determine image aspect from image object? */
728 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600729 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600730
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600731 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600732 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600733
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600734 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600735 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600736 copy_region.srcSubresource.arraySlice = 0;
737 copy_region.srcSubresource.mipLevel = 0;
738 copy_region.srcOffset.x = 0;
739 copy_region.srcOffset.y = 0;
740 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600741 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600742 copy_region.destSubresource.arraySlice = 0;
743 copy_region.destSubresource.mipLevel = 0;
744 copy_region.destOffset.x = 0;
745 copy_region.destOffset.y = 0;
746 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600747 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600748
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600749 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600750 src_image.obj(), src_image.layout(),
751 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600752 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600753
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600754 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600755
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600756 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600757
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600758 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600759 assert(!err);
760
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600761 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600762
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600763 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600764}
765
Tony Barbour6918cd52015-04-09 12:58:51 -0600766VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
767 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700768{
769 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600770 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600771 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600772 void *data;
773 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600774 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600775 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600776
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600777 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600778 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600779
780 if (colors == NULL)
781 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700782
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800783 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700784
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600785 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600786 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600787 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600788 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600789 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600790 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600791 view.channels.r = VK_CHANNEL_SWIZZLE_R;
792 view.channels.g = VK_CHANNEL_SWIZZLE_G;
793 view.channels.b = VK_CHANNEL_SWIZZLE_B;
794 view.channels.a = VK_CHANNEL_SWIZZLE_A;
795 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600796 view.subresourceRange.baseMipLevel = 0;
797 view.subresourceRange.mipLevels = 1;
798 view.subresourceRange.baseArraySlice = 0;
799 view.subresourceRange.arraySize = 1;
800 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700801
Tony Barboure2c58df2014-11-25 13:18:32 -0700802 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600803 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700804
805 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800806 view.image = obj();
807 m_textureView.init(*m_device, view);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800808 m_descriptorInfo.imageView = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700809
Tony Barbour5dc515d2015-04-01 17:47:06 -0600810 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700811
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800812 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700813 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800814 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600815 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700816 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600817 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600818 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700819}
Tony Barbour82c39522014-12-04 14:33:33 -0700820
Tony Barbour6918cd52015-04-09 12:58:51 -0600821VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700822{
Tony Barboure2c58df2014-11-25 13:18:32 -0700823 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700824
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600825 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800826 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600827 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
828 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
829 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600830 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600831 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
832 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
833 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800834 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600835 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600836 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800837 samplerCreateInfo.minLod = 0.0;
838 samplerCreateInfo.maxLod = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600839 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700840
Chia-I Wue9864b52014-12-28 16:32:24 +0800841 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700842}
Tony Barboure2c58df2014-11-25 13:18:32 -0700843
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700844/*
845 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
846 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600847VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700848{
849 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700850 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700851
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800852 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700853}
854
Tony Barbour6918cd52015-04-09 12:58:51 -0600855VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600856{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600857 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600858 if (m_commandBuffer) {
859 delete m_commandBuffer;
860 }
861}
862
Tony Barbour6918cd52015-04-09 12:58:51 -0600863VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700864{
Tony Barboure2c58df2014-11-25 13:18:32 -0700865 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800866 m_commandBuffer = 0;
867
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800868 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700869 m_numVertices = constantCount;
870 m_stride = constantSize;
871
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600872 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800873 const size_t allocationSize = constantCount * constantSize;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600874 init(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700875
Chia-I Wua07fee62014-12-28 15:26:08 +0800876 void *pData = map();
877 memcpy(pData, data, allocationSize);
878 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700879
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800880 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600881 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600882 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800883 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600884 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800885 view_info.offset = 0;
886 view_info.range = allocationSize;
887 m_bufferView.init(*m_device, view_info);
888
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800889 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700890}
Tony Barbour82c39522014-12-04 14:33:33 -0700891
Tony Barbourd1c35722015-04-16 15:59:00 -0600892void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700893{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600894 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700895}
896
897
Tony Barbour6918cd52015-04-09 12:58:51 -0600898void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600899 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600900 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600901 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
902 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
903 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
904 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600905 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600906 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600907 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
908 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
909 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
910 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
911 VK_MEMORY_INPUT_SHADER_READ_BIT |
912 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
913 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
914 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700915{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600916 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700917
Tony Barbour38422802014-12-10 14:36:31 -0700918 if (!m_commandBuffer)
919 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600920 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700921
Tony Barbour6918cd52015-04-09 12:58:51 -0600922 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700923 }
924 else
925 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800926 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700927 }
928
Tony Barboure2c58df2014-11-25 13:18:32 -0700929 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600930 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600931 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600932 cmd_buf_info.pNext = NULL;
933 cmd_buf_info.flags = 0;
934
Jon Ashburnc4164b12014-12-31 17:10:47 -0700935 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600936 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700937
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600938 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000939 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600940 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700941
Tony Barbourd1c35722015-04-16 15:59:00 -0600942 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000943
944 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600945 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700946
947 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700948 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600949 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700950
Tony Barboure2c58df2014-11-25 13:18:32 -0700951 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600952 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700953 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600954 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
955 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700956}
957
Tony Barbour6918cd52015-04-09 12:58:51 -0600958VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
959 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700960{
961
962}
963
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600964void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700965{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600966 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700967
968 m_numVertices = numIndexes;
969 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700970 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600971 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700972 m_stride = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -0600973 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700974 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600975 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700976 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600977 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700978 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600979 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700980 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600981 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700982 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800983 default:
984 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600985 m_stride = 2;
986 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800987 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700988 }
989
Chia-I Wua07fee62014-12-28 15:26:08 +0800990 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600991 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
992 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700993
Chia-I Wua07fee62014-12-28 15:26:08 +0800994 void *pData = map();
995 memcpy(pData, data, allocationSize);
996 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700997
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800998 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600999 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001000 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001001 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -06001002 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001003 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001004 view_info.offset = 0;
1005 view_info.range = allocationSize;
1006 m_bufferView.init(*m_device, view_info);
1007
Chia-I Wu9d00ed72015-05-25 16:27:55 +08001008 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001009}
1010
Tony Barbourd1c35722015-04-16 15:59:00 -06001011void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001012{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001013 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001014}
Tony Barboure2c58df2014-11-25 13:18:32 -07001015
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001016VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001017{
1018 return m_indexType;
1019}
1020
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001021VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001022{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001023 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001024 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001025 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001026 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -07001027 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001028 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001029
Tony Barboure2c58df2014-11-25 13:18:32 -07001030 return stageInfo;
1031}
1032
Tony Barbourd1c35722015-04-16 15:59:00 -06001033VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001034{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001035 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001036 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001037 VkShaderCreateInfo createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -07001038 size_t shader_len;
1039
1040 m_stage = stage;
1041 m_device = device;
1042
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001043 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001044 createInfo.pNext = NULL;
1045
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001046 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001047
1048 shader_len = strlen(shader_code);
1049 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1050 createInfo.pCode = malloc(createInfo.codeSize);
1051 createInfo.flags = 0;
1052
Tony Barbourd1c35722015-04-16 15:59:00 -06001053 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001054 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -07001055 ((uint32_t *) createInfo.pCode)[1] = 0;
1056 ((uint32_t *) createInfo.pCode)[2] = stage;
1057 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
1058
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001059 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001060
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001061 // Use Reference GLSL to SPV compiler
1062 framework->GLSLtoSPV(stage, shader_code, spv);
1063 createInfo.pCode = spv.data();
1064 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -07001065 createInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001066 }
Cody Northrop475663c2015-04-15 11:19:06 -06001067
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001068 err = init_try(*m_device, createInfo);
1069
Cody Northrop475663c2015-04-15 11:19:06 -06001070 assert(VK_SUCCESS == err);
Tony Barbourf325bf12014-12-03 15:59:38 -07001071}
Tony Barbour82c39522014-12-04 14:33:33 -07001072
Tony Barbour6918cd52015-04-09 12:58:51 -06001073VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001074{
Tony Barboure2c58df2014-11-25 13:18:32 -07001075 m_device = device;
1076 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
1077 m_vertexBufferCount = 0;
1078
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001079 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1080 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001081 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001082 m_ia_state.disableVertexReuse = VK_FALSE;
1083 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001084 m_ia_state.primitiveRestartIndex = 0;
1085
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001086 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001087 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001088 m_rs_state.depthClipEnable = VK_FALSE;
1089 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
1090 m_rs_state.programPointSize = VK_FALSE;
1091 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1092 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001093 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001094 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1095 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001096
Tony Barboure2c58df2014-11-25 13:18:32 -07001097 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001098 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001099 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001100 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1101 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001102
Tony Barbourf52346d2015-01-16 14:27:35 -07001103 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001104 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1105 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001106 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1107 m_ms_state.samples = 1;
1108 m_ms_state.minSampleShading = 0;
1109 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001110
Tony Barbourd81b8882015-05-15 09:37:57 -06001111 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
1112 m_vp_state.pNext = &m_ms_state;
1113 m_vp_state.viewportCount = 1;
1114 m_vp_state.depthMode = VK_DEPTH_MODE_ZERO_TO_ONE;
1115 m_vp_state.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1116
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001117 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -06001118 m_ds_state.pNext = &m_vp_state,
Tony Barbourd1c35722015-04-16 15:59:00 -06001119 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001120 m_ds_state.depthTestEnable = VK_FALSE;
1121 m_ds_state.depthWriteEnable = VK_FALSE;
1122 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001123 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001124 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1125 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1126 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001127 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001128 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001129 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001130
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001131 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001132 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001133 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001134 att.channelWriteMask = 0xf;
1135 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001136
1137};
1138
Tony Barbour6918cd52015-04-09 12:58:51 -06001139void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001140{
1141 m_shaderObjs.push_back(shader);
1142}
1143
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001144void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001145{
1146 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1147 m_vi_state.attributeCount = count;
1148}
1149
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001150void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001151{
1152 m_vi_state.pVertexBindingDescriptions = vi_binding;
1153 m_vi_state.bindingCount = count;
1154}
1155
Tony Barbour6918cd52015-04-09 12:58:51 -06001156void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001157{
1158 m_vertexBufferObjs.push_back(vertexDataBuffer);
1159 m_vertexBufferBindings.push_back(binding);
1160 m_vertexBufferCount++;
1161}
1162
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001163void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001164{
Tony Barbourf52346d2015-01-16 14:27:35 -07001165 if (binding+1 > m_colorAttachments.size())
1166 {
1167 m_colorAttachments.resize(binding+1);
1168 }
1169 m_colorAttachments[binding] = *att;
1170}
1171
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001172void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001173{
1174 m_ds_state.format = ds_state->format;
1175 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1176 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1177 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001178 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001179 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1180 m_ds_state.back = ds_state->back;
1181 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001182}
1183
Chris Forbes95292b12015-05-25 11:13:26 +12001184VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001185{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001186 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001187 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001188
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001189 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001190
1191 for (int i=0; i<m_shaderObjs.size(); i++)
1192 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001193 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001194 shaderCreateInfo->pNext = head_ptr;
1195 head_ptr = shaderCreateInfo;
1196 }
1197
1198 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1199 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001200 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001201 m_vi_state.pNext = head_ptr;
1202 head_ptr = &m_vi_state;
1203 }
1204
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001205 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1206 info.pNext = head_ptr;
1207 info.flags = 0;
1208 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001209
Tony Barbourf52346d2015-01-16 14:27:35 -07001210 m_cb_state.attachmentCount = m_colorAttachments.size();
1211 m_cb_state.pAttachments = &m_colorAttachments[0];
1212
Chris Forbes95292b12015-05-25 11:13:26 +12001213 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001214}
Chia-I Wu2648d092014-12-29 14:24:14 +08001215
Tony Barbourd1c35722015-04-16 15:59:00 -06001216vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001217{
Tony Barbourd1c35722015-04-16 15:59:00 -06001218 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001219 if (this->mem_refs_.size()) {
1220 mems.reserve(this->mem_refs_.size());
1221 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1222 mems.push_back(this->mem_refs_[i]);
1223 }
1224
1225 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001226}
1227
Tony Barbour6918cd52015-04-09 12:58:51 -06001228VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001229 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001230{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001231 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001232}
Tony Barbour471338d2014-12-10 17:28:39 -07001233
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001234VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001235{
Chia-I Wud28343c2014-12-28 15:12:48 +08001236 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001237}
Tony Barbour471338d2014-12-10 17:28:39 -07001238
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001239VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001240{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001241 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001242 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001243}
1244
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001245VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001246{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001247 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001248 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001249}
1250
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001251VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001252{
1253 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001254 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001255}
1256
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001257VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001258{
Chia-I Wud28343c2014-12-28 15:12:48 +08001259 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001260 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001261}
1262
Tony Barbourd1c35722015-04-16 15:59:00 -06001263void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001264{
Tony Barbourd1c35722015-04-16 15:59:00 -06001265 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001266}
1267
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001268void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001269 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001270{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001271 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001272 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001273 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001274 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1275 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1276 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001277 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001278 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001279
1280 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001281 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001282 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001283 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001284 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001285 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001286 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001287
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001288 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001289 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001290 memory_barrier.outputMask = output_mask;
1291 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001292 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001293 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001294 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001295
Tony Barbourd1c35722015-04-16 15:59:00 -06001296 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001297
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001298 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001299 memory_barrier.image = m_renderTargets[i]->image();
1300 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001301 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001302 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001303
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001304 vkCmdClearColorImage(obj(),
1305 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001306 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001307
Tony Barbour30cc9e82014-12-17 11:53:55 -07001308 }
1309
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001310 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001311 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001312 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001313 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001314 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001315 dsRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001316 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001317 dsRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001318
1319 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001320
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001321 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001322 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001323 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001324 memory_barrier.subresourceRange = dsRange;
1325
Tony Barbourd1c35722015-04-16 15:59:00 -06001326 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001327
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001328 vkCmdClearDepthStencil(obj(),
1329 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001330 depth_clear_color, stencil_clear_color,
1331 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001332
1333 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001334 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001335 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001336 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001337 memory_barrier.subresourceRange = dsRange;
Tony Barbourd1c35722015-04-16 15:59:00 -06001338 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001339 }
1340}
1341
Tony Barbour6918cd52015-04-09 12:58:51 -06001342void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001343{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001344 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001345 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001346 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001347 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1348 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1349 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001350 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001351 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001352 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001353 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1354 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1355 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1356 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1357 VK_MEMORY_INPUT_SHADER_READ_BIT |
1358 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1359 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001360 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001361
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001362 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001363 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001364 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001365 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001366 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001367 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001368
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001369 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001370 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001371 memory_barrier.outputMask = output_mask;
1372 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001373 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001374 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001375 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001376
Tony Barbourd1c35722015-04-16 15:59:00 -06001377 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001378
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001379 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001380 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001381 memory_barrier.image = m_renderTargets[i]->image();
1382 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001383 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001384 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001385 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001386}
1387
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001388void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001389{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001390 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001391 renderpass,
1392 framebuffer,
1393 };
1394
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001395 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001396}
1397
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001398void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001399{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001400 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001401}
1402
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001403void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001404{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001405 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001406}
1407
Tony Barbour6918cd52015-04-09 12:58:51 -06001408void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001409{
1410 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001411}
1412
Tony Barbour6918cd52015-04-09 12:58:51 -06001413void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001414{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001415 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001416}
1417
Tony Barbour6918cd52015-04-09 12:58:51 -06001418void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001419{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001420 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001421}
1422
Tony Barbour6918cd52015-04-09 12:58:51 -06001423void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001424{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001425 QueueCommandBuffer(NULL);
1426}
1427
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001428void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001429{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001430 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001431
1432 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001433 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1434 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001435
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001436 err = vkQueueWaitIdle( m_device->m_queue );
1437 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001438
1439 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001440 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001441}
1442
Tony Barbour6918cd52015-04-09 12:58:51 -06001443void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001444{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001445 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001446}
1447
Tony Barbour6918cd52015-04-09 12:58:51 -06001448void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001449{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001450 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001451
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001452 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001453 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropd4c1a502015-04-16 13:41:56 -06001454 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001455}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001456
Tony Barbour6918cd52015-04-09 12:58:51 -06001457void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001458{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001459 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001460}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001461
Tony Barbourd1c35722015-04-16 15:59:00 -06001462void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001463{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001464 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001465}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001466
Tony Barbour6918cd52015-04-09 12:58:51 -06001467VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001468{
1469 m_initialized = false;
1470}
Tony Barbour6918cd52015-04-09 12:58:51 -06001471bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001472{
1473 return m_initialized;
1474}
1475
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001476VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001477{
1478 return &m_depthStencilBindInfo;
1479}
1480
Tony Barbour6918cd52015-04-09 12:58:51 -06001481void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001482{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001483 VkImageCreateInfo image_info;
1484 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001485
1486 m_device = device;
1487 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001488 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001489
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001490 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001491 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001492 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001493 image_info.format = m_depth_stencil_fmt;
1494 image_info.extent.width = width;
1495 image_info.extent.height = height;
1496 image_info.extent.depth = 1;
1497 image_info.mipLevels = 1;
1498 image_info.arraySize = 1;
1499 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001500 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001501 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001502 image_info.flags = 0;
1503 init(*m_device, image_info);
1504
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001505 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001506 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001507 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001508 view_info.mipLevel = 0;
1509 view_info.baseArraySlice = 0;
1510 view_info.arraySize = 1;
1511 view_info.flags = 0;
1512 view_info.image = obj();
1513 m_depthStencilView.init(*m_device, view_info);
1514
1515 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001516 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001517}