blob: ba19a97226c535b050ee7ac53fff874904302e33 [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Jon Ashburn83a64252015-04-15 11:31:12 -060030#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
31
Tony Barbour6918cd52015-04-09 12:58:51 -060032VkRenderFramework::VkRenderFramework() :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060033 m_cmdBuffer( VK_NULL_HANDLE ),
Tony Barbourf77fd652015-04-09 11:07:02 -060034 m_renderPass(VK_NULL_HANDLE),
35 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060036 m_stateRaster( VK_NULL_HANDLE ),
37 m_colorBlend( VK_NULL_HANDLE ),
38 m_stateViewport( VK_NULL_HANDLE ),
39 m_stateDepthStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060040 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070041 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060042 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
43 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Chris Forbese182fe02015-06-15 09:32:35 +120044 m_clear_via_load_op( false ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070045 m_depth_clear_color( 1.0 ),
Courtney Goeltzenleuchterb6c9aca2015-06-08 16:19:37 -060046 m_stencil_clear_color( 0 ),
47 m_depthStencil( NULL ),
48 m_dbgCreateMsgCallback( VK_NULL_HANDLE ),
49 m_dbgDestroyMsgCallback( VK_NULL_HANDLE ),
50 m_globalMsgCallback( VK_NULL_HANDLE ),
51 m_devMsgCallback( VK_NULL_HANDLE )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060052{
Tony Barbour1c45ce02015-03-27 17:03:18 -060053
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070054 // clear the back buffer to dark grey
55 m_clear_color.color.rawColor[0] = 64;
56 m_clear_color.color.rawColor[1] = 64;
57 m_clear_color.color.rawColor[2] = 64;
58 m_clear_color.color.rawColor[3] = 0;
59 m_clear_color.useRawValue = true;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060060}
61
Tony Barbour6918cd52015-04-09 12:58:51 -060062VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060063{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060064
65}
66
Tony Barbour6918cd52015-04-09 12:58:51 -060067void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060068{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060069 std::vector<const char*> instance_extension_names;
70 std::vector<const char*> device_extension_names;
71 InitFramework(instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060072}
73
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060074void VkRenderFramework::InitFramework(
75 std::vector<const char *> instance_extension_names,
76 std::vector<const char *> device_extension_names,
77 PFN_vkDbgMsgCallback dbgFunction,
78 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060079{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060080 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060081 std::vector<VkExtensionProperties> instance_extensions;
82 std::vector<VkExtensionProperties> device_extensions;
83 uint32_t extCount = 0;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060084 VkResult U_ASSERT_ONLY err;
Tony Barbour59a47322015-06-24 16:06:58 -060085 err = vkGetGlobalExtensionCount(&extCount);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060086 assert(!err);
87
88 VkExtensionProperties extProp;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060089 bool32_t extFound;
90
91 for (uint32_t i = 0; i < instance_extension_names.size(); i++) {
92 extFound = 0;
93 for (uint32_t j = 0; j < extCount; j++) {
Tony Barbour59a47322015-06-24 16:06:58 -060094 err = vkGetGlobalExtensionProperties(j, &extProp);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060095 assert(!err);
96 if (!strcmp(instance_extension_names[i], extProp.name)) {
97 instance_extensions.push_back(extProp);
98 extFound = 1;
99 }
100 }
101 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << instance_extension_names[i] << " which is necessary to pass this test";
102 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600103 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -0600104 instInfo.pNext = NULL;
105 instInfo.pAppInfo = &app_info;
106 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600107 instInfo.extensionCount = instance_extensions.size();
108 instInfo.pEnabledExtensions = (instance_extensions.size()) ? &instance_extensions[0] : NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600109 err = vkCreateInstance(&instInfo, &this->inst);
110 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600111
Jon Ashburn83a64252015-04-15 11:31:12 -0600112 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
113 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
114 ASSERT_VK_SUCCESS(err);
115 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600116 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700117 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600118 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600119 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
120 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
121 if (m_dbgCreateMsgCallback) {
122 err = m_dbgCreateMsgCallback(this->inst,
123 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
124 dbgFunction,
125 userData,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600126 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600127 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600128
129 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
130 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600131 }
Tony Barbour15524c32015-04-29 17:34:29 -0600132 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600133
Tony Barbour59a47322015-06-24 16:06:58 -0600134 err = vkGetPhysicalDeviceExtensionCount(objs[0], &extCount);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600135 assert(!err);
136
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600137 for (uint32_t i = 0; i < device_extension_names.size(); i++) {
138 extFound = 0;
139 for (uint32_t j = 0; j < extCount; j++) {
Tony Barbour59a47322015-06-24 16:06:58 -0600140 err = vkGetPhysicalDeviceExtensionProperties(objs[0], j, &extProp);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600141 assert(!err);
142 if (!strcmp(device_extension_names[i], extProp.name)) {
143 device_extensions.push_back(extProp);
144 extFound = 1;
145 }
146 }
147 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << device_extension_names[i] << " which is necessary to pass this test";
148 }
149 /* TODO: Testing unenabled extension */
150
151// PFN_vkDbgSetObjectName obj_name = (PFN_vkDbgSetObjectName) vkGetInstanceProcAddr(this->inst,
152// "vkDbgSetObjectName");
153// ASSERT_NE(obj_name, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
154// obj_name()
155 m_device = new VkDeviceObj(0, objs[0], device_extensions);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600156
157 /* Now register callback on device */
158 if (0) {
159 if (m_dbgCreateMsgCallback) {
160 err = m_dbgCreateMsgCallback(this->inst,
161 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
162 dbgFunction,
163 userData,
164 &m_devMsgCallback);
165 ASSERT_VK_SUCCESS(err);
166 }
167 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600168 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600169
Tony Barbour6918cd52015-04-09 12:58:51 -0600170 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600171}
172
Tony Barbour6918cd52015-04-09 12:58:51 -0600173void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600174{
Mike Stroyanb050c682015-04-17 12:36:38 -0600175 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
176 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
177 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
178 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
179 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
180 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600181
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600182 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
183 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
184
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600185 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600186 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600187 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600188 while (!m_renderTargets.empty()) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600189 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
Mike Stroyanb050c682015-04-17 12:36:38 -0600190 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
191 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600192 m_renderTargets.pop_back();
193 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600194
Tony Barbour1c45ce02015-03-27 17:03:18 -0600195 delete m_depthStencil;
196
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600197 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800198 delete m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600199 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600200}
201
Tony Barbour6918cd52015-04-09 12:58:51 -0600202void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600203{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600204 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600205
Tony Barbourd1c35722015-04-16 15:59:00 -0600206 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600207
208 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600209 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600210 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700211
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;
Mark Lobodzinski365feea2015-06-12 11:14:17 -0600226 depthStencil.minDepthBounds = 0.f;
227 depthStencil.maxDepthBounds = 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);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600303
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600304 VkFormatProperties props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600305 VkResult err;
306
Chris Forbesbc0bb772015-06-21 22:55:02 +1200307 err = vkGetPhysicalDeviceFormatInfo(m_device->gpu().obj(), m_render_target_fmt, &props);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600308 ASSERT_VK_SUCCESS(err);
309
310 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600311 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600312 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_LINEAR);
313 }
314 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600315 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600316 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL);
317 }
318 else {
319 FAIL() << "Neither Linear nor Optimal allowed for render target";
320 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600321
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600322 m_renderTargets.push_back(img);
Tony Barbourf52346d2015-01-16 14:27:35 -0700323 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600324 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chris Forbese182fe02015-06-15 09:32:35 +1200325 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 -0600326 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600327 clear_colors.push_back(m_clear_color);
Chia-I Wuecebf752014-12-05 10:45:15 +0800328 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600329
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700330 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600331 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600332 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600333 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700334 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600335 fb_info.pColorAttachments = m_colorBindings;
336 fb_info.pDepthStencilAttachment = dsBinding;
337 fb_info.sampleCount = 1;
338 fb_info.width = (uint32_t)m_width;
339 fb_info.height = (uint32_t)m_height;
340 fb_info.layers = 1;
341
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600342 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600343
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600344 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600345 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600346 rp_info.renderArea.extent.width = (uint32_t) m_width;
347 rp_info.renderArea.extent.height = (uint32_t) m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600348
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700349 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600350 rp_info.pColorFormats = &m_render_target_fmt;
351 rp_info.pColorLayouts = &m_colorBindings[0].layout;
352 rp_info.pColorLoadOps = &load_ops[0];
353 rp_info.pColorStoreOps = &store_ops[0];
354 rp_info.pColorLoadClearValues = &clear_colors[0];
355 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600356 if (dsBinding) {
357 rp_info.depthStencilLayout = dsBinding->layout;
358 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600359 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600360 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600361 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
362 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600363 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600364 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Tony Barbour51a05ce2015-06-03 11:40:51 -0600365 rp_info.sampleCount = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600366 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600367}
368
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600369
370
Tony Barbourd1c35722015-04-16 15:59:00 -0600371VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600372 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800373{
374 init();
375
376 props = gpu().properties();
377 queue_props = &gpu().queue_properties()[0];
378}
379
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600380VkDeviceObj::VkDeviceObj(uint32_t id,
381 VkPhysicalDevice obj,
382 std::vector<VkExtensionProperties> extensions) :
383 vk_testing::Device(obj), id(id)
384{
385 init(extensions);
386
387 props = gpu().properties();
388 queue_props = &gpu().queue_properties()[0];
389}
390
Tony Barbour6918cd52015-04-09 12:58:51 -0600391void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800392{
393 ASSERT_NE(true, graphics_queues().empty());
394 m_queue = graphics_queues()[0]->obj();
395}
396
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600397VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
398 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700399{
Tony Barboure2c58df2014-11-25 13:18:32 -0700400
401}
402
Tony Barbour6918cd52015-04-09 12:58:51 -0600403VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700404{
Chia-I Wu11078b02015-01-04 16:27:24 +0800405 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700406}
Tony Barbour82c39522014-12-04 14:33:33 -0700407
Tony Barbour6918cd52015-04-09 12:58:51 -0600408int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700409{
Chia-I Wu11078b02015-01-04 16:27:24 +0800410 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600411 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600412 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800413 tc.count = 1;
414 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700415
Chia-I Wu11078b02015-01-04 16:27:24 +0800416 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700417}
Tony Barbour82c39522014-12-04 14:33:33 -0700418
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600419int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700420{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600421 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800422 tc.type = type;
423 tc.count = 1;
424 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700425
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800426 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
427 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600428
Chia-I Wu11078b02015-01-04 16:27:24 +0800429 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700430}
Tony Barbour82c39522014-12-04 14:33:33 -0700431
Tony Barbour6918cd52015-04-09 12:58:51 -0600432int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700433{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600434 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600435 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800436 tc.count = 1;
437 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700438
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800439 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600440 tmp.sampler = sampler->obj();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800441 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700442
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800443 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
444 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700445
Chia-I Wu11078b02015-01-04 16:27:24 +0800446 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700447}
Chia-I Wu11078b02015-01-04 16:27:24 +0800448
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500449VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700450{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500451 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700452}
453
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600454VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700455{
Chia-I Wu11078b02015-01-04 16:27:24 +0800456 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700457}
Tony Barboure2c58df2014-11-25 13:18:32 -0700458
Tony Barbour6918cd52015-04-09 12:58:51 -0600459void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700460{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600461 // create VkDescriptorPool
462 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600463 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800464 pool.count = m_type_counts.size();
465 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600466 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700467
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600468 // create VkDescriptorSetLayout
469 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800470 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800471 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800472 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800473 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600474 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800475 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700476 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800477
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600478 // create VkDescriptorSetLayout
479 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600480 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800481 layout.count = bindings.size();
482 layout.pBinding = &bindings[0];
483
Chia-I Wu41126e52015-03-26 15:27:55 +0800484 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600485 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800486 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500487
488 // create VkPipelineLayout
489 VkPipelineLayoutCreateInfo pipeline_layout = {};
490 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
491 pipeline_layout.descriptorSetCount = layouts.size();
492 pipeline_layout.pSetLayouts = NULL;
493
494 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700495
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600496 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500497 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800498
Chia-I Wu41126e52015-03-26 15:27:55 +0800499 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800500 size_t imageSamplerCount = 0;
501 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
502 it != m_writes.end(); it++) {
503 it->destSet = m_set->obj();
504 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
505 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800506 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800507
508 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800509 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700510}
Tony Barboure2c58df2014-11-25 13:18:32 -0700511
Tony Barbour6918cd52015-04-09 12:58:51 -0600512VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800513{
514 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800515 m_descriptorInfo.imageView = VK_NULL_HANDLE;
516 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800517}
518
Tony Barbour6918cd52015-04-09 12:58:51 -0600519void VkImageObj::ImageMemoryBarrier(
520 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600521 VkImageAspect aspect,
522 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600523 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600524 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
525 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
526 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
527 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600528 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600529 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600530 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
531 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
532 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
533 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
534 VK_MEMORY_INPUT_SHADER_READ_BIT |
535 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
536 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
537 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600538 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600539{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600540 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
541 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600542 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
543 subresourceRange);
544
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600545 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600546
Tony Barbourd1c35722015-04-16 15:59:00 -0600547 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600548
549 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600550 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 -0600551}
552
Tony Barbour6918cd52015-04-09 12:58:51 -0600553void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600554 VkImageAspect aspect,
555 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600556{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600557 VkFlags output_mask, input_mask;
558 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600559 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600560 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
561 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
562 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600563 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600564 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600565 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600566 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
567 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
568 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
569 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
570 VK_MEMORY_INPUT_SHADER_READ_BIT |
571 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
572 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600573 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600574
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800575 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600576 return;
577 }
578
579 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600580 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600581 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
582 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600583 break;
584
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600585 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600586 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
587 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600588 break;
589
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600590 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600591 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
592 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600593 break;
594
Tony Barbourf20f87b2015-04-22 09:02:32 -0600595 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600596 default:
597 output_mask = all_cache_outputs;
598 input_mask = all_cache_inputs;
599 break;
600 }
601
602 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800603 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600604}
605
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600606void VkImageObj::SetLayout(VkImageAspect aspect,
607 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600608{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600609 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600610
611 if (image_layout == m_descriptorInfo.imageLayout) {
612 return;
613 }
614
Tony Barbour6918cd52015-04-09 12:58:51 -0600615 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600616
617 /* Build command buffer to set image layout in the driver */
618 err = cmd_buf.BeginCommandBuffer();
619 assert(!err);
620
621 SetLayout(&cmd_buf, aspect, image_layout);
622
623 err = cmd_buf.EndCommandBuffer();
624 assert(!err);
625
626 cmd_buf.QueueCommandBuffer();
627}
628
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600629bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600630{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600631 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600632 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600633 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600634
Tony Barbour579f7802015-04-03 15:11:43 -0600635 return true;
636}
637
Tony Barbour6918cd52015-04-09 12:58:51 -0600638void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600639 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600640 VkImageTiling requested_tiling,
641 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800642{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600643 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600644 VkFormatProperties image_fmt;
645 VkImageTiling tiling;
646 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600647
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800648 mipCount = 0;
649
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600650 uint32_t _w = w;
651 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800652 while( ( _w > 0 ) || ( _h > 0 ) )
653 {
654 _w >>= 1;
655 _h >>= 1;
656 mipCount++;
657 }
658
Chris Forbesbc0bb772015-06-21 22:55:02 +1200659 err = vkGetPhysicalDeviceFormatInfo(m_device->gpu().obj(), fmt, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600660 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600661
Tony Barbourd1c35722015-04-16 15:59:00 -0600662 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600663 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600664 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600665 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600666 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600667 } else {
668 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
669 }
670 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600671 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600672 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600673 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600674 } else {
675 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
676 }
677
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600678 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600679 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800680 imageCreateInfo.format = fmt;
681 imageCreateInfo.extent.width = w;
682 imageCreateInfo.extent.height = h;
683 imageCreateInfo.mipLevels = mipCount;
684 imageCreateInfo.tiling = tiling;
685
686 imageCreateInfo.usage = usage;
687
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600688 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800689
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600690 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600691 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600692 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600693 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600694 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800695}
696
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600697VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800698{
699 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600700 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800701}
702
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600703VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800704{
705 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600706 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800707}
708
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600709VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600710{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600711 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600712 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600713 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600714
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600715 /* Build command buffer to copy staging texture to usable texture */
716 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600717 assert(!err);
718
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600719 /* TODO: Can we determine image aspect from image object? */
720 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600721 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600722
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600723 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600724 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600725
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600726 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600727 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600728 copy_region.srcSubresource.arraySlice = 0;
729 copy_region.srcSubresource.mipLevel = 0;
730 copy_region.srcOffset.x = 0;
731 copy_region.srcOffset.y = 0;
732 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600733 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600734 copy_region.destSubresource.arraySlice = 0;
735 copy_region.destSubresource.mipLevel = 0;
736 copy_region.destOffset.x = 0;
737 copy_region.destOffset.y = 0;
738 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600739 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600740
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600741 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600742 src_image.obj(), src_image.layout(),
743 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600744 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600745
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600746 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600747
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600748 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600749
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600750 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600751 assert(!err);
752
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600753 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600754
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600755 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600756}
757
Tony Barbour6918cd52015-04-09 12:58:51 -0600758VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
759 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700760{
761 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600762 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600763 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600764 void *data;
765 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600766 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600767 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600768
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600769 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600770 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600771
772 if (colors == NULL)
773 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700774
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800775 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700776
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600777 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600778 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600779 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600780 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600781 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600782 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600783 view.channels.r = VK_CHANNEL_SWIZZLE_R;
784 view.channels.g = VK_CHANNEL_SWIZZLE_G;
785 view.channels.b = VK_CHANNEL_SWIZZLE_B;
786 view.channels.a = VK_CHANNEL_SWIZZLE_A;
787 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600788 view.subresourceRange.baseMipLevel = 0;
789 view.subresourceRange.mipLevels = 1;
790 view.subresourceRange.baseArraySlice = 0;
791 view.subresourceRange.arraySize = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700792
Tony Barboure2c58df2014-11-25 13:18:32 -0700793 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600794 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700795
796 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800797 view.image = obj();
798 m_textureView.init(*m_device, view);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800799 m_descriptorInfo.imageView = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700800
Tony Barbour5dc515d2015-04-01 17:47:06 -0600801 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700802
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800803 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700804 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800805 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600806 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700807 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600808 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600809 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700810}
Tony Barbour82c39522014-12-04 14:33:33 -0700811
Tony Barbour6918cd52015-04-09 12:58:51 -0600812VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700813{
Tony Barboure2c58df2014-11-25 13:18:32 -0700814 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700815
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600816 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800817 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600818 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
819 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
820 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600821 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600822 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
823 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
824 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800825 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600826 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600827 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800828 samplerCreateInfo.minLod = 0.0;
829 samplerCreateInfo.maxLod = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600830 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700831
Chia-I Wue9864b52014-12-28 16:32:24 +0800832 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700833}
Tony Barboure2c58df2014-11-25 13:18:32 -0700834
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700835/*
836 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
837 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600838VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700839{
840 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700841 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700842
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800843 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700844}
845
Tony Barbour6918cd52015-04-09 12:58:51 -0600846VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600847{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600848 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600849 if (m_commandBuffer) {
850 delete m_commandBuffer;
851 }
852}
853
Tony Barbour6918cd52015-04-09 12:58:51 -0600854VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700855{
Tony Barboure2c58df2014-11-25 13:18:32 -0700856 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800857 m_commandBuffer = 0;
858
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800859 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700860 m_numVertices = constantCount;
861 m_stride = constantSize;
862
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600863 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800864 const size_t allocationSize = constantCount * constantSize;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600865 init(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700866
Chia-I Wua07fee62014-12-28 15:26:08 +0800867 void *pData = map();
868 memcpy(pData, data, allocationSize);
869 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700870
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800871 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600872 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600873 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800874 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600875 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800876 view_info.offset = 0;
877 view_info.range = allocationSize;
878 m_bufferView.init(*m_device, view_info);
879
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800880 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700881}
Tony Barbour82c39522014-12-04 14:33:33 -0700882
Tony Barbourd1c35722015-04-16 15:59:00 -0600883void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700884{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600885 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700886}
887
888
Tony Barbour6918cd52015-04-09 12:58:51 -0600889void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600890 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600891 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600892 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
893 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
894 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
895 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600896 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600897 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600898 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
899 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
900 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
901 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
902 VK_MEMORY_INPUT_SHADER_READ_BIT |
903 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
904 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
905 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700906{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600907 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700908
Tony Barbour38422802014-12-10 14:36:31 -0700909 if (!m_commandBuffer)
910 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600911 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700912
Tony Barbour6918cd52015-04-09 12:58:51 -0600913 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700914 }
915 else
916 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800917 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700918 }
919
Tony Barboure2c58df2014-11-25 13:18:32 -0700920 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600921 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600922 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600923 cmd_buf_info.pNext = NULL;
924 cmd_buf_info.flags = 0;
925
Jon Ashburnc4164b12014-12-31 17:10:47 -0700926 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600927 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700928
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600929 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000930 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600931 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700932
Tony Barbourd1c35722015-04-16 15:59:00 -0600933 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000934
935 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600936 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700937
938 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700939 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600940 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700941
Tony Barboure2c58df2014-11-25 13:18:32 -0700942 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600943 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700944 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600945 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
946 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700947}
948
Tony Barbour6918cd52015-04-09 12:58:51 -0600949VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
950 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700951{
952
953}
954
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600955void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700956{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600957 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700958
959 m_numVertices = numIndexes;
960 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700961 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600962 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700963 m_stride = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -0600964 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700965 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600966 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700967 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600968 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700969 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600970 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700971 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600972 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700973 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800974 default:
975 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600976 m_stride = 2;
977 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800978 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700979 }
980
Chia-I Wua07fee62014-12-28 15:26:08 +0800981 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600982 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
983 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700984
Chia-I Wua07fee62014-12-28 15:26:08 +0800985 void *pData = map();
986 memcpy(pData, data, allocationSize);
987 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700988
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800989 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600990 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600991 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800992 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600993 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700994 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800995 view_info.offset = 0;
996 view_info.range = allocationSize;
997 m_bufferView.init(*m_device, view_info);
998
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800999 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001000}
1001
Tony Barbourd1c35722015-04-16 15:59:00 -06001002void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001003{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001004 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001005}
Tony Barboure2c58df2014-11-25 13:18:32 -07001006
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001007VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001008{
1009 return m_indexType;
1010}
1011
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001012VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001013{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001014 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001015 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1016 stageInfo->stage = m_stage;
1017 stageInfo->shader = obj();
1018 stageInfo->linkConstBufferCount = 0;
1019 stageInfo->pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001020
Tony Barboure2c58df2014-11-25 13:18:32 -07001021 return stageInfo;
1022}
1023
Tony Barbourd1c35722015-04-16 15:59:00 -06001024VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001025{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001026 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001027 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001028 VkShaderCreateInfo createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -07001029 size_t shader_len;
1030
1031 m_stage = stage;
1032 m_device = device;
1033
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001034 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001035 createInfo.pNext = NULL;
1036
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001037 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001038
1039 shader_len = strlen(shader_code);
1040 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1041 createInfo.pCode = malloc(createInfo.codeSize);
1042 createInfo.flags = 0;
1043
Tony Barbourd1c35722015-04-16 15:59:00 -06001044 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001045 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -07001046 ((uint32_t *) createInfo.pCode)[1] = 0;
1047 ((uint32_t *) createInfo.pCode)[2] = stage;
1048 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
1049
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001050 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001051
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001052 // Use Reference GLSL to SPV compiler
1053 framework->GLSLtoSPV(stage, shader_code, spv);
1054 createInfo.pCode = spv.data();
1055 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -07001056 createInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001057 }
Cody Northrop475663c2015-04-15 11:19:06 -06001058
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001059 err = init_try(*m_device, createInfo);
1060
Cody Northrop475663c2015-04-15 11:19:06 -06001061 assert(VK_SUCCESS == err);
Tony Barbourf325bf12014-12-03 15:59:38 -07001062}
Tony Barbour82c39522014-12-04 14:33:33 -07001063
Tony Barbour6918cd52015-04-09 12:58:51 -06001064VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001065{
Tony Barboure2c58df2014-11-25 13:18:32 -07001066 m_device = device;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001067
1068 m_vi_state.pNext = VK_NULL_HANDLE;
1069 m_vi_state.bindingCount = 0;
1070 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1071 m_vi_state.attributeCount = 0;
1072 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1073
Tony Barboure2c58df2014-11-25 13:18:32 -07001074 m_vertexBufferCount = 0;
1075
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001076 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1077 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001078 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001079 m_ia_state.disableVertexReuse = VK_FALSE;
1080 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001081 m_ia_state.primitiveRestartIndex = 0;
1082
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001083 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001084 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001085 m_rs_state.depthClipEnable = VK_FALSE;
1086 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001087 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1088 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001089 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001090 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1091 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001092
Tony Barboure2c58df2014-11-25 13:18:32 -07001093 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001094 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001095 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001096 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1097 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001098
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001099 m_ms_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001100 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1101 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001102 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
Tony Barbourdfd533a2015-06-26 10:18:34 -06001103 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001104 m_ms_state.minSampleShading = 0;
1105 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001106
Tony Barbourd81b8882015-05-15 09:37:57 -06001107 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001108 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001109 m_vp_state.viewportCount = 1;
1110 m_vp_state.depthMode = VK_DEPTH_MODE_ZERO_TO_ONE;
1111 m_vp_state.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1112
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001113 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001114 m_ds_state.pNext = VK_NULL_HANDLE,
Tony Barbourd1c35722015-04-16 15:59:00 -06001115 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001116 m_ds_state.depthTestEnable = VK_FALSE;
1117 m_ds_state.depthWriteEnable = VK_FALSE;
1118 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001119 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001120 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1121 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1122 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001123 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001124 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001125 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001126
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001127 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001128 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001129 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001130 att.channelWriteMask = 0xf;
1131 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001132
1133};
1134
Tony Barbour6918cd52015-04-09 12:58:51 -06001135void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001136{
1137 m_shaderObjs.push_back(shader);
1138}
1139
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001140void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001141{
1142 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1143 m_vi_state.attributeCount = count;
1144}
1145
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001146void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001147{
1148 m_vi_state.pVertexBindingDescriptions = vi_binding;
1149 m_vi_state.bindingCount = count;
1150}
1151
Tony Barbour6918cd52015-04-09 12:58:51 -06001152void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001153{
1154 m_vertexBufferObjs.push_back(vertexDataBuffer);
1155 m_vertexBufferBindings.push_back(binding);
1156 m_vertexBufferCount++;
1157}
1158
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001159void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001160{
Tony Barbourf52346d2015-01-16 14:27:35 -07001161 if (binding+1 > m_colorAttachments.size())
1162 {
1163 m_colorAttachments.resize(binding+1);
1164 }
1165 m_colorAttachments[binding] = *att;
1166}
1167
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001168void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001169{
1170 m_ds_state.format = ds_state->format;
1171 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1172 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1173 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001174 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001175 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1176 m_ds_state.back = ds_state->back;
1177 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001178}
1179
Chris Forbes95292b12015-05-25 11:13:26 +12001180VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001181{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001182 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001183
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001184 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001185
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001186 info.stageCount = m_shaderObjs.size();
1187 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1188
Tony Barbour976e1cf2014-12-17 11:57:31 -07001189 for (int i=0; i<m_shaderObjs.size(); i++)
1190 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001191 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001192 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001193 }
1194
1195 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1196 {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001197 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001198 }
1199
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001200 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001201 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001202 info.flags = 0;
1203 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001204
Tony Barbourf52346d2015-01-16 14:27:35 -07001205 m_cb_state.attachmentCount = m_colorAttachments.size();
1206 m_cb_state.pAttachments = &m_colorAttachments[0];
1207
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001208 info.pTessState = NULL;
1209 info.pVertexInputState = &m_vi_state;
1210 info.pIaState = &m_ia_state;
1211 info.pVpState = &m_vp_state;
1212 info.pRsState = &m_rs_state;
1213 info.pMsState = &m_ms_state;
1214 info.pDsState = &m_ds_state;
1215 info.pCbState = &m_cb_state;
1216
Chris Forbes95292b12015-05-25 11:13:26 +12001217 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001218}
Chia-I Wu2648d092014-12-29 14:24:14 +08001219
Tony Barbourd1c35722015-04-16 15:59:00 -06001220vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001221{
Tony Barbourd1c35722015-04-16 15:59:00 -06001222 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001223 if (this->mem_refs_.size()) {
1224 mems.reserve(this->mem_refs_.size());
1225 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1226 mems.push_back(this->mem_refs_[i]);
1227 }
1228
1229 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001230}
1231
Tony Barbour6918cd52015-04-09 12:58:51 -06001232VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001233 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001234{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001235 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001236}
Tony Barbour471338d2014-12-10 17:28:39 -07001237
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001238VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001239{
Chia-I Wud28343c2014-12-28 15:12:48 +08001240 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001241}
Tony Barbour471338d2014-12-10 17:28:39 -07001242
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001243VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001244{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001245 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001246 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001247}
1248
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001249VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001250{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001251 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001252 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001253}
1254
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001255VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001256{
1257 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001258 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001259}
1260
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001261VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001262{
Chia-I Wud28343c2014-12-28 15:12:48 +08001263 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001264 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001265}
1266
Tony Barbourd1c35722015-04-16 15:59:00 -06001267void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001268{
Tony Barbourd1c35722015-04-16 15:59:00 -06001269 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001270}
1271
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001272void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001273 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001274{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001275 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001276 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001277 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001278 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1279 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1280 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001281 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001282 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001283
1284 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001285 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001286 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001287 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001288 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001289 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001290 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001291
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001292 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001293 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001294 memory_barrier.outputMask = output_mask;
1295 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001296 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001297 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001298 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001299
Tony Barbourd1c35722015-04-16 15:59:00 -06001300 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001301
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001302 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001303 memory_barrier.image = m_renderTargets[i]->image();
1304 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001305 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001306 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001307
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001308 vkCmdClearColorImage(obj(),
1309 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001310 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001311
Tony Barbour30cc9e82014-12-17 11:53:55 -07001312 }
1313
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001314 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001315 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001316 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001317 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001318 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001319 dsRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001320 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001321 dsRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001322
1323 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001324
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001325 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001326 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001327 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001328 memory_barrier.subresourceRange = dsRange;
1329
Tony Barbourd1c35722015-04-16 15:59:00 -06001330 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001331
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001332 vkCmdClearDepthStencil(obj(),
1333 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001334 depth_clear_color, stencil_clear_color,
1335 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001336
1337 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001338 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001339 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001340 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001341 memory_barrier.subresourceRange = dsRange;
Tony Barbourd1c35722015-04-16 15:59:00 -06001342 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001343 }
1344}
1345
Tony Barbour6918cd52015-04-09 12:58:51 -06001346void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001347{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001348 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001349 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001350 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001351 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1352 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1353 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001354 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001355 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001356 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001357 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1358 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1359 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1360 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1361 VK_MEMORY_INPUT_SHADER_READ_BIT |
1362 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1363 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001364 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001365
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001366 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001367 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001368 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001369 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001370 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001371 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001372
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001373 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001374 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001375 memory_barrier.outputMask = output_mask;
1376 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001377 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001378 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001379 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001380
Tony Barbourd1c35722015-04-16 15:59:00 -06001381 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001382
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001383 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001384 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001385 memory_barrier.image = m_renderTargets[i]->image();
1386 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001387 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001388 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001389 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001390}
1391
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001392void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001393{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001394 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001395 renderpass,
1396 framebuffer,
1397 };
1398
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001399 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001400}
1401
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001402void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001403{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001404 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001405}
1406
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001407void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001408{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001409 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001410}
1411
Tony Barbour6918cd52015-04-09 12:58:51 -06001412void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001413{
1414 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001415}
1416
Tony Barbour6918cd52015-04-09 12:58:51 -06001417void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001418{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001419 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001420}
1421
Tony Barbour6918cd52015-04-09 12:58:51 -06001422void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001423{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001424 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001425}
1426
Tony Barbour6918cd52015-04-09 12:58:51 -06001427void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001428{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001429 QueueCommandBuffer(NULL);
1430}
1431
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001432void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001433{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001434 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001435
1436 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001437 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1438 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001439
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001440 err = vkQueueWaitIdle( m_device->m_queue );
1441 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001442
1443 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001444 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001445}
1446
Tony Barbour6918cd52015-04-09 12:58:51 -06001447void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001448{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001449 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001450}
1451
Tony Barbour6918cd52015-04-09 12:58:51 -06001452void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001453{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001454 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001455
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001456 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Mark Lobodzinskif2093b62015-06-15 13:21:21 -06001457 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northropd4c1a502015-04-16 13:41:56 -06001458 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001459}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001460
Tony Barbour6918cd52015-04-09 12:58:51 -06001461void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001462{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001463 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001464}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001465
Tony Barbourd1c35722015-04-16 15:59:00 -06001466void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001467{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001468 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001469}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001470
Tony Barbour6918cd52015-04-09 12:58:51 -06001471VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001472{
1473 m_initialized = false;
1474}
Tony Barbour6918cd52015-04-09 12:58:51 -06001475bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001476{
1477 return m_initialized;
1478}
1479
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001480VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001481{
1482 return &m_depthStencilBindInfo;
1483}
1484
Tony Barbour6918cd52015-04-09 12:58:51 -06001485void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001486{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001487 VkImageCreateInfo image_info;
1488 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001489
1490 m_device = device;
1491 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001492 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001493
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001494 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001495 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001496 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001497 image_info.format = m_depth_stencil_fmt;
1498 image_info.extent.width = width;
1499 image_info.extent.height = height;
1500 image_info.extent.depth = 1;
1501 image_info.mipLevels = 1;
1502 image_info.arraySize = 1;
1503 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001504 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001505 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001506 image_info.flags = 0;
1507 init(*m_device, image_info);
1508
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001509 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001510 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001511 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001512 view_info.mipLevel = 0;
1513 view_info.baseArraySlice = 0;
1514 view_info.arraySize = 1;
1515 view_info.flags = 0;
1516 view_info.image = obj();
1517 m_depthStencilView.init(*m_device, view_info);
1518
1519 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001520 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001521}