blob: 1b1e3da8030820eee4a4449fc1f08dc4079f8193 [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 ),
Chris Forbese182fe02015-06-15 09:32:35 +120044 m_clear_via_load_op( false ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070045 m_depth_clear_color( 1.0 ),
46 m_stencil_clear_color( 0 )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060047{
Tony Barbour1c45ce02015-03-27 17:03:18 -060048
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070049 // clear the back buffer to dark grey
50 m_clear_color.color.rawColor[0] = 64;
51 m_clear_color.color.rawColor[1] = 64;
52 m_clear_color.color.rawColor[2] = 64;
53 m_clear_color.color.rawColor[3] = 0;
54 m_clear_color.useRawValue = true;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060055}
56
Tony Barbour6918cd52015-04-09 12:58:51 -060057VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060058{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060059
60}
61
Tony Barbour6918cd52015-04-09 12:58:51 -060062void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060063{
Tony Barbour9b9fe782015-04-23 15:28:27 -060064 const std::vector<const char *> extensions;
65 InitFramework(extensions);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060066}
67
Tony Barbour15524c32015-04-29 17:34:29 -060068void VkRenderFramework::InitFramework(const std::vector<const char *> &extensions, VK_DBG_MSG_CALLBACK_FUNCTION dbgFunction, void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060069{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060070 VkResult err;
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060071 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060072 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060073 instInfo.pNext = NULL;
74 instInfo.pAppInfo = &app_info;
75 instInfo.pAllocCb = NULL;
Tony Barbour9b9fe782015-04-23 15:28:27 -060076 instInfo.extensionCount = extensions.size();
77 instInfo.ppEnabledExtensionNames = (extensions.size()) ? &extensions[0] : NULL;;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060078 err = vkCreateInstance(&instInfo, &this->inst);
79 ASSERT_VK_SUCCESS(err);
Jon Ashburn83a64252015-04-15 11:31:12 -060080 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
81 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
82 ASSERT_VK_SUCCESS(err);
83 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060084 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070085 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -060086 if (dbgFunction) {
87 err = vkDbgRegisterMsgCallback(this->inst, dbgFunction, userData);
88 ASSERT_VK_SUCCESS(err);
89 }
Tony Barbour9b9fe782015-04-23 15:28:27 -060090 m_device = new VkDeviceObj(0, objs[0]);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060091 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -060092
Tony Barbour6918cd52015-04-09 12:58:51 -060093 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060094}
95
Tony Barbour6918cd52015-04-09 12:58:51 -060096void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060097{
Mike Stroyanb050c682015-04-17 12:36:38 -060098 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
99 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
100 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
101 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
102 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
103 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600104
105 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600106 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600107 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600108 while (!m_renderTargets.empty()) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600109 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
Mike Stroyanb050c682015-04-17 12:36:38 -0600110 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
111 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600112 m_renderTargets.pop_back();
113 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600114
Tony Barbour1c45ce02015-03-27 17:03:18 -0600115 delete m_depthStencil;
116
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600117 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800118 delete m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600119 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600120}
121
Tony Barbour6918cd52015-04-09 12:58:51 -0600122void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600123{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600124 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600125
Tony Barbourd1c35722015-04-16 15:59:00 -0600126 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600127
128 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600129 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600130 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700131 raster.pointSize = 1.0;
132
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600133 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
134 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600135
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600136 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600137 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600138 blend.blendConst[0] = 1.0f;
139 blend.blendConst[1] = 1.0f;
140 blend.blendConst[2] = 1.0f;
141 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600142 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
143 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600144
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600145 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600146 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600147 depthStencil.minDepth = 0.f;
148 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700149 depthStencil.stencilFrontRef = 0;
150 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600151 depthStencil.stencilReadMask = 0xff;
152 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600153
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600154 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
155 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600156
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600157 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600158
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600159 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700160 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
161
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600162 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
163 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600164}
165
Tony Barbour6918cd52015-04-09 12:58:51 -0600166void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600167{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600168 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600169
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600170 VkViewport viewport;
171 VkRect scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600172
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600173 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600174 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700175 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700176 viewport.originX = 0;
177 viewport.originY = 0;
178 viewport.width = 1.f * width;
179 viewport.height = 1.f * height;
180 viewport.minDepth = 0.f;
181 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600182 scissor.extent.width = (int32_t) width;
183 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700184 scissor.offset.x = 0;
185 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700186 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700187 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700188
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600189 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
190 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600191 m_width = width;
192 m_height = height;
193}
194
Tony Barbour6918cd52015-04-09 12:58:51 -0600195void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600196{
197 InitViewport(m_width, m_height);
198}
Tony Barbour6918cd52015-04-09 12:58:51 -0600199void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600200{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700201 InitRenderTarget(1);
202}
203
Tony Barbour6918cd52015-04-09 12:58:51 -0600204void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700205{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600206 InitRenderTarget(targets, NULL);
207}
208
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600209void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600210{
211 InitRenderTarget(1, dsBinding);
212}
213
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600214void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600215{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600216 std::vector<VkAttachmentLoadOp> load_ops;
217 std::vector<VkAttachmentStoreOp> store_ops;
218 std::vector<VkClearColor> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600219
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600220 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800221
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700222 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600223 VkImageObj *img = new VkImageObj(m_device);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600224
225 VkFormatProperties props;
226 size_t size = sizeof(props);
227 VkResult err;
228
229 err = vkGetFormatInfo(m_device->obj(), m_render_target_fmt,
230 VK_FORMAT_INFO_TYPE_PROPERTIES,
231 &size, &props);
232 ASSERT_VK_SUCCESS(err);
233
234 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600235 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600236 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_LINEAR);
237 }
238 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600239 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600240 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL);
241 }
242 else {
243 FAIL() << "Neither Linear nor Optimal allowed for render target";
244 }
245
246 m_renderTargets.push_back(img);
Tony Barbourf52346d2015-01-16 14:27:35 -0700247 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600248 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chris Forbese182fe02015-06-15 09:32:35 +1200249 load_ops.push_back(m_clear_via_load_op ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600250 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600251 clear_colors.push_back(m_clear_color);
Chia-I Wuecebf752014-12-05 10:45:15 +0800252 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600253
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700254 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600255 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600256 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600257 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700258 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600259 fb_info.pColorAttachments = m_colorBindings;
260 fb_info.pDepthStencilAttachment = dsBinding;
261 fb_info.sampleCount = 1;
262 fb_info.width = (uint32_t)m_width;
263 fb_info.height = (uint32_t)m_height;
264 fb_info.layers = 1;
265
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600266 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600267
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600268 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600269 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600270 rp_info.renderArea.extent.width = (uint32_t) m_width;
271 rp_info.renderArea.extent.height = (uint32_t) m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600272
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700273 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600274 rp_info.pColorFormats = &m_render_target_fmt;
275 rp_info.pColorLayouts = &m_colorBindings[0].layout;
276 rp_info.pColorLoadOps = &load_ops[0];
277 rp_info.pColorStoreOps = &store_ops[0];
278 rp_info.pColorLoadClearValues = &clear_colors[0];
279 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600280 if (dsBinding) {
281 rp_info.depthStencilLayout = dsBinding->layout;
282 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600283 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600284 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600285 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
286 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600287 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600288 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Tony Barbour51a05ce2015-06-03 11:40:51 -0600289 rp_info.sampleCount = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600290 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600291}
292
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600293
294
Tony Barbourd1c35722015-04-16 15:59:00 -0600295VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600296 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800297{
298 init();
299
300 props = gpu().properties();
301 queue_props = &gpu().queue_properties()[0];
302}
303
Tony Barbour6918cd52015-04-09 12:58:51 -0600304void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800305{
306 ASSERT_NE(true, graphics_queues().empty());
307 m_queue = graphics_queues()[0]->obj();
308}
309
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600310VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
311 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700312{
Tony Barboure2c58df2014-11-25 13:18:32 -0700313
314}
315
Tony Barbour6918cd52015-04-09 12:58:51 -0600316VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700317{
Chia-I Wu11078b02015-01-04 16:27:24 +0800318 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700319}
Tony Barbour82c39522014-12-04 14:33:33 -0700320
Tony Barbour6918cd52015-04-09 12:58:51 -0600321int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700322{
Chia-I Wu11078b02015-01-04 16:27:24 +0800323 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600324 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600325 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800326 tc.count = 1;
327 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700328
Chia-I Wu11078b02015-01-04 16:27:24 +0800329 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700330}
Tony Barbour82c39522014-12-04 14:33:33 -0700331
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600332int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700333{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600334 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800335 tc.type = type;
336 tc.count = 1;
337 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700338
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800339 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
340 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600341
Chia-I Wu11078b02015-01-04 16:27:24 +0800342 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700343}
Tony Barbour82c39522014-12-04 14:33:33 -0700344
Tony Barbour6918cd52015-04-09 12:58:51 -0600345int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700346{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600347 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600348 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800349 tc.count = 1;
350 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700351
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800352 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600353 tmp.sampler = sampler->obj();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800354 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700355
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800356 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
357 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700358
Chia-I Wu11078b02015-01-04 16:27:24 +0800359 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700360}
Chia-I Wu11078b02015-01-04 16:27:24 +0800361
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500362VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700363{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500364 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700365}
366
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600367VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700368{
Chia-I Wu11078b02015-01-04 16:27:24 +0800369 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700370}
Tony Barboure2c58df2014-11-25 13:18:32 -0700371
Tony Barbour6918cd52015-04-09 12:58:51 -0600372void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700373{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600374 // create VkDescriptorPool
375 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600376 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800377 pool.count = m_type_counts.size();
378 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600379 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700380
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600381 // create VkDescriptorSetLayout
382 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800383 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800384 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800385 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800386 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600387 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800388 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700389 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800390
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600391 // create VkDescriptorSetLayout
392 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600393 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800394 layout.count = bindings.size();
395 layout.pBinding = &bindings[0];
396
Chia-I Wu41126e52015-03-26 15:27:55 +0800397 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600398 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800399 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500400
401 // create VkPipelineLayout
402 VkPipelineLayoutCreateInfo pipeline_layout = {};
403 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
404 pipeline_layout.descriptorSetCount = layouts.size();
405 pipeline_layout.pSetLayouts = NULL;
406
407 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700408
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600409 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500410 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800411
Chia-I Wu41126e52015-03-26 15:27:55 +0800412 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800413 size_t imageSamplerCount = 0;
414 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
415 it != m_writes.end(); it++) {
416 it->destSet = m_set->obj();
417 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
418 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800419 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800420
421 // do the updates
Chia-I Wu11078b02015-01-04 16:27:24 +0800422 clear_sets(*m_set);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800423 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700424}
Tony Barboure2c58df2014-11-25 13:18:32 -0700425
Tony Barbour6918cd52015-04-09 12:58:51 -0600426VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800427{
428 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800429 m_descriptorInfo.imageView = VK_NULL_HANDLE;
430 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800431}
432
Tony Barbour6918cd52015-04-09 12:58:51 -0600433void VkImageObj::ImageMemoryBarrier(
434 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600435 VkImageAspect aspect,
436 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600437 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600438 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
439 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
440 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
441 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600442 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600443 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600444 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
445 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
446 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
447 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
448 VK_MEMORY_INPUT_SHADER_READ_BIT |
449 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
450 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
451 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600452 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600453{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600454 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
455 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600456 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
457 subresourceRange);
458
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600459 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600460
Tony Barbourd1c35722015-04-16 15:59:00 -0600461 VkPipeEvent pipe_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600462
463 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600464 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 -0600465}
466
Tony Barbour6918cd52015-04-09 12:58:51 -0600467void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600468 VkImageAspect aspect,
469 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600470{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600471 VkFlags output_mask, input_mask;
472 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600473 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600474 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
475 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
476 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600477 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600478 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600479 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600480 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
481 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
482 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
483 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
484 VK_MEMORY_INPUT_SHADER_READ_BIT |
485 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
486 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600487 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600488
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800489 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600490 return;
491 }
492
493 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600494 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600495 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
496 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600497 break;
498
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600499 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600500 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
501 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600502 break;
503
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600504 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600505 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
506 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600507 break;
508
Tony Barbourf20f87b2015-04-22 09:02:32 -0600509 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600510 default:
511 output_mask = all_cache_outputs;
512 input_mask = all_cache_inputs;
513 break;
514 }
515
516 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800517 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600518}
519
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600520void VkImageObj::SetLayout(VkImageAspect aspect,
521 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600522{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600523 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600524
525 if (image_layout == m_descriptorInfo.imageLayout) {
526 return;
527 }
528
Tony Barbour6918cd52015-04-09 12:58:51 -0600529 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600530
531 /* Build command buffer to set image layout in the driver */
532 err = cmd_buf.BeginCommandBuffer();
533 assert(!err);
534
535 SetLayout(&cmd_buf, aspect, image_layout);
536
537 err = cmd_buf.EndCommandBuffer();
538 assert(!err);
539
540 cmd_buf.QueueCommandBuffer();
541}
542
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600543bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600544{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600545 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600546 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600547 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600548
Tony Barbour579f7802015-04-03 15:11:43 -0600549 return true;
550}
551
Tony Barbour6918cd52015-04-09 12:58:51 -0600552void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600553 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600554 VkImageTiling requested_tiling,
555 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800556{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600557 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600558 VkFormatProperties image_fmt;
559 VkImageTiling tiling;
560 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600561 size_t size;
562
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800563 mipCount = 0;
564
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600565 uint32_t _w = w;
566 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800567 while( ( _w > 0 ) || ( _h > 0 ) )
568 {
569 _w >>= 1;
570 _h >>= 1;
571 mipCount++;
572 }
573
Tony Barbour579f7802015-04-03 15:11:43 -0600574 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600575 err = vkGetFormatInfo(m_device->obj(), fmt,
Tony Barbourd1c35722015-04-16 15:59:00 -0600576 VK_FORMAT_INFO_TYPE_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600577 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600578 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600579
Tony Barbourd1c35722015-04-16 15:59:00 -0600580 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600581 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600582 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600583 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600584 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600585 } else {
586 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
587 }
588 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600589 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600590 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600591 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600592 } else {
593 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
594 }
595
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600596 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600597 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800598 imageCreateInfo.format = fmt;
599 imageCreateInfo.extent.width = w;
600 imageCreateInfo.extent.height = h;
601 imageCreateInfo.mipLevels = mipCount;
602 imageCreateInfo.tiling = tiling;
603
604 imageCreateInfo.usage = usage;
605
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600606 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800607
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600608 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600609 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600610 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600611 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600612 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800613}
614
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600615VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800616{
617 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600618 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800619}
620
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600621VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800622{
623 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600624 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800625}
626
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600627VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600628{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600629 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600630 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600631 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600632
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600633 /* Build command buffer to copy staging texture to usable texture */
634 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600635 assert(!err);
636
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600637 /* TODO: Can we determine image aspect from image object? */
638 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600639 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600640
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600641 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600642 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600643
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600644 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600645 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600646 copy_region.srcSubresource.arraySlice = 0;
647 copy_region.srcSubresource.mipLevel = 0;
648 copy_region.srcOffset.x = 0;
649 copy_region.srcOffset.y = 0;
650 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600651 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600652 copy_region.destSubresource.arraySlice = 0;
653 copy_region.destSubresource.mipLevel = 0;
654 copy_region.destOffset.x = 0;
655 copy_region.destOffset.y = 0;
656 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600657 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600658
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600659 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600660 src_image.obj(), src_image.layout(),
661 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600662 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600663
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600664 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600665
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600666 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600667
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600668 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600669 assert(!err);
670
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600671 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600672
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600673 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600674}
675
Tony Barbour6918cd52015-04-09 12:58:51 -0600676VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
677 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700678{
679 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600680 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600681 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600682 void *data;
683 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600684 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600685 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600686
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600687 stagingImage.init(16, 16, tex_format, 0, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600688 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600689
690 if (colors == NULL)
691 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700692
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800693 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700694
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600695 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600696 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600697 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600698 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600699 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600700 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600701 view.channels.r = VK_CHANNEL_SWIZZLE_R;
702 view.channels.g = VK_CHANNEL_SWIZZLE_G;
703 view.channels.b = VK_CHANNEL_SWIZZLE_B;
704 view.channels.a = VK_CHANNEL_SWIZZLE_A;
705 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600706 view.subresourceRange.baseMipLevel = 0;
707 view.subresourceRange.mipLevels = 1;
708 view.subresourceRange.baseArraySlice = 0;
709 view.subresourceRange.arraySize = 1;
710 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700711
Tony Barboure2c58df2014-11-25 13:18:32 -0700712 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600713 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700714
715 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800716 view.image = obj();
717 m_textureView.init(*m_device, view);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800718 m_descriptorInfo.imageView = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700719
Tony Barbour5dc515d2015-04-01 17:47:06 -0600720 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700721
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800722 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700723 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800724 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600725 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700726 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600727 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600728 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700729}
Tony Barbour82c39522014-12-04 14:33:33 -0700730
Tony Barbour6918cd52015-04-09 12:58:51 -0600731VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700732{
Tony Barboure2c58df2014-11-25 13:18:32 -0700733 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700734
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600735 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800736 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600737 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
738 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
739 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600740 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600741 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
742 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
743 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800744 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600745 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600746 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800747 samplerCreateInfo.minLod = 0.0;
748 samplerCreateInfo.maxLod = 0.0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600749 samplerCreateInfo.borderColor = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700750
Chia-I Wue9864b52014-12-28 16:32:24 +0800751 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700752}
Tony Barboure2c58df2014-11-25 13:18:32 -0700753
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700754/*
755 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
756 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600757VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700758{
759 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700760 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700761
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800762 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700763}
764
Tony Barbour6918cd52015-04-09 12:58:51 -0600765VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600766{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600767 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600768 if (m_commandBuffer) {
769 delete m_commandBuffer;
770 }
771}
772
Tony Barbour6918cd52015-04-09 12:58:51 -0600773VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700774{
Tony Barboure2c58df2014-11-25 13:18:32 -0700775 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800776 m_commandBuffer = 0;
777
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800778 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700779 m_numVertices = constantCount;
780 m_stride = constantSize;
781
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600782 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800783 const size_t allocationSize = constantCount * constantSize;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600784 init(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700785
Chia-I Wua07fee62014-12-28 15:26:08 +0800786 void *pData = map();
787 memcpy(pData, data, allocationSize);
788 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700789
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800790 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600791 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600792 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800793 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600794 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800795 view_info.offset = 0;
796 view_info.range = allocationSize;
797 m_bufferView.init(*m_device, view_info);
798
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800799 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700800}
Tony Barbour82c39522014-12-04 14:33:33 -0700801
Tony Barbourd1c35722015-04-16 15:59:00 -0600802void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700803{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600804 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700805}
806
807
Tony Barbour6918cd52015-04-09 12:58:51 -0600808void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600809 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600810 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600811 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
812 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
813 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
814 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600815 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600816 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600817 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
818 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
819 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
820 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
821 VK_MEMORY_INPUT_SHADER_READ_BIT |
822 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
823 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
824 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700825{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600826 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700827
Tony Barbour38422802014-12-10 14:36:31 -0700828 if (!m_commandBuffer)
829 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600830 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700831
Tony Barbour6918cd52015-04-09 12:58:51 -0600832 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700833 }
834 else
835 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800836 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700837 }
838
Tony Barboure2c58df2014-11-25 13:18:32 -0700839 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600840 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600841 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600842 cmd_buf_info.pNext = NULL;
843 cmd_buf_info.flags = 0;
844
Jon Ashburnc4164b12014-12-31 17:10:47 -0700845 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600846 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700847
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600848 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000849 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600850 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700851
Tony Barbourd1c35722015-04-16 15:59:00 -0600852 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000853
854 // write barrier to the command buffer
Tony Barbourd1c35722015-04-16 15:59:00 -0600855 m_commandBuffer->PipelineBarrier(VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700856
857 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700858 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600859 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700860
Tony Barboure2c58df2014-11-25 13:18:32 -0700861 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600862 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700863 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600864 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
865 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700866}
867
Tony Barbour6918cd52015-04-09 12:58:51 -0600868VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
869 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700870{
871
872}
873
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600874void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700875{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600876 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700877
878 m_numVertices = numIndexes;
879 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700880 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600881 case VK_INDEX_TYPE_UINT8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700882 m_stride = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -0600883 viewFormat = VK_FORMAT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700884 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600885 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700886 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600887 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700888 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600889 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700890 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600891 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700892 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800893 default:
894 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600895 m_stride = 2;
896 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800897 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700898 }
899
Chia-I Wua07fee62014-12-28 15:26:08 +0800900 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600901 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
902 init(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700903
Chia-I Wua07fee62014-12-28 15:26:08 +0800904 void *pData = map();
905 memcpy(pData, data, allocationSize);
906 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700907
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800908 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600909 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600910 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800911 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600912 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700913 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800914 view_info.offset = 0;
915 view_info.range = allocationSize;
916 m_bufferView.init(*m_device, view_info);
917
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800918 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700919}
920
Tony Barbourd1c35722015-04-16 15:59:00 -0600921void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700922{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600923 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700924}
Tony Barboure2c58df2014-11-25 13:18:32 -0700925
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600926VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -0700927{
928 return m_indexType;
929}
930
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600931VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700932{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600933 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600934 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700935 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800936 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700937 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600938 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700939
Tony Barboure2c58df2014-11-25 13:18:32 -0700940 return stageInfo;
941}
942
Tony Barbourd1c35722015-04-16 15:59:00 -0600943VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -0700944{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600945 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600946 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600947 VkShaderCreateInfo createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700948 size_t shader_len;
949
950 m_stage = stage;
951 m_device = device;
952
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600953 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700954 createInfo.pNext = NULL;
955
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600956 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700957
958 shader_len = strlen(shader_code);
959 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
960 createInfo.pCode = malloc(createInfo.codeSize);
961 createInfo.flags = 0;
962
Tony Barbourd1c35722015-04-16 15:59:00 -0600963 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600964 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700965 ((uint32_t *) createInfo.pCode)[1] = 0;
966 ((uint32_t *) createInfo.pCode)[2] = stage;
967 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
968
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800969 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700970 }
971
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600972 if (framework->m_use_spv || err) {
973 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600974 err = VK_SUCCESS;
Tony Barboure2c58df2014-11-25 13:18:32 -0700975
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600976 // Use Reference GLSL to SPV compiler
977 framework->GLSLtoSPV(stage, shader_code, spv);
978 createInfo.pCode = spv.data();
979 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700980 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700981
Cody Northrop475663c2015-04-15 11:19:06 -0600982 err = init_try(*m_device, createInfo);
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800983 }
Cody Northrop475663c2015-04-15 11:19:06 -0600984
985 assert(VK_SUCCESS == err);
Tony Barbourf325bf12014-12-03 15:59:38 -0700986}
Tony Barbour82c39522014-12-04 14:33:33 -0700987
Tony Barbour6918cd52015-04-09 12:58:51 -0600988VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700989{
Tony Barboure2c58df2014-11-25 13:18:32 -0700990 m_device = device;
991 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
992 m_vertexBufferCount = 0;
993
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600994 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
995 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600996 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600997 m_ia_state.disableVertexReuse = VK_FALSE;
998 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700999 m_ia_state.primitiveRestartIndex = 0;
1000
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001001 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001002 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001003 m_rs_state.depthClipEnable = VK_FALSE;
1004 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
1005 m_rs_state.programPointSize = VK_FALSE;
1006 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1007 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001008 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001009 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1010 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001011
Tony Barboure2c58df2014-11-25 13:18:32 -07001012 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001013 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001014 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001015 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1016 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001017
Tony Barbourf52346d2015-01-16 14:27:35 -07001018 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001019 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1020 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001021 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1022 m_ms_state.samples = 1;
1023 m_ms_state.minSampleShading = 0;
1024 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001025
Tony Barbourd81b8882015-05-15 09:37:57 -06001026 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
1027 m_vp_state.pNext = &m_ms_state;
1028 m_vp_state.viewportCount = 1;
1029 m_vp_state.depthMode = VK_DEPTH_MODE_ZERO_TO_ONE;
1030 m_vp_state.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1031
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001032 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -06001033 m_ds_state.pNext = &m_vp_state,
Tony Barbourd1c35722015-04-16 15:59:00 -06001034 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001035 m_ds_state.depthTestEnable = VK_FALSE;
1036 m_ds_state.depthWriteEnable = VK_FALSE;
1037 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001038 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001039 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1040 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1041 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001042 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001043 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001044 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001045
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001046 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001047 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001048 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001049 att.channelWriteMask = 0xf;
1050 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001051
1052};
1053
Tony Barbour6918cd52015-04-09 12:58:51 -06001054void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001055{
1056 m_shaderObjs.push_back(shader);
1057}
1058
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001059void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001060{
1061 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1062 m_vi_state.attributeCount = count;
1063}
1064
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001065void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001066{
1067 m_vi_state.pVertexBindingDescriptions = vi_binding;
1068 m_vi_state.bindingCount = count;
1069}
1070
Tony Barbour6918cd52015-04-09 12:58:51 -06001071void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001072{
1073 m_vertexBufferObjs.push_back(vertexDataBuffer);
1074 m_vertexBufferBindings.push_back(binding);
1075 m_vertexBufferCount++;
1076}
1077
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001078void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001079{
Tony Barbourf52346d2015-01-16 14:27:35 -07001080 if (binding+1 > m_colorAttachments.size())
1081 {
1082 m_colorAttachments.resize(binding+1);
1083 }
1084 m_colorAttachments[binding] = *att;
1085}
1086
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001087void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001088{
1089 m_ds_state.format = ds_state->format;
1090 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1091 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1092 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001093 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001094 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1095 m_ds_state.back = ds_state->back;
1096 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001097}
1098
Chris Forbes95292b12015-05-25 11:13:26 +12001099VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001100{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001101 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001102 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001103
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001104 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001105
1106 for (int i=0; i<m_shaderObjs.size(); i++)
1107 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001108 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001109 shaderCreateInfo->pNext = head_ptr;
1110 head_ptr = shaderCreateInfo;
1111 }
1112
1113 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1114 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001115 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001116 m_vi_state.pNext = head_ptr;
1117 head_ptr = &m_vi_state;
1118 }
1119
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001120 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1121 info.pNext = head_ptr;
1122 info.flags = 0;
1123 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001124
Tony Barbourf52346d2015-01-16 14:27:35 -07001125 m_cb_state.attachmentCount = m_colorAttachments.size();
1126 m_cb_state.pAttachments = &m_colorAttachments[0];
1127
Chris Forbes95292b12015-05-25 11:13:26 +12001128 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001129}
Chia-I Wu2648d092014-12-29 14:24:14 +08001130
Tony Barbourd1c35722015-04-16 15:59:00 -06001131vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001132{
Tony Barbourd1c35722015-04-16 15:59:00 -06001133 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001134 if (this->mem_refs_.size()) {
1135 mems.reserve(this->mem_refs_.size());
1136 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1137 mems.push_back(this->mem_refs_[i]);
1138 }
1139
1140 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001141}
1142
Tony Barbour6918cd52015-04-09 12:58:51 -06001143VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001144 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001145{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001146 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001147}
Tony Barbour471338d2014-12-10 17:28:39 -07001148
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001149VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001150{
Chia-I Wud28343c2014-12-28 15:12:48 +08001151 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001152}
Tony Barbour471338d2014-12-10 17:28:39 -07001153
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001154VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001155{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001156 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001157 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001158}
1159
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001160VkResult VkCommandBufferObj::BeginCommandBuffer(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001161{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001162 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001163 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001164}
1165
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001166VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001167{
1168 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001169 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001170}
1171
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001172VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001173{
Chia-I Wud28343c2014-12-28 15:12:48 +08001174 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001175 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001176}
1177
Tony Barbourd1c35722015-04-16 15:59:00 -06001178void VkCommandBufferObj::PipelineBarrier(VkWaitEvent waitEvent, uint32_t pipeEventCount, const VkPipeEvent* pPipeEvents, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001179{
Tony Barbourd1c35722015-04-16 15:59:00 -06001180 vkCmdPipelineBarrier(obj(), waitEvent, pipeEventCount, pPipeEvents, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001181}
1182
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001183void VkCommandBufferObj::ClearAllBuffers(VkClearColor clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001184 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001185{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001186 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001187 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001188 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001189 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1190 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1191 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001192 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001193 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001194
1195 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001196 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001197 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001198 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001199 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001200 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001201 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001202
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001203 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001204 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001205 memory_barrier.outputMask = output_mask;
1206 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001207 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001208 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001209 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001210
Tony Barbourd1c35722015-04-16 15:59:00 -06001211 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001212
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001213 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001214 memory_barrier.image = m_renderTargets[i]->image();
1215 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001216 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001217 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001218
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001219 vkCmdClearColorImage(obj(),
1220 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001221 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001222
Tony Barbour30cc9e82014-12-17 11:53:55 -07001223 }
1224
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001225 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001226 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001227 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001228 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001229 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001230 dsRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001231 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001232 dsRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001233
1234 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001235
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001236 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001237 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001238 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001239 memory_barrier.subresourceRange = dsRange;
1240
Tony Barbourd1c35722015-04-16 15:59:00 -06001241 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001242
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001243 vkCmdClearDepthStencil(obj(),
1244 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001245 depth_clear_color, stencil_clear_color,
1246 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001247
1248 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001249 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001250 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001251 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001252 memory_barrier.subresourceRange = dsRange;
Tony Barbourd1c35722015-04-16 15:59:00 -06001253 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001254 }
1255}
1256
Tony Barbour6918cd52015-04-09 12:58:51 -06001257void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001258{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001259 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001260 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001261 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001262 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1263 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1264 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001265 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001266 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001267 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001268 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1269 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1270 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1271 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1272 VK_MEMORY_INPUT_SHADER_READ_BIT |
1273 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1274 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001275 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001276
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001277 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001278 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001279 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001280 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001281 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001282 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001283
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001284 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001285 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001286 memory_barrier.outputMask = output_mask;
1287 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001288 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001289 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001290 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001291
Tony Barbourd1c35722015-04-16 15:59:00 -06001292 VkPipeEvent set_events[] = { VK_PIPE_EVENT_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001293
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001294 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001295 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001296 memory_barrier.image = m_renderTargets[i]->image();
1297 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbourd1c35722015-04-16 15:59:00 -06001298 vkCmdPipelineBarrier( obj(), VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001299 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001300 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001301}
1302
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001303void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001304{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001305 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001306 renderpass,
1307 framebuffer,
1308 };
1309
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001310 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001311}
1312
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001313void VkCommandBufferObj::EndRenderPass(VkRenderPass renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001314{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001315 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001316}
1317
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001318void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001319{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001320 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001321}
1322
Tony Barbour6918cd52015-04-09 12:58:51 -06001323void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001324{
1325 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001326}
1327
Tony Barbour6918cd52015-04-09 12:58:51 -06001328void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001329{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001330 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001331}
1332
Tony Barbour6918cd52015-04-09 12:58:51 -06001333void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001334{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001335 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001336}
1337
Tony Barbour6918cd52015-04-09 12:58:51 -06001338void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001339{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001340 QueueCommandBuffer(NULL);
1341}
1342
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001343void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001344{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001345 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001346
1347 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001348 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1349 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001350
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001351 err = vkQueueWaitIdle( m_device->m_queue );
1352 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001353
1354 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001355 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001356}
1357
Tony Barbour6918cd52015-04-09 12:58:51 -06001358void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001359{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001360 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001361}
1362
Tony Barbour6918cd52015-04-09 12:58:51 -06001363void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001364{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001365 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001366
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001367 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001368 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropd4c1a502015-04-16 13:41:56 -06001369 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001370}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001371
Tony Barbour6918cd52015-04-09 12:58:51 -06001372void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001373{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001374 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001375}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001376
Tony Barbourd1c35722015-04-16 15:59:00 -06001377void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001378{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001379 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001380}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001381
Tony Barbour6918cd52015-04-09 12:58:51 -06001382VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001383{
1384 m_initialized = false;
1385}
Tony Barbour6918cd52015-04-09 12:58:51 -06001386bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001387{
1388 return m_initialized;
1389}
1390
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001391VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001392{
1393 return &m_depthStencilBindInfo;
1394}
1395
Tony Barbour6918cd52015-04-09 12:58:51 -06001396void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001397{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001398 VkImageCreateInfo image_info;
1399 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001400
1401 m_device = device;
1402 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001403 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001404
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001405 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001406 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001407 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001408 image_info.format = m_depth_stencil_fmt;
1409 image_info.extent.width = width;
1410 image_info.extent.height = height;
1411 image_info.extent.depth = 1;
1412 image_info.mipLevels = 1;
1413 image_info.arraySize = 1;
1414 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001415 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001416 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001417 image_info.flags = 0;
1418 init(*m_device, image_info);
1419
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001420 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001421 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001422 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001423 view_info.mipLevel = 0;
1424 view_info.baseArraySlice = 0;
1425 view_info.arraySize = 1;
1426 view_info.flags = 0;
1427 view_info.image = obj();
1428 m_depthStencilView.init(*m_device, view_info);
1429
1430 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001431 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001432}