blob: a8359c7bc673d8c281ddb138fd91e4ad57fd6bfc [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 Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060068 std::vector<const char*> instance_layer_names;
69 std::vector<const char*> device_layer_names;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060070 std::vector<const char*> instance_extension_names;
71 std::vector<const char*> device_extension_names;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060072 InitFramework(
73 instance_layer_names, device_layer_names,
74 instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060075}
76
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060077void VkRenderFramework::InitFramework(
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060078 std::vector<const char *> instance_layer_names,
79 std::vector<const char *> device_layer_names,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060080 std::vector<const char *> instance_extension_names,
81 std::vector<const char *> device_extension_names,
82 PFN_vkDbgMsgCallback dbgFunction,
83 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060084{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060085 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060086 std::vector<VkExtensionProperties> instance_extensions;
87 std::vector<VkExtensionProperties> device_extensions;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060088 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060089
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -060090 /* TODO: Verify requested extensions are available */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060091
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060092 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060093 instInfo.pNext = NULL;
94 instInfo.pAppInfo = &app_info;
95 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060096 instInfo.layerCount = instance_layer_names.size();
97 instInfo.ppEnabledLayerNames =(instance_layer_names.size()) ? &instance_layer_names[0] : NULL;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -060098 instInfo.extensionCount = instance_extension_names.size();
99 instInfo.ppEnabledExtensionNames = (instance_extension_names.size()) ? &instance_extension_names[0] : NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600100 err = vkCreateInstance(&instInfo, &this->inst);
101 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600102
Jon Ashburn83a64252015-04-15 11:31:12 -0600103 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
104 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
105 ASSERT_VK_SUCCESS(err);
106 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600107 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700108 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600109 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600110 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
111 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
112 if (m_dbgCreateMsgCallback) {
113 err = m_dbgCreateMsgCallback(this->inst,
114 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
115 dbgFunction,
116 userData,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600117 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600118 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600119
120 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
121 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600122 }
Tony Barbour15524c32015-04-29 17:34:29 -0600123 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600124
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600125 /* TODO: Verify requested physical device extensions are available */
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600126 m_device = new VkDeviceObj(0, objs[0], device_layer_names, device_extension_names);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600127
128 /* Now register callback on device */
129 if (0) {
130 if (m_dbgCreateMsgCallback) {
131 err = m_dbgCreateMsgCallback(this->inst,
132 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
133 dbgFunction,
134 userData,
135 &m_devMsgCallback);
136 ASSERT_VK_SUCCESS(err);
137 }
138 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600139 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600140
Tony Barbour6918cd52015-04-09 12:58:51 -0600141 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600142}
143
Tony Barbour6918cd52015-04-09 12:58:51 -0600144void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600145{
Mike Stroyanb050c682015-04-17 12:36:38 -0600146 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
147 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
148 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
149 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
150 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
151 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600152
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600153 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
154 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
155
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600156 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600157 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600158 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600159 while (!m_renderTargets.empty()) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600160 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
Mike Stroyanb050c682015-04-17 12:36:38 -0600161 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
162 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600163 m_renderTargets.pop_back();
164 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600165
Tony Barbour1c45ce02015-03-27 17:03:18 -0600166 delete m_depthStencil;
167
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600168 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800169 delete m_device;
Chris Forbesbe2fae62015-07-07 11:02:22 +1200170 if (this->inst) vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600171}
172
Tony Barbour6918cd52015-04-09 12:58:51 -0600173void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600174{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600175 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600176
Tony Barbourd1c35722015-04-16 15:59:00 -0600177 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600178
179 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600180 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600181 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700182
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600183 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
184 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600185
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600186 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600187 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600188 blend.blendConst[0] = 1.0f;
189 blend.blendConst[1] = 1.0f;
190 blend.blendConst[2] = 1.0f;
191 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600192 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
193 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600194
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600195 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600196 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski365feea2015-06-12 11:14:17 -0600197 depthStencil.minDepthBounds = 0.f;
198 depthStencil.maxDepthBounds = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700199 depthStencil.stencilFrontRef = 0;
200 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600201 depthStencil.stencilReadMask = 0xff;
202 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600203
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600204 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
205 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600206
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600207 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600208
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600209 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700210 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
211
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600212 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
213 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600214}
215
Tony Barbour6918cd52015-04-09 12:58:51 -0600216void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600217{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600218 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600219
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600220 VkViewport viewport;
Chris Forbesd9be82b2015-06-22 17:21:59 +1200221 VkRect2D scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600222
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600223 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600224 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700225 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700226 viewport.originX = 0;
227 viewport.originY = 0;
228 viewport.width = 1.f * width;
229 viewport.height = 1.f * height;
230 viewport.minDepth = 0.f;
231 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600232 scissor.extent.width = (int32_t) width;
233 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700234 scissor.offset.x = 0;
235 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700236 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700237 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700238
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600239 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
240 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600241 m_width = width;
242 m_height = height;
243}
244
Tony Barbour6918cd52015-04-09 12:58:51 -0600245void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600246{
247 InitViewport(m_width, m_height);
248}
Tony Barbour6918cd52015-04-09 12:58:51 -0600249void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600250{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700251 InitRenderTarget(1);
252}
253
Tony Barbour6918cd52015-04-09 12:58:51 -0600254void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700255{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600256 InitRenderTarget(targets, NULL);
257}
258
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600259void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600260{
261 InitRenderTarget(1, dsBinding);
262}
263
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600264void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600265{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600266 std::vector<VkAttachmentLoadOp> load_ops;
267 std::vector<VkAttachmentStoreOp> store_ops;
Chris Forbesf0796e12015-06-24 14:34:53 +1200268 std::vector<VkClearColorValue> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600269
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600270 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800271
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700272 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600273 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600274
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600275 VkFormatProperties props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600276 VkResult err;
277
Chris Forbesbc0bb772015-06-21 22:55:02 +1200278 err = vkGetPhysicalDeviceFormatInfo(m_device->gpu().obj(), m_render_target_fmt, &props);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600279 ASSERT_VK_SUCCESS(err);
280
281 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600282 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600283 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_LINEAR);
284 }
285 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600286 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600287 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL);
288 }
289 else {
290 FAIL() << "Neither Linear nor Optimal allowed for render target";
291 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600292
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600293 m_renderTargets.push_back(img);
Tony Barbourf52346d2015-01-16 14:27:35 -0700294 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600295 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chris Forbese182fe02015-06-15 09:32:35 +1200296 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 -0600297 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600298 clear_colors.push_back(m_clear_color);
Chia-I Wuecebf752014-12-05 10:45:15 +0800299 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600300
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700301 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600302 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600303 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600304 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700305 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600306 fb_info.pColorAttachments = m_colorBindings;
307 fb_info.pDepthStencilAttachment = dsBinding;
308 fb_info.sampleCount = 1;
309 fb_info.width = (uint32_t)m_width;
310 fb_info.height = (uint32_t)m_height;
311 fb_info.layers = 1;
312
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600313 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600314
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600315 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600316 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600317 rp_info.renderArea.extent.width = (uint32_t) m_width;
318 rp_info.renderArea.extent.height = (uint32_t) m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600319
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700320 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600321 rp_info.pColorFormats = &m_render_target_fmt;
322 rp_info.pColorLayouts = &m_colorBindings[0].layout;
323 rp_info.pColorLoadOps = &load_ops[0];
324 rp_info.pColorStoreOps = &store_ops[0];
325 rp_info.pColorLoadClearValues = &clear_colors[0];
326 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600327 if (dsBinding) {
328 rp_info.depthStencilLayout = dsBinding->layout;
329 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600330 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600331 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600332 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
333 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600334 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600335 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Tony Barbour51a05ce2015-06-03 11:40:51 -0600336 rp_info.sampleCount = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600337 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600338}
339
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600340
341
Tony Barbourd1c35722015-04-16 15:59:00 -0600342VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600343 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800344{
345 init();
346
347 props = gpu().properties();
348 queue_props = &gpu().queue_properties()[0];
349}
350
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600351VkDeviceObj::VkDeviceObj(uint32_t id,
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600352 VkPhysicalDevice obj, std::vector<const char *> &layer_names,
353 std::vector<const char *> &extension_names) :
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600354 vk_testing::Device(obj), id(id)
355{
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600356 init(layer_names, extension_names);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600357
358 props = gpu().properties();
359 queue_props = &gpu().queue_properties()[0];
360}
361
Tony Barbour6918cd52015-04-09 12:58:51 -0600362void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800363{
364 ASSERT_NE(true, graphics_queues().empty());
365 m_queue = graphics_queues()[0]->obj();
366}
367
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600368VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
369 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700370{
Tony Barboure2c58df2014-11-25 13:18:32 -0700371
372}
373
Tony Barbour6918cd52015-04-09 12:58:51 -0600374VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700375{
Chia-I Wu11078b02015-01-04 16:27:24 +0800376 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700377}
Tony Barbour82c39522014-12-04 14:33:33 -0700378
Tony Barbour6918cd52015-04-09 12:58:51 -0600379int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700380{
Chia-I Wu11078b02015-01-04 16:27:24 +0800381 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600382 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600383 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800384 tc.count = 1;
385 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700386
Chia-I Wu11078b02015-01-04 16:27:24 +0800387 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700388}
Tony Barbour82c39522014-12-04 14:33:33 -0700389
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600390int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700391{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600392 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800393 tc.type = type;
394 tc.count = 1;
395 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700396
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800397 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
398 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600399
Chia-I Wu11078b02015-01-04 16:27:24 +0800400 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700401}
Tony Barbour82c39522014-12-04 14:33:33 -0700402
Tony Barbour6918cd52015-04-09 12:58:51 -0600403int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700404{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600405 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600406 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800407 tc.count = 1;
408 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700409
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800410 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600411 tmp.sampler = sampler->obj();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800412 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700413
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800414 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
415 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700416
Chia-I Wu11078b02015-01-04 16:27:24 +0800417 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700418}
Chia-I Wu11078b02015-01-04 16:27:24 +0800419
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500420VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700421{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500422 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700423}
424
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600425VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700426{
Chia-I Wu11078b02015-01-04 16:27:24 +0800427 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700428}
Tony Barboure2c58df2014-11-25 13:18:32 -0700429
Tony Barbour6918cd52015-04-09 12:58:51 -0600430void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700431{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600432 // create VkDescriptorPool
433 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600434 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800435 pool.count = m_type_counts.size();
436 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600437 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700438
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600439 // create VkDescriptorSetLayout
440 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800441 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800442 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800443 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800444 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600445 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800446 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700447 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800448
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600449 // create VkDescriptorSetLayout
450 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600451 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800452 layout.count = bindings.size();
453 layout.pBinding = &bindings[0];
454
Chia-I Wu41126e52015-03-26 15:27:55 +0800455 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600456 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800457 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500458
459 // create VkPipelineLayout
460 VkPipelineLayoutCreateInfo pipeline_layout = {};
461 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
462 pipeline_layout.descriptorSetCount = layouts.size();
463 pipeline_layout.pSetLayouts = NULL;
464
465 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700466
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600467 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500468 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800469
Chia-I Wu41126e52015-03-26 15:27:55 +0800470 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800471 size_t imageSamplerCount = 0;
472 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
473 it != m_writes.end(); it++) {
474 it->destSet = m_set->obj();
475 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
476 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800477 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800478
479 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800480 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700481}
Tony Barboure2c58df2014-11-25 13:18:32 -0700482
Tony Barbour6918cd52015-04-09 12:58:51 -0600483VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800484{
485 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800486 m_descriptorInfo.imageView = VK_NULL_HANDLE;
487 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800488}
489
Tony Barbour6918cd52015-04-09 12:58:51 -0600490void VkImageObj::ImageMemoryBarrier(
491 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600492 VkImageAspect aspect,
493 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600494 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600495 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
496 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
497 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
498 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600499 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600500 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600501 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
502 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
503 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
504 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
505 VK_MEMORY_INPUT_SHADER_READ_BIT |
506 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
507 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
508 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600509 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600510{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600511 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
512 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600513 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
514 subresourceRange);
515
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600516 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600517
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600518 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
519 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600520
521 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600522 vkCmdPipelineBarrier(cmd_buf->obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600523}
524
Tony Barbour6918cd52015-04-09 12:58:51 -0600525void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600526 VkImageAspect aspect,
527 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600528{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600529 VkFlags output_mask, input_mask;
530 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600531 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600532 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
533 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
534 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600535 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600536 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600537 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600538 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
539 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
540 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
541 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
542 VK_MEMORY_INPUT_SHADER_READ_BIT |
543 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
544 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600545 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600546
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800547 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600548 return;
549 }
550
551 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600552 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600553 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
554 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600555 break;
556
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600557 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600558 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
559 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600560 break;
561
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600562 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600563 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
564 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600565 break;
566
567 default:
568 output_mask = all_cache_outputs;
569 input_mask = all_cache_inputs;
570 break;
571 }
572
573 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800574 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600575}
576
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600577void VkImageObj::SetLayout(VkImageAspect aspect,
578 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600579{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600580 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600581
582 if (image_layout == m_descriptorInfo.imageLayout) {
583 return;
584 }
585
Tony Barbour6918cd52015-04-09 12:58:51 -0600586 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600587
588 /* Build command buffer to set image layout in the driver */
589 err = cmd_buf.BeginCommandBuffer();
590 assert(!err);
591
592 SetLayout(&cmd_buf, aspect, image_layout);
593
594 err = cmd_buf.EndCommandBuffer();
595 assert(!err);
596
597 cmd_buf.QueueCommandBuffer();
598}
599
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600600bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600601{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600602 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600603 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600604 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600605
Tony Barbour579f7802015-04-03 15:11:43 -0600606 return true;
607}
608
Tony Barbour6918cd52015-04-09 12:58:51 -0600609void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600610 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600611 VkImageTiling requested_tiling,
612 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800613{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600614 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600615 VkFormatProperties image_fmt;
616 VkImageTiling tiling;
617 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600618
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800619 mipCount = 0;
620
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600621 uint32_t _w = w;
622 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800623 while( ( _w > 0 ) || ( _h > 0 ) )
624 {
625 _w >>= 1;
626 _h >>= 1;
627 mipCount++;
628 }
629
Chris Forbesbc0bb772015-06-21 22:55:02 +1200630 err = vkGetPhysicalDeviceFormatInfo(m_device->gpu().obj(), fmt, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600631 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600632
Tony Barbourd1c35722015-04-16 15:59:00 -0600633 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600634 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600635 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600636 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600637 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600638 } else {
639 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
640 }
641 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600642 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600643 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600644 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600645 } else {
646 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
647 }
648
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600649 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600650 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800651 imageCreateInfo.format = fmt;
652 imageCreateInfo.extent.width = w;
653 imageCreateInfo.extent.height = h;
654 imageCreateInfo.mipLevels = mipCount;
655 imageCreateInfo.tiling = tiling;
656
657 imageCreateInfo.usage = usage;
658
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600659 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800660
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600661 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600662 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600663 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600664 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600665 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800666}
667
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600668VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800669{
670 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600671 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800672}
673
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600674VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800675{
676 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600677 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800678}
679
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600680VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600681{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600682 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600683 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600684 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600685
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600686 /* Build command buffer to copy staging texture to usable texture */
687 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600688 assert(!err);
689
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600690 /* TODO: Can we determine image aspect from image object? */
691 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600692 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600693
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600694 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600695 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600696
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600697 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600698 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600699 copy_region.srcSubresource.arraySlice = 0;
700 copy_region.srcSubresource.mipLevel = 0;
701 copy_region.srcOffset.x = 0;
702 copy_region.srcOffset.y = 0;
703 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600704 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600705 copy_region.destSubresource.arraySlice = 0;
706 copy_region.destSubresource.mipLevel = 0;
707 copy_region.destOffset.x = 0;
708 copy_region.destOffset.y = 0;
709 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600710 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600711
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600712 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600713 src_image.obj(), src_image.layout(),
714 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600715 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600716
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600717 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600718
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600719 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600720
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600721 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600722 assert(!err);
723
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600724 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600725
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600726 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600727}
728
Tony Barbour6918cd52015-04-09 12:58:51 -0600729VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
730 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700731{
732 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600733 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600734 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600735 void *data;
736 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600737 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600738 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600739
Cody Northropbb6fdb32015-06-17 08:28:19 -0600740 stagingImage.init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600741 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600742
743 if (colors == NULL)
744 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700745
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800746 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700747
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600748 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600749 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600750 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600751 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600752 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600753 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600754 view.channels.r = VK_CHANNEL_SWIZZLE_R;
755 view.channels.g = VK_CHANNEL_SWIZZLE_G;
756 view.channels.b = VK_CHANNEL_SWIZZLE_B;
757 view.channels.a = VK_CHANNEL_SWIZZLE_A;
758 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600759 view.subresourceRange.baseMipLevel = 0;
760 view.subresourceRange.mipLevels = 1;
761 view.subresourceRange.baseArraySlice = 0;
762 view.subresourceRange.arraySize = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700763
Tony Barboure2c58df2014-11-25 13:18:32 -0700764 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600765 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700766
767 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800768 view.image = obj();
769 m_textureView.init(*m_device, view);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800770 m_descriptorInfo.imageView = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700771
Tony Barbour5dc515d2015-04-01 17:47:06 -0600772 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700773
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800774 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700775 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800776 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600777 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700778 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600779 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600780 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700781}
Tony Barbour82c39522014-12-04 14:33:33 -0700782
Tony Barbour6918cd52015-04-09 12:58:51 -0600783VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700784{
Tony Barboure2c58df2014-11-25 13:18:32 -0700785 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700786
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600787 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800788 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600789 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
790 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
791 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600792 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600793 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
794 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
795 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800796 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600797 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600798 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800799 samplerCreateInfo.minLod = 0.0;
800 samplerCreateInfo.maxLod = 0.0;
Tony Barbour26b17f82015-06-25 16:56:44 -0600801 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700802
Chia-I Wue9864b52014-12-28 16:32:24 +0800803 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700804}
Tony Barboure2c58df2014-11-25 13:18:32 -0700805
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700806/*
807 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
808 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600809VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700810{
811 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700812 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700813
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800814 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700815}
816
Tony Barbour6918cd52015-04-09 12:58:51 -0600817VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600818{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600819 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600820 if (m_commandBuffer) {
821 delete m_commandBuffer;
822 }
823}
824
Tony Barbour6918cd52015-04-09 12:58:51 -0600825VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700826{
Tony Barboure2c58df2014-11-25 13:18:32 -0700827 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800828 m_commandBuffer = 0;
829
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800830 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700831 m_numVertices = constantCount;
832 m_stride = constantSize;
833
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600834 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800835 const size_t allocationSize = constantCount * constantSize;
Cody Northrop7fb43862015-06-22 14:56:14 -0600836 init_as_src_and_dst(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700837
Chia-I Wua07fee62014-12-28 15:26:08 +0800838 void *pData = map();
839 memcpy(pData, data, allocationSize);
840 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700841
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800842 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600843 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600844 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800845 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600846 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800847 view_info.offset = 0;
848 view_info.range = allocationSize;
849 m_bufferView.init(*m_device, view_info);
850
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800851 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700852}
Tony Barbour82c39522014-12-04 14:33:33 -0700853
Tony Barbourd1c35722015-04-16 15:59:00 -0600854void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700855{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600856 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700857}
858
859
Tony Barbour6918cd52015-04-09 12:58:51 -0600860void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600861 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600862 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600863 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
864 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
865 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
866 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600867 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600868 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600869 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
870 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
871 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
872 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
873 VK_MEMORY_INPUT_SHADER_READ_BIT |
874 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
875 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
876 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700877{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600878 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700879
Tony Barbour38422802014-12-10 14:36:31 -0700880 if (!m_commandBuffer)
881 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600882 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700883
Tony Barbour6918cd52015-04-09 12:58:51 -0600884 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700885 }
886 else
887 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800888 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700889 }
890
Tony Barboure2c58df2014-11-25 13:18:32 -0700891 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600892 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600893 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600894 cmd_buf_info.pNext = NULL;
895 cmd_buf_info.flags = 0;
896
Jon Ashburnc4164b12014-12-31 17:10:47 -0700897 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600898 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700899
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600900 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000901 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600902 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700903
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600904 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
905 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000906
907 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600908 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700909
910 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700911 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600912 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700913
Tony Barboure2c58df2014-11-25 13:18:32 -0700914 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600915 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700916 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600917 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
918 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700919}
920
Tony Barbour6918cd52015-04-09 12:58:51 -0600921VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
922 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700923{
924
925}
926
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600927void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700928{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600929 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700930
931 m_numVertices = numIndexes;
932 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700933 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600934 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700935 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600936 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700937 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600938 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700939 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600940 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700941 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800942 default:
943 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600944 m_stride = 2;
945 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800946 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700947 }
948
Chia-I Wua07fee62014-12-28 15:26:08 +0800949 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600950 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northrop7fb43862015-06-22 14:56:14 -0600951 init_as_src_and_dst(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700952
Chia-I Wua07fee62014-12-28 15:26:08 +0800953 void *pData = map();
954 memcpy(pData, data, allocationSize);
955 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700956
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800957 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600958 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600959 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800960 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600961 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700962 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800963 view_info.offset = 0;
964 view_info.range = allocationSize;
965 m_bufferView.init(*m_device, view_info);
966
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800967 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700968}
969
Tony Barbourd1c35722015-04-16 15:59:00 -0600970void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700971{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600972 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700973}
Tony Barboure2c58df2014-11-25 13:18:32 -0700974
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600975VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -0700976{
977 return m_indexType;
978}
979
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600980VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700981{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600982 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600983 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
984 stageInfo->stage = m_stage;
985 stageInfo->shader = obj();
986 stageInfo->linkConstBufferCount = 0;
987 stageInfo->pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700988
Tony Barboure2c58df2014-11-25 13:18:32 -0700989 return stageInfo;
990}
991
Tony Barbourd1c35722015-04-16 15:59:00 -0600992VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -0700993{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600994 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600995 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600996 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600997 VkShaderModuleCreateInfo moduleCreateInfo;
998 vk_testing::ShaderModule module;
Tony Barboure2c58df2014-11-25 13:18:32 -0700999 size_t shader_len;
1000
1001 m_stage = stage;
1002 m_device = device;
1003
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001004 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1005 moduleCreateInfo.pNext = NULL;
1006
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001007 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001008 createInfo.pNext = NULL;
1009
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001010 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001011
1012 shader_len = strlen(shader_code);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001013 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1014 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1015 moduleCreateInfo.flags = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001016
Tony Barbourd1c35722015-04-16 15:59:00 -06001017 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001018 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1019 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1020 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1021 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barboure2c58df2014-11-25 13:18:32 -07001022
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001023 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001024
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001025 // Use Reference GLSL to SPV compiler
1026 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001027 moduleCreateInfo.pCode = spv.data();
1028 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1029 moduleCreateInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001030 }
Cody Northrop475663c2015-04-15 11:19:06 -06001031
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001032 err = module.init_try(*m_device, moduleCreateInfo);
1033 assert(VK_SUCCESS == err);
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001034
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001035 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1036 createInfo.pNext = NULL;
1037 createInfo.module = module.obj();
1038 createInfo.pName = "main";
1039 createInfo.flags = 0;
1040
1041 err = init_try(*m_device, createInfo);
Cody Northrop475663c2015-04-15 11:19:06 -06001042 assert(VK_SUCCESS == err);
Tony Barbourf325bf12014-12-03 15:59:38 -07001043}
Tony Barbour82c39522014-12-04 14:33:33 -07001044
Tony Barbour6918cd52015-04-09 12:58:51 -06001045VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001046{
Tony Barboure2c58df2014-11-25 13:18:32 -07001047 m_device = device;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001048
1049 m_vi_state.pNext = VK_NULL_HANDLE;
1050 m_vi_state.bindingCount = 0;
1051 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1052 m_vi_state.attributeCount = 0;
1053 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1054
Tony Barboure2c58df2014-11-25 13:18:32 -07001055 m_vertexBufferCount = 0;
1056
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001057 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1058 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001059 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001060 m_ia_state.disableVertexReuse = VK_FALSE;
1061 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001062 m_ia_state.primitiveRestartIndex = 0;
1063
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001064 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001065 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001066 m_rs_state.depthClipEnable = VK_FALSE;
1067 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001068 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1069 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001070 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001071 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1072 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001073
Tony Barboure2c58df2014-11-25 13:18:32 -07001074 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001075 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001076 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001077 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1078 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001079
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001080 m_ms_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001081 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1082 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001083 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
Tony Barbourdfd533a2015-06-26 10:18:34 -06001084 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001085 m_ms_state.minSampleShading = 0;
1086 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001087
Tony Barbourd81b8882015-05-15 09:37:57 -06001088 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001089 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001090 m_vp_state.viewportCount = 1;
1091 m_vp_state.depthMode = VK_DEPTH_MODE_ZERO_TO_ONE;
1092 m_vp_state.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1093
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001094 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001095 m_ds_state.pNext = VK_NULL_HANDLE,
Tony Barbourd1c35722015-04-16 15:59:00 -06001096 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001097 m_ds_state.depthTestEnable = VK_FALSE;
1098 m_ds_state.depthWriteEnable = VK_FALSE;
1099 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001100 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001101 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1102 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1103 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001104 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001105 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001106 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001107
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001108 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001109 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001110 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001111 att.channelWriteMask = 0xf;
1112 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001113
1114};
1115
Tony Barbour6918cd52015-04-09 12:58:51 -06001116void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001117{
1118 m_shaderObjs.push_back(shader);
1119}
1120
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001121void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001122{
1123 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1124 m_vi_state.attributeCount = count;
1125}
1126
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001127void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001128{
1129 m_vi_state.pVertexBindingDescriptions = vi_binding;
1130 m_vi_state.bindingCount = count;
1131}
1132
Tony Barbour6918cd52015-04-09 12:58:51 -06001133void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001134{
1135 m_vertexBufferObjs.push_back(vertexDataBuffer);
1136 m_vertexBufferBindings.push_back(binding);
1137 m_vertexBufferCount++;
1138}
1139
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001140void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001141{
Tony Barbourf52346d2015-01-16 14:27:35 -07001142 if (binding+1 > m_colorAttachments.size())
1143 {
1144 m_colorAttachments.resize(binding+1);
1145 }
1146 m_colorAttachments[binding] = *att;
1147}
1148
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001149void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001150{
1151 m_ds_state.format = ds_state->format;
1152 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1153 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1154 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001155 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001156 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1157 m_ds_state.back = ds_state->back;
1158 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001159}
1160
Chris Forbes95292b12015-05-25 11:13:26 +12001161VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001162{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001163 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001164
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001165 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001166
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001167 info.stageCount = m_shaderObjs.size();
1168 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1169
Tony Barbour976e1cf2014-12-17 11:57:31 -07001170 for (int i=0; i<m_shaderObjs.size(); i++)
1171 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001172 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001173 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001174 }
1175
1176 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1177 {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001178 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001179 }
1180
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001181 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001182 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001183 info.flags = 0;
1184 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001185
Tony Barbourf52346d2015-01-16 14:27:35 -07001186 m_cb_state.attachmentCount = m_colorAttachments.size();
1187 m_cb_state.pAttachments = &m_colorAttachments[0];
1188
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001189 info.pTessState = NULL;
1190 info.pVertexInputState = &m_vi_state;
1191 info.pIaState = &m_ia_state;
1192 info.pVpState = &m_vp_state;
1193 info.pRsState = &m_rs_state;
1194 info.pMsState = &m_ms_state;
1195 info.pDsState = &m_ds_state;
1196 info.pCbState = &m_cb_state;
1197
Chris Forbes95292b12015-05-25 11:13:26 +12001198 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001199}
Chia-I Wu2648d092014-12-29 14:24:14 +08001200
Tony Barbourd1c35722015-04-16 15:59:00 -06001201vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001202{
Tony Barbourd1c35722015-04-16 15:59:00 -06001203 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001204 if (this->mem_refs_.size()) {
1205 mems.reserve(this->mem_refs_.size());
1206 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1207 mems.push_back(this->mem_refs_[i]);
1208 }
1209
1210 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001211}
1212
Tony Barbour6918cd52015-04-09 12:58:51 -06001213VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001214 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001215{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001216 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001217}
Tony Barbour471338d2014-12-10 17:28:39 -07001218
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001219VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001220{
Chia-I Wud28343c2014-12-28 15:12:48 +08001221 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001222}
Tony Barbour471338d2014-12-10 17:28:39 -07001223
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001224VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001225{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001226 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001227 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001228}
1229
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001230VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001231{
1232 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001233 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001234}
1235
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001236VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001237{
Chia-I Wud28343c2014-12-28 15:12:48 +08001238 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001239 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001240}
1241
Courtney Goeltzenleuchtercd2a0992015-07-09 11:44:38 -06001242void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, VkBool32 byRegion, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001243{
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001244 vkCmdPipelineBarrier(obj(), src_stages, dest_stages, byRegion, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001245}
1246
Chris Forbesf0796e12015-06-24 14:34:53 +12001247void VkCommandBufferObj::ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001248 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001249{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001250 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001251 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001252 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001253 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1254 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1255 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001256 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001257 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001258
1259 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001260 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001261 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001262 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001263 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001264 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001265 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001266
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001267 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001268 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001269 memory_barrier.outputMask = output_mask;
1270 memory_barrier.inputMask = input_mask;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001271 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001272 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001273 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001274
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001275 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1276 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001277
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001278 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001279 memory_barrier.image = m_renderTargets[i]->image();
1280 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001281 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001282 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001283
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001284 vkCmdClearColorImage(obj(),
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001285 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001286 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001287
Tony Barbour30cc9e82014-12-17 11:53:55 -07001288 }
1289
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001290 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001291 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001292 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001293 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001294 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001295 dsRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001296 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001297 dsRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001298
1299 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001300
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001301 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001302 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001303 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001304 memory_barrier.subresourceRange = dsRange;
1305
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001306 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001307
Chris Forbesd9be82b2015-06-22 17:21:59 +12001308 vkCmdClearDepthStencilImage(obj(),
1309 depthStencilObj->obj(), VK_IMAGE_LAYOUT_GENERAL,
1310 depth_clear_color, stencil_clear_color,
1311 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001312
1313 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001314 memory_barrier.image = depthStencilObj->obj();
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001315 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001316 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001317 memory_barrier.subresourceRange = dsRange;
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001318 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001319 }
1320}
1321
Tony Barbour6918cd52015-04-09 12:58:51 -06001322void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001323{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001324 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001325 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001326 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001327 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1328 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1329 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001330 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001331 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001332 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001333 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1334 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1335 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1336 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1337 VK_MEMORY_INPUT_SHADER_READ_BIT |
1338 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1339 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001340 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001341
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001342 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001343 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001344 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001345 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001346 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001347 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001348
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001349 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001350 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001351 memory_barrier.outputMask = output_mask;
1352 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001353 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001354 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001355 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001356
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001357 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1358 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001359
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001360 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001361 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001362 memory_barrier.image = m_renderTargets[i]->image();
1363 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001364 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001365 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001366 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001367}
1368
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001369void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001370{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001371 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001372 renderpass,
1373 framebuffer,
1374 };
1375
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001376 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001377}
1378
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001379void VkCommandBufferObj::EndRenderPass()
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001380{
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001381 vkCmdEndRenderPass(obj());
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001382}
1383
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001384void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001385{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001386 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001387}
1388
Tony Barbour6918cd52015-04-09 12:58:51 -06001389void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001390{
1391 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001392}
1393
Tony Barbour6918cd52015-04-09 12:58:51 -06001394void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001395{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001396 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001397}
1398
Tony Barbour6918cd52015-04-09 12:58:51 -06001399void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001400{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001401 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001402}
1403
Tony Barbour6918cd52015-04-09 12:58:51 -06001404void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001405{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001406 QueueCommandBuffer(NULL);
1407}
1408
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001409void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001410{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001411 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001412
1413 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001414 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1415 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001416
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001417 err = vkQueueWaitIdle( m_device->m_queue );
1418 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001419
1420 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001421 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001422}
1423
Tony Barbour6918cd52015-04-09 12:58:51 -06001424void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001425{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001426 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001427}
1428
Tony Barbour6918cd52015-04-09 12:58:51 -06001429void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001430{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001431 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001432
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001433 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Mark Lobodzinskif2093b62015-06-15 13:21:21 -06001434 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northropd4c1a502015-04-16 13:41:56 -06001435 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001436}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001437
Tony Barbour6918cd52015-04-09 12:58:51 -06001438void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001439{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001440 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001441}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001442
Tony Barbourd1c35722015-04-16 15:59:00 -06001443void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001444{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001445 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001446}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001447
Tony Barbour6918cd52015-04-09 12:58:51 -06001448VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001449{
1450 m_initialized = false;
1451}
Tony Barbour6918cd52015-04-09 12:58:51 -06001452bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001453{
1454 return m_initialized;
1455}
1456
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001457VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001458{
1459 return &m_depthStencilBindInfo;
1460}
1461
Tony Barbour6918cd52015-04-09 12:58:51 -06001462void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001463{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001464 VkImageCreateInfo image_info;
1465 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001466
1467 m_device = device;
1468 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001469 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001470
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001471 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001472 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001473 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001474 image_info.format = m_depth_stencil_fmt;
1475 image_info.extent.width = width;
1476 image_info.extent.height = height;
1477 image_info.extent.depth = 1;
1478 image_info.mipLevels = 1;
1479 image_info.arraySize = 1;
1480 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001481 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001482 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001483 image_info.flags = 0;
1484 init(*m_device, image_info);
1485
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001486 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001487 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001488 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001489 view_info.mipLevel = 0;
1490 view_info.baseArraySlice = 0;
1491 view_info.arraySize = 1;
1492 view_info.flags = 0;
1493 view_info.image = obj();
1494 m_depthStencilView.init(*m_device, view_info);
1495
1496 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001497 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001498}