blob: a199c1937011ea5d833bff4d3e511619e6c3d7b6 [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 Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600498 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800499 tc.type = type;
500 tc.count = 1;
501 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700502
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800503 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
504 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600505
Chia-I Wu11078b02015-01-04 16:27:24 +0800506 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700507}
Tony Barbour82c39522014-12-04 14:33:33 -0700508
Tony Barbour6918cd52015-04-09 12:58:51 -0600509int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700510{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600511 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600512 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800513 tc.count = 1;
514 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700515
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800516 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Chia-I Wu8c721c62015-07-03 11:49:42 +0800517 tmp.sampler = sampler->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800518 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700519
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800520 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
521 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700522
Chia-I Wu11078b02015-01-04 16:27:24 +0800523 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700524}
Chia-I Wu11078b02015-01-04 16:27:24 +0800525
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500526VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700527{
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800528 return m_pipeline_layout.handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700529}
530
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600531VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700532{
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800533 return m_set->handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700534}
Tony Barboure2c58df2014-11-25 13:18:32 -0700535
Tony Barbour6918cd52015-04-09 12:58:51 -0600536void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700537{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600538 // create VkDescriptorPool
539 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600540 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800541 pool.count = m_type_counts.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600542 pool.pTypeCount = m_type_counts.data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600543 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700544
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600545 // create VkDescriptorSetLayout
546 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800547 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800548 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800549 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800550 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600551 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800552 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700553 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800554
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600555 // create VkDescriptorSetLayout
556 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600557 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800558 layout.count = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600559 layout.pBinding = bindings.data();
Chia-I Wub41393e2015-03-26 15:04:41 +0800560
Chia-I Wu41126e52015-03-26 15:27:55 +0800561 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600562 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800563 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500564
565 // create VkPipelineLayout
566 VkPipelineLayoutCreateInfo pipeline_layout = {};
567 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
568 pipeline_layout.descriptorSetCount = layouts.size();
569 pipeline_layout.pSetLayouts = NULL;
570
571 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700572
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600573 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500574 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800575
Chia-I Wu41126e52015-03-26 15:27:55 +0800576 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800577 size_t imageSamplerCount = 0;
578 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
579 it != m_writes.end(); it++) {
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800580 it->destSet = m_set->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800581 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
582 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800583 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800584
585 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800586 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700587}
Tony Barboure2c58df2014-11-25 13:18:32 -0700588
Tony Barbour6918cd52015-04-09 12:58:51 -0600589VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800590{
591 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800592 m_descriptorInfo.imageView = VK_NULL_HANDLE;
593 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800594}
595
Tony Barbour6918cd52015-04-09 12:58:51 -0600596void VkImageObj::ImageMemoryBarrier(
597 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600598 VkImageAspect aspect,
599 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600600 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600601 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
602 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
603 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
604 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600605 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600606 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600607 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
608 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
609 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
610 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
611 VK_MEMORY_INPUT_SHADER_READ_BIT |
612 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
613 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
614 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600615 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600616{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600617 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
618 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600619 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
620 subresourceRange);
621
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600622 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600623
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600624 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
625 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600626
627 // write barrier to the command buffer
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -0600628 vkCmdPipelineBarrier(cmd_buf->handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600629}
630
Tony Barbour6918cd52015-04-09 12:58:51 -0600631void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600632 VkImageAspect aspect,
633 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600634{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600635 VkFlags output_mask, input_mask;
636 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600637 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600638 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
639 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
640 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600641 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600642 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600643 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600644 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
645 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
646 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
647 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
648 VK_MEMORY_INPUT_SHADER_READ_BIT |
649 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
650 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600651 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600652
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800653 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600654 return;
655 }
656
657 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600658 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600659 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
660 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600661 break;
662
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600663 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600664 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
665 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600666 break;
667
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600668 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600669 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
670 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600671 break;
672
673 default:
674 output_mask = all_cache_outputs;
675 input_mask = all_cache_inputs;
676 break;
677 }
678
679 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800680 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600681}
682
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600683void VkImageObj::SetLayout(VkImageAspect aspect,
684 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600685{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600686 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600687
688 if (image_layout == m_descriptorInfo.imageLayout) {
689 return;
690 }
691
Tony Barbourfe3351b2015-07-28 10:17:20 -0600692 VkCmdPoolCreateInfo cmd_pool_info = {};
693 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
694 cmd_pool_info.pNext = NULL;
695 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
696 cmd_pool_info.flags = 0;
697 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
698 VkCommandBufferObj cmd_buf(m_device, pool.handle());
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600699
700 /* Build command buffer to set image layout in the driver */
701 err = cmd_buf.BeginCommandBuffer();
702 assert(!err);
703
704 SetLayout(&cmd_buf, aspect, image_layout);
705
706 err = cmd_buf.EndCommandBuffer();
707 assert(!err);
708
709 cmd_buf.QueueCommandBuffer();
710}
711
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600712bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600713{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600714 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600715 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600716 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600717
Tony Barbour579f7802015-04-03 15:11:43 -0600718 return true;
719}
720
Tony Barbour6918cd52015-04-09 12:58:51 -0600721void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600722 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600723 VkImageTiling requested_tiling,
724 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800725{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600726 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600727 VkFormatProperties image_fmt;
728 VkImageTiling tiling;
729 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600730
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800731 mipCount = 0;
732
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600733 uint32_t _w = w;
734 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800735 while( ( _w > 0 ) || ( _h > 0 ) )
736 {
737 _w >>= 1;
738 _h >>= 1;
739 mipCount++;
740 }
741
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600742 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), fmt, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600743 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600744
Tony Barbourd1c35722015-04-16 15:59:00 -0600745 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600746 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600747 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600748 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600749 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600750 } else {
751 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
752 }
753 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600754 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600755 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600756 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600757 } else {
758 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
759 }
760
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600761 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600762 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800763 imageCreateInfo.format = fmt;
764 imageCreateInfo.extent.width = w;
765 imageCreateInfo.extent.height = h;
766 imageCreateInfo.mipLevels = mipCount;
767 imageCreateInfo.tiling = tiling;
768
769 imageCreateInfo.usage = usage;
770
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600771 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800772
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600773 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600774 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600775 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600776 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600777 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800778}
779
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600780VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600781{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600782 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600783 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600784
Tony Barbourfe3351b2015-07-28 10:17:20 -0600785 VkCmdPoolCreateInfo cmd_pool_info = {};
786 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
787 cmd_pool_info.pNext = NULL;
788 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
789 cmd_pool_info.flags = 0;
790 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
791 VkCommandBufferObj cmd_buf(m_device, pool.handle());
792
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600793 /* Build command buffer to copy staging texture to usable texture */
794 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600795 assert(!err);
796
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600797 /* TODO: Can we determine image aspect from image object? */
798 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600799 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600800
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600801 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600802 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600803
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600804 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600805 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600806 copy_region.srcSubresource.arrayLayer = 0;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600807 copy_region.srcSubresource.mipLevel = 0;
808 copy_region.srcOffset.x = 0;
809 copy_region.srcOffset.y = 0;
810 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600811 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600812 copy_region.destSubresource.arrayLayer = 0;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600813 copy_region.destSubresource.mipLevel = 0;
814 copy_region.destOffset.x = 0;
815 copy_region.destOffset.y = 0;
816 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600817 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600818
Chia-I Wube2b9172015-07-03 11:49:42 +0800819 vkCmdCopyImage(cmd_buf.handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +0800820 src_image.handle(), src_image.layout(),
821 handle(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600822 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600823
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600824 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600825
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600826 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600827
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600828 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600829 assert(!err);
830
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600831 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600832
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600833 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600834}
835
Tony Barbour6918cd52015-04-09 12:58:51 -0600836VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
837 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700838{
839 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600840 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600841 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600842 void *data;
843 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600844 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600845 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600846
Tony Barboure65788f2015-07-21 17:01:42 -0600847 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 -0600848 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600849
850 if (colors == NULL)
851 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700852
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800853 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700854
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600855 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600856 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600857 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600858 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600859 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600860 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600861 view.channels.r = VK_CHANNEL_SWIZZLE_R;
862 view.channels.g = VK_CHANNEL_SWIZZLE_G;
863 view.channels.b = VK_CHANNEL_SWIZZLE_B;
864 view.channels.a = VK_CHANNEL_SWIZZLE_A;
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -0600865 view.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600866 view.subresourceRange.baseMipLevel = 0;
867 view.subresourceRange.mipLevels = 1;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600868 view.subresourceRange.baseArrayLayer = 0;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600869 view.subresourceRange.arraySize = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700870
Tony Barboure2c58df2014-11-25 13:18:32 -0700871 /* create image */
Tony Barboure65788f2015-07-21 17:01:42 -0600872 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 -0700873
874 /* create image view */
Chia-I Wu681d7a02015-07-03 13:44:34 +0800875 view.image = handle();
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800876 m_textureView.init(*m_device, view);
Chia-I Wu3158bf32015-07-03 11:49:42 +0800877 m_descriptorInfo.imageView = m_textureView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700878
Chia-I Wu681d7a02015-07-03 13:44:34 +0800879 data = stagingImage.MapMemory();
Tony Barboure2c58df2014-11-25 13:18:32 -0700880
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800881 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700882 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800883 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600884 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700885 }
Chia-I Wu681d7a02015-07-03 13:44:34 +0800886 stagingImage.UnmapMemory();
Tony Barbour6918cd52015-04-09 12:58:51 -0600887 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700888}
Tony Barbour82c39522014-12-04 14:33:33 -0700889
Tony Barbour6918cd52015-04-09 12:58:51 -0600890VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700891{
Tony Barboure2c58df2014-11-25 13:18:32 -0700892 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700893
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600894 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800895 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600896 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
897 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
898 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600899 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchter97953352015-09-10 14:08:50 -0600900 samplerCreateInfo.addressModeU = VK_TEX_ADDRESS_MODE_WRAP;
901 samplerCreateInfo.addressModeV = VK_TEX_ADDRESS_MODE_WRAP;
902 samplerCreateInfo.addressModeW = VK_TEX_ADDRESS_MODE_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800903 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600904 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600905 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800906 samplerCreateInfo.minLod = 0.0;
907 samplerCreateInfo.maxLod = 0.0;
Tony Barbour26b17f82015-06-25 16:56:44 -0600908 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Mark Lobodzinski52ac6582015-09-01 15:42:56 -0600909 samplerCreateInfo.unnormalizedCoordinates = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700910
Chia-I Wue9864b52014-12-28 16:32:24 +0800911 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700912}
Tony Barboure2c58df2014-11-25 13:18:32 -0700913
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700914/*
915 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
916 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600917VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700918{
919 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700920 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700921
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800922 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700923}
924
Tony Barbour6918cd52015-04-09 12:58:51 -0600925VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600926{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600927 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600928 if (m_commandBuffer) {
929 delete m_commandBuffer;
Tony Barbour3d83f492015-07-28 10:23:02 -0600930 delete m_cmdPool;
Tony Barbour044703b2015-03-26 12:37:52 -0600931 }
932}
933
Tony Barbour6918cd52015-04-09 12:58:51 -0600934VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700935{
Tony Barboure2c58df2014-11-25 13:18:32 -0700936 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800937 m_commandBuffer = 0;
938
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800939 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700940 m_numVertices = constantCount;
941 m_stride = constantSize;
942
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600943 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800944 const size_t allocationSize = constantCount * constantSize;
Cody Northrop7fb43862015-06-22 14:56:14 -0600945 init_as_src_and_dst(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700946
Chia-I Wu681d7a02015-07-03 13:44:34 +0800947 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +0800948 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800949 memory().unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700950
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800951 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600952 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600953 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu681d7a02015-07-03 13:44:34 +0800954 view_info.buffer = handle();
Tony Barbourd1c35722015-04-16 15:59:00 -0600955 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800956 view_info.offset = 0;
957 view_info.range = allocationSize;
958 m_bufferView.init(*m_device, view_info);
959
Chia-I Wu3158bf32015-07-03 11:49:42 +0800960 this->m_descriptorInfo.bufferView = m_bufferView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700961}
Tony Barbour82c39522014-12-04 14:33:33 -0700962
Tony Barbourd1c35722015-04-16 15:59:00 -0600963void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700964{
Chia-I Wu681d7a02015-07-03 13:44:34 +0800965 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &handle(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700966}
967
968
Tony Barbour6918cd52015-04-09 12:58:51 -0600969void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600970 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600971 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600972 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
973 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
974 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
975 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600976 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600977 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600978 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
979 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
980 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
981 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
982 VK_MEMORY_INPUT_SHADER_READ_BIT |
983 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
984 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
985 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700986{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600987 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700988
Tony Barbour38422802014-12-10 14:36:31 -0700989 if (!m_commandBuffer)
990 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600991 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbourfe3351b2015-07-28 10:17:20 -0600992 VkCmdPoolCreateInfo cmd_pool_info = {};
993 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
994 cmd_pool_info.pNext = NULL;
995 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
996 cmd_pool_info.flags = 0;
997 m_cmdPool = new vk_testing::CmdPool(*m_device, cmd_pool_info);
998 m_commandBuffer = new VkCommandBufferObj(m_device, m_cmdPool->handle());
Tony Barbour38422802014-12-10 14:36:31 -0700999 }
1000 else
1001 {
Chia-I Wua07fee62014-12-28 15:26:08 +08001002 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -07001003 }
1004
Tony Barboure2c58df2014-11-25 13:18:32 -07001005 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001006 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001007 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -06001008 cmd_buf_info.pNext = NULL;
1009 cmd_buf_info.flags = 0;
Cody Northropcc09b9e2015-08-11 11:35:58 -06001010 cmd_buf_info.renderPass = VK_NULL_HANDLE;
1011 cmd_buf_info.subpass = 0;
1012 cmd_buf_info.framebuffer = VK_NULL_HANDLE;
Tony Barbourbdf0a312015-04-01 17:10:07 -06001013
Jon Ashburnc4164b12014-12-31 17:10:47 -07001014 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001015 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001016
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001017 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001018 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001019 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -07001020
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001021 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1022 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001023
1024 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001025 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -07001026
1027 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -07001028 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001029 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001030
Tony Barboure2c58df2014-11-25 13:18:32 -07001031 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001032 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -07001033 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wud9e8e822015-07-03 11:45:55 +08001034 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.handle() );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001035 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001036}
1037
Tony Barbour6918cd52015-04-09 12:58:51 -06001038VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
1039 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001040{
1041
1042}
1043
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001044void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001045{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001046 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001047
1048 m_numVertices = numIndexes;
1049 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001050 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -06001051 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001052 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -06001053 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001054 break;
Tony Barbourd1c35722015-04-16 15:59:00 -06001055 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001056 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -06001057 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001058 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001059 default:
1060 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -06001061 m_stride = 2;
1062 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001063 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001064 }
1065
Chia-I Wua07fee62014-12-28 15:26:08 +08001066 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -06001067 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northrop7fb43862015-06-22 14:56:14 -06001068 init_as_src_and_dst(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001069
Chia-I Wu681d7a02015-07-03 13:44:34 +08001070 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +08001071 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +08001072 memory().unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001073
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001074 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001075 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001076 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001077 view_info.buffer = handle();
Tony Barbourd1c35722015-04-16 15:59:00 -06001078 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001079 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001080 view_info.offset = 0;
1081 view_info.range = allocationSize;
1082 m_bufferView.init(*m_device, view_info);
1083
Chia-I Wu3158bf32015-07-03 11:49:42 +08001084 this->m_descriptorInfo.bufferView = m_bufferView.handle();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001085}
1086
Tony Barbourd1c35722015-04-16 15:59:00 -06001087void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001088{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001089 vkCmdBindIndexBuffer(cmdBuffer, handle(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001090}
Tony Barboure2c58df2014-11-25 13:18:32 -07001091
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001092VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001093{
1094 return m_indexType;
1095}
1096
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001097VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001098{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001099 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001100 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1101 stageInfo->stage = m_stage;
Chia-I Wu4d0c7922015-07-03 11:49:42 +08001102 stageInfo->shader = handle();
Tony Barboure2c58df2014-11-25 13:18:32 -07001103
Tony Barboure2c58df2014-11-25 13:18:32 -07001104 return stageInfo;
1105}
1106
Tony Barbourd1c35722015-04-16 15:59:00 -06001107VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001108{
Tony Barbourb5d2c942015-07-14 13:34:05 -06001109 VkResult U_ASSERT_ONLY err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001110 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001111 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001112 VkShaderModuleCreateInfo moduleCreateInfo;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001113 vk_testing::ShaderModule *module = new vk_testing::ShaderModule();
Tony Barboure2c58df2014-11-25 13:18:32 -07001114 size_t shader_len;
1115
1116 m_stage = stage;
1117 m_device = device;
1118
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001119 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1120 moduleCreateInfo.pNext = NULL;
1121
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001122 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001123 createInfo.pNext = NULL;
1124
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001125 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001126
1127 shader_len = strlen(shader_code);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001128 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1129 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1130 moduleCreateInfo.flags = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001131
Tony Barbourd1c35722015-04-16 15:59:00 -06001132 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001133 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1134 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1135 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1136 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barboure2c58df2014-11-25 13:18:32 -07001137
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001138 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001139
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001140 // Use Reference GLSL to SPV compiler
1141 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001142 moduleCreateInfo.pCode = spv.data();
1143 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1144 moduleCreateInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001145 }
Cody Northrop475663c2015-04-15 11:19:06 -06001146
Tony Barbour823e6ca2015-07-27 13:31:04 -06001147 err = module->init_try(*m_device, moduleCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001148 assert(VK_SUCCESS == err);
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001149
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001150 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1151 createInfo.pNext = NULL;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001152 createInfo.module = module->handle();
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001153 createInfo.pName = "main";
1154 createInfo.flags = 0;
Cody Northropfd4bcfd2015-08-24 15:11:10 -06001155 createInfo.stage = stage;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001156
1157 err = init_try(*m_device, createInfo);
Cody Northrop475663c2015-04-15 11:19:06 -06001158 assert(VK_SUCCESS == err);
Tony Barbour823e6ca2015-07-27 13:31:04 -06001159 framework->m_shader_modules.push_back(module);
Tony Barbourf325bf12014-12-03 15:59:38 -07001160}
Tony Barbour82c39522014-12-04 14:33:33 -07001161
Tony Barbour6918cd52015-04-09 12:58:51 -06001162VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001163{
Tony Barboure2c58df2014-11-25 13:18:32 -07001164 m_device = device;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001165
1166 m_vi_state.pNext = VK_NULL_HANDLE;
1167 m_vi_state.bindingCount = 0;
1168 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1169 m_vi_state.attributeCount = 0;
1170 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1171
Tony Barboure2c58df2014-11-25 13:18:32 -07001172 m_vertexBufferCount = 0;
1173
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001174 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001175 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001176 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001177 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001178
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001179 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001180 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001181 m_rs_state.depthClipEnable = VK_FALSE;
1182 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001183 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001184 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1185 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Cody Northrop12365112015-08-17 11:10:49 -06001186 m_rs_state.depthBiasEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001187
Tony Barboure2c58df2014-11-25 13:18:32 -07001188 memset(&m_cb_state,0,sizeof(m_cb_state));
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001189 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001190 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001191 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1192 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001193
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001194 m_ms_state.pNext = VK_NULL_HANDLE;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001195 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop02e61ed2015-08-04 14:34:54 -06001196 m_ms_state.pSampleMask = NULL;
Tony Barbourdfd533a2015-06-26 10:18:34 -06001197 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001198 m_ms_state.minSampleShading = 0;
1199 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001200
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001201 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001202 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001203 m_vp_state.viewportCount = 1;
Tony Barbourd81b8882015-05-15 09:37:57 -06001204
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001205 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001206 m_ds_state.pNext = VK_NULL_HANDLE,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001207 m_ds_state.depthTestEnable = VK_FALSE;
1208 m_ds_state.depthWriteEnable = VK_FALSE;
Cody Northrop271ba752015-08-26 10:01:32 -06001209 m_ds_state.depthBoundsTestEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001210 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001211 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1212 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1213 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001214 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001215 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001216 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001217};
1218
Tony Barbour6918cd52015-04-09 12:58:51 -06001219void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001220{
1221 m_shaderObjs.push_back(shader);
1222}
1223
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001224void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001225{
1226 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1227 m_vi_state.attributeCount = count;
1228}
1229
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001230void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001231{
1232 m_vi_state.pVertexBindingDescriptions = vi_binding;
1233 m_vi_state.bindingCount = count;
1234}
1235
Tony Barbour6918cd52015-04-09 12:58:51 -06001236void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001237{
1238 m_vertexBufferObjs.push_back(vertexDataBuffer);
1239 m_vertexBufferBindings.push_back(binding);
1240 m_vertexBufferCount++;
1241}
1242
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001243void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineColorBlendAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001244{
Tony Barbourf52346d2015-01-16 14:27:35 -07001245 if (binding+1 > m_colorAttachments.size())
1246 {
1247 m_colorAttachments.resize(binding+1);
1248 }
1249 m_colorAttachments[binding] = *att;
1250}
1251
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001252void VkPipelineObj::SetDepthStencil(VkPipelineDepthStencilStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001253{
Tony Barbourf52346d2015-01-16 14:27:35 -07001254 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1255 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
Cody Northrop271ba752015-08-26 10:01:32 -06001256 m_ds_state.depthBoundsTestEnable = ds_state->depthBoundsTestEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001257 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001258 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1259 m_ds_state.back = ds_state->back;
1260 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001261}
1262
Tony Barbour0ec8cd52015-08-06 09:27:11 -06001263void VkPipelineObj::SetMSAA(VkPipelineMultisampleStateCreateInfo *ms_state)
1264{
1265 memcpy(&m_ms_state, ms_state, sizeof(VkPipelineMultisampleStateCreateInfo));
1266}
1267
Tony Barbour5781e8f2015-08-04 16:23:11 -06001268VkResult VkPipelineObj::CreateVKPipeline(VkPipelineLayout layout, VkRenderPass render_pass)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001269{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001270 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001271
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001272 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001273
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001274 info.stageCount = m_shaderObjs.size();
1275 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1276
Tony Barbour976e1cf2014-12-17 11:57:31 -07001277 for (int i=0; i<m_shaderObjs.size(); i++)
1278 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001279 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001280 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001281 }
1282
Tony Barboure815fd72015-07-27 09:36:24 -06001283 if (m_vi_state.attributeCount && m_vi_state.bindingCount) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001284 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barboure815fd72015-07-27 09:36:24 -06001285 info.pVertexInputState = &m_vi_state;
1286 } else {
1287 info.pVertexInputState = NULL;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001288 }
1289
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001290 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001291 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001292 info.flags = 0;
Tony Barbour5781e8f2015-08-04 16:23:11 -06001293 info.layout = layout;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001294
Tony Barbourf52346d2015-01-16 14:27:35 -07001295 m_cb_state.attachmentCount = m_colorAttachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -06001296 m_cb_state.pAttachments = m_colorAttachments.data();
Tony Barbourf52346d2015-01-16 14:27:35 -07001297
Chia-I Wu08accc62015-07-07 11:50:03 +08001298 info.renderPass = render_pass;
1299 info.subpass = 0;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001300 info.pTessellationState = NULL;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001301 info.pInputAssemblyState = &m_ia_state;
1302 info.pViewportState = &m_vp_state;
1303 info.pRasterState = &m_rs_state;
1304 info.pMultisampleState = &m_ms_state;
1305 info.pDepthStencilState = &m_ds_state;
1306 info.pColorBlendState = &m_cb_state;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001307
Chris Forbes95292b12015-05-25 11:13:26 +12001308 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001309}
Chia-I Wu2648d092014-12-29 14:24:14 +08001310
Tony Barbourfe3351b2015-07-28 10:17:20 -06001311VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device, VkCmdPool pool)
Tony Barbour6d047bf2014-12-10 14:34:45 -07001312{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001313 m_device = device;
Courtney Goeltzenleuchter0c8385b2015-07-13 12:57:11 -06001314
Tony Barbourfe3351b2015-07-28 10:17:20 -06001315 init(*device, vk_testing::CmdBuffer::create_info(pool));
Tony Barbour6d047bf2014-12-10 14:34:45 -07001316}
Tony Barbour471338d2014-12-10 17:28:39 -07001317
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001318VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001319{
Chia-I Wube2b9172015-07-03 11:49:42 +08001320 return handle();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001321}
Tony Barbour471338d2014-12-10 17:28:39 -07001322
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001323VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001324{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001325 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001326 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001327}
1328
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001329VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001330{
1331 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001332 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001333}
1334
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001335VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001336{
Chia-I Wud28343c2014-12-28 15:12:48 +08001337 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001338 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001339}
1340
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001341void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, VkBool32 byRegion, uint32_t memBarrierCount, const void* const* ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001342{
Chia-I Wube2b9172015-07-03 11:49:42 +08001343 vkCmdPipelineBarrier(handle(), src_stages, dest_stages, byRegion, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001344}
1345
Chris Forbesf0796e12015-06-24 14:34:53 +12001346void VkCommandBufferObj::ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001347 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001348{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001349 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001350 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001351 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001352 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1353 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1354 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001355 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001356 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001357
1358 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001359 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001360 srRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001361 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter13dbde02015-08-28 16:47:15 -06001362 srRange.mipLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001363 srRange.baseArrayLayer = 0;
1364 srRange.arraySize = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001365
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001366 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001367 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001368 memory_barrier.outputMask = output_mask;
1369 memory_barrier.inputMask = input_mask;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001370 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001371 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001372 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001373
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001374 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1375 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001376
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001377 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001378 memory_barrier.image = m_renderTargets[i]->image();
1379 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001380 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001381 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001382
Chia-I Wube2b9172015-07-03 11:49:42 +08001383 vkCmdClearColorImage(handle(),
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001384 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001385 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001386
Tony Barbour30cc9e82014-12-17 11:53:55 -07001387 }
1388
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001389 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001390 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001391 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001392 dsRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001393 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter13dbde02015-08-28 16:47:15 -06001394 dsRange.mipLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001395 dsRange.baseArrayLayer = 0;
1396 dsRange.arraySize = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001397
1398 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001399
Cody Northrop3cc85e92015-08-04 10:47:08 -06001400 memory_barrier.oldLayout = memory_barrier.newLayout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001401 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001402 memory_barrier.image = depthStencilObj->handle();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001403 memory_barrier.subresourceRange = dsRange;
1404
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001405 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001406
Chia-I Wube2b9172015-07-03 11:49:42 +08001407 vkCmdClearDepthStencilImage(handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +08001408 depthStencilObj->handle(), VK_IMAGE_LAYOUT_GENERAL,
Chris Forbesd9be82b2015-06-22 17:21:59 +12001409 depth_clear_color, stencil_clear_color,
1410 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001411
1412 // prepare depth buffer for rendering
Chia-I Wu681d7a02015-07-03 13:44:34 +08001413 memory_barrier.image = depthStencilObj->handle();
Cody Northrop3cc85e92015-08-04 10:47:08 -06001414 memory_barrier.newLayout = memory_barrier.oldLayout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001415 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001416 memory_barrier.subresourceRange = dsRange;
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001417 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001418 }
1419}
1420
Tony Barbourc1eb1a52015-07-20 13:00:10 -06001421void VkCommandBufferObj::FillBuffer(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize fill_size, uint32_t data)
1422{
1423 vkCmdFillBuffer( handle(), buffer, offset, fill_size, data);
1424}
1425
Tony Barbour6918cd52015-04-09 12:58:51 -06001426void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001427{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001428 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001429 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001430 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001431 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1432 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1433 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001434 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001435 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001436 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001437 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1438 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1439 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1440 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1441 VK_MEMORY_INPUT_SHADER_READ_BIT |
1442 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1443 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001444 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001445
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001446 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001447 srRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001448 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter13dbde02015-08-28 16:47:15 -06001449 srRange.mipLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001450 srRange.baseArrayLayer = 0;
1451 srRange.arraySize = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001452
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001453 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001454 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001455 memory_barrier.outputMask = output_mask;
1456 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001457 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001458 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001459 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001460
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001461 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1462 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001463
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001464 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001465 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001466 memory_barrier.image = m_renderTargets[i]->image();
1467 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001468 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001469 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001470 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001471}
1472
Chia-I Wu08accc62015-07-07 11:50:03 +08001473void VkCommandBufferObj::BeginRenderPass(const VkRenderPassBeginInfo &info)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001474{
Chia-I Wube2b9172015-07-03 11:49:42 +08001475 vkCmdBeginRenderPass( handle(), &info, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001476}
1477
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001478void VkCommandBufferObj::EndRenderPass()
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001479{
Chia-I Wube2b9172015-07-03 11:49:42 +08001480 vkCmdEndRenderPass(handle());
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001481}
1482
Tony Barbour67e99152015-07-10 14:10:27 -06001483void VkCommandBufferObj::BindDynamicViewportState(VkDynamicViewportState viewportState)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001484{
Tony Barbour67e99152015-07-10 14:10:27 -06001485 vkCmdBindDynamicViewportState( handle(), viewportState);
1486}
1487
Cody Northrop271ba752015-08-26 10:01:32 -06001488void VkCommandBufferObj::BindDynamicLineWidthState(VkDynamicLineWidthState lineWidthState)
Tony Barbour67e99152015-07-10 14:10:27 -06001489{
Cody Northrop271ba752015-08-26 10:01:32 -06001490 vkCmdBindDynamicLineWidthState( handle(), lineWidthState);
Cody Northrop12365112015-08-17 11:10:49 -06001491}
1492
Cody Northrop271ba752015-08-26 10:01:32 -06001493void VkCommandBufferObj::BindDynamicDepthBiasState(VkDynamicDepthBiasState depthBiasState)
Cody Northrop12365112015-08-17 11:10:49 -06001494{
Cody Northrop271ba752015-08-26 10:01:32 -06001495 vkCmdBindDynamicDepthBiasState( handle(), depthBiasState);
Tony Barbour67e99152015-07-10 14:10:27 -06001496}
1497
Cody Northrop271ba752015-08-26 10:01:32 -06001498void VkCommandBufferObj::BindDynamicBlendState(VkDynamicBlendState blendState)
Tony Barbour67e99152015-07-10 14:10:27 -06001499{
Cody Northrop271ba752015-08-26 10:01:32 -06001500 vkCmdBindDynamicBlendState( handle(), blendState);
Tony Barbour67e99152015-07-10 14:10:27 -06001501}
1502
Cody Northrop271ba752015-08-26 10:01:32 -06001503void VkCommandBufferObj::BindDynamicDepthBoundsState(VkDynamicDepthBoundsState depthBoundsState)
Tony Barbour67e99152015-07-10 14:10:27 -06001504{
Cody Northrop271ba752015-08-26 10:01:32 -06001505 vkCmdBindDynamicDepthBoundsState( handle(), depthBoundsState);
Cody Northrop82485a82015-08-18 15:21:16 -06001506}
1507
1508void VkCommandBufferObj::BindDynamicStencilState(VkDynamicStencilState stencilState)
1509{
1510 vkCmdBindDynamicStencilState( handle(), stencilState);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001511}
1512
Tony Barbour6918cd52015-04-09 12:58:51 -06001513void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001514{
1515 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001516}
1517
Tony Barbour6918cd52015-04-09 12:58:51 -06001518void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001519{
Chia-I Wube2b9172015-07-03 11:49:42 +08001520 vkCmdDrawIndexed(handle(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001521}
1522
Tony Barbour6918cd52015-04-09 12:58:51 -06001523void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001524{
Chia-I Wube2b9172015-07-03 11:49:42 +08001525 vkCmdDraw(handle(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001526}
1527
Tony Barbour6918cd52015-04-09 12:58:51 -06001528void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001529{
Tony Barbour67e99152015-07-10 14:10:27 -06001530 VkFence nullFence = { VK_NULL_HANDLE };
1531 QueueCommandBuffer(nullFence);
Tony Barbour09b7ac32015-04-08 10:11:29 -06001532}
1533
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001534void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001535{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001536 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001537
1538 // submit the command buffer to the universal queue
Chia-I Wube2b9172015-07-03 11:49:42 +08001539 err = vkQueueSubmit( m_device->m_queue, 1, &handle(), fence );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001540 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001541
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001542 err = vkQueueWaitIdle( m_device->m_queue );
1543 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001544
1545 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001546 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001547}
1548
Tony Barbour6918cd52015-04-09 12:58:51 -06001549void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001550{
Chia-I Wube2b9172015-07-03 11:49:42 +08001551 vkCmdBindPipeline( handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.handle() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001552}
1553
Tony Barbour6918cd52015-04-09 12:58:51 -06001554void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001555{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001556 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001557
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001558 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wube2b9172015-07-03 11:49:42 +08001559 vkCmdBindDescriptorSets(handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northropd4c1a502015-04-16 13:41:56 -06001560 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001561}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001562
Cody Northropd2ad0342015-08-05 11:15:02 -06001563void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, VkDeviceSize offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001564{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001565 vkCmdBindIndexBuffer(handle(), indexBuffer->handle(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001566}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001567
Tony Barbourd1c35722015-04-16 15:59:00 -06001568void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001569{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001570 vkCmdBindVertexBuffers(handle(), binding, 1, &vertexBuffer->handle(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001571}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001572
Tony Barbour6918cd52015-04-09 12:58:51 -06001573VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001574{
1575 m_initialized = false;
1576}
Tony Barbour6918cd52015-04-09 12:58:51 -06001577bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001578{
1579 return m_initialized;
1580}
1581
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001582VkImageView* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001583{
Chia-I Wu08accc62015-07-07 11:50:03 +08001584 return &m_attachmentBindInfo;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001585}
1586
Chia-I Wu08accc62015-07-07 11:50:03 +08001587void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height, VkFormat format)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001588{
Cody Northrop78e71932015-09-03 16:09:57 -06001589 VkImageCreateInfo image_info = {};
1590 VkImageViewCreateInfo view_info = {};
Tony Barbour1c45ce02015-03-27 17:03:18 -06001591
1592 m_device = device;
1593 m_initialized = true;
Chia-I Wu08accc62015-07-07 11:50:03 +08001594 m_depth_stencil_fmt = format;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001595
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001596 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001597 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001598 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001599 image_info.format = m_depth_stencil_fmt;
1600 image_info.extent.width = width;
1601 image_info.extent.height = height;
1602 image_info.extent.depth = 1;
1603 image_info.mipLevels = 1;
1604 image_info.arraySize = 1;
1605 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001606 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchter660f0ca2015-09-10 14:14:11 -06001607 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001608 image_info.flags = 0;
Tony Barbour3d83f492015-07-28 10:23:02 -06001609 image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1610 image_info.queueFamilyCount = 0;
1611 image_info.pQueueFamilyIndices = NULL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001612 init(*m_device, image_info);
1613
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001614 view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001615 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001616 view_info.image = VK_NULL_HANDLE;
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001617 view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001618 view_info.subresourceRange.baseMipLevel = 0;
1619 view_info.subresourceRange.mipLevels = 1;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001620 view_info.subresourceRange.baseArrayLayer = 0;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001621 view_info.subresourceRange.arraySize = 1;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001622 view_info.flags = 0;
Tony Barbour3d83f492015-07-28 10:23:02 -06001623 view_info.format = m_depth_stencil_fmt;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001624 view_info.image = handle();
Cody Northrop78e71932015-09-03 16:09:57 -06001625 view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001626 m_imageView.init(*m_device, view_info);
Tony Barbour1c45ce02015-03-27 17:03:18 -06001627
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -06001628 m_attachmentBindInfo = m_imageView.handle();
Tony Barbour1c45ce02015-03-27 17:03:18 -06001629}