blob: e64284216ac70258365e86fdbc5b6f6db2c21abe [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());
Mark Lobodzinski23065352015-05-29 09:32:35 -0500109 vkBindObjectMemory(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image(), VK_NULL_HANDLE, 0);
Mike Stroyanb050c682015-04-17 12:36:38 -0600110 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);
Chia-I Wuecebf752014-12-05 10:45:15 +0800226 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700227 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600228 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600229 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600230 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700231 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600232 fb_info.pColorAttachments = m_colorBindings;
233 fb_info.pDepthStencilAttachment = dsBinding;
234 fb_info.sampleCount = 1;
235 fb_info.width = (uint32_t)m_width;
236 fb_info.height = (uint32_t)m_height;
237 fb_info.layers = 1;
238
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600239 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600240
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600241 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600242 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700243 rp_info.renderArea.extent.width = m_width;
244 rp_info.renderArea.extent.height = m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600245
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700246 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600247 rp_info.pColorFormats = &m_render_target_fmt;
248 rp_info.pColorLayouts = &m_colorBindings[0].layout;
249 rp_info.pColorLoadOps = &load_ops[0];
250 rp_info.pColorStoreOps = &store_ops[0];
251 rp_info.pColorLoadClearValues = &clear_colors[0];
252 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600253 if (dsBinding) {
254 rp_info.depthStencilLayout = dsBinding->layout;
255 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600256 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600257 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600258 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
259 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600260 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600261 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
262 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600263}
264
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600265
266
Tony Barbourd1c35722015-04-16 15:59:00 -0600267VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600268 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800269{
270 init();
271
272 props = gpu().properties();
273 queue_props = &gpu().queue_properties()[0];
274}
275
Tony Barbour6918cd52015-04-09 12:58:51 -0600276void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800277{
278 ASSERT_NE(true, graphics_queues().empty());
279 m_queue = graphics_queues()[0]->obj();
280}
281
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600282VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
283 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700284{
Tony Barboure2c58df2014-11-25 13:18:32 -0700285
286}
287
Tony Barbour6918cd52015-04-09 12:58:51 -0600288VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700289{
Chia-I Wu11078b02015-01-04 16:27:24 +0800290 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700291}
Tony Barbour82c39522014-12-04 14:33:33 -0700292
Tony Barbour6918cd52015-04-09 12:58:51 -0600293int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700294{
Chia-I Wu11078b02015-01-04 16:27:24 +0800295 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600296 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600297 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800298 tc.count = 1;
299 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700300
Chia-I Wu11078b02015-01-04 16:27:24 +0800301 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700302}
Tony Barbour82c39522014-12-04 14:33:33 -0700303
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600304int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700305{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600306 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800307 tc.type = type;
308 tc.count = 1;
309 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700310
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600311 m_updateBuffers.push_back(vk_testing::DescriptorSet::update(type,
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600312 m_nextSlot, 0, 1, &constantBuffer.m_bufferViewInfo));
313
Chia-I Wu11078b02015-01-04 16:27:24 +0800314 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700315}
Tony Barbour82c39522014-12-04 14:33:33 -0700316
Tony Barbour6918cd52015-04-09 12:58:51 -0600317int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700318{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600319 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600320 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800321 tc.count = 1;
322 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700323
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600324 VkSamplerImageViewInfo tmp = {};
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600325 tmp.sampler = sampler->obj();
Chia-I Wu11078b02015-01-04 16:27:24 +0800326 tmp.pImageView = &texture->m_textureViewInfo;
327 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700328
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600329 m_updateSamplerTextures.push_back(vk_testing::DescriptorSet::update(m_nextSlot, 0, 1,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600330 (const VkSamplerImageViewInfo *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700331
Chia-I Wu11078b02015-01-04 16:27:24 +0800332 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700333}
Chia-I Wu11078b02015-01-04 16:27:24 +0800334
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500335VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700336{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500337 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700338}
339
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600340VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700341{
Chia-I Wu11078b02015-01-04 16:27:24 +0800342 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700343}
Tony Barboure2c58df2014-11-25 13:18:32 -0700344
Tony Barbour6918cd52015-04-09 12:58:51 -0600345void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700346{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600347 // create VkDescriptorPool
348 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600349 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800350 pool.count = m_type_counts.size();
351 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600352 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700353
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600354 // create VkDescriptorSetLayout
355 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800356 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800357 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800358 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800359 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600360 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800361 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700362 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800363
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600364 // create VkDescriptorSetLayout
365 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600366 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800367 layout.count = bindings.size();
368 layout.pBinding = &bindings[0];
369
Chia-I Wu41126e52015-03-26 15:27:55 +0800370 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600371 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800372 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500373
374 // create VkPipelineLayout
375 VkPipelineLayoutCreateInfo pipeline_layout = {};
376 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
377 pipeline_layout.descriptorSetCount = layouts.size();
378 pipeline_layout.pSetLayouts = NULL;
379
380 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700381
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600382 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500383 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800384
Chia-I Wu41126e52015-03-26 15:27:55 +0800385 // build the update array
386 vector<const void *> update_array;
Chia-I Wu11078b02015-01-04 16:27:24 +0800387
Chia-I Wu41126e52015-03-26 15:27:55 +0800388 for (int i = 0; i < m_updateBuffers.size(); i++) {
389 update_array.push_back(&m_updateBuffers[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800390 }
391 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
392 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
Chia-I Wu41126e52015-03-26 15:27:55 +0800393 update_array.push_back(&m_updateSamplerTextures[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800394 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800395
396 // do the updates
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600397 m_device->begin_descriptor_pool_update(VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu11078b02015-01-04 16:27:24 +0800398 clear_sets(*m_set);
Chia-I Wu41126e52015-03-26 15:27:55 +0800399 m_set->update(update_array);
Chia-I Wu985ba162015-03-26 13:14:16 +0800400 m_device->end_descriptor_pool_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700401}
Tony Barboure2c58df2014-11-25 13:18:32 -0700402
Tony Barbour6918cd52015-04-09 12:58:51 -0600403VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800404{
405 m_device = dev;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600406 m_imageInfo.view = VK_NULL_HANDLE;
407 m_imageInfo.layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800408}
409
Tony Barbour6918cd52015-04-09 12:58:51 -0600410void VkImageObj::ImageMemoryBarrier(
411 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600412 VkImageAspect aspect,
413 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600414 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600415 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
416 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
417 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
418 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600419 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600420 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600421 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
422 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
423 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
424 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
425 VK_MEMORY_INPUT_SHADER_READ_BIT |
426 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
427 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
428 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600429 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600430{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600431 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
432 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600433 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
434 subresourceRange);
435
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600436 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600437
Tony Barbourd1c35722015-04-16 15:59:00 -0600438 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600439
440 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600441 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 -0600442}
443
Tony Barbour6918cd52015-04-09 12:58:51 -0600444void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600445 VkImageAspect aspect,
446 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600447{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600448 VkFlags output_mask, input_mask;
449 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600450 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600451 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
452 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
453 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600454 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600455 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600456 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600457 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
458 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
459 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
460 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
461 VK_MEMORY_INPUT_SHADER_READ_BIT |
462 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
463 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600464 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600465
466 if (image_layout == m_imageInfo.layout) {
467 return;
468 }
469
470 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600471 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600472 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
473 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600474 break;
475
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600476 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600477 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
478 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600479 break;
480
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600481 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600482 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
483 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600484 break;
485
Tony Barbourf20f87b2015-04-22 09:02:32 -0600486 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600487 default:
488 output_mask = all_cache_outputs;
489 input_mask = all_cache_inputs;
490 break;
491 }
492
493 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
494 m_imageInfo.layout = image_layout;
495}
496
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600497void VkImageObj::SetLayout(VkImageAspect aspect,
498 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600499{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600500 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600501 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600502
503 /* Build command buffer to set image layout in the driver */
504 err = cmd_buf.BeginCommandBuffer();
505 assert(!err);
506
507 SetLayout(&cmd_buf, aspect, image_layout);
508
509 err = cmd_buf.EndCommandBuffer();
510 assert(!err);
511
512 cmd_buf.QueueCommandBuffer();
513}
514
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600515bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600516{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600517 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600518 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600519 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600520
Tony Barbour579f7802015-04-03 15:11:43 -0600521 return true;
522}
523
Tony Barbour6918cd52015-04-09 12:58:51 -0600524void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600525 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600526 VkImageTiling requested_tiling,
527 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800528{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600529 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600530 VkFormatProperties image_fmt;
531 VkImageTiling tiling;
532 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600533 size_t size;
534
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800535 mipCount = 0;
536
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600537 uint32_t _w = w;
538 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800539 while( ( _w > 0 ) || ( _h > 0 ) )
540 {
541 _w >>= 1;
542 _h >>= 1;
543 mipCount++;
544 }
545
Tony Barbour579f7802015-04-03 15:11:43 -0600546 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600547 err = vkGetFormatInfo(m_device->obj(), fmt,
Tony Barbourd1c35722015-04-16 15:59:00 -0600548 VK_FORMAT_INFO_TYPE_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600549 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600550 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600551
Tony Barbourd1c35722015-04-16 15:59:00 -0600552 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600553 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600554 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600555 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600556 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600557 } else {
558 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
559 }
560 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600561 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600562 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600563 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600564 } else {
565 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
566 }
567
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600568 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600569 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800570 imageCreateInfo.format = fmt;
571 imageCreateInfo.extent.width = w;
572 imageCreateInfo.extent.height = h;
573 imageCreateInfo.mipLevels = mipCount;
574 imageCreateInfo.tiling = tiling;
575
576 imageCreateInfo.usage = usage;
577
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600578 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800579
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600580 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600581 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600582 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600583 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600584 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800585}
586
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600587VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800588{
589 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600590 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800591}
592
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600593VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800594{
595 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600596 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800597}
598
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600599VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600600{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600601 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600602 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600603 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600604
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600605 /* Build command buffer to copy staging texture to usable texture */
606 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600607 assert(!err);
608
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600609 /* TODO: Can we determine image aspect from image object? */
610 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600611 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600612
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600613 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600614 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600615
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600616 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600617 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600618 copy_region.srcSubresource.arraySlice = 0;
619 copy_region.srcSubresource.mipLevel = 0;
620 copy_region.srcOffset.x = 0;
621 copy_region.srcOffset.y = 0;
622 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600623 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600624 copy_region.destSubresource.arraySlice = 0;
625 copy_region.destSubresource.mipLevel = 0;
626 copy_region.destOffset.x = 0;
627 copy_region.destOffset.y = 0;
628 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600629 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600630
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600631 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600632 src_image.obj(), src_image.layout(),
633 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600634 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600635
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600636 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600637
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600638 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600639
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600640 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600641 assert(!err);
642
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600643 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600644
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600645 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600646}
647
Tony Barbour6918cd52015-04-09 12:58:51 -0600648VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
649 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700650{
651 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600652 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600653 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600654 void *data;
655 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600656 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600657 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600658
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600659 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600660 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600661
662 if (colors == NULL)
663 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700664
665 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
666
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600667 m_textureViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700668
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600669 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600670 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600671 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600672 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600673 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600674 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600675 view.channels.r = VK_CHANNEL_SWIZZLE_R;
676 view.channels.g = VK_CHANNEL_SWIZZLE_G;
677 view.channels.b = VK_CHANNEL_SWIZZLE_B;
678 view.channels.a = VK_CHANNEL_SWIZZLE_A;
679 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600680 view.subresourceRange.baseMipLevel = 0;
681 view.subresourceRange.mipLevels = 1;
682 view.subresourceRange.baseArraySlice = 0;
683 view.subresourceRange.arraySize = 1;
684 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700685
Tony Barboure2c58df2014-11-25 13:18:32 -0700686 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600687 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700688
689 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800690 view.image = obj();
691 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600692 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700693
Tony Barbour5dc515d2015-04-01 17:47:06 -0600694 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700695
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800696 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700697 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800698 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600699 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700700 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600701 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600702 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700703}
Tony Barbour82c39522014-12-04 14:33:33 -0700704
Tony Barbour6918cd52015-04-09 12:58:51 -0600705VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700706{
Tony Barboure2c58df2014-11-25 13:18:32 -0700707 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700708
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600709 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800710 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600711 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
712 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
713 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600714 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600715 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
716 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
717 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800718 samplerCreateInfo.mipLodBias = 0.0;
719 samplerCreateInfo.maxAnisotropy = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600720 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800721 samplerCreateInfo.minLod = 0.0;
722 samplerCreateInfo.maxLod = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600723 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700724
Chia-I Wue9864b52014-12-28 16:32:24 +0800725 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700726}
Tony Barboure2c58df2014-11-25 13:18:32 -0700727
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700728/*
729 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
730 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600731VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700732{
733 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700734 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700735
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800736 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700737}
738
Tony Barbour6918cd52015-04-09 12:58:51 -0600739VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600740{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600741 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600742 if (m_commandBuffer) {
743 delete m_commandBuffer;
744 }
745}
746
Tony Barbour6918cd52015-04-09 12:58:51 -0600747VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700748{
Tony Barboure2c58df2014-11-25 13:18:32 -0700749 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800750 m_commandBuffer = 0;
751
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800752 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700753 m_numVertices = constantCount;
754 m_stride = constantSize;
755
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600756 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800757 const size_t allocationSize = constantCount * constantSize;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600758 init(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700759
Chia-I Wua07fee62014-12-28 15:26:08 +0800760 void *pData = map();
761 memcpy(pData, data, allocationSize);
762 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700763
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800764 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600765 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600766 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800767 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600768 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800769 view_info.offset = 0;
770 view_info.range = allocationSize;
771 m_bufferView.init(*m_device, view_info);
772
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600773 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800774 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700775}
Tony Barbour82c39522014-12-04 14:33:33 -0700776
Tony Barbourd1c35722015-04-16 15:59:00 -0600777void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700778{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600779 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700780}
781
782
Tony Barbour6918cd52015-04-09 12:58:51 -0600783void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600784 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600785 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600786 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
787 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
788 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
789 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600790 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600791 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600792 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
793 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
794 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
795 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
796 VK_MEMORY_INPUT_SHADER_READ_BIT |
797 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
798 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
799 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700800{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600801 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700802
Tony Barbour38422802014-12-10 14:36:31 -0700803 if (!m_commandBuffer)
804 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600805 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700806
Tony Barbour6918cd52015-04-09 12:58:51 -0600807 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700808 }
809 else
810 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800811 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700812 }
813
Tony Barboure2c58df2014-11-25 13:18:32 -0700814 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600815 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600816 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600817 cmd_buf_info.pNext = NULL;
818 cmd_buf_info.flags = 0;
819
Jon Ashburnc4164b12014-12-31 17:10:47 -0700820 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600821 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700822
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600823 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000824 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600825 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700826
Tony Barbourd1c35722015-04-16 15:59:00 -0600827 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000828
829 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600830 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700831
832 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700833 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600834 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700835
Tony Barboure2c58df2014-11-25 13:18:32 -0700836 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600837 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700838 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600839 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
840 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700841}
842
Tony Barbour6918cd52015-04-09 12:58:51 -0600843VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
844 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700845{
846
847}
848
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600849void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700850{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600851 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700852
853 m_numVertices = numIndexes;
854 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700855 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600856 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700857 m_stride = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -0600858 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700859 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600860 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700861 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600862 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700863 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600864 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700865 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600866 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700867 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800868 default:
869 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600870 m_stride = 2;
871 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800872 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700873 }
874
Chia-I Wua07fee62014-12-28 15:26:08 +0800875 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600876 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
877 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700878
Chia-I Wua07fee62014-12-28 15:26:08 +0800879 void *pData = map();
880 memcpy(pData, data, allocationSize);
881 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700882
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800883 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600884 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600885 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800886 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600887 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700888 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800889 view_info.offset = 0;
890 view_info.range = allocationSize;
891 m_bufferView.init(*m_device, view_info);
892
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600893 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800894 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700895}
896
Tony Barbourd1c35722015-04-16 15:59:00 -0600897void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700898{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600899 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700900}
Tony Barboure2c58df2014-11-25 13:18:32 -0700901
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600902VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -0700903{
904 return m_indexType;
905}
906
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600907VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700908{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600909 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600910 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700911 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800912 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700913 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600914 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700915
Tony Barboure2c58df2014-11-25 13:18:32 -0700916 return stageInfo;
917}
918
Tony Barbourd1c35722015-04-16 15:59:00 -0600919VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -0700920{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600921 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600922 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600923 VkShaderCreateInfo createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700924 size_t shader_len;
925
926 m_stage = stage;
927 m_device = device;
928
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600929 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700930 createInfo.pNext = NULL;
931
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600932 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700933
934 shader_len = strlen(shader_code);
935 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
936 createInfo.pCode = malloc(createInfo.codeSize);
937 createInfo.flags = 0;
938
Tony Barbourd1c35722015-04-16 15:59:00 -0600939 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600940 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700941 ((uint32_t *) createInfo.pCode)[1] = 0;
942 ((uint32_t *) createInfo.pCode)[2] = stage;
943 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
944
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800945 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700946 }
947
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600948 if (framework->m_use_spv || err) {
949 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600950 err = VK_SUCCESS;
Tony Barboure2c58df2014-11-25 13:18:32 -0700951
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600952 // Use Reference GLSL to SPV compiler
953 framework->GLSLtoSPV(stage, shader_code, spv);
954 createInfo.pCode = spv.data();
955 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700956 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700957
Cody Northrop475663c2015-04-15 11:19:06 -0600958 err = init_try(*m_device, createInfo);
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800959 }
Cody Northrop475663c2015-04-15 11:19:06 -0600960
961 assert(VK_SUCCESS == err);
Tony Barbourf325bf12014-12-03 15:59:38 -0700962}
Tony Barbour82c39522014-12-04 14:33:33 -0700963
Tony Barbour6918cd52015-04-09 12:58:51 -0600964VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700965{
Tony Barboure2c58df2014-11-25 13:18:32 -0700966 m_device = device;
967 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
968 m_vertexBufferCount = 0;
969
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600970 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
971 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600972 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600973 m_ia_state.disableVertexReuse = VK_FALSE;
974 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700975 m_ia_state.primitiveRestartIndex = 0;
976
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600977 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700978 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600979 m_rs_state.depthClipEnable = VK_FALSE;
980 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
981 m_rs_state.programPointSize = VK_FALSE;
982 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
983 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600984 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -0600985 m_rs_state.cullMode = VK_CULL_MODE_BACK;
986 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700987
Tony Barboure2c58df2014-11-25 13:18:32 -0700988 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600989 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700990 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600991 m_cb_state.alphaToCoverageEnable = VK_FALSE;
992 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -0700993
Tony Barbourf52346d2015-01-16 14:27:35 -0700994 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600995 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
996 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -0700997 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
998 m_ms_state.samples = 1;
999 m_ms_state.minSampleShading = 0;
1000 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001001
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001002 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -07001003 m_ds_state.pNext = &m_ms_state,
Tony Barbourd1c35722015-04-16 15:59:00 -06001004 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001005 m_ds_state.depthTestEnable = VK_FALSE;
1006 m_ds_state.depthWriteEnable = VK_FALSE;
1007 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001008 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001009 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1010 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1011 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001012 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001013 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001014 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001015
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001016 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001017 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001018 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001019 att.channelWriteMask = 0xf;
1020 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001021
1022};
1023
Tony Barbour6918cd52015-04-09 12:58:51 -06001024void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001025{
1026 m_shaderObjs.push_back(shader);
1027}
1028
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001029void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001030{
1031 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1032 m_vi_state.attributeCount = count;
1033}
1034
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001035void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001036{
1037 m_vi_state.pVertexBindingDescriptions = vi_binding;
1038 m_vi_state.bindingCount = count;
1039}
1040
Tony Barbour6918cd52015-04-09 12:58:51 -06001041void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001042{
1043 m_vertexBufferObjs.push_back(vertexDataBuffer);
1044 m_vertexBufferBindings.push_back(binding);
1045 m_vertexBufferCount++;
1046}
1047
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001048void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001049{
Tony Barbourf52346d2015-01-16 14:27:35 -07001050 if (binding+1 > m_colorAttachments.size())
1051 {
1052 m_colorAttachments.resize(binding+1);
1053 }
1054 m_colorAttachments[binding] = *att;
1055}
1056
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001057void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001058{
1059 m_ds_state.format = ds_state->format;
1060 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1061 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1062 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001063 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001064 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1065 m_ds_state.back = ds_state->back;
1066 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001067}
1068
Tony Barbour6918cd52015-04-09 12:58:51 -06001069void VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001070{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001071 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001072 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001073
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001074 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001075
1076 for (int i=0; i<m_shaderObjs.size(); i++)
1077 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001078 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001079 shaderCreateInfo->pNext = head_ptr;
1080 head_ptr = shaderCreateInfo;
1081 }
1082
1083 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1084 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001085 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001086 m_vi_state.pNext = head_ptr;
1087 head_ptr = &m_vi_state;
1088 }
1089
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001090 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1091 info.pNext = head_ptr;
1092 info.flags = 0;
1093 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001094
Tony Barbourf52346d2015-01-16 14:27:35 -07001095 m_cb_state.attachmentCount = m_colorAttachments.size();
1096 m_cb_state.pAttachments = &m_colorAttachments[0];
1097
Chia-I Wu2648d092014-12-29 14:24:14 +08001098 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001099}
Chia-I Wu2648d092014-12-29 14:24:14 +08001100
Tony Barbourd1c35722015-04-16 15:59:00 -06001101vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001102{
Tony Barbourd1c35722015-04-16 15:59:00 -06001103 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001104 if (this->mem_refs_.size()) {
1105 mems.reserve(this->mem_refs_.size());
1106 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1107 mems.push_back(this->mem_refs_[i]);
1108 }
1109
1110 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001111}
1112
Tony Barbour6918cd52015-04-09 12:58:51 -06001113VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001114 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001115{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001116 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001117}
Tony Barbour471338d2014-12-10 17:28:39 -07001118
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001119VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001120{
Chia-I Wud28343c2014-12-28 15:12:48 +08001121 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001122}
Tony Barbour471338d2014-12-10 17:28:39 -07001123
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001124VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001125{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001126 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001127 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001128}
1129
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001130VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001131{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001132 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001133 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001134}
1135
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001136VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001137{
1138 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001139 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001140}
1141
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001142VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001143{
Chia-I Wud28343c2014-12-28 15:12:48 +08001144 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001145 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001146}
1147
Tony Barbourd1c35722015-04-16 15:59:00 -06001148void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001149{
Tony Barbourd1c35722015-04-16 15:59:00 -06001150 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001151}
1152
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001153void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001154 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001155{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001156 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001157 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001158 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001159 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1160 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1161 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001162 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001163 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001164
1165 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001166 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001167 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001168 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001169 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001170 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001171 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001172
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001173 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001174 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001175 memory_barrier.outputMask = output_mask;
1176 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001177 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001178 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001179 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001180
Tony Barbourd1c35722015-04-16 15:59:00 -06001181 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001182
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001183 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001184 memory_barrier.image = m_renderTargets[i]->image();
1185 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001186 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001187 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001188
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001189 vkCmdClearColorImage(obj(),
1190 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001191 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001192
Tony Barbour30cc9e82014-12-17 11:53:55 -07001193 }
1194
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001195 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001196 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001197 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001198 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001199 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001200 dsRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001201 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001202 dsRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001203
1204 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001205
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001206 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001207 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001208 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001209 memory_barrier.subresourceRange = dsRange;
1210
Tony Barbourd1c35722015-04-16 15:59:00 -06001211 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001212
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001213 vkCmdClearDepthStencil(obj(),
1214 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001215 depth_clear_color, stencil_clear_color,
1216 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001217
1218 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001219 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001220 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001221 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001222 memory_barrier.subresourceRange = dsRange;
Tony Barbourd1c35722015-04-16 15:59:00 -06001223 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001224 }
1225}
1226
Tony Barbour6918cd52015-04-09 12:58:51 -06001227void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001228{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001229 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001230 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001231 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001232 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1233 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1234 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001235 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001236 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001237 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001238 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1239 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1240 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1241 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1242 VK_MEMORY_INPUT_SHADER_READ_BIT |
1243 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1244 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001245 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001246
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001247 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001248 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001249 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001250 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001251 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001252 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001253
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001254 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001255 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001256 memory_barrier.outputMask = output_mask;
1257 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001258 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001259 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001260 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001261
Tony Barbourd1c35722015-04-16 15:59:00 -06001262 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001263
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001264 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001265 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001266 memory_barrier.image = m_renderTargets[i]->image();
1267 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001268 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001269 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001270 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001271}
1272
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001273void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001274{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001275 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001276 renderpass,
1277 framebuffer,
1278 };
1279
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001280 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001281}
1282
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001283void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001284{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001285 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001286}
1287
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001288void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001289{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001290 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001291}
1292
Tony Barbour6918cd52015-04-09 12:58:51 -06001293void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001294{
1295 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001296}
1297
Tony Barbour6918cd52015-04-09 12:58:51 -06001298void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001299{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001300 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001301}
1302
Tony Barbour6918cd52015-04-09 12:58:51 -06001303void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001304{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001305 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001306}
1307
Tony Barbour6918cd52015-04-09 12:58:51 -06001308void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001309{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001310 QueueCommandBuffer(NULL);
1311}
1312
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001313void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001314{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001315 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001316
1317 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001318 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1319 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001320
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001321 err = vkQueueWaitIdle( m_device->m_queue );
1322 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001323
1324 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001325 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001326}
1327
Tony Barbour6918cd52015-04-09 12:58:51 -06001328void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001329{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001330 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001331}
1332
Tony Barbour6918cd52015-04-09 12:58:51 -06001333void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001334{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001335 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001336
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001337 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001338 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropd4c1a502015-04-16 13:41:56 -06001339 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001340}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001341
Tony Barbour6918cd52015-04-09 12:58:51 -06001342void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001343{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001344 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001345}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001346
Tony Barbourd1c35722015-04-16 15:59:00 -06001347void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001348{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001349 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001350}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001351
Tony Barbour6918cd52015-04-09 12:58:51 -06001352VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001353{
1354 m_initialized = false;
1355}
Tony Barbour6918cd52015-04-09 12:58:51 -06001356bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001357{
1358 return m_initialized;
1359}
1360
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001361VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001362{
1363 return &m_depthStencilBindInfo;
1364}
1365
Tony Barbour6918cd52015-04-09 12:58:51 -06001366void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001367{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001368 VkImageCreateInfo image_info;
1369 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001370
1371 m_device = device;
1372 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001373 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001374
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001375 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001376 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001377 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001378 image_info.format = m_depth_stencil_fmt;
1379 image_info.extent.width = width;
1380 image_info.extent.height = height;
1381 image_info.extent.depth = 1;
1382 image_info.mipLevels = 1;
1383 image_info.arraySize = 1;
1384 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001385 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001386 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001387 image_info.flags = 0;
1388 init(*m_device, image_info);
1389
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001390 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001391 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001392 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001393 view_info.mipLevel = 0;
1394 view_info.baseArraySlice = 0;
1395 view_info.arraySize = 1;
1396 view_info.flags = 0;
1397 view_info.image = obj();
1398 m_depthStencilView.init(*m_device, view_info);
1399
1400 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001401 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001402}