blob: 90a2b07379d0a07cdcd99536515e0d930602ccc6 [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());
Mike Stroyanb050c682015-04-17 12:36:38 -0600109 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
110 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600111 m_renderTargets.pop_back();
112 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600113
Tony Barbour1c45ce02015-03-27 17:03:18 -0600114 delete m_depthStencil;
115
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600116 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800117 delete m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600118 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600119}
120
Tony Barbour6918cd52015-04-09 12:58:51 -0600121void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600122{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600123 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600124
Tony Barbourd1c35722015-04-16 15:59:00 -0600125 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600126
127 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600128 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600129 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700130 raster.pointSize = 1.0;
131
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600132 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
133 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600134
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600135 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600136 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600137 blend.blendConst[0] = 1.0f;
138 blend.blendConst[1] = 1.0f;
139 blend.blendConst[2] = 1.0f;
140 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600141 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
142 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600143
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600144 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600145 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600146 depthStencil.minDepth = 0.f;
147 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700148 depthStencil.stencilFrontRef = 0;
149 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600150 depthStencil.stencilReadMask = 0xff;
151 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600152
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600153 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
154 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600155
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600156 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600157
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600158 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700159 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
160
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600161 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
162 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600163}
164
Tony Barbour6918cd52015-04-09 12:58:51 -0600165void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600166{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600167 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600168
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600169 VkViewport viewport;
170 VkRect scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600171
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600172 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600173 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700174 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700175 viewport.originX = 0;
176 viewport.originY = 0;
177 viewport.width = 1.f * width;
178 viewport.height = 1.f * height;
179 viewport.minDepth = 0.f;
180 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600181 scissor.extent.width = (int32_t) width;
182 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700183 scissor.offset.x = 0;
184 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700185 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700186 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700187
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600188 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
189 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600190 m_width = width;
191 m_height = height;
192}
193
Tony Barbour6918cd52015-04-09 12:58:51 -0600194void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600195{
196 InitViewport(m_width, m_height);
197}
Tony Barbour6918cd52015-04-09 12:58:51 -0600198void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600199{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700200 InitRenderTarget(1);
201}
202
Tony Barbour6918cd52015-04-09 12:58:51 -0600203void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700204{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600205 InitRenderTarget(targets, NULL);
206}
207
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600208void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600209{
210 InitRenderTarget(1, dsBinding);
211}
212
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600213void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600214{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600215 std::vector<VkAttachmentLoadOp> load_ops;
216 std::vector<VkAttachmentStoreOp> store_ops;
217 std::vector<VkClearColor> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600218
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600219 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800220
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700221 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600222 VkImageObj *img = new VkImageObj(m_device);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600223
224 VkFormatProperties props;
225 size_t size = sizeof(props);
226 VkResult err;
227
228 err = vkGetFormatInfo(m_device->obj(), m_render_target_fmt,
229 VK_FORMAT_INFO_TYPE_PROPERTIES,
230 &size, &props);
231 ASSERT_VK_SUCCESS(err);
232
233 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600234 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600235 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_LINEAR);
236 }
237 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600238 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600239 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL);
240 }
241 else {
242 FAIL() << "Neither Linear nor Optimal allowed for render target";
243 }
244
245 m_renderTargets.push_back(img);
Tony Barbourf52346d2015-01-16 14:27:35 -0700246 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600247 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600248 load_ops.push_back(VK_ATTACHMENT_LOAD_OP_LOAD);
249 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600250 clear_colors.push_back(m_clear_color);
Chia-I Wuecebf752014-12-05 10:45:15 +0800251 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600252
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700253 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600254 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600255 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600256 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700257 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600258 fb_info.pColorAttachments = m_colorBindings;
259 fb_info.pDepthStencilAttachment = dsBinding;
260 fb_info.sampleCount = 1;
261 fb_info.width = (uint32_t)m_width;
262 fb_info.height = (uint32_t)m_height;
263 fb_info.layers = 1;
264
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600265 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600266
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600267 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600268 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600269 rp_info.renderArea.extent.width = (uint32_t) m_width;
270 rp_info.renderArea.extent.height = (uint32_t) m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600271
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700272 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600273 rp_info.pColorFormats = &m_render_target_fmt;
274 rp_info.pColorLayouts = &m_colorBindings[0].layout;
275 rp_info.pColorLoadOps = &load_ops[0];
276 rp_info.pColorStoreOps = &store_ops[0];
277 rp_info.pColorLoadClearValues = &clear_colors[0];
278 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600279 if (dsBinding) {
280 rp_info.depthStencilLayout = dsBinding->layout;
281 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600282 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600283 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600284 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
285 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600286 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600287 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Tony Barbour51a05ce2015-06-03 11:40:51 -0600288 rp_info.sampleCount = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600289 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600290}
291
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600292
293
Tony Barbourd1c35722015-04-16 15:59:00 -0600294VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600295 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800296{
297 init();
298
299 props = gpu().properties();
300 queue_props = &gpu().queue_properties()[0];
301}
302
Tony Barbour6918cd52015-04-09 12:58:51 -0600303void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800304{
305 ASSERT_NE(true, graphics_queues().empty());
306 m_queue = graphics_queues()[0]->obj();
307}
308
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600309VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
310 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700311{
Tony Barboure2c58df2014-11-25 13:18:32 -0700312
313}
314
Tony Barbour6918cd52015-04-09 12:58:51 -0600315VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700316{
Chia-I Wu11078b02015-01-04 16:27:24 +0800317 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700318}
Tony Barbour82c39522014-12-04 14:33:33 -0700319
Tony Barbour6918cd52015-04-09 12:58:51 -0600320int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700321{
Chia-I Wu11078b02015-01-04 16:27:24 +0800322 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600323 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600324 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800325 tc.count = 1;
326 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700327
Chia-I Wu11078b02015-01-04 16:27:24 +0800328 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700329}
Tony Barbour82c39522014-12-04 14:33:33 -0700330
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600331int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700332{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600333 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800334 tc.type = type;
335 tc.count = 1;
336 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700337
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800338 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
339 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600340
Chia-I Wu11078b02015-01-04 16:27:24 +0800341 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700342}
Tony Barbour82c39522014-12-04 14:33:33 -0700343
Tony Barbour6918cd52015-04-09 12:58:51 -0600344int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700345{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600346 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600347 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800348 tc.count = 1;
349 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700350
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800351 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600352 tmp.sampler = sampler->obj();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800353 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700354
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800355 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
356 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700357
Chia-I Wu11078b02015-01-04 16:27:24 +0800358 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700359}
Chia-I Wu11078b02015-01-04 16:27:24 +0800360
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500361VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700362{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500363 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700364}
365
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600366VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700367{
Chia-I Wu11078b02015-01-04 16:27:24 +0800368 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700369}
Tony Barboure2c58df2014-11-25 13:18:32 -0700370
Tony Barbour6918cd52015-04-09 12:58:51 -0600371void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700372{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600373 // create VkDescriptorPool
374 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600375 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800376 pool.count = m_type_counts.size();
377 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600378 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700379
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600380 // create VkDescriptorSetLayout
381 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800382 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800383 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800384 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800385 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600386 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800387 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700388 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800389
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600390 // create VkDescriptorSetLayout
391 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600392 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800393 layout.count = bindings.size();
394 layout.pBinding = &bindings[0];
395
Chia-I Wu41126e52015-03-26 15:27:55 +0800396 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600397 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800398 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500399
400 // create VkPipelineLayout
401 VkPipelineLayoutCreateInfo pipeline_layout = {};
402 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
403 pipeline_layout.descriptorSetCount = layouts.size();
404 pipeline_layout.pSetLayouts = NULL;
405
406 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700407
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600408 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500409 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800410
Chia-I Wu41126e52015-03-26 15:27:55 +0800411 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800412 size_t imageSamplerCount = 0;
413 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
414 it != m_writes.end(); it++) {
415 it->destSet = m_set->obj();
416 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
417 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800418 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800419
420 // do the updates
Chia-I Wu11078b02015-01-04 16:27:24 +0800421 clear_sets(*m_set);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800422 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700423}
Tony Barboure2c58df2014-11-25 13:18:32 -0700424
Tony Barbour6918cd52015-04-09 12:58:51 -0600425VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800426{
427 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800428 m_descriptorInfo.imageView = VK_NULL_HANDLE;
429 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800430}
431
Tony Barbour6918cd52015-04-09 12:58:51 -0600432void VkImageObj::ImageMemoryBarrier(
433 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600434 VkImageAspect aspect,
435 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600436 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600437 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
438 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
439 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
440 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600441 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600442 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600443 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
444 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
445 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
446 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
447 VK_MEMORY_INPUT_SHADER_READ_BIT |
448 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
449 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
450 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600451 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600452{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600453 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
454 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600455 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
456 subresourceRange);
457
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600458 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600459
Tony Barbourd1c35722015-04-16 15:59:00 -0600460 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600461
462 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600463 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 -0600464}
465
Tony Barbour6918cd52015-04-09 12:58:51 -0600466void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600467 VkImageAspect aspect,
468 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600469{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600470 VkFlags output_mask, input_mask;
471 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600472 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600473 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
474 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
475 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600476 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600477 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600478 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600479 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
480 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
481 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
482 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
483 VK_MEMORY_INPUT_SHADER_READ_BIT |
484 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
485 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600486 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600487
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800488 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600489 return;
490 }
491
492 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600493 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600494 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
495 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600496 break;
497
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600498 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600499 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
500 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600501 break;
502
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600503 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600504 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
505 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600506 break;
507
Tony Barbourf20f87b2015-04-22 09:02:32 -0600508 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600509 default:
510 output_mask = all_cache_outputs;
511 input_mask = all_cache_inputs;
512 break;
513 }
514
515 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800516 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600517}
518
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600519void VkImageObj::SetLayout(VkImageAspect aspect,
520 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600521{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600522 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600523
524 if (image_layout == m_descriptorInfo.imageLayout) {
525 return;
526 }
527
Tony Barbour6918cd52015-04-09 12:58:51 -0600528 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600529
530 /* Build command buffer to set image layout in the driver */
531 err = cmd_buf.BeginCommandBuffer();
532 assert(!err);
533
534 SetLayout(&cmd_buf, aspect, image_layout);
535
536 err = cmd_buf.EndCommandBuffer();
537 assert(!err);
538
539 cmd_buf.QueueCommandBuffer();
540}
541
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600542bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600543{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600544 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600545 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600546 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600547
Tony Barbour579f7802015-04-03 15:11:43 -0600548 return true;
549}
550
Tony Barbour6918cd52015-04-09 12:58:51 -0600551void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600552 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600553 VkImageTiling requested_tiling,
554 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800555{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600556 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600557 VkFormatProperties image_fmt;
558 VkImageTiling tiling;
559 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600560 size_t size;
561
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800562 mipCount = 0;
563
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600564 uint32_t _w = w;
565 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800566 while( ( _w > 0 ) || ( _h > 0 ) )
567 {
568 _w >>= 1;
569 _h >>= 1;
570 mipCount++;
571 }
572
Tony Barbour579f7802015-04-03 15:11:43 -0600573 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600574 err = vkGetFormatInfo(m_device->obj(), fmt,
Tony Barbourd1c35722015-04-16 15:59:00 -0600575 VK_FORMAT_INFO_TYPE_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600576 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600577 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600578
Tony Barbourd1c35722015-04-16 15:59:00 -0600579 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600580 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600581 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600582 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600583 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600584 } else {
585 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
586 }
587 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600588 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600589 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600590 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600591 } else {
592 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
593 }
594
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600595 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600596 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800597 imageCreateInfo.format = fmt;
598 imageCreateInfo.extent.width = w;
599 imageCreateInfo.extent.height = h;
600 imageCreateInfo.mipLevels = mipCount;
601 imageCreateInfo.tiling = tiling;
602
603 imageCreateInfo.usage = usage;
604
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600605 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800606
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600607 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600608 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600609 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600610 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600611 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800612}
613
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600614VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800615{
616 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600617 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800618}
619
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600620VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800621{
622 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600623 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800624}
625
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600626VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600627{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600628 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600629 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600630 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600631
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600632 /* Build command buffer to copy staging texture to usable texture */
633 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600634 assert(!err);
635
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600636 /* TODO: Can we determine image aspect from image object? */
637 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600638 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600639
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600640 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600641 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600642
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600643 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600644 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600645 copy_region.srcSubresource.arraySlice = 0;
646 copy_region.srcSubresource.mipLevel = 0;
647 copy_region.srcOffset.x = 0;
648 copy_region.srcOffset.y = 0;
649 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600650 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600651 copy_region.destSubresource.arraySlice = 0;
652 copy_region.destSubresource.mipLevel = 0;
653 copy_region.destOffset.x = 0;
654 copy_region.destOffset.y = 0;
655 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600656 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600657
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600658 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600659 src_image.obj(), src_image.layout(),
660 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600661 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600662
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600663 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600664
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600665 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600666
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600667 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600668 assert(!err);
669
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600670 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600671
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600672 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600673}
674
Tony Barbour6918cd52015-04-09 12:58:51 -0600675VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
676 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700677{
678 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600679 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600680 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600681 void *data;
682 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600683 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600684 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600685
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600686 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600687 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600688
689 if (colors == NULL)
690 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700691
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800692 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700693
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600694 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600695 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600696 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600697 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600698 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600699 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600700 view.channels.r = VK_CHANNEL_SWIZZLE_R;
701 view.channels.g = VK_CHANNEL_SWIZZLE_G;
702 view.channels.b = VK_CHANNEL_SWIZZLE_B;
703 view.channels.a = VK_CHANNEL_SWIZZLE_A;
704 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600705 view.subresourceRange.baseMipLevel = 0;
706 view.subresourceRange.mipLevels = 1;
707 view.subresourceRange.baseArraySlice = 0;
708 view.subresourceRange.arraySize = 1;
709 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700710
Tony Barboure2c58df2014-11-25 13:18:32 -0700711 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600712 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700713
714 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800715 view.image = obj();
716 m_textureView.init(*m_device, view);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800717 m_descriptorInfo.imageView = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700718
Tony Barbour5dc515d2015-04-01 17:47:06 -0600719 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700720
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800721 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700722 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800723 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600724 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700725 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600726 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600727 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700728}
Tony Barbour82c39522014-12-04 14:33:33 -0700729
Tony Barbour6918cd52015-04-09 12:58:51 -0600730VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700731{
Tony Barboure2c58df2014-11-25 13:18:32 -0700732 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700733
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600734 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800735 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600736 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
737 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
738 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600739 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600740 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
741 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
742 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800743 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600744 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600745 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800746 samplerCreateInfo.minLod = 0.0;
747 samplerCreateInfo.maxLod = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600748 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700749
Chia-I Wue9864b52014-12-28 16:32:24 +0800750 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700751}
Tony Barboure2c58df2014-11-25 13:18:32 -0700752
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700753/*
754 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
755 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600756VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700757{
758 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700759 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700760
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800761 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700762}
763
Tony Barbour6918cd52015-04-09 12:58:51 -0600764VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600765{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600766 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600767 if (m_commandBuffer) {
768 delete m_commandBuffer;
769 }
770}
771
Tony Barbour6918cd52015-04-09 12:58:51 -0600772VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700773{
Tony Barboure2c58df2014-11-25 13:18:32 -0700774 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800775 m_commandBuffer = 0;
776
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800777 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700778 m_numVertices = constantCount;
779 m_stride = constantSize;
780
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600781 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800782 const size_t allocationSize = constantCount * constantSize;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600783 init(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700784
Chia-I Wua07fee62014-12-28 15:26:08 +0800785 void *pData = map();
786 memcpy(pData, data, allocationSize);
787 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700788
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800789 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600790 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600791 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800792 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600793 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800794 view_info.offset = 0;
795 view_info.range = allocationSize;
796 m_bufferView.init(*m_device, view_info);
797
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800798 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700799}
Tony Barbour82c39522014-12-04 14:33:33 -0700800
Tony Barbourd1c35722015-04-16 15:59:00 -0600801void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700802{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600803 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700804}
805
806
Tony Barbour6918cd52015-04-09 12:58:51 -0600807void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600808 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600809 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600810 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
811 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
812 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
813 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600814 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600815 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600816 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
817 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
818 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
819 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
820 VK_MEMORY_INPUT_SHADER_READ_BIT |
821 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
822 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
823 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700824{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600825 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700826
Tony Barbour38422802014-12-10 14:36:31 -0700827 if (!m_commandBuffer)
828 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600829 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700830
Tony Barbour6918cd52015-04-09 12:58:51 -0600831 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700832 }
833 else
834 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800835 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700836 }
837
Tony Barboure2c58df2014-11-25 13:18:32 -0700838 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600839 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600840 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600841 cmd_buf_info.pNext = NULL;
842 cmd_buf_info.flags = 0;
843
Jon Ashburnc4164b12014-12-31 17:10:47 -0700844 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600845 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700846
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600847 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000848 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600849 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700850
Tony Barbourd1c35722015-04-16 15:59:00 -0600851 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000852
853 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600854 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700855
856 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700857 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600858 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700859
Tony Barboure2c58df2014-11-25 13:18:32 -0700860 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600861 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700862 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600863 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
864 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700865}
866
Tony Barbour6918cd52015-04-09 12:58:51 -0600867VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
868 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700869{
870
871}
872
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600873void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700874{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600875 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700876
877 m_numVertices = numIndexes;
878 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700879 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600880 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700881 m_stride = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -0600882 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700883 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600884 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700885 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600886 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700887 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600888 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700889 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600890 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700891 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800892 default:
893 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600894 m_stride = 2;
895 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800896 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700897 }
898
Chia-I Wua07fee62014-12-28 15:26:08 +0800899 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600900 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
901 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700902
Chia-I Wua07fee62014-12-28 15:26:08 +0800903 void *pData = map();
904 memcpy(pData, data, allocationSize);
905 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700906
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800907 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600908 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600909 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800910 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600911 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700912 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800913 view_info.offset = 0;
914 view_info.range = allocationSize;
915 m_bufferView.init(*m_device, view_info);
916
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800917 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700918}
919
Tony Barbourd1c35722015-04-16 15:59:00 -0600920void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700921{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600922 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700923}
Tony Barboure2c58df2014-11-25 13:18:32 -0700924
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600925VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -0700926{
927 return m_indexType;
928}
929
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600930VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700931{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600932 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600933 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700934 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800935 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700936 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600937 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700938
Tony Barboure2c58df2014-11-25 13:18:32 -0700939 return stageInfo;
940}
941
Tony Barbourd1c35722015-04-16 15:59:00 -0600942VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -0700943{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600944 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600945 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600946 VkShaderCreateInfo createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700947 size_t shader_len;
948
949 m_stage = stage;
950 m_device = device;
951
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600952 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700953 createInfo.pNext = NULL;
954
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600955 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700956
957 shader_len = strlen(shader_code);
958 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
959 createInfo.pCode = malloc(createInfo.codeSize);
960 createInfo.flags = 0;
961
Tony Barbourd1c35722015-04-16 15:59:00 -0600962 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600963 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700964 ((uint32_t *) createInfo.pCode)[1] = 0;
965 ((uint32_t *) createInfo.pCode)[2] = stage;
966 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
967
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800968 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700969 }
970
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600971 if (framework->m_use_spv || err) {
972 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600973 err = VK_SUCCESS;
Tony Barboure2c58df2014-11-25 13:18:32 -0700974
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600975 // Use Reference GLSL to SPV compiler
976 framework->GLSLtoSPV(stage, shader_code, spv);
977 createInfo.pCode = spv.data();
978 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700979 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700980
Cody Northrop475663c2015-04-15 11:19:06 -0600981 err = init_try(*m_device, createInfo);
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800982 }
Cody Northrop475663c2015-04-15 11:19:06 -0600983
984 assert(VK_SUCCESS == err);
Tony Barbourf325bf12014-12-03 15:59:38 -0700985}
Tony Barbour82c39522014-12-04 14:33:33 -0700986
Tony Barbour6918cd52015-04-09 12:58:51 -0600987VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700988{
Tony Barboure2c58df2014-11-25 13:18:32 -0700989 m_device = device;
990 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
991 m_vertexBufferCount = 0;
992
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600993 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
994 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600995 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600996 m_ia_state.disableVertexReuse = VK_FALSE;
997 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700998 m_ia_state.primitiveRestartIndex = 0;
999
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001000 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001001 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001002 m_rs_state.depthClipEnable = VK_FALSE;
1003 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
1004 m_rs_state.programPointSize = VK_FALSE;
1005 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1006 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001007 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001008 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1009 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001010
Tony Barboure2c58df2014-11-25 13:18:32 -07001011 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001012 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001013 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001014 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1015 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001016
Tony Barbourf52346d2015-01-16 14:27:35 -07001017 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001018 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1019 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001020 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1021 m_ms_state.samples = 1;
1022 m_ms_state.minSampleShading = 0;
1023 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001024
Tony Barbourd81b8882015-05-15 09:37:57 -06001025 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
1026 m_vp_state.pNext = &m_ms_state;
1027 m_vp_state.viewportCount = 1;
1028 m_vp_state.depthMode = VK_DEPTH_MODE_ZERO_TO_ONE;
1029 m_vp_state.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1030
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001031 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -06001032 m_ds_state.pNext = &m_vp_state,
Tony Barbourd1c35722015-04-16 15:59:00 -06001033 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001034 m_ds_state.depthTestEnable = VK_FALSE;
1035 m_ds_state.depthWriteEnable = VK_FALSE;
1036 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001037 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001038 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1039 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1040 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001041 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001042 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001043 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001044
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001045 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001046 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001047 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001048 att.channelWriteMask = 0xf;
1049 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001050
1051};
1052
Tony Barbour6918cd52015-04-09 12:58:51 -06001053void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001054{
1055 m_shaderObjs.push_back(shader);
1056}
1057
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001058void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001059{
1060 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1061 m_vi_state.attributeCount = count;
1062}
1063
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001064void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001065{
1066 m_vi_state.pVertexBindingDescriptions = vi_binding;
1067 m_vi_state.bindingCount = count;
1068}
1069
Tony Barbour6918cd52015-04-09 12:58:51 -06001070void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001071{
1072 m_vertexBufferObjs.push_back(vertexDataBuffer);
1073 m_vertexBufferBindings.push_back(binding);
1074 m_vertexBufferCount++;
1075}
1076
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001077void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001078{
Tony Barbourf52346d2015-01-16 14:27:35 -07001079 if (binding+1 > m_colorAttachments.size())
1080 {
1081 m_colorAttachments.resize(binding+1);
1082 }
1083 m_colorAttachments[binding] = *att;
1084}
1085
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001086void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001087{
1088 m_ds_state.format = ds_state->format;
1089 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1090 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1091 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001092 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001093 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1094 m_ds_state.back = ds_state->back;
1095 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001096}
1097
Chris Forbes95292b12015-05-25 11:13:26 +12001098VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001099{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001100 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001101 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001102
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001103 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001104
1105 for (int i=0; i<m_shaderObjs.size(); i++)
1106 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001107 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001108 shaderCreateInfo->pNext = head_ptr;
1109 head_ptr = shaderCreateInfo;
1110 }
1111
1112 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1113 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001114 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001115 m_vi_state.pNext = head_ptr;
1116 head_ptr = &m_vi_state;
1117 }
1118
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001119 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1120 info.pNext = head_ptr;
1121 info.flags = 0;
1122 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001123
Tony Barbourf52346d2015-01-16 14:27:35 -07001124 m_cb_state.attachmentCount = m_colorAttachments.size();
1125 m_cb_state.pAttachments = &m_colorAttachments[0];
1126
Chris Forbes95292b12015-05-25 11:13:26 +12001127 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001128}
Chia-I Wu2648d092014-12-29 14:24:14 +08001129
Tony Barbourd1c35722015-04-16 15:59:00 -06001130vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001131{
Tony Barbourd1c35722015-04-16 15:59:00 -06001132 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001133 if (this->mem_refs_.size()) {
1134 mems.reserve(this->mem_refs_.size());
1135 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1136 mems.push_back(this->mem_refs_[i]);
1137 }
1138
1139 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001140}
1141
Tony Barbour6918cd52015-04-09 12:58:51 -06001142VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001143 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001144{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001145 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001146}
Tony Barbour471338d2014-12-10 17:28:39 -07001147
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001148VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001149{
Chia-I Wud28343c2014-12-28 15:12:48 +08001150 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001151}
Tony Barbour471338d2014-12-10 17:28:39 -07001152
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001153VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001154{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001155 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001156 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001157}
1158
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001159VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001160{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001161 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001162 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001163}
1164
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001165VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001166{
1167 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001168 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001169}
1170
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001171VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001172{
Chia-I Wud28343c2014-12-28 15:12:48 +08001173 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001174 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001175}
1176
Tony Barbourd1c35722015-04-16 15:59:00 -06001177void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001178{
Tony Barbourd1c35722015-04-16 15:59:00 -06001179 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001180}
1181
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001182void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001183 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001184{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001185 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001186 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001187 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001188 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1189 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1190 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001191 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001192 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001193
1194 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001195 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001196 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001197 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001198 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001199 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001200 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001201
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001202 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001203 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001204 memory_barrier.outputMask = output_mask;
1205 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001206 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001207 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001208 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001209
Tony Barbourd1c35722015-04-16 15:59:00 -06001210 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001211
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001212 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001213 memory_barrier.image = m_renderTargets[i]->image();
1214 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001215 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001216 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001217
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001218 vkCmdClearColorImage(obj(),
1219 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001220 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001221
Tony Barbour30cc9e82014-12-17 11:53:55 -07001222 }
1223
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001224 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001225 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001226 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001227 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001228 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001229 dsRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001230 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001231 dsRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001232
1233 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001234
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001235 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001236 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001237 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001238 memory_barrier.subresourceRange = dsRange;
1239
Tony Barbourd1c35722015-04-16 15:59:00 -06001240 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001241
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001242 vkCmdClearDepthStencil(obj(),
1243 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001244 depth_clear_color, stencil_clear_color,
1245 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001246
1247 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001248 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001249 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001250 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001251 memory_barrier.subresourceRange = dsRange;
Tony Barbourd1c35722015-04-16 15:59:00 -06001252 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001253 }
1254}
1255
Tony Barbour6918cd52015-04-09 12:58:51 -06001256void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001257{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001258 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001259 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001260 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001261 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1262 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1263 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001264 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001265 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001266 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001267 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1268 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1269 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1270 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1271 VK_MEMORY_INPUT_SHADER_READ_BIT |
1272 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1273 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001274 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001275
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001276 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001277 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001278 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001279 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001280 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001281 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001282
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001283 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001284 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001285 memory_barrier.outputMask = output_mask;
1286 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001287 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001288 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001289 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001290
Tony Barbourd1c35722015-04-16 15:59:00 -06001291 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001292
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001293 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001294 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001295 memory_barrier.image = m_renderTargets[i]->image();
1296 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001297 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001298 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001299 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001300}
1301
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001302void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001303{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001304 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001305 renderpass,
1306 framebuffer,
1307 };
1308
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001309 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001310}
1311
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001312void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001313{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001314 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001315}
1316
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001317void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001318{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001319 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001320}
1321
Tony Barbour6918cd52015-04-09 12:58:51 -06001322void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001323{
1324 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001325}
1326
Tony Barbour6918cd52015-04-09 12:58:51 -06001327void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001328{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001329 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001330}
1331
Tony Barbour6918cd52015-04-09 12:58:51 -06001332void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001333{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001334 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001335}
1336
Tony Barbour6918cd52015-04-09 12:58:51 -06001337void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001338{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001339 QueueCommandBuffer(NULL);
1340}
1341
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001342void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001343{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001344 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001345
1346 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001347 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1348 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001349
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001350 err = vkQueueWaitIdle( m_device->m_queue );
1351 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001352
1353 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001354 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001355}
1356
Tony Barbour6918cd52015-04-09 12:58:51 -06001357void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001358{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001359 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001360}
1361
Tony Barbour6918cd52015-04-09 12:58:51 -06001362void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001363{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001364 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001365
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001366 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001367 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropd4c1a502015-04-16 13:41:56 -06001368 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001369}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001370
Tony Barbour6918cd52015-04-09 12:58:51 -06001371void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001372{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001373 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001374}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001375
Tony Barbourd1c35722015-04-16 15:59:00 -06001376void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001377{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001378 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001379}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001380
Tony Barbour6918cd52015-04-09 12:58:51 -06001381VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001382{
1383 m_initialized = false;
1384}
Tony Barbour6918cd52015-04-09 12:58:51 -06001385bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001386{
1387 return m_initialized;
1388}
1389
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001390VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001391{
1392 return &m_depthStencilBindInfo;
1393}
1394
Tony Barbour6918cd52015-04-09 12:58:51 -06001395void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001396{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001397 VkImageCreateInfo image_info;
1398 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001399
1400 m_device = device;
1401 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001402 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001403
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001404 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001405 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001406 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001407 image_info.format = m_depth_stencil_fmt;
1408 image_info.extent.width = width;
1409 image_info.extent.height = height;
1410 image_info.extent.depth = 1;
1411 image_info.mipLevels = 1;
1412 image_info.arraySize = 1;
1413 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001414 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001415 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001416 image_info.flags = 0;
1417 init(*m_device, image_info);
1418
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001419 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001420 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001421 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001422 view_info.mipLevel = 0;
1423 view_info.baseArraySlice = 0;
1424 view_info.arraySize = 1;
1425 view_info.flags = 0;
1426 view_info.image = obj();
1427 m_depthStencilView.init(*m_device, view_info);
1428
1429 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001430 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001431}