blob: ae62008b740a29f0ad8b55a6c57b25f73bfc50a9 [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 ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070044 m_depth_clear_color( 1.0 ),
45 m_stencil_clear_color( 0 )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060046{
Tony Barbour1c45ce02015-03-27 17:03:18 -060047
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070048 // clear the back buffer to dark grey
49 m_clear_color.color.rawColor[0] = 64;
50 m_clear_color.color.rawColor[1] = 64;
51 m_clear_color.color.rawColor[2] = 64;
52 m_clear_color.color.rawColor[3] = 0;
53 m_clear_color.useRawValue = true;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060054}
55
Tony Barbour6918cd52015-04-09 12:58:51 -060056VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060057{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060058
59}
60
Tony Barbour6918cd52015-04-09 12:58:51 -060061void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060062{
Tony Barbour9b9fe782015-04-23 15:28:27 -060063 const std::vector<const char *> extensions;
64 InitFramework(extensions);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060065}
66
Tony Barbour15524c32015-04-29 17:34:29 -060067void VkRenderFramework::InitFramework(const std::vector<const char *> &extensions, VK_DBG_MSG_CALLBACK_FUNCTION dbgFunction, void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060068{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060069 VkResult err;
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060070 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060071 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060072 instInfo.pNext = NULL;
73 instInfo.pAppInfo = &app_info;
74 instInfo.pAllocCb = NULL;
Tony Barbour9b9fe782015-04-23 15:28:27 -060075 instInfo.extensionCount = extensions.size();
76 instInfo.ppEnabledExtensionNames = (extensions.size()) ? &extensions[0] : NULL;;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060077 err = vkCreateInstance(&instInfo, &this->inst);
78 ASSERT_VK_SUCCESS(err);
Jon Ashburn83a64252015-04-15 11:31:12 -060079 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
80 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
81 ASSERT_VK_SUCCESS(err);
82 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060083 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070084 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -060085 if (dbgFunction) {
86 err = vkDbgRegisterMsgCallback(this->inst, dbgFunction, userData);
87 ASSERT_VK_SUCCESS(err);
88 }
Tony Barbour9b9fe782015-04-23 15:28:27 -060089 m_device = new VkDeviceObj(0, objs[0]);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060090 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -060091
Tony Barbour6918cd52015-04-09 12:58:51 -060092 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060093}
94
Tony Barbour6918cd52015-04-09 12:58:51 -060095void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060096{
Mike Stroyanb050c682015-04-17 12:36:38 -060097 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
98 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
99 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
100 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
101 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
102 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600103
104 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600105 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600106 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600107 while (!m_renderTargets.empty()) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600108 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
109 vkQueueBindObjectMemory(m_device->m_queue, VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image(), 0, VK_NULL_HANDLE, 0);
110 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
111 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600112 m_renderTargets.pop_back();
113 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600114
Tony Barbour1c45ce02015-03-27 17:03:18 -0600115 delete m_depthStencil;
116
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600117 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800118 delete m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600119 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600120}
121
Tony Barbour6918cd52015-04-09 12:58:51 -0600122void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600123{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600124 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600125
Tony Barbourd1c35722015-04-16 15:59:00 -0600126 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600127
128 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600129 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600130 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700131 raster.pointSize = 1.0;
132
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600133 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
134 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600135
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600136 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600137 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
138 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
139 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600140
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600141 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600142 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600143 depthStencil.minDepth = 0.f;
144 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700145 depthStencil.stencilFrontRef = 0;
146 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600147
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600148 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
149 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600150
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600151 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600152
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600153 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700154 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
155
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600156 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
157 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600158}
159
Tony Barbour6918cd52015-04-09 12:58:51 -0600160void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600161{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600162 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600163
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600164 VkViewport viewport;
165 VkRect scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600166
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600167 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600168 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700169 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700170 viewport.originX = 0;
171 viewport.originY = 0;
172 viewport.width = 1.f * width;
173 viewport.height = 1.f * height;
174 viewport.minDepth = 0.f;
175 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700176 scissor.extent.width = width;
177 scissor.extent.height = height;
178 scissor.offset.x = 0;
179 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700180 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700181 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700182
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600183 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
184 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600185 m_width = width;
186 m_height = height;
187}
188
Tony Barbour6918cd52015-04-09 12:58:51 -0600189void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600190{
191 InitViewport(m_width, m_height);
192}
Tony Barbour6918cd52015-04-09 12:58:51 -0600193void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600194{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700195 InitRenderTarget(1);
196}
197
Tony Barbour6918cd52015-04-09 12:58:51 -0600198void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700199{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600200 InitRenderTarget(targets, NULL);
201}
202
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600203void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600204{
205 InitRenderTarget(1, dsBinding);
206}
207
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600208void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600209{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600210 std::vector<VkAttachmentLoadOp> load_ops;
211 std::vector<VkAttachmentStoreOp> store_ops;
212 std::vector<VkClearColor> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600213
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600214 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800215
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700216 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600217 VkImageObj *img = new VkImageObj(m_device);
Chia-I Wuf50ee212014-12-29 14:31:52 +0800218 img->init(m_width, m_height, m_render_target_fmt,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600219 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700220 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600221 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700222 m_renderTargets.push_back(img);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600223 load_ops.push_back(VK_ATTACHMENT_LOAD_OP_LOAD);
224 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600225 clear_colors.push_back(m_clear_color);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600226// m_mem_ref_mgr.AddMemoryRefs(*img);
Chia-I Wuecebf752014-12-05 10:45:15 +0800227 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700228 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600229 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600230 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600231 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700232 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600233 fb_info.pColorAttachments = m_colorBindings;
234 fb_info.pDepthStencilAttachment = dsBinding;
235 fb_info.sampleCount = 1;
236 fb_info.width = (uint32_t)m_width;
237 fb_info.height = (uint32_t)m_height;
238 fb_info.layers = 1;
239
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600240 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600241
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600242 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600243 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700244 rp_info.renderArea.extent.width = m_width;
245 rp_info.renderArea.extent.height = m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600246
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700247 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600248 rp_info.pColorFormats = &m_render_target_fmt;
249 rp_info.pColorLayouts = &m_colorBindings[0].layout;
250 rp_info.pColorLoadOps = &load_ops[0];
251 rp_info.pColorStoreOps = &store_ops[0];
252 rp_info.pColorLoadClearValues = &clear_colors[0];
253 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600254 if (dsBinding) {
255 rp_info.depthStencilLayout = dsBinding->layout;
256 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600257 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600258 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600259 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
260 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600261 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600262 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
263 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600264}
265
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600266
267
Tony Barbourd1c35722015-04-16 15:59:00 -0600268VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600269 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800270{
271 init();
272
273 props = gpu().properties();
274 queue_props = &gpu().queue_properties()[0];
275}
276
Tony Barbour6918cd52015-04-09 12:58:51 -0600277void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800278{
279 ASSERT_NE(true, graphics_queues().empty());
280 m_queue = graphics_queues()[0]->obj();
281}
282
Tony Barbour6918cd52015-04-09 12:58:51 -0600283VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700284{
285 m_device = device;
286 m_nextSlot = 0;
287
288}
289
Tony Barbour6918cd52015-04-09 12:58:51 -0600290VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700291{
Chia-I Wu11078b02015-01-04 16:27:24 +0800292 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700293}
Tony Barbour82c39522014-12-04 14:33:33 -0700294
Tony Barbour6918cd52015-04-09 12:58:51 -0600295int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700296{
Chia-I Wu11078b02015-01-04 16:27:24 +0800297 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600298 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600299 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800300 tc.count = 1;
301 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700302
Chia-I Wu11078b02015-01-04 16:27:24 +0800303 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700304}
Tony Barbour82c39522014-12-04 14:33:33 -0700305
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600306int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700307{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600308 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800309 tc.type = type;
310 tc.count = 1;
311 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700312
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600313 m_updateBuffers.push_back(vk_testing::DescriptorSet::update(type,
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600314 m_nextSlot, 0, 1, &constantBuffer.m_bufferViewInfo));
315
316 // Track mem references for this descriptor set object
317 mem_ref_mgr.AddMemoryRefs(constantBuffer);
Chia-I Wu11078b02015-01-04 16:27:24 +0800318
319 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700320}
Tony Barbour82c39522014-12-04 14:33:33 -0700321
Tony Barbour6918cd52015-04-09 12:58:51 -0600322int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700323{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600324 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600325 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800326 tc.count = 1;
327 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700328
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600329 VkSamplerImageViewInfo tmp = {};
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600330 tmp.sampler = sampler->obj();
Chia-I Wu11078b02015-01-04 16:27:24 +0800331 tmp.pImageView = &texture->m_textureViewInfo;
332 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700333
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600334 m_updateSamplerTextures.push_back(vk_testing::DescriptorSet::update(m_nextSlot, 0, 1,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600335 (const VkSamplerImageViewInfo *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700336
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600337 // Track mem references for the texture referenced here
338 mem_ref_mgr.AddMemoryRefs(*texture);
339
Chia-I Wu11078b02015-01-04 16:27:24 +0800340 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700341}
Chia-I Wu11078b02015-01-04 16:27:24 +0800342
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500343VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700344{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500345 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700346}
347
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600348VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700349{
Chia-I Wu11078b02015-01-04 16:27:24 +0800350 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700351}
Tony Barboure2c58df2014-11-25 13:18:32 -0700352
Tony Barbour6918cd52015-04-09 12:58:51 -0600353void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700354{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600355 // create VkDescriptorPool
356 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600357 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800358 pool.count = m_type_counts.size();
359 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600360 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700361
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600362 // create VkDescriptorSetLayout
363 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800364 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800365 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800366 bindings[i].descriptorType = m_type_counts[i].type;
367 bindings[i].count = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600368 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800369 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700370 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800371
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600372 // create VkDescriptorSetLayout
373 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600374 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800375 layout.count = bindings.size();
376 layout.pBinding = &bindings[0];
377
Chia-I Wu41126e52015-03-26 15:27:55 +0800378 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600379 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800380 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500381
382 // create VkPipelineLayout
383 VkPipelineLayoutCreateInfo pipeline_layout = {};
384 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
385 pipeline_layout.descriptorSetCount = layouts.size();
386 pipeline_layout.pSetLayouts = NULL;
387
388 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700389
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600390 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500391 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800392
Chia-I Wu41126e52015-03-26 15:27:55 +0800393 // build the update array
394 vector<const void *> update_array;
Chia-I Wu11078b02015-01-04 16:27:24 +0800395
Chia-I Wu41126e52015-03-26 15:27:55 +0800396 for (int i = 0; i < m_updateBuffers.size(); i++) {
397 update_array.push_back(&m_updateBuffers[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800398 }
399 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
400 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
Chia-I Wu41126e52015-03-26 15:27:55 +0800401 update_array.push_back(&m_updateSamplerTextures[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800402 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800403
404 // do the updates
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600405 m_device->begin_descriptor_pool_update(VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu11078b02015-01-04 16:27:24 +0800406 clear_sets(*m_set);
Chia-I Wu41126e52015-03-26 15:27:55 +0800407 m_set->update(update_array);
Chia-I Wu985ba162015-03-26 13:14:16 +0800408 m_device->end_descriptor_pool_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700409}
Tony Barboure2c58df2014-11-25 13:18:32 -0700410
Tony Barbour6918cd52015-04-09 12:58:51 -0600411VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800412{
413 m_device = dev;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600414 m_imageInfo.view = VK_NULL_HANDLE;
415 m_imageInfo.layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800416}
417
Tony Barbour6918cd52015-04-09 12:58:51 -0600418void VkImageObj::ImageMemoryBarrier(
419 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600420 VkImageAspect aspect,
421 VkFlags output_mask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600422 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
423 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
424 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
425 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
426 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600427 VkFlags input_mask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600428 VK_MEMORY_INPUT_CPU_READ_BIT |
429 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
430 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
431 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
432 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
433 VK_MEMORY_INPUT_SHADER_READ_BIT |
434 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
435 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
436 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600437 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600438{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600439 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
440 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600441 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
442 subresourceRange);
443
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600444 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600445
Tony Barbourd1c35722015-04-16 15:59:00 -0600446 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600447
448 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600449 vkCmdPipelineBarrier(cmd_buf->obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, pipe_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600450}
451
Tony Barbour6918cd52015-04-09 12:58:51 -0600452void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600453 VkImageAspect aspect,
454 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600455{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600456 VkFlags output_mask, input_mask;
457 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600458 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
459 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
460 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
461 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600462 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600463 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600464 VK_MEMORY_INPUT_CPU_READ_BIT |
465 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
466 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
467 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
468 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
469 VK_MEMORY_INPUT_SHADER_READ_BIT |
470 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
471 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600472 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600473
474 if (image_layout == m_imageInfo.layout) {
475 return;
476 }
477
478 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600479 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600480 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
481 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600482 break;
483
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600484 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600485 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
486 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600487 break;
488
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600489 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600490 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
491 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600492 break;
493
Tony Barbourf20f87b2015-04-22 09:02:32 -0600494 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600495 default:
496 output_mask = all_cache_outputs;
497 input_mask = all_cache_inputs;
498 break;
499 }
500
501 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
502 m_imageInfo.layout = image_layout;
503}
504
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600505void VkImageObj::SetLayout(VkImageAspect aspect,
506 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600507{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600508 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600509 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600510
511 /* Build command buffer to set image layout in the driver */
512 err = cmd_buf.BeginCommandBuffer();
513 assert(!err);
514
515 SetLayout(&cmd_buf, aspect, image_layout);
516
517 err = cmd_buf.EndCommandBuffer();
518 assert(!err);
519
520 cmd_buf.QueueCommandBuffer();
521}
522
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600523bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600524{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600525 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600526 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600527 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600528
Tony Barbour579f7802015-04-03 15:11:43 -0600529 return true;
530}
531
Tony Barbour6918cd52015-04-09 12:58:51 -0600532void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600533 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600534 VkImageTiling requested_tiling,
535 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800536{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600537 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600538 VkFormatProperties image_fmt;
539 VkImageTiling tiling;
540 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600541 size_t size;
542
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800543 mipCount = 0;
544
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600545 uint32_t _w = w;
546 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800547 while( ( _w > 0 ) || ( _h > 0 ) )
548 {
549 _w >>= 1;
550 _h >>= 1;
551 mipCount++;
552 }
553
Tony Barbour579f7802015-04-03 15:11:43 -0600554 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600555 err = vkGetFormatInfo(m_device->obj(), fmt,
Tony Barbourd1c35722015-04-16 15:59:00 -0600556 VK_FORMAT_INFO_TYPE_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600557 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600558 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600559
Tony Barbourd1c35722015-04-16 15:59:00 -0600560 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600561 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600562 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600563 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600564 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600565 } else {
566 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
567 }
568 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600569 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600570 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600571 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600572 } else {
573 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
574 }
575
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600576 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600577 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800578 imageCreateInfo.format = fmt;
579 imageCreateInfo.extent.width = w;
580 imageCreateInfo.extent.height = h;
581 imageCreateInfo.mipLevels = mipCount;
582 imageCreateInfo.tiling = tiling;
583
584 imageCreateInfo.usage = usage;
585
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600586 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800587
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600588 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600589 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600590 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600591 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600592 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800593}
594
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600595VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800596{
597 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600598 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800599}
600
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600601VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800602{
603 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600604 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800605}
606
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600607VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600608{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600609 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600610 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600611 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600612
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600613 /* Build command buffer to copy staging texture to usable texture */
614 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600615 assert(!err);
616
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600617 /* TODO: Can we determine image aspect from image object? */
618 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600619 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600620
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600621 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600622 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600623
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600624 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600625 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600626 copy_region.srcSubresource.arraySlice = 0;
627 copy_region.srcSubresource.mipLevel = 0;
628 copy_region.srcOffset.x = 0;
629 copy_region.srcOffset.y = 0;
630 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600631 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600632 copy_region.destSubresource.arraySlice = 0;
633 copy_region.destSubresource.mipLevel = 0;
634 copy_region.destOffset.x = 0;
635 copy_region.destOffset.y = 0;
636 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600637 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600638
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600639 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600640 src_image.obj(), src_image.layout(),
641 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600642 1, &copy_region);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600643 cmd_buf.mem_ref_mgr.AddMemoryRefs(src_image);
644 cmd_buf.mem_ref_mgr.AddMemoryRefs(*this);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600645
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600646 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600647
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600648 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600649
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600650 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600651 assert(!err);
652
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600653 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600654
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600655 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600656}
657
Tony Barbour6918cd52015-04-09 12:58:51 -0600658VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
659 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700660{
661 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600662 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600663 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600664 void *data;
665 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600666 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600667 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600668
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600669 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600670 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600671
672 if (colors == NULL)
673 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700674
675 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
676
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600677 m_textureViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700678
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600679 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600680 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600681 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600682 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600683 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600684 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600685 view.channels.r = VK_CHANNEL_SWIZZLE_R;
686 view.channels.g = VK_CHANNEL_SWIZZLE_G;
687 view.channels.b = VK_CHANNEL_SWIZZLE_B;
688 view.channels.a = VK_CHANNEL_SWIZZLE_A;
689 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600690 view.subresourceRange.baseMipLevel = 0;
691 view.subresourceRange.mipLevels = 1;
692 view.subresourceRange.baseArraySlice = 0;
693 view.subresourceRange.arraySize = 1;
694 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700695
Tony Barboure2c58df2014-11-25 13:18:32 -0700696 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600697 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700698
699 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800700 view.image = obj();
701 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600702 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700703
Tony Barbour5dc515d2015-04-01 17:47:06 -0600704 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700705
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800706 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700707 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800708 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600709 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700710 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600711 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600712 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700713}
Tony Barbour82c39522014-12-04 14:33:33 -0700714
Tony Barbour6918cd52015-04-09 12:58:51 -0600715VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700716{
Tony Barboure2c58df2014-11-25 13:18:32 -0700717 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700718
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600719 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800720 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600721 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
722 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
723 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600724 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600725 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
726 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
727 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800728 samplerCreateInfo.mipLodBias = 0.0;
729 samplerCreateInfo.maxAnisotropy = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600730 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800731 samplerCreateInfo.minLod = 0.0;
732 samplerCreateInfo.maxLod = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600733 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700734
Chia-I Wue9864b52014-12-28 16:32:24 +0800735 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700736}
Tony Barboure2c58df2014-11-25 13:18:32 -0700737
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700738/*
739 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
740 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600741VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700742{
743 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700744 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700745
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800746 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700747}
748
Tony Barbour6918cd52015-04-09 12:58:51 -0600749VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600750{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600751 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600752 if (m_commandBuffer) {
753 delete m_commandBuffer;
754 }
755}
756
Tony Barbour6918cd52015-04-09 12:58:51 -0600757VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700758{
Tony Barboure2c58df2014-11-25 13:18:32 -0700759 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800760 m_commandBuffer = 0;
761
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800762 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700763 m_numVertices = constantCount;
764 m_stride = constantSize;
765
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600766 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800767 const size_t allocationSize = constantCount * constantSize;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600768 init(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700769
Chia-I Wua07fee62014-12-28 15:26:08 +0800770 void *pData = map();
771 memcpy(pData, data, allocationSize);
772 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700773
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800774 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600775 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600776 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800777 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600778 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800779 view_info.offset = 0;
780 view_info.range = allocationSize;
781 m_bufferView.init(*m_device, view_info);
782
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600783 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800784 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700785}
Tony Barbour82c39522014-12-04 14:33:33 -0700786
Tony Barbourd1c35722015-04-16 15:59:00 -0600787void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700788{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600789 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700790}
791
792
Tony Barbour6918cd52015-04-09 12:58:51 -0600793void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600794 VkFlags outputMask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600795 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
796 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
797 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
798 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
799 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600800 VkFlags inputMask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600801 VK_MEMORY_INPUT_CPU_READ_BIT |
802 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
803 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
804 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
805 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
806 VK_MEMORY_INPUT_SHADER_READ_BIT |
807 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
808 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
809 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700810{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600811 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700812
Tony Barbour38422802014-12-10 14:36:31 -0700813 if (!m_commandBuffer)
814 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600815 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700816
Tony Barbour6918cd52015-04-09 12:58:51 -0600817 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700818 }
819 else
820 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800821 m_device->wait(m_fence);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600822 m_commandBuffer->mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour38422802014-12-10 14:36:31 -0700823 }
824
Tony Barboure2c58df2014-11-25 13:18:32 -0700825 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600826 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600827 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600828 cmd_buf_info.pNext = NULL;
829 cmd_buf_info.flags = 0;
830
Jon Ashburnc4164b12014-12-31 17:10:47 -0700831 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600832 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700833
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600834 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000835 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600836 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700837
Tony Barbourd1c35722015-04-16 15:59:00 -0600838 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000839
840 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600841 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700842
843 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700844 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600845 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700846
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600847 /*
848 * Tell driver about memory references made in this command buffer
849 * Note: Since this command buffer only has a PipelineBarrier
850 * command there really aren't any memory refs to worry about.
851 */
852 m_commandBuffer->mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
Tony Barboure2c58df2014-11-25 13:18:32 -0700853
854 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600855 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700856 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600857 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
858 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700859}
860
Tony Barbour6918cd52015-04-09 12:58:51 -0600861VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
862 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700863{
864
865}
866
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600867void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700868{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600869 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700870
871 m_numVertices = numIndexes;
872 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700873 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600874 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700875 m_stride = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -0600876 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700877 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600878 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700879 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600880 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700881 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600882 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700883 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600884 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700885 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800886 default:
887 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600888 m_stride = 2;
889 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800890 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700891 }
892
Chia-I Wua07fee62014-12-28 15:26:08 +0800893 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600894 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
895 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700896
Chia-I Wua07fee62014-12-28 15:26:08 +0800897 void *pData = map();
898 memcpy(pData, data, allocationSize);
899 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700900
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800901 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600902 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600903 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800904 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600905 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700906 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800907 view_info.offset = 0;
908 view_info.range = allocationSize;
909 m_bufferView.init(*m_device, view_info);
910
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600911 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800912 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700913}
914
Tony Barbourd1c35722015-04-16 15:59:00 -0600915void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700916{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600917 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700918}
Tony Barboure2c58df2014-11-25 13:18:32 -0700919
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600920VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -0700921{
922 return m_indexType;
923}
924
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600925VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700926{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600927 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600928 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700929 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800930 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700931 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600932 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700933
Tony Barboure2c58df2014-11-25 13:18:32 -0700934 return stageInfo;
935}
936
Tony Barbourd1c35722015-04-16 15:59:00 -0600937VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -0700938{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600939 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600940 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600941 VkShaderCreateInfo createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700942 size_t shader_len;
943
944 m_stage = stage;
945 m_device = device;
946
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600947 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700948 createInfo.pNext = NULL;
949
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600950 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700951
952 shader_len = strlen(shader_code);
953 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
954 createInfo.pCode = malloc(createInfo.codeSize);
955 createInfo.flags = 0;
956
Tony Barbourd1c35722015-04-16 15:59:00 -0600957 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600958 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700959 ((uint32_t *) createInfo.pCode)[1] = 0;
960 ((uint32_t *) createInfo.pCode)[2] = stage;
961 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
962
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800963 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700964 }
965
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600966 if (framework->m_use_spv || err) {
967 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600968 err = VK_SUCCESS;
Tony Barboure2c58df2014-11-25 13:18:32 -0700969
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600970 // Use Reference GLSL to SPV compiler
971 framework->GLSLtoSPV(stage, shader_code, spv);
972 createInfo.pCode = spv.data();
973 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700974 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700975
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800976 init(*m_device, createInfo);
977 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700978}
Tony Barbour82c39522014-12-04 14:33:33 -0700979
Tony Barbour6918cd52015-04-09 12:58:51 -0600980VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700981{
Tony Barboure2c58df2014-11-25 13:18:32 -0700982 m_device = device;
983 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
984 m_vertexBufferCount = 0;
985
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600986 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
987 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600988 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600989 m_ia_state.disableVertexReuse = VK_FALSE;
990 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700991 m_ia_state.primitiveRestartIndex = 0;
992
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600993 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700994 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600995 m_rs_state.depthClipEnable = VK_FALSE;
996 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
997 m_rs_state.programPointSize = VK_FALSE;
998 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
999 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001000 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001001 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1002 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001003
Tony Barboure2c58df2014-11-25 13:18:32 -07001004 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001005 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001006 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001007 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1008 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001009
Tony Barbourf52346d2015-01-16 14:27:35 -07001010 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001011 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1012 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001013 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1014 m_ms_state.samples = 1;
1015 m_ms_state.minSampleShading = 0;
1016 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001017
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001018 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -07001019 m_ds_state.pNext = &m_ms_state,
Tony Barbourd1c35722015-04-16 15:59:00 -06001020 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001021 m_ds_state.depthTestEnable = VK_FALSE;
1022 m_ds_state.depthWriteEnable = VK_FALSE;
1023 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001024 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001025 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1026 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1027 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001028 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001029 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001030 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001031
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001032 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001033 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001034 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001035 att.channelWriteMask = 0xf;
1036 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001037
1038};
1039
Tony Barbour6918cd52015-04-09 12:58:51 -06001040void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001041{
1042 m_shaderObjs.push_back(shader);
1043}
1044
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001045void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001046{
1047 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1048 m_vi_state.attributeCount = count;
1049}
1050
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001051void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001052{
1053 m_vi_state.pVertexBindingDescriptions = vi_binding;
1054 m_vi_state.bindingCount = count;
1055}
1056
Tony Barbour6918cd52015-04-09 12:58:51 -06001057void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001058{
1059 m_vertexBufferObjs.push_back(vertexDataBuffer);
1060 m_vertexBufferBindings.push_back(binding);
1061 m_vertexBufferCount++;
1062}
1063
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001064void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001065{
Tony Barbourf52346d2015-01-16 14:27:35 -07001066 if (binding+1 > m_colorAttachments.size())
1067 {
1068 m_colorAttachments.resize(binding+1);
1069 }
1070 m_colorAttachments[binding] = *att;
1071}
1072
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001073void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001074{
1075 m_ds_state.format = ds_state->format;
1076 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1077 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1078 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001079 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001080 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1081 m_ds_state.back = ds_state->back;
1082 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001083}
1084
Tony Barbour6918cd52015-04-09 12:58:51 -06001085void VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001086{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001087 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001088 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001089
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001090 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001091
1092 for (int i=0; i<m_shaderObjs.size(); i++)
1093 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001094 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001095 shaderCreateInfo->pNext = head_ptr;
1096 head_ptr = shaderCreateInfo;
1097 }
1098
1099 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1100 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001101 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001102 m_vi_state.pNext = head_ptr;
1103 head_ptr = &m_vi_state;
1104 }
1105
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001106 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1107 info.pNext = head_ptr;
1108 info.flags = 0;
1109 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001110
Tony Barbourf52346d2015-01-16 14:27:35 -07001111 m_cb_state.attachmentCount = m_colorAttachments.size();
1112 m_cb_state.pAttachments = &m_colorAttachments[0];
1113
Chia-I Wu2648d092014-12-29 14:24:14 +08001114 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001115}
Chia-I Wu2648d092014-12-29 14:24:14 +08001116
Tony Barbourd1c35722015-04-16 15:59:00 -06001117vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001118{
Tony Barbourd1c35722015-04-16 15:59:00 -06001119 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001120 if (this->mem_refs_.size()) {
1121 mems.reserve(this->mem_refs_.size());
1122 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1123 mems.push_back(this->mem_refs_[i]);
1124 }
1125
1126 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001127}
1128
Tony Barbour6918cd52015-04-09 12:58:51 -06001129void VkMemoryRefManager::AddMemoryRefs(vk_testing::Object &vkObject)
Tony Barboure2c58df2014-11-25 13:18:32 -07001130{
Tony Barbourd1c35722015-04-16 15:59:00 -06001131 const std::vector<VkDeviceMemory> mems = vkObject.memories();
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001132 AddMemoryRefs(mems);
1133}
Tony Barboure2c58df2014-11-25 13:18:32 -07001134
Tony Barbourd1c35722015-04-16 15:59:00 -06001135void VkMemoryRefManager::AddMemoryRefs(vector<VkDeviceMemory> mem)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001136{
1137 for (size_t i = 0; i < mem.size(); i++) {
1138 if (mem[i] != NULL) {
1139 this->mem_refs_.push_back(mem[i]);
1140 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001141 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001142}
Tony Barbour82c39522014-12-04 14:33:33 -07001143
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001144void VkMemoryRefManager::EmitAddMemoryRefs(VkQueue queue)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001145{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001146 vkQueueAddMemReferences(queue, mem_refs_.size(), &mem_refs_[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001147}
Tony Barbour82c39522014-12-04 14:33:33 -07001148
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001149void VkMemoryRefManager::EmitRemoveMemoryRefs(VkQueue queue)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001150{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001151 vkQueueRemoveMemReferences(queue, mem_refs_.size(), &mem_refs_[0]);
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001152}
1153
Tony Barbour6918cd52015-04-09 12:58:51 -06001154VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001155 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001156{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001157 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001158}
Tony Barbour471338d2014-12-10 17:28:39 -07001159
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001160VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001161{
Chia-I Wud28343c2014-12-28 15:12:48 +08001162 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001163}
Tony Barbour471338d2014-12-10 17:28:39 -07001164
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001165VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001166{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001167 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001168 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001169}
1170
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001171VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001172{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001173 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001174 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001175}
1176
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001177VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001178{
1179 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001180 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001181}
1182
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001183VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001184{
Chia-I Wud28343c2014-12-28 15:12:48 +08001185 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001186 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001187}
1188
Tony Barbourd1c35722015-04-16 15:59:00 -06001189void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001190{
Tony Barbourd1c35722015-04-16 15:59:00 -06001191 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001192}
1193
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001194void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001195 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001196{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001197 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001198 const VkFlags output_mask =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001199 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
1200 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1201 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1202 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001203 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001204 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001205
1206 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001207 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001208 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001209 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001210 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001211 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001212 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001213
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001214 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001215 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001216 memory_barrier.outputMask = output_mask;
1217 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001218 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001219 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001220 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001221
Tony Barbourd1c35722015-04-16 15:59:00 -06001222 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001223
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001224 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001225 memory_barrier.image = m_renderTargets[i]->image();
1226 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001227 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001228 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001229
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001230 vkCmdClearColorImage(obj(),
1231 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001232 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001233
1234 mem_ref_mgr.AddMemoryRefs(*m_renderTargets[i]);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001235 }
1236
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001237 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001238 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001239 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001240 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001241 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001242 dsRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001243 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001244 dsRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001245
1246 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001247
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001248 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001249 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001250 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001251 memory_barrier.subresourceRange = dsRange;
1252
Tony Barbourd1c35722015-04-16 15:59:00 -06001253 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001254
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001255 vkCmdClearDepthStencil(obj(),
1256 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001257 depth_clear_color, stencil_clear_color,
1258 1, &dsRange);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001259 mem_ref_mgr.AddMemoryRefs(*depthStencilObj);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001260
1261 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001262 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001263 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001264 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001265 memory_barrier.subresourceRange = dsRange;
Tony Barbourd1c35722015-04-16 15:59:00 -06001266 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001267 }
1268}
1269
Tony Barbour6918cd52015-04-09 12:58:51 -06001270void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001271{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001272 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001273 const VkFlags output_mask =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001274 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
1275 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1276 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1277 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001278 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001279 const VkFlags input_mask =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001280 VK_MEMORY_INPUT_CPU_READ_BIT |
1281 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1282 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1283 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1284 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1285 VK_MEMORY_INPUT_SHADER_READ_BIT |
1286 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1287 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001288 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001289
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001290 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001291 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001292 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001293 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001294 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001295 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001296
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001297 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001298 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001299 memory_barrier.outputMask = output_mask;
1300 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001301 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001302 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001303 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001304
Tony Barbourd1c35722015-04-16 15:59:00 -06001305 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001306
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001307 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001308 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001309 memory_barrier.image = m_renderTargets[i]->image();
1310 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001311 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001312 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001313 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001314}
1315
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001316void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001317{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001318 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001319 renderpass,
1320 framebuffer,
1321 };
1322
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001323 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001324}
1325
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001326void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001327{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001328 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001329}
1330
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001331void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001332{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001333 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001334}
1335
Tony Barbour6918cd52015-04-09 12:58:51 -06001336void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001337{
1338 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001339}
1340
Tony Barbour6918cd52015-04-09 12:58:51 -06001341void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001342{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001343 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001344}
1345
Tony Barbour6918cd52015-04-09 12:58:51 -06001346void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001347{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001348 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001349}
1350
Tony Barbour6918cd52015-04-09 12:58:51 -06001351void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001352{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001353 QueueCommandBuffer(NULL);
1354}
1355
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001356void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001357{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001358 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001359
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001360 mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
1361
Tony Barbour30cc9e82014-12-17 11:53:55 -07001362 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001363 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1364 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001365
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001366 err = vkQueueWaitIdle( m_device->m_queue );
1367 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001368
1369 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001370 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001371
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001372 /*
1373 * Now that processing on this command buffer is complete
1374 * we can remove the memory references.
1375 */
1376 mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001377}
1378
Tony Barbour6918cd52015-04-09 12:58:51 -06001379void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001380{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001381 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001382 mem_ref_mgr.AddMemoryRefs(pipeline);
1383}
1384
Tony Barbour6918cd52015-04-09 12:58:51 -06001385void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001386{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001387 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001388
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001389 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001390 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropd4c1a502015-04-16 13:41:56 -06001391 0, 1, &set_obj, 0, NULL );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001392
1393 // Add descriptor set mem refs to command buffer's list
1394 mem_ref_mgr.AddMemoryRefs(descriptorSet.memories());
1395 mem_ref_mgr.AddMemoryRefs(descriptorSet.mem_ref_mgr.mem_refs());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001396}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001397
Tony Barbour6918cd52015-04-09 12:58:51 -06001398void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001399{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001400 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001401 mem_ref_mgr.AddMemoryRefs(*indexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001402}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001403
Tony Barbourd1c35722015-04-16 15:59:00 -06001404void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001405{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001406 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001407 mem_ref_mgr.AddMemoryRefs(*vertexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001408}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001409
Tony Barbour6918cd52015-04-09 12:58:51 -06001410VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001411{
1412 m_initialized = false;
1413}
Tony Barbour6918cd52015-04-09 12:58:51 -06001414bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001415{
1416 return m_initialized;
1417}
1418
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001419VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001420{
1421 return &m_depthStencilBindInfo;
1422}
1423
Tony Barbour6918cd52015-04-09 12:58:51 -06001424void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001425{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001426 VkImageCreateInfo image_info;
1427 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001428
1429 m_device = device;
1430 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001431 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001432
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001433 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001434 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001435 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001436 image_info.format = m_depth_stencil_fmt;
1437 image_info.extent.width = width;
1438 image_info.extent.height = height;
1439 image_info.extent.depth = 1;
1440 image_info.mipLevels = 1;
1441 image_info.arraySize = 1;
1442 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001443 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001444 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001445 image_info.flags = 0;
1446 init(*m_device, image_info);
1447
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001448 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001449 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001450 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001451 view_info.mipLevel = 0;
1452 view_info.baseArraySlice = 0;
1453 view_info.arraySize = 1;
1454 view_info.flags = 0;
1455 view_info.image = obj();
1456 m_depthStencilView.init(*m_device, view_info);
1457
1458 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001459 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001460}