blob: 7ba8874832da5a25e993b484fcd56e62f39c4051 [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{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060063 VkResult err;
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060064 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060065 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060066 instInfo.pNext = NULL;
67 instInfo.pAppInfo = &app_info;
68 instInfo.pAllocCb = NULL;
69 instInfo.extensionCount = 0;
70 instInfo.ppEnabledExtensionNames = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060071 err = vkCreateInstance(&instInfo, &this->inst);
72 ASSERT_VK_SUCCESS(err);
Jon Ashburn83a64252015-04-15 11:31:12 -060073 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
74 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
75 ASSERT_VK_SUCCESS(err);
76 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060077 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070078 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060079
Tony Barbour6918cd52015-04-09 12:58:51 -060080 m_device = new VkDeviceObj(0, objs[0]);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060081 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -060082
Tony Barbour6918cd52015-04-09 12:58:51 -060083 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060084}
85
Tony Barbour6918cd52015-04-09 12:58:51 -060086void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060087{
Mike Stroyanb050c682015-04-17 12:36:38 -060088 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
89 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
90 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
91 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
92 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
93 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060094
95 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -060096 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060097 }
Tobin Ehlis976fc162015-03-26 08:23:25 -060098 while (!m_renderTargets.empty()) {
Mike Stroyanb050c682015-04-17 12:36:38 -060099 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
100 vkQueueBindObjectMemory(m_device->m_queue, VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image(), 0, VK_NULL_HANDLE, 0);
101 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
102 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600103 m_renderTargets.pop_back();
104 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600105
Tony Barbour1c45ce02015-03-27 17:03:18 -0600106 delete m_depthStencil;
107
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600108 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800109 delete m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600110 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600111}
112
Tony Barbour6918cd52015-04-09 12:58:51 -0600113void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600114{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600115 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600116
Tony Barbourd1c35722015-04-16 15:59:00 -0600117 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600118
119 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600120 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600121 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700122 raster.pointSize = 1.0;
123
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600124 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
125 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600126
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600127 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600128 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
129 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
130 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600131
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600132 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600133 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600134 depthStencil.minDepth = 0.f;
135 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700136 depthStencil.stencilFrontRef = 0;
137 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600138
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600139 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
140 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600141
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600142 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600143
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600144 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700145 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
146
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600147 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
148 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600149}
150
Tony Barbour6918cd52015-04-09 12:58:51 -0600151void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600152{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600153 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600154
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600155 VkViewport viewport;
156 VkRect scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600157
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600158 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600159 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700160 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700161 viewport.originX = 0;
162 viewport.originY = 0;
163 viewport.width = 1.f * width;
164 viewport.height = 1.f * height;
165 viewport.minDepth = 0.f;
166 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700167 scissor.extent.width = width;
168 scissor.extent.height = height;
169 scissor.offset.x = 0;
170 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700171 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700172 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700173
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600174 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
175 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600176 m_width = width;
177 m_height = height;
178}
179
Tony Barbour6918cd52015-04-09 12:58:51 -0600180void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600181{
182 InitViewport(m_width, m_height);
183}
Tony Barbour6918cd52015-04-09 12:58:51 -0600184void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600185{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700186 InitRenderTarget(1);
187}
188
Tony Barbour6918cd52015-04-09 12:58:51 -0600189void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700190{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600191 InitRenderTarget(targets, NULL);
192}
193
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600194void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600195{
196 InitRenderTarget(1, dsBinding);
197}
198
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600199void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600200{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600201 std::vector<VkAttachmentLoadOp> load_ops;
202 std::vector<VkAttachmentStoreOp> store_ops;
203 std::vector<VkClearColor> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600204
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600205 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800206
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700207 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600208 VkImageObj *img = new VkImageObj(m_device);
Chia-I Wuf50ee212014-12-29 14:31:52 +0800209 img->init(m_width, m_height, m_render_target_fmt,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600210 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700211 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600212 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700213 m_renderTargets.push_back(img);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600214 load_ops.push_back(VK_ATTACHMENT_LOAD_OP_LOAD);
215 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600216 clear_colors.push_back(m_clear_color);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600217// m_mem_ref_mgr.AddMemoryRefs(*img);
Chia-I Wuecebf752014-12-05 10:45:15 +0800218 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700219 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600220 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600221 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600222 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700223 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600224 fb_info.pColorAttachments = m_colorBindings;
225 fb_info.pDepthStencilAttachment = dsBinding;
226 fb_info.sampleCount = 1;
227 fb_info.width = (uint32_t)m_width;
228 fb_info.height = (uint32_t)m_height;
229 fb_info.layers = 1;
230
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600231 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600232
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600233 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600234 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700235 rp_info.renderArea.extent.width = m_width;
236 rp_info.renderArea.extent.height = m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600237
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700238 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600239 rp_info.pColorFormats = &m_render_target_fmt;
240 rp_info.pColorLayouts = &m_colorBindings[0].layout;
241 rp_info.pColorLoadOps = &load_ops[0];
242 rp_info.pColorStoreOps = &store_ops[0];
243 rp_info.pColorLoadClearValues = &clear_colors[0];
244 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600245 if (dsBinding) {
246 rp_info.depthStencilLayout = dsBinding->layout;
247 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600248 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600249 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600250 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
251 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600252 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600253 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
254 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600255}
256
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600257
258
Tony Barbourd1c35722015-04-16 15:59:00 -0600259VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600260 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800261{
262 init();
263
264 props = gpu().properties();
265 queue_props = &gpu().queue_properties()[0];
266}
267
Tony Barbour6918cd52015-04-09 12:58:51 -0600268void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800269{
270 ASSERT_NE(true, graphics_queues().empty());
271 m_queue = graphics_queues()[0]->obj();
272}
273
Tony Barbour6918cd52015-04-09 12:58:51 -0600274VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700275{
276 m_device = device;
277 m_nextSlot = 0;
278
279}
280
Tony Barbour6918cd52015-04-09 12:58:51 -0600281VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700282{
Chia-I Wu11078b02015-01-04 16:27:24 +0800283 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700284}
Tony Barbour82c39522014-12-04 14:33:33 -0700285
Tony Barbour6918cd52015-04-09 12:58:51 -0600286int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700287{
Chia-I Wu11078b02015-01-04 16:27:24 +0800288 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600289 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600290 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800291 tc.count = 1;
292 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700293
Chia-I Wu11078b02015-01-04 16:27:24 +0800294 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700295}
Tony Barbour82c39522014-12-04 14:33:33 -0700296
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600297int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700298{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600299 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800300 tc.type = type;
301 tc.count = 1;
302 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700303
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600304 m_updateBuffers.push_back(vk_testing::DescriptorSet::update(type,
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600305 m_nextSlot, 0, 1, &constantBuffer.m_bufferViewInfo));
306
307 // Track mem references for this descriptor set object
308 mem_ref_mgr.AddMemoryRefs(constantBuffer);
Chia-I Wu11078b02015-01-04 16:27:24 +0800309
310 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700311}
Tony Barbour82c39522014-12-04 14:33:33 -0700312
Tony Barbour6918cd52015-04-09 12:58:51 -0600313int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700314{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600315 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600316 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800317 tc.count = 1;
318 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700319
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600320 VkSamplerImageViewInfo tmp = {};
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600321 tmp.sampler = sampler->obj();
Chia-I Wu11078b02015-01-04 16:27:24 +0800322 tmp.pImageView = &texture->m_textureViewInfo;
323 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700324
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600325 m_updateSamplerTextures.push_back(vk_testing::DescriptorSet::update(m_nextSlot, 0, 1,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600326 (const VkSamplerImageViewInfo *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700327
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600328 // Track mem references for the texture referenced here
329 mem_ref_mgr.AddMemoryRefs(*texture);
330
Chia-I Wu11078b02015-01-04 16:27:24 +0800331 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700332}
Chia-I Wu11078b02015-01-04 16:27:24 +0800333
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500334VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700335{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500336 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700337}
338
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600339VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700340{
Chia-I Wu11078b02015-01-04 16:27:24 +0800341 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700342}
Tony Barboure2c58df2014-11-25 13:18:32 -0700343
Tony Barbour6918cd52015-04-09 12:58:51 -0600344void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700345{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600346 // create VkDescriptorPool
347 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600348 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800349 pool.count = m_type_counts.size();
350 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600351 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700352
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600353 // create VkDescriptorSetLayout
354 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800355 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800356 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800357 bindings[i].descriptorType = m_type_counts[i].type;
358 bindings[i].count = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600359 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800360 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700361 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800362
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600363 // create VkDescriptorSetLayout
364 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600365 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800366 layout.count = bindings.size();
367 layout.pBinding = &bindings[0];
368
Chia-I Wu41126e52015-03-26 15:27:55 +0800369 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600370 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800371 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500372
373 // create VkPipelineLayout
374 VkPipelineLayoutCreateInfo pipeline_layout = {};
375 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
376 pipeline_layout.descriptorSetCount = layouts.size();
377 pipeline_layout.pSetLayouts = NULL;
378
379 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700380
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600381 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500382 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800383
Chia-I Wu41126e52015-03-26 15:27:55 +0800384 // build the update array
385 vector<const void *> update_array;
Chia-I Wu11078b02015-01-04 16:27:24 +0800386
Chia-I Wu41126e52015-03-26 15:27:55 +0800387 for (int i = 0; i < m_updateBuffers.size(); i++) {
388 update_array.push_back(&m_updateBuffers[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800389 }
390 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
391 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
Chia-I Wu41126e52015-03-26 15:27:55 +0800392 update_array.push_back(&m_updateSamplerTextures[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800393 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800394
395 // do the updates
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600396 m_device->begin_descriptor_pool_update(VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu11078b02015-01-04 16:27:24 +0800397 clear_sets(*m_set);
Chia-I Wu41126e52015-03-26 15:27:55 +0800398 m_set->update(update_array);
Chia-I Wu985ba162015-03-26 13:14:16 +0800399 m_device->end_descriptor_pool_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700400}
Tony Barboure2c58df2014-11-25 13:18:32 -0700401
Tony Barbour6918cd52015-04-09 12:58:51 -0600402VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800403{
404 m_device = dev;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600405 m_imageInfo.view = VK_NULL_HANDLE;
406 m_imageInfo.layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800407}
408
Tony Barbour6918cd52015-04-09 12:58:51 -0600409void VkImageObj::ImageMemoryBarrier(
410 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600411 VkImageAspect aspect,
412 VkFlags output_mask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600413 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
414 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
415 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
416 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
417 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600418 VkFlags input_mask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600419 VK_MEMORY_INPUT_CPU_READ_BIT |
420 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
421 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
422 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
423 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
424 VK_MEMORY_INPUT_SHADER_READ_BIT |
425 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
426 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
427 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600428 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600429{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600430 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
431 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600432 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
433 subresourceRange);
434
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600435 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600436
Tony Barbourd1c35722015-04-16 15:59:00 -0600437 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600438
439 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600440 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 -0600441}
442
Tony Barbour6918cd52015-04-09 12:58:51 -0600443void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600444 VkImageAspect aspect,
445 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600446{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600447 VkFlags output_mask, input_mask;
448 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600449 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
450 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
451 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
452 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600453 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600454 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600455 VK_MEMORY_INPUT_CPU_READ_BIT |
456 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
457 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
458 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
459 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
460 VK_MEMORY_INPUT_SHADER_READ_BIT |
461 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
462 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600463 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600464
465 if (image_layout == m_imageInfo.layout) {
466 return;
467 }
468
469 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600470 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600471 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
472 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600473 break;
474
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600475 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600476 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
477 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600478 break;
479
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600480 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600481 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
482 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600483 break;
484
Tony Barbourf20f87b2015-04-22 09:02:32 -0600485 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600486 default:
487 output_mask = all_cache_outputs;
488 input_mask = all_cache_inputs;
489 break;
490 }
491
492 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
493 m_imageInfo.layout = image_layout;
494}
495
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600496void VkImageObj::SetLayout(VkImageAspect aspect,
497 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600498{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600499 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600500 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600501
502 /* Build command buffer to set image layout in the driver */
503 err = cmd_buf.BeginCommandBuffer();
504 assert(!err);
505
506 SetLayout(&cmd_buf, aspect, image_layout);
507
508 err = cmd_buf.EndCommandBuffer();
509 assert(!err);
510
511 cmd_buf.QueueCommandBuffer();
512}
513
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600514bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600515{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600516 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600517 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600518 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600519
Tony Barbour579f7802015-04-03 15:11:43 -0600520 return true;
521}
522
Tony Barbour6918cd52015-04-09 12:58:51 -0600523void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600524 VkFormat fmt, VkFlags usage,
525 VkImageTiling requested_tiling)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800526{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600527 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600528 VkFormatProperties image_fmt;
529 VkImageTiling tiling;
530 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600531 size_t size;
532
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800533 mipCount = 0;
534
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600535 uint32_t _w = w;
536 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800537 while( ( _w > 0 ) || ( _h > 0 ) )
538 {
539 _w >>= 1;
540 _h >>= 1;
541 mipCount++;
542 }
543
Tony Barbour579f7802015-04-03 15:11:43 -0600544 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600545 err = vkGetFormatInfo(m_device->obj(), fmt,
Tony Barbourd1c35722015-04-16 15:59:00 -0600546 VK_FORMAT_INFO_TYPE_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600547 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600548 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600549
Tony Barbourd1c35722015-04-16 15:59:00 -0600550 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600551 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600552 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600553 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600554 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600555 } else {
556 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
557 }
558 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600559 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600560 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600561 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600562 } else {
563 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
564 }
565
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600566 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600567 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800568 imageCreateInfo.format = fmt;
569 imageCreateInfo.extent.width = w;
570 imageCreateInfo.extent.height = h;
571 imageCreateInfo.mipLevels = mipCount;
572 imageCreateInfo.tiling = tiling;
573
574 imageCreateInfo.usage = usage;
575
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600576 vk_testing::Image::init(*m_device, imageCreateInfo);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800577
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600578 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600579 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600580 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600581 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600582 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800583}
584
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600585VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800586{
587 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600588 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800589}
590
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600591VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800592{
593 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600594 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800595}
596
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600597VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600598{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600599 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600600 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600601 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600602
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600603 /* Build command buffer to copy staging texture to usable texture */
604 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600605 assert(!err);
606
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600607 /* TODO: Can we determine image aspect from image object? */
608 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600609 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600610
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600611 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600612 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600613
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600614 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600615 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600616 copy_region.srcSubresource.arraySlice = 0;
617 copy_region.srcSubresource.mipLevel = 0;
618 copy_region.srcOffset.x = 0;
619 copy_region.srcOffset.y = 0;
620 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600621 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600622 copy_region.destSubresource.arraySlice = 0;
623 copy_region.destSubresource.mipLevel = 0;
624 copy_region.destOffset.x = 0;
625 copy_region.destOffset.y = 0;
626 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600627 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600628
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600629 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600630 src_image.obj(), src_image.layout(),
631 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600632 1, &copy_region);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600633 cmd_buf.mem_ref_mgr.AddMemoryRefs(src_image);
634 cmd_buf.mem_ref_mgr.AddMemoryRefs(*this);
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 Barbour5dc515d2015-04-01 17:47:06 -0600657
Tony Barbourd1c35722015-04-16 15:59:00 -0600658 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600659 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600660
661 if (colors == NULL)
662 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700663
664 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
665
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600666 m_textureViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700667
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600668 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600669 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600670 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600671 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600672 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600673 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600674 view.channels.r = VK_CHANNEL_SWIZZLE_R;
675 view.channels.g = VK_CHANNEL_SWIZZLE_G;
676 view.channels.b = VK_CHANNEL_SWIZZLE_B;
677 view.channels.a = VK_CHANNEL_SWIZZLE_A;
678 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600679 view.subresourceRange.baseMipLevel = 0;
680 view.subresourceRange.mipLevels = 1;
681 view.subresourceRange.baseArraySlice = 0;
682 view.subresourceRange.arraySize = 1;
683 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700684
Tony Barboure2c58df2014-11-25 13:18:32 -0700685 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600686 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700687
688 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800689 view.image = obj();
690 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600691 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700692
Tony Barbour5dc515d2015-04-01 17:47:06 -0600693 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700694
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800695 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700696 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800697 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600698 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700699 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600700 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600701 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700702}
Tony Barbour82c39522014-12-04 14:33:33 -0700703
Tony Barbour6918cd52015-04-09 12:58:51 -0600704VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700705{
Tony Barboure2c58df2014-11-25 13:18:32 -0700706 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700707
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600708 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800709 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600710 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
711 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
712 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600713 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600714 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
715 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
716 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800717 samplerCreateInfo.mipLodBias = 0.0;
718 samplerCreateInfo.maxAnisotropy = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600719 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800720 samplerCreateInfo.minLod = 0.0;
721 samplerCreateInfo.maxLod = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600722 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700723
Chia-I Wue9864b52014-12-28 16:32:24 +0800724 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700725}
Tony Barboure2c58df2014-11-25 13:18:32 -0700726
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700727/*
728 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
729 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600730VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700731{
732 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700733 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700734
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800735 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700736}
737
Tony Barbour6918cd52015-04-09 12:58:51 -0600738VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600739{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600740 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600741 if (m_commandBuffer) {
742 delete m_commandBuffer;
743 }
744}
745
Tony Barbour6918cd52015-04-09 12:58:51 -0600746VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700747{
Tony Barboure2c58df2014-11-25 13:18:32 -0700748 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800749 m_commandBuffer = 0;
750
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800751 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700752 m_numVertices = constantCount;
753 m_stride = constantSize;
754
Chia-I Wua07fee62014-12-28 15:26:08 +0800755 const size_t allocationSize = constantCount * constantSize;
756 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700757
Chia-I Wua07fee62014-12-28 15:26:08 +0800758 void *pData = map();
759 memcpy(pData, data, allocationSize);
760 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700761
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800762 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600763 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600764 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800765 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600766 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800767 view_info.offset = 0;
768 view_info.range = allocationSize;
769 m_bufferView.init(*m_device, view_info);
770
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600771 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800772 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700773}
Tony Barbour82c39522014-12-04 14:33:33 -0700774
Tony Barbourd1c35722015-04-16 15:59:00 -0600775void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700776{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600777 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700778}
779
780
Tony Barbour6918cd52015-04-09 12:58:51 -0600781void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600782 VkFlags outputMask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600783 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
784 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
785 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
786 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
787 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600788 VkFlags inputMask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600789 VK_MEMORY_INPUT_CPU_READ_BIT |
790 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
791 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
792 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
793 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
794 VK_MEMORY_INPUT_SHADER_READ_BIT |
795 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
796 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
797 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700798{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600799 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700800
Tony Barbour38422802014-12-10 14:36:31 -0700801 if (!m_commandBuffer)
802 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600803 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700804
Tony Barbour6918cd52015-04-09 12:58:51 -0600805 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700806 }
807 else
808 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800809 m_device->wait(m_fence);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600810 m_commandBuffer->mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour38422802014-12-10 14:36:31 -0700811 }
812
Tony Barboure2c58df2014-11-25 13:18:32 -0700813 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600814 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600815 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600816 cmd_buf_info.pNext = NULL;
817 cmd_buf_info.flags = 0;
818
Jon Ashburnc4164b12014-12-31 17:10:47 -0700819 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600820 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700821
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600822 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000823 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600824 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700825
Tony Barbourd1c35722015-04-16 15:59:00 -0600826 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000827
828 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600829 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700830
831 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700832 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600833 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700834
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600835 /*
836 * Tell driver about memory references made in this command buffer
837 * Note: Since this command buffer only has a PipelineBarrier
838 * command there really aren't any memory refs to worry about.
839 */
840 m_commandBuffer->mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
Tony Barboure2c58df2014-11-25 13:18:32 -0700841
842 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600843 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700844 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600845 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
846 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700847}
848
Tony Barbour6918cd52015-04-09 12:58:51 -0600849VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
850 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700851{
852
853}
854
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600855void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700856{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600857 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700858
859 m_numVertices = numIndexes;
860 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700861 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600862 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700863 m_stride = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -0600864 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700865 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600866 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700867 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600868 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700869 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600870 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700871 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600872 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700873 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800874 default:
875 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600876 m_stride = 2;
877 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800878 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700879 }
880
Chia-I Wua07fee62014-12-28 15:26:08 +0800881 const size_t allocationSize = numIndexes * m_stride;
882 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700883
Chia-I Wua07fee62014-12-28 15:26:08 +0800884 void *pData = map();
885 memcpy(pData, data, allocationSize);
886 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700887
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800888 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600889 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600890 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800891 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600892 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700893 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800894 view_info.offset = 0;
895 view_info.range = allocationSize;
896 m_bufferView.init(*m_device, view_info);
897
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600898 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800899 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700900}
901
Tony Barbourd1c35722015-04-16 15:59:00 -0600902void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700903{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600904 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700905}
Tony Barboure2c58df2014-11-25 13:18:32 -0700906
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600907VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -0700908{
909 return m_indexType;
910}
911
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600912VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700913{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600914 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600915 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700916 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800917 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700918 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600919 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700920
Tony Barboure2c58df2014-11-25 13:18:32 -0700921 return stageInfo;
922}
923
Tony Barbourd1c35722015-04-16 15:59:00 -0600924VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -0700925{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600926 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600927 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600928 VkShaderCreateInfo createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700929 size_t shader_len;
930
931 m_stage = stage;
932 m_device = device;
933
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600934 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700935 createInfo.pNext = NULL;
936
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600937 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700938
939 shader_len = strlen(shader_code);
940 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
941 createInfo.pCode = malloc(createInfo.codeSize);
942 createInfo.flags = 0;
943
Tony Barbourd1c35722015-04-16 15:59:00 -0600944 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600945 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700946 ((uint32_t *) createInfo.pCode)[1] = 0;
947 ((uint32_t *) createInfo.pCode)[2] = stage;
948 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
949
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800950 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700951 }
952
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600953 if (framework->m_use_spv || err) {
954 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600955 err = VK_SUCCESS;
Tony Barboure2c58df2014-11-25 13:18:32 -0700956
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600957 // Use Reference GLSL to SPV compiler
958 framework->GLSLtoSPV(stage, shader_code, spv);
959 createInfo.pCode = spv.data();
960 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700961 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700962
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800963 init(*m_device, createInfo);
964 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700965}
Tony Barbour82c39522014-12-04 14:33:33 -0700966
Tony Barbour6918cd52015-04-09 12:58:51 -0600967VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700968{
Tony Barboure2c58df2014-11-25 13:18:32 -0700969 m_device = device;
970 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
971 m_vertexBufferCount = 0;
972
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600973 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
974 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600975 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600976 m_ia_state.disableVertexReuse = VK_FALSE;
977 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700978 m_ia_state.primitiveRestartIndex = 0;
979
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600980 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700981 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600982 m_rs_state.depthClipEnable = VK_FALSE;
983 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
984 m_rs_state.programPointSize = VK_FALSE;
985 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
986 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600987 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
988 m_rs_state.cullMode = VK_CULL_MODE_NONE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600989 m_rs_state.frontFace = VK_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700990
Tony Barboure2c58df2014-11-25 13:18:32 -0700991 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600992 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700993 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600994 m_cb_state.alphaToCoverageEnable = VK_FALSE;
995 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -0700996
Tony Barbourf52346d2015-01-16 14:27:35 -0700997 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600998 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
999 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001000 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1001 m_ms_state.samples = 1;
1002 m_ms_state.minSampleShading = 0;
1003 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001004
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001005 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -07001006 m_ds_state.pNext = &m_ms_state,
Tony Barbourd1c35722015-04-16 15:59:00 -06001007 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001008 m_ds_state.depthTestEnable = VK_FALSE;
1009 m_ds_state.depthWriteEnable = VK_FALSE;
1010 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001011 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001012 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1013 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1014 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001015 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001016 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001017 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001018
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001019 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001020 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001021 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001022 att.channelWriteMask = 0xf;
1023 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001024
1025};
1026
Tony Barbour6918cd52015-04-09 12:58:51 -06001027void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001028{
1029 m_shaderObjs.push_back(shader);
1030}
1031
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001032void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001033{
1034 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1035 m_vi_state.attributeCount = count;
1036}
1037
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001038void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001039{
1040 m_vi_state.pVertexBindingDescriptions = vi_binding;
1041 m_vi_state.bindingCount = count;
1042}
1043
Tony Barbour6918cd52015-04-09 12:58:51 -06001044void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001045{
1046 m_vertexBufferObjs.push_back(vertexDataBuffer);
1047 m_vertexBufferBindings.push_back(binding);
1048 m_vertexBufferCount++;
1049}
1050
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001051void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001052{
Tony Barbourf52346d2015-01-16 14:27:35 -07001053 if (binding+1 > m_colorAttachments.size())
1054 {
1055 m_colorAttachments.resize(binding+1);
1056 }
1057 m_colorAttachments[binding] = *att;
1058}
1059
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001060void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001061{
1062 m_ds_state.format = ds_state->format;
1063 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1064 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1065 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001066 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001067 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1068 m_ds_state.back = ds_state->back;
1069 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001070}
1071
Tony Barbour6918cd52015-04-09 12:58:51 -06001072void VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001073{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001074 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001075 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001076
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001077 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001078
1079 for (int i=0; i<m_shaderObjs.size(); i++)
1080 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001081 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001082 shaderCreateInfo->pNext = head_ptr;
1083 head_ptr = shaderCreateInfo;
1084 }
1085
1086 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1087 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001088 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001089 m_vi_state.pNext = head_ptr;
1090 head_ptr = &m_vi_state;
1091 }
1092
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001093 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1094 info.pNext = head_ptr;
1095 info.flags = 0;
1096 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001097
Tony Barbourf52346d2015-01-16 14:27:35 -07001098 m_cb_state.attachmentCount = m_colorAttachments.size();
1099 m_cb_state.pAttachments = &m_colorAttachments[0];
1100
Chia-I Wu2648d092014-12-29 14:24:14 +08001101 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001102}
Chia-I Wu2648d092014-12-29 14:24:14 +08001103
Tony Barbourd1c35722015-04-16 15:59:00 -06001104vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001105{
Tony Barbourd1c35722015-04-16 15:59:00 -06001106 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001107 if (this->mem_refs_.size()) {
1108 mems.reserve(this->mem_refs_.size());
1109 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1110 mems.push_back(this->mem_refs_[i]);
1111 }
1112
1113 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001114}
1115
Tony Barbour6918cd52015-04-09 12:58:51 -06001116void VkMemoryRefManager::AddMemoryRefs(vk_testing::Object &vkObject)
Tony Barboure2c58df2014-11-25 13:18:32 -07001117{
Tony Barbourd1c35722015-04-16 15:59:00 -06001118 const std::vector<VkDeviceMemory> mems = vkObject.memories();
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001119 AddMemoryRefs(mems);
1120}
Tony Barboure2c58df2014-11-25 13:18:32 -07001121
Tony Barbourd1c35722015-04-16 15:59:00 -06001122void VkMemoryRefManager::AddMemoryRefs(vector<VkDeviceMemory> mem)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001123{
1124 for (size_t i = 0; i < mem.size(); i++) {
1125 if (mem[i] != NULL) {
1126 this->mem_refs_.push_back(mem[i]);
1127 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001128 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001129}
Tony Barbour82c39522014-12-04 14:33:33 -07001130
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001131void VkMemoryRefManager::EmitAddMemoryRefs(VkQueue queue)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001132{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001133 vkQueueAddMemReferences(queue, mem_refs_.size(), &mem_refs_[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001134}
Tony Barbour82c39522014-12-04 14:33:33 -07001135
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001136void VkMemoryRefManager::EmitRemoveMemoryRefs(VkQueue queue)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001137{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001138 vkQueueRemoveMemReferences(queue, mem_refs_.size(), &mem_refs_[0]);
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001139}
1140
Tony Barbour6918cd52015-04-09 12:58:51 -06001141VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001142 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001143{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001144 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001145}
Tony Barbour471338d2014-12-10 17:28:39 -07001146
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001147VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001148{
Chia-I Wud28343c2014-12-28 15:12:48 +08001149 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001150}
Tony Barbour471338d2014-12-10 17:28:39 -07001151
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001152VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001153{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001154 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001155 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001156}
1157
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001158VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001159{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001160 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001161 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001162}
1163
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001164VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001165{
1166 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001167 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001168}
1169
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001170VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001171{
Chia-I Wud28343c2014-12-28 15:12:48 +08001172 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001173 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001174}
1175
Tony Barbourd1c35722015-04-16 15:59:00 -06001176void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001177{
Tony Barbourd1c35722015-04-16 15:59:00 -06001178 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001179}
1180
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001181void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001182 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001183{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001184 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001185 const VkFlags output_mask =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001186 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
1187 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1188 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1189 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001190 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001191 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001192
1193 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001194 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001195 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001196 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001197 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001198 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001199 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001200
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001201 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001202 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001203 memory_barrier.outputMask = output_mask;
1204 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001205 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001206 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001207 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001208
Tony Barbourd1c35722015-04-16 15:59:00 -06001209 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001210
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001211 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001212 memory_barrier.image = m_renderTargets[i]->image();
1213 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001214 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001215 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001216
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001217 vkCmdClearColorImage(obj(),
1218 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -06001219 clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001220
1221 mem_ref_mgr.AddMemoryRefs(*m_renderTargets[i]);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001222 }
1223
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001224 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001225 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001226 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001227 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001228 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001229 dsRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001230 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001231 dsRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001232
1233 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001234
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001235 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001236 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001237 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001238 memory_barrier.subresourceRange = dsRange;
1239
Tony Barbourd1c35722015-04-16 15:59:00 -06001240 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001241
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001242 vkCmdClearDepthStencil(obj(),
1243 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001244 depth_clear_color, stencil_clear_color,
1245 1, &dsRange);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001246 mem_ref_mgr.AddMemoryRefs(*depthStencilObj);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001247
1248 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001249 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001250 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001251 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001252 memory_barrier.subresourceRange = dsRange;
Tony Barbourd1c35722015-04-16 15:59:00 -06001253 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001254 }
1255}
1256
Tony Barbour6918cd52015-04-09 12:58:51 -06001257void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001258{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001259 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001260 const VkFlags output_mask =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001261 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
1262 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1263 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1264 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001265 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001266 const VkFlags input_mask =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001267 VK_MEMORY_INPUT_CPU_READ_BIT |
1268 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1269 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1270 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1271 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1272 VK_MEMORY_INPUT_SHADER_READ_BIT |
1273 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1274 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001275 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001276
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001277 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001278 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001279 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001280 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001281 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001282 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001283
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001284 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001285 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001286 memory_barrier.outputMask = output_mask;
1287 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001288 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001289 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001290 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001291
Tony Barbourd1c35722015-04-16 15:59:00 -06001292 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001293
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001294 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001295 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001296 memory_barrier.image = m_renderTargets[i]->image();
1297 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001298 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001299 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001300 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001301}
1302
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001303void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001304{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001305 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001306 renderpass,
1307 framebuffer,
1308 };
1309
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001310 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001311}
1312
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001313void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001314{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001315 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001316}
1317
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001318void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001319{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001320 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001321}
1322
Tony Barbour6918cd52015-04-09 12:58:51 -06001323void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001324{
1325 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001326}
1327
Tony Barbour6918cd52015-04-09 12:58:51 -06001328void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001329{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001330 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001331}
1332
Tony Barbour6918cd52015-04-09 12:58:51 -06001333void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001334{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001335 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001336}
1337
Tony Barbour6918cd52015-04-09 12:58:51 -06001338void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001339{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001340 QueueCommandBuffer(NULL);
1341}
1342
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001343void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001344{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001345 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001346
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001347 mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
1348
Tony Barbour30cc9e82014-12-17 11:53:55 -07001349 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001350 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1351 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001352
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001353 err = vkQueueWaitIdle( m_device->m_queue );
1354 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001355
1356 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001357 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001358
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001359 /*
1360 * Now that processing on this command buffer is complete
1361 * we can remove the memory references.
1362 */
1363 mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001364}
1365
Tony Barbour6918cd52015-04-09 12:58:51 -06001366void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001367{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001368 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001369 mem_ref_mgr.AddMemoryRefs(pipeline);
1370}
1371
Tony Barbour6918cd52015-04-09 12:58:51 -06001372void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001373{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001374 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001375
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001376 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001377 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropd4c1a502015-04-16 13:41:56 -06001378 0, 1, &set_obj, 0, NULL );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001379
1380 // Add descriptor set mem refs to command buffer's list
1381 mem_ref_mgr.AddMemoryRefs(descriptorSet.memories());
1382 mem_ref_mgr.AddMemoryRefs(descriptorSet.mem_ref_mgr.mem_refs());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001383}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001384
Tony Barbour6918cd52015-04-09 12:58:51 -06001385void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001386{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001387 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001388 mem_ref_mgr.AddMemoryRefs(*indexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001389}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001390
Tony Barbourd1c35722015-04-16 15:59:00 -06001391void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001392{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001393 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001394 mem_ref_mgr.AddMemoryRefs(*vertexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001395}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001396
Tony Barbour6918cd52015-04-09 12:58:51 -06001397VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001398{
1399 m_initialized = false;
1400}
Tony Barbour6918cd52015-04-09 12:58:51 -06001401bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001402{
1403 return m_initialized;
1404}
1405
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001406VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001407{
1408 return &m_depthStencilBindInfo;
1409}
1410
Tony Barbour6918cd52015-04-09 12:58:51 -06001411void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001412{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001413 VkImageCreateInfo image_info;
1414 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001415
1416 m_device = device;
1417 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001418 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001419
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001420 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001421 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001422 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001423 image_info.format = m_depth_stencil_fmt;
1424 image_info.extent.width = width;
1425 image_info.extent.height = height;
1426 image_info.extent.depth = 1;
1427 image_info.mipLevels = 1;
1428 image_info.arraySize = 1;
1429 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001430 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001431 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001432 image_info.flags = 0;
1433 init(*m_device, image_info);
1434
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001435 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001436 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001437 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001438 view_info.mipLevel = 0;
1439 view_info.baseArraySlice = 0;
1440 view_info.arraySize = 1;
1441 view_info.flags = 0;
1442 view_info.image = obj();
1443 m_depthStencilView.init(*m_device, view_info);
1444
1445 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001446 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001447}