blob: 864f37d404bd1b3062d728dda855b4b53b249e41 [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Jon Ashburn83a64252015-04-15 11:31:12 -060030#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
31
Tony Barbour6918cd52015-04-09 12:58:51 -060032VkRenderFramework::VkRenderFramework() :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060033 m_cmdBuffer( VK_NULL_HANDLE ),
Tony Barbourf77fd652015-04-09 11:07:02 -060034 m_renderPass(VK_NULL_HANDLE),
35 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060036 m_stateRaster( VK_NULL_HANDLE ),
37 m_colorBlend( VK_NULL_HANDLE ),
38 m_stateViewport( VK_NULL_HANDLE ),
39 m_stateDepthStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060040 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070041 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060042 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
43 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070044 m_depth_clear_color( 1.0 ),
45 m_stencil_clear_color( 0 )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060046{
Tony Barbour1c45ce02015-03-27 17:03:18 -060047
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070048 // clear the back buffer to dark grey
49 m_clear_color.color.rawColor[0] = 64;
50 m_clear_color.color.rawColor[1] = 64;
51 m_clear_color.color.rawColor[2] = 64;
52 m_clear_color.color.rawColor[3] = 0;
53 m_clear_color.useRawValue = true;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060054}
55
Tony Barbour6918cd52015-04-09 12:58:51 -060056VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060057{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060058
59}
60
Tony Barbour6918cd52015-04-09 12:58:51 -060061void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060062{
Tony Barbour9b9fe782015-04-23 15:28:27 -060063 const std::vector<const char *> extensions;
64 InitFramework(extensions);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060065}
66
Tony Barbour9b9fe782015-04-23 15:28:27 -060067void VkRenderFramework::InitFramework(const std::vector<const char *> &extensions)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060068{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060069 VkResult err;
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060070 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060071 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060072 instInfo.pNext = NULL;
73 instInfo.pAppInfo = &app_info;
74 instInfo.pAllocCb = NULL;
Tony Barbour9b9fe782015-04-23 15:28:27 -060075 instInfo.extensionCount = extensions.size();
76 instInfo.ppEnabledExtensionNames = (extensions.size()) ? &extensions[0] : NULL;;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060077 err = vkCreateInstance(&instInfo, &this->inst);
78 ASSERT_VK_SUCCESS(err);
Jon Ashburn83a64252015-04-15 11:31:12 -060079 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
80 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
81 ASSERT_VK_SUCCESS(err);
82 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060083 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070084 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060085
Tony Barbour9b9fe782015-04-23 15:28:27 -060086 m_device = new VkDeviceObj(0, objs[0]);
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 Barbour6918cd52015-04-09 12:58:51 -0600274void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800275{
276 ASSERT_NE(true, graphics_queues().empty());
277 m_queue = graphics_queues()[0]->obj();
278}
279
Tony Barbour6918cd52015-04-09 12:58:51 -0600280VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700281{
282 m_device = device;
283 m_nextSlot = 0;
284
285}
286
Tony Barbour6918cd52015-04-09 12:58:51 -0600287VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700288{
Chia-I Wu11078b02015-01-04 16:27:24 +0800289 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700290}
Tony Barbour82c39522014-12-04 14:33:33 -0700291
Tony Barbour6918cd52015-04-09 12:58:51 -0600292int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700293{
Chia-I Wu11078b02015-01-04 16:27:24 +0800294 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600295 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600296 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800297 tc.count = 1;
298 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700299
Chia-I Wu11078b02015-01-04 16:27:24 +0800300 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700301}
Tony Barbour82c39522014-12-04 14:33:33 -0700302
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600303int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700304{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600305 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800306 tc.type = type;
307 tc.count = 1;
308 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700309
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600310 m_updateBuffers.push_back(vk_testing::DescriptorSet::update(type,
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600311 m_nextSlot, 0, 1, &constantBuffer.m_bufferViewInfo));
312
313 // Track mem references for this descriptor set object
314 mem_ref_mgr.AddMemoryRefs(constantBuffer);
Chia-I Wu11078b02015-01-04 16:27:24 +0800315
316 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700317}
Tony Barbour82c39522014-12-04 14:33:33 -0700318
Tony Barbour6918cd52015-04-09 12:58:51 -0600319int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700320{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600321 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600322 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800323 tc.count = 1;
324 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700325
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600326 VkSamplerImageViewInfo tmp = {};
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600327 tmp.sampler = sampler->obj();
Chia-I Wu11078b02015-01-04 16:27:24 +0800328 tmp.pImageView = &texture->m_textureViewInfo;
329 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700330
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600331 m_updateSamplerTextures.push_back(vk_testing::DescriptorSet::update(m_nextSlot, 0, 1,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600332 (const VkSamplerImageViewInfo *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700333
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600334 // Track mem references for the texture referenced here
335 mem_ref_mgr.AddMemoryRefs(*texture);
336
Chia-I Wu11078b02015-01-04 16:27:24 +0800337 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700338}
Chia-I Wu11078b02015-01-04 16:27:24 +0800339
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500340VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700341{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500342 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700343}
344
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600345VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700346{
Chia-I Wu11078b02015-01-04 16:27:24 +0800347 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700348}
Tony Barboure2c58df2014-11-25 13:18:32 -0700349
Tony Barbour6918cd52015-04-09 12:58:51 -0600350void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700351{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600352 // create VkDescriptorPool
353 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600354 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800355 pool.count = m_type_counts.size();
356 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600357 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700358
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600359 // create VkDescriptorSetLayout
360 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800361 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800362 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800363 bindings[i].descriptorType = m_type_counts[i].type;
364 bindings[i].count = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600365 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800366 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700367 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800368
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600369 // create VkDescriptorSetLayout
370 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600371 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800372 layout.count = bindings.size();
373 layout.pBinding = &bindings[0];
374
Chia-I Wu41126e52015-03-26 15:27:55 +0800375 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600376 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800377 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500378
379 // create VkPipelineLayout
380 VkPipelineLayoutCreateInfo pipeline_layout = {};
381 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
382 pipeline_layout.descriptorSetCount = layouts.size();
383 pipeline_layout.pSetLayouts = NULL;
384
385 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700386
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600387 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500388 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800389
Chia-I Wu41126e52015-03-26 15:27:55 +0800390 // build the update array
391 vector<const void *> update_array;
Chia-I Wu11078b02015-01-04 16:27:24 +0800392
Chia-I Wu41126e52015-03-26 15:27:55 +0800393 for (int i = 0; i < m_updateBuffers.size(); i++) {
394 update_array.push_back(&m_updateBuffers[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800395 }
396 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
397 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
Chia-I Wu41126e52015-03-26 15:27:55 +0800398 update_array.push_back(&m_updateSamplerTextures[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800399 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800400
401 // do the updates
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600402 m_device->begin_descriptor_pool_update(VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu11078b02015-01-04 16:27:24 +0800403 clear_sets(*m_set);
Chia-I Wu41126e52015-03-26 15:27:55 +0800404 m_set->update(update_array);
Chia-I Wu985ba162015-03-26 13:14:16 +0800405 m_device->end_descriptor_pool_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700406}
Tony Barboure2c58df2014-11-25 13:18:32 -0700407
Tony Barbour6918cd52015-04-09 12:58:51 -0600408VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800409{
410 m_device = dev;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600411 m_imageInfo.view = VK_NULL_HANDLE;
412 m_imageInfo.layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800413}
414
Tony Barbour6918cd52015-04-09 12:58:51 -0600415void VkImageObj::ImageMemoryBarrier(
416 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600417 VkImageAspect aspect,
418 VkFlags output_mask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600419 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
420 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
421 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
422 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
423 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600424 VkFlags input_mask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600425 VK_MEMORY_INPUT_CPU_READ_BIT |
426 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
427 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
428 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
429 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
430 VK_MEMORY_INPUT_SHADER_READ_BIT |
431 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
432 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
433 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600434 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600435{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600436 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
437 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600438 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
439 subresourceRange);
440
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600441 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600442
Tony Barbourd1c35722015-04-16 15:59:00 -0600443 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600444
445 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600446 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 -0600447}
448
Tony Barbour6918cd52015-04-09 12:58:51 -0600449void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600450 VkImageAspect aspect,
451 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600452{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600453 VkFlags output_mask, input_mask;
454 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600455 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
456 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
457 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
458 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600459 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600460 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600461 VK_MEMORY_INPUT_CPU_READ_BIT |
462 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
463 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
464 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
465 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
466 VK_MEMORY_INPUT_SHADER_READ_BIT |
467 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
468 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600469 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600470
471 if (image_layout == m_imageInfo.layout) {
472 return;
473 }
474
475 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600476 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600477 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
478 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600479 break;
480
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600481 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600482 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
483 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600484 break;
485
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600486 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600487 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
488 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600489 break;
490
Tony Barbourf20f87b2015-04-22 09:02:32 -0600491 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600492 default:
493 output_mask = all_cache_outputs;
494 input_mask = all_cache_inputs;
495 break;
496 }
497
498 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
499 m_imageInfo.layout = image_layout;
500}
501
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600502void VkImageObj::SetLayout(VkImageAspect aspect,
503 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600504{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600505 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600506 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600507
508 /* Build command buffer to set image layout in the driver */
509 err = cmd_buf.BeginCommandBuffer();
510 assert(!err);
511
512 SetLayout(&cmd_buf, aspect, image_layout);
513
514 err = cmd_buf.EndCommandBuffer();
515 assert(!err);
516
517 cmd_buf.QueueCommandBuffer();
518}
519
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600520bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600521{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600522 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600523 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600524 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600525
Tony Barbour579f7802015-04-03 15:11:43 -0600526 return true;
527}
528
Tony Barbour6918cd52015-04-09 12:58:51 -0600529void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600530 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600531 VkImageTiling requested_tiling,
532 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800533{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600534 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600535 VkFormatProperties image_fmt;
536 VkImageTiling tiling;
537 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600538 size_t size;
539
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800540 mipCount = 0;
541
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600542 uint32_t _w = w;
543 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800544 while( ( _w > 0 ) || ( _h > 0 ) )
545 {
546 _w >>= 1;
547 _h >>= 1;
548 mipCount++;
549 }
550
Tony Barbour579f7802015-04-03 15:11:43 -0600551 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600552 err = vkGetFormatInfo(m_device->obj(), fmt,
Tony Barbourd1c35722015-04-16 15:59:00 -0600553 VK_FORMAT_INFO_TYPE_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600554 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600555 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600556
Tony Barbourd1c35722015-04-16 15:59:00 -0600557 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600558 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600559 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600560 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600561 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600562 } else {
563 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
564 }
565 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600566 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600567 } else 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 {
570 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
571 }
572
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600573 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600574 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800575 imageCreateInfo.format = fmt;
576 imageCreateInfo.extent.width = w;
577 imageCreateInfo.extent.height = h;
578 imageCreateInfo.mipLevels = mipCount;
579 imageCreateInfo.tiling = tiling;
580
581 imageCreateInfo.usage = usage;
582
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600583 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800584
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600585 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600586 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600587 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600588 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600589 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800590}
591
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600592VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800593{
594 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600595 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800596}
597
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600598VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800599{
600 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600601 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800602}
603
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600604VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600605{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600606 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600607 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600608 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600609
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600610 /* Build command buffer to copy staging texture to usable texture */
611 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600612 assert(!err);
613
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600614 /* TODO: Can we determine image aspect from image object? */
615 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600616 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600617
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600618 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600619 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600620
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600621 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600622 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600623 copy_region.srcSubresource.arraySlice = 0;
624 copy_region.srcSubresource.mipLevel = 0;
625 copy_region.srcOffset.x = 0;
626 copy_region.srcOffset.y = 0;
627 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600628 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600629 copy_region.destSubresource.arraySlice = 0;
630 copy_region.destSubresource.mipLevel = 0;
631 copy_region.destOffset.x = 0;
632 copy_region.destOffset.y = 0;
633 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600634 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600635
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600636 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600637 src_image.obj(), src_image.layout(),
638 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600639 1, &copy_region);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600640 cmd_buf.mem_ref_mgr.AddMemoryRefs(src_image);
641 cmd_buf.mem_ref_mgr.AddMemoryRefs(*this);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600642
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600643 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600644
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600645 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600646
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600647 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600648 assert(!err);
649
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600650 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600651
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600652 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600653}
654
Tony Barbour6918cd52015-04-09 12:58:51 -0600655VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
656 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700657{
658 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600659 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600660 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600661 void *data;
662 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600663 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600664 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600665
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600666 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600667 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600668
669 if (colors == NULL)
670 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700671
672 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
673
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600674 m_textureViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700675
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600676 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600677 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600678 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600679 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600680 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600681 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600682 view.channels.r = VK_CHANNEL_SWIZZLE_R;
683 view.channels.g = VK_CHANNEL_SWIZZLE_G;
684 view.channels.b = VK_CHANNEL_SWIZZLE_B;
685 view.channels.a = VK_CHANNEL_SWIZZLE_A;
686 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600687 view.subresourceRange.baseMipLevel = 0;
688 view.subresourceRange.mipLevels = 1;
689 view.subresourceRange.baseArraySlice = 0;
690 view.subresourceRange.arraySize = 1;
691 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700692
Tony Barboure2c58df2014-11-25 13:18:32 -0700693 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600694 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700695
696 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800697 view.image = obj();
698 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600699 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700700
Tony Barbour5dc515d2015-04-01 17:47:06 -0600701 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700702
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800703 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700704 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800705 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600706 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700707 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600708 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600709 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700710}
Tony Barbour82c39522014-12-04 14:33:33 -0700711
Tony Barbour6918cd52015-04-09 12:58:51 -0600712VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700713{
Tony Barboure2c58df2014-11-25 13:18:32 -0700714 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700715
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600716 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800717 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600718 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
719 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
720 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600721 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600722 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
723 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
724 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800725 samplerCreateInfo.mipLodBias = 0.0;
726 samplerCreateInfo.maxAnisotropy = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600727 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800728 samplerCreateInfo.minLod = 0.0;
729 samplerCreateInfo.maxLod = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600730 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700731
Chia-I Wue9864b52014-12-28 16:32:24 +0800732 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700733}
Tony Barboure2c58df2014-11-25 13:18:32 -0700734
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700735/*
736 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
737 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600738VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700739{
740 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700741 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700742
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800743 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700744}
745
Tony Barbour6918cd52015-04-09 12:58:51 -0600746VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600747{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600748 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600749 if (m_commandBuffer) {
750 delete m_commandBuffer;
751 }
752}
753
Tony Barbour6918cd52015-04-09 12:58:51 -0600754VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700755{
Tony Barboure2c58df2014-11-25 13:18:32 -0700756 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800757 m_commandBuffer = 0;
758
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800759 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700760 m_numVertices = constantCount;
761 m_stride = constantSize;
762
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600763 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800764 const size_t allocationSize = constantCount * constantSize;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600765 init(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700766
Chia-I Wua07fee62014-12-28 15:26:08 +0800767 void *pData = map();
768 memcpy(pData, data, allocationSize);
769 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700770
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800771 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600772 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600773 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800774 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600775 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800776 view_info.offset = 0;
777 view_info.range = allocationSize;
778 m_bufferView.init(*m_device, view_info);
779
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600780 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800781 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700782}
Tony Barbour82c39522014-12-04 14:33:33 -0700783
Tony Barbourd1c35722015-04-16 15:59:00 -0600784void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700785{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600786 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700787}
788
789
Tony Barbour6918cd52015-04-09 12:58:51 -0600790void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600791 VkFlags outputMask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600792 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
793 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
794 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
795 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
796 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600797 VkFlags inputMask /*=
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600798 VK_MEMORY_INPUT_CPU_READ_BIT |
799 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
800 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
801 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
802 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
803 VK_MEMORY_INPUT_SHADER_READ_BIT |
804 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
805 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
806 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700807{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600808 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700809
Tony Barbour38422802014-12-10 14:36:31 -0700810 if (!m_commandBuffer)
811 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600812 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700813
Tony Barbour6918cd52015-04-09 12:58:51 -0600814 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700815 }
816 else
817 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800818 m_device->wait(m_fence);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600819 m_commandBuffer->mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour38422802014-12-10 14:36:31 -0700820 }
821
Tony Barboure2c58df2014-11-25 13:18:32 -0700822 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600823 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600824 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600825 cmd_buf_info.pNext = NULL;
826 cmd_buf_info.flags = 0;
827
Jon Ashburnc4164b12014-12-31 17:10:47 -0700828 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600829 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700830
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600831 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000832 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600833 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700834
Tony Barbourd1c35722015-04-16 15:59:00 -0600835 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000836
837 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600838 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700839
840 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700841 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600842 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700843
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600844 /*
845 * Tell driver about memory references made in this command buffer
846 * Note: Since this command buffer only has a PipelineBarrier
847 * command there really aren't any memory refs to worry about.
848 */
849 m_commandBuffer->mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
Tony Barboure2c58df2014-11-25 13:18:32 -0700850
851 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600852 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700853 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600854 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
855 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700856}
857
Tony Barbour6918cd52015-04-09 12:58:51 -0600858VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
859 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700860{
861
862}
863
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600864void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700865{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600866 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700867
868 m_numVertices = numIndexes;
869 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700870 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600871 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700872 m_stride = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -0600873 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700874 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600875 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700876 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600877 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700878 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600879 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700880 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600881 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700882 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800883 default:
884 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600885 m_stride = 2;
886 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800887 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700888 }
889
Chia-I Wua07fee62014-12-28 15:26:08 +0800890 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600891 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
892 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700893
Chia-I Wua07fee62014-12-28 15:26:08 +0800894 void *pData = map();
895 memcpy(pData, data, allocationSize);
896 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700897
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800898 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600899 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600900 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800901 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600902 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700903 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800904 view_info.offset = 0;
905 view_info.range = allocationSize;
906 m_bufferView.init(*m_device, view_info);
907
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600908 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800909 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700910}
911
Tony Barbourd1c35722015-04-16 15:59:00 -0600912void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700913{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600914 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700915}
Tony Barboure2c58df2014-11-25 13:18:32 -0700916
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600917VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -0700918{
919 return m_indexType;
920}
921
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600922VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700923{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600924 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600925 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700926 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800927 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700928 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600929 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700930
Tony Barboure2c58df2014-11-25 13:18:32 -0700931 return stageInfo;
932}
933
Tony Barbourd1c35722015-04-16 15:59:00 -0600934VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -0700935{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600936 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600937 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600938 VkShaderCreateInfo createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700939 size_t shader_len;
940
941 m_stage = stage;
942 m_device = device;
943
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600944 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700945 createInfo.pNext = NULL;
946
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600947 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700948
949 shader_len = strlen(shader_code);
950 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
951 createInfo.pCode = malloc(createInfo.codeSize);
952 createInfo.flags = 0;
953
Tony Barbourd1c35722015-04-16 15:59:00 -0600954 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600955 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700956 ((uint32_t *) createInfo.pCode)[1] = 0;
957 ((uint32_t *) createInfo.pCode)[2] = stage;
958 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
959
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800960 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700961 }
962
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600963 if (framework->m_use_spv || err) {
964 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600965 err = VK_SUCCESS;
Tony Barboure2c58df2014-11-25 13:18:32 -0700966
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600967 // Use Reference GLSL to SPV compiler
968 framework->GLSLtoSPV(stage, shader_code, spv);
969 createInfo.pCode = spv.data();
970 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700971 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700972
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800973 init(*m_device, createInfo);
974 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700975}
Tony Barbour82c39522014-12-04 14:33:33 -0700976
Tony Barbour6918cd52015-04-09 12:58:51 -0600977VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700978{
Tony Barboure2c58df2014-11-25 13:18:32 -0700979 m_device = device;
980 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
981 m_vertexBufferCount = 0;
982
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600983 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
984 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600985 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600986 m_ia_state.disableVertexReuse = VK_FALSE;
987 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700988 m_ia_state.primitiveRestartIndex = 0;
989
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600990 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700991 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600992 m_rs_state.depthClipEnable = VK_FALSE;
993 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
994 m_rs_state.programPointSize = VK_FALSE;
995 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
996 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600997 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
998 m_rs_state.cullMode = VK_CULL_MODE_NONE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600999 m_rs_state.frontFace = VK_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001000
Tony Barboure2c58df2014-11-25 13:18:32 -07001001 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001002 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001003 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001004 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1005 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001006
Tony Barbourf52346d2015-01-16 14:27:35 -07001007 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001008 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1009 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001010 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1011 m_ms_state.samples = 1;
1012 m_ms_state.minSampleShading = 0;
1013 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001014
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001015 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -07001016 m_ds_state.pNext = &m_ms_state,
Tony Barbourd1c35722015-04-16 15:59:00 -06001017 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001018 m_ds_state.depthTestEnable = VK_FALSE;
1019 m_ds_state.depthWriteEnable = VK_FALSE;
1020 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001021 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001022 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1023 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1024 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001025 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001026 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001027 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001028
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001029 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001030 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001031 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001032 att.channelWriteMask = 0xf;
1033 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001034
1035};
1036
Tony Barbour6918cd52015-04-09 12:58:51 -06001037void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001038{
1039 m_shaderObjs.push_back(shader);
1040}
1041
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001042void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001043{
1044 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1045 m_vi_state.attributeCount = count;
1046}
1047
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001048void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001049{
1050 m_vi_state.pVertexBindingDescriptions = vi_binding;
1051 m_vi_state.bindingCount = count;
1052}
1053
Tony Barbour6918cd52015-04-09 12:58:51 -06001054void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001055{
1056 m_vertexBufferObjs.push_back(vertexDataBuffer);
1057 m_vertexBufferBindings.push_back(binding);
1058 m_vertexBufferCount++;
1059}
1060
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001061void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001062{
Tony Barbourf52346d2015-01-16 14:27:35 -07001063 if (binding+1 > m_colorAttachments.size())
1064 {
1065 m_colorAttachments.resize(binding+1);
1066 }
1067 m_colorAttachments[binding] = *att;
1068}
1069
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001070void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001071{
1072 m_ds_state.format = ds_state->format;
1073 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1074 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1075 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001076 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001077 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1078 m_ds_state.back = ds_state->back;
1079 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001080}
1081
Tony Barbour6918cd52015-04-09 12:58:51 -06001082void VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001083{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001084 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001085 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001086
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001087 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001088
1089 for (int i=0; i<m_shaderObjs.size(); i++)
1090 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001091 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001092 shaderCreateInfo->pNext = head_ptr;
1093 head_ptr = shaderCreateInfo;
1094 }
1095
1096 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1097 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001098 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001099 m_vi_state.pNext = head_ptr;
1100 head_ptr = &m_vi_state;
1101 }
1102
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001103 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1104 info.pNext = head_ptr;
1105 info.flags = 0;
1106 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001107
Tony Barbourf52346d2015-01-16 14:27:35 -07001108 m_cb_state.attachmentCount = m_colorAttachments.size();
1109 m_cb_state.pAttachments = &m_colorAttachments[0];
1110
Chia-I Wu2648d092014-12-29 14:24:14 +08001111 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001112}
Chia-I Wu2648d092014-12-29 14:24:14 +08001113
Tony Barbourd1c35722015-04-16 15:59:00 -06001114vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001115{
Tony Barbourd1c35722015-04-16 15:59:00 -06001116 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001117 if (this->mem_refs_.size()) {
1118 mems.reserve(this->mem_refs_.size());
1119 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1120 mems.push_back(this->mem_refs_[i]);
1121 }
1122
1123 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001124}
1125
Tony Barbour6918cd52015-04-09 12:58:51 -06001126void VkMemoryRefManager::AddMemoryRefs(vk_testing::Object &vkObject)
Tony Barboure2c58df2014-11-25 13:18:32 -07001127{
Tony Barbourd1c35722015-04-16 15:59:00 -06001128 const std::vector<VkDeviceMemory> mems = vkObject.memories();
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001129 AddMemoryRefs(mems);
1130}
Tony Barboure2c58df2014-11-25 13:18:32 -07001131
Tony Barbourd1c35722015-04-16 15:59:00 -06001132void VkMemoryRefManager::AddMemoryRefs(vector<VkDeviceMemory> mem)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001133{
1134 for (size_t i = 0; i < mem.size(); i++) {
1135 if (mem[i] != NULL) {
1136 this->mem_refs_.push_back(mem[i]);
1137 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001138 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001139}
Tony Barbour82c39522014-12-04 14:33:33 -07001140
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001141void VkMemoryRefManager::EmitAddMemoryRefs(VkQueue queue)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001142{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001143 vkQueueAddMemReferences(queue, mem_refs_.size(), &mem_refs_[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001144}
Tony Barbour82c39522014-12-04 14:33:33 -07001145
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001146void VkMemoryRefManager::EmitRemoveMemoryRefs(VkQueue queue)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001147{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001148 vkQueueRemoveMemReferences(queue, mem_refs_.size(), &mem_refs_[0]);
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001149}
1150
Tony Barbour6918cd52015-04-09 12:58:51 -06001151VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001152 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001153{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001154 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001155}
Tony Barbour471338d2014-12-10 17:28:39 -07001156
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001157VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001158{
Chia-I Wud28343c2014-12-28 15:12:48 +08001159 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001160}
Tony Barbour471338d2014-12-10 17:28:39 -07001161
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001162VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001163{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001164 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001165 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001166}
1167
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001168VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001169{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001170 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001171 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001172}
1173
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001174VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001175{
1176 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001177 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001178}
1179
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001180VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001181{
Chia-I Wud28343c2014-12-28 15:12:48 +08001182 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001183 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001184}
1185
Tony Barbourd1c35722015-04-16 15:59:00 -06001186void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001187{
Tony Barbourd1c35722015-04-16 15:59:00 -06001188 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001189}
1190
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001191void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001192 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001193{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001194 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001195 const VkFlags output_mask =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001196 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
1197 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1198 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1199 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001200 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001201 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001202
1203 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001204 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001205 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001206 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001207 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001208 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001209 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001210
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001211 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001212 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001213 memory_barrier.outputMask = output_mask;
1214 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001215 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001216 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001217 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001218
Tony Barbourd1c35722015-04-16 15:59:00 -06001219 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001220
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001221 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001222 memory_barrier.image = m_renderTargets[i]->image();
1223 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001224 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001225 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001226
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001227 vkCmdClearColorImage(obj(),
1228 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -06001229 clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001230
1231 mem_ref_mgr.AddMemoryRefs(*m_renderTargets[i]);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001232 }
1233
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001234 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001235 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001236 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001237 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001238 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001239 dsRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001240 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001241 dsRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001242
1243 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001244
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001245 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001246 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001247 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001248 memory_barrier.subresourceRange = dsRange;
1249
Tony Barbourd1c35722015-04-16 15:59:00 -06001250 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001251
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001252 vkCmdClearDepthStencil(obj(),
1253 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001254 depth_clear_color, stencil_clear_color,
1255 1, &dsRange);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001256 mem_ref_mgr.AddMemoryRefs(*depthStencilObj);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001257
1258 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001259 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001260 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001261 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001262 memory_barrier.subresourceRange = dsRange;
Tony Barbourd1c35722015-04-16 15:59:00 -06001263 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001264 }
1265}
1266
Tony Barbour6918cd52015-04-09 12:58:51 -06001267void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001268{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001269 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001270 const VkFlags output_mask =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001271 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
1272 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1273 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1274 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001275 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001276 const VkFlags input_mask =
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001277 VK_MEMORY_INPUT_CPU_READ_BIT |
1278 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1279 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1280 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1281 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1282 VK_MEMORY_INPUT_SHADER_READ_BIT |
1283 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1284 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001285 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001286
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001287 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001288 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001289 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001290 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001291 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001292 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001293
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001294 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001295 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001296 memory_barrier.outputMask = output_mask;
1297 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001298 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001299 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001300 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001301
Tony Barbourd1c35722015-04-16 15:59:00 -06001302 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001303
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001304 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001305 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001306 memory_barrier.image = m_renderTargets[i]->image();
1307 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001308 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001309 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001310 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001311}
1312
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001313void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001314{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001315 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001316 renderpass,
1317 framebuffer,
1318 };
1319
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001320 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001321}
1322
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001323void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001324{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001325 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001326}
1327
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001328void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001329{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001330 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001331}
1332
Tony Barbour6918cd52015-04-09 12:58:51 -06001333void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001334{
1335 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001336}
1337
Tony Barbour6918cd52015-04-09 12:58:51 -06001338void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001339{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001340 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001341}
1342
Tony Barbour6918cd52015-04-09 12:58:51 -06001343void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001344{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001345 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001346}
1347
Tony Barbour6918cd52015-04-09 12:58:51 -06001348void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001349{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001350 QueueCommandBuffer(NULL);
1351}
1352
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001353void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001354{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001355 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001356
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001357 mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
1358
Tony Barbour30cc9e82014-12-17 11:53:55 -07001359 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001360 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1361 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001362
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001363 err = vkQueueWaitIdle( m_device->m_queue );
1364 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001365
1366 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001367 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001368
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001369 /*
1370 * Now that processing on this command buffer is complete
1371 * we can remove the memory references.
1372 */
1373 mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001374}
1375
Tony Barbour6918cd52015-04-09 12:58:51 -06001376void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001377{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001378 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001379 mem_ref_mgr.AddMemoryRefs(pipeline);
1380}
1381
Tony Barbour6918cd52015-04-09 12:58:51 -06001382void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001383{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001384 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001385
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001386 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001387 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropd4c1a502015-04-16 13:41:56 -06001388 0, 1, &set_obj, 0, NULL );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001389
1390 // Add descriptor set mem refs to command buffer's list
1391 mem_ref_mgr.AddMemoryRefs(descriptorSet.memories());
1392 mem_ref_mgr.AddMemoryRefs(descriptorSet.mem_ref_mgr.mem_refs());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001393}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001394
Tony Barbour6918cd52015-04-09 12:58:51 -06001395void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001396{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001397 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001398 mem_ref_mgr.AddMemoryRefs(*indexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001399}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001400
Tony Barbourd1c35722015-04-16 15:59:00 -06001401void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001402{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001403 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001404 mem_ref_mgr.AddMemoryRefs(*vertexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001405}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001406
Tony Barbour6918cd52015-04-09 12:58:51 -06001407VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001408{
1409 m_initialized = false;
1410}
Tony Barbour6918cd52015-04-09 12:58:51 -06001411bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001412{
1413 return m_initialized;
1414}
1415
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001416VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001417{
1418 return &m_depthStencilBindInfo;
1419}
1420
Tony Barbour6918cd52015-04-09 12:58:51 -06001421void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001422{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001423 VkImageCreateInfo image_info;
1424 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001425
1426 m_device = device;
1427 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001428 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001429
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001430 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001431 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001432 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001433 image_info.format = m_depth_stencil_fmt;
1434 image_info.extent.width = width;
1435 image_info.extent.height = height;
1436 image_info.extent.depth = 1;
1437 image_info.mipLevels = 1;
1438 image_info.arraySize = 1;
1439 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001440 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001441 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001442 image_info.flags = 0;
1443 init(*m_device, image_info);
1444
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001445 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001446 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001447 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001448 view_info.mipLevel = 0;
1449 view_info.baseArraySlice = 0;
1450 view_info.arraySize = 1;
1451 view_info.flags = 0;
1452 view_info.image = obj();
1453 m_depthStencilView.init(*m_device, view_info);
1454
1455 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001456 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001457}