blob: d67a700884a82823c66f2cbb009b18ed8f14728e [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Jon Ashburn83a64252015-04-15 11:31:12 -060030#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
31
Tony Barbour6918cd52015-04-09 12:58:51 -060032VkRenderFramework::VkRenderFramework() :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060033 m_cmdBuffer( VK_NULL_HANDLE ),
Tony Barbourf77fd652015-04-09 11:07:02 -060034 m_renderPass(VK_NULL_HANDLE),
35 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060036 m_stateRaster( VK_NULL_HANDLE ),
37 m_colorBlend( VK_NULL_HANDLE ),
38 m_stateViewport( VK_NULL_HANDLE ),
39 m_stateDepthStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060040 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070041 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060042 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
43 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070044 m_depth_clear_color( 1.0 ),
45 m_stencil_clear_color( 0 )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060046{
Tony Barbour1c45ce02015-03-27 17:03:18 -060047
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070048 // clear the back buffer to dark grey
49 m_clear_color.color.rawColor[0] = 64;
50 m_clear_color.color.rawColor[1] = 64;
51 m_clear_color.color.rawColor[2] = 64;
52 m_clear_color.color.rawColor[3] = 0;
53 m_clear_color.useRawValue = true;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060054}
55
Tony Barbour6918cd52015-04-09 12:58:51 -060056VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060057{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060058
59}
60
Tony Barbour6918cd52015-04-09 12:58:51 -060061void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060062{
Tony Barbour3fdff9e2015-04-23 12:55:36 -060063 const std::vector<const char *> layers;
64 InitFramework(layers);
65}
66
67void VkRenderFramework::InitFramework(const std::vector<const char *> &layers)
68{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060069 VkResult err;
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060070 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060071 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060072 instInfo.pNext = NULL;
73 instInfo.pAppInfo = &app_info;
74 instInfo.pAllocCb = NULL;
75 instInfo.extensionCount = 0;
76 instInfo.ppEnabledExtensionNames = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060077 err = vkCreateInstance(&instInfo, &this->inst);
78 ASSERT_VK_SUCCESS(err);
Jon Ashburn83a64252015-04-15 11:31:12 -060079 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
80 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
81 ASSERT_VK_SUCCESS(err);
82 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060083 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070084 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060085
Tony Barbour3fdff9e2015-04-23 12:55:36 -060086 m_device = new VkDeviceObj(0, objs[0], layers);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060087 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -060088
Tony Barbour6918cd52015-04-09 12:58:51 -060089 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060090}
91
Tony Barbour6918cd52015-04-09 12:58:51 -060092void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060093{
Mike Stroyanb050c682015-04-17 12:36:38 -060094 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
95 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
96 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
97 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
98 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
99 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600100
101 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600102 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600103 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600104 while (!m_renderTargets.empty()) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600105 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
106 vkQueueBindObjectMemory(m_device->m_queue, VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image(), 0, VK_NULL_HANDLE, 0);
107 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
108 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600109 m_renderTargets.pop_back();
110 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600111
Tony Barbour1c45ce02015-03-27 17:03:18 -0600112 delete m_depthStencil;
113
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600114 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800115 delete m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600116 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600117}
118
Tony Barbour6918cd52015-04-09 12:58:51 -0600119void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600120{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600121 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600122
Tony Barbourd1c35722015-04-16 15:59:00 -0600123 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600124
125 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600126 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600127 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700128 raster.pointSize = 1.0;
129
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600130 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
131 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600132
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600133 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600134 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
135 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
136 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600137
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600138 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600139 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600140 depthStencil.minDepth = 0.f;
141 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700142 depthStencil.stencilFrontRef = 0;
143 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600144
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600145 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
146 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600147
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600148 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600149
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600150 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700151 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
152
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600153 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
154 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600155}
156
Tony Barbour6918cd52015-04-09 12:58:51 -0600157void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600158{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600159 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600160
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600161 VkViewport viewport;
162 VkRect scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600163
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600164 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600165 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700166 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700167 viewport.originX = 0;
168 viewport.originY = 0;
169 viewport.width = 1.f * width;
170 viewport.height = 1.f * height;
171 viewport.minDepth = 0.f;
172 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700173 scissor.extent.width = width;
174 scissor.extent.height = height;
175 scissor.offset.x = 0;
176 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700177 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700178 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700179
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600180 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
181 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600182 m_width = width;
183 m_height = height;
184}
185
Tony Barbour6918cd52015-04-09 12:58:51 -0600186void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600187{
188 InitViewport(m_width, m_height);
189}
Tony Barbour6918cd52015-04-09 12:58:51 -0600190void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600191{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700192 InitRenderTarget(1);
193}
194
Tony Barbour6918cd52015-04-09 12:58:51 -0600195void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700196{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600197 InitRenderTarget(targets, NULL);
198}
199
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600200void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600201{
202 InitRenderTarget(1, dsBinding);
203}
204
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600205void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600206{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600207 std::vector<VkAttachmentLoadOp> load_ops;
208 std::vector<VkAttachmentStoreOp> store_ops;
209 std::vector<VkClearColor> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600210
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600211 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800212
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700213 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600214 VkImageObj *img = new VkImageObj(m_device);
Chia-I Wuf50ee212014-12-29 14:31:52 +0800215 img->init(m_width, m_height, m_render_target_fmt,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600216 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700217 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600218 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700219 m_renderTargets.push_back(img);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600220 load_ops.push_back(VK_ATTACHMENT_LOAD_OP_LOAD);
221 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600222 clear_colors.push_back(m_clear_color);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600223// m_mem_ref_mgr.AddMemoryRefs(*img);
Chia-I Wuecebf752014-12-05 10:45:15 +0800224 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700225 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600226 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600227 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600228 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700229 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600230 fb_info.pColorAttachments = m_colorBindings;
231 fb_info.pDepthStencilAttachment = dsBinding;
232 fb_info.sampleCount = 1;
233 fb_info.width = (uint32_t)m_width;
234 fb_info.height = (uint32_t)m_height;
235 fb_info.layers = 1;
236
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600237 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600238
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600239 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600240 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700241 rp_info.renderArea.extent.width = m_width;
242 rp_info.renderArea.extent.height = m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600243
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700244 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600245 rp_info.pColorFormats = &m_render_target_fmt;
246 rp_info.pColorLayouts = &m_colorBindings[0].layout;
247 rp_info.pColorLoadOps = &load_ops[0];
248 rp_info.pColorStoreOps = &store_ops[0];
249 rp_info.pColorLoadClearValues = &clear_colors[0];
250 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600251 if (dsBinding) {
252 rp_info.depthStencilLayout = dsBinding->layout;
253 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600254 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600255 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600256 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
257 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600258 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600259 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
260 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600261}
262
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600263
264
Tony Barbourd1c35722015-04-16 15:59:00 -0600265VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600266 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800267{
268 init();
269
270 props = gpu().properties();
271 queue_props = &gpu().queue_properties()[0];
272}
273
Tony Barbour3fdff9e2015-04-23 12:55:36 -0600274VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj, const std::vector<const char *> &layers) :
275 vk_testing::Device(obj), id(id)
276{
277 init(layers);
278
279 props = gpu().properties();
280 queue_props = &gpu().queue_properties()[0];
281}
282
Tony Barbour6918cd52015-04-09 12:58:51 -0600283void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800284{
285 ASSERT_NE(true, graphics_queues().empty());
286 m_queue = graphics_queues()[0]->obj();
287}
288
Tony Barbour6918cd52015-04-09 12:58:51 -0600289VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700290{
291 m_device = device;
292 m_nextSlot = 0;
293
294}
295
Tony Barbour6918cd52015-04-09 12:58:51 -0600296VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700297{
Chia-I Wu11078b02015-01-04 16:27:24 +0800298 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700299}
Tony Barbour82c39522014-12-04 14:33:33 -0700300
Tony Barbour6918cd52015-04-09 12:58:51 -0600301int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700302{
Chia-I Wu11078b02015-01-04 16:27:24 +0800303 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600304 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600305 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800306 tc.count = 1;
307 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700308
Chia-I Wu11078b02015-01-04 16:27:24 +0800309 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700310}
Tony Barbour82c39522014-12-04 14:33:33 -0700311
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600312int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700313{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600314 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800315 tc.type = type;
316 tc.count = 1;
317 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700318
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600319 m_updateBuffers.push_back(vk_testing::DescriptorSet::update(type,
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600320 m_nextSlot, 0, 1, &constantBuffer.m_bufferViewInfo));
321
322 // Track mem references for this descriptor set object
323 mem_ref_mgr.AddMemoryRefs(constantBuffer);
Chia-I Wu11078b02015-01-04 16:27:24 +0800324
325 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700326}
Tony Barbour82c39522014-12-04 14:33:33 -0700327
Tony Barbour6918cd52015-04-09 12:58:51 -0600328int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700329{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600330 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600331 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800332 tc.count = 1;
333 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700334
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600335 VkSamplerImageViewInfo tmp = {};
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600336 tmp.sampler = sampler->obj();
Chia-I Wu11078b02015-01-04 16:27:24 +0800337 tmp.pImageView = &texture->m_textureViewInfo;
338 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700339
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600340 m_updateSamplerTextures.push_back(vk_testing::DescriptorSet::update(m_nextSlot, 0, 1,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600341 (const VkSamplerImageViewInfo *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700342
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600343 // Track mem references for the texture referenced here
344 mem_ref_mgr.AddMemoryRefs(*texture);
345
Chia-I Wu11078b02015-01-04 16:27:24 +0800346 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700347}
Chia-I Wu11078b02015-01-04 16:27:24 +0800348
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500349VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700350{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500351 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700352}
353
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600354VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700355{
Chia-I Wu11078b02015-01-04 16:27:24 +0800356 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700357}
Tony Barboure2c58df2014-11-25 13:18:32 -0700358
Tony Barbour6918cd52015-04-09 12:58:51 -0600359void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700360{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600361 // create VkDescriptorPool
362 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600363 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800364 pool.count = m_type_counts.size();
365 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600366 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700367
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600368 // create VkDescriptorSetLayout
369 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800370 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800371 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800372 bindings[i].descriptorType = m_type_counts[i].type;
373 bindings[i].count = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600374 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800375 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700376 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800377
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600378 // create VkDescriptorSetLayout
379 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600380 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800381 layout.count = bindings.size();
382 layout.pBinding = &bindings[0];
383
Chia-I Wu41126e52015-03-26 15:27:55 +0800384 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600385 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800386 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500387
388 // create VkPipelineLayout
389 VkPipelineLayoutCreateInfo pipeline_layout = {};
390 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
391 pipeline_layout.descriptorSetCount = layouts.size();
392 pipeline_layout.pSetLayouts = NULL;
393
394 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700395
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600396 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500397 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800398
Chia-I Wu41126e52015-03-26 15:27:55 +0800399 // build the update array
400 vector<const void *> update_array;
Chia-I Wu11078b02015-01-04 16:27:24 +0800401
Chia-I Wu41126e52015-03-26 15:27:55 +0800402 for (int i = 0; i < m_updateBuffers.size(); i++) {
403 update_array.push_back(&m_updateBuffers[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800404 }
405 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
406 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
Chia-I Wu41126e52015-03-26 15:27:55 +0800407 update_array.push_back(&m_updateSamplerTextures[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800408 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800409
410 // do the updates
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600411 m_device->begin_descriptor_pool_update(VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu11078b02015-01-04 16:27:24 +0800412 clear_sets(*m_set);
Chia-I Wu41126e52015-03-26 15:27:55 +0800413 m_set->update(update_array);
Chia-I Wu985ba162015-03-26 13:14:16 +0800414 m_device->end_descriptor_pool_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700415}
Tony Barboure2c58df2014-11-25 13:18:32 -0700416
Tony Barbour6918cd52015-04-09 12:58:51 -0600417VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800418{
419 m_device = dev;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600420 m_imageInfo.view = VK_NULL_HANDLE;
421 m_imageInfo.layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800422}
423
Tony Barbour6918cd52015-04-09 12:58:51 -0600424void VkImageObj::ImageMemoryBarrier(
425 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600426 VkImageAspect aspect,
427 VkFlags output_mask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600428 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
429 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
430 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
431 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
432 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600433 VkFlags input_mask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600434 VK_MEMORY_INPUT_CPU_READ_BIT |
435 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
436 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
437 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
438 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
439 VK_MEMORY_INPUT_SHADER_READ_BIT |
440 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
441 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
442 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600443 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600444{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600445 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
446 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600447 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
448 subresourceRange);
449
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600450 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600451
Tony Barbourd1c35722015-04-16 15:59:00 -0600452 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600453
454 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600455 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 -0600456}
457
Tony Barbour6918cd52015-04-09 12:58:51 -0600458void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600459 VkImageAspect aspect,
460 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600461{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600462 VkFlags output_mask, input_mask;
463 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600464 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
465 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
466 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
467 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600468 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600469 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600470 VK_MEMORY_INPUT_CPU_READ_BIT |
471 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
472 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
473 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
474 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
475 VK_MEMORY_INPUT_SHADER_READ_BIT |
476 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
477 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600478 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600479
480 if (image_layout == m_imageInfo.layout) {
481 return;
482 }
483
484 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600485 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600486 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
487 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600488 break;
489
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600490 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600491 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
492 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600493 break;
494
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600495 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600496 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
497 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600498 break;
499
Tony Barbourf20f87b2015-04-22 09:02:32 -0600500 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600501 default:
502 output_mask = all_cache_outputs;
503 input_mask = all_cache_inputs;
504 break;
505 }
506
507 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
508 m_imageInfo.layout = image_layout;
509}
510
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600511void VkImageObj::SetLayout(VkImageAspect aspect,
512 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600513{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600514 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600515 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600516
517 /* Build command buffer to set image layout in the driver */
518 err = cmd_buf.BeginCommandBuffer();
519 assert(!err);
520
521 SetLayout(&cmd_buf, aspect, image_layout);
522
523 err = cmd_buf.EndCommandBuffer();
524 assert(!err);
525
526 cmd_buf.QueueCommandBuffer();
527}
528
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600529bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600530{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600531 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600532 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600533 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600534
Tony Barbour579f7802015-04-03 15:11:43 -0600535 return true;
536}
537
Tony Barbour6918cd52015-04-09 12:58:51 -0600538void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600539 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600540 VkImageTiling requested_tiling,
541 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800542{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600543 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600544 VkFormatProperties image_fmt;
545 VkImageTiling tiling;
546 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600547 size_t size;
548
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800549 mipCount = 0;
550
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600551 uint32_t _w = w;
552 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800553 while( ( _w > 0 ) || ( _h > 0 ) )
554 {
555 _w >>= 1;
556 _h >>= 1;
557 mipCount++;
558 }
559
Tony Barbour579f7802015-04-03 15:11:43 -0600560 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600561 err = vkGetFormatInfo(m_device->obj(), fmt,
Tony Barbourd1c35722015-04-16 15:59:00 -0600562 VK_FORMAT_INFO_TYPE_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600563 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600564 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600565
Tony Barbourd1c35722015-04-16 15:59:00 -0600566 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600567 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600568 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600569 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600570 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600571 } else {
572 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
573 }
574 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600575 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600576 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600577 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600578 } else {
579 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
580 }
581
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600582 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600583 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800584 imageCreateInfo.format = fmt;
585 imageCreateInfo.extent.width = w;
586 imageCreateInfo.extent.height = h;
587 imageCreateInfo.mipLevels = mipCount;
588 imageCreateInfo.tiling = tiling;
589
590 imageCreateInfo.usage = usage;
591
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600592 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800593
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600594 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600595 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600596 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600597 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600598 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800599}
600
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600601VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800602{
603 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600604 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800605}
606
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600607VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800608{
609 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600610 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800611}
612
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600613VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600614{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600615 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600616 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600617 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600618
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600619 /* Build command buffer to copy staging texture to usable texture */
620 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600621 assert(!err);
622
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600623 /* TODO: Can we determine image aspect from image object? */
624 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600625 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600626
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600627 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600628 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600629
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600630 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600631 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600632 copy_region.srcSubresource.arraySlice = 0;
633 copy_region.srcSubresource.mipLevel = 0;
634 copy_region.srcOffset.x = 0;
635 copy_region.srcOffset.y = 0;
636 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600637 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600638 copy_region.destSubresource.arraySlice = 0;
639 copy_region.destSubresource.mipLevel = 0;
640 copy_region.destOffset.x = 0;
641 copy_region.destOffset.y = 0;
642 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600643 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600644
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600645 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600646 src_image.obj(), src_image.layout(),
647 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600648 1, &copy_region);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600649 cmd_buf.mem_ref_mgr.AddMemoryRefs(src_image);
650 cmd_buf.mem_ref_mgr.AddMemoryRefs(*this);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600651
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600652 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600653
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600654 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600655
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600656 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600657 assert(!err);
658
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600659 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600660
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600661 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600662}
663
Tony Barbour6918cd52015-04-09 12:58:51 -0600664VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
665 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700666{
667 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600668 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600669 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600670 void *data;
671 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600672 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600673 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600674
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600675 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600676 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600677
678 if (colors == NULL)
679 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700680
681 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
682
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600683 m_textureViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700684
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600685 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600686 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600687 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600688 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600689 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600690 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600691 view.channels.r = VK_CHANNEL_SWIZZLE_R;
692 view.channels.g = VK_CHANNEL_SWIZZLE_G;
693 view.channels.b = VK_CHANNEL_SWIZZLE_B;
694 view.channels.a = VK_CHANNEL_SWIZZLE_A;
695 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600696 view.subresourceRange.baseMipLevel = 0;
697 view.subresourceRange.mipLevels = 1;
698 view.subresourceRange.baseArraySlice = 0;
699 view.subresourceRange.arraySize = 1;
700 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700701
Tony Barboure2c58df2014-11-25 13:18:32 -0700702 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600703 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700704
705 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800706 view.image = obj();
707 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600708 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700709
Tony Barbour5dc515d2015-04-01 17:47:06 -0600710 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700711
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800712 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700713 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800714 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600715 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700716 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600717 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600718 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700719}
Tony Barbour82c39522014-12-04 14:33:33 -0700720
Tony Barbour6918cd52015-04-09 12:58:51 -0600721VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700722{
Tony Barboure2c58df2014-11-25 13:18:32 -0700723 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700724
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600725 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800726 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600727 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
728 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
729 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600730 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600731 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
732 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
733 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800734 samplerCreateInfo.mipLodBias = 0.0;
735 samplerCreateInfo.maxAnisotropy = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600736 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800737 samplerCreateInfo.minLod = 0.0;
738 samplerCreateInfo.maxLod = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600739 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700740
Chia-I Wue9864b52014-12-28 16:32:24 +0800741 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700742}
Tony Barboure2c58df2014-11-25 13:18:32 -0700743
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700744/*
745 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
746 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600747VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700748{
749 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700750 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700751
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800752 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700753}
754
Tony Barbour6918cd52015-04-09 12:58:51 -0600755VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600756{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600757 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600758 if (m_commandBuffer) {
759 delete m_commandBuffer;
760 }
761}
762
Tony Barbour6918cd52015-04-09 12:58:51 -0600763VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700764{
Tony Barboure2c58df2014-11-25 13:18:32 -0700765 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800766 m_commandBuffer = 0;
767
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800768 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700769 m_numVertices = constantCount;
770 m_stride = constantSize;
771
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600772 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800773 const size_t allocationSize = constantCount * constantSize;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600774 init(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700775
Chia-I Wua07fee62014-12-28 15:26:08 +0800776 void *pData = map();
777 memcpy(pData, data, allocationSize);
778 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700779
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800780 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600781 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600782 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800783 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600784 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800785 view_info.offset = 0;
786 view_info.range = allocationSize;
787 m_bufferView.init(*m_device, view_info);
788
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600789 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800790 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700791}
Tony Barbour82c39522014-12-04 14:33:33 -0700792
Tony Barbourd1c35722015-04-16 15:59:00 -0600793void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700794{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600795 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700796}
797
798
Tony Barbour6918cd52015-04-09 12:58:51 -0600799void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600800 VkFlags outputMask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600801 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
802 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
803 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
804 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
805 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600806 VkFlags inputMask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600807 VK_MEMORY_INPUT_CPU_READ_BIT |
808 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
809 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
810 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
811 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
812 VK_MEMORY_INPUT_SHADER_READ_BIT |
813 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
814 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
815 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700816{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600817 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700818
Tony Barbour38422802014-12-10 14:36:31 -0700819 if (!m_commandBuffer)
820 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600821 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700822
Tony Barbour6918cd52015-04-09 12:58:51 -0600823 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700824 }
825 else
826 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800827 m_device->wait(m_fence);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600828 m_commandBuffer->mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour38422802014-12-10 14:36:31 -0700829 }
830
Tony Barboure2c58df2014-11-25 13:18:32 -0700831 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600832 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600833 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600834 cmd_buf_info.pNext = NULL;
835 cmd_buf_info.flags = 0;
836
Jon Ashburnc4164b12014-12-31 17:10:47 -0700837 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600838 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700839
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600840 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000841 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600842 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700843
Tony Barbourd1c35722015-04-16 15:59:00 -0600844 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000845
846 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600847 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700848
849 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700850 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600851 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700852
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600853 /*
854 * Tell driver about memory references made in this command buffer
855 * Note: Since this command buffer only has a PipelineBarrier
856 * command there really aren't any memory refs to worry about.
857 */
858 m_commandBuffer->mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
Tony Barboure2c58df2014-11-25 13:18:32 -0700859
860 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600861 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700862 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600863 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
864 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700865}
866
Tony Barbour6918cd52015-04-09 12:58:51 -0600867VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
868 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700869{
870
871}
872
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600873void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700874{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600875 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700876
877 m_numVertices = numIndexes;
878 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700879 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600880 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700881 m_stride = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -0600882 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700883 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600884 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700885 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600886 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700887 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600888 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700889 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600890 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700891 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800892 default:
893 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600894 m_stride = 2;
895 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800896 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700897 }
898
Chia-I Wua07fee62014-12-28 15:26:08 +0800899 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600900 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
901 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700902
Chia-I Wua07fee62014-12-28 15:26:08 +0800903 void *pData = map();
904 memcpy(pData, data, allocationSize);
905 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700906
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800907 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600908 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600909 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800910 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600911 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700912 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800913 view_info.offset = 0;
914 view_info.range = allocationSize;
915 m_bufferView.init(*m_device, view_info);
916
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600917 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800918 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700919}
920
Tony Barbourd1c35722015-04-16 15:59:00 -0600921void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700922{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600923 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700924}
Tony Barboure2c58df2014-11-25 13:18:32 -0700925
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600926VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -0700927{
928 return m_indexType;
929}
930
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600931VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700932{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600933 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600934 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700935 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800936 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700937 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600938 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700939
Tony Barboure2c58df2014-11-25 13:18:32 -0700940 return stageInfo;
941}
942
Tony Barbourd1c35722015-04-16 15:59:00 -0600943VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -0700944{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600945 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600946 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600947 VkShaderCreateInfo createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700948 size_t shader_len;
949
950 m_stage = stage;
951 m_device = device;
952
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600953 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700954 createInfo.pNext = NULL;
955
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600956 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700957
958 shader_len = strlen(shader_code);
959 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
960 createInfo.pCode = malloc(createInfo.codeSize);
961 createInfo.flags = 0;
962
Tony Barbourd1c35722015-04-16 15:59:00 -0600963 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600964 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700965 ((uint32_t *) createInfo.pCode)[1] = 0;
966 ((uint32_t *) createInfo.pCode)[2] = stage;
967 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
968
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800969 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700970 }
971
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600972 if (framework->m_use_spv || err) {
973 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600974 err = VK_SUCCESS;
Tony Barboure2c58df2014-11-25 13:18:32 -0700975
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600976 // Use Reference GLSL to SPV compiler
977 framework->GLSLtoSPV(stage, shader_code, spv);
978 createInfo.pCode = spv.data();
979 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700980 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700981
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800982 init(*m_device, createInfo);
983 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700984}
Tony Barbour82c39522014-12-04 14:33:33 -0700985
Tony Barbour6918cd52015-04-09 12:58:51 -0600986VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700987{
Tony Barboure2c58df2014-11-25 13:18:32 -0700988 m_device = device;
989 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
990 m_vertexBufferCount = 0;
991
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600992 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
993 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600994 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600995 m_ia_state.disableVertexReuse = VK_FALSE;
996 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700997 m_ia_state.primitiveRestartIndex = 0;
998
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600999 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001000 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001001 m_rs_state.depthClipEnable = VK_FALSE;
1002 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
1003 m_rs_state.programPointSize = VK_FALSE;
1004 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1005 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001006 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
1007 m_rs_state.cullMode = VK_CULL_MODE_NONE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001008 m_rs_state.frontFace = VK_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001009
Tony Barboure2c58df2014-11-25 13:18:32 -07001010 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001011 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001012 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001013 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1014 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001015
Tony Barbourf52346d2015-01-16 14:27:35 -07001016 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001017 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1018 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001019 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1020 m_ms_state.samples = 1;
1021 m_ms_state.minSampleShading = 0;
1022 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001023
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001024 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -07001025 m_ds_state.pNext = &m_ms_state,
Tony Barbourd1c35722015-04-16 15:59:00 -06001026 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001027 m_ds_state.depthTestEnable = VK_FALSE;
1028 m_ds_state.depthWriteEnable = VK_FALSE;
1029 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001030 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001031 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1032 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1033 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001034 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001035 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001036 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001037
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001038 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001039 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001040 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001041 att.channelWriteMask = 0xf;
1042 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001043
1044};
1045
Tony Barbour6918cd52015-04-09 12:58:51 -06001046void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001047{
1048 m_shaderObjs.push_back(shader);
1049}
1050
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001051void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001052{
1053 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1054 m_vi_state.attributeCount = count;
1055}
1056
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001057void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001058{
1059 m_vi_state.pVertexBindingDescriptions = vi_binding;
1060 m_vi_state.bindingCount = count;
1061}
1062
Tony Barbour6918cd52015-04-09 12:58:51 -06001063void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001064{
1065 m_vertexBufferObjs.push_back(vertexDataBuffer);
1066 m_vertexBufferBindings.push_back(binding);
1067 m_vertexBufferCount++;
1068}
1069
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001070void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001071{
Tony Barbourf52346d2015-01-16 14:27:35 -07001072 if (binding+1 > m_colorAttachments.size())
1073 {
1074 m_colorAttachments.resize(binding+1);
1075 }
1076 m_colorAttachments[binding] = *att;
1077}
1078
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001079void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001080{
1081 m_ds_state.format = ds_state->format;
1082 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1083 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1084 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001085 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001086 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1087 m_ds_state.back = ds_state->back;
1088 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001089}
1090
Tony Barbour6918cd52015-04-09 12:58:51 -06001091void VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001092{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001093 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001094 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001095
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001096 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001097
1098 for (int i=0; i<m_shaderObjs.size(); i++)
1099 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001100 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001101 shaderCreateInfo->pNext = head_ptr;
1102 head_ptr = shaderCreateInfo;
1103 }
1104
1105 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1106 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001107 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001108 m_vi_state.pNext = head_ptr;
1109 head_ptr = &m_vi_state;
1110 }
1111
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001112 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1113 info.pNext = head_ptr;
1114 info.flags = 0;
1115 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001116
Tony Barbourf52346d2015-01-16 14:27:35 -07001117 m_cb_state.attachmentCount = m_colorAttachments.size();
1118 m_cb_state.pAttachments = &m_colorAttachments[0];
1119
Chia-I Wu2648d092014-12-29 14:24:14 +08001120 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001121}
Chia-I Wu2648d092014-12-29 14:24:14 +08001122
Tony Barbourd1c35722015-04-16 15:59:00 -06001123vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001124{
Tony Barbourd1c35722015-04-16 15:59:00 -06001125 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001126 if (this->mem_refs_.size()) {
1127 mems.reserve(this->mem_refs_.size());
1128 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1129 mems.push_back(this->mem_refs_[i]);
1130 }
1131
1132 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001133}
1134
Tony Barbour6918cd52015-04-09 12:58:51 -06001135void VkMemoryRefManager::AddMemoryRefs(vk_testing::Object &vkObject)
Tony Barboure2c58df2014-11-25 13:18:32 -07001136{
Tony Barbourd1c35722015-04-16 15:59:00 -06001137 const std::vector<VkDeviceMemory> mems = vkObject.memories();
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001138 AddMemoryRefs(mems);
1139}
Tony Barboure2c58df2014-11-25 13:18:32 -07001140
Tony Barbourd1c35722015-04-16 15:59:00 -06001141void VkMemoryRefManager::AddMemoryRefs(vector<VkDeviceMemory> mem)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001142{
1143 for (size_t i = 0; i < mem.size(); i++) {
1144 if (mem[i] != NULL) {
1145 this->mem_refs_.push_back(mem[i]);
1146 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001147 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001148}
Tony Barbour82c39522014-12-04 14:33:33 -07001149
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001150void VkMemoryRefManager::EmitAddMemoryRefs(VkQueue queue)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001151{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001152 vkQueueAddMemReferences(queue, mem_refs_.size(), &mem_refs_[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001153}
Tony Barbour82c39522014-12-04 14:33:33 -07001154
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001155void VkMemoryRefManager::EmitRemoveMemoryRefs(VkQueue queue)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001156{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001157 vkQueueRemoveMemReferences(queue, mem_refs_.size(), &mem_refs_[0]);
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001158}
1159
Tony Barbour6918cd52015-04-09 12:58:51 -06001160VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001161 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001162{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001163 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001164}
Tony Barbour471338d2014-12-10 17:28:39 -07001165
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001166VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001167{
Chia-I Wud28343c2014-12-28 15:12:48 +08001168 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001169}
Tony Barbour471338d2014-12-10 17:28:39 -07001170
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001171VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001172{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001173 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001174 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001175}
1176
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001177VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001178{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001179 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001180 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001181}
1182
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001183VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001184{
1185 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001186 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001187}
1188
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001189VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001190{
Chia-I Wud28343c2014-12-28 15:12:48 +08001191 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001192 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001193}
1194
Tony Barbourd1c35722015-04-16 15:59:00 -06001195void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001196{
Tony Barbourd1c35722015-04-16 15:59:00 -06001197 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001198}
1199
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001200void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001201 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001202{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001203 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001204 const VkFlags output_mask =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001205 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
1206 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1207 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1208 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001209 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001210 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001211
1212 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001213 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001214 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001215 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001216 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001217 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001218 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001219
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001220 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001221 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001222 memory_barrier.outputMask = output_mask;
1223 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001224 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001225 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001226 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001227
Tony Barbourd1c35722015-04-16 15:59:00 -06001228 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001229
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001230 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001231 memory_barrier.image = m_renderTargets[i]->image();
1232 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001233 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001234 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001235
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001236 vkCmdClearColorImage(obj(),
1237 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -06001238 clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001239
1240 mem_ref_mgr.AddMemoryRefs(*m_renderTargets[i]);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001241 }
1242
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001243 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001244 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001245 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001246 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001247 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001248 dsRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001249 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001250 dsRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001251
1252 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001253
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001254 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001255 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001256 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001257 memory_barrier.subresourceRange = dsRange;
1258
Tony Barbourd1c35722015-04-16 15:59:00 -06001259 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001260
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001261 vkCmdClearDepthStencil(obj(),
1262 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001263 depth_clear_color, stencil_clear_color,
1264 1, &dsRange);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001265 mem_ref_mgr.AddMemoryRefs(*depthStencilObj);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001266
1267 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001268 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001269 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001270 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001271 memory_barrier.subresourceRange = dsRange;
Tony Barbourd1c35722015-04-16 15:59:00 -06001272 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001273 }
1274}
1275
Tony Barbour6918cd52015-04-09 12:58:51 -06001276void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001277{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001278 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001279 const VkFlags output_mask =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001280 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
1281 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1282 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1283 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001284 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001285 const VkFlags input_mask =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001286 VK_MEMORY_INPUT_CPU_READ_BIT |
1287 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1288 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1289 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1290 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1291 VK_MEMORY_INPUT_SHADER_READ_BIT |
1292 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1293 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001294 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001295
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001296 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001297 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001298 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001299 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001300 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001301 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001302
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001303 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001304 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001305 memory_barrier.outputMask = output_mask;
1306 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001307 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001308 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001309 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001310
Tony Barbourd1c35722015-04-16 15:59:00 -06001311 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001312
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001313 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001314 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001315 memory_barrier.image = m_renderTargets[i]->image();
1316 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001317 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001318 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001319 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001320}
1321
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001322void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001323{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001324 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001325 renderpass,
1326 framebuffer,
1327 };
1328
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001329 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001330}
1331
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001332void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001333{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001334 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001335}
1336
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001337void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001338{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001339 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001340}
1341
Tony Barbour6918cd52015-04-09 12:58:51 -06001342void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001343{
1344 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001345}
1346
Tony Barbour6918cd52015-04-09 12:58:51 -06001347void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001348{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001349 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001350}
1351
Tony Barbour6918cd52015-04-09 12:58:51 -06001352void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001353{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001354 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001355}
1356
Tony Barbour6918cd52015-04-09 12:58:51 -06001357void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001358{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001359 QueueCommandBuffer(NULL);
1360}
1361
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001362void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001363{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001364 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001365
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001366 mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
1367
Tony Barbour30cc9e82014-12-17 11:53:55 -07001368 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001369 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1370 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001371
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001372 err = vkQueueWaitIdle( m_device->m_queue );
1373 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001374
1375 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001376 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001377
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001378 /*
1379 * Now that processing on this command buffer is complete
1380 * we can remove the memory references.
1381 */
1382 mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001383}
1384
Tony Barbour6918cd52015-04-09 12:58:51 -06001385void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001386{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001387 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001388 mem_ref_mgr.AddMemoryRefs(pipeline);
1389}
1390
Tony Barbour6918cd52015-04-09 12:58:51 -06001391void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001392{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001393 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001394
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001395 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001396 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropd4c1a502015-04-16 13:41:56 -06001397 0, 1, &set_obj, 0, NULL );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001398
1399 // Add descriptor set mem refs to command buffer's list
1400 mem_ref_mgr.AddMemoryRefs(descriptorSet.memories());
1401 mem_ref_mgr.AddMemoryRefs(descriptorSet.mem_ref_mgr.mem_refs());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001402}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001403
Tony Barbour6918cd52015-04-09 12:58:51 -06001404void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001405{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001406 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001407 mem_ref_mgr.AddMemoryRefs(*indexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001408}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001409
Tony Barbourd1c35722015-04-16 15:59:00 -06001410void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001411{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001412 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001413 mem_ref_mgr.AddMemoryRefs(*vertexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001414}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001415
Tony Barbour6918cd52015-04-09 12:58:51 -06001416VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001417{
1418 m_initialized = false;
1419}
Tony Barbour6918cd52015-04-09 12:58:51 -06001420bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001421{
1422 return m_initialized;
1423}
1424
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001425VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001426{
1427 return &m_depthStencilBindInfo;
1428}
1429
Tony Barbour6918cd52015-04-09 12:58:51 -06001430void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001431{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001432 VkImageCreateInfo image_info;
1433 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001434
1435 m_device = device;
1436 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001437 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001438
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001439 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001440 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001441 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001442 image_info.format = m_depth_stencil_fmt;
1443 image_info.extent.width = width;
1444 image_info.extent.height = height;
1445 image_info.extent.depth = 1;
1446 image_info.mipLevels = 1;
1447 image_info.arraySize = 1;
1448 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001449 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001450 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001451 image_info.flags = 0;
1452 init(*m_device, image_info);
1453
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001454 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001455 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001456 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001457 view_info.mipLevel = 0;
1458 view_info.baseArraySlice = 0;
1459 view_info.arraySize = 1;
1460 view_info.flags = 0;
1461 view_info.image = obj();
1462 m_depthStencilView.init(*m_device, view_info);
1463
1464 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001465 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001466}