blob: d6b7ab5a0eca9f66a09372d2a1f89e5055560a21 [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
Chris Forbesf0796e12015-06-24 14:34:53 +120055 m_clear_color.f32[0] = 0.25f;
56 m_clear_color.f32[1] = 0.25f;
57 m_clear_color.f32[2] = 0.25f;
58 m_clear_color.f32[3] = 0.0f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060059}
60
Tony Barbour6918cd52015-04-09 12:58:51 -060061VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060062{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060063
64}
65
Tony Barbour6918cd52015-04-09 12:58:51 -060066void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060067{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060068 std::vector<const char*> instance_extension_names;
69 std::vector<const char*> device_extension_names;
70 InitFramework(instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060071}
72
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060073void VkRenderFramework::InitFramework(
74 std::vector<const char *> instance_extension_names,
75 std::vector<const char *> device_extension_names,
76 PFN_vkDbgMsgCallback dbgFunction,
77 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060078{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060079 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060080 std::vector<VkExtensionProperties> instance_extensions;
81 std::vector<VkExtensionProperties> device_extensions;
82 uint32_t extCount = 0;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060083 VkResult U_ASSERT_ONLY err;
Tony Barbour59a47322015-06-24 16:06:58 -060084 err = vkGetGlobalExtensionCount(&extCount);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060085 assert(!err);
86
87 VkExtensionProperties extProp;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060088 bool32_t extFound;
89
90 for (uint32_t i = 0; i < instance_extension_names.size(); i++) {
91 extFound = 0;
92 for (uint32_t j = 0; j < extCount; j++) {
Tony Barbour59a47322015-06-24 16:06:58 -060093 err = vkGetGlobalExtensionProperties(j, &extProp);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060094 assert(!err);
95 if (!strcmp(instance_extension_names[i], extProp.name)) {
96 instance_extensions.push_back(extProp);
97 extFound = 1;
98 }
99 }
100 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << instance_extension_names[i] << " which is necessary to pass this test";
101 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600102 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -0600103 instInfo.pNext = NULL;
104 instInfo.pAppInfo = &app_info;
105 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600106 instInfo.extensionCount = instance_extensions.size();
107 instInfo.pEnabledExtensions = (instance_extensions.size()) ? &instance_extensions[0] : NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600108 err = vkCreateInstance(&instInfo, &this->inst);
109 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600110
Jon Ashburn83a64252015-04-15 11:31:12 -0600111 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
112 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
113 ASSERT_VK_SUCCESS(err);
114 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600115 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700116 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600117 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600118 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
119 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
120 if (m_dbgCreateMsgCallback) {
121 err = m_dbgCreateMsgCallback(this->inst,
122 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
123 dbgFunction,
124 userData,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600125 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600126 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600127
128 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
129 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600130 }
Tony Barbour15524c32015-04-29 17:34:29 -0600131 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600132
Tony Barbour59a47322015-06-24 16:06:58 -0600133 err = vkGetPhysicalDeviceExtensionCount(objs[0], &extCount);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600134 assert(!err);
135
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600136 for (uint32_t i = 0; i < device_extension_names.size(); i++) {
137 extFound = 0;
138 for (uint32_t j = 0; j < extCount; j++) {
Tony Barbour59a47322015-06-24 16:06:58 -0600139 err = vkGetPhysicalDeviceExtensionProperties(objs[0], j, &extProp);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600140 assert(!err);
141 if (!strcmp(device_extension_names[i], extProp.name)) {
142 device_extensions.push_back(extProp);
143 extFound = 1;
144 }
145 }
146 ASSERT_EQ(extFound, 1) << "ERROR: Cannot find extension named " << device_extension_names[i] << " which is necessary to pass this test";
147 }
148 /* TODO: Testing unenabled extension */
149
150// PFN_vkDbgSetObjectName obj_name = (PFN_vkDbgSetObjectName) vkGetInstanceProcAddr(this->inst,
151// "vkDbgSetObjectName");
152// ASSERT_NE(obj_name, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
153// obj_name()
154 m_device = new VkDeviceObj(0, objs[0], device_extensions);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600155
156 /* Now register callback on device */
157 if (0) {
158 if (m_dbgCreateMsgCallback) {
159 err = m_dbgCreateMsgCallback(this->inst,
160 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
161 dbgFunction,
162 userData,
163 &m_devMsgCallback);
164 ASSERT_VK_SUCCESS(err);
165 }
166 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600167 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600168
Tony Barbour6918cd52015-04-09 12:58:51 -0600169 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600170}
171
Tony Barbour6918cd52015-04-09 12:58:51 -0600172void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600173{
Mike Stroyanb050c682015-04-17 12:36:38 -0600174 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
175 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
176 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
177 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
178 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
179 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600180
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600181 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
182 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
183
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600184 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600185 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600186 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600187 while (!m_renderTargets.empty()) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600188 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
Mike Stroyanb050c682015-04-17 12:36:38 -0600189 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
190 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600191 m_renderTargets.pop_back();
192 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600193
Tony Barbour1c45ce02015-03-27 17:03:18 -0600194 delete m_depthStencil;
195
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600196 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800197 delete m_device;
Chris Forbesbe2fae62015-07-07 11:02:22 +1200198 if (this->inst) vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600199}
200
Tony Barbour6918cd52015-04-09 12:58:51 -0600201void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600202{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600203 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600204
Tony Barbourd1c35722015-04-16 15:59:00 -0600205 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600206
207 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600208 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600209 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700210
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600211 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
212 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600213
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600214 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600215 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600216 blend.blendConst[0] = 1.0f;
217 blend.blendConst[1] = 1.0f;
218 blend.blendConst[2] = 1.0f;
219 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600220 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
221 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600222
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600223 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600224 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski365feea2015-06-12 11:14:17 -0600225 depthStencil.minDepthBounds = 0.f;
226 depthStencil.maxDepthBounds = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700227 depthStencil.stencilFrontRef = 0;
228 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600229 depthStencil.stencilReadMask = 0xff;
230 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600231
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600232 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
233 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600234
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600235 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600236
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600237 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700238 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
239
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600240 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
241 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600242}
243
Tony Barbour6918cd52015-04-09 12:58:51 -0600244void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600245{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600246 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600247
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600248 VkViewport viewport;
Chris Forbesd9be82b2015-06-22 17:21:59 +1200249 VkRect2D scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600250
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600251 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600252 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700253 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700254 viewport.originX = 0;
255 viewport.originY = 0;
256 viewport.width = 1.f * width;
257 viewport.height = 1.f * height;
258 viewport.minDepth = 0.f;
259 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600260 scissor.extent.width = (int32_t) width;
261 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700262 scissor.offset.x = 0;
263 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700264 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700265 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700266
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600267 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
268 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600269 m_width = width;
270 m_height = height;
271}
272
Tony Barbour6918cd52015-04-09 12:58:51 -0600273void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600274{
275 InitViewport(m_width, m_height);
276}
Tony Barbour6918cd52015-04-09 12:58:51 -0600277void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600278{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700279 InitRenderTarget(1);
280}
281
Tony Barbour6918cd52015-04-09 12:58:51 -0600282void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700283{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600284 InitRenderTarget(targets, NULL);
285}
286
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600287void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600288{
289 InitRenderTarget(1, dsBinding);
290}
291
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600292void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600293{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600294 std::vector<VkAttachmentLoadOp> load_ops;
295 std::vector<VkAttachmentStoreOp> store_ops;
Chris Forbesf0796e12015-06-24 14:34:53 +1200296 std::vector<VkClearColorValue> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600297
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600298 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800299
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700300 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600301 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600302
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600303 VkFormatProperties props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600304 VkResult err;
305
Chris Forbesbc0bb772015-06-21 22:55:02 +1200306 err = vkGetPhysicalDeviceFormatInfo(m_device->gpu().obj(), m_render_target_fmt, &props);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600307 ASSERT_VK_SUCCESS(err);
308
309 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600310 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600311 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_LINEAR);
312 }
313 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600314 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600315 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL);
316 }
317 else {
318 FAIL() << "Neither Linear nor Optimal allowed for render target";
319 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600320
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600321 m_renderTargets.push_back(img);
Tony Barbourf52346d2015-01-16 14:27:35 -0700322 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600323 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chris Forbese182fe02015-06-15 09:32:35 +1200324 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 -0600325 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600326 clear_colors.push_back(m_clear_color);
Chia-I Wuecebf752014-12-05 10:45:15 +0800327 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600328
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700329 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600330 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600331 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600332 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700333 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600334 fb_info.pColorAttachments = m_colorBindings;
335 fb_info.pDepthStencilAttachment = dsBinding;
336 fb_info.sampleCount = 1;
337 fb_info.width = (uint32_t)m_width;
338 fb_info.height = (uint32_t)m_height;
339 fb_info.layers = 1;
340
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600341 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600342
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600343 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600344 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600345 rp_info.renderArea.extent.width = (uint32_t) m_width;
346 rp_info.renderArea.extent.height = (uint32_t) m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600347
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700348 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600349 rp_info.pColorFormats = &m_render_target_fmt;
350 rp_info.pColorLayouts = &m_colorBindings[0].layout;
351 rp_info.pColorLoadOps = &load_ops[0];
352 rp_info.pColorStoreOps = &store_ops[0];
353 rp_info.pColorLoadClearValues = &clear_colors[0];
354 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600355 if (dsBinding) {
356 rp_info.depthStencilLayout = dsBinding->layout;
357 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600358 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600359 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600360 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
361 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600362 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600363 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Tony Barbour51a05ce2015-06-03 11:40:51 -0600364 rp_info.sampleCount = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600365 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600366}
367
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600368
369
Tony Barbourd1c35722015-04-16 15:59:00 -0600370VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600371 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800372{
373 init();
374
375 props = gpu().properties();
376 queue_props = &gpu().queue_properties()[0];
377}
378
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600379VkDeviceObj::VkDeviceObj(uint32_t id,
380 VkPhysicalDevice obj,
381 std::vector<VkExtensionProperties> extensions) :
382 vk_testing::Device(obj), id(id)
383{
384 init(extensions);
385
386 props = gpu().properties();
387 queue_props = &gpu().queue_properties()[0];
388}
389
Tony Barbour6918cd52015-04-09 12:58:51 -0600390void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800391{
392 ASSERT_NE(true, graphics_queues().empty());
393 m_queue = graphics_queues()[0]->obj();
394}
395
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600396VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
397 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700398{
Tony Barboure2c58df2014-11-25 13:18:32 -0700399
400}
401
Tony Barbour6918cd52015-04-09 12:58:51 -0600402VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700403{
Chia-I Wu11078b02015-01-04 16:27:24 +0800404 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700405}
Tony Barbour82c39522014-12-04 14:33:33 -0700406
Tony Barbour6918cd52015-04-09 12:58:51 -0600407int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700408{
Chia-I Wu11078b02015-01-04 16:27:24 +0800409 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600410 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600411 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800412 tc.count = 1;
413 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700414
Chia-I Wu11078b02015-01-04 16:27:24 +0800415 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700416}
Tony Barbour82c39522014-12-04 14:33:33 -0700417
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600418int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700419{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600420 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800421 tc.type = type;
422 tc.count = 1;
423 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700424
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800425 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
426 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600427
Chia-I Wu11078b02015-01-04 16:27:24 +0800428 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700429}
Tony Barbour82c39522014-12-04 14:33:33 -0700430
Tony Barbour6918cd52015-04-09 12:58:51 -0600431int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700432{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600433 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600434 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800435 tc.count = 1;
436 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700437
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800438 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600439 tmp.sampler = sampler->obj();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800440 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700441
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800442 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
443 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700444
Chia-I Wu11078b02015-01-04 16:27:24 +0800445 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700446}
Chia-I Wu11078b02015-01-04 16:27:24 +0800447
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500448VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700449{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500450 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700451}
452
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600453VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700454{
Chia-I Wu11078b02015-01-04 16:27:24 +0800455 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700456}
Tony Barboure2c58df2014-11-25 13:18:32 -0700457
Tony Barbour6918cd52015-04-09 12:58:51 -0600458void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700459{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600460 // create VkDescriptorPool
461 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600462 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800463 pool.count = m_type_counts.size();
464 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600465 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700466
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600467 // create VkDescriptorSetLayout
468 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800469 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800470 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800471 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800472 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600473 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800474 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700475 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800476
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600477 // create VkDescriptorSetLayout
478 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600479 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800480 layout.count = bindings.size();
481 layout.pBinding = &bindings[0];
482
Chia-I Wu41126e52015-03-26 15:27:55 +0800483 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600484 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800485 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500486
487 // create VkPipelineLayout
488 VkPipelineLayoutCreateInfo pipeline_layout = {};
489 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
490 pipeline_layout.descriptorSetCount = layouts.size();
491 pipeline_layout.pSetLayouts = NULL;
492
493 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700494
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600495 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500496 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800497
Chia-I Wu41126e52015-03-26 15:27:55 +0800498 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800499 size_t imageSamplerCount = 0;
500 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
501 it != m_writes.end(); it++) {
502 it->destSet = m_set->obj();
503 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
504 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800505 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800506
507 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800508 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700509}
Tony Barboure2c58df2014-11-25 13:18:32 -0700510
Tony Barbour6918cd52015-04-09 12:58:51 -0600511VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800512{
513 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800514 m_descriptorInfo.imageView = VK_NULL_HANDLE;
515 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800516}
517
Tony Barbour6918cd52015-04-09 12:58:51 -0600518void VkImageObj::ImageMemoryBarrier(
519 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600520 VkImageAspect aspect,
521 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600522 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600523 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
524 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
525 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
526 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600527 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600528 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600529 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
530 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
531 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
532 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
533 VK_MEMORY_INPUT_SHADER_READ_BIT |
534 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
535 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
536 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600537 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600538{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600539 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
540 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600541 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
542 subresourceRange);
543
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600544 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600545
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600546 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
547 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600548
549 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600550 vkCmdPipelineBarrier(cmd_buf->obj(), src_stages, dest_stages, false, 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
595 default:
596 output_mask = all_cache_outputs;
597 input_mask = all_cache_inputs;
598 break;
599 }
600
601 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800602 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600603}
604
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600605void VkImageObj::SetLayout(VkImageAspect aspect,
606 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600607{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600608 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600609
610 if (image_layout == m_descriptorInfo.imageLayout) {
611 return;
612 }
613
Tony Barbour6918cd52015-04-09 12:58:51 -0600614 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600615
616 /* Build command buffer to set image layout in the driver */
617 err = cmd_buf.BeginCommandBuffer();
618 assert(!err);
619
620 SetLayout(&cmd_buf, aspect, image_layout);
621
622 err = cmd_buf.EndCommandBuffer();
623 assert(!err);
624
625 cmd_buf.QueueCommandBuffer();
626}
627
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600628bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600629{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600630 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600631 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600632 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600633
Tony Barbour579f7802015-04-03 15:11:43 -0600634 return true;
635}
636
Tony Barbour6918cd52015-04-09 12:58:51 -0600637void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600638 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600639 VkImageTiling requested_tiling,
640 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800641{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600642 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600643 VkFormatProperties image_fmt;
644 VkImageTiling tiling;
645 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600646
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800647 mipCount = 0;
648
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600649 uint32_t _w = w;
650 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800651 while( ( _w > 0 ) || ( _h > 0 ) )
652 {
653 _w >>= 1;
654 _h >>= 1;
655 mipCount++;
656 }
657
Chris Forbesbc0bb772015-06-21 22:55:02 +1200658 err = vkGetPhysicalDeviceFormatInfo(m_device->gpu().obj(), fmt, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600659 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600660
Tony Barbourd1c35722015-04-16 15:59:00 -0600661 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600662 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600663 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600664 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600665 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600666 } else {
667 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
668 }
669 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600670 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600671 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600672 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600673 } else {
674 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
675 }
676
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600677 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600678 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800679 imageCreateInfo.format = fmt;
680 imageCreateInfo.extent.width = w;
681 imageCreateInfo.extent.height = h;
682 imageCreateInfo.mipLevels = mipCount;
683 imageCreateInfo.tiling = tiling;
684
685 imageCreateInfo.usage = usage;
686
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600687 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800688
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600689 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600690 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600691 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600692 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600693 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800694}
695
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600696VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800697{
698 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600699 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800700}
701
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600702VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800703{
704 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600705 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800706}
707
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600708VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600709{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600710 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600711 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600712 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600713
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600714 /* Build command buffer to copy staging texture to usable texture */
715 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600716 assert(!err);
717
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600718 /* TODO: Can we determine image aspect from image object? */
719 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600720 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600721
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600722 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600723 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600724
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600725 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600726 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600727 copy_region.srcSubresource.arraySlice = 0;
728 copy_region.srcSubresource.mipLevel = 0;
729 copy_region.srcOffset.x = 0;
730 copy_region.srcOffset.y = 0;
731 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600732 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600733 copy_region.destSubresource.arraySlice = 0;
734 copy_region.destSubresource.mipLevel = 0;
735 copy_region.destOffset.x = 0;
736 copy_region.destOffset.y = 0;
737 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600738 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600739
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600740 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600741 src_image.obj(), src_image.layout(),
742 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600743 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600744
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600745 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600746
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600747 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600748
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600749 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600750 assert(!err);
751
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600752 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600753
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600754 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600755}
756
Tony Barbour6918cd52015-04-09 12:58:51 -0600757VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
758 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700759{
760 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600761 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600762 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600763 void *data;
764 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600765 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600766 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600767
Cody Northropbb6fdb32015-06-17 08:28:19 -0600768 stagingImage.init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600769 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600770
771 if (colors == NULL)
772 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700773
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800774 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700775
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600776 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600777 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600778 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600779 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600780 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600781 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600782 view.channels.r = VK_CHANNEL_SWIZZLE_R;
783 view.channels.g = VK_CHANNEL_SWIZZLE_G;
784 view.channels.b = VK_CHANNEL_SWIZZLE_B;
785 view.channels.a = VK_CHANNEL_SWIZZLE_A;
786 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600787 view.subresourceRange.baseMipLevel = 0;
788 view.subresourceRange.mipLevels = 1;
789 view.subresourceRange.baseArraySlice = 0;
790 view.subresourceRange.arraySize = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700791
Tony Barboure2c58df2014-11-25 13:18:32 -0700792 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600793 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700794
795 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800796 view.image = obj();
797 m_textureView.init(*m_device, view);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800798 m_descriptorInfo.imageView = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700799
Tony Barbour5dc515d2015-04-01 17:47:06 -0600800 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700801
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800802 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700803 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800804 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600805 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700806 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600807 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600808 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700809}
Tony Barbour82c39522014-12-04 14:33:33 -0700810
Tony Barbour6918cd52015-04-09 12:58:51 -0600811VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700812{
Tony Barboure2c58df2014-11-25 13:18:32 -0700813 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700814
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600815 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800816 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600817 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
818 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
819 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600820 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600821 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
822 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
823 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800824 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600825 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600826 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800827 samplerCreateInfo.minLod = 0.0;
828 samplerCreateInfo.maxLod = 0.0;
Tony Barbour26b17f82015-06-25 16:56:44 -0600829 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700830
Chia-I Wue9864b52014-12-28 16:32:24 +0800831 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700832}
Tony Barboure2c58df2014-11-25 13:18:32 -0700833
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700834/*
835 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
836 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600837VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700838{
839 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700840 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700841
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800842 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700843}
844
Tony Barbour6918cd52015-04-09 12:58:51 -0600845VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600846{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600847 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600848 if (m_commandBuffer) {
849 delete m_commandBuffer;
850 }
851}
852
Tony Barbour6918cd52015-04-09 12:58:51 -0600853VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700854{
Tony Barboure2c58df2014-11-25 13:18:32 -0700855 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800856 m_commandBuffer = 0;
857
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800858 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700859 m_numVertices = constantCount;
860 m_stride = constantSize;
861
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600862 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800863 const size_t allocationSize = constantCount * constantSize;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600864 init(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700865
Chia-I Wua07fee62014-12-28 15:26:08 +0800866 void *pData = map();
867 memcpy(pData, data, allocationSize);
868 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700869
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800870 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600871 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600872 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800873 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600874 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800875 view_info.offset = 0;
876 view_info.range = allocationSize;
877 m_bufferView.init(*m_device, view_info);
878
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800879 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700880}
Tony Barbour82c39522014-12-04 14:33:33 -0700881
Tony Barbourd1c35722015-04-16 15:59:00 -0600882void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700883{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600884 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700885}
886
887
Tony Barbour6918cd52015-04-09 12:58:51 -0600888void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600889 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600890 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600891 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
892 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
893 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
894 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600895 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600896 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600897 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
898 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
899 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
900 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
901 VK_MEMORY_INPUT_SHADER_READ_BIT |
902 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
903 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
904 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700905{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600906 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700907
Tony Barbour38422802014-12-10 14:36:31 -0700908 if (!m_commandBuffer)
909 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600910 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700911
Tony Barbour6918cd52015-04-09 12:58:51 -0600912 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700913 }
914 else
915 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800916 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700917 }
918
Tony Barboure2c58df2014-11-25 13:18:32 -0700919 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600920 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600921 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600922 cmd_buf_info.pNext = NULL;
923 cmd_buf_info.flags = 0;
924
Jon Ashburnc4164b12014-12-31 17:10:47 -0700925 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600926 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700927
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600928 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000929 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600930 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700931
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600932 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
933 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000934
935 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600936 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, false, 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_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700963 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600964 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700965 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600966 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700967 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600968 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700969 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800970 default:
971 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600972 m_stride = 2;
973 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800974 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700975 }
976
Chia-I Wua07fee62014-12-28 15:26:08 +0800977 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600978 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
979 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700980
Chia-I Wua07fee62014-12-28 15:26:08 +0800981 void *pData = map();
982 memcpy(pData, data, allocationSize);
983 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700984
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800985 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600986 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600987 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800988 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600989 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700990 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800991 view_info.offset = 0;
992 view_info.range = allocationSize;
993 m_bufferView.init(*m_device, view_info);
994
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800995 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700996}
997
Tony Barbourd1c35722015-04-16 15:59:00 -0600998void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700999{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001000 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001001}
Tony Barboure2c58df2014-11-25 13:18:32 -07001002
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001003VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001004{
1005 return m_indexType;
1006}
1007
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001008VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001009{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001010 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001011 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1012 stageInfo->stage = m_stage;
1013 stageInfo->shader = obj();
1014 stageInfo->linkConstBufferCount = 0;
1015 stageInfo->pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001016
Tony Barboure2c58df2014-11-25 13:18:32 -07001017 return stageInfo;
1018}
1019
Tony Barbourd1c35722015-04-16 15:59:00 -06001020VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001021{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001022 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001023 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001024 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001025 VkShaderModuleCreateInfo moduleCreateInfo;
1026 vk_testing::ShaderModule module;
Tony Barboure2c58df2014-11-25 13:18:32 -07001027 size_t shader_len;
1028
1029 m_stage = stage;
1030 m_device = device;
1031
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001032 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1033 moduleCreateInfo.pNext = NULL;
1034
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001035 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001036 createInfo.pNext = NULL;
1037
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001038 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001039
1040 shader_len = strlen(shader_code);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001041 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1042 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1043 moduleCreateInfo.flags = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001044
Tony Barbourd1c35722015-04-16 15:59:00 -06001045 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001046 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1047 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1048 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1049 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barboure2c58df2014-11-25 13:18:32 -07001050
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001051 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001052
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001053 // Use Reference GLSL to SPV compiler
1054 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001055 moduleCreateInfo.pCode = spv.data();
1056 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1057 moduleCreateInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001058 }
Cody Northrop475663c2015-04-15 11:19:06 -06001059
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001060 err = module.init_try(*m_device, moduleCreateInfo);
1061 assert(VK_SUCCESS == err);
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001062
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001063 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1064 createInfo.pNext = NULL;
1065 createInfo.module = module.obj();
1066 createInfo.pName = "main";
1067 createInfo.flags = 0;
1068
1069 err = init_try(*m_device, createInfo);
Cody Northrop475663c2015-04-15 11:19:06 -06001070 assert(VK_SUCCESS == err);
Tony Barbourf325bf12014-12-03 15:59:38 -07001071}
Tony Barbour82c39522014-12-04 14:33:33 -07001072
Tony Barbour6918cd52015-04-09 12:58:51 -06001073VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001074{
Tony Barboure2c58df2014-11-25 13:18:32 -07001075 m_device = device;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001076
1077 m_vi_state.pNext = VK_NULL_HANDLE;
1078 m_vi_state.bindingCount = 0;
1079 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1080 m_vi_state.attributeCount = 0;
1081 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1082
Tony Barboure2c58df2014-11-25 13:18:32 -07001083 m_vertexBufferCount = 0;
1084
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001085 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1086 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001087 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001088 m_ia_state.disableVertexReuse = VK_FALSE;
1089 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001090 m_ia_state.primitiveRestartIndex = 0;
1091
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001092 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001093 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001094 m_rs_state.depthClipEnable = VK_FALSE;
1095 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001096 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1097 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001098 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001099 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1100 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001101
Tony Barboure2c58df2014-11-25 13:18:32 -07001102 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001103 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001104 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001105 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1106 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001107
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001108 m_ms_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001109 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1110 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001111 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
Tony Barbourdfd533a2015-06-26 10:18:34 -06001112 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001113 m_ms_state.minSampleShading = 0;
1114 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001115
Tony Barbourd81b8882015-05-15 09:37:57 -06001116 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001117 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001118 m_vp_state.viewportCount = 1;
1119 m_vp_state.depthMode = VK_DEPTH_MODE_ZERO_TO_ONE;
1120 m_vp_state.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1121
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001122 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001123 m_ds_state.pNext = VK_NULL_HANDLE,
Tony Barbourd1c35722015-04-16 15:59:00 -06001124 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001125 m_ds_state.depthTestEnable = VK_FALSE;
1126 m_ds_state.depthWriteEnable = VK_FALSE;
1127 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001128 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001129 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1130 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1131 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001132 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001133 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001134 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001135
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001136 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001137 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001138 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001139 att.channelWriteMask = 0xf;
1140 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001141
1142};
1143
Tony Barbour6918cd52015-04-09 12:58:51 -06001144void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001145{
1146 m_shaderObjs.push_back(shader);
1147}
1148
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001149void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001150{
1151 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1152 m_vi_state.attributeCount = count;
1153}
1154
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001155void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001156{
1157 m_vi_state.pVertexBindingDescriptions = vi_binding;
1158 m_vi_state.bindingCount = count;
1159}
1160
Tony Barbour6918cd52015-04-09 12:58:51 -06001161void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001162{
1163 m_vertexBufferObjs.push_back(vertexDataBuffer);
1164 m_vertexBufferBindings.push_back(binding);
1165 m_vertexBufferCount++;
1166}
1167
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001168void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001169{
Tony Barbourf52346d2015-01-16 14:27:35 -07001170 if (binding+1 > m_colorAttachments.size())
1171 {
1172 m_colorAttachments.resize(binding+1);
1173 }
1174 m_colorAttachments[binding] = *att;
1175}
1176
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001177void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001178{
1179 m_ds_state.format = ds_state->format;
1180 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1181 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1182 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001183 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001184 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1185 m_ds_state.back = ds_state->back;
1186 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001187}
1188
Chris Forbes95292b12015-05-25 11:13:26 +12001189VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001190{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001191 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001192
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001193 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001194
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001195 info.stageCount = m_shaderObjs.size();
1196 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1197
Tony Barbour976e1cf2014-12-17 11:57:31 -07001198 for (int i=0; i<m_shaderObjs.size(); i++)
1199 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001200 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001201 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001202 }
1203
1204 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1205 {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001206 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001207 }
1208
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001209 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001210 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001211 info.flags = 0;
1212 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001213
Tony Barbourf52346d2015-01-16 14:27:35 -07001214 m_cb_state.attachmentCount = m_colorAttachments.size();
1215 m_cb_state.pAttachments = &m_colorAttachments[0];
1216
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001217 info.pTessState = NULL;
1218 info.pVertexInputState = &m_vi_state;
1219 info.pIaState = &m_ia_state;
1220 info.pVpState = &m_vp_state;
1221 info.pRsState = &m_rs_state;
1222 info.pMsState = &m_ms_state;
1223 info.pDsState = &m_ds_state;
1224 info.pCbState = &m_cb_state;
1225
Chris Forbes95292b12015-05-25 11:13:26 +12001226 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001227}
Chia-I Wu2648d092014-12-29 14:24:14 +08001228
Tony Barbourd1c35722015-04-16 15:59:00 -06001229vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001230{
Tony Barbourd1c35722015-04-16 15:59:00 -06001231 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001232 if (this->mem_refs_.size()) {
1233 mems.reserve(this->mem_refs_.size());
1234 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1235 mems.push_back(this->mem_refs_[i]);
1236 }
1237
1238 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001239}
1240
Tony Barbour6918cd52015-04-09 12:58:51 -06001241VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001242 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001243{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001244 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001245}
Tony Barbour471338d2014-12-10 17:28:39 -07001246
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001247VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001248{
Chia-I Wud28343c2014-12-28 15:12:48 +08001249 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001250}
Tony Barbour471338d2014-12-10 17:28:39 -07001251
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001252VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001253{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001254 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001255 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001256}
1257
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001258VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001259{
1260 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001261 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001262}
1263
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001264VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001265{
Chia-I Wud28343c2014-12-28 15:12:48 +08001266 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001267 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001268}
1269
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001270void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, bool32_t byRegion, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001271{
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001272 vkCmdPipelineBarrier(obj(), src_stages, dest_stages, byRegion, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001273}
1274
Chris Forbesf0796e12015-06-24 14:34:53 +12001275void VkCommandBufferObj::ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001276 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001277{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001278 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001279 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001280 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001281 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1282 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1283 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001284 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001285 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001286
1287 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001288 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001289 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001290 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001291 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001292 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001293 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001294
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001295 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001296 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001297 memory_barrier.outputMask = output_mask;
1298 memory_barrier.inputMask = input_mask;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001299 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001300 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001301 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001302
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001303 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1304 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001305
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001306 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001307 memory_barrier.image = m_renderTargets[i]->image();
1308 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001309 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001310 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001311
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001312 vkCmdClearColorImage(obj(),
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001313 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001314 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001315
Tony Barbour30cc9e82014-12-17 11:53:55 -07001316 }
1317
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001318 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001319 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001320 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001321 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001322 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001323 dsRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001324 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001325 dsRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001326
1327 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001328
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001329 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001330 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001331 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001332 memory_barrier.subresourceRange = dsRange;
1333
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001334 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001335
Chris Forbesd9be82b2015-06-22 17:21:59 +12001336 vkCmdClearDepthStencilImage(obj(),
1337 depthStencilObj->obj(), VK_IMAGE_LAYOUT_GENERAL,
1338 depth_clear_color, stencil_clear_color,
1339 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001340
1341 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001342 memory_barrier.image = depthStencilObj->obj();
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001343 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001344 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001345 memory_barrier.subresourceRange = dsRange;
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001346 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001347 }
1348}
1349
Tony Barbour6918cd52015-04-09 12:58:51 -06001350void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001351{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001352 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001353 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001354 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001355 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1356 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1357 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001358 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001359 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001360 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001361 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1362 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1363 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1364 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1365 VK_MEMORY_INPUT_SHADER_READ_BIT |
1366 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1367 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001368 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001369
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001370 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001371 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001372 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001373 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001374 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001375 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001376
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001377 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001378 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001379 memory_barrier.outputMask = output_mask;
1380 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001381 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001382 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001383 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001384
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001385 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1386 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001387
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001388 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001389 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001390 memory_barrier.image = m_renderTargets[i]->image();
1391 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001392 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001393 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001394 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001395}
1396
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001397void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001398{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001399 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001400 renderpass,
1401 framebuffer,
1402 };
1403
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001404 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001405}
1406
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001407void VkCommandBufferObj::EndRenderPass()
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001408{
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001409 vkCmdEndRenderPass(obj());
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001410}
1411
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001412void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001413{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001414 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001415}
1416
Tony Barbour6918cd52015-04-09 12:58:51 -06001417void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001418{
1419 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001420}
1421
Tony Barbour6918cd52015-04-09 12:58:51 -06001422void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001423{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001424 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001425}
1426
Tony Barbour6918cd52015-04-09 12:58:51 -06001427void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001428{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001429 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001430}
1431
Tony Barbour6918cd52015-04-09 12:58:51 -06001432void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001433{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001434 QueueCommandBuffer(NULL);
1435}
1436
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001437void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001438{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001439 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001440
1441 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001442 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1443 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001444
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001445 err = vkQueueWaitIdle( m_device->m_queue );
1446 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001447
1448 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001449 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001450}
1451
Tony Barbour6918cd52015-04-09 12:58:51 -06001452void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001453{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001454 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001455}
1456
Tony Barbour6918cd52015-04-09 12:58:51 -06001457void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001458{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001459 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001460
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001461 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Mark Lobodzinskif2093b62015-06-15 13:21:21 -06001462 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northropd4c1a502015-04-16 13:41:56 -06001463 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001464}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001465
Tony Barbour6918cd52015-04-09 12:58:51 -06001466void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001467{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001468 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001469}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001470
Tony Barbourd1c35722015-04-16 15:59:00 -06001471void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001472{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001473 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001474}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001475
Tony Barbour6918cd52015-04-09 12:58:51 -06001476VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001477{
1478 m_initialized = false;
1479}
Tony Barbour6918cd52015-04-09 12:58:51 -06001480bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001481{
1482 return m_initialized;
1483}
1484
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001485VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001486{
1487 return &m_depthStencilBindInfo;
1488}
1489
Tony Barbour6918cd52015-04-09 12:58:51 -06001490void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001491{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001492 VkImageCreateInfo image_info;
1493 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001494
1495 m_device = device;
1496 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001497 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001498
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001499 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001500 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001501 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001502 image_info.format = m_depth_stencil_fmt;
1503 image_info.extent.width = width;
1504 image_info.extent.height = height;
1505 image_info.extent.depth = 1;
1506 image_info.mipLevels = 1;
1507 image_info.arraySize = 1;
1508 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001509 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001510 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001511 image_info.flags = 0;
1512 init(*m_device, image_info);
1513
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001514 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001515 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001516 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001517 view_info.mipLevel = 0;
1518 view_info.baseArraySlice = 0;
1519 view_info.arraySize = 1;
1520 view_info.flags = 0;
1521 view_info.image = obj();
1522 m_depthStencilView.init(*m_device, view_info);
1523
1524 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001525 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001526}