blob: 780b71f9e9a03a98072300b9589f2232e8164f0a [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"
Tony Barbour6a3faf02015-07-23 10:36:18 -060029#include <vk_wsi_swapchain.h>
30#include <vk_wsi_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),
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060043 m_stateRaster( VK_NULL_HANDLE ),
44 m_colorBlend( VK_NULL_HANDLE ),
45 m_stateViewport( VK_NULL_HANDLE ),
46 m_stateDepthStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060047 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070048 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060049 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
50 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Tony Barbour71bd4b32015-07-21 17:00:26 -060051 m_clear_via_load_op( true ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070052 m_depth_clear_color( 1.0 ),
Courtney Goeltzenleuchterb6c9aca2015-06-08 16:19:37 -060053 m_stencil_clear_color( 0 ),
54 m_depthStencil( NULL ),
55 m_dbgCreateMsgCallback( VK_NULL_HANDLE ),
56 m_dbgDestroyMsgCallback( VK_NULL_HANDLE ),
57 m_globalMsgCallback( VK_NULL_HANDLE ),
58 m_devMsgCallback( VK_NULL_HANDLE )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060059{
Tony Barbour1c45ce02015-03-27 17:03:18 -060060
Chia-I Wu08accc62015-07-07 11:50:03 +080061 memset(&m_renderPassBeginInfo, 0, sizeof(m_renderPassBeginInfo));
62 m_renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
63
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070064 // clear the back buffer to dark grey
Chris Forbesf0796e12015-06-24 14:34:53 +120065 m_clear_color.f32[0] = 0.25f;
66 m_clear_color.f32[1] = 0.25f;
67 m_clear_color.f32[2] = 0.25f;
68 m_clear_color.f32[3] = 0.0f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060069}
70
Tony Barbour6918cd52015-04-09 12:58:51 -060071VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060072{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060073
74}
75
Tony Barbour6918cd52015-04-09 12:58:51 -060076void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060077{
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060078 std::vector<const char*> instance_layer_names;
79 std::vector<const char*> device_layer_names;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060080 std::vector<const char*> instance_extension_names;
81 std::vector<const char*> device_extension_names;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060082 InitFramework(
83 instance_layer_names, device_layer_names,
84 instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060085}
86
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060087void VkRenderFramework::InitFramework(
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060088 std::vector<const char *> instance_layer_names,
89 std::vector<const char *> device_layer_names,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060090 std::vector<const char *> instance_extension_names,
91 std::vector<const char *> device_extension_names,
92 PFN_vkDbgMsgCallback dbgFunction,
93 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060094{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060095 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060096 std::vector<VkExtensionProperties> instance_extensions;
97 std::vector<VkExtensionProperties> device_extensions;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060098 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060099
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600100 /* TODO: Verify requested extensions are available */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600101
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600102 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -0600103 instInfo.pNext = NULL;
104 instInfo.pAppInfo = &app_info;
105 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600106 instInfo.layerCount = instance_layer_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600107 instInfo.ppEnabledLayerNames = instance_layer_names.data();
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600108 instInfo.extensionCount = instance_extension_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600109 instInfo.ppEnabledExtensionNames = instance_extension_names.data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600110 err = vkCreateInstance(&instInfo, &this->inst);
111 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600112
Jon Ashburn83a64252015-04-15 11:31:12 -0600113 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
114 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
115 ASSERT_VK_SUCCESS(err);
116 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600117 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700118 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600119 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600120 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
121 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
122 if (m_dbgCreateMsgCallback) {
123 err = m_dbgCreateMsgCallback(this->inst,
124 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
125 dbgFunction,
126 userData,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600127 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600128 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600129
130 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
131 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600132 }
Tony Barbour15524c32015-04-29 17:34:29 -0600133 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600134
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600135 /* TODO: Verify requested physical device extensions are available */
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600136 m_device = new VkDeviceObj(0, objs[0], device_layer_names, device_extension_names);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600137
138 /* Now register callback on device */
139 if (0) {
140 if (m_dbgCreateMsgCallback) {
141 err = m_dbgCreateMsgCallback(this->inst,
142 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
143 dbgFunction,
144 userData,
145 &m_devMsgCallback);
146 ASSERT_VK_SUCCESS(err);
147 }
148 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600149 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600150
Tony Barbour6918cd52015-04-09 12:58:51 -0600151 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600152}
153
Tony Barbour6918cd52015-04-09 12:58:51 -0600154void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600155{
Tony Barbour67e99152015-07-10 14:10:27 -0600156 if (m_colorBlend) vkDestroyDynamicColorBlendState(device(), m_colorBlend);
157 if (m_stateDepthStencil) vkDestroyDynamicDepthStencilState(device(), m_stateDepthStencil);
158 if (m_stateRaster) vkDestroyDynamicRasterState(device(), m_stateRaster);
Tony Barbourfe3351b2015-07-28 10:17:20 -0600159 if (m_cmdBuffer)
160 delete m_cmdBuffer;
Cody Northrope62183e2015-07-09 18:08:05 -0600161 if (m_cmdPool) vkDestroyCommandPool(device(), m_cmdPool);
Tony Barbour67e99152015-07-10 14:10:27 -0600162 if (m_framebuffer) vkDestroyFramebuffer(device(), m_framebuffer);
163 if (m_renderPass) vkDestroyRenderPass(device(), m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600164
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600165 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
166 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
167
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600168 if (m_stateViewport) {
Tony Barbour67e99152015-07-10 14:10:27 -0600169 vkDestroyDynamicViewportState(device(), m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600170 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600171 while (!m_renderTargets.empty()) {
Tony Barbour6a3faf02015-07-23 10:36:18 -0600172 vkDestroyAttachmentView(device(), m_renderTargets.back()->targetView(m_render_target_fmt));
Tony Barbour67e99152015-07-10 14:10:27 -0600173 vkDestroyImage(device(), m_renderTargets.back()->image());
Mike Stroyanb050c682015-04-17 12:36:38 -0600174 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600175 m_renderTargets.pop_back();
176 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600177
Tony Barbour1c45ce02015-03-27 17:03:18 -0600178 delete m_depthStencil;
Tony Barbour823e6ca2015-07-27 13:31:04 -0600179 while (!m_shader_modules.empty())
180 {
181 delete m_shader_modules.back();
182 m_shader_modules.pop_back();
183 }
Tony Barbour1c45ce02015-03-27 17:03:18 -0600184
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600185 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800186 delete m_device;
Chris Forbesbe2fae62015-07-07 11:02:22 +1200187 if (this->inst) vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600188}
189
Tony Barbour6918cd52015-04-09 12:58:51 -0600190void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600191{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600192 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600193
Tony Barbour6a3faf02015-07-23 10:36:18 -0600194 // Get the list of VkFormat's that are supported:
Ian Elliott8b139792015-08-07 11:51:12 -0600195 PFN_vkGetSurfaceFormatsWSI fpGetSurfaceFormatsWSI;
196 uint32_t formatCount;
Tony Barbour6a3faf02015-07-23 10:36:18 -0600197 VkSurfaceDescriptionWSI surface_description;
198 surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
199 surface_description.pNext = NULL;
Ian Elliott8b139792015-08-07 11:51:12 -0600200 GET_DEVICE_PROC_ADDR(device(), GetSurfaceFormatsWSI);
201 err = fpGetSurfaceFormatsWSI(device(),
Tony Barbour6a3faf02015-07-23 10:36:18 -0600202 (VkSurfaceDescriptionWSI *) &surface_description,
Ian Elliott8b139792015-08-07 11:51:12 -0600203 &formatCount, NULL);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600204 ASSERT_VK_SUCCESS(err);
Ian Elliott8b139792015-08-07 11:51:12 -0600205 VkSurfaceFormatWSI *surfFormats =
206 (VkSurfaceFormatWSI *)malloc(formatCount * sizeof(VkSurfaceFormatWSI));
207 err = fpGetSurfaceFormatsWSI(device(),
Tony Barbour6a3faf02015-07-23 10:36:18 -0600208 (VkSurfaceDescriptionWSI *) &surface_description,
Ian Elliott8b139792015-08-07 11:51:12 -0600209 &formatCount, surfFormats);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600210 ASSERT_VK_SUCCESS(err);
211 m_render_target_fmt = surfFormats[0].format;
212 free(surfFormats);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600213
214 // create a raster state (solid, back-face culling)
Tony Barbour67e99152015-07-10 14:10:27 -0600215 VkDynamicRasterStateCreateInfo raster = {};
216 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700217
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600218 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
219 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600220
Tony Barbour67e99152015-07-10 14:10:27 -0600221 VkDynamicColorBlendStateCreateInfo blend = {};
222 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600223 blend.blendConst[0] = 1.0f;
224 blend.blendConst[1] = 1.0f;
225 blend.blendConst[2] = 1.0f;
226 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600227 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
228 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600229
Tony Barbour67e99152015-07-10 14:10:27 -0600230 VkDynamicDepthStencilStateCreateInfo depthStencil = {};
231 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski365feea2015-06-12 11:14:17 -0600232 depthStencil.minDepthBounds = 0.f;
233 depthStencil.maxDepthBounds = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700234 depthStencil.stencilFrontRef = 0;
235 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600236 depthStencil.stencilReadMask = 0xff;
237 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600238
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600239 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
240 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600241
Cody Northrope62183e2015-07-09 18:08:05 -0600242 VkCmdPoolCreateInfo cmd_pool_info;
243 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
244 cmd_pool_info.pNext = NULL,
245 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
246 cmd_pool_info.flags = 0,
247 err = vkCreateCommandPool(device(), &cmd_pool_info, &m_cmdPool);
248 assert(!err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600249
Tony Barbourfe3351b2015-07-28 10:17:20 -0600250 m_cmdBuffer = new VkCommandBufferObj(m_device, m_cmdPool);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600251}
252
Tony Barbour6918cd52015-04-09 12:58:51 -0600253void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600254{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600255 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600256
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600257 VkViewport viewport;
Chris Forbesd9be82b2015-06-22 17:21:59 +1200258 VkRect2D scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600259
Tony Barbour67e99152015-07-10 14:10:27 -0600260 VkDynamicViewportStateCreateInfo viewportCreate = {};
261 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700262 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700263 viewport.originX = 0;
264 viewport.originY = 0;
265 viewport.width = 1.f * width;
266 viewport.height = 1.f * height;
267 viewport.minDepth = 0.f;
268 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600269 scissor.extent.width = (int32_t) width;
270 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700271 scissor.offset.x = 0;
272 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700273 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700274 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700275
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600276 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
277 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600278 m_width = width;
279 m_height = height;
280}
281
Tony Barbour6918cd52015-04-09 12:58:51 -0600282void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600283{
284 InitViewport(m_width, m_height);
285}
Tony Barbour6918cd52015-04-09 12:58:51 -0600286void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600287{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700288 InitRenderTarget(1);
289}
290
Tony Barbour6918cd52015-04-09 12:58:51 -0600291void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700292{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600293 InitRenderTarget(targets, NULL);
294}
295
Cody Northrop3cc85e92015-08-04 10:47:08 -0600296void VkRenderFramework::InitRenderTarget(VkAttachmentView *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600297{
298 InitRenderTarget(1, dsBinding);
299}
300
Cody Northrop3cc85e92015-08-04 10:47:08 -0600301void VkRenderFramework::InitRenderTarget(uint32_t targets, VkAttachmentView *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600302{
Chia-I Wu08accc62015-07-07 11:50:03 +0800303 std::vector<VkAttachmentDescription> attachments;
304 std::vector<VkAttachmentReference> color_references;
Cody Northrop3cc85e92015-08-04 10:47:08 -0600305 std::vector<VkAttachmentView> bindings;
Cody Northropd2ad0342015-08-05 11:15:02 -0600306 attachments.reserve(targets + 1); // +1 for dsBinding
Chia-I Wu08accc62015-07-07 11:50:03 +0800307 color_references.reserve(targets);
Cody Northropd2ad0342015-08-05 11:15:02 -0600308 bindings.reserve(targets + 1); // +1 for dsBinding
Tony Barbour1c45ce02015-03-27 17:03:18 -0600309
Chia-I Wu08accc62015-07-07 11:50:03 +0800310 VkAttachmentDescription att = {};
311 att.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION;
312 att.pNext = NULL;
313 att.format = m_render_target_fmt;
314 att.samples = 1;
315 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
316 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
317 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
318 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
319 att.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
320 att.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chia-I Wuecebf752014-12-05 10:45:15 +0800321
Chia-I Wu08accc62015-07-07 11:50:03 +0800322 VkAttachmentReference ref = {};
323 ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
324
325 m_renderPassClearValues.clear();
326 VkClearValue clear = {};
327 clear.color = m_clear_color;
328
Cody Northrop3cc85e92015-08-04 10:47:08 -0600329 VkAttachmentView bind = {};
Chia-I Wu08accc62015-07-07 11:50:03 +0800330
331 for (uint32_t i = 0; i < targets; i++) {
332 attachments.push_back(att);
333
334 ref.attachment = i;
335 color_references.push_back(ref);
336
337 m_renderPassClearValues.push_back(clear);
338
Tony Barbour6918cd52015-04-09 12:58:51 -0600339 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600340
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600341 VkFormatProperties props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600342 VkResult err;
343
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600344 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), m_render_target_fmt, &props);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600345 ASSERT_VK_SUCCESS(err);
346
347 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600348 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600349 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
350 VK_IMAGE_TILING_LINEAR);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600351 }
352 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600353 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600354 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
355 VK_IMAGE_TILING_OPTIMAL);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600356 }
357 else {
358 FAIL() << "Neither Linear nor Optimal allowed for render target";
359 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600360
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600361 m_renderTargets.push_back(img);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600362 bind = img->targetView(m_render_target_fmt);
Chia-I Wu08accc62015-07-07 11:50:03 +0800363 bindings.push_back(bind);
Chia-I Wuecebf752014-12-05 10:45:15 +0800364 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600365
Chia-I Wu08accc62015-07-07 11:50:03 +0800366 VkSubpassDescription subpass = {};
367 subpass.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION;
368 subpass.pNext = NULL;
369 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
370 subpass.flags = 0;
371 subpass.inputCount = 0;
Cody Northropa505dda2015-08-04 11:16:41 -0600372 subpass.pInputAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800373 subpass.colorCount = targets;
Cody Northropa505dda2015-08-04 11:16:41 -0600374 subpass.pColorAttachments = color_references.data();
375 subpass.pResolveAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800376
377 if (dsBinding) {
378 att.format = m_depth_stencil_fmt;
Tony Barbour71bd4b32015-07-21 17:00:26 -0600379 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 +0800380 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
381 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
382 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Chia-I Wu08accc62015-07-07 11:50:03 +0800383 attachments.push_back(att);
384
385 clear.ds.depth = m_depth_clear_color;
386 clear.ds.stencil = m_stencil_clear_color;
387 m_renderPassClearValues.push_back(clear);
388
389 bindings.push_back(*dsBinding);
390
391 subpass.depthStencilAttachment.attachment = targets;
Chia-I Wu08accc62015-07-07 11:50:03 +0800392 } else {
393 subpass.depthStencilAttachment.attachment = VK_ATTACHMENT_UNUSED;
394 }
395
396 subpass.preserveCount = 0;
Cody Northropa505dda2015-08-04 11:16:41 -0600397 subpass.pPreserveAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800398
399 VkRenderPassCreateInfo rp_info = {};
400 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
401 rp_info.attachmentCount = attachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600402 rp_info.pAttachments = attachments.data();
Chia-I Wu08accc62015-07-07 11:50:03 +0800403 rp_info.subpassCount = 1;
404 rp_info.pSubpasses = &subpass;
405
406 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
407
408 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600409 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600410 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600411 fb_info.pNext = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800412 fb_info.renderPass = m_renderPass;
413 fb_info.attachmentCount = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600414 fb_info.pAttachments = bindings.data();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600415 fb_info.width = (uint32_t)m_width;
416 fb_info.height = (uint32_t)m_height;
417 fb_info.layers = 1;
418
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600419 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600420
Chia-I Wu08accc62015-07-07 11:50:03 +0800421 m_renderPassBeginInfo.renderPass = m_renderPass;
422 m_renderPassBeginInfo.framebuffer = m_framebuffer;
Cody Northropd2ad0342015-08-05 11:15:02 -0600423 m_renderPassBeginInfo.renderArea.extent.width = (int32_t) m_width;
424 m_renderPassBeginInfo.renderArea.extent.height = (int32_t) m_height;
Cody Northrop23dd89d2015-08-04 11:51:03 -0600425 m_renderPassBeginInfo.clearValueCount = m_renderPassClearValues.size();
426 m_renderPassBeginInfo.pClearValues = m_renderPassClearValues.data();
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600427}
428
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600429
430
Tony Barbourd1c35722015-04-16 15:59:00 -0600431VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600432 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800433{
434 init();
435
Chia-I Wu999f0482015-07-03 10:32:05 +0800436 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600437 queue_props = phy().queue_properties().data();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800438}
439
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600440VkDeviceObj::VkDeviceObj(uint32_t id,
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600441 VkPhysicalDevice obj, std::vector<const char *> &layer_names,
442 std::vector<const char *> &extension_names) :
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600443 vk_testing::Device(obj), id(id)
444{
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600445 init(layer_names, extension_names);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600446
Chia-I Wu999f0482015-07-03 10:32:05 +0800447 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600448 queue_props = phy().queue_properties().data();
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600449}
450
Tony Barbour6918cd52015-04-09 12:58:51 -0600451void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800452{
453 ASSERT_NE(true, graphics_queues().empty());
Chia-I Wudf12ffd2015-07-03 10:53:18 +0800454 m_queue = graphics_queues()[0]->handle();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800455}
456
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600457VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
458 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700459{
Tony Barboure2c58df2014-11-25 13:18:32 -0700460
461}
462
Tony Barbour6918cd52015-04-09 12:58:51 -0600463VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700464{
Chia-I Wu11078b02015-01-04 16:27:24 +0800465 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700466}
Tony Barbour82c39522014-12-04 14:33:33 -0700467
Tony Barbour6918cd52015-04-09 12:58:51 -0600468int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700469{
Chia-I Wu11078b02015-01-04 16:27:24 +0800470 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600471 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600472 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800473 tc.count = 1;
474 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700475
Chia-I Wu11078b02015-01-04 16:27:24 +0800476 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700477}
Tony Barbour82c39522014-12-04 14:33:33 -0700478
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600479int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700480{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600481 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800482 tc.type = type;
483 tc.count = 1;
484 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700485
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800486 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
487 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600488
Chia-I Wu11078b02015-01-04 16:27:24 +0800489 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700490}
Tony Barbour82c39522014-12-04 14:33:33 -0700491
Tony Barbour6918cd52015-04-09 12:58:51 -0600492int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700493{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600494 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600495 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800496 tc.count = 1;
497 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700498
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800499 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Chia-I Wu8c721c62015-07-03 11:49:42 +0800500 tmp.sampler = sampler->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800501 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700502
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800503 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
504 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700505
Chia-I Wu11078b02015-01-04 16:27:24 +0800506 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700507}
Chia-I Wu11078b02015-01-04 16:27:24 +0800508
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500509VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700510{
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800511 return m_pipeline_layout.handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700512}
513
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600514VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700515{
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800516 return m_set->handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700517}
Tony Barboure2c58df2014-11-25 13:18:32 -0700518
Tony Barbour6918cd52015-04-09 12:58:51 -0600519void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700520{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600521 // create VkDescriptorPool
522 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600523 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800524 pool.count = m_type_counts.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600525 pool.pTypeCount = m_type_counts.data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600526 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700527
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600528 // create VkDescriptorSetLayout
529 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800530 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800531 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800532 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800533 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600534 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800535 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700536 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800537
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600538 // create VkDescriptorSetLayout
539 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600540 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800541 layout.count = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600542 layout.pBinding = bindings.data();
Chia-I Wub41393e2015-03-26 15:04:41 +0800543
Chia-I Wu41126e52015-03-26 15:27:55 +0800544 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600545 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800546 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500547
548 // create VkPipelineLayout
549 VkPipelineLayoutCreateInfo pipeline_layout = {};
550 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
551 pipeline_layout.descriptorSetCount = layouts.size();
552 pipeline_layout.pSetLayouts = NULL;
553
554 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700555
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600556 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500557 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800558
Chia-I Wu41126e52015-03-26 15:27:55 +0800559 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800560 size_t imageSamplerCount = 0;
561 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
562 it != m_writes.end(); it++) {
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800563 it->destSet = m_set->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800564 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
565 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800566 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800567
568 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800569 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700570}
Tony Barboure2c58df2014-11-25 13:18:32 -0700571
Tony Barbour6918cd52015-04-09 12:58:51 -0600572VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800573{
574 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800575 m_descriptorInfo.imageView = VK_NULL_HANDLE;
576 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800577}
578
Tony Barbour6918cd52015-04-09 12:58:51 -0600579void VkImageObj::ImageMemoryBarrier(
580 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600581 VkImageAspect aspect,
582 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600583 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600584 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
585 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
586 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
587 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600588 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600589 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600590 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
591 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
592 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
593 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
594 VK_MEMORY_INPUT_SHADER_READ_BIT |
595 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
596 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
597 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600598 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600599{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600600 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
601 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600602 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
603 subresourceRange);
604
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600605 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600606
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600607 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
608 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600609
610 // write barrier to the command buffer
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -0600611 vkCmdPipelineBarrier(cmd_buf->handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600612}
613
Tony Barbour6918cd52015-04-09 12:58:51 -0600614void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600615 VkImageAspect aspect,
616 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600617{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600618 VkFlags output_mask, input_mask;
619 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600620 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600621 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
622 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
623 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600624 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600625 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600626 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600627 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
628 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
629 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
630 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
631 VK_MEMORY_INPUT_SHADER_READ_BIT |
632 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
633 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600634 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600635
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800636 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600637 return;
638 }
639
640 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600641 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600642 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
643 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600644 break;
645
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600646 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600647 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
648 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600649 break;
650
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600651 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600652 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
653 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600654 break;
655
656 default:
657 output_mask = all_cache_outputs;
658 input_mask = all_cache_inputs;
659 break;
660 }
661
662 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800663 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600664}
665
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600666void VkImageObj::SetLayout(VkImageAspect aspect,
667 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600668{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600669 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600670
671 if (image_layout == m_descriptorInfo.imageLayout) {
672 return;
673 }
674
Tony Barbourfe3351b2015-07-28 10:17:20 -0600675 VkCmdPoolCreateInfo cmd_pool_info = {};
676 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
677 cmd_pool_info.pNext = NULL;
678 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
679 cmd_pool_info.flags = 0;
680 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
681 VkCommandBufferObj cmd_buf(m_device, pool.handle());
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600682
683 /* Build command buffer to set image layout in the driver */
684 err = cmd_buf.BeginCommandBuffer();
685 assert(!err);
686
687 SetLayout(&cmd_buf, aspect, image_layout);
688
689 err = cmd_buf.EndCommandBuffer();
690 assert(!err);
691
692 cmd_buf.QueueCommandBuffer();
693}
694
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600695bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600696{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600697 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600698 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600699 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600700
Tony Barbour579f7802015-04-03 15:11:43 -0600701 return true;
702}
703
Tony Barbour6918cd52015-04-09 12:58:51 -0600704void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600705 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600706 VkImageTiling requested_tiling,
707 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800708{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600709 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600710 VkFormatProperties image_fmt;
711 VkImageTiling tiling;
712 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600713
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800714 mipCount = 0;
715
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600716 uint32_t _w = w;
717 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800718 while( ( _w > 0 ) || ( _h > 0 ) )
719 {
720 _w >>= 1;
721 _h >>= 1;
722 mipCount++;
723 }
724
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600725 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), fmt, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600726 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600727
Tony Barbourd1c35722015-04-16 15:59:00 -0600728 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600729 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600730 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600731 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600732 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600733 } else {
734 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
735 }
736 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600737 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600738 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600739 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600740 } else {
741 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
742 }
743
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600744 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600745 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800746 imageCreateInfo.format = fmt;
747 imageCreateInfo.extent.width = w;
748 imageCreateInfo.extent.height = h;
749 imageCreateInfo.mipLevels = mipCount;
750 imageCreateInfo.tiling = tiling;
751
752 imageCreateInfo.usage = usage;
753
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600754 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800755
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600756 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600757 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600758 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600759 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600760 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800761}
762
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600763VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600764{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600765 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600766 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600767
Tony Barbourfe3351b2015-07-28 10:17:20 -0600768 VkCmdPoolCreateInfo cmd_pool_info = {};
769 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
770 cmd_pool_info.pNext = NULL;
771 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
772 cmd_pool_info.flags = 0;
773 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
774 VkCommandBufferObj cmd_buf(m_device, pool.handle());
775
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600776 /* Build command buffer to copy staging texture to usable texture */
777 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600778 assert(!err);
779
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600780 /* TODO: Can we determine image aspect from image object? */
781 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600782 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600783
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600784 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600785 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600786
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600787 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600788 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600789 copy_region.srcSubresource.arraySlice = 0;
790 copy_region.srcSubresource.mipLevel = 0;
791 copy_region.srcOffset.x = 0;
792 copy_region.srcOffset.y = 0;
793 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600794 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600795 copy_region.destSubresource.arraySlice = 0;
796 copy_region.destSubresource.mipLevel = 0;
797 copy_region.destOffset.x = 0;
798 copy_region.destOffset.y = 0;
799 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600800 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600801
Chia-I Wube2b9172015-07-03 11:49:42 +0800802 vkCmdCopyImage(cmd_buf.handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +0800803 src_image.handle(), src_image.layout(),
804 handle(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600805 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600806
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600807 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600808
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600809 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600810
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600811 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600812 assert(!err);
813
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600814 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600815
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600816 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600817}
818
Tony Barbour6918cd52015-04-09 12:58:51 -0600819VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
820 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700821{
822 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600823 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600824 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600825 void *data;
826 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600827 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600828 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600829
Tony Barboure65788f2015-07-21 17:01:42 -0600830 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 -0600831 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600832
833 if (colors == NULL)
834 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700835
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800836 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700837
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600838 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600839 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600840 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600841 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600842 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600843 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600844 view.channels.r = VK_CHANNEL_SWIZZLE_R;
845 view.channels.g = VK_CHANNEL_SWIZZLE_G;
846 view.channels.b = VK_CHANNEL_SWIZZLE_B;
847 view.channels.a = VK_CHANNEL_SWIZZLE_A;
848 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600849 view.subresourceRange.baseMipLevel = 0;
850 view.subresourceRange.mipLevels = 1;
851 view.subresourceRange.baseArraySlice = 0;
852 view.subresourceRange.arraySize = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700853
Tony Barboure2c58df2014-11-25 13:18:32 -0700854 /* create image */
Tony Barboure65788f2015-07-21 17:01:42 -0600855 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 -0700856
857 /* create image view */
Chia-I Wu681d7a02015-07-03 13:44:34 +0800858 view.image = handle();
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800859 m_textureView.init(*m_device, view);
Chia-I Wu3158bf32015-07-03 11:49:42 +0800860 m_descriptorInfo.imageView = m_textureView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700861
Chia-I Wu681d7a02015-07-03 13:44:34 +0800862 data = stagingImage.MapMemory();
Tony Barboure2c58df2014-11-25 13:18:32 -0700863
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800864 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700865 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800866 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600867 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700868 }
Chia-I Wu681d7a02015-07-03 13:44:34 +0800869 stagingImage.UnmapMemory();
Tony Barbour6918cd52015-04-09 12:58:51 -0600870 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700871}
Tony Barbour82c39522014-12-04 14:33:33 -0700872
Tony Barbour6918cd52015-04-09 12:58:51 -0600873VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700874{
Tony Barboure2c58df2014-11-25 13:18:32 -0700875 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700876
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600877 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800878 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600879 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
880 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
881 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600882 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600883 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
884 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
885 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800886 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600887 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600888 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800889 samplerCreateInfo.minLod = 0.0;
890 samplerCreateInfo.maxLod = 0.0;
Tony Barbour26b17f82015-06-25 16:56:44 -0600891 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Cody Northropab1f9e22015-08-11 15:50:55 -0600892 samplerCreateInfo.texelCoords = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700893
Chia-I Wue9864b52014-12-28 16:32:24 +0800894 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700895}
Tony Barboure2c58df2014-11-25 13:18:32 -0700896
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700897/*
898 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
899 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600900VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700901{
902 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700903 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700904
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800905 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700906}
907
Tony Barbour6918cd52015-04-09 12:58:51 -0600908VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600909{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600910 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600911 if (m_commandBuffer) {
912 delete m_commandBuffer;
Tony Barbour3d83f492015-07-28 10:23:02 -0600913 delete m_cmdPool;
Tony Barbour044703b2015-03-26 12:37:52 -0600914 }
915}
916
Tony Barbour6918cd52015-04-09 12:58:51 -0600917VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700918{
Tony Barboure2c58df2014-11-25 13:18:32 -0700919 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800920 m_commandBuffer = 0;
921
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800922 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700923 m_numVertices = constantCount;
924 m_stride = constantSize;
925
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600926 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800927 const size_t allocationSize = constantCount * constantSize;
Cody Northrop7fb43862015-06-22 14:56:14 -0600928 init_as_src_and_dst(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700929
Chia-I Wu681d7a02015-07-03 13:44:34 +0800930 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +0800931 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800932 memory().unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700933
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800934 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600935 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600936 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu681d7a02015-07-03 13:44:34 +0800937 view_info.buffer = handle();
Tony Barbourd1c35722015-04-16 15:59:00 -0600938 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800939 view_info.offset = 0;
940 view_info.range = allocationSize;
941 m_bufferView.init(*m_device, view_info);
942
Chia-I Wu3158bf32015-07-03 11:49:42 +0800943 this->m_descriptorInfo.bufferView = m_bufferView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700944}
Tony Barbour82c39522014-12-04 14:33:33 -0700945
Tony Barbourd1c35722015-04-16 15:59:00 -0600946void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700947{
Chia-I Wu681d7a02015-07-03 13:44:34 +0800948 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &handle(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700949}
950
951
Tony Barbour6918cd52015-04-09 12:58:51 -0600952void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600953 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600954 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600955 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
956 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
957 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
958 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600959 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600960 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600961 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
962 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
963 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
964 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
965 VK_MEMORY_INPUT_SHADER_READ_BIT |
966 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
967 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
968 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700969{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600970 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700971
Tony Barbour38422802014-12-10 14:36:31 -0700972 if (!m_commandBuffer)
973 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600974 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbourfe3351b2015-07-28 10:17:20 -0600975 VkCmdPoolCreateInfo cmd_pool_info = {};
976 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
977 cmd_pool_info.pNext = NULL;
978 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
979 cmd_pool_info.flags = 0;
980 m_cmdPool = new vk_testing::CmdPool(*m_device, cmd_pool_info);
981 m_commandBuffer = new VkCommandBufferObj(m_device, m_cmdPool->handle());
Tony Barbour38422802014-12-10 14:36:31 -0700982 }
983 else
984 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800985 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700986 }
987
Tony Barboure2c58df2014-11-25 13:18:32 -0700988 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600989 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600990 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600991 cmd_buf_info.pNext = NULL;
992 cmd_buf_info.flags = 0;
Cody Northropcc09b9e2015-08-11 11:35:58 -0600993 cmd_buf_info.renderPass = VK_NULL_HANDLE;
994 cmd_buf_info.subpass = 0;
995 cmd_buf_info.framebuffer = VK_NULL_HANDLE;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600996
Jon Ashburnc4164b12014-12-31 17:10:47 -0700997 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600998 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700999
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001000 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001001 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001002 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -07001003
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001004 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1005 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001006
1007 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001008 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -07001009
1010 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -07001011 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001012 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001013
Tony Barboure2c58df2014-11-25 13:18:32 -07001014 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001015 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -07001016 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wud9e8e822015-07-03 11:45:55 +08001017 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.handle() );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001018 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001019}
1020
Tony Barbour6918cd52015-04-09 12:58:51 -06001021VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
1022 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001023{
1024
1025}
1026
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001027void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001028{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001029 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001030
1031 m_numVertices = numIndexes;
1032 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001033 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -06001034 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001035 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -06001036 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001037 break;
Tony Barbourd1c35722015-04-16 15:59:00 -06001038 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001039 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -06001040 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001041 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001042 default:
1043 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -06001044 m_stride = 2;
1045 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001046 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001047 }
1048
Chia-I Wua07fee62014-12-28 15:26:08 +08001049 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -06001050 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northrop7fb43862015-06-22 14:56:14 -06001051 init_as_src_and_dst(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001052
Chia-I Wu681d7a02015-07-03 13:44:34 +08001053 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +08001054 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +08001055 memory().unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001056
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001057 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001058 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001059 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001060 view_info.buffer = handle();
Tony Barbourd1c35722015-04-16 15:59:00 -06001061 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001062 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001063 view_info.offset = 0;
1064 view_info.range = allocationSize;
1065 m_bufferView.init(*m_device, view_info);
1066
Chia-I Wu3158bf32015-07-03 11:49:42 +08001067 this->m_descriptorInfo.bufferView = m_bufferView.handle();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001068}
1069
Tony Barbourd1c35722015-04-16 15:59:00 -06001070void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001071{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001072 vkCmdBindIndexBuffer(cmdBuffer, handle(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001073}
Tony Barboure2c58df2014-11-25 13:18:32 -07001074
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001075VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001076{
1077 return m_indexType;
1078}
1079
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001080VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001081{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001082 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001083 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1084 stageInfo->stage = m_stage;
Chia-I Wu4d0c7922015-07-03 11:49:42 +08001085 stageInfo->shader = handle();
Tony Barboure2c58df2014-11-25 13:18:32 -07001086
Tony Barboure2c58df2014-11-25 13:18:32 -07001087 return stageInfo;
1088}
1089
Tony Barbourd1c35722015-04-16 15:59:00 -06001090VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001091{
Tony Barbourb5d2c942015-07-14 13:34:05 -06001092 VkResult U_ASSERT_ONLY err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001093 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001094 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001095 VkShaderModuleCreateInfo moduleCreateInfo;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001096 vk_testing::ShaderModule *module = new vk_testing::ShaderModule();
Tony Barboure2c58df2014-11-25 13:18:32 -07001097 size_t shader_len;
1098
1099 m_stage = stage;
1100 m_device = device;
1101
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001102 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1103 moduleCreateInfo.pNext = NULL;
1104
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001105 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001106 createInfo.pNext = NULL;
1107
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001108 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001109
1110 shader_len = strlen(shader_code);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001111 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1112 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1113 moduleCreateInfo.flags = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001114
Tony Barbourd1c35722015-04-16 15:59:00 -06001115 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001116 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1117 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1118 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1119 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barboure2c58df2014-11-25 13:18:32 -07001120
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001121 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001122
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001123 // Use Reference GLSL to SPV compiler
1124 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001125 moduleCreateInfo.pCode = spv.data();
1126 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1127 moduleCreateInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001128 }
Cody Northrop475663c2015-04-15 11:19:06 -06001129
Tony Barbour823e6ca2015-07-27 13:31:04 -06001130 err = module->init_try(*m_device, moduleCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001131 assert(VK_SUCCESS == err);
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001132
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001133 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1134 createInfo.pNext = NULL;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001135 createInfo.module = module->handle();
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001136 createInfo.pName = "main";
1137 createInfo.flags = 0;
1138
1139 err = init_try(*m_device, createInfo);
Cody Northrop475663c2015-04-15 11:19:06 -06001140 assert(VK_SUCCESS == err);
Tony Barbour823e6ca2015-07-27 13:31:04 -06001141 framework->m_shader_modules.push_back(module);
Tony Barbourf325bf12014-12-03 15:59:38 -07001142}
Tony Barbour82c39522014-12-04 14:33:33 -07001143
Tony Barbour6918cd52015-04-09 12:58:51 -06001144VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001145{
Tony Barboure2c58df2014-11-25 13:18:32 -07001146 m_device = device;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001147
1148 m_vi_state.pNext = VK_NULL_HANDLE;
1149 m_vi_state.bindingCount = 0;
1150 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1151 m_vi_state.attributeCount = 0;
1152 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1153
Tony Barboure2c58df2014-11-25 13:18:32 -07001154 m_vertexBufferCount = 0;
1155
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001156 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001157 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001158 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001159 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001160
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001161 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001162 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001163 m_rs_state.depthClipEnable = VK_FALSE;
1164 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001165 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001166 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1167 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001168
Tony Barboure2c58df2014-11-25 13:18:32 -07001169 memset(&m_cb_state,0,sizeof(m_cb_state));
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001170 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001171 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001172 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1173 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001174
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001175 m_ms_state.pNext = VK_NULL_HANDLE;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001176 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop02e61ed2015-08-04 14:34:54 -06001177 m_ms_state.pSampleMask = NULL;
Tony Barbourdfd533a2015-06-26 10:18:34 -06001178 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001179 m_ms_state.minSampleShading = 0;
1180 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001181
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001182 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001183 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001184 m_vp_state.viewportCount = 1;
Tony Barbourd81b8882015-05-15 09:37:57 -06001185
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001186 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001187 m_ds_state.pNext = VK_NULL_HANDLE,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001188 m_ds_state.depthTestEnable = VK_FALSE;
1189 m_ds_state.depthWriteEnable = VK_FALSE;
1190 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001191 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001192 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1193 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1194 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001195 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001196 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001197 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001198};
1199
Tony Barbour6918cd52015-04-09 12:58:51 -06001200void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001201{
1202 m_shaderObjs.push_back(shader);
1203}
1204
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001205void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001206{
1207 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1208 m_vi_state.attributeCount = count;
1209}
1210
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001211void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001212{
1213 m_vi_state.pVertexBindingDescriptions = vi_binding;
1214 m_vi_state.bindingCount = count;
1215}
1216
Tony Barbour6918cd52015-04-09 12:58:51 -06001217void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001218{
1219 m_vertexBufferObjs.push_back(vertexDataBuffer);
1220 m_vertexBufferBindings.push_back(binding);
1221 m_vertexBufferCount++;
1222}
1223
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001224void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineColorBlendAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001225{
Tony Barbourf52346d2015-01-16 14:27:35 -07001226 if (binding+1 > m_colorAttachments.size())
1227 {
1228 m_colorAttachments.resize(binding+1);
1229 }
1230 m_colorAttachments[binding] = *att;
1231}
1232
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001233void VkPipelineObj::SetDepthStencil(VkPipelineDepthStencilStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001234{
Tony Barbourf52346d2015-01-16 14:27:35 -07001235 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1236 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1237 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001238 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001239 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1240 m_ds_state.back = ds_state->back;
1241 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001242}
1243
Tony Barbour0ec8cd52015-08-06 09:27:11 -06001244void VkPipelineObj::SetMSAA(VkPipelineMultisampleStateCreateInfo *ms_state)
1245{
1246 memcpy(&m_ms_state, ms_state, sizeof(VkPipelineMultisampleStateCreateInfo));
1247}
1248
Tony Barbour5781e8f2015-08-04 16:23:11 -06001249VkResult VkPipelineObj::CreateVKPipeline(VkPipelineLayout layout, VkRenderPass render_pass)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001250{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001251 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001252
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001253 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001254
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001255 info.stageCount = m_shaderObjs.size();
1256 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1257
Tony Barbour976e1cf2014-12-17 11:57:31 -07001258 for (int i=0; i<m_shaderObjs.size(); i++)
1259 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001260 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001261 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001262 }
1263
Tony Barboure815fd72015-07-27 09:36:24 -06001264 if (m_vi_state.attributeCount && m_vi_state.bindingCount) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001265 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barboure815fd72015-07-27 09:36:24 -06001266 info.pVertexInputState = &m_vi_state;
1267 } else {
1268 info.pVertexInputState = NULL;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001269 }
1270
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001271 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001272 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001273 info.flags = 0;
Tony Barbour5781e8f2015-08-04 16:23:11 -06001274 info.layout = layout;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001275
Tony Barbourf52346d2015-01-16 14:27:35 -07001276 m_cb_state.attachmentCount = m_colorAttachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -06001277 m_cb_state.pAttachments = m_colorAttachments.data();
Tony Barbourf52346d2015-01-16 14:27:35 -07001278
Chia-I Wu08accc62015-07-07 11:50:03 +08001279 info.renderPass = render_pass;
1280 info.subpass = 0;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001281 info.pTessellationState = NULL;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001282 info.pInputAssemblyState = &m_ia_state;
1283 info.pViewportState = &m_vp_state;
1284 info.pRasterState = &m_rs_state;
1285 info.pMultisampleState = &m_ms_state;
1286 info.pDepthStencilState = &m_ds_state;
1287 info.pColorBlendState = &m_cb_state;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001288
Chris Forbes95292b12015-05-25 11:13:26 +12001289 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001290}
Chia-I Wu2648d092014-12-29 14:24:14 +08001291
Tony Barbourfe3351b2015-07-28 10:17:20 -06001292VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device, VkCmdPool pool)
Tony Barbour6d047bf2014-12-10 14:34:45 -07001293{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001294 m_device = device;
Courtney Goeltzenleuchter0c8385b2015-07-13 12:57:11 -06001295
Tony Barbourfe3351b2015-07-28 10:17:20 -06001296 init(*device, vk_testing::CmdBuffer::create_info(pool));
Tony Barbour6d047bf2014-12-10 14:34:45 -07001297}
Tony Barbour471338d2014-12-10 17:28:39 -07001298
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001299VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001300{
Chia-I Wube2b9172015-07-03 11:49:42 +08001301 return handle();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001302}
Tony Barbour471338d2014-12-10 17:28:39 -07001303
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001304VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001305{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001306 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001307 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001308}
1309
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001310VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001311{
1312 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001313 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001314}
1315
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001316VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001317{
Chia-I Wud28343c2014-12-28 15:12:48 +08001318 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001319 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001320}
1321
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001322void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, VkBool32 byRegion, uint32_t memBarrierCount, const void* const* ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001323{
Chia-I Wube2b9172015-07-03 11:49:42 +08001324 vkCmdPipelineBarrier(handle(), src_stages, dest_stages, byRegion, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001325}
1326
Chris Forbesf0796e12015-06-24 14:34:53 +12001327void VkCommandBufferObj::ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001328 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001329{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001330 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001331 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001332 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001333 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1334 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1335 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001336 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001337 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001338
1339 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001340 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001341 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001342 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001343 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001344 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001345 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001346
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001347 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001348 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001349 memory_barrier.outputMask = output_mask;
1350 memory_barrier.inputMask = input_mask;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001351 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001352 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001353 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001354
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001355 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1356 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001357
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001358 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001359 memory_barrier.image = m_renderTargets[i]->image();
1360 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001361 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001362 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001363
Chia-I Wube2b9172015-07-03 11:49:42 +08001364 vkCmdClearColorImage(handle(),
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001365 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001366 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001367
Tony Barbour30cc9e82014-12-17 11:53:55 -07001368 }
1369
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001370 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001371 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001372 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001373 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001374 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001375 dsRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001376 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001377 dsRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001378
1379 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001380
Cody Northrop3cc85e92015-08-04 10:47:08 -06001381 memory_barrier.oldLayout = memory_barrier.newLayout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001382 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001383 memory_barrier.image = depthStencilObj->handle();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001384 memory_barrier.subresourceRange = dsRange;
1385
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001386 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001387
Chia-I Wube2b9172015-07-03 11:49:42 +08001388 vkCmdClearDepthStencilImage(handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +08001389 depthStencilObj->handle(), VK_IMAGE_LAYOUT_GENERAL,
Chris Forbesd9be82b2015-06-22 17:21:59 +12001390 depth_clear_color, stencil_clear_color,
1391 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001392
1393 // prepare depth buffer for rendering
Chia-I Wu681d7a02015-07-03 13:44:34 +08001394 memory_barrier.image = depthStencilObj->handle();
Cody Northrop3cc85e92015-08-04 10:47:08 -06001395 memory_barrier.newLayout = memory_barrier.oldLayout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001396 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001397 memory_barrier.subresourceRange = dsRange;
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001398 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001399 }
1400}
1401
Tony Barbourc1eb1a52015-07-20 13:00:10 -06001402void VkCommandBufferObj::FillBuffer(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize fill_size, uint32_t data)
1403{
1404 vkCmdFillBuffer( handle(), buffer, offset, fill_size, data);
1405}
1406
Tony Barbour6918cd52015-04-09 12:58:51 -06001407void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001408{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001409 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001410 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001411 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001412 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1413 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1414 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001415 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001416 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001417 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001418 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1419 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1420 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1421 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1422 VK_MEMORY_INPUT_SHADER_READ_BIT |
1423 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1424 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001425 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001426
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001427 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001428 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001429 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001430 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001431 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001432 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001433
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001434 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001435 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001436 memory_barrier.outputMask = output_mask;
1437 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001438 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001439 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001440 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001441
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001442 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1443 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001444
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001445 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001446 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001447 memory_barrier.image = m_renderTargets[i]->image();
1448 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001449 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001450 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001451 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001452}
1453
Chia-I Wu08accc62015-07-07 11:50:03 +08001454void VkCommandBufferObj::BeginRenderPass(const VkRenderPassBeginInfo &info)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001455{
Chia-I Wube2b9172015-07-03 11:49:42 +08001456 vkCmdBeginRenderPass( handle(), &info, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001457}
1458
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001459void VkCommandBufferObj::EndRenderPass()
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001460{
Chia-I Wube2b9172015-07-03 11:49:42 +08001461 vkCmdEndRenderPass(handle());
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001462}
1463
Tony Barbour67e99152015-07-10 14:10:27 -06001464void VkCommandBufferObj::BindDynamicViewportState(VkDynamicViewportState viewportState)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001465{
Tony Barbour67e99152015-07-10 14:10:27 -06001466 vkCmdBindDynamicViewportState( handle(), viewportState);
1467}
1468
1469void VkCommandBufferObj::BindDynamicRasterState(VkDynamicRasterState rasterState)
1470{
1471 vkCmdBindDynamicRasterState( handle(), rasterState);
1472}
1473
1474void VkCommandBufferObj::BindDynamicColorBlendState(VkDynamicColorBlendState colorBlendState)
1475{
1476 vkCmdBindDynamicColorBlendState( handle(), colorBlendState);
1477}
1478
1479void VkCommandBufferObj::BindDynamicDepthStencilState(VkDynamicDepthStencilState depthStencilState)
1480{
1481 vkCmdBindDynamicDepthStencilState( handle(), depthStencilState);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001482}
1483
Tony Barbour6918cd52015-04-09 12:58:51 -06001484void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001485{
1486 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001487}
1488
Tony Barbour6918cd52015-04-09 12:58:51 -06001489void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001490{
Chia-I Wube2b9172015-07-03 11:49:42 +08001491 vkCmdDrawIndexed(handle(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001492}
1493
Tony Barbour6918cd52015-04-09 12:58:51 -06001494void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001495{
Chia-I Wube2b9172015-07-03 11:49:42 +08001496 vkCmdDraw(handle(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001497}
1498
Tony Barbour6918cd52015-04-09 12:58:51 -06001499void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001500{
Tony Barbour67e99152015-07-10 14:10:27 -06001501 VkFence nullFence = { VK_NULL_HANDLE };
1502 QueueCommandBuffer(nullFence);
Tony Barbour09b7ac32015-04-08 10:11:29 -06001503}
1504
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001505void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001506{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001507 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001508
1509 // submit the command buffer to the universal queue
Chia-I Wube2b9172015-07-03 11:49:42 +08001510 err = vkQueueSubmit( m_device->m_queue, 1, &handle(), fence );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001511 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001512
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001513 err = vkQueueWaitIdle( m_device->m_queue );
1514 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001515
1516 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001517 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001518}
1519
Tony Barbour6918cd52015-04-09 12:58:51 -06001520void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001521{
Chia-I Wube2b9172015-07-03 11:49:42 +08001522 vkCmdBindPipeline( handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.handle() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001523}
1524
Tony Barbour6918cd52015-04-09 12:58:51 -06001525void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001526{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001527 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001528
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001529 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wube2b9172015-07-03 11:49:42 +08001530 vkCmdBindDescriptorSets(handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northropd4c1a502015-04-16 13:41:56 -06001531 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001532}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001533
Cody Northropd2ad0342015-08-05 11:15:02 -06001534void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, VkDeviceSize offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001535{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001536 vkCmdBindIndexBuffer(handle(), indexBuffer->handle(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001537}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001538
Tony Barbourd1c35722015-04-16 15:59:00 -06001539void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001540{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001541 vkCmdBindVertexBuffers(handle(), binding, 1, &vertexBuffer->handle(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001542}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001543
Tony Barbour6918cd52015-04-09 12:58:51 -06001544VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001545{
1546 m_initialized = false;
1547}
Tony Barbour6918cd52015-04-09 12:58:51 -06001548bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001549{
1550 return m_initialized;
1551}
1552
Cody Northrop3cc85e92015-08-04 10:47:08 -06001553VkAttachmentView* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001554{
Chia-I Wu08accc62015-07-07 11:50:03 +08001555 return &m_attachmentBindInfo;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001556}
1557
Chia-I Wu08accc62015-07-07 11:50:03 +08001558void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height, VkFormat format)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001559{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001560 VkImageCreateInfo image_info;
Chia-I Wu08accc62015-07-07 11:50:03 +08001561 VkAttachmentViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001562
1563 m_device = device;
1564 m_initialized = true;
Chia-I Wu08accc62015-07-07 11:50:03 +08001565 m_depth_stencil_fmt = format;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001566
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001567 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001568 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001569 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001570 image_info.format = m_depth_stencil_fmt;
1571 image_info.extent.width = width;
1572 image_info.extent.height = height;
1573 image_info.extent.depth = 1;
1574 image_info.mipLevels = 1;
1575 image_info.arraySize = 1;
1576 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001577 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001578 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001579 image_info.flags = 0;
Tony Barbour3d83f492015-07-28 10:23:02 -06001580 image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1581 image_info.queueFamilyCount = 0;
1582 image_info.pQueueFamilyIndices = NULL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001583 init(*m_device, image_info);
1584
Chia-I Wu08accc62015-07-07 11:50:03 +08001585 view_info.sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001586 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001587 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001588 view_info.mipLevel = 0;
1589 view_info.baseArraySlice = 0;
1590 view_info.arraySize = 1;
1591 view_info.flags = 0;
Tony Barbour3d83f492015-07-28 10:23:02 -06001592 view_info.format = m_depth_stencil_fmt;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001593 view_info.image = handle();
Chia-I Wu08accc62015-07-07 11:50:03 +08001594 m_attachmentView.init(*m_device, view_info);
Tony Barbour1c45ce02015-03-27 17:03:18 -06001595
Cody Northrop3cc85e92015-08-04 10:47:08 -06001596 m_attachmentBindInfo = m_attachmentView.handle();
Tony Barbour1c45ce02015-03-27 17:03:18 -06001597}