blob: 1171a2c106abdb657bb4ae1cfd8fdd9f30c08bc8 [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"
Ian Elliott7e40db92015-08-21 15:09:33 -060029#include <vk_ext_khr_swapchain.h>
30#include <vk_ext_khr_device_swapchain.h>
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060031
Jon Ashburn83a64252015-04-15 11:31:12 -060032#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
Tony Barbour6a3faf02015-07-23 10:36:18 -060033#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
34{ \
35 fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
36 assert(fp##entrypoint != NULL); \
37}
Jon Ashburn83a64252015-04-15 11:31:12 -060038
Tony Barbour6918cd52015-04-09 12:58:51 -060039VkRenderFramework::VkRenderFramework() :
Tony Barbourfe3351b2015-07-28 10:17:20 -060040 m_cmdBuffer(),
Tony Barbourf77fd652015-04-09 11:07:02 -060041 m_renderPass(VK_NULL_HANDLE),
42 m_framebuffer(VK_NULL_HANDLE),
Cody Northrop271ba752015-08-26 10:01:32 -060043 m_stateLineWidth( VK_NULL_HANDLE ),
44 m_stateDepthBias( VK_NULL_HANDLE ),
45 m_stateBlend( VK_NULL_HANDLE ),
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060046 m_stateViewport( VK_NULL_HANDLE ),
Cody Northrop271ba752015-08-26 10:01:32 -060047 m_stateDepthBounds( VK_NULL_HANDLE ),
Cody Northrop82485a82015-08-18 15:21:16 -060048 m_stateStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060049 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070050 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060051 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
52 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Tony Barbour71bd4b32015-07-21 17:00:26 -060053 m_clear_via_load_op( true ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070054 m_depth_clear_color( 1.0 ),
Courtney Goeltzenleuchterb6c9aca2015-06-08 16:19:37 -060055 m_stencil_clear_color( 0 ),
56 m_depthStencil( NULL ),
57 m_dbgCreateMsgCallback( VK_NULL_HANDLE ),
58 m_dbgDestroyMsgCallback( VK_NULL_HANDLE ),
59 m_globalMsgCallback( VK_NULL_HANDLE ),
60 m_devMsgCallback( VK_NULL_HANDLE )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060061{
Tony Barbour1c45ce02015-03-27 17:03:18 -060062
Chia-I Wu08accc62015-07-07 11:50:03 +080063 memset(&m_renderPassBeginInfo, 0, sizeof(m_renderPassBeginInfo));
64 m_renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
65
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070066 // clear the back buffer to dark grey
Cody Northrop85789d52015-08-25 15:26:38 -060067 m_clear_color.float32[0] = 0.25f;
68 m_clear_color.float32[1] = 0.25f;
69 m_clear_color.float32[2] = 0.25f;
70 m_clear_color.float32[3] = 0.0f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060071}
72
Tony Barbour6918cd52015-04-09 12:58:51 -060073VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060074{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060075
76}
77
Tony Barbour6918cd52015-04-09 12:58:51 -060078void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060079{
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060080 std::vector<const char*> instance_layer_names;
81 std::vector<const char*> device_layer_names;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060082 std::vector<const char*> instance_extension_names;
83 std::vector<const char*> device_extension_names;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060084 InitFramework(
85 instance_layer_names, device_layer_names,
86 instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060087}
88
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060089void VkRenderFramework::InitFramework(
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060090 std::vector<const char *> instance_layer_names,
91 std::vector<const char *> device_layer_names,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060092 std::vector<const char *> instance_extension_names,
93 std::vector<const char *> device_extension_names,
94 PFN_vkDbgMsgCallback dbgFunction,
95 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060096{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060097 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060098 std::vector<VkExtensionProperties> instance_extensions;
99 std::vector<VkExtensionProperties> device_extensions;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600100 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600101
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600102 /* TODO: Verify requested extensions are available */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600103
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600104 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -0600105 instInfo.pNext = NULL;
106 instInfo.pAppInfo = &app_info;
107 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600108 instInfo.layerCount = instance_layer_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600109 instInfo.ppEnabledLayerNames = instance_layer_names.data();
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600110 instInfo.extensionCount = instance_extension_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600111 instInfo.ppEnabledExtensionNames = instance_extension_names.data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600112 err = vkCreateInstance(&instInfo, &this->inst);
113 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600114
Jon Ashburn83a64252015-04-15 11:31:12 -0600115 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
116 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
117 ASSERT_VK_SUCCESS(err);
118 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600119 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700120 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600121 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600122 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
123 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
124 if (m_dbgCreateMsgCallback) {
125 err = m_dbgCreateMsgCallback(this->inst,
126 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
127 dbgFunction,
128 userData,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600129 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600130 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600131
132 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
133 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600134 }
Tony Barbour15524c32015-04-29 17:34:29 -0600135 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600136
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600137 /* TODO: Verify requested physical device extensions are available */
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600138 m_device = new VkDeviceObj(0, objs[0], device_layer_names, device_extension_names);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600139
140 /* Now register callback on device */
141 if (0) {
142 if (m_dbgCreateMsgCallback) {
143 err = m_dbgCreateMsgCallback(this->inst,
144 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
145 dbgFunction,
146 userData,
147 &m_devMsgCallback);
148 ASSERT_VK_SUCCESS(err);
149 }
150 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600151 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600152
Tony Barbour6918cd52015-04-09 12:58:51 -0600153 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600154}
155
Tony Barbour6918cd52015-04-09 12:58:51 -0600156void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600157{
Cody Northrop271ba752015-08-26 10:01:32 -0600158 if (m_stateBlend) vkDestroyDynamicBlendState(device(), m_stateBlend);
159 if (m_stateDepthBounds) vkDestroyDynamicDepthBoundsState(device(), m_stateDepthBounds);
Cody Northrop82485a82015-08-18 15:21:16 -0600160 if (m_stateStencil) vkDestroyDynamicStencilState(device(), m_stateStencil);
Cody Northrop271ba752015-08-26 10:01:32 -0600161 if (m_stateLineWidth) vkDestroyDynamicLineWidthState(device(), m_stateLineWidth);
162 if (m_stateDepthBias) vkDestroyDynamicDepthBiasState(device(), m_stateDepthBias);
Tony Barbourfe3351b2015-07-28 10:17:20 -0600163 if (m_cmdBuffer)
164 delete m_cmdBuffer;
Cody Northrope62183e2015-07-09 18:08:05 -0600165 if (m_cmdPool) vkDestroyCommandPool(device(), m_cmdPool);
Tony Barbour67e99152015-07-10 14:10:27 -0600166 if (m_framebuffer) vkDestroyFramebuffer(device(), m_framebuffer);
167 if (m_renderPass) vkDestroyRenderPass(device(), m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600168
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600169 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
170 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
171
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600172 if (m_stateViewport) {
Tony Barbour67e99152015-07-10 14:10:27 -0600173 vkDestroyDynamicViewportState(device(), m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600174 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600175 while (!m_renderTargets.empty()) {
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600176 vkDestroyImageView(device(), m_renderTargets.back()->targetView(m_render_target_fmt));
Tony Barbour67e99152015-07-10 14:10:27 -0600177 vkDestroyImage(device(), m_renderTargets.back()->image());
Mike Stroyanb050c682015-04-17 12:36:38 -0600178 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600179 m_renderTargets.pop_back();
180 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600181
Tony Barbour1c45ce02015-03-27 17:03:18 -0600182 delete m_depthStencil;
Tony Barbour823e6ca2015-07-27 13:31:04 -0600183 while (!m_shader_modules.empty())
184 {
185 delete m_shader_modules.back();
186 m_shader_modules.pop_back();
187 }
Tony Barbour1c45ce02015-03-27 17:03:18 -0600188
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600189 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800190 delete m_device;
Chris Forbesbe2fae62015-07-07 11:02:22 +1200191 if (this->inst) vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600192}
193
Tony Barbour6918cd52015-04-09 12:58:51 -0600194void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600195{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600196 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600197
Tony Barbour6a3faf02015-07-23 10:36:18 -0600198 // Get the list of VkFormat's that are supported:
Ian Elliott7e40db92015-08-21 15:09:33 -0600199 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
Ian Elliott8b139792015-08-07 11:51:12 -0600200 uint32_t formatCount;
Ian Elliott7e40db92015-08-21 15:09:33 -0600201 VkSurfaceDescriptionKHR surface_description;
202 surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Tony Barbour6a3faf02015-07-23 10:36:18 -0600203 surface_description.pNext = NULL;
Ian Elliott7e40db92015-08-21 15:09:33 -0600204 GET_DEVICE_PROC_ADDR(device(), GetSurfaceFormatsKHR);
205 err = fpGetSurfaceFormatsKHR(device(),
206 (VkSurfaceDescriptionKHR *) &surface_description,
Ian Elliott8b139792015-08-07 11:51:12 -0600207 &formatCount, NULL);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600208 ASSERT_VK_SUCCESS(err);
Ian Elliott7e40db92015-08-21 15:09:33 -0600209 VkSurfaceFormatKHR *surfFormats =
210 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
211 err = fpGetSurfaceFormatsKHR(device(),
212 (VkSurfaceDescriptionKHR *) &surface_description,
Ian Elliott8b139792015-08-07 11:51:12 -0600213 &formatCount, surfFormats);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600214 ASSERT_VK_SUCCESS(err);
215 m_render_target_fmt = surfFormats[0].format;
216 free(surfFormats);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600217
Cody Northrop271ba752015-08-26 10:01:32 -0600218 VkDynamicLineWidthStateCreateInfo lineWidth = {};
219 lineWidth.sType = VK_STRUCTURE_TYPE_DYNAMIC_LINE_WIDTH_STATE_CREATE_INFO;
220 lineWidth.lineWidth = 1;
221 err = vkCreateDynamicLineWidthState( device(), &lineWidth, &m_stateLineWidth );
Cody Northrop12365112015-08-17 11:10:49 -0600222 ASSERT_VK_SUCCESS(err);
Tony Barbourf52346d2015-01-16 14:27:35 -0700223
Cody Northrop271ba752015-08-26 10:01:32 -0600224 VkDynamicDepthBiasStateCreateInfo depthBias = {};
225 depthBias.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BIAS_STATE_CREATE_INFO;
226 depthBias.depthBias = 0.0f;
227 depthBias.depthBiasClamp = 0.0f;
228 depthBias.slopeScaledDepthBias = 0.0f;
229 err = vkCreateDynamicDepthBiasState( device(), &depthBias, &m_stateDepthBias );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600230 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600231
Cody Northrop271ba752015-08-26 10:01:32 -0600232 VkDynamicBlendStateCreateInfo blend = {};
233 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_BLEND_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600234 blend.blendConst[0] = 1.0f;
235 blend.blendConst[1] = 1.0f;
236 blend.blendConst[2] = 1.0f;
237 blend.blendConst[3] = 1.0f;
Cody Northrop271ba752015-08-26 10:01:32 -0600238 err = vkCreateDynamicBlendState(device(), &blend, &m_stateBlend);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600239 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600240
Cody Northrop271ba752015-08-26 10:01:32 -0600241 VkDynamicDepthBoundsStateCreateInfo depth = {};
242 depth.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BOUNDS_STATE_CREATE_INFO;
Cody Northrop82485a82015-08-18 15:21:16 -0600243 depth.minDepthBounds = 0.f;
244 depth.maxDepthBounds = 1.f;
Cody Northrop271ba752015-08-26 10:01:32 -0600245 err = vkCreateDynamicDepthBoundsState( device(), &depth, &m_stateDepthBounds );
Cody Northrop82485a82015-08-18 15:21:16 -0600246 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600247
Cody Northrop82485a82015-08-18 15:21:16 -0600248 VkDynamicStencilStateCreateInfo stencil = {};
249 stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_STENCIL_STATE_CREATE_INFO;
Cody Northrop271ba752015-08-26 10:01:32 -0600250 stencil.stencilCompareMask = 0xff;
Cody Northrop82485a82015-08-18 15:21:16 -0600251 stencil.stencilWriteMask = 0xff;
252 stencil.stencilReference = 0;
253 err = vkCreateDynamicStencilState( device(), &stencil, &stencil, &m_stateStencil );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600254 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600255
Cody Northrope62183e2015-07-09 18:08:05 -0600256 VkCmdPoolCreateInfo cmd_pool_info;
257 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
258 cmd_pool_info.pNext = NULL,
259 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
260 cmd_pool_info.flags = 0,
261 err = vkCreateCommandPool(device(), &cmd_pool_info, &m_cmdPool);
262 assert(!err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600263
Tony Barbourfe3351b2015-07-28 10:17:20 -0600264 m_cmdBuffer = new VkCommandBufferObj(m_device, m_cmdPool);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600265}
266
Tony Barbour6918cd52015-04-09 12:58:51 -0600267void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600268{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600269 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600270
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600271 VkViewport viewport;
Chris Forbesd9be82b2015-06-22 17:21:59 +1200272 VkRect2D scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600273
Tony Barbour67e99152015-07-10 14:10:27 -0600274 VkDynamicViewportStateCreateInfo viewportCreate = {};
275 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700276 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700277 viewport.originX = 0;
278 viewport.originY = 0;
279 viewport.width = 1.f * width;
280 viewport.height = 1.f * height;
281 viewport.minDepth = 0.f;
282 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600283 scissor.extent.width = (int32_t) width;
284 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700285 scissor.offset.x = 0;
286 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700287 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700288 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700289
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600290 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
291 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600292 m_width = width;
293 m_height = height;
294}
295
Tony Barbour6918cd52015-04-09 12:58:51 -0600296void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600297{
298 InitViewport(m_width, m_height);
299}
Tony Barbour6918cd52015-04-09 12:58:51 -0600300void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600301{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700302 InitRenderTarget(1);
303}
304
Tony Barbour6918cd52015-04-09 12:58:51 -0600305void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700306{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600307 InitRenderTarget(targets, NULL);
308}
309
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600310void VkRenderFramework::InitRenderTarget(VkImageView *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600311{
312 InitRenderTarget(1, dsBinding);
313}
314
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600315void VkRenderFramework::InitRenderTarget(uint32_t targets, VkImageView *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600316{
Chia-I Wu08accc62015-07-07 11:50:03 +0800317 std::vector<VkAttachmentDescription> attachments;
318 std::vector<VkAttachmentReference> color_references;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600319 std::vector<VkImageView> bindings;
Cody Northropd2ad0342015-08-05 11:15:02 -0600320 attachments.reserve(targets + 1); // +1 for dsBinding
Chia-I Wu08accc62015-07-07 11:50:03 +0800321 color_references.reserve(targets);
Cody Northropd2ad0342015-08-05 11:15:02 -0600322 bindings.reserve(targets + 1); // +1 for dsBinding
Tony Barbour1c45ce02015-03-27 17:03:18 -0600323
Chia-I Wu08accc62015-07-07 11:50:03 +0800324 VkAttachmentDescription att = {};
325 att.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION;
326 att.pNext = NULL;
327 att.format = m_render_target_fmt;
328 att.samples = 1;
329 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
330 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
331 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
332 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
333 att.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
334 att.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chia-I Wuecebf752014-12-05 10:45:15 +0800335
Chia-I Wu08accc62015-07-07 11:50:03 +0800336 VkAttachmentReference ref = {};
337 ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
338
339 m_renderPassClearValues.clear();
340 VkClearValue clear = {};
341 clear.color = m_clear_color;
342
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600343 VkImageView bind = {};
Chia-I Wu08accc62015-07-07 11:50:03 +0800344
345 for (uint32_t i = 0; i < targets; i++) {
346 attachments.push_back(att);
347
348 ref.attachment = i;
349 color_references.push_back(ref);
350
351 m_renderPassClearValues.push_back(clear);
352
Tony Barbour6918cd52015-04-09 12:58:51 -0600353 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600354
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600355 VkFormatProperties props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600356 VkResult err;
357
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600358 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), m_render_target_fmt, &props);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600359 ASSERT_VK_SUCCESS(err);
360
361 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600362 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600363 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
364 VK_IMAGE_TILING_LINEAR);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600365 }
366 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600367 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600368 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
369 VK_IMAGE_TILING_OPTIMAL);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600370 }
371 else {
372 FAIL() << "Neither Linear nor Optimal allowed for render target";
373 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600374
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600375 m_renderTargets.push_back(img);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600376 bind = img->targetView(m_render_target_fmt);
Chia-I Wu08accc62015-07-07 11:50:03 +0800377 bindings.push_back(bind);
Chia-I Wuecebf752014-12-05 10:45:15 +0800378 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600379
Chia-I Wu08accc62015-07-07 11:50:03 +0800380 VkSubpassDescription subpass = {};
381 subpass.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION;
382 subpass.pNext = NULL;
383 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
384 subpass.flags = 0;
385 subpass.inputCount = 0;
Cody Northropa505dda2015-08-04 11:16:41 -0600386 subpass.pInputAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800387 subpass.colorCount = targets;
Cody Northropa505dda2015-08-04 11:16:41 -0600388 subpass.pColorAttachments = color_references.data();
389 subpass.pResolveAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800390
391 if (dsBinding) {
392 att.format = m_depth_stencil_fmt;
Tony Barbour71bd4b32015-07-21 17:00:26 -0600393 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;;
Chia-I Wu08accc62015-07-07 11:50:03 +0800394 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
395 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
396 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Cody Northrop78e71932015-09-03 16:09:57 -0600397 att.initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
398 att.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800399 attachments.push_back(att);
400
Cody Northrop85789d52015-08-25 15:26:38 -0600401 clear.depthStencil.depth = m_depth_clear_color;
402 clear.depthStencil.stencil = m_stencil_clear_color;
Chia-I Wu08accc62015-07-07 11:50:03 +0800403 m_renderPassClearValues.push_back(clear);
404
405 bindings.push_back(*dsBinding);
406
407 subpass.depthStencilAttachment.attachment = targets;
Cody Northrop78e71932015-09-03 16:09:57 -0600408 subpass.depthStencilAttachment.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800409 } else {
410 subpass.depthStencilAttachment.attachment = VK_ATTACHMENT_UNUSED;
411 }
412
413 subpass.preserveCount = 0;
Cody Northropa505dda2015-08-04 11:16:41 -0600414 subpass.pPreserveAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800415
416 VkRenderPassCreateInfo rp_info = {};
417 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
418 rp_info.attachmentCount = attachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600419 rp_info.pAttachments = attachments.data();
Chia-I Wu08accc62015-07-07 11:50:03 +0800420 rp_info.subpassCount = 1;
421 rp_info.pSubpasses = &subpass;
422
423 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
424
425 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600426 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600427 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600428 fb_info.pNext = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800429 fb_info.renderPass = m_renderPass;
430 fb_info.attachmentCount = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600431 fb_info.pAttachments = bindings.data();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600432 fb_info.width = (uint32_t)m_width;
433 fb_info.height = (uint32_t)m_height;
434 fb_info.layers = 1;
435
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600436 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600437
Chia-I Wu08accc62015-07-07 11:50:03 +0800438 m_renderPassBeginInfo.renderPass = m_renderPass;
439 m_renderPassBeginInfo.framebuffer = m_framebuffer;
Cody Northropd2ad0342015-08-05 11:15:02 -0600440 m_renderPassBeginInfo.renderArea.extent.width = (int32_t) m_width;
441 m_renderPassBeginInfo.renderArea.extent.height = (int32_t) m_height;
Cody Northrop23dd89d2015-08-04 11:51:03 -0600442 m_renderPassBeginInfo.clearValueCount = m_renderPassClearValues.size();
443 m_renderPassBeginInfo.pClearValues = m_renderPassClearValues.data();
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600444}
445
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600446
447
Tony Barbourd1c35722015-04-16 15:59:00 -0600448VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600449 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800450{
451 init();
452
Chia-I Wu999f0482015-07-03 10:32:05 +0800453 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600454 queue_props = phy().queue_properties().data();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800455}
456
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600457VkDeviceObj::VkDeviceObj(uint32_t id,
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600458 VkPhysicalDevice obj, std::vector<const char *> &layer_names,
459 std::vector<const char *> &extension_names) :
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600460 vk_testing::Device(obj), id(id)
461{
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600462 init(layer_names, extension_names);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600463
Chia-I Wu999f0482015-07-03 10:32:05 +0800464 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600465 queue_props = phy().queue_properties().data();
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600466}
467
Tony Barbour6918cd52015-04-09 12:58:51 -0600468void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800469{
470 ASSERT_NE(true, graphics_queues().empty());
Chia-I Wudf12ffd2015-07-03 10:53:18 +0800471 m_queue = graphics_queues()[0]->handle();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800472}
473
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600474VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
475 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700476{
Tony Barboure2c58df2014-11-25 13:18:32 -0700477
478}
479
Tony Barbour6918cd52015-04-09 12:58:51 -0600480VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700481{
Chia-I Wu11078b02015-01-04 16:27:24 +0800482 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700483}
Tony Barbour82c39522014-12-04 14:33:33 -0700484
Tony Barbour6918cd52015-04-09 12:58:51 -0600485int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700486{
Chia-I Wu11078b02015-01-04 16:27:24 +0800487 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600488 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600489 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800490 tc.count = 1;
491 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700492
Chia-I Wu11078b02015-01-04 16:27:24 +0800493 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700494}
Tony Barbour82c39522014-12-04 14:33:33 -0700495
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600496int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700497{
Courtney Goeltzenleuchter6eb1aeb2015-09-11 15:29:21 -0600498 assert(type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
499 type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
500 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
501 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600502 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800503 tc.type = type;
504 tc.count = 1;
505 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700506
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800507 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
508 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600509
Chia-I Wu11078b02015-01-04 16:27:24 +0800510 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700511}
Tony Barbour82c39522014-12-04 14:33:33 -0700512
Tony Barbour6918cd52015-04-09 12:58:51 -0600513int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700514{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600515 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600516 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800517 tc.count = 1;
518 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700519
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800520 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Chia-I Wu8c721c62015-07-03 11:49:42 +0800521 tmp.sampler = sampler->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800522 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700523
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800524 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
525 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700526
Chia-I Wu11078b02015-01-04 16:27:24 +0800527 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700528}
Chia-I Wu11078b02015-01-04 16:27:24 +0800529
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500530VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700531{
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800532 return m_pipeline_layout.handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700533}
534
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600535VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700536{
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800537 return m_set->handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700538}
Tony Barboure2c58df2014-11-25 13:18:32 -0700539
Tony Barbour6918cd52015-04-09 12:58:51 -0600540void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700541{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600542 // create VkDescriptorPool
543 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600544 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800545 pool.count = m_type_counts.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600546 pool.pTypeCount = m_type_counts.data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600547 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700548
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600549 // create VkDescriptorSetLayout
550 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800551 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800552 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800553 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800554 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600555 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800556 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700557 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800558
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600559 // create VkDescriptorSetLayout
560 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600561 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800562 layout.count = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600563 layout.pBinding = bindings.data();
Chia-I Wub41393e2015-03-26 15:04:41 +0800564
Chia-I Wu41126e52015-03-26 15:27:55 +0800565 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600566 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800567 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500568
569 // create VkPipelineLayout
570 VkPipelineLayoutCreateInfo pipeline_layout = {};
571 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
572 pipeline_layout.descriptorSetCount = layouts.size();
573 pipeline_layout.pSetLayouts = NULL;
574
575 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700576
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600577 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500578 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800579
Chia-I Wu41126e52015-03-26 15:27:55 +0800580 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800581 size_t imageSamplerCount = 0;
582 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
583 it != m_writes.end(); it++) {
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800584 it->destSet = m_set->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800585 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
586 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800587 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800588
589 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800590 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700591}
Tony Barboure2c58df2014-11-25 13:18:32 -0700592
Tony Barbour6918cd52015-04-09 12:58:51 -0600593VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800594{
595 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800596 m_descriptorInfo.imageView = VK_NULL_HANDLE;
597 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800598}
599
Tony Barbour6918cd52015-04-09 12:58:51 -0600600void VkImageObj::ImageMemoryBarrier(
601 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600602 VkImageAspect aspect,
603 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600604 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600605 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
606 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
607 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
608 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600609 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600610 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600611 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
612 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
613 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
614 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
615 VK_MEMORY_INPUT_SHADER_READ_BIT |
616 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
617 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
618 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600619 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600620{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600621 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
622 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600623 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
624 subresourceRange);
625
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600626 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600627
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600628 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
629 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600630
631 // write barrier to the command buffer
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -0600632 vkCmdPipelineBarrier(cmd_buf->handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600633}
634
Tony Barbour6918cd52015-04-09 12:58:51 -0600635void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600636 VkImageAspect aspect,
637 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600638{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600639 VkFlags output_mask, input_mask;
640 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600641 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600642 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
643 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
644 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600645 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600646 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600647 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600648 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
649 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
650 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
651 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
652 VK_MEMORY_INPUT_SHADER_READ_BIT |
653 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
654 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600655 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600656
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800657 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600658 return;
659 }
660
661 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600662 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600663 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
664 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600665 break;
666
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600667 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600668 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
669 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600670 break;
671
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600672 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600673 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
674 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600675 break;
676
677 default:
678 output_mask = all_cache_outputs;
679 input_mask = all_cache_inputs;
680 break;
681 }
682
683 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800684 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600685}
686
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600687void VkImageObj::SetLayout(VkImageAspect aspect,
688 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600689{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600690 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600691
692 if (image_layout == m_descriptorInfo.imageLayout) {
693 return;
694 }
695
Tony Barbourfe3351b2015-07-28 10:17:20 -0600696 VkCmdPoolCreateInfo cmd_pool_info = {};
697 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
698 cmd_pool_info.pNext = NULL;
699 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
700 cmd_pool_info.flags = 0;
701 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
702 VkCommandBufferObj cmd_buf(m_device, pool.handle());
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600703
704 /* Build command buffer to set image layout in the driver */
705 err = cmd_buf.BeginCommandBuffer();
706 assert(!err);
707
708 SetLayout(&cmd_buf, aspect, image_layout);
709
710 err = cmd_buf.EndCommandBuffer();
711 assert(!err);
712
713 cmd_buf.QueueCommandBuffer();
714}
715
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600716bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600717{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600718 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600719 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600720 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600721
Tony Barbour579f7802015-04-03 15:11:43 -0600722 return true;
723}
724
Tony Barbour6918cd52015-04-09 12:58:51 -0600725void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600726 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600727 VkImageTiling requested_tiling,
728 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800729{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600730 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600731 VkFormatProperties image_fmt;
732 VkImageTiling tiling;
733 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600734
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800735 mipCount = 0;
736
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600737 uint32_t _w = w;
738 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800739 while( ( _w > 0 ) || ( _h > 0 ) )
740 {
741 _w >>= 1;
742 _h >>= 1;
743 mipCount++;
744 }
745
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600746 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), fmt, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600747 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600748
Tony Barbourd1c35722015-04-16 15:59:00 -0600749 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600750 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600751 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600752 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600753 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600754 } else {
755 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
756 }
757 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600758 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600759 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600760 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600761 } else {
762 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
763 }
764
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600765 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600766 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800767 imageCreateInfo.format = fmt;
768 imageCreateInfo.extent.width = w;
769 imageCreateInfo.extent.height = h;
770 imageCreateInfo.mipLevels = mipCount;
771 imageCreateInfo.tiling = tiling;
772
773 imageCreateInfo.usage = usage;
774
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600775 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800776
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600777 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600778 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600779 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600780 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600781 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800782}
783
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600784VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600785{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600786 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600787 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600788
Tony Barbourfe3351b2015-07-28 10:17:20 -0600789 VkCmdPoolCreateInfo cmd_pool_info = {};
790 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
791 cmd_pool_info.pNext = NULL;
792 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
793 cmd_pool_info.flags = 0;
794 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
795 VkCommandBufferObj cmd_buf(m_device, pool.handle());
796
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600797 /* Build command buffer to copy staging texture to usable texture */
798 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600799 assert(!err);
800
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600801 /* TODO: Can we determine image aspect from image object? */
802 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600803 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600804
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600805 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600806 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600807
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600808 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600809 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600810 copy_region.srcSubresource.arrayLayer = 0;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600811 copy_region.srcSubresource.mipLevel = 0;
812 copy_region.srcOffset.x = 0;
813 copy_region.srcOffset.y = 0;
814 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600815 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600816 copy_region.destSubresource.arrayLayer = 0;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600817 copy_region.destSubresource.mipLevel = 0;
818 copy_region.destOffset.x = 0;
819 copy_region.destOffset.y = 0;
820 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600821 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600822
Chia-I Wube2b9172015-07-03 11:49:42 +0800823 vkCmdCopyImage(cmd_buf.handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +0800824 src_image.handle(), src_image.layout(),
825 handle(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600826 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600827
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600828 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600829
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600830 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600831
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600832 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600833 assert(!err);
834
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600835 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600836
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600837 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600838}
839
Tony Barbour6918cd52015-04-09 12:58:51 -0600840VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
841 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700842{
843 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600844 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600845 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600846 void *data;
847 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600848 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600849 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600850
Tony Barboure65788f2015-07-21 17:01:42 -0600851 stagingImage.init(16, 16, tex_format, VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600852 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600853
854 if (colors == NULL)
855 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700856
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800857 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700858
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600859 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600860 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600861 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600862 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600863 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600864 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600865 view.channels.r = VK_CHANNEL_SWIZZLE_R;
866 view.channels.g = VK_CHANNEL_SWIZZLE_G;
867 view.channels.b = VK_CHANNEL_SWIZZLE_B;
868 view.channels.a = VK_CHANNEL_SWIZZLE_A;
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -0600869 view.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600870 view.subresourceRange.baseMipLevel = 0;
871 view.subresourceRange.mipLevels = 1;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600872 view.subresourceRange.baseArrayLayer = 0;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600873 view.subresourceRange.arraySize = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700874
Tony Barboure2c58df2014-11-25 13:18:32 -0700875 /* create image */
Tony Barboure65788f2015-07-21 17:01:42 -0600876 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700877
878 /* create image view */
Chia-I Wu681d7a02015-07-03 13:44:34 +0800879 view.image = handle();
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800880 m_textureView.init(*m_device, view);
Chia-I Wu3158bf32015-07-03 11:49:42 +0800881 m_descriptorInfo.imageView = m_textureView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700882
Chia-I Wu681d7a02015-07-03 13:44:34 +0800883 data = stagingImage.MapMemory();
Tony Barboure2c58df2014-11-25 13:18:32 -0700884
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800885 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700886 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800887 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600888 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700889 }
Chia-I Wu681d7a02015-07-03 13:44:34 +0800890 stagingImage.UnmapMemory();
Tony Barbour6918cd52015-04-09 12:58:51 -0600891 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700892}
Tony Barbour82c39522014-12-04 14:33:33 -0700893
Tony Barbour6918cd52015-04-09 12:58:51 -0600894VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700895{
Tony Barboure2c58df2014-11-25 13:18:32 -0700896 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700897
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600898 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800899 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600900 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
901 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
902 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600903 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchter97953352015-09-10 14:08:50 -0600904 samplerCreateInfo.addressModeU = VK_TEX_ADDRESS_MODE_WRAP;
905 samplerCreateInfo.addressModeV = VK_TEX_ADDRESS_MODE_WRAP;
906 samplerCreateInfo.addressModeW = VK_TEX_ADDRESS_MODE_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800907 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600908 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600909 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800910 samplerCreateInfo.minLod = 0.0;
911 samplerCreateInfo.maxLod = 0.0;
Tony Barbour26b17f82015-06-25 16:56:44 -0600912 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Mark Lobodzinski52ac6582015-09-01 15:42:56 -0600913 samplerCreateInfo.unnormalizedCoordinates = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700914
Chia-I Wue9864b52014-12-28 16:32:24 +0800915 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700916}
Tony Barboure2c58df2014-11-25 13:18:32 -0700917
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700918/*
919 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
920 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600921VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700922{
923 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700924 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700925
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800926 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700927}
928
Tony Barbour6918cd52015-04-09 12:58:51 -0600929VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600930{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600931 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600932 if (m_commandBuffer) {
933 delete m_commandBuffer;
Tony Barbour3d83f492015-07-28 10:23:02 -0600934 delete m_cmdPool;
Tony Barbour044703b2015-03-26 12:37:52 -0600935 }
936}
937
Tony Barbour6918cd52015-04-09 12:58:51 -0600938VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700939{
Tony Barboure2c58df2014-11-25 13:18:32 -0700940 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800941 m_commandBuffer = 0;
942
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800943 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700944 m_numVertices = constantCount;
945 m_stride = constantSize;
946
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600947 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800948 const size_t allocationSize = constantCount * constantSize;
Cody Northrop7fb43862015-06-22 14:56:14 -0600949 init_as_src_and_dst(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700950
Chia-I Wu681d7a02015-07-03 13:44:34 +0800951 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +0800952 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800953 memory().unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700954
Courtney Goeltzenleuchter6eb1aeb2015-09-11 15:29:21 -0600955 /*
956 * Constant buffers are going to be used as vertex input buffers
957 * or as shader uniform buffers. So, we'll create the shaderbuffer
958 * descriptor here so it's ready if needed.
959 */
Courtney Goeltzenleuchterb46f2272015-09-14 14:14:21 -0600960 this->m_descriptorInfo.bufferInfo.buffer = handle();
961 this->m_descriptorInfo.bufferInfo.offset = 0;
962 this->m_descriptorInfo.bufferInfo.range = allocationSize;
Tony Barboure2c58df2014-11-25 13:18:32 -0700963}
Tony Barbour82c39522014-12-04 14:33:33 -0700964
Tony Barbourd1c35722015-04-16 15:59:00 -0600965void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700966{
Chia-I Wu681d7a02015-07-03 13:44:34 +0800967 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &handle(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700968}
969
970
Tony Barbour6918cd52015-04-09 12:58:51 -0600971void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600972 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600973 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600974 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
975 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
976 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
977 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600978 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600979 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600980 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
981 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
982 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
983 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
984 VK_MEMORY_INPUT_SHADER_READ_BIT |
985 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
986 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
987 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700988{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600989 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700990
Tony Barbour38422802014-12-10 14:36:31 -0700991 if (!m_commandBuffer)
992 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600993 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbourfe3351b2015-07-28 10:17:20 -0600994 VkCmdPoolCreateInfo cmd_pool_info = {};
995 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
996 cmd_pool_info.pNext = NULL;
997 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
998 cmd_pool_info.flags = 0;
999 m_cmdPool = new vk_testing::CmdPool(*m_device, cmd_pool_info);
1000 m_commandBuffer = new VkCommandBufferObj(m_device, m_cmdPool->handle());
Tony Barbour38422802014-12-10 14:36:31 -07001001 }
1002 else
1003 {
Chia-I Wua07fee62014-12-28 15:26:08 +08001004 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -07001005 }
1006
Tony Barboure2c58df2014-11-25 13:18:32 -07001007 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001008 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001009 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -06001010 cmd_buf_info.pNext = NULL;
1011 cmd_buf_info.flags = 0;
Cody Northropcc09b9e2015-08-11 11:35:58 -06001012 cmd_buf_info.renderPass = VK_NULL_HANDLE;
1013 cmd_buf_info.subpass = 0;
1014 cmd_buf_info.framebuffer = VK_NULL_HANDLE;
Tony Barbourbdf0a312015-04-01 17:10:07 -06001015
Jon Ashburnc4164b12014-12-31 17:10:47 -07001016 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001017 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001018
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001019 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001020 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001021 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -07001022
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001023 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1024 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001025
1026 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001027 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -07001028
1029 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -07001030 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001031 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001032
Tony Barboure2c58df2014-11-25 13:18:32 -07001033 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001034 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -07001035 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wud9e8e822015-07-03 11:45:55 +08001036 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.handle() );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001037 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001038}
1039
Tony Barbour6918cd52015-04-09 12:58:51 -06001040VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
1041 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001042{
1043
1044}
1045
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001046void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001047{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001048 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001049
1050 m_numVertices = numIndexes;
1051 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001052 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -06001053 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001054 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -06001055 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001056 break;
Tony Barbourd1c35722015-04-16 15:59:00 -06001057 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001058 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -06001059 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001060 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001061 default:
1062 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -06001063 m_stride = 2;
1064 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001065 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001066 }
1067
Chia-I Wua07fee62014-12-28 15:26:08 +08001068 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -06001069 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northrop7fb43862015-06-22 14:56:14 -06001070 init_as_src_and_dst(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001071
Chia-I Wu681d7a02015-07-03 13:44:34 +08001072 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +08001073 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +08001074 memory().unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001075
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001076 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001077 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001078 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001079 view_info.buffer = handle();
Tony Barbourd1c35722015-04-16 15:59:00 -06001080 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001081 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001082 view_info.offset = 0;
1083 view_info.range = allocationSize;
1084 m_bufferView.init(*m_device, view_info);
1085
Chia-I Wu3158bf32015-07-03 11:49:42 +08001086 this->m_descriptorInfo.bufferView = m_bufferView.handle();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001087}
1088
Tony Barbourd1c35722015-04-16 15:59:00 -06001089void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001090{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001091 vkCmdBindIndexBuffer(cmdBuffer, handle(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001092}
Tony Barboure2c58df2014-11-25 13:18:32 -07001093
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001094VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001095{
1096 return m_indexType;
1097}
1098
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001099VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001100{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001101 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001102 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1103 stageInfo->stage = m_stage;
Chia-I Wu4d0c7922015-07-03 11:49:42 +08001104 stageInfo->shader = handle();
Tony Barboure2c58df2014-11-25 13:18:32 -07001105
Tony Barboure2c58df2014-11-25 13:18:32 -07001106 return stageInfo;
1107}
1108
Tony Barbourd1c35722015-04-16 15:59:00 -06001109VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001110{
Tony Barbourb5d2c942015-07-14 13:34:05 -06001111 VkResult U_ASSERT_ONLY err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001112 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001113 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001114 VkShaderModuleCreateInfo moduleCreateInfo;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001115 vk_testing::ShaderModule *module = new vk_testing::ShaderModule();
Tony Barboure2c58df2014-11-25 13:18:32 -07001116 size_t shader_len;
1117
1118 m_stage = stage;
1119 m_device = device;
1120
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001121 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1122 moduleCreateInfo.pNext = NULL;
1123
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001124 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001125 createInfo.pNext = NULL;
1126
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001127 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001128
1129 shader_len = strlen(shader_code);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001130 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1131 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1132 moduleCreateInfo.flags = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001133
Tony Barbourd1c35722015-04-16 15:59:00 -06001134 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001135 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1136 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1137 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1138 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barboure2c58df2014-11-25 13:18:32 -07001139
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001140 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001141
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001142 // Use Reference GLSL to SPV compiler
1143 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001144 moduleCreateInfo.pCode = spv.data();
1145 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1146 moduleCreateInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001147 }
Cody Northrop475663c2015-04-15 11:19:06 -06001148
Tony Barbour823e6ca2015-07-27 13:31:04 -06001149 err = module->init_try(*m_device, moduleCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001150 assert(VK_SUCCESS == err);
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001151
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001152 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1153 createInfo.pNext = NULL;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001154 createInfo.module = module->handle();
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001155 createInfo.pName = "main";
1156 createInfo.flags = 0;
Cody Northropfd4bcfd2015-08-24 15:11:10 -06001157 createInfo.stage = stage;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001158
1159 err = init_try(*m_device, createInfo);
Cody Northrop475663c2015-04-15 11:19:06 -06001160 assert(VK_SUCCESS == err);
Tony Barbour823e6ca2015-07-27 13:31:04 -06001161 framework->m_shader_modules.push_back(module);
Tony Barbourf325bf12014-12-03 15:59:38 -07001162}
Tony Barbour82c39522014-12-04 14:33:33 -07001163
Tony Barbour6918cd52015-04-09 12:58:51 -06001164VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001165{
Tony Barboure2c58df2014-11-25 13:18:32 -07001166 m_device = device;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001167
1168 m_vi_state.pNext = VK_NULL_HANDLE;
1169 m_vi_state.bindingCount = 0;
1170 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1171 m_vi_state.attributeCount = 0;
1172 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1173
Tony Barboure2c58df2014-11-25 13:18:32 -07001174 m_vertexBufferCount = 0;
1175
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001176 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001177 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001178 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001179 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001180
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001181 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001182 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001183 m_rs_state.depthClipEnable = VK_FALSE;
1184 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001185 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001186 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1187 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Cody Northrop12365112015-08-17 11:10:49 -06001188 m_rs_state.depthBiasEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001189
Tony Barboure2c58df2014-11-25 13:18:32 -07001190 memset(&m_cb_state,0,sizeof(m_cb_state));
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001191 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001192 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001193 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1194 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001195
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001196 m_ms_state.pNext = VK_NULL_HANDLE;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001197 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop02e61ed2015-08-04 14:34:54 -06001198 m_ms_state.pSampleMask = NULL;
Tony Barbourdfd533a2015-06-26 10:18:34 -06001199 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001200 m_ms_state.minSampleShading = 0;
1201 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001202
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001203 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001204 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001205 m_vp_state.viewportCount = 1;
Tony Barbourd81b8882015-05-15 09:37:57 -06001206
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001207 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001208 m_ds_state.pNext = VK_NULL_HANDLE,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001209 m_ds_state.depthTestEnable = VK_FALSE;
1210 m_ds_state.depthWriteEnable = VK_FALSE;
Cody Northrop271ba752015-08-26 10:01:32 -06001211 m_ds_state.depthBoundsTestEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001212 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001213 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1214 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1215 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001216 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001217 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001218 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001219};
1220
Tony Barbour6918cd52015-04-09 12:58:51 -06001221void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001222{
1223 m_shaderObjs.push_back(shader);
1224}
1225
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001226void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001227{
1228 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1229 m_vi_state.attributeCount = count;
1230}
1231
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001232void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001233{
1234 m_vi_state.pVertexBindingDescriptions = vi_binding;
1235 m_vi_state.bindingCount = count;
1236}
1237
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001238void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineColorBlendAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001239{
Tony Barbourf52346d2015-01-16 14:27:35 -07001240 if (binding+1 > m_colorAttachments.size())
1241 {
1242 m_colorAttachments.resize(binding+1);
1243 }
1244 m_colorAttachments[binding] = *att;
1245}
1246
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001247void VkPipelineObj::SetDepthStencil(VkPipelineDepthStencilStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001248{
Tony Barbourf52346d2015-01-16 14:27:35 -07001249 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1250 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
Cody Northrop271ba752015-08-26 10:01:32 -06001251 m_ds_state.depthBoundsTestEnable = ds_state->depthBoundsTestEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001252 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001253 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1254 m_ds_state.back = ds_state->back;
1255 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001256}
1257
Tony Barbour0ec8cd52015-08-06 09:27:11 -06001258void VkPipelineObj::SetMSAA(VkPipelineMultisampleStateCreateInfo *ms_state)
1259{
1260 memcpy(&m_ms_state, ms_state, sizeof(VkPipelineMultisampleStateCreateInfo));
1261}
1262
Tony Barbour5781e8f2015-08-04 16:23:11 -06001263VkResult VkPipelineObj::CreateVKPipeline(VkPipelineLayout layout, VkRenderPass render_pass)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001264{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001265 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001266
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001267 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001268
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001269 info.stageCount = m_shaderObjs.size();
1270 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1271
Tony Barbour976e1cf2014-12-17 11:57:31 -07001272 for (int i=0; i<m_shaderObjs.size(); i++)
1273 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001274 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001275 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001276 }
1277
Tony Barboure815fd72015-07-27 09:36:24 -06001278 if (m_vi_state.attributeCount && m_vi_state.bindingCount) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001279 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barboure815fd72015-07-27 09:36:24 -06001280 info.pVertexInputState = &m_vi_state;
1281 } else {
1282 info.pVertexInputState = NULL;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001283 }
1284
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001285 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001286 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001287 info.flags = 0;
Tony Barbour5781e8f2015-08-04 16:23:11 -06001288 info.layout = layout;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001289
Tony Barbourf52346d2015-01-16 14:27:35 -07001290 m_cb_state.attachmentCount = m_colorAttachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -06001291 m_cb_state.pAttachments = m_colorAttachments.data();
Tony Barbourf52346d2015-01-16 14:27:35 -07001292
Chia-I Wu08accc62015-07-07 11:50:03 +08001293 info.renderPass = render_pass;
1294 info.subpass = 0;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001295 info.pTessellationState = NULL;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001296 info.pInputAssemblyState = &m_ia_state;
1297 info.pViewportState = &m_vp_state;
1298 info.pRasterState = &m_rs_state;
1299 info.pMultisampleState = &m_ms_state;
1300 info.pDepthStencilState = &m_ds_state;
1301 info.pColorBlendState = &m_cb_state;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001302
Chris Forbes95292b12015-05-25 11:13:26 +12001303 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001304}
Chia-I Wu2648d092014-12-29 14:24:14 +08001305
Tony Barbourfe3351b2015-07-28 10:17:20 -06001306VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device, VkCmdPool pool)
Tony Barbour6d047bf2014-12-10 14:34:45 -07001307{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001308 m_device = device;
Courtney Goeltzenleuchter0c8385b2015-07-13 12:57:11 -06001309
Tony Barbourfe3351b2015-07-28 10:17:20 -06001310 init(*device, vk_testing::CmdBuffer::create_info(pool));
Tony Barbour6d047bf2014-12-10 14:34:45 -07001311}
Tony Barbour471338d2014-12-10 17:28:39 -07001312
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001313VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001314{
Chia-I Wube2b9172015-07-03 11:49:42 +08001315 return handle();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001316}
Tony Barbour471338d2014-12-10 17:28:39 -07001317
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001318VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001319{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001320 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001321 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001322}
1323
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001324VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001325{
1326 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001327 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001328}
1329
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001330VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001331{
Chia-I Wud28343c2014-12-28 15:12:48 +08001332 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001333 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001334}
1335
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001336void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, VkBool32 byRegion, uint32_t memBarrierCount, const void* const* ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001337{
Chia-I Wube2b9172015-07-03 11:49:42 +08001338 vkCmdPipelineBarrier(handle(), src_stages, dest_stages, byRegion, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001339}
1340
Chris Forbesf0796e12015-06-24 14:34:53 +12001341void VkCommandBufferObj::ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001342 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001343{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001344 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001345 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001346 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001347 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1348 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1349 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001350 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001351 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001352
1353 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001354 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001355 srRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001356 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter13dbde02015-08-28 16:47:15 -06001357 srRange.mipLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001358 srRange.baseArrayLayer = 0;
1359 srRange.arraySize = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001360
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001361 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001362 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001363 memory_barrier.outputMask = output_mask;
1364 memory_barrier.inputMask = input_mask;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001365 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001366 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001367 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001368
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001369 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1370 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001371
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001372 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001373 memory_barrier.image = m_renderTargets[i]->image();
1374 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001375 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001376 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001377
Chia-I Wube2b9172015-07-03 11:49:42 +08001378 vkCmdClearColorImage(handle(),
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001379 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001380 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001381
Tony Barbour30cc9e82014-12-17 11:53:55 -07001382 }
1383
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001384 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001385 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001386 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001387 dsRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001388 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter13dbde02015-08-28 16:47:15 -06001389 dsRange.mipLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001390 dsRange.baseArrayLayer = 0;
1391 dsRange.arraySize = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001392
1393 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001394
Cody Northrop3cc85e92015-08-04 10:47:08 -06001395 memory_barrier.oldLayout = memory_barrier.newLayout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001396 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001397 memory_barrier.image = depthStencilObj->handle();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001398 memory_barrier.subresourceRange = dsRange;
1399
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001400 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001401
Chia-I Wube2b9172015-07-03 11:49:42 +08001402 vkCmdClearDepthStencilImage(handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +08001403 depthStencilObj->handle(), VK_IMAGE_LAYOUT_GENERAL,
Chris Forbesd9be82b2015-06-22 17:21:59 +12001404 depth_clear_color, stencil_clear_color,
1405 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001406
1407 // prepare depth buffer for rendering
Chia-I Wu681d7a02015-07-03 13:44:34 +08001408 memory_barrier.image = depthStencilObj->handle();
Cody Northrop3cc85e92015-08-04 10:47:08 -06001409 memory_barrier.newLayout = memory_barrier.oldLayout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001410 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001411 memory_barrier.subresourceRange = dsRange;
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001412 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001413 }
1414}
1415
Tony Barbourc1eb1a52015-07-20 13:00:10 -06001416void VkCommandBufferObj::FillBuffer(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize fill_size, uint32_t data)
1417{
1418 vkCmdFillBuffer( handle(), buffer, offset, fill_size, data);
1419}
1420
Tony Barbour6918cd52015-04-09 12:58:51 -06001421void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001422{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001423 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001424 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001425 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001426 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1427 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1428 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001429 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001430 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001431 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001432 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1433 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1434 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1435 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1436 VK_MEMORY_INPUT_SHADER_READ_BIT |
1437 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1438 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001439 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001440
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001441 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001442 srRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001443 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter13dbde02015-08-28 16:47:15 -06001444 srRange.mipLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001445 srRange.baseArrayLayer = 0;
1446 srRange.arraySize = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001447
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001448 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001449 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001450 memory_barrier.outputMask = output_mask;
1451 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001452 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001453 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001454 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001455
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001456 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1457 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001458
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001459 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001460 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001461 memory_barrier.image = m_renderTargets[i]->image();
1462 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001463 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001464 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001465 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001466}
1467
Chia-I Wu08accc62015-07-07 11:50:03 +08001468void VkCommandBufferObj::BeginRenderPass(const VkRenderPassBeginInfo &info)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001469{
Chia-I Wube2b9172015-07-03 11:49:42 +08001470 vkCmdBeginRenderPass( handle(), &info, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001471}
1472
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001473void VkCommandBufferObj::EndRenderPass()
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001474{
Chia-I Wube2b9172015-07-03 11:49:42 +08001475 vkCmdEndRenderPass(handle());
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001476}
1477
Tony Barbour67e99152015-07-10 14:10:27 -06001478void VkCommandBufferObj::BindDynamicViewportState(VkDynamicViewportState viewportState)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001479{
Tony Barbour67e99152015-07-10 14:10:27 -06001480 vkCmdBindDynamicViewportState( handle(), viewportState);
1481}
1482
Cody Northrop271ba752015-08-26 10:01:32 -06001483void VkCommandBufferObj::BindDynamicLineWidthState(VkDynamicLineWidthState lineWidthState)
Tony Barbour67e99152015-07-10 14:10:27 -06001484{
Cody Northrop271ba752015-08-26 10:01:32 -06001485 vkCmdBindDynamicLineWidthState( handle(), lineWidthState);
Cody Northrop12365112015-08-17 11:10:49 -06001486}
1487
Cody Northrop271ba752015-08-26 10:01:32 -06001488void VkCommandBufferObj::BindDynamicDepthBiasState(VkDynamicDepthBiasState depthBiasState)
Cody Northrop12365112015-08-17 11:10:49 -06001489{
Cody Northrop271ba752015-08-26 10:01:32 -06001490 vkCmdBindDynamicDepthBiasState( handle(), depthBiasState);
Tony Barbour67e99152015-07-10 14:10:27 -06001491}
1492
Cody Northrop271ba752015-08-26 10:01:32 -06001493void VkCommandBufferObj::BindDynamicBlendState(VkDynamicBlendState blendState)
Tony Barbour67e99152015-07-10 14:10:27 -06001494{
Cody Northrop271ba752015-08-26 10:01:32 -06001495 vkCmdBindDynamicBlendState( handle(), blendState);
Tony Barbour67e99152015-07-10 14:10:27 -06001496}
1497
Cody Northrop271ba752015-08-26 10:01:32 -06001498void VkCommandBufferObj::BindDynamicDepthBoundsState(VkDynamicDepthBoundsState depthBoundsState)
Tony Barbour67e99152015-07-10 14:10:27 -06001499{
Cody Northrop271ba752015-08-26 10:01:32 -06001500 vkCmdBindDynamicDepthBoundsState( handle(), depthBoundsState);
Cody Northrop82485a82015-08-18 15:21:16 -06001501}
1502
1503void VkCommandBufferObj::BindDynamicStencilState(VkDynamicStencilState stencilState)
1504{
1505 vkCmdBindDynamicStencilState( handle(), stencilState);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001506}
1507
Tony Barbour6918cd52015-04-09 12:58:51 -06001508void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001509{
1510 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001511}
1512
Tony Barbour6918cd52015-04-09 12:58:51 -06001513void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001514{
Chia-I Wube2b9172015-07-03 11:49:42 +08001515 vkCmdDrawIndexed(handle(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001516}
1517
Tony Barbour6918cd52015-04-09 12:58:51 -06001518void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001519{
Chia-I Wube2b9172015-07-03 11:49:42 +08001520 vkCmdDraw(handle(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001521}
1522
Tony Barbour6918cd52015-04-09 12:58:51 -06001523void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001524{
Tony Barbour67e99152015-07-10 14:10:27 -06001525 VkFence nullFence = { VK_NULL_HANDLE };
1526 QueueCommandBuffer(nullFence);
Tony Barbour09b7ac32015-04-08 10:11:29 -06001527}
1528
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001529void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001530{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001531 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001532
1533 // submit the command buffer to the universal queue
Chia-I Wube2b9172015-07-03 11:49:42 +08001534 err = vkQueueSubmit( m_device->m_queue, 1, &handle(), fence );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001535 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001536
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001537 err = vkQueueWaitIdle( m_device->m_queue );
1538 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001539
1540 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001541 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001542}
1543
Tony Barbour6918cd52015-04-09 12:58:51 -06001544void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001545{
Chia-I Wube2b9172015-07-03 11:49:42 +08001546 vkCmdBindPipeline( handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.handle() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001547}
1548
Tony Barbour6918cd52015-04-09 12:58:51 -06001549void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001550{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001551 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001552
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001553 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wube2b9172015-07-03 11:49:42 +08001554 vkCmdBindDescriptorSets(handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northropd4c1a502015-04-16 13:41:56 -06001555 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001556}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001557
Cody Northropd2ad0342015-08-05 11:15:02 -06001558void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, VkDeviceSize offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001559{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001560 vkCmdBindIndexBuffer(handle(), indexBuffer->handle(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001561}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001562
Tony Barbourd1c35722015-04-16 15:59:00 -06001563void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001564{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001565 vkCmdBindVertexBuffers(handle(), binding, 1, &vertexBuffer->handle(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001566}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001567
Tony Barbour6918cd52015-04-09 12:58:51 -06001568VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001569{
1570 m_initialized = false;
1571}
Tony Barbour6918cd52015-04-09 12:58:51 -06001572bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001573{
1574 return m_initialized;
1575}
1576
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001577VkImageView* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001578{
Chia-I Wu08accc62015-07-07 11:50:03 +08001579 return &m_attachmentBindInfo;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001580}
1581
Chia-I Wu08accc62015-07-07 11:50:03 +08001582void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height, VkFormat format)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001583{
Cody Northrop78e71932015-09-03 16:09:57 -06001584 VkImageCreateInfo image_info = {};
1585 VkImageViewCreateInfo view_info = {};
Tony Barbour1c45ce02015-03-27 17:03:18 -06001586
1587 m_device = device;
1588 m_initialized = true;
Chia-I Wu08accc62015-07-07 11:50:03 +08001589 m_depth_stencil_fmt = format;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001590
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001591 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001592 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001593 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001594 image_info.format = m_depth_stencil_fmt;
1595 image_info.extent.width = width;
1596 image_info.extent.height = height;
1597 image_info.extent.depth = 1;
1598 image_info.mipLevels = 1;
1599 image_info.arraySize = 1;
1600 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001601 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchter660f0ca2015-09-10 14:14:11 -06001602 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001603 image_info.flags = 0;
Tony Barbour3d83f492015-07-28 10:23:02 -06001604 image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1605 image_info.queueFamilyCount = 0;
1606 image_info.pQueueFamilyIndices = NULL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001607 init(*m_device, image_info);
1608
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001609 view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001610 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001611 view_info.image = VK_NULL_HANDLE;
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001612 view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001613 view_info.subresourceRange.baseMipLevel = 0;
1614 view_info.subresourceRange.mipLevels = 1;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001615 view_info.subresourceRange.baseArrayLayer = 0;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001616 view_info.subresourceRange.arraySize = 1;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001617 view_info.flags = 0;
Tony Barbour3d83f492015-07-28 10:23:02 -06001618 view_info.format = m_depth_stencil_fmt;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001619 view_info.image = handle();
Cody Northrop78e71932015-09-03 16:09:57 -06001620 view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001621 m_imageView.init(*m_device, view_info);
Tony Barbour1c45ce02015-03-27 17:03:18 -06001622
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001623 m_attachmentBindInfo = m_imageView.handle();
Tony Barbour1c45ce02015-03-27 17:03:18 -06001624}