blob: 30f3d01c8073e82e02b349edfbb7a83e7aaeccc8 [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Ian Elliott7e40db92015-08-21 15:09:33 -060029#include <vk_ext_khr_swapchain.h>
30#include <vk_ext_khr_device_swapchain.h>
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060031
Jon Ashburn83a64252015-04-15 11:31:12 -060032#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
Tony Barbour6a3faf02015-07-23 10:36:18 -060033#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
34{ \
35 fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
36 assert(fp##entrypoint != NULL); \
37}
Jon Ashburn83a64252015-04-15 11:31:12 -060038
Tony Barbour6918cd52015-04-09 12:58:51 -060039VkRenderFramework::VkRenderFramework() :
Tony Barbourfe3351b2015-07-28 10:17:20 -060040 m_cmdBuffer(),
Tony Barbourf77fd652015-04-09 11:07:02 -060041 m_renderPass(VK_NULL_HANDLE),
42 m_framebuffer(VK_NULL_HANDLE),
Cody Northrop271ba752015-08-26 10:01:32 -060043 m_stateLineWidth( VK_NULL_HANDLE ),
44 m_stateDepthBias( VK_NULL_HANDLE ),
45 m_stateBlend( VK_NULL_HANDLE ),
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060046 m_stateViewport( VK_NULL_HANDLE ),
Cody Northrop271ba752015-08-26 10:01:32 -060047 m_stateDepthBounds( VK_NULL_HANDLE ),
Cody Northrop82485a82015-08-18 15:21:16 -060048 m_stateStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060049 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070050 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060051 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
52 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Tony Barbour71bd4b32015-07-21 17:00:26 -060053 m_clear_via_load_op( true ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070054 m_depth_clear_color( 1.0 ),
Courtney Goeltzenleuchterb6c9aca2015-06-08 16:19:37 -060055 m_stencil_clear_color( 0 ),
56 m_depthStencil( NULL ),
57 m_dbgCreateMsgCallback( VK_NULL_HANDLE ),
58 m_dbgDestroyMsgCallback( VK_NULL_HANDLE ),
59 m_globalMsgCallback( VK_NULL_HANDLE ),
60 m_devMsgCallback( VK_NULL_HANDLE )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060061{
Tony Barbour1c45ce02015-03-27 17:03:18 -060062
Chia-I Wu08accc62015-07-07 11:50:03 +080063 memset(&m_renderPassBeginInfo, 0, sizeof(m_renderPassBeginInfo));
64 m_renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
65
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070066 // clear the back buffer to dark grey
Cody Northrop85789d52015-08-25 15:26:38 -060067 m_clear_color.float32[0] = 0.25f;
68 m_clear_color.float32[1] = 0.25f;
69 m_clear_color.float32[2] = 0.25f;
70 m_clear_color.float32[3] = 0.0f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060071}
72
Tony Barbour6918cd52015-04-09 12:58:51 -060073VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060074{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060075
76}
77
Tony Barbour6918cd52015-04-09 12:58:51 -060078void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060079{
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060080 std::vector<const char*> instance_layer_names;
81 std::vector<const char*> device_layer_names;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060082 std::vector<const char*> instance_extension_names;
83 std::vector<const char*> device_extension_names;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060084 InitFramework(
85 instance_layer_names, device_layer_names,
86 instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060087}
88
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060089void VkRenderFramework::InitFramework(
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060090 std::vector<const char *> instance_layer_names,
91 std::vector<const char *> device_layer_names,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060092 std::vector<const char *> instance_extension_names,
93 std::vector<const char *> device_extension_names,
94 PFN_vkDbgMsgCallback dbgFunction,
95 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060096{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060097 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060098 std::vector<VkExtensionProperties> instance_extensions;
99 std::vector<VkExtensionProperties> device_extensions;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600100 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600101
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600102 /* TODO: Verify requested extensions are available */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600103
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600104 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -0600105 instInfo.pNext = NULL;
106 instInfo.pAppInfo = &app_info;
107 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600108 instInfo.layerCount = instance_layer_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600109 instInfo.ppEnabledLayerNames = instance_layer_names.data();
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600110 instInfo.extensionCount = instance_extension_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600111 instInfo.ppEnabledExtensionNames = instance_extension_names.data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600112 err = vkCreateInstance(&instInfo, &this->inst);
113 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600114
Jon Ashburn83a64252015-04-15 11:31:12 -0600115 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
116 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
117 ASSERT_VK_SUCCESS(err);
118 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600119 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700120 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600121 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600122 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
123 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
124 if (m_dbgCreateMsgCallback) {
125 err = m_dbgCreateMsgCallback(this->inst,
126 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
127 dbgFunction,
128 userData,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600129 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600130 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600131
132 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
133 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600134 }
Tony Barbour15524c32015-04-29 17:34:29 -0600135 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600136
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600137 /* TODO: Verify requested physical device extensions are available */
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600138 m_device = new VkDeviceObj(0, objs[0], device_layer_names, device_extension_names);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600139
140 /* Now register callback on device */
141 if (0) {
142 if (m_dbgCreateMsgCallback) {
143 err = m_dbgCreateMsgCallback(this->inst,
144 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
145 dbgFunction,
146 userData,
147 &m_devMsgCallback);
148 ASSERT_VK_SUCCESS(err);
149 }
150 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600151 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600152
Tony Barbour6918cd52015-04-09 12:58:51 -0600153 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600154}
155
Tony Barbour6918cd52015-04-09 12:58:51 -0600156void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600157{
Cody Northrop271ba752015-08-26 10:01:32 -0600158 if (m_stateBlend) vkDestroyDynamicBlendState(device(), m_stateBlend);
159 if (m_stateDepthBounds) vkDestroyDynamicDepthBoundsState(device(), m_stateDepthBounds);
Cody Northrop82485a82015-08-18 15:21:16 -0600160 if (m_stateStencil) vkDestroyDynamicStencilState(device(), m_stateStencil);
Cody Northrop271ba752015-08-26 10:01:32 -0600161 if (m_stateLineWidth) vkDestroyDynamicLineWidthState(device(), m_stateLineWidth);
162 if (m_stateDepthBias) vkDestroyDynamicDepthBiasState(device(), m_stateDepthBias);
Tony Barbourfe3351b2015-07-28 10:17:20 -0600163 if (m_cmdBuffer)
164 delete m_cmdBuffer;
Cody Northrope62183e2015-07-09 18:08:05 -0600165 if (m_cmdPool) vkDestroyCommandPool(device(), m_cmdPool);
Tony Barbour67e99152015-07-10 14:10:27 -0600166 if (m_framebuffer) vkDestroyFramebuffer(device(), m_framebuffer);
167 if (m_renderPass) vkDestroyRenderPass(device(), m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600168
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600169 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
170 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
171
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600172 if (m_stateViewport) {
Tony Barbour67e99152015-07-10 14:10:27 -0600173 vkDestroyDynamicViewportState(device(), m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600174 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600175 while (!m_renderTargets.empty()) {
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600176 vkDestroyImageView(device(), m_renderTargets.back()->targetView(m_render_target_fmt));
Tony Barbour67e99152015-07-10 14:10:27 -0600177 vkDestroyImage(device(), m_renderTargets.back()->image());
Mike Stroyanb050c682015-04-17 12:36:38 -0600178 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600179 m_renderTargets.pop_back();
180 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600181
Tony Barbour1c45ce02015-03-27 17:03:18 -0600182 delete m_depthStencil;
Tony Barbour823e6ca2015-07-27 13:31:04 -0600183 while (!m_shader_modules.empty())
184 {
185 delete m_shader_modules.back();
186 m_shader_modules.pop_back();
187 }
Tony Barbour1c45ce02015-03-27 17:03:18 -0600188
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600189 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800190 delete m_device;
Chris Forbesbe2fae62015-07-07 11:02:22 +1200191 if (this->inst) vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600192}
193
Tony Barbour6918cd52015-04-09 12:58:51 -0600194void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600195{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600196 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600197
Tony Barbour6a3faf02015-07-23 10:36:18 -0600198 // Get the list of VkFormat's that are supported:
Ian Elliott7e40db92015-08-21 15:09:33 -0600199 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
Ian Elliott8b139792015-08-07 11:51:12 -0600200 uint32_t formatCount;
Ian Elliott7e40db92015-08-21 15:09:33 -0600201 VkSurfaceDescriptionKHR surface_description;
202 surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Tony Barbour6a3faf02015-07-23 10:36:18 -0600203 surface_description.pNext = NULL;
Ian Elliott7e40db92015-08-21 15:09:33 -0600204 GET_DEVICE_PROC_ADDR(device(), GetSurfaceFormatsKHR);
205 err = fpGetSurfaceFormatsKHR(device(),
206 (VkSurfaceDescriptionKHR *) &surface_description,
Ian Elliott8b139792015-08-07 11:51:12 -0600207 &formatCount, NULL);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600208 ASSERT_VK_SUCCESS(err);
Ian Elliott7e40db92015-08-21 15:09:33 -0600209 VkSurfaceFormatKHR *surfFormats =
210 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
211 err = fpGetSurfaceFormatsKHR(device(),
212 (VkSurfaceDescriptionKHR *) &surface_description,
Ian Elliott8b139792015-08-07 11:51:12 -0600213 &formatCount, surfFormats);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600214 ASSERT_VK_SUCCESS(err);
215 m_render_target_fmt = surfFormats[0].format;
216 free(surfFormats);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600217
Cody Northrop271ba752015-08-26 10:01:32 -0600218 VkDynamicLineWidthStateCreateInfo lineWidth = {};
219 lineWidth.sType = VK_STRUCTURE_TYPE_DYNAMIC_LINE_WIDTH_STATE_CREATE_INFO;
220 lineWidth.lineWidth = 1;
221 err = vkCreateDynamicLineWidthState( device(), &lineWidth, &m_stateLineWidth );
Cody Northrop12365112015-08-17 11:10:49 -0600222 ASSERT_VK_SUCCESS(err);
Tony Barbourf52346d2015-01-16 14:27:35 -0700223
Cody Northrop271ba752015-08-26 10:01:32 -0600224 VkDynamicDepthBiasStateCreateInfo depthBias = {};
225 depthBias.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BIAS_STATE_CREATE_INFO;
226 depthBias.depthBias = 0.0f;
227 depthBias.depthBiasClamp = 0.0f;
228 depthBias.slopeScaledDepthBias = 0.0f;
229 err = vkCreateDynamicDepthBiasState( device(), &depthBias, &m_stateDepthBias );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600230 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600231
Cody Northrop271ba752015-08-26 10:01:32 -0600232 VkDynamicBlendStateCreateInfo blend = {};
233 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_BLEND_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600234 blend.blendConst[0] = 1.0f;
235 blend.blendConst[1] = 1.0f;
236 blend.blendConst[2] = 1.0f;
237 blend.blendConst[3] = 1.0f;
Cody Northrop271ba752015-08-26 10:01:32 -0600238 err = vkCreateDynamicBlendState(device(), &blend, &m_stateBlend);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600239 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600240
Cody Northrop271ba752015-08-26 10:01:32 -0600241 VkDynamicDepthBoundsStateCreateInfo depth = {};
242 depth.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BOUNDS_STATE_CREATE_INFO;
Cody Northrop82485a82015-08-18 15:21:16 -0600243 depth.minDepthBounds = 0.f;
244 depth.maxDepthBounds = 1.f;
Cody Northrop271ba752015-08-26 10:01:32 -0600245 err = vkCreateDynamicDepthBoundsState( device(), &depth, &m_stateDepthBounds );
Cody Northrop82485a82015-08-18 15:21:16 -0600246 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600247
Cody Northrop82485a82015-08-18 15:21:16 -0600248 VkDynamicStencilStateCreateInfo stencil = {};
249 stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_STENCIL_STATE_CREATE_INFO;
Cody Northrop271ba752015-08-26 10:01:32 -0600250 stencil.stencilCompareMask = 0xff;
Cody Northrop82485a82015-08-18 15:21:16 -0600251 stencil.stencilWriteMask = 0xff;
252 stencil.stencilReference = 0;
253 err = vkCreateDynamicStencilState( device(), &stencil, &stencil, &m_stateStencil );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600254 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600255
Cody Northrope62183e2015-07-09 18:08:05 -0600256 VkCmdPoolCreateInfo cmd_pool_info;
257 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
258 cmd_pool_info.pNext = NULL,
259 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
260 cmd_pool_info.flags = 0,
261 err = vkCreateCommandPool(device(), &cmd_pool_info, &m_cmdPool);
262 assert(!err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600263
Tony Barbourfe3351b2015-07-28 10:17:20 -0600264 m_cmdBuffer = new VkCommandBufferObj(m_device, m_cmdPool);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600265}
266
Tony Barbour6918cd52015-04-09 12:58:51 -0600267void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600268{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600269 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600270
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600271 VkViewport viewport;
Chris Forbesd9be82b2015-06-22 17:21:59 +1200272 VkRect2D scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600273
Tony Barbour67e99152015-07-10 14:10:27 -0600274 VkDynamicViewportStateCreateInfo viewportCreate = {};
275 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700276 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700277 viewport.originX = 0;
278 viewport.originY = 0;
279 viewport.width = 1.f * width;
280 viewport.height = 1.f * height;
281 viewport.minDepth = 0.f;
282 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600283 scissor.extent.width = (int32_t) width;
284 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700285 scissor.offset.x = 0;
286 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700287 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700288 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700289
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600290 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
291 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600292 m_width = width;
293 m_height = height;
294}
295
Tony Barbour6918cd52015-04-09 12:58:51 -0600296void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600297{
298 InitViewport(m_width, m_height);
299}
Tony Barbour6918cd52015-04-09 12:58:51 -0600300void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600301{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700302 InitRenderTarget(1);
303}
304
Tony Barbour6918cd52015-04-09 12:58:51 -0600305void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700306{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600307 InitRenderTarget(targets, NULL);
308}
309
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600310void VkRenderFramework::InitRenderTarget(VkImageView *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600311{
312 InitRenderTarget(1, dsBinding);
313}
314
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600315void VkRenderFramework::InitRenderTarget(uint32_t targets, VkImageView *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600316{
Chia-I Wu08accc62015-07-07 11:50:03 +0800317 std::vector<VkAttachmentDescription> attachments;
318 std::vector<VkAttachmentReference> color_references;
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600319 std::vector<VkImageView> bindings;
Cody Northropd2ad0342015-08-05 11:15:02 -0600320 attachments.reserve(targets + 1); // +1 for dsBinding
Chia-I Wu08accc62015-07-07 11:50:03 +0800321 color_references.reserve(targets);
Cody Northropd2ad0342015-08-05 11:15:02 -0600322 bindings.reserve(targets + 1); // +1 for dsBinding
Tony Barbour1c45ce02015-03-27 17:03:18 -0600323
Chia-I Wu08accc62015-07-07 11:50:03 +0800324 VkAttachmentDescription att = {};
325 att.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION;
326 att.pNext = NULL;
327 att.format = m_render_target_fmt;
328 att.samples = 1;
329 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
330 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
331 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
332 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
333 att.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
334 att.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chia-I Wuecebf752014-12-05 10:45:15 +0800335
Chia-I Wu08accc62015-07-07 11:50:03 +0800336 VkAttachmentReference ref = {};
337 ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
338
339 m_renderPassClearValues.clear();
340 VkClearValue clear = {};
341 clear.color = m_clear_color;
342
Courtney Goeltzenleuchter5861a1b2015-09-01 17:30:39 -0600343 VkImageView bind = {};
Chia-I Wu08accc62015-07-07 11:50:03 +0800344
345 for (uint32_t i = 0; i < targets; i++) {
346 attachments.push_back(att);
347
348 ref.attachment = i;
349 color_references.push_back(ref);
350
351 m_renderPassClearValues.push_back(clear);
352
Tony Barbour6918cd52015-04-09 12:58:51 -0600353 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600354
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600355 VkFormatProperties props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600356 VkResult err;
357
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600358 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), m_render_target_fmt, &props);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600359 ASSERT_VK_SUCCESS(err);
360
361 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600362 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600363 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
364 VK_IMAGE_TILING_LINEAR);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600365 }
366 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600367 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600368 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
369 VK_IMAGE_TILING_OPTIMAL);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600370 }
371 else {
372 FAIL() << "Neither Linear nor Optimal allowed for render target";
373 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600374
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600375 m_renderTargets.push_back(img);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600376 bind = img->targetView(m_render_target_fmt);
Chia-I Wu08accc62015-07-07 11:50:03 +0800377 bindings.push_back(bind);
Chia-I Wuecebf752014-12-05 10:45:15 +0800378 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600379
Chia-I Wu08accc62015-07-07 11:50:03 +0800380 VkSubpassDescription subpass = {};
381 subpass.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION;
382 subpass.pNext = NULL;
383 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
384 subpass.flags = 0;
385 subpass.inputCount = 0;
Cody Northropa505dda2015-08-04 11:16:41 -0600386 subpass.pInputAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800387 subpass.colorCount = targets;
Cody Northropa505dda2015-08-04 11:16:41 -0600388 subpass.pColorAttachments = color_references.data();
389 subpass.pResolveAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800390
391 if (dsBinding) {
392 att.format = m_depth_stencil_fmt;
Tony Barbour71bd4b32015-07-21 17:00:26 -0600393 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;;
Chia-I Wu08accc62015-07-07 11:50:03 +0800394 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
395 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
396 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Cody Northrop78e71932015-09-03 16:09:57 -0600397 att.initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
398 att.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800399 attachments.push_back(att);
400
Cody Northrop85789d52015-08-25 15:26:38 -0600401 clear.depthStencil.depth = m_depth_clear_color;
402 clear.depthStencil.stencil = m_stencil_clear_color;
Chia-I Wu08accc62015-07-07 11:50:03 +0800403 m_renderPassClearValues.push_back(clear);
404
405 bindings.push_back(*dsBinding);
406
407 subpass.depthStencilAttachment.attachment = targets;
Cody Northrop78e71932015-09-03 16:09:57 -0600408 subpass.depthStencilAttachment.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800409 } else {
410 subpass.depthStencilAttachment.attachment = VK_ATTACHMENT_UNUSED;
411 }
412
413 subpass.preserveCount = 0;
Cody Northropa505dda2015-08-04 11:16:41 -0600414 subpass.pPreserveAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800415
416 VkRenderPassCreateInfo rp_info = {};
417 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
418 rp_info.attachmentCount = attachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600419 rp_info.pAttachments = attachments.data();
Chia-I Wu08accc62015-07-07 11:50:03 +0800420 rp_info.subpassCount = 1;
421 rp_info.pSubpasses = &subpass;
422
423 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
424
425 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600426 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600427 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600428 fb_info.pNext = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800429 fb_info.renderPass = m_renderPass;
430 fb_info.attachmentCount = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600431 fb_info.pAttachments = bindings.data();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600432 fb_info.width = (uint32_t)m_width;
433 fb_info.height = (uint32_t)m_height;
434 fb_info.layers = 1;
435
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600436 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600437
Chia-I Wu08accc62015-07-07 11:50:03 +0800438 m_renderPassBeginInfo.renderPass = m_renderPass;
439 m_renderPassBeginInfo.framebuffer = m_framebuffer;
Cody Northropd2ad0342015-08-05 11:15:02 -0600440 m_renderPassBeginInfo.renderArea.extent.width = (int32_t) m_width;
441 m_renderPassBeginInfo.renderArea.extent.height = (int32_t) m_height;
Cody Northrop23dd89d2015-08-04 11:51:03 -0600442 m_renderPassBeginInfo.clearValueCount = m_renderPassClearValues.size();
443 m_renderPassBeginInfo.pClearValues = m_renderPassClearValues.data();
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600444}
445
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600446
447
Tony Barbourd1c35722015-04-16 15:59:00 -0600448VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600449 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800450{
451 init();
452
Chia-I Wu999f0482015-07-03 10:32:05 +0800453 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600454 queue_props = phy().queue_properties().data();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800455}
456
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600457VkDeviceObj::VkDeviceObj(uint32_t id,
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600458 VkPhysicalDevice obj, std::vector<const char *> &layer_names,
459 std::vector<const char *> &extension_names) :
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600460 vk_testing::Device(obj), id(id)
461{
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600462 init(layer_names, extension_names);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600463
Chia-I Wu999f0482015-07-03 10:32:05 +0800464 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600465 queue_props = phy().queue_properties().data();
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600466}
467
Tony Barbour6918cd52015-04-09 12:58:51 -0600468void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800469{
470 ASSERT_NE(true, graphics_queues().empty());
Chia-I Wudf12ffd2015-07-03 10:53:18 +0800471 m_queue = graphics_queues()[0]->handle();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800472}
473
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600474VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
475 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700476{
Tony Barboure2c58df2014-11-25 13:18:32 -0700477
478}
479
Tony Barbour6918cd52015-04-09 12:58:51 -0600480VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700481{
Chia-I Wu11078b02015-01-04 16:27:24 +0800482 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700483}
Tony Barbour82c39522014-12-04 14:33:33 -0700484
Tony Barbour6918cd52015-04-09 12:58:51 -0600485int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700486{
Chia-I Wu11078b02015-01-04 16:27:24 +0800487 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600488 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600489 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800490 tc.count = 1;
491 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700492
Chia-I Wu11078b02015-01-04 16:27:24 +0800493 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700494}
Tony Barbour82c39522014-12-04 14:33:33 -0700495
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600496int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700497{
Courtney Goeltzenleuchter6eb1aeb2015-09-11 15:29:21 -0600498 assert(type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
499 type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
500 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
501 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600502 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800503 tc.type = type;
504 tc.count = 1;
505 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700506
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800507 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
508 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600509
Chia-I Wu11078b02015-01-04 16:27:24 +0800510 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700511}
Tony Barbour82c39522014-12-04 14:33:33 -0700512
Tony Barbour6918cd52015-04-09 12:58:51 -0600513int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700514{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600515 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600516 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800517 tc.count = 1;
518 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700519
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800520 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Chia-I Wu8c721c62015-07-03 11:49:42 +0800521 tmp.sampler = sampler->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800522 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700523
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800524 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
525 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700526
Chia-I Wu11078b02015-01-04 16:27:24 +0800527 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700528}
Chia-I Wu11078b02015-01-04 16:27:24 +0800529
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500530VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700531{
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800532 return m_pipeline_layout.handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700533}
534
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600535VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700536{
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800537 return m_set->handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700538}
Tony Barboure2c58df2014-11-25 13:18:32 -0700539
Tony Barbour6918cd52015-04-09 12:58:51 -0600540void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700541{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600542 // create VkDescriptorPool
543 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600544 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800545 pool.count = m_type_counts.size();
Courtney Goeltzenleuchterfe908d32015-09-16 16:12:45 -0600546 pool.poolUsage = VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT;
547 pool.maxSets = 1;
Tony Barbour482c6092015-07-27 09:37:48 -0600548 pool.pTypeCount = m_type_counts.data();
Courtney Goeltzenleuchterfe908d32015-09-16 16:12:45 -0600549 init(*m_device, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700550
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600551 // create VkDescriptorSetLayout
552 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800553 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800554 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800555 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800556 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600557 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800558 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700559 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800560
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600561 // create VkDescriptorSetLayout
562 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600563 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800564 layout.count = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600565 layout.pBinding = bindings.data();
Chia-I Wub41393e2015-03-26 15:04:41 +0800566
Chia-I Wu41126e52015-03-26 15:27:55 +0800567 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600568 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800569 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500570
571 // create VkPipelineLayout
572 VkPipelineLayoutCreateInfo pipeline_layout = {};
573 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
574 pipeline_layout.descriptorSetCount = layouts.size();
575 pipeline_layout.pSetLayouts = NULL;
576
577 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700578
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600579 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500580 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800581
Chia-I Wu41126e52015-03-26 15:27:55 +0800582 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800583 size_t imageSamplerCount = 0;
584 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
585 it != m_writes.end(); it++) {
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800586 it->destSet = m_set->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800587 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
588 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800589 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800590
591 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800592 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700593}
Tony Barboure2c58df2014-11-25 13:18:32 -0700594
Tony Barbour6918cd52015-04-09 12:58:51 -0600595VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800596{
597 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800598 m_descriptorInfo.imageView = VK_NULL_HANDLE;
599 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800600}
601
Tony Barbour6918cd52015-04-09 12:58:51 -0600602void VkImageObj::ImageMemoryBarrier(
603 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600604 VkImageAspect aspect,
605 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600606 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600607 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
608 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
609 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
610 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600611 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600612 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600613 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
614 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
615 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
616 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
617 VK_MEMORY_INPUT_SHADER_READ_BIT |
618 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
619 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
620 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600621 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600622{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600623 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
624 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600625 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
626 subresourceRange);
627
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600628 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600629
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600630 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
631 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600632
633 // write barrier to the command buffer
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -0600634 vkCmdPipelineBarrier(cmd_buf->handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600635}
636
Tony Barbour6918cd52015-04-09 12:58:51 -0600637void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600638 VkImageAspect aspect,
639 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600640{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600641 VkFlags output_mask, input_mask;
642 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600643 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600644 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
645 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
646 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600647 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600648 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600649 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600650 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
651 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
652 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
653 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
654 VK_MEMORY_INPUT_SHADER_READ_BIT |
655 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
656 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600657 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600658
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800659 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600660 return;
661 }
662
663 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600664 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600665 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
666 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600667 break;
668
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600669 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600670 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
671 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600672 break;
673
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600674 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600675 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
676 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600677 break;
678
679 default:
680 output_mask = all_cache_outputs;
681 input_mask = all_cache_inputs;
682 break;
683 }
684
685 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800686 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600687}
688
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600689void VkImageObj::SetLayout(VkImageAspect aspect,
690 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600691{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600692 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600693
694 if (image_layout == m_descriptorInfo.imageLayout) {
695 return;
696 }
697
Tony Barbourfe3351b2015-07-28 10:17:20 -0600698 VkCmdPoolCreateInfo cmd_pool_info = {};
699 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
700 cmd_pool_info.pNext = NULL;
701 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
702 cmd_pool_info.flags = 0;
703 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
704 VkCommandBufferObj cmd_buf(m_device, pool.handle());
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600705
706 /* Build command buffer to set image layout in the driver */
707 err = cmd_buf.BeginCommandBuffer();
708 assert(!err);
709
710 SetLayout(&cmd_buf, aspect, image_layout);
711
712 err = cmd_buf.EndCommandBuffer();
713 assert(!err);
714
715 cmd_buf.QueueCommandBuffer();
716}
717
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600718bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600719{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600720 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600721 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600722 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600723
Tony Barbour579f7802015-04-03 15:11:43 -0600724 return true;
725}
726
Tony Barbour6918cd52015-04-09 12:58:51 -0600727void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600728 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600729 VkImageTiling requested_tiling,
730 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800731{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600732 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600733 VkFormatProperties image_fmt;
734 VkImageTiling tiling;
735 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600736
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800737 mipCount = 0;
738
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600739 uint32_t _w = w;
740 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800741 while( ( _w > 0 ) || ( _h > 0 ) )
742 {
743 _w >>= 1;
744 _h >>= 1;
745 mipCount++;
746 }
747
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600748 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), fmt, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600749 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600750
Tony Barbourd1c35722015-04-16 15:59:00 -0600751 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600752 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600753 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600754 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600755 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600756 } else {
757 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
758 }
759 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600760 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600761 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600762 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600763 } else {
764 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
765 }
766
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600767 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600768 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800769 imageCreateInfo.format = fmt;
770 imageCreateInfo.extent.width = w;
771 imageCreateInfo.extent.height = h;
772 imageCreateInfo.mipLevels = mipCount;
773 imageCreateInfo.tiling = tiling;
774
775 imageCreateInfo.usage = usage;
776
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600777 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800778
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600779 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600780 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600781 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600782 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600783 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800784}
785
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600786VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600787{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600788 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600789 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600790
Tony Barbourfe3351b2015-07-28 10:17:20 -0600791 VkCmdPoolCreateInfo cmd_pool_info = {};
792 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
793 cmd_pool_info.pNext = NULL;
794 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
795 cmd_pool_info.flags = 0;
796 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
797 VkCommandBufferObj cmd_buf(m_device, pool.handle());
798
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600799 /* Build command buffer to copy staging texture to usable texture */
800 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600801 assert(!err);
802
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600803 /* TODO: Can we determine image aspect from image object? */
804 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600805 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600806
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600807 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600808 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600809
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600810 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600811 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600812 copy_region.srcSubresource.arrayLayer = 0;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600813 copy_region.srcSubresource.mipLevel = 0;
814 copy_region.srcOffset.x = 0;
815 copy_region.srcOffset.y = 0;
816 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600817 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600818 copy_region.destSubresource.arrayLayer = 0;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600819 copy_region.destSubresource.mipLevel = 0;
820 copy_region.destOffset.x = 0;
821 copy_region.destOffset.y = 0;
822 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600823 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600824
Chia-I Wube2b9172015-07-03 11:49:42 +0800825 vkCmdCopyImage(cmd_buf.handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +0800826 src_image.handle(), src_image.layout(),
827 handle(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600828 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600829
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600830 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600831
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600832 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600833
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600834 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600835 assert(!err);
836
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600837 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600838
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600839 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600840}
841
Tony Barbour6918cd52015-04-09 12:58:51 -0600842VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
843 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700844{
845 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600846 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600847 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600848 void *data;
849 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600850 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600851 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600852
Tony Barboure65788f2015-07-21 17:01:42 -0600853 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 -0600854 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600855
856 if (colors == NULL)
857 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700858
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800859 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700860
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600861 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600862 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600863 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600864 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600865 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600866 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600867 view.channels.r = VK_CHANNEL_SWIZZLE_R;
868 view.channels.g = VK_CHANNEL_SWIZZLE_G;
869 view.channels.b = VK_CHANNEL_SWIZZLE_B;
870 view.channels.a = VK_CHANNEL_SWIZZLE_A;
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -0600871 view.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600872 view.subresourceRange.baseMipLevel = 0;
873 view.subresourceRange.mipLevels = 1;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600874 view.subresourceRange.baseArrayLayer = 0;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600875 view.subresourceRange.arraySize = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700876
Tony Barboure2c58df2014-11-25 13:18:32 -0700877 /* create image */
Tony Barboure65788f2015-07-21 17:01:42 -0600878 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 -0700879
880 /* create image view */
Chia-I Wu681d7a02015-07-03 13:44:34 +0800881 view.image = handle();
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800882 m_textureView.init(*m_device, view);
Chia-I Wu3158bf32015-07-03 11:49:42 +0800883 m_descriptorInfo.imageView = m_textureView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700884
Chia-I Wu681d7a02015-07-03 13:44:34 +0800885 data = stagingImage.MapMemory();
Tony Barboure2c58df2014-11-25 13:18:32 -0700886
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800887 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700888 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800889 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600890 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700891 }
Chia-I Wu681d7a02015-07-03 13:44:34 +0800892 stagingImage.UnmapMemory();
Tony Barbour6918cd52015-04-09 12:58:51 -0600893 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700894}
Tony Barbour82c39522014-12-04 14:33:33 -0700895
Tony Barbour6918cd52015-04-09 12:58:51 -0600896VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700897{
Tony Barboure2c58df2014-11-25 13:18:32 -0700898 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700899
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600900 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800901 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600902 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
903 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
904 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600905 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchter97953352015-09-10 14:08:50 -0600906 samplerCreateInfo.addressModeU = VK_TEX_ADDRESS_MODE_WRAP;
907 samplerCreateInfo.addressModeV = VK_TEX_ADDRESS_MODE_WRAP;
908 samplerCreateInfo.addressModeW = VK_TEX_ADDRESS_MODE_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800909 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600910 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600911 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800912 samplerCreateInfo.minLod = 0.0;
913 samplerCreateInfo.maxLod = 0.0;
Tony Barbour26b17f82015-06-25 16:56:44 -0600914 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Mark Lobodzinski52ac6582015-09-01 15:42:56 -0600915 samplerCreateInfo.unnormalizedCoordinates = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700916
Chia-I Wue9864b52014-12-28 16:32:24 +0800917 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700918}
Tony Barboure2c58df2014-11-25 13:18:32 -0700919
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700920/*
921 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
922 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600923VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700924{
925 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700926 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700927
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800928 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700929}
930
Tony Barbour6918cd52015-04-09 12:58:51 -0600931VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600932{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600933 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600934 if (m_commandBuffer) {
935 delete m_commandBuffer;
Tony Barbour3d83f492015-07-28 10:23:02 -0600936 delete m_cmdPool;
Tony Barbour044703b2015-03-26 12:37:52 -0600937 }
938}
939
Tony Barbour6918cd52015-04-09 12:58:51 -0600940VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700941{
Tony Barboure2c58df2014-11-25 13:18:32 -0700942 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800943 m_commandBuffer = 0;
944
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800945 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700946 m_numVertices = constantCount;
947 m_stride = constantSize;
948
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600949 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800950 const size_t allocationSize = constantCount * constantSize;
Cody Northrop7fb43862015-06-22 14:56:14 -0600951 init_as_src_and_dst(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700952
Chia-I Wu681d7a02015-07-03 13:44:34 +0800953 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +0800954 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800955 memory().unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700956
Courtney Goeltzenleuchter6eb1aeb2015-09-11 15:29:21 -0600957 /*
958 * Constant buffers are going to be used as vertex input buffers
959 * or as shader uniform buffers. So, we'll create the shaderbuffer
960 * descriptor here so it's ready if needed.
961 */
Courtney Goeltzenleuchterb46f2272015-09-14 14:14:21 -0600962 this->m_descriptorInfo.bufferInfo.buffer = handle();
963 this->m_descriptorInfo.bufferInfo.offset = 0;
964 this->m_descriptorInfo.bufferInfo.range = allocationSize;
Tony Barboure2c58df2014-11-25 13:18:32 -0700965}
Tony Barbour82c39522014-12-04 14:33:33 -0700966
Tony Barbourd1c35722015-04-16 15:59:00 -0600967void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700968{
Chia-I Wu681d7a02015-07-03 13:44:34 +0800969 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &handle(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700970}
971
972
Tony Barbour6918cd52015-04-09 12:58:51 -0600973void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600974 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600975 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600976 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
977 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
978 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
979 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600980 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600981 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600982 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
983 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
984 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
985 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
986 VK_MEMORY_INPUT_SHADER_READ_BIT |
987 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
988 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
989 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700990{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600991 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700992
Tony Barbour38422802014-12-10 14:36:31 -0700993 if (!m_commandBuffer)
994 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600995 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbourfe3351b2015-07-28 10:17:20 -0600996 VkCmdPoolCreateInfo cmd_pool_info = {};
997 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
998 cmd_pool_info.pNext = NULL;
999 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
1000 cmd_pool_info.flags = 0;
1001 m_cmdPool = new vk_testing::CmdPool(*m_device, cmd_pool_info);
1002 m_commandBuffer = new VkCommandBufferObj(m_device, m_cmdPool->handle());
Tony Barbour38422802014-12-10 14:36:31 -07001003 }
1004 else
1005 {
Chia-I Wua07fee62014-12-28 15:26:08 +08001006 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -07001007 }
1008
Tony Barboure2c58df2014-11-25 13:18:32 -07001009 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001010 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001011 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -06001012 cmd_buf_info.pNext = NULL;
1013 cmd_buf_info.flags = 0;
Cody Northropcc09b9e2015-08-11 11:35:58 -06001014 cmd_buf_info.renderPass = VK_NULL_HANDLE;
1015 cmd_buf_info.subpass = 0;
1016 cmd_buf_info.framebuffer = VK_NULL_HANDLE;
Tony Barbourbdf0a312015-04-01 17:10:07 -06001017
Jon Ashburnc4164b12014-12-31 17:10:47 -07001018 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001019 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001020
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001021 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001022 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001023 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -07001024
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001025 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1026 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001027
1028 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001029 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -07001030
1031 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -07001032 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001033 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001034
Tony Barboure2c58df2014-11-25 13:18:32 -07001035 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001036 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -07001037 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wud9e8e822015-07-03 11:45:55 +08001038 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.handle() );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001039 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001040}
1041
Tony Barbour6918cd52015-04-09 12:58:51 -06001042VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
1043 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001044{
1045
1046}
1047
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001048void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001049{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001050 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001051
1052 m_numVertices = numIndexes;
1053 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001054 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -06001055 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001056 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -06001057 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001058 break;
Tony Barbourd1c35722015-04-16 15:59:00 -06001059 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001060 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -06001061 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001062 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001063 default:
1064 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -06001065 m_stride = 2;
1066 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001067 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001068 }
1069
Chia-I Wua07fee62014-12-28 15:26:08 +08001070 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -06001071 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northrop7fb43862015-06-22 14:56:14 -06001072 init_as_src_and_dst(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001073
Chia-I Wu681d7a02015-07-03 13:44:34 +08001074 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +08001075 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +08001076 memory().unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001077
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001078 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001079 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001080 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001081 view_info.buffer = handle();
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001082 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001083 view_info.offset = 0;
1084 view_info.range = allocationSize;
1085 m_bufferView.init(*m_device, view_info);
1086
Chia-I Wu3158bf32015-07-03 11:49:42 +08001087 this->m_descriptorInfo.bufferView = m_bufferView.handle();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001088}
1089
Tony Barbourd1c35722015-04-16 15:59:00 -06001090void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001091{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001092 vkCmdBindIndexBuffer(cmdBuffer, handle(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001093}
Tony Barboure2c58df2014-11-25 13:18:32 -07001094
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001095VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001096{
1097 return m_indexType;
1098}
1099
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001100VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001101{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001102 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001103 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1104 stageInfo->stage = m_stage;
Chia-I Wu4d0c7922015-07-03 11:49:42 +08001105 stageInfo->shader = handle();
Tony Barboure2c58df2014-11-25 13:18:32 -07001106
Tony Barboure2c58df2014-11-25 13:18:32 -07001107 return stageInfo;
1108}
1109
Tony Barbourd1c35722015-04-16 15:59:00 -06001110VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001111{
Tony Barbourb5d2c942015-07-14 13:34:05 -06001112 VkResult U_ASSERT_ONLY err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001113 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001114 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001115 VkShaderModuleCreateInfo moduleCreateInfo;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001116 vk_testing::ShaderModule *module = new vk_testing::ShaderModule();
Tony Barboure2c58df2014-11-25 13:18:32 -07001117 size_t shader_len;
1118
1119 m_stage = stage;
1120 m_device = device;
1121
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001122 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1123 moduleCreateInfo.pNext = NULL;
1124
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001125 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001126 createInfo.pNext = NULL;
1127
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001128 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001129
1130 shader_len = strlen(shader_code);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001131 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1132 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1133 moduleCreateInfo.flags = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001134
Tony Barbourd1c35722015-04-16 15:59:00 -06001135 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001136 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1137 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1138 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1139 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barboure2c58df2014-11-25 13:18:32 -07001140
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001141 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001142
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001143 // Use Reference GLSL to SPV compiler
1144 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001145 moduleCreateInfo.pCode = spv.data();
1146 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1147 moduleCreateInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001148 }
Cody Northrop475663c2015-04-15 11:19:06 -06001149
Tony Barbour823e6ca2015-07-27 13:31:04 -06001150 err = module->init_try(*m_device, moduleCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001151 assert(VK_SUCCESS == err);
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001152
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001153 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1154 createInfo.pNext = NULL;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001155 createInfo.module = module->handle();
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001156 createInfo.pName = "main";
1157 createInfo.flags = 0;
Cody Northropfd4bcfd2015-08-24 15:11:10 -06001158 createInfo.stage = stage;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001159
1160 err = init_try(*m_device, createInfo);
Cody Northrop475663c2015-04-15 11:19:06 -06001161 assert(VK_SUCCESS == err);
Tony Barbour823e6ca2015-07-27 13:31:04 -06001162 framework->m_shader_modules.push_back(module);
Tony Barbourf325bf12014-12-03 15:59:38 -07001163}
Tony Barbour82c39522014-12-04 14:33:33 -07001164
Tony Barbour6918cd52015-04-09 12:58:51 -06001165VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001166{
Tony Barboure2c58df2014-11-25 13:18:32 -07001167 m_device = device;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001168
1169 m_vi_state.pNext = VK_NULL_HANDLE;
1170 m_vi_state.bindingCount = 0;
1171 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1172 m_vi_state.attributeCount = 0;
1173 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1174
Tony Barboure2c58df2014-11-25 13:18:32 -07001175 m_vertexBufferCount = 0;
1176
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001177 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001178 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001179 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001180 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001181
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001182 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001183 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001184 m_rs_state.depthClipEnable = VK_FALSE;
1185 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001186 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001187 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1188 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Cody Northrop12365112015-08-17 11:10:49 -06001189 m_rs_state.depthBiasEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001190
Tony Barboure2c58df2014-11-25 13:18:32 -07001191 memset(&m_cb_state,0,sizeof(m_cb_state));
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001192 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001193 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001194 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1195 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001196
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001197 m_ms_state.pNext = VK_NULL_HANDLE;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001198 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop02e61ed2015-08-04 14:34:54 -06001199 m_ms_state.pSampleMask = NULL;
Tony Barbourdfd533a2015-06-26 10:18:34 -06001200 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001201 m_ms_state.minSampleShading = 0;
1202 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001203
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001204 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001205 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001206 m_vp_state.viewportCount = 1;
Tony Barbourd81b8882015-05-15 09:37:57 -06001207
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001208 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001209 m_ds_state.pNext = VK_NULL_HANDLE,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001210 m_ds_state.depthTestEnable = VK_FALSE;
1211 m_ds_state.depthWriteEnable = VK_FALSE;
Cody Northrop271ba752015-08-26 10:01:32 -06001212 m_ds_state.depthBoundsTestEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001213 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001214 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1215 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1216 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001217 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001218 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001219 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001220};
1221
Tony Barbour6918cd52015-04-09 12:58:51 -06001222void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001223{
1224 m_shaderObjs.push_back(shader);
1225}
1226
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001227void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001228{
1229 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1230 m_vi_state.attributeCount = count;
1231}
1232
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001233void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001234{
1235 m_vi_state.pVertexBindingDescriptions = vi_binding;
1236 m_vi_state.bindingCount = count;
1237}
1238
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001239void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineColorBlendAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001240{
Tony Barbourf52346d2015-01-16 14:27:35 -07001241 if (binding+1 > m_colorAttachments.size())
1242 {
1243 m_colorAttachments.resize(binding+1);
1244 }
1245 m_colorAttachments[binding] = *att;
1246}
1247
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001248void VkPipelineObj::SetDepthStencil(VkPipelineDepthStencilStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001249{
Tony Barbourf52346d2015-01-16 14:27:35 -07001250 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1251 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
Cody Northrop271ba752015-08-26 10:01:32 -06001252 m_ds_state.depthBoundsTestEnable = ds_state->depthBoundsTestEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001253 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001254 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1255 m_ds_state.back = ds_state->back;
1256 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001257}
1258
Tony Barbour0ec8cd52015-08-06 09:27:11 -06001259void VkPipelineObj::SetMSAA(VkPipelineMultisampleStateCreateInfo *ms_state)
1260{
1261 memcpy(&m_ms_state, ms_state, sizeof(VkPipelineMultisampleStateCreateInfo));
1262}
1263
Tony Barbour5781e8f2015-08-04 16:23:11 -06001264VkResult VkPipelineObj::CreateVKPipeline(VkPipelineLayout layout, VkRenderPass render_pass)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001265{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001266 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001267
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001268 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001269
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001270 info.stageCount = m_shaderObjs.size();
1271 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1272
Tony Barbour976e1cf2014-12-17 11:57:31 -07001273 for (int i=0; i<m_shaderObjs.size(); i++)
1274 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001275 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001276 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001277 }
1278
Tony Barboure815fd72015-07-27 09:36:24 -06001279 if (m_vi_state.attributeCount && m_vi_state.bindingCount) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001280 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barboure815fd72015-07-27 09:36:24 -06001281 info.pVertexInputState = &m_vi_state;
1282 } else {
1283 info.pVertexInputState = NULL;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001284 }
1285
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001286 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001287 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001288 info.flags = 0;
Tony Barbour5781e8f2015-08-04 16:23:11 -06001289 info.layout = layout;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001290
Tony Barbourf52346d2015-01-16 14:27:35 -07001291 m_cb_state.attachmentCount = m_colorAttachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -06001292 m_cb_state.pAttachments = m_colorAttachments.data();
Tony Barbourf52346d2015-01-16 14:27:35 -07001293
Chia-I Wu08accc62015-07-07 11:50:03 +08001294 info.renderPass = render_pass;
1295 info.subpass = 0;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001296 info.pTessellationState = NULL;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001297 info.pInputAssemblyState = &m_ia_state;
1298 info.pViewportState = &m_vp_state;
1299 info.pRasterState = &m_rs_state;
1300 info.pMultisampleState = &m_ms_state;
1301 info.pDepthStencilState = &m_ds_state;
1302 info.pColorBlendState = &m_cb_state;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001303
Chris Forbes95292b12015-05-25 11:13:26 +12001304 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001305}
Chia-I Wu2648d092014-12-29 14:24:14 +08001306
Tony Barbourfe3351b2015-07-28 10:17:20 -06001307VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device, VkCmdPool pool)
Tony Barbour6d047bf2014-12-10 14:34:45 -07001308{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001309 m_device = device;
Courtney Goeltzenleuchter0c8385b2015-07-13 12:57:11 -06001310
Tony Barbourfe3351b2015-07-28 10:17:20 -06001311 init(*device, vk_testing::CmdBuffer::create_info(pool));
Tony Barbour6d047bf2014-12-10 14:34:45 -07001312}
Tony Barbour471338d2014-12-10 17:28:39 -07001313
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001314VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001315{
Chia-I Wube2b9172015-07-03 11:49:42 +08001316 return handle();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001317}
Tony Barbour471338d2014-12-10 17:28:39 -07001318
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001319VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001320{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001321 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001322 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001323}
1324
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001325VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001326{
1327 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001328 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001329}
1330
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001331VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001332{
Chia-I Wud28343c2014-12-28 15:12:48 +08001333 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001334 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001335}
1336
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001337void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, VkBool32 byRegion, uint32_t memBarrierCount, const void* const* ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001338{
Chia-I Wube2b9172015-07-03 11:49:42 +08001339 vkCmdPipelineBarrier(handle(), src_stages, dest_stages, byRegion, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001340}
1341
Chris Forbesf0796e12015-06-24 14:34:53 +12001342void VkCommandBufferObj::ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001343 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001344{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001345 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001346 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001347 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001348 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1349 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1350 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001351 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001352 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001353
1354 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001355 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001356 srRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001357 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter13dbde02015-08-28 16:47:15 -06001358 srRange.mipLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001359 srRange.baseArrayLayer = 0;
1360 srRange.arraySize = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001361
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001362 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001363 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001364 memory_barrier.outputMask = output_mask;
1365 memory_barrier.inputMask = input_mask;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001366 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001367 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001368 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001369
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001370 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1371 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001372
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001373 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001374 memory_barrier.image = m_renderTargets[i]->image();
1375 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001376 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001377 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001378
Chia-I Wube2b9172015-07-03 11:49:42 +08001379 vkCmdClearColorImage(handle(),
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001380 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001381 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001382
Tony Barbour30cc9e82014-12-17 11:53:55 -07001383 }
1384
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001385 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001386 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001387 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -06001388 dsRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001389 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter13dbde02015-08-28 16:47:15 -06001390 dsRange.mipLevels = VK_REMAINING_MIP_LEVELS;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -06001391 dsRange.baseArrayLayer = 0;
1392 dsRange.arraySize = VK_REMAINING_ARRAY_LAYERS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001393
1394 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001395
Cody Northrop3cc85e92015-08-04 10:47:08 -06001396 memory_barrier.oldLayout = memory_barrier.newLayout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001397 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001398 memory_barrier.image = depthStencilObj->handle();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001399 memory_barrier.subresourceRange = dsRange;
1400
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001401 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001402
Courtney Goeltzenleuchter45df9e12015-09-15 18:03:22 -06001403 VkClearDepthStencilValue clear_value = {
1404 depth_clear_color,
1405 stencil_clear_color
1406 };
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,
Courtney Goeltzenleuchter45df9e12015-09-15 18:03:22 -06001409 &clear_value,
Chris Forbesd9be82b2015-06-22 17:21:59 +12001410 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}