blob: ae0eceb03267092ee100c8639a2ea24385894548 [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Jon Ashburn83a64252015-04-15 11:31:12 -060030#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
31
Tony Barbour6918cd52015-04-09 12:58:51 -060032VkRenderFramework::VkRenderFramework() :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060033 m_cmdBuffer( VK_NULL_HANDLE ),
Tony Barbourf77fd652015-04-09 11:07:02 -060034 m_renderPass(VK_NULL_HANDLE),
35 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060036 m_stateRaster( VK_NULL_HANDLE ),
37 m_colorBlend( VK_NULL_HANDLE ),
38 m_stateViewport( VK_NULL_HANDLE ),
39 m_stateDepthStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060040 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070041 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060042 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
43 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070044 m_depth_clear_color( 1.0 ),
45 m_stencil_clear_color( 0 )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060046{
Tony Barbour1c45ce02015-03-27 17:03:18 -060047
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070048 // clear the back buffer to dark grey
49 m_clear_color.color.rawColor[0] = 64;
50 m_clear_color.color.rawColor[1] = 64;
51 m_clear_color.color.rawColor[2] = 64;
52 m_clear_color.color.rawColor[3] = 0;
53 m_clear_color.useRawValue = true;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060054}
55
Tony Barbour6918cd52015-04-09 12:58:51 -060056VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060057{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060058
59}
60
Tony Barbour6918cd52015-04-09 12:58:51 -060061void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060062{
Tony Barbour9b9fe782015-04-23 15:28:27 -060063 const std::vector<const char *> extensions;
64 InitFramework(extensions);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060065}
66
Tony Barbour15524c32015-04-29 17:34:29 -060067void VkRenderFramework::InitFramework(const std::vector<const char *> &extensions, VK_DBG_MSG_CALLBACK_FUNCTION dbgFunction, void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060068{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060069 VkResult err;
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060070 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060071 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060072 instInfo.pNext = NULL;
73 instInfo.pAppInfo = &app_info;
74 instInfo.pAllocCb = NULL;
Tony Barbour9b9fe782015-04-23 15:28:27 -060075 instInfo.extensionCount = extensions.size();
76 instInfo.ppEnabledExtensionNames = (extensions.size()) ? &extensions[0] : NULL;;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060077 err = vkCreateInstance(&instInfo, &this->inst);
78 ASSERT_VK_SUCCESS(err);
Jon Ashburn83a64252015-04-15 11:31:12 -060079 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
80 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
81 ASSERT_VK_SUCCESS(err);
82 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060083 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070084 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -060085 if (dbgFunction) {
86 err = vkDbgRegisterMsgCallback(this->inst, dbgFunction, userData);
87 ASSERT_VK_SUCCESS(err);
88 }
Tony Barbour9b9fe782015-04-23 15:28:27 -060089 m_device = new VkDeviceObj(0, objs[0]);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060090 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -060091
Tony Barbour6918cd52015-04-09 12:58:51 -060092 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060093}
94
Tony Barbour6918cd52015-04-09 12:58:51 -060095void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060096{
Mike Stroyanb050c682015-04-17 12:36:38 -060097 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
98 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
99 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
100 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
101 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
102 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600103
104 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600105 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600106 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600107 while (!m_renderTargets.empty()) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600108 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
Mark Lobodzinski23065352015-05-29 09:32:35 -0500109 vkBindObjectMemory(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image(), VK_NULL_HANDLE, 0);
Mike Stroyanb050c682015-04-17 12:36:38 -0600110 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
111 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600112 m_renderTargets.pop_back();
113 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600114
Tony Barbour1c45ce02015-03-27 17:03:18 -0600115 delete m_depthStencil;
116
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600117 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800118 delete m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600119 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600120}
121
Tony Barbour6918cd52015-04-09 12:58:51 -0600122void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600123{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600124 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600125
Tony Barbourd1c35722015-04-16 15:59:00 -0600126 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600127
128 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600129 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600130 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700131 raster.pointSize = 1.0;
132
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600133 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
134 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600135
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600136 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600137 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
138 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
139 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600140
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600141 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600142 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600143 depthStencil.minDepth = 0.f;
144 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700145 depthStencil.stencilFrontRef = 0;
146 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600147
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600148 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
149 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600150
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600151 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600152
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600153 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700154 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
155
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600156 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
157 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600158}
159
Tony Barbour6918cd52015-04-09 12:58:51 -0600160void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600161{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600162 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600163
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600164 VkViewport viewport;
165 VkRect scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600166
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600167 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600168 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700169 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700170 viewport.originX = 0;
171 viewport.originY = 0;
172 viewport.width = 1.f * width;
173 viewport.height = 1.f * height;
174 viewport.minDepth = 0.f;
175 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700176 scissor.extent.width = width;
177 scissor.extent.height = height;
178 scissor.offset.x = 0;
179 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700180 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700181 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700182
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600183 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
184 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600185 m_width = width;
186 m_height = height;
187}
188
Tony Barbour6918cd52015-04-09 12:58:51 -0600189void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600190{
191 InitViewport(m_width, m_height);
192}
Tony Barbour6918cd52015-04-09 12:58:51 -0600193void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600194{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700195 InitRenderTarget(1);
196}
197
Tony Barbour6918cd52015-04-09 12:58:51 -0600198void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700199{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600200 InitRenderTarget(targets, NULL);
201}
202
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600203void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600204{
205 InitRenderTarget(1, dsBinding);
206}
207
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600208void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600209{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600210 std::vector<VkAttachmentLoadOp> load_ops;
211 std::vector<VkAttachmentStoreOp> store_ops;
212 std::vector<VkClearColor> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600213
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600214 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800215
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700216 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600217 VkImageObj *img = new VkImageObj(m_device);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600218
219 VkFormatProperties props;
220 size_t size = sizeof(props);
221 VkResult err;
222
223 err = vkGetFormatInfo(m_device->obj(), m_render_target_fmt,
224 VK_FORMAT_INFO_TYPE_PROPERTIES,
225 &size, &props);
226 ASSERT_VK_SUCCESS(err);
227
228 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
229 img->init(m_width, m_height, m_render_target_fmt,
230 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_LINEAR);
231 }
232 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
233 img->init(m_width, m_height, m_render_target_fmt,
234 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL);
235 }
236 else {
237 FAIL() << "Neither Linear nor Optimal allowed for render target";
238 }
239
240 m_renderTargets.push_back(img);
Tony Barbourf52346d2015-01-16 14:27:35 -0700241 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600242 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600243 load_ops.push_back(VK_ATTACHMENT_LOAD_OP_LOAD);
244 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600245 clear_colors.push_back(m_clear_color);
Chia-I Wuecebf752014-12-05 10:45:15 +0800246 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600247
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700248 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600249 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600250 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600251 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700252 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600253 fb_info.pColorAttachments = m_colorBindings;
254 fb_info.pDepthStencilAttachment = dsBinding;
255 fb_info.sampleCount = 1;
256 fb_info.width = (uint32_t)m_width;
257 fb_info.height = (uint32_t)m_height;
258 fb_info.layers = 1;
259
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600260 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600261
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600262 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600263 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700264 rp_info.renderArea.extent.width = m_width;
265 rp_info.renderArea.extent.height = m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600266
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700267 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600268 rp_info.pColorFormats = &m_render_target_fmt;
269 rp_info.pColorLayouts = &m_colorBindings[0].layout;
270 rp_info.pColorLoadOps = &load_ops[0];
271 rp_info.pColorStoreOps = &store_ops[0];
272 rp_info.pColorLoadClearValues = &clear_colors[0];
273 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600274 if (dsBinding) {
275 rp_info.depthStencilLayout = dsBinding->layout;
276 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600277 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600278 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600279 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
280 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600281 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600282 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
283 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600284}
285
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600286
287
Tony Barbourd1c35722015-04-16 15:59:00 -0600288VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600289 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800290{
291 init();
292
293 props = gpu().properties();
294 queue_props = &gpu().queue_properties()[0];
295}
296
Tony Barbour6918cd52015-04-09 12:58:51 -0600297void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800298{
299 ASSERT_NE(true, graphics_queues().empty());
300 m_queue = graphics_queues()[0]->obj();
301}
302
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600303VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
304 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700305{
Tony Barboure2c58df2014-11-25 13:18:32 -0700306
307}
308
Tony Barbour6918cd52015-04-09 12:58:51 -0600309VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700310{
Chia-I Wu11078b02015-01-04 16:27:24 +0800311 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700312}
Tony Barbour82c39522014-12-04 14:33:33 -0700313
Tony Barbour6918cd52015-04-09 12:58:51 -0600314int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700315{
Chia-I Wu11078b02015-01-04 16:27:24 +0800316 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600317 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600318 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800319 tc.count = 1;
320 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700321
Chia-I Wu11078b02015-01-04 16:27:24 +0800322 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700323}
Tony Barbour82c39522014-12-04 14:33:33 -0700324
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600325int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700326{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600327 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800328 tc.type = type;
329 tc.count = 1;
330 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700331
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800332 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
333 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600334
Chia-I Wu11078b02015-01-04 16:27:24 +0800335 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700336}
Tony Barbour82c39522014-12-04 14:33:33 -0700337
Tony Barbour6918cd52015-04-09 12:58:51 -0600338int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700339{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600340 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600341 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800342 tc.count = 1;
343 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700344
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800345 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600346 tmp.sampler = sampler->obj();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800347 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700348
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800349 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
350 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700351
Chia-I Wu11078b02015-01-04 16:27:24 +0800352 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700353}
Chia-I Wu11078b02015-01-04 16:27:24 +0800354
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500355VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700356{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500357 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700358}
359
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600360VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700361{
Chia-I Wu11078b02015-01-04 16:27:24 +0800362 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700363}
Tony Barboure2c58df2014-11-25 13:18:32 -0700364
Tony Barbour6918cd52015-04-09 12:58:51 -0600365void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700366{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600367 // create VkDescriptorPool
368 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600369 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800370 pool.count = m_type_counts.size();
371 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600372 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700373
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600374 // create VkDescriptorSetLayout
375 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800376 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800377 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800378 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800379 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600380 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800381 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700382 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800383
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600384 // create VkDescriptorSetLayout
385 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600386 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800387 layout.count = bindings.size();
388 layout.pBinding = &bindings[0];
389
Chia-I Wu41126e52015-03-26 15:27:55 +0800390 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600391 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800392 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500393
394 // create VkPipelineLayout
395 VkPipelineLayoutCreateInfo pipeline_layout = {};
396 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
397 pipeline_layout.descriptorSetCount = layouts.size();
398 pipeline_layout.pSetLayouts = NULL;
399
400 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700401
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600402 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500403 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800404
Chia-I Wu41126e52015-03-26 15:27:55 +0800405 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800406 size_t imageSamplerCount = 0;
407 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
408 it != m_writes.end(); it++) {
409 it->destSet = m_set->obj();
410 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
411 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800412 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800413
414 // do the updates
Chia-I Wu11078b02015-01-04 16:27:24 +0800415 clear_sets(*m_set);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800416 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700417}
Tony Barboure2c58df2014-11-25 13:18:32 -0700418
Tony Barbour6918cd52015-04-09 12:58:51 -0600419VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800420{
421 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800422 m_descriptorInfo.imageView = VK_NULL_HANDLE;
423 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800424}
425
Tony Barbour6918cd52015-04-09 12:58:51 -0600426void VkImageObj::ImageMemoryBarrier(
427 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600428 VkImageAspect aspect,
429 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600430 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600431 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
432 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
433 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
434 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600435 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600436 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600437 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
438 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
439 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
440 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
441 VK_MEMORY_INPUT_SHADER_READ_BIT |
442 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
443 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
444 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600445 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600446{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600447 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
448 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600449 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
450 subresourceRange);
451
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600452 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600453
Tony Barbourd1c35722015-04-16 15:59:00 -0600454 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600455
456 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600457 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 -0600458}
459
Tony Barbour6918cd52015-04-09 12:58:51 -0600460void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600461 VkImageAspect aspect,
462 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600463{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600464 VkFlags output_mask, input_mask;
465 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600466 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600467 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
468 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
469 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600470 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600471 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600472 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600473 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
474 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
475 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
476 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
477 VK_MEMORY_INPUT_SHADER_READ_BIT |
478 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
479 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600480 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600481
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800482 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600483 return;
484 }
485
486 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600487 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600488 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
489 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600490 break;
491
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600492 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600493 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
494 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600495 break;
496
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600497 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600498 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
499 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600500 break;
501
Tony Barbourf20f87b2015-04-22 09:02:32 -0600502 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600503 default:
504 output_mask = all_cache_outputs;
505 input_mask = all_cache_inputs;
506 break;
507 }
508
509 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800510 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600511}
512
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600513void VkImageObj::SetLayout(VkImageAspect aspect,
514 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600515{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600516 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600517
518 if (image_layout == m_descriptorInfo.imageLayout) {
519 return;
520 }
521
Tony Barbour6918cd52015-04-09 12:58:51 -0600522 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600523
524 /* Build command buffer to set image layout in the driver */
525 err = cmd_buf.BeginCommandBuffer();
526 assert(!err);
527
528 SetLayout(&cmd_buf, aspect, image_layout);
529
530 err = cmd_buf.EndCommandBuffer();
531 assert(!err);
532
533 cmd_buf.QueueCommandBuffer();
534}
535
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600536bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600537{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600538 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600539 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600540 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600541
Tony Barbour579f7802015-04-03 15:11:43 -0600542 return true;
543}
544
Tony Barbour6918cd52015-04-09 12:58:51 -0600545void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600546 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600547 VkImageTiling requested_tiling,
548 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800549{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600550 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600551 VkFormatProperties image_fmt;
552 VkImageTiling tiling;
553 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600554 size_t size;
555
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800556 mipCount = 0;
557
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600558 uint32_t _w = w;
559 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800560 while( ( _w > 0 ) || ( _h > 0 ) )
561 {
562 _w >>= 1;
563 _h >>= 1;
564 mipCount++;
565 }
566
Tony Barbour579f7802015-04-03 15:11:43 -0600567 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600568 err = vkGetFormatInfo(m_device->obj(), fmt,
Tony Barbourd1c35722015-04-16 15:59:00 -0600569 VK_FORMAT_INFO_TYPE_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600570 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600571 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600572
Tony Barbourd1c35722015-04-16 15:59:00 -0600573 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600574 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600575 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600576 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600577 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600578 } else {
579 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
580 }
581 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600582 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600583 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600584 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600585 } else {
586 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
587 }
588
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600589 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600590 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800591 imageCreateInfo.format = fmt;
592 imageCreateInfo.extent.width = w;
593 imageCreateInfo.extent.height = h;
594 imageCreateInfo.mipLevels = mipCount;
595 imageCreateInfo.tiling = tiling;
596
597 imageCreateInfo.usage = usage;
598
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600599 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800600
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600601 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600602 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600603 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600604 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600605 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800606}
607
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600608VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800609{
610 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600611 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800612}
613
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600614VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800615{
616 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600617 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800618}
619
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600620VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600621{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600622 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600623 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600624 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600625
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600626 /* Build command buffer to copy staging texture to usable texture */
627 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600628 assert(!err);
629
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600630 /* TODO: Can we determine image aspect from image object? */
631 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600632 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600633
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600634 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600635 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600636
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600637 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600638 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600639 copy_region.srcSubresource.arraySlice = 0;
640 copy_region.srcSubresource.mipLevel = 0;
641 copy_region.srcOffset.x = 0;
642 copy_region.srcOffset.y = 0;
643 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600644 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600645 copy_region.destSubresource.arraySlice = 0;
646 copy_region.destSubresource.mipLevel = 0;
647 copy_region.destOffset.x = 0;
648 copy_region.destOffset.y = 0;
649 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600650 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600651
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600652 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600653 src_image.obj(), src_image.layout(),
654 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600655 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600656
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600657 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600658
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600659 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600660
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600661 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600662 assert(!err);
663
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600664 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600665
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600666 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600667}
668
Tony Barbour6918cd52015-04-09 12:58:51 -0600669VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
670 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700671{
672 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600673 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600674 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600675 void *data;
676 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600677 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600678 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600679
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600680 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600681 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600682
683 if (colors == NULL)
684 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700685
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800686 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700687
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600688 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600689 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600690 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600691 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600692 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600693 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600694 view.channels.r = VK_CHANNEL_SWIZZLE_R;
695 view.channels.g = VK_CHANNEL_SWIZZLE_G;
696 view.channels.b = VK_CHANNEL_SWIZZLE_B;
697 view.channels.a = VK_CHANNEL_SWIZZLE_A;
698 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600699 view.subresourceRange.baseMipLevel = 0;
700 view.subresourceRange.mipLevels = 1;
701 view.subresourceRange.baseArraySlice = 0;
702 view.subresourceRange.arraySize = 1;
703 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700704
Tony Barboure2c58df2014-11-25 13:18:32 -0700705 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600706 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700707
708 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800709 view.image = obj();
710 m_textureView.init(*m_device, view);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800711 m_descriptorInfo.imageView = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700712
Tony Barbour5dc515d2015-04-01 17:47:06 -0600713 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700714
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800715 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700716 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800717 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600718 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700719 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600720 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600721 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700722}
Tony Barbour82c39522014-12-04 14:33:33 -0700723
Tony Barbour6918cd52015-04-09 12:58:51 -0600724VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700725{
Tony Barboure2c58df2014-11-25 13:18:32 -0700726 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700727
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600728 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800729 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600730 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
731 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
732 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600733 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600734 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
735 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
736 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800737 samplerCreateInfo.mipLodBias = 0.0;
738 samplerCreateInfo.maxAnisotropy = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600739 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800740 samplerCreateInfo.minLod = 0.0;
741 samplerCreateInfo.maxLod = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600742 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700743
Chia-I Wue9864b52014-12-28 16:32:24 +0800744 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700745}
Tony Barboure2c58df2014-11-25 13:18:32 -0700746
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700747/*
748 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
749 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600750VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700751{
752 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700753 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700754
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800755 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700756}
757
Tony Barbour6918cd52015-04-09 12:58:51 -0600758VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600759{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600760 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600761 if (m_commandBuffer) {
762 delete m_commandBuffer;
763 }
764}
765
Tony Barbour6918cd52015-04-09 12:58:51 -0600766VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700767{
Tony Barboure2c58df2014-11-25 13:18:32 -0700768 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800769 m_commandBuffer = 0;
770
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800771 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700772 m_numVertices = constantCount;
773 m_stride = constantSize;
774
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600775 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800776 const size_t allocationSize = constantCount * constantSize;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600777 init(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700778
Chia-I Wua07fee62014-12-28 15:26:08 +0800779 void *pData = map();
780 memcpy(pData, data, allocationSize);
781 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700782
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800783 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600784 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600785 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800786 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600787 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800788 view_info.offset = 0;
789 view_info.range = allocationSize;
790 m_bufferView.init(*m_device, view_info);
791
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800792 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700793}
Tony Barbour82c39522014-12-04 14:33:33 -0700794
Tony Barbourd1c35722015-04-16 15:59:00 -0600795void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700796{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600797 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700798}
799
800
Tony Barbour6918cd52015-04-09 12:58:51 -0600801void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600802 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600803 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600804 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
805 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
806 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
807 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600808 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600809 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600810 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
811 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
812 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
813 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
814 VK_MEMORY_INPUT_SHADER_READ_BIT |
815 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
816 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
817 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700818{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600819 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700820
Tony Barbour38422802014-12-10 14:36:31 -0700821 if (!m_commandBuffer)
822 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600823 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700824
Tony Barbour6918cd52015-04-09 12:58:51 -0600825 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700826 }
827 else
828 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800829 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700830 }
831
Tony Barboure2c58df2014-11-25 13:18:32 -0700832 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600833 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600834 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600835 cmd_buf_info.pNext = NULL;
836 cmd_buf_info.flags = 0;
837
Jon Ashburnc4164b12014-12-31 17:10:47 -0700838 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600839 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700840
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600841 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000842 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600843 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700844
Tony Barbourd1c35722015-04-16 15:59:00 -0600845 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000846
847 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600848 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700849
850 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700851 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600852 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700853
Tony Barboure2c58df2014-11-25 13:18:32 -0700854 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600855 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700856 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600857 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
858 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700859}
860
Tony Barbour6918cd52015-04-09 12:58:51 -0600861VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
862 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700863{
864
865}
866
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600867void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700868{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600869 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700870
871 m_numVertices = numIndexes;
872 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700873 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600874 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700875 m_stride = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -0600876 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700877 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600878 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700879 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600880 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700881 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600882 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700883 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600884 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700885 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800886 default:
887 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600888 m_stride = 2;
889 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800890 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700891 }
892
Chia-I Wua07fee62014-12-28 15:26:08 +0800893 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600894 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
895 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700896
Chia-I Wua07fee62014-12-28 15:26:08 +0800897 void *pData = map();
898 memcpy(pData, data, allocationSize);
899 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700900
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800901 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600902 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600903 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800904 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600905 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700906 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800907 view_info.offset = 0;
908 view_info.range = allocationSize;
909 m_bufferView.init(*m_device, view_info);
910
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800911 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700912}
913
Tony Barbourd1c35722015-04-16 15:59:00 -0600914void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700915{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600916 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700917}
Tony Barboure2c58df2014-11-25 13:18:32 -0700918
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600919VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -0700920{
921 return m_indexType;
922}
923
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600924VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700925{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600926 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600927 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700928 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800929 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700930 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600931 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700932
Tony Barboure2c58df2014-11-25 13:18:32 -0700933 return stageInfo;
934}
935
Tony Barbourd1c35722015-04-16 15:59:00 -0600936VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -0700937{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600938 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600939 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600940 VkShaderCreateInfo createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700941 size_t shader_len;
942
943 m_stage = stage;
944 m_device = device;
945
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600946 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700947 createInfo.pNext = NULL;
948
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600949 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700950
951 shader_len = strlen(shader_code);
952 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
953 createInfo.pCode = malloc(createInfo.codeSize);
954 createInfo.flags = 0;
955
Tony Barbourd1c35722015-04-16 15:59:00 -0600956 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600957 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700958 ((uint32_t *) createInfo.pCode)[1] = 0;
959 ((uint32_t *) createInfo.pCode)[2] = stage;
960 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
961
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800962 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700963 }
964
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600965 if (framework->m_use_spv || err) {
966 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600967 err = VK_SUCCESS;
Tony Barboure2c58df2014-11-25 13:18:32 -0700968
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600969 // Use Reference GLSL to SPV compiler
970 framework->GLSLtoSPV(stage, shader_code, spv);
971 createInfo.pCode = spv.data();
972 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700973 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700974
Cody Northrop475663c2015-04-15 11:19:06 -0600975 err = init_try(*m_device, createInfo);
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800976 }
Cody Northrop475663c2015-04-15 11:19:06 -0600977
978 assert(VK_SUCCESS == err);
Tony Barbourf325bf12014-12-03 15:59:38 -0700979}
Tony Barbour82c39522014-12-04 14:33:33 -0700980
Tony Barbour6918cd52015-04-09 12:58:51 -0600981VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700982{
Tony Barboure2c58df2014-11-25 13:18:32 -0700983 m_device = device;
984 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
985 m_vertexBufferCount = 0;
986
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600987 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
988 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600989 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600990 m_ia_state.disableVertexReuse = VK_FALSE;
991 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700992 m_ia_state.primitiveRestartIndex = 0;
993
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600994 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700995 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600996 m_rs_state.depthClipEnable = VK_FALSE;
997 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
998 m_rs_state.programPointSize = VK_FALSE;
999 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1000 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001001 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001002 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1003 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001004
Tony Barboure2c58df2014-11-25 13:18:32 -07001005 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001006 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001007 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001008 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1009 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001010
Tony Barbourf52346d2015-01-16 14:27:35 -07001011 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001012 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1013 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001014 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1015 m_ms_state.samples = 1;
1016 m_ms_state.minSampleShading = 0;
1017 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001018
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001019 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -07001020 m_ds_state.pNext = &m_ms_state,
Tony Barbourd1c35722015-04-16 15:59:00 -06001021 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001022 m_ds_state.depthTestEnable = VK_FALSE;
1023 m_ds_state.depthWriteEnable = VK_FALSE;
1024 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001025 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001026 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1027 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1028 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001029 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001030 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001031 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001032
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001033 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001034 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001035 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001036 att.channelWriteMask = 0xf;
1037 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001038
1039};
1040
Tony Barbour6918cd52015-04-09 12:58:51 -06001041void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001042{
1043 m_shaderObjs.push_back(shader);
1044}
1045
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001046void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001047{
1048 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1049 m_vi_state.attributeCount = count;
1050}
1051
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001052void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001053{
1054 m_vi_state.pVertexBindingDescriptions = vi_binding;
1055 m_vi_state.bindingCount = count;
1056}
1057
Tony Barbour6918cd52015-04-09 12:58:51 -06001058void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001059{
1060 m_vertexBufferObjs.push_back(vertexDataBuffer);
1061 m_vertexBufferBindings.push_back(binding);
1062 m_vertexBufferCount++;
1063}
1064
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001065void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001066{
Tony Barbourf52346d2015-01-16 14:27:35 -07001067 if (binding+1 > m_colorAttachments.size())
1068 {
1069 m_colorAttachments.resize(binding+1);
1070 }
1071 m_colorAttachments[binding] = *att;
1072}
1073
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001074void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001075{
1076 m_ds_state.format = ds_state->format;
1077 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1078 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1079 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001080 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001081 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1082 m_ds_state.back = ds_state->back;
1083 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001084}
1085
Tony Barbour6918cd52015-04-09 12:58:51 -06001086void VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001087{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001088 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001089 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001090
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001091 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001092
1093 for (int i=0; i<m_shaderObjs.size(); i++)
1094 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001095 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001096 shaderCreateInfo->pNext = head_ptr;
1097 head_ptr = shaderCreateInfo;
1098 }
1099
1100 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1101 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001102 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001103 m_vi_state.pNext = head_ptr;
1104 head_ptr = &m_vi_state;
1105 }
1106
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001107 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1108 info.pNext = head_ptr;
1109 info.flags = 0;
1110 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001111
Tony Barbourf52346d2015-01-16 14:27:35 -07001112 m_cb_state.attachmentCount = m_colorAttachments.size();
1113 m_cb_state.pAttachments = &m_colorAttachments[0];
1114
Chia-I Wu2648d092014-12-29 14:24:14 +08001115 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001116}
Chia-I Wu2648d092014-12-29 14:24:14 +08001117
Tony Barbourd1c35722015-04-16 15:59:00 -06001118vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001119{
Tony Barbourd1c35722015-04-16 15:59:00 -06001120 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001121 if (this->mem_refs_.size()) {
1122 mems.reserve(this->mem_refs_.size());
1123 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1124 mems.push_back(this->mem_refs_[i]);
1125 }
1126
1127 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001128}
1129
Tony Barbour6918cd52015-04-09 12:58:51 -06001130VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001131 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001132{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001133 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001134}
Tony Barbour471338d2014-12-10 17:28:39 -07001135
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001136VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001137{
Chia-I Wud28343c2014-12-28 15:12:48 +08001138 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001139}
Tony Barbour471338d2014-12-10 17:28:39 -07001140
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001141VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001142{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001143 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001144 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001145}
1146
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001147VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001148{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001149 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001150 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001151}
1152
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001153VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001154{
1155 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001156 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001157}
1158
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001159VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001160{
Chia-I Wud28343c2014-12-28 15:12:48 +08001161 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001162 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001163}
1164
Tony Barbourd1c35722015-04-16 15:59:00 -06001165void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001166{
Tony Barbourd1c35722015-04-16 15:59:00 -06001167 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001168}
1169
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001170void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001171 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001172{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001173 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001174 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001175 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001176 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1177 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1178 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001179 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001180 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001181
1182 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001183 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001184 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001185 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001186 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001187 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001188 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001189
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001190 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001191 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001192 memory_barrier.outputMask = output_mask;
1193 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001194 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001195 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001196 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001197
Tony Barbourd1c35722015-04-16 15:59:00 -06001198 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001199
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001200 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001201 memory_barrier.image = m_renderTargets[i]->image();
1202 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001203 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001204 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001205
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001206 vkCmdClearColorImage(obj(),
1207 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001208 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001209
Tony Barbour30cc9e82014-12-17 11:53:55 -07001210 }
1211
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001212 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001213 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001214 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001215 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001216 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001217 dsRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001218 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001219 dsRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001220
1221 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001222
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001223 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001224 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001225 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001226 memory_barrier.subresourceRange = dsRange;
1227
Tony Barbourd1c35722015-04-16 15:59:00 -06001228 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001229
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001230 vkCmdClearDepthStencil(obj(),
1231 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001232 depth_clear_color, stencil_clear_color,
1233 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001234
1235 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001236 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001237 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001238 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001239 memory_barrier.subresourceRange = dsRange;
Tony Barbourd1c35722015-04-16 15:59:00 -06001240 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001241 }
1242}
1243
Tony Barbour6918cd52015-04-09 12:58:51 -06001244void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001245{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001246 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001247 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001248 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001249 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1250 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1251 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001252 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001253 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001254 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001255 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1256 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1257 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1258 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1259 VK_MEMORY_INPUT_SHADER_READ_BIT |
1260 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1261 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001262 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001263
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001264 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001265 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001266 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001267 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001268 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001269 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001270
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001271 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001272 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001273 memory_barrier.outputMask = output_mask;
1274 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001275 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001276 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001277 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001278
Tony Barbourd1c35722015-04-16 15:59:00 -06001279 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001280
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001281 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001282 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001283 memory_barrier.image = m_renderTargets[i]->image();
1284 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001285 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001286 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001287 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001288}
1289
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001290void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001291{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001292 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001293 renderpass,
1294 framebuffer,
1295 };
1296
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001297 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001298}
1299
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001300void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001301{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001302 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001303}
1304
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001305void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001306{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001307 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001308}
1309
Tony Barbour6918cd52015-04-09 12:58:51 -06001310void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001311{
1312 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001313}
1314
Tony Barbour6918cd52015-04-09 12:58:51 -06001315void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001316{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001317 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001318}
1319
Tony Barbour6918cd52015-04-09 12:58:51 -06001320void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001321{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001322 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001323}
1324
Tony Barbour6918cd52015-04-09 12:58:51 -06001325void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001326{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001327 QueueCommandBuffer(NULL);
1328}
1329
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001330void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001331{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001332 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001333
1334 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001335 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1336 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001337
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001338 err = vkQueueWaitIdle( m_device->m_queue );
1339 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001340
1341 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001342 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001343}
1344
Tony Barbour6918cd52015-04-09 12:58:51 -06001345void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001346{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001347 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001348}
1349
Tony Barbour6918cd52015-04-09 12:58:51 -06001350void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001351{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001352 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001353
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001354 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001355 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropd4c1a502015-04-16 13:41:56 -06001356 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001357}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001358
Tony Barbour6918cd52015-04-09 12:58:51 -06001359void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001360{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001361 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001362}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001363
Tony Barbourd1c35722015-04-16 15:59:00 -06001364void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001365{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001366 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001367}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001368
Tony Barbour6918cd52015-04-09 12:58:51 -06001369VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001370{
1371 m_initialized = false;
1372}
Tony Barbour6918cd52015-04-09 12:58:51 -06001373bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001374{
1375 return m_initialized;
1376}
1377
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001378VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001379{
1380 return &m_depthStencilBindInfo;
1381}
1382
Tony Barbour6918cd52015-04-09 12:58:51 -06001383void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001384{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001385 VkImageCreateInfo image_info;
1386 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001387
1388 m_device = device;
1389 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001390 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001391
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001392 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001393 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001394 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001395 image_info.format = m_depth_stencil_fmt;
1396 image_info.extent.width = width;
1397 image_info.extent.height = height;
1398 image_info.extent.depth = 1;
1399 image_info.mipLevels = 1;
1400 image_info.arraySize = 1;
1401 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001402 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001403 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001404 image_info.flags = 0;
1405 init(*m_device, image_info);
1406
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001407 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001408 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001409 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001410 view_info.mipLevel = 0;
1411 view_info.baseArraySlice = 0;
1412 view_info.arraySize = 1;
1413 view_info.flags = 0;
1414 view_info.image = obj();
1415 m_depthStencilView.init(*m_device, view_info);
1416
1417 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001418 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001419}