blob: 753fe53a6db37c11eba51977c599e08c17092c83 [file] [log] [blame]
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchter5744b972014-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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -060029
Jon Ashburn07b309a2015-04-15 11:31:12 -060030#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
31
Tony Barbour01999182015-04-09 12:58:51 -060032VkRenderFramework::VkRenderFramework() :
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060033 m_cmdBuffer( VK_NULL_HANDLE ),
Tony Barbourbc868f92015-04-09 11:07:02 -060034 m_renderPass(VK_NULL_HANDLE),
35 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchter9cc421e2015-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 Goeltzenleuchtere5409342014-10-08 14:26:40 -060040 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter7e305322015-03-05 17:26:38 -070041 m_height( 256.0 ), // default window height
Tony Barbour8205d902015-04-16 15:59:00 -060042 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
43 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Courtney Goeltzenleuchter7e305322015-03-05 17:26:38 -070044 m_depth_clear_color( 1.0 ),
45 m_stencil_clear_color( 0 )
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -060046{
Tony Barbour17c6ab12015-03-27 17:03:18 -060047
Courtney Goeltzenleuchter7e305322015-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 Goeltzenleuchter5744b972014-10-08 08:50:49 -060054}
55
Tony Barbour01999182015-04-09 12:58:51 -060056VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -060057{
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060058
59}
60
Tony Barbour01999182015-04-09 12:58:51 -060061void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060062{
Tony Barbour04ada4a2015-04-23 15:28:27 -060063 const std::vector<const char *> extensions;
64 InitFramework(extensions);
Tony Barbour950ebc02015-04-23 12:55:36 -060065}
66
Tony Barbour0c1bdc62015-04-29 17:34:29 -060067void VkRenderFramework::InitFramework(const std::vector<const char *> &extensions, VK_DBG_MSG_CALLBACK_FUNCTION dbgFunction, void *userData)
Tony Barbour950ebc02015-04-23 12:55:36 -060068{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060069 VkResult err;
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -060070 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060071 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburn29669a42015-04-04 14:52:07 -060072 instInfo.pNext = NULL;
73 instInfo.pAppInfo = &app_info;
74 instInfo.pAllocCb = NULL;
Tony Barbour04ada4a2015-04-23 15:28:27 -060075 instInfo.extensionCount = extensions.size();
76 instInfo.ppEnabledExtensionNames = (extensions.size()) ? &extensions[0] : NULL;;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060077 err = vkCreateInstance(&instInfo, &this->inst);
78 ASSERT_VK_SUCCESS(err);
Jon Ashburn07b309a2015-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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060083 ASSERT_VK_SUCCESS(err);
Jon Ashburn19733c92014-11-26 11:06:49 -070084 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour0c1bdc62015-04-29 17:34:29 -060085 if (dbgFunction) {
86 err = vkDbgRegisterMsgCallback(this->inst, dbgFunction, userData);
87 ASSERT_VK_SUCCESS(err);
88 }
Tony Barbour04ada4a2015-04-23 15:28:27 -060089 m_device = new VkDeviceObj(0, objs[0]);
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060090 m_device->get_device_queue();
Tony Barbour17c6ab12015-03-27 17:03:18 -060091
Tony Barbour01999182015-04-09 12:58:51 -060092 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060093}
94
Tony Barbour01999182015-04-09 12:58:51 -060095void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060096{
Mike Stroyan230e6252015-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 Goeltzenleuchter5744b972014-10-08 08:50:49 -0600103
104 if (m_stateViewport) {
Mike Stroyan230e6252015-04-17 12:36:38 -0600105 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600106 }
Tobin Ehlis12ee35f2015-03-26 08:23:25 -0600107 while (!m_renderTargets.empty()) {
Mike Stroyan230e6252015-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 Ehlis12ee35f2015-03-26 08:23:25 -0600112 m_renderTargets.pop_back();
113 }
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600114
Tony Barbour17c6ab12015-03-27 17:03:18 -0600115 delete m_depthStencil;
116
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600117 // reset the driver
Chia-I Wu1c9869e2014-12-28 14:27:28 +0800118 delete m_device;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600119 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600120}
121
Tony Barbour01999182015-04-09 12:58:51 -0600122void VkRenderFramework::InitState()
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600123{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600124 VkResult err;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600125
Tony Barbour8205d902015-04-16 15:59:00 -0600126 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600127
128 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600129 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600130 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700131 raster.pointSize = 1.0;
132
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600133 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
134 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600135
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600136 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchter9cc421e2015-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 Goeltzenleuchter5744b972014-10-08 08:50:49 -0600140
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600141 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600142 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600143 depthStencil.minDepth = 0.f;
144 depthStencil.maxDepth = 1.f;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700145 depthStencil.stencilFrontRef = 0;
146 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600147
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600148 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
149 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600150
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600151 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600152
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600153 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700154 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
155
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600156 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
157 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600158}
159
Tony Barbour01999182015-04-09 12:58:51 -0600160void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600161{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600162 VkResult err;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600163
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600164 VkViewport viewport;
165 VkRect scissor;
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600166
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600167 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600168 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -0700169 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourfa6cac72015-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 Goeltzenleuchterc6e32f92015-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 Barbourfa6cac72015-01-16 14:27:35 -0700180 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -0700181 viewportCreate.pScissors = &scissor;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700182
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600183 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
184 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600185 m_width = width;
186 m_height = height;
187}
188
Tony Barbour01999182015-04-09 12:58:51 -0600189void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600190{
191 InitViewport(m_width, m_height);
192}
Tony Barbour01999182015-04-09 12:58:51 -0600193void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600194{
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -0700195 InitRenderTarget(1);
196}
197
Tony Barbour01999182015-04-09 12:58:51 -0600198void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -0700199{
Tony Barbour17c6ab12015-03-27 17:03:18 -0600200 InitRenderTarget(targets, NULL);
201}
202
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600203void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour17c6ab12015-03-27 17:03:18 -0600204{
205 InitRenderTarget(1, dsBinding);
206}
207
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600208void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour17c6ab12015-03-27 17:03:18 -0600209{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600210 std::vector<VkAttachmentLoadOp> load_ops;
211 std::vector<VkAttachmentStoreOp> store_ops;
212 std::vector<VkClearColor> clear_colors;
Tony Barbour17c6ab12015-03-27 17:03:18 -0600213
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600214 uint32_t i;
Chia-I Wue7748802014-12-05 10:45:15 +0800215
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -0700216 for (i = 0; i < targets; i++) {
Tony Barbour01999182015-04-09 12:58:51 -0600217 VkImageObj *img = new VkImageObj(m_device);
Chia-I Wu462d2092014-12-29 14:31:52 +0800218 img->init(m_width, m_height, m_render_target_fmt,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600219 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourfa6cac72015-01-16 14:27:35 -0700220 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600221 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700222 m_renderTargets.push_back(img);
Courtney Goeltzenleuchter9cc421e2015-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 Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600225 clear_colors.push_back(m_clear_color);
Chia-I Wue7748802014-12-05 10:45:15 +0800226 }
Jeremy Hayese0c3b222015-01-14 16:17:08 -0700227 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600228 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600229 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbour901f3bc2015-04-01 17:10:07 -0600230 fb_info.pNext = NULL;
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -0700231 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbour901f3bc2015-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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600239 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600240
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600241 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600242 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jeremy Hayese0c3b222015-01-14 16:17:08 -0700243 rp_info.renderArea.extent.width = m_width;
244 rp_info.renderArea.extent.height = m_height;
Tony Barbour17c6ab12015-03-27 17:03:18 -0600245
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -0700246 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchtere3b0f3a2015-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 Barbour17c6ab12015-03-27 17:03:18 -0600253 if (dsBinding) {
254 rp_info.depthStencilLayout = dsBinding->layout;
255 }
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600256 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600257 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600258 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
259 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600260 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600261 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
262 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchter5744b972014-10-08 08:50:49 -0600263}
264
Mark Lobodzinski15427102015-02-18 16:38:17 -0600265
266
Tony Barbour8205d902015-04-16 15:59:00 -0600267VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600268 vk_testing::Device(obj), id(id)
Chia-I Wu234a0ae2014-12-29 15:23:20 +0800269{
270 init();
271
272 props = gpu().properties();
273 queue_props = &gpu().queue_properties()[0];
274}
275
Tony Barbour01999182015-04-09 12:58:51 -0600276void VkDeviceObj::get_device_queue()
Chia-I Wu234a0ae2014-12-29 15:23:20 +0800277{
278 ASSERT_NE(true, graphics_queues().empty());
279 m_queue = graphics_queues()[0]->obj();
280}
281
Tony Barbour01999182015-04-09 12:58:51 -0600282VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device)
Tony Barbourf43b6982014-11-25 13:18:32 -0700283{
284 m_device = device;
285 m_nextSlot = 0;
286
287}
288
Tony Barbour01999182015-04-09 12:58:51 -0600289VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barbourf43b6982014-11-25 13:18:32 -0700290{
Chia-I Wuf8385062015-01-04 16:27:24 +0800291 delete m_set;
Tony Barbourf43b6982014-11-25 13:18:32 -0700292}
Tony Barbour408ca622014-12-04 14:33:33 -0700293
Tony Barbour01999182015-04-09 12:58:51 -0600294int VkDescriptorSetObj::AppendDummy()
Tony Barbourf43b6982014-11-25 13:18:32 -0700295{
Chia-I Wuf8385062015-01-04 16:27:24 +0800296 /* request a descriptor but do not update it */
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600297 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600298 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wuf8385062015-01-04 16:27:24 +0800299 tc.count = 1;
300 m_type_counts.push_back(tc);
Tony Barbourf43b6982014-11-25 13:18:32 -0700301
Chia-I Wuf8385062015-01-04 16:27:24 +0800302 return m_nextSlot++;
Tony Barbourf43b6982014-11-25 13:18:32 -0700303}
Tony Barbour408ca622014-12-04 14:33:33 -0700304
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600305int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barbourf43b6982014-11-25 13:18:32 -0700306{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600307 VkDescriptorTypeCount tc = {};
Chia-I Wuf8385062015-01-04 16:27:24 +0800308 tc.type = type;
309 tc.count = 1;
310 m_type_counts.push_back(tc);
Tony Barbourf43b6982014-11-25 13:18:32 -0700311
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600312 m_updateBuffers.push_back(vk_testing::DescriptorSet::update(type,
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600313 m_nextSlot, 0, 1, &constantBuffer.m_bufferViewInfo));
314
Chia-I Wuf8385062015-01-04 16:27:24 +0800315 return m_nextSlot++;
Tony Barbourf43b6982014-11-25 13:18:32 -0700316}
Tony Barbour408ca622014-12-04 14:33:33 -0700317
Tony Barbour01999182015-04-09 12:58:51 -0600318int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barbourf43b6982014-11-25 13:18:32 -0700319{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600320 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600321 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wuf8385062015-01-04 16:27:24 +0800322 tc.count = 1;
323 m_type_counts.push_back(tc);
Tony Barbourf43b6982014-11-25 13:18:32 -0700324
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600325 VkSamplerImageViewInfo tmp = {};
Courtney Goeltzenleuchterd38dcc92015-04-09 11:43:10 -0600326 tmp.sampler = sampler->obj();
Chia-I Wuf8385062015-01-04 16:27:24 +0800327 tmp.pImageView = &texture->m_textureViewInfo;
328 m_samplerTextureInfo.push_back(tmp);
Tony Barbourf43b6982014-11-25 13:18:32 -0700329
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600330 m_updateSamplerTextures.push_back(vk_testing::DescriptorSet::update(m_nextSlot, 0, 1,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600331 (const VkSamplerImageViewInfo *) NULL));
Tony Barbourf43b6982014-11-25 13:18:32 -0700332
Chia-I Wuf8385062015-01-04 16:27:24 +0800333 return m_nextSlot++;
Tony Barbourf43b6982014-11-25 13:18:32 -0700334}
Chia-I Wuf8385062015-01-04 16:27:24 +0800335
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500336VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbour3edb6ac2014-12-17 10:54:03 -0700337{
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500338 return m_pipeline_layout.obj();
Tony Barbour3edb6ac2014-12-17 10:54:03 -0700339}
340
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600341VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbour3edb6ac2014-12-17 10:54:03 -0700342{
Chia-I Wuf8385062015-01-04 16:27:24 +0800343 return m_set->obj();
Tony Barbour3edb6ac2014-12-17 10:54:03 -0700344}
Tony Barbourf43b6982014-11-25 13:18:32 -0700345
Tony Barbour01999182015-04-09 12:58:51 -0600346void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barboura5712982014-12-18 17:06:21 -0700347{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600348 // create VkDescriptorPool
349 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600350 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu8d24b3b2015-03-26 13:14:16 +0800351 pool.count = m_type_counts.size();
352 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600353 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barboura5712982014-12-18 17:06:21 -0700354
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600355 // create VkDescriptorSetLayout
356 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wufc9d9132015-03-26 15:04:41 +0800357 bindings.resize(m_type_counts.size());
Chia-I Wuf8385062015-01-04 16:27:24 +0800358 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wufc9d9132015-03-26 15:04:41 +0800359 bindings[i].descriptorType = m_type_counts[i].type;
360 bindings[i].count = m_type_counts[i].count;
Tony Barbour8205d902015-04-16 15:59:00 -0600361 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu310eece2015-03-27 12:56:09 +0800362 bindings[i].pImmutableSamplers = NULL;
Tony Barbourf43b6982014-11-25 13:18:32 -0700363 }
Chia-I Wu6ae4cfb2014-12-28 22:32:36 +0800364
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600365 // create VkDescriptorSetLayout
366 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600367 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wufc9d9132015-03-26 15:04:41 +0800368 layout.count = bindings.size();
369 layout.pBinding = &bindings[0];
370
Chia-I Wu7732cb22015-03-26 15:27:55 +0800371 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600372 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu7732cb22015-03-26 15:27:55 +0800373 layouts.push_back(&m_layout);
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500374
375 // create VkPipelineLayout
376 VkPipelineLayoutCreateInfo pipeline_layout = {};
377 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
378 pipeline_layout.descriptorSetCount = layouts.size();
379 pipeline_layout.pSetLayouts = NULL;
380
381 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barbourf43b6982014-11-25 13:18:32 -0700382
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600383 // create VkDescriptorSet
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500384 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wuf8385062015-01-04 16:27:24 +0800385
Chia-I Wu7732cb22015-03-26 15:27:55 +0800386 // build the update array
387 vector<const void *> update_array;
Chia-I Wuf8385062015-01-04 16:27:24 +0800388
Chia-I Wu7732cb22015-03-26 15:27:55 +0800389 for (int i = 0; i < m_updateBuffers.size(); i++) {
390 update_array.push_back(&m_updateBuffers[i]);
Chia-I Wuf8385062015-01-04 16:27:24 +0800391 }
392 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
393 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
Chia-I Wu7732cb22015-03-26 15:27:55 +0800394 update_array.push_back(&m_updateSamplerTextures[i]);
Chia-I Wuf8385062015-01-04 16:27:24 +0800395 }
Chia-I Wuf8385062015-01-04 16:27:24 +0800396
397 // do the updates
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600398 m_device->begin_descriptor_pool_update(VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wuf8385062015-01-04 16:27:24 +0800399 clear_sets(*m_set);
Chia-I Wu7732cb22015-03-26 15:27:55 +0800400 m_set->update(update_array);
Chia-I Wu8d24b3b2015-03-26 13:14:16 +0800401 m_device->end_descriptor_pool_update(*cmdBuffer);
Tony Barbour342ae3b2014-12-03 13:59:18 -0700402}
Tony Barbourf43b6982014-11-25 13:18:32 -0700403
Tony Barbour01999182015-04-09 12:58:51 -0600404VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800405{
406 m_device = dev;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600407 m_imageInfo.view = VK_NULL_HANDLE;
408 m_imageInfo.layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800409}
410
Tony Barbour01999182015-04-09 12:58:51 -0600411void VkImageObj::ImageMemoryBarrier(
412 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600413 VkImageAspect aspect,
414 VkFlags output_mask /*=
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600415 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600416 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
417 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
418 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
419 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600420 VkFlags input_mask /*=
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600421 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600422 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
423 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
424 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
425 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
426 VK_MEMORY_INPUT_SHADER_READ_BIT |
427 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
428 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
429 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600430 VkImageLayout image_layout)
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600431{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600432 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
433 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600434 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
435 subresourceRange);
436
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600437 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600438
Tony Barbour8205d902015-04-16 15:59:00 -0600439 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600440
441 // write barrier to the command buffer
Tony Barbour8205d902015-04-16 15:59:00 -0600442 vkCmdPipelineBarrier(cmd_buf->obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, pipe_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600443}
444
Tony Barbour01999182015-04-09 12:58:51 -0600445void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600446 VkImageAspect aspect,
447 VkImageLayout image_layout)
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600448{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600449 VkFlags output_mask, input_mask;
450 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600451 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600452 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
453 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
454 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600455 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600456 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600457 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600458 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
459 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
460 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
461 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
462 VK_MEMORY_INPUT_SHADER_READ_BIT |
463 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
464 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600465 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600466
467 if (image_layout == m_imageInfo.layout) {
468 return;
469 }
470
471 switch (image_layout) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600472 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600473 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
474 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600475 break;
476
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600477 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600478 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
479 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600480 break;
481
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600482 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600483 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
484 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600485 break;
486
Tony Barbour22a30862015-04-22 09:02:32 -0600487 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600488 default:
489 output_mask = all_cache_outputs;
490 input_mask = all_cache_inputs;
491 break;
492 }
493
494 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
495 m_imageInfo.layout = image_layout;
496}
497
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600498void VkImageObj::SetLayout(VkImageAspect aspect,
499 VkImageLayout image_layout)
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600500{
Tony Barbour22a30862015-04-22 09:02:32 -0600501 VkResult U_ASSERT_ONLY err;
Tony Barbour01999182015-04-09 12:58:51 -0600502 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600503
504 /* Build command buffer to set image layout in the driver */
505 err = cmd_buf.BeginCommandBuffer();
506 assert(!err);
507
508 SetLayout(&cmd_buf, aspect, image_layout);
509
510 err = cmd_buf.EndCommandBuffer();
511 assert(!err);
512
513 cmd_buf.QueueCommandBuffer();
514}
515
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600516bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600517{
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600518 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbour8205d902015-04-16 15:59:00 -0600519 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600520 return false;
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600521
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600522 return true;
523}
524
Tony Barbour01999182015-04-09 12:58:51 -0600525void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600526 VkFormat fmt, VkFlags usage,
Tony Barbour94310562015-04-22 15:10:33 -0600527 VkImageTiling requested_tiling,
528 VkMemoryPropertyFlags reqs)
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800529{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600530 uint32_t mipCount;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600531 VkFormatProperties image_fmt;
532 VkImageTiling tiling;
533 VkResult err;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600534 size_t size;
535
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800536 mipCount = 0;
537
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600538 uint32_t _w = w;
539 uint32_t _h = h;
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800540 while( ( _w > 0 ) || ( _h > 0 ) )
541 {
542 _w >>= 1;
543 _h >>= 1;
544 mipCount++;
545 }
546
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600547 size = sizeof(image_fmt);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600548 err = vkGetFormatInfo(m_device->obj(), fmt,
Tony Barbour8205d902015-04-16 15:59:00 -0600549 VK_FORMAT_INFO_TYPE_PROPERTIES,
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600550 &size, &image_fmt);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600551 ASSERT_VK_SUCCESS(err);
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600552
Tony Barbour8205d902015-04-16 15:59:00 -0600553 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600554 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbour8205d902015-04-16 15:59:00 -0600555 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600556 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbour8205d902015-04-16 15:59:00 -0600557 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600558 } else {
559 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
560 }
561 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbour8205d902015-04-16 15:59:00 -0600562 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600563 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbour8205d902015-04-16 15:59:00 -0600564 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour6b20a0f2015-04-03 15:11:43 -0600565 } else {
566 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
567 }
568
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600569 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbour8205d902015-04-16 15:59:00 -0600570 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800571 imageCreateInfo.format = fmt;
572 imageCreateInfo.extent.width = w;
573 imageCreateInfo.extent.height = h;
574 imageCreateInfo.mipLevels = mipCount;
575 imageCreateInfo.tiling = tiling;
576
577 imageCreateInfo.usage = usage;
578
Tony Barbour94310562015-04-22 15:10:33 -0600579 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800580
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600581 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600582 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600583 } else {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600584 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchter4d18b082015-04-07 08:57:30 -0600585 }
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800586}
587
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600588VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800589{
590 *ptr = map();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600591 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800592}
593
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600594VkResult VkImageObj::UnmapMemory()
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800595{
596 unmap();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600597 return VK_SUCCESS;
Chia-I Wu13ecdcd2014-12-29 14:38:28 +0800598}
599
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600600VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour16b71bc2015-04-01 17:47:06 -0600601{
Tony Barbour22a30862015-04-22 09:02:32 -0600602 VkResult U_ASSERT_ONLY err;
Tony Barbour01999182015-04-09 12:58:51 -0600603 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600604 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600605
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600606 /* Build command buffer to copy staging texture to usable texture */
607 err = cmd_buf.BeginCommandBuffer();
Tony Barbour16b71bc2015-04-01 17:47:06 -0600608 assert(!err);
609
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600610 /* TODO: Can we determine image aspect from image object? */
611 src_image_layout = src_image.layout();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600612 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour16b71bc2015-04-01 17:47:06 -0600613
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600614 dest_image_layout = this->layout();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600615 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour17c6ab12015-03-27 17:03:18 -0600616
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600617 VkImageCopy copy_region = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600618 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600619 copy_region.srcSubresource.arraySlice = 0;
620 copy_region.srcSubresource.mipLevel = 0;
621 copy_region.srcOffset.x = 0;
622 copy_region.srcOffset.y = 0;
623 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600624 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600625 copy_region.destSubresource.arraySlice = 0;
626 copy_region.destSubresource.mipLevel = 0;
627 copy_region.destOffset.x = 0;
628 copy_region.destOffset.y = 0;
629 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600630 copy_region.extent = src_image.extent();
Tony Barbour16b71bc2015-04-01 17:47:06 -0600631
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600632 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600633 src_image.obj(), src_image.layout(),
634 obj(), layout(),
Tony Barbour17c6ab12015-03-27 17:03:18 -0600635 1, &copy_region);
Tony Barbour16b71bc2015-04-01 17:47:06 -0600636
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600637 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour16b71bc2015-04-01 17:47:06 -0600638
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600639 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour17c6ab12015-03-27 17:03:18 -0600640
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600641 err = cmd_buf.EndCommandBuffer();
Tony Barbour16b71bc2015-04-01 17:47:06 -0600642 assert(!err);
643
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600644 cmd_buf.QueueCommandBuffer();
Tony Barbour16b71bc2015-04-01 17:47:06 -0600645
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600646 return VK_SUCCESS;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600647}
648
Tony Barbour01999182015-04-09 12:58:51 -0600649VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
650 :VkImageObj(device)
Tony Barbourf43b6982014-11-25 13:18:32 -0700651{
652 m_device = device;
Tony Barbour8205d902015-04-16 15:59:00 -0600653 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbour2f421a02015-04-01 16:38:10 -0600654 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour16b71bc2015-04-01 17:47:06 -0600655 void *data;
656 int32_t x, y;
Tony Barbour01999182015-04-09 12:58:51 -0600657 VkImageObj stagingImage(device);
Tony Barbour94310562015-04-22 15:10:33 -0600658 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600659
Tony Barbour94310562015-04-22 15:10:33 -0600660 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600661 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbour2f421a02015-04-01 16:38:10 -0600662
663 if (colors == NULL)
664 colors = tex_colors;
Tony Barbourf43b6982014-11-25 13:18:32 -0700665
666 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
667
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600668 m_textureViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Tony Barbourf43b6982014-11-25 13:18:32 -0700669
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600670 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600671 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbour901f3bc2015-04-01 17:10:07 -0600672 view.pNext = NULL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600673 view.image = VK_NULL_HANDLE;
Tony Barbour8205d902015-04-16 15:59:00 -0600674 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour16b71bc2015-04-01 17:47:06 -0600675 view.format = tex_format;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600676 view.channels.r = VK_CHANNEL_SWIZZLE_R;
677 view.channels.g = VK_CHANNEL_SWIZZLE_G;
678 view.channels.b = VK_CHANNEL_SWIZZLE_B;
679 view.channels.a = VK_CHANNEL_SWIZZLE_A;
680 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour901f3bc2015-04-01 17:10:07 -0600681 view.subresourceRange.baseMipLevel = 0;
682 view.subresourceRange.mipLevels = 1;
683 view.subresourceRange.baseArraySlice = 0;
684 view.subresourceRange.arraySize = 1;
685 view.minLod = 0.0f;
Tony Barbourf43b6982014-11-25 13:18:32 -0700686
Tony Barbourf43b6982014-11-25 13:18:32 -0700687 /* create image */
Tony Barbour8205d902015-04-16 15:59:00 -0600688 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barbourf43b6982014-11-25 13:18:32 -0700689
690 /* create image view */
Chia-I Wudbec1222014-12-28 15:55:09 +0800691 view.image = obj();
692 m_textureView.init(*m_device, view);
Tony Barbour16b71bc2015-04-01 17:47:06 -0600693 m_textureViewInfo.view = m_textureView.obj();
Tony Barbourf43b6982014-11-25 13:18:32 -0700694
Tony Barbour16b71bc2015-04-01 17:47:06 -0600695 data = stagingImage.map();
Tony Barbourf43b6982014-11-25 13:18:32 -0700696
Chia-I Wudbec1222014-12-28 15:55:09 +0800697 for (y = 0; y < extent().height; y++) {
Tony Barbourf43b6982014-11-25 13:18:32 -0700698 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wudbec1222014-12-28 15:55:09 +0800699 for (x = 0; x < extent().width; x++)
Tony Barbour2f421a02015-04-01 16:38:10 -0600700 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barbourf43b6982014-11-25 13:18:32 -0700701 }
Tony Barbour16b71bc2015-04-01 17:47:06 -0600702 stagingImage.unmap();
Tony Barbour01999182015-04-09 12:58:51 -0600703 VkImageObj::CopyImage(stagingImage);
Tony Barbourf43b6982014-11-25 13:18:32 -0700704}
Tony Barbour408ca622014-12-04 14:33:33 -0700705
Tony Barbour01999182015-04-09 12:58:51 -0600706VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barbourf43b6982014-11-25 13:18:32 -0700707{
Tony Barbourf43b6982014-11-25 13:18:32 -0700708 m_device = device;
Tony Barbourf43b6982014-11-25 13:18:32 -0700709
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600710 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wua64266a2014-12-28 16:32:24 +0800711 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600712 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
713 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
714 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbour8205d902015-04-16 15:59:00 -0600715 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600716 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
717 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
718 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wua64266a2014-12-28 16:32:24 +0800719 samplerCreateInfo.mipLodBias = 0.0;
720 samplerCreateInfo.maxAnisotropy = 0.0;
Tony Barbour8205d902015-04-16 15:59:00 -0600721 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wua64266a2014-12-28 16:32:24 +0800722 samplerCreateInfo.minLod = 0.0;
723 samplerCreateInfo.maxLod = 0.0;
Tony Barbour8205d902015-04-16 15:59:00 -0600724 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barbourf43b6982014-11-25 13:18:32 -0700725
Chia-I Wua64266a2014-12-28 16:32:24 +0800726 init(*m_device, samplerCreateInfo);
Tony Barbour2cb6bf52014-12-03 15:59:38 -0700727}
Tony Barbourf43b6982014-11-25 13:18:32 -0700728
Courtney Goeltzenleuchterea2db0d2014-12-04 15:18:47 -0700729/*
730 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
731 */
Tony Barbour01999182015-04-09 12:58:51 -0600732VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterea2db0d2014-12-04 15:18:47 -0700733{
734 m_device = device;
Tony Barbour9f47dc72014-12-10 14:36:31 -0700735 m_commandBuffer = 0;
Courtney Goeltzenleuchterea2db0d2014-12-04 15:18:47 -0700736
Chia-I Wu714df452015-01-01 07:55:04 +0800737 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchterea2db0d2014-12-04 15:18:47 -0700738}
739
Tony Barbour01999182015-04-09 12:58:51 -0600740VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour864c0332015-03-26 12:37:52 -0600741{
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600742 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour864c0332015-03-26 12:37:52 -0600743 if (m_commandBuffer) {
744 delete m_commandBuffer;
745 }
746}
747
Tony Barbour01999182015-04-09 12:58:51 -0600748VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barbourf43b6982014-11-25 13:18:32 -0700749{
Tony Barbourf43b6982014-11-25 13:18:32 -0700750 m_device = device;
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800751 m_commandBuffer = 0;
752
Chia-I Wu714df452015-01-01 07:55:04 +0800753 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barbourf43b6982014-11-25 13:18:32 -0700754 m_numVertices = constantCount;
755 m_stride = constantSize;
756
Tony Barbour94310562015-04-22 15:10:33 -0600757 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800758 const size_t allocationSize = constantCount * constantSize;
Tony Barbour94310562015-04-22 15:10:33 -0600759 init(*m_device, allocationSize, reqs);
Tony Barbourf43b6982014-11-25 13:18:32 -0700760
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800761 void *pData = map();
762 memcpy(pData, data, allocationSize);
763 unmap();
Tony Barbourf43b6982014-11-25 13:18:32 -0700764
Chia-I Wu714df452015-01-01 07:55:04 +0800765 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600766 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600767 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +0800768 view_info.buffer = obj();
Tony Barbour8205d902015-04-16 15:59:00 -0600769 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +0800770 view_info.offset = 0;
771 view_info.range = allocationSize;
772 m_bufferView.init(*m_device, view_info);
773
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600774 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +0800775 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barbourf43b6982014-11-25 13:18:32 -0700776}
Tony Barbour408ca622014-12-04 14:33:33 -0700777
Tony Barbour8205d902015-04-16 15:59:00 -0600778void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter7715bc42014-12-04 15:26:56 -0700779{
Courtney Goeltzenleuchter46962942015-04-16 13:38:46 -0600780 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter7715bc42014-12-04 15:26:56 -0700781}
782
783
Tony Barbour01999182015-04-09 12:58:51 -0600784void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600785 VkFlags outputMask /*=
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600786 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600787 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
788 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
789 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
790 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600791 VkFlags inputMask /*=
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600792 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600793 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
794 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
795 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
796 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
797 VK_MEMORY_INPUT_SHADER_READ_BIT |
798 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
799 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
800 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barbourf43b6982014-11-25 13:18:32 -0700801{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600802 VkResult err = VK_SUCCESS;
Tony Barbour9f47dc72014-12-10 14:36:31 -0700803
Tony Barbour9f47dc72014-12-10 14:36:31 -0700804 if (!m_commandBuffer)
805 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600806 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour9f47dc72014-12-10 14:36:31 -0700807
Tony Barbour01999182015-04-09 12:58:51 -0600808 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour9f47dc72014-12-10 14:36:31 -0700809 }
810 else
811 {
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800812 m_device->wait(m_fence);
Tony Barbour9f47dc72014-12-10 14:36:31 -0700813 }
814
Tony Barbourf43b6982014-11-25 13:18:32 -0700815 // open the command buffer
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600816 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600817 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbour901f3bc2015-04-01 17:10:07 -0600818 cmd_buf_info.pNext = NULL;
819 cmd_buf_info.flags = 0;
820
Jon Ashburn744b8cf2014-12-31 17:10:47 -0700821 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600822 ASSERT_VK_SUCCESS(err);
Tony Barbourf43b6982014-11-25 13:18:32 -0700823
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600824 VkBufferMemoryBarrier memory_barrier =
Mike Stroyan55658c22014-12-04 11:08:39 +0000825 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600826 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barbourf43b6982014-11-25 13:18:32 -0700827
Tony Barbour8205d902015-04-16 15:59:00 -0600828 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyan55658c22014-12-04 11:08:39 +0000829
830 // write barrier to the command buffer
Tony Barbour8205d902015-04-16 15:59:00 -0600831 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbourf43b6982014-11-25 13:18:32 -0700832
833 // finish recording the command buffer
Tony Barboure8f14162014-12-10 17:28:39 -0700834 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600835 ASSERT_VK_SUCCESS(err);
Tony Barbourf43b6982014-11-25 13:18:32 -0700836
Tony Barbourf43b6982014-11-25 13:18:32 -0700837 // submit the command buffer to the universal queue
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600838 VkCmdBuffer bufferArray[1];
Tony Barboure8f14162014-12-10 17:28:39 -0700839 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600840 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
841 ASSERT_VK_SUCCESS(err);
Tony Barbourf43b6982014-11-25 13:18:32 -0700842}
843
Tony Barbour01999182015-04-09 12:58:51 -0600844VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
845 : VkConstantBufferObj(device)
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700846{
847
848}
849
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600850void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700851{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600852 VkFormat viewFormat;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700853
854 m_numVertices = numIndexes;
855 m_indexType = indexType;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700856 switch (indexType) {
Tony Barbour8205d902015-04-16 15:59:00 -0600857 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700858 m_stride = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600859 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700860 break;
Tony Barbour8205d902015-04-16 15:59:00 -0600861 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700862 m_stride = 2;
Tony Barbour8205d902015-04-16 15:59:00 -0600863 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700864 break;
Tony Barbour8205d902015-04-16 15:59:00 -0600865 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700866 m_stride = 4;
Tony Barbour8205d902015-04-16 15:59:00 -0600867 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700868 break;
Chia-I Wubbdeacc2014-12-15 23:50:11 +0800869 default:
870 assert(!"unknown index type");
Tony Barbour22a30862015-04-22 09:02:32 -0600871 m_stride = 2;
872 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wubbdeacc2014-12-15 23:50:11 +0800873 break;
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700874 }
875
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800876 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour94310562015-04-22 15:10:33 -0600877 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
878 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700879
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800880 void *pData = map();
881 memcpy(pData, data, allocationSize);
882 unmap();
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700883
Chia-I Wu714df452015-01-01 07:55:04 +0800884 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600885 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600886 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +0800887 view_info.buffer = obj();
Tony Barbour8205d902015-04-16 15:59:00 -0600888 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700889 view_info.format = viewFormat;
Chia-I Wu714df452015-01-01 07:55:04 +0800890 view_info.offset = 0;
891 view_info.range = allocationSize;
892 m_bufferView.init(*m_device, view_info);
893
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600894 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +0800895 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700896}
897
Tony Barbour8205d902015-04-16 15:59:00 -0600898void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700899{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600900 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchterfa834c92014-12-04 15:24:05 -0700901}
Tony Barbourf43b6982014-11-25 13:18:32 -0700902
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600903VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbour140a59f2014-12-17 10:57:58 -0700904{
905 return m_indexType;
906}
907
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600908VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barbourf43b6982014-11-25 13:18:32 -0700909{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600910 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600911 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barbourf43b6982014-11-25 13:18:32 -0700912 stageInfo->shader.stage = m_stage;
Chia-I Wuc2c31082014-12-29 14:14:03 +0800913 stageInfo->shader.shader = obj();
Tony Barbourf43b6982014-11-25 13:18:32 -0700914 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600915 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barbourf43b6982014-11-25 13:18:32 -0700916
Tony Barbourf43b6982014-11-25 13:18:32 -0700917 return stageInfo;
918}
919
Tony Barbour8205d902015-04-16 15:59:00 -0600920VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barbourf43b6982014-11-25 13:18:32 -0700921{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600922 VkResult err = VK_SUCCESS;
Cody Northropacfb0492015-03-17 15:55:58 -0600923 std::vector<unsigned int> spv;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600924 VkShaderCreateInfo createInfo;
Tony Barbourf43b6982014-11-25 13:18:32 -0700925 size_t shader_len;
926
927 m_stage = stage;
928 m_device = device;
929
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600930 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barbourf43b6982014-11-25 13:18:32 -0700931 createInfo.pNext = NULL;
932
Cody Northropacfb0492015-03-17 15:55:58 -0600933 if (!framework->m_use_spv) {
Tony Barbourf43b6982014-11-25 13:18:32 -0700934
935 shader_len = strlen(shader_code);
936 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
937 createInfo.pCode = malloc(createInfo.codeSize);
938 createInfo.flags = 0;
939
Tony Barbour8205d902015-04-16 15:59:00 -0600940 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northropacfb0492015-03-17 15:55:58 -0600941 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barbourf43b6982014-11-25 13:18:32 -0700942 ((uint32_t *) createInfo.pCode)[1] = 0;
943 ((uint32_t *) createInfo.pCode)[2] = stage;
944 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
945
Chia-I Wuc2c31082014-12-29 14:14:03 +0800946 err = init_try(*m_device, createInfo);
Tony Barbourf43b6982014-11-25 13:18:32 -0700947 }
948
Cody Northropacfb0492015-03-17 15:55:58 -0600949 if (framework->m_use_spv || err) {
950 std::vector<unsigned int> spv;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600951 err = VK_SUCCESS;
Tony Barbourf43b6982014-11-25 13:18:32 -0700952
Cody Northropacfb0492015-03-17 15:55:58 -0600953 // Use Reference GLSL to SPV compiler
954 framework->GLSLtoSPV(stage, shader_code, spv);
955 createInfo.pCode = spv.data();
956 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barbourf43b6982014-11-25 13:18:32 -0700957 createInfo.flags = 0;
Tony Barbour408ca622014-12-04 14:33:33 -0700958
Chia-I Wuc2c31082014-12-29 14:14:03 +0800959 init(*m_device, createInfo);
960 }
Tony Barbour2cb6bf52014-12-03 15:59:38 -0700961}
Tony Barbour408ca622014-12-04 14:33:33 -0700962
Tony Barbour01999182015-04-09 12:58:51 -0600963VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barbourf43b6982014-11-25 13:18:32 -0700964{
Tony Barbourf43b6982014-11-25 13:18:32 -0700965 m_device = device;
966 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
967 m_vertexBufferCount = 0;
968
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600969 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
970 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbour8205d902015-04-16 15:59:00 -0600971 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600972 m_ia_state.disableVertexReuse = VK_FALSE;
973 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barbourf43b6982014-11-25 13:18:32 -0700974 m_ia_state.primitiveRestartIndex = 0;
975
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600976 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbourf43b6982014-11-25 13:18:32 -0700977 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600978 m_rs_state.depthClipEnable = VK_FALSE;
979 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
980 m_rs_state.programPointSize = VK_FALSE;
981 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
982 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbour8205d902015-04-16 15:59:00 -0600983 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter28846ae2015-04-30 17:44:12 -0600984 m_rs_state.cullMode = VK_CULL_MODE_BACK;
985 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barbourf43b6982014-11-25 13:18:32 -0700986
Tony Barbourf43b6982014-11-25 13:18:32 -0700987 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600988 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbourf43b6982014-11-25 13:18:32 -0700989 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600990 m_cb_state.alphaToCoverageEnable = VK_FALSE;
991 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barbourf43b6982014-11-25 13:18:32 -0700992
Tony Barbourfa6cac72015-01-16 14:27:35 -0700993 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600994 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
995 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700996 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
997 m_ms_state.samples = 1;
998 m_ms_state.minSampleShading = 0;
999 m_ms_state.sampleShadingEnable = 0;
Tony Barbourf43b6982014-11-25 13:18:32 -07001000
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001001 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001002 m_ds_state.pNext = &m_ms_state,
Tony Barbour8205d902015-04-16 15:59:00 -06001003 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001004 m_ds_state.depthTestEnable = VK_FALSE;
1005 m_ds_state.depthWriteEnable = VK_FALSE;
1006 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbour8205d902015-04-16 15:59:00 -06001007 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001008 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1009 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1010 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001011 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001012 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001013 m_ds_state.front = m_ds_state.back;
Tony Barbourf43b6982014-11-25 13:18:32 -07001014
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001015 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001016 att.blendEnable = VK_FALSE;
Tony Barbour8205d902015-04-16 15:59:00 -06001017 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001018 att.channelWriteMask = 0xf;
1019 AddColorAttachment(0, &att);
Tony Barbourf43b6982014-11-25 13:18:32 -07001020
1021};
1022
Tony Barbour01999182015-04-09 12:58:51 -06001023void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barbourf43b6982014-11-25 13:18:32 -07001024{
1025 m_shaderObjs.push_back(shader);
1026}
1027
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001028void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barbourf43b6982014-11-25 13:18:32 -07001029{
1030 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1031 m_vi_state.attributeCount = count;
1032}
1033
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001034void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barbourf43b6982014-11-25 13:18:32 -07001035{
1036 m_vi_state.pVertexBindingDescriptions = vi_binding;
1037 m_vi_state.bindingCount = count;
1038}
1039
Tony Barbour01999182015-04-09 12:58:51 -06001040void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barbourf43b6982014-11-25 13:18:32 -07001041{
1042 m_vertexBufferObjs.push_back(vertexDataBuffer);
1043 m_vertexBufferBindings.push_back(binding);
1044 m_vertexBufferCount++;
1045}
1046
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001047void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wue7748802014-12-05 10:45:15 +08001048{
Tony Barbourfa6cac72015-01-16 14:27:35 -07001049 if (binding+1 > m_colorAttachments.size())
1050 {
1051 m_colorAttachments.resize(binding+1);
1052 }
1053 m_colorAttachments[binding] = *att;
1054}
1055
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001056void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourfa6cac72015-01-16 14:27:35 -07001057{
1058 m_ds_state.format = ds_state->format;
1059 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1060 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1061 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbour8205d902015-04-16 15:59:00 -06001062 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001063 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1064 m_ds_state.back = ds_state->back;
1065 m_ds_state.front = ds_state->front;
Chia-I Wue7748802014-12-05 10:45:15 +08001066}
1067
Tony Barbour01999182015-04-09 12:58:51 -06001068void VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001069{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001070 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001071 VkGraphicsPipelineCreateInfo info = {};
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001072
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001073 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001074
1075 for (int i=0; i<m_shaderObjs.size(); i++)
1076 {
Chia-I Wuf8385062015-01-04 16:27:24 +08001077 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001078 shaderCreateInfo->pNext = head_ptr;
1079 head_ptr = shaderCreateInfo;
1080 }
1081
1082 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1083 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001084 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001085 m_vi_state.pNext = head_ptr;
1086 head_ptr = &m_vi_state;
1087 }
1088
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001089 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1090 info.pNext = head_ptr;
1091 info.flags = 0;
1092 info.layout = descriptorSet.GetPipelineLayout();
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001093
Tony Barbourfa6cac72015-01-16 14:27:35 -07001094 m_cb_state.attachmentCount = m_colorAttachments.size();
1095 m_cb_state.pAttachments = &m_colorAttachments[0];
1096
Chia-I Wuf5d81872014-12-29 14:24:14 +08001097 init(*m_device, info);
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001098}
Chia-I Wuf5d81872014-12-29 14:24:14 +08001099
Tony Barbour8205d902015-04-16 15:59:00 -06001100vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001101{
Tony Barbour8205d902015-04-16 15:59:00 -06001102 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001103 if (this->mem_refs_.size()) {
1104 mems.reserve(this->mem_refs_.size());
1105 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1106 mems.push_back(this->mem_refs_[i]);
1107 }
1108
1109 return mems;
Tony Barboure7f2c7c2014-12-17 11:57:31 -07001110}
1111
Tony Barbour01999182015-04-09 12:58:51 -06001112VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001113 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001114{
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001115 m_device = device;
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001116}
Tony Barboure8f14162014-12-10 17:28:39 -07001117
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001118VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001119{
Chia-I Wuae0564f2014-12-28 15:12:48 +08001120 return obj();
Tony Barbour4f3e5a62014-12-10 14:34:45 -07001121}
Tony Barboure8f14162014-12-10 17:28:39 -07001122
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001123VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barboure8f14162014-12-10 17:28:39 -07001124{
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001125 begin(pInfo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001126 return VK_SUCCESS;
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001127}
1128
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001129VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001130{
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001131 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001132 return VK_SUCCESS;
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001133}
1134
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001135VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001136{
1137 begin();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001138 return VK_SUCCESS;
Tony Barboure8f14162014-12-10 17:28:39 -07001139}
1140
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001141VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barboure8f14162014-12-10 17:28:39 -07001142{
Chia-I Wuae0564f2014-12-28 15:12:48 +08001143 end();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001144 return VK_SUCCESS;
Tony Barboure8f14162014-12-10 17:28:39 -07001145}
1146
Tony Barbour8205d902015-04-16 15:59:00 -06001147void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barboure8f14162014-12-10 17:28:39 -07001148{
Tony Barbour8205d902015-04-16 15:59:00 -06001149 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barboure8f14162014-12-10 17:28:39 -07001150}
1151
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001152void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour01999182015-04-09 12:58:51 -06001153 VkDepthStencilObj *depthStencilObj)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001154{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001155 uint32_t i;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001156 const VkFlags output_mask =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001157 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001158 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1159 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1160 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001161 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001162 const VkFlags input_mask = 0;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001163
1164 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001165 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001166 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001167 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001168 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001169 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001170 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001171
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001172 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001173 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyan55658c22014-12-04 11:08:39 +00001174 memory_barrier.outputMask = output_mask;
1175 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001176 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyan55658c22014-12-04 11:08:39 +00001177 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001178 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyan55658c22014-12-04 11:08:39 +00001179
Tony Barbour8205d902015-04-16 15:59:00 -06001180 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyan55658c22014-12-04 11:08:39 +00001181
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -07001182 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyan55658c22014-12-04 11:08:39 +00001183 memory_barrier.image = m_renderTargets[i]->image();
1184 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbour8205d902015-04-16 15:59:00 -06001185 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyan55658c22014-12-04 11:08:39 +00001186 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001187
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001188 vkCmdClearColorImage(obj(),
1189 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterda4a99e2015-04-23 17:49:22 -06001190 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001191
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001192 }
1193
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001194 if (depthStencilObj)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001195 {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001196 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001197 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001198 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001199 dsRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001200 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001201 dsRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001202
1203 // prepare the depth buffer for clear
Mike Stroyan55658c22014-12-04 11:08:39 +00001204
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001205 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001206 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001207 memory_barrier.image = depthStencilObj->obj();
Mike Stroyan55658c22014-12-04 11:08:39 +00001208 memory_barrier.subresourceRange = dsRange;
1209
Tony Barbour8205d902015-04-16 15:59:00 -06001210 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001211
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001212 vkCmdClearDepthStencil(obj(),
1213 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter7e305322015-03-05 17:26:38 -07001214 depth_clear_color, stencil_clear_color,
1215 1, &dsRange);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001216
1217 // prepare depth buffer for rendering
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001218 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001219 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001220 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyan55658c22014-12-04 11:08:39 +00001221 memory_barrier.subresourceRange = dsRange;
Tony Barbour8205d902015-04-16 15:59:00 -06001222 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001223 }
1224}
1225
Tony Barbour01999182015-04-09 12:58:51 -06001226void VkCommandBufferObj::PrepareAttachments()
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001227{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001228 uint32_t i;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001229 const VkFlags output_mask =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001230 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001231 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1232 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1233 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001234 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001235 const VkFlags input_mask =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001236 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001237 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1238 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1239 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1240 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1241 VK_MEMORY_INPUT_SHADER_READ_BIT |
1242 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1243 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001244 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyan55658c22014-12-04 11:08:39 +00001245
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001246 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001247 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001248 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001249 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001250 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001251 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001252
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001253 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001254 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyan55658c22014-12-04 11:08:39 +00001255 memory_barrier.outputMask = output_mask;
1256 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001257 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyan55658c22014-12-04 11:08:39 +00001258 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001259 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyan55658c22014-12-04 11:08:39 +00001260
Tony Barbour8205d902015-04-16 15:59:00 -06001261 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyan55658c22014-12-04 11:08:39 +00001262
Courtney Goeltzenleuchterdd745fd2015-03-05 16:47:18 -07001263 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001264 {
Mike Stroyan55658c22014-12-04 11:08:39 +00001265 memory_barrier.image = m_renderTargets[i]->image();
1266 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbour8205d902015-04-16 15:59:00 -06001267 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyan55658c22014-12-04 11:08:39 +00001268 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001269 }
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001270}
1271
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001272void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001273{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001274 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001275 renderpass,
1276 framebuffer,
1277 };
1278
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001279 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001280}
1281
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001282void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001283{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001284 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -06001285}
1286
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001287void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001288{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001289 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001290}
1291
Tony Barbour01999182015-04-09 12:58:51 -06001292void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001293{
1294 m_renderTargets.push_back(renderTarget);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001295}
1296
Tony Barbour01999182015-04-09 12:58:51 -06001297void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001298{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001299 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001300}
1301
Tony Barbour01999182015-04-09 12:58:51 -06001302void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001303{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001304 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001305}
1306
Tony Barbour01999182015-04-09 12:58:51 -06001307void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001308{
Tony Barbour67324f72015-04-08 10:11:29 -06001309 QueueCommandBuffer(NULL);
1310}
1311
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001312void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour67324f72015-04-08 10:11:29 -06001313{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001314 VkResult err = VK_SUCCESS;
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001315
1316 // submit the command buffer to the universal queue
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001317 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1318 ASSERT_VK_SUCCESS( err );
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001319
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001320 err = vkQueueWaitIdle( m_device->m_queue );
1321 ASSERT_VK_SUCCESS( err );
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001322
1323 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001324 vkDeviceWaitIdle(m_device->device());
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001325}
1326
Tony Barbour01999182015-04-09 12:58:51 -06001327void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001328{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001329 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001330}
1331
Tony Barbour01999182015-04-09 12:58:51 -06001332void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001333{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001334 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu862c5572015-03-28 15:23:55 +08001335
Chia-I Wu714df452015-01-01 07:55:04 +08001336 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001337 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northrop1a01b1d2015-04-16 13:41:56 -06001338 0, 1, &set_obj, 0, NULL );
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001339}
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001340
Tony Barbour01999182015-04-09 12:58:51 -06001341void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001342{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001343 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001344}
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001345
Tony Barbour8205d902015-04-16 15:59:00 -06001346void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001347{
Courtney Goeltzenleuchter46962942015-04-16 13:38:46 -06001348 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour1d7a55d2014-12-17 11:53:55 -07001349}
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001350
Tony Barbour01999182015-04-09 12:58:51 -06001351VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour17c6ab12015-03-27 17:03:18 -06001352{
1353 m_initialized = false;
1354}
Tony Barbour01999182015-04-09 12:58:51 -06001355bool VkDepthStencilObj::Initialized()
Tony Barbour17c6ab12015-03-27 17:03:18 -06001356{
1357 return m_initialized;
1358}
1359
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001360VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour17c6ab12015-03-27 17:03:18 -06001361{
1362 return &m_depthStencilBindInfo;
1363}
1364
Tony Barbour01999182015-04-09 12:58:51 -06001365void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour17c6ab12015-03-27 17:03:18 -06001366{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001367 VkImageCreateInfo image_info;
1368 VkDepthStencilViewCreateInfo view_info;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001369
1370 m_device = device;
1371 m_initialized = true;
Tony Barbour8205d902015-04-16 15:59:00 -06001372 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001373
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001374 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001375 image_info.pNext = NULL;
Tony Barbour8205d902015-04-16 15:59:00 -06001376 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001377 image_info.format = m_depth_stencil_fmt;
1378 image_info.extent.width = width;
1379 image_info.extent.height = height;
1380 image_info.extent.depth = 1;
1381 image_info.mipLevels = 1;
1382 image_info.arraySize = 1;
1383 image_info.samples = 1;
Tony Barbour8205d902015-04-16 15:59:00 -06001384 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001385 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001386 image_info.flags = 0;
1387 init(*m_device, image_info);
1388
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001389 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001390 view_info.pNext = NULL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001391 view_info.image = VK_NULL_HANDLE;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001392 view_info.mipLevel = 0;
1393 view_info.baseArraySlice = 0;
1394 view_info.arraySize = 1;
1395 view_info.flags = 0;
1396 view_info.image = obj();
1397 m_depthStencilView.init(*m_device, view_info);
1398
1399 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001400 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour17c6ab12015-03-27 17:03:18 -06001401}