blob: 4c03073259f59f0959b533ac9620f9a4034a0210 [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Jon Ashburn83a64252015-04-15 11:31:12 -060030#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
31
Tony Barbour6918cd52015-04-09 12:58:51 -060032VkRenderFramework::VkRenderFramework() :
Tony Barbourfe3351b2015-07-28 10:17:20 -060033 m_cmdBuffer(),
Tony Barbourf77fd652015-04-09 11:07:02 -060034 m_renderPass(VK_NULL_HANDLE),
35 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060036 m_stateRaster( VK_NULL_HANDLE ),
37 m_colorBlend( VK_NULL_HANDLE ),
38 m_stateViewport( VK_NULL_HANDLE ),
39 m_stateDepthStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060040 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070041 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060042 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
43 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Tony Barbour71bd4b32015-07-21 17:00:26 -060044 m_clear_via_load_op( true ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070045 m_depth_clear_color( 1.0 ),
Courtney Goeltzenleuchterb6c9aca2015-06-08 16:19:37 -060046 m_stencil_clear_color( 0 ),
47 m_depthStencil( NULL ),
48 m_dbgCreateMsgCallback( VK_NULL_HANDLE ),
49 m_dbgDestroyMsgCallback( VK_NULL_HANDLE ),
50 m_globalMsgCallback( VK_NULL_HANDLE ),
51 m_devMsgCallback( VK_NULL_HANDLE )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060052{
Tony Barbour1c45ce02015-03-27 17:03:18 -060053
Chia-I Wu08accc62015-07-07 11:50:03 +080054 memset(&m_renderPassBeginInfo, 0, sizeof(m_renderPassBeginInfo));
55 m_renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
56
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070057 // clear the back buffer to dark grey
Chris Forbesf0796e12015-06-24 14:34:53 +120058 m_clear_color.f32[0] = 0.25f;
59 m_clear_color.f32[1] = 0.25f;
60 m_clear_color.f32[2] = 0.25f;
61 m_clear_color.f32[3] = 0.0f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060062}
63
Tony Barbour6918cd52015-04-09 12:58:51 -060064VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060065{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060066
67}
68
Tony Barbour6918cd52015-04-09 12:58:51 -060069void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060070{
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060071 std::vector<const char*> instance_layer_names;
72 std::vector<const char*> device_layer_names;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060073 std::vector<const char*> instance_extension_names;
74 std::vector<const char*> device_extension_names;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060075 InitFramework(
76 instance_layer_names, device_layer_names,
77 instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060078}
79
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060080void VkRenderFramework::InitFramework(
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060081 std::vector<const char *> instance_layer_names,
82 std::vector<const char *> device_layer_names,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060083 std::vector<const char *> instance_extension_names,
84 std::vector<const char *> device_extension_names,
85 PFN_vkDbgMsgCallback dbgFunction,
86 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060087{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060088 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060089 std::vector<VkExtensionProperties> instance_extensions;
90 std::vector<VkExtensionProperties> device_extensions;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060091 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060092
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -060093 /* TODO: Verify requested extensions are available */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060094
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060095 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060096 instInfo.pNext = NULL;
97 instInfo.pAppInfo = &app_info;
98 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060099 instInfo.layerCount = instance_layer_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600100 instInfo.ppEnabledLayerNames = instance_layer_names.data();
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600101 instInfo.extensionCount = instance_extension_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600102 instInfo.ppEnabledExtensionNames = instance_extension_names.data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600103 err = vkCreateInstance(&instInfo, &this->inst);
104 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600105
Jon Ashburn83a64252015-04-15 11:31:12 -0600106 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
107 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
108 ASSERT_VK_SUCCESS(err);
109 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600110 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700111 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600112 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600113 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
114 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
115 if (m_dbgCreateMsgCallback) {
116 err = m_dbgCreateMsgCallback(this->inst,
117 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
118 dbgFunction,
119 userData,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600120 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600121 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600122
123 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
124 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600125 }
Tony Barbour15524c32015-04-29 17:34:29 -0600126 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600127
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600128 /* TODO: Verify requested physical device extensions are available */
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600129 m_device = new VkDeviceObj(0, objs[0], device_layer_names, device_extension_names);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600130
131 /* Now register callback on device */
132 if (0) {
133 if (m_dbgCreateMsgCallback) {
134 err = m_dbgCreateMsgCallback(this->inst,
135 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
136 dbgFunction,
137 userData,
138 &m_devMsgCallback);
139 ASSERT_VK_SUCCESS(err);
140 }
141 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600142 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600143
Tony Barbour6918cd52015-04-09 12:58:51 -0600144 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600145}
146
Tony Barbour6918cd52015-04-09 12:58:51 -0600147void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600148{
Tony Barbour67e99152015-07-10 14:10:27 -0600149 if (m_colorBlend) vkDestroyDynamicColorBlendState(device(), m_colorBlend);
150 if (m_stateDepthStencil) vkDestroyDynamicDepthStencilState(device(), m_stateDepthStencil);
151 if (m_stateRaster) vkDestroyDynamicRasterState(device(), m_stateRaster);
Tony Barbourfe3351b2015-07-28 10:17:20 -0600152 if (m_cmdBuffer)
153 delete m_cmdBuffer;
Cody Northrope62183e2015-07-09 18:08:05 -0600154 if (m_cmdPool) vkDestroyCommandPool(device(), m_cmdPool);
Tony Barbour67e99152015-07-10 14:10:27 -0600155 if (m_framebuffer) vkDestroyFramebuffer(device(), m_framebuffer);
156 if (m_renderPass) vkDestroyRenderPass(device(), m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600157
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600158 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
159 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
160
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600161 if (m_stateViewport) {
Tony Barbour67e99152015-07-10 14:10:27 -0600162 vkDestroyDynamicViewportState(device(), m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600163 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600164 while (!m_renderTargets.empty()) {
Tony Barbour67e99152015-07-10 14:10:27 -0600165 vkDestroyAttachmentView(device(), m_renderTargets.back()->targetView());
166 vkDestroyImage(device(), m_renderTargets.back()->image());
Mike Stroyanb050c682015-04-17 12:36:38 -0600167 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600168 m_renderTargets.pop_back();
169 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600170
Tony Barbour1c45ce02015-03-27 17:03:18 -0600171 delete m_depthStencil;
Tony Barbour823e6ca2015-07-27 13:31:04 -0600172 while (!m_shader_modules.empty())
173 {
174 delete m_shader_modules.back();
175 m_shader_modules.pop_back();
176 }
Tony Barbour1c45ce02015-03-27 17:03:18 -0600177
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600178 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800179 delete m_device;
Chris Forbesbe2fae62015-07-07 11:02:22 +1200180 if (this->inst) vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600181}
182
Tony Barbour6918cd52015-04-09 12:58:51 -0600183void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600184{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600185 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600186
Tony Barbourd1c35722015-04-16 15:59:00 -0600187 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600188
189 // create a raster state (solid, back-face culling)
Tony Barbour67e99152015-07-10 14:10:27 -0600190 VkDynamicRasterStateCreateInfo raster = {};
191 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700192
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600193 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
194 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600195
Tony Barbour67e99152015-07-10 14:10:27 -0600196 VkDynamicColorBlendStateCreateInfo blend = {};
197 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600198 blend.blendConst[0] = 1.0f;
199 blend.blendConst[1] = 1.0f;
200 blend.blendConst[2] = 1.0f;
201 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600202 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
203 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600204
Tony Barbour67e99152015-07-10 14:10:27 -0600205 VkDynamicDepthStencilStateCreateInfo depthStencil = {};
206 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski365feea2015-06-12 11:14:17 -0600207 depthStencil.minDepthBounds = 0.f;
208 depthStencil.maxDepthBounds = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700209 depthStencil.stencilFrontRef = 0;
210 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600211 depthStencil.stencilReadMask = 0xff;
212 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600213
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600214 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
215 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600216
Cody Northrope62183e2015-07-09 18:08:05 -0600217 VkCmdPoolCreateInfo cmd_pool_info;
218 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
219 cmd_pool_info.pNext = NULL,
220 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
221 cmd_pool_info.flags = 0,
222 err = vkCreateCommandPool(device(), &cmd_pool_info, &m_cmdPool);
223 assert(!err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600224
Tony Barbourfe3351b2015-07-28 10:17:20 -0600225 m_cmdBuffer = new VkCommandBufferObj(m_device, m_cmdPool);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600226}
227
Tony Barbour6918cd52015-04-09 12:58:51 -0600228void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600229{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600230 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600231
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600232 VkViewport viewport;
Chris Forbesd9be82b2015-06-22 17:21:59 +1200233 VkRect2D scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600234
Tony Barbour67e99152015-07-10 14:10:27 -0600235 VkDynamicViewportStateCreateInfo viewportCreate = {};
236 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700237 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700238 viewport.originX = 0;
239 viewport.originY = 0;
240 viewport.width = 1.f * width;
241 viewport.height = 1.f * height;
242 viewport.minDepth = 0.f;
243 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600244 scissor.extent.width = (int32_t) width;
245 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700246 scissor.offset.x = 0;
247 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700248 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700249 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700250
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600251 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
252 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600253 m_width = width;
254 m_height = height;
255}
256
Tony Barbour6918cd52015-04-09 12:58:51 -0600257void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600258{
259 InitViewport(m_width, m_height);
260}
Tony Barbour6918cd52015-04-09 12:58:51 -0600261void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600262{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700263 InitRenderTarget(1);
264}
265
Tony Barbour6918cd52015-04-09 12:58:51 -0600266void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700267{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600268 InitRenderTarget(targets, NULL);
269}
270
Chia-I Wu08accc62015-07-07 11:50:03 +0800271void VkRenderFramework::InitRenderTarget(VkAttachmentBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600272{
273 InitRenderTarget(1, dsBinding);
274}
275
Chia-I Wu08accc62015-07-07 11:50:03 +0800276void VkRenderFramework::InitRenderTarget(uint32_t targets, VkAttachmentBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600277{
Chia-I Wu08accc62015-07-07 11:50:03 +0800278 std::vector<VkAttachmentDescription> attachments;
279 std::vector<VkAttachmentReference> color_references;
280 std::vector<VkAttachmentBindInfo> bindings;
281 attachments.reserve(targets + (bool) dsBinding);
282 color_references.reserve(targets);
283 bindings.reserve(targets + (bool) dsBinding);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600284
Chia-I Wu08accc62015-07-07 11:50:03 +0800285 VkAttachmentDescription att = {};
286 att.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION;
287 att.pNext = NULL;
288 att.format = m_render_target_fmt;
289 att.samples = 1;
290 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
291 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
292 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
293 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
294 att.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
295 att.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chia-I Wuecebf752014-12-05 10:45:15 +0800296
Chia-I Wu08accc62015-07-07 11:50:03 +0800297 VkAttachmentReference ref = {};
298 ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
299
300 m_renderPassClearValues.clear();
301 VkClearValue clear = {};
302 clear.color = m_clear_color;
303
304 VkAttachmentBindInfo bind = {};
305 bind.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
306
307 for (uint32_t i = 0; i < targets; i++) {
308 attachments.push_back(att);
309
310 ref.attachment = i;
311 color_references.push_back(ref);
312
313 m_renderPassClearValues.push_back(clear);
314
Tony Barbour6918cd52015-04-09 12:58:51 -0600315 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600316
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600317 VkFormatProperties props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600318 VkResult err;
319
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600320 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), m_render_target_fmt, &props);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600321 ASSERT_VK_SUCCESS(err);
322
323 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600324 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600325 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
326 VK_IMAGE_TILING_LINEAR);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600327 }
328 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600329 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600330 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
331 VK_IMAGE_TILING_OPTIMAL);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600332 }
333 else {
334 FAIL() << "Neither Linear nor Optimal allowed for render target";
335 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600336
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600337 m_renderTargets.push_back(img);
Chia-I Wu08accc62015-07-07 11:50:03 +0800338 bind.view = img->targetView();
339
340 bindings.push_back(bind);
Chia-I Wuecebf752014-12-05 10:45:15 +0800341 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600342
Chia-I Wu08accc62015-07-07 11:50:03 +0800343 VkSubpassDescription subpass = {};
344 subpass.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION;
345 subpass.pNext = NULL;
346 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
347 subpass.flags = 0;
348 subpass.inputCount = 0;
349 subpass.inputAttachments = NULL;
350 subpass.colorCount = targets;
Tony Barbour482c6092015-07-27 09:37:48 -0600351 subpass.colorAttachments = color_references.data();
Chia-I Wu08accc62015-07-07 11:50:03 +0800352 subpass.resolveAttachments = NULL;
353
354 if (dsBinding) {
355 att.format = m_depth_stencil_fmt;
Tony Barbour71bd4b32015-07-21 17:00:26 -0600356 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 +0800357 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
358 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
359 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
360 att.initialLayout = dsBinding->layout;
361 att.finalLayout = dsBinding->layout;
362 attachments.push_back(att);
363
364 clear.ds.depth = m_depth_clear_color;
365 clear.ds.stencil = m_stencil_clear_color;
366 m_renderPassClearValues.push_back(clear);
367
368 bindings.push_back(*dsBinding);
369
370 subpass.depthStencilAttachment.attachment = targets;
371 subpass.depthStencilAttachment.layout = dsBinding->layout;
372 } else {
373 subpass.depthStencilAttachment.attachment = VK_ATTACHMENT_UNUSED;
374 }
375
376 subpass.preserveCount = 0;
377 subpass.preserveAttachments = NULL;
378
379 VkRenderPassCreateInfo rp_info = {};
380 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
381 rp_info.attachmentCount = attachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600382 rp_info.pAttachments = attachments.data();
Chia-I Wu08accc62015-07-07 11:50:03 +0800383 rp_info.subpassCount = 1;
384 rp_info.pSubpasses = &subpass;
385
386 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
387
388 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600389 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600390 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600391 fb_info.pNext = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800392 fb_info.renderPass = m_renderPass;
393 fb_info.attachmentCount = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600394 fb_info.pAttachments = bindings.data();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600395 fb_info.width = (uint32_t)m_width;
396 fb_info.height = (uint32_t)m_height;
397 fb_info.layers = 1;
398
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600399 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600400
Chia-I Wu08accc62015-07-07 11:50:03 +0800401 m_renderPassBeginInfo.renderPass = m_renderPass;
402 m_renderPassBeginInfo.framebuffer = m_framebuffer;
403 m_renderPassBeginInfo.renderArea.extent.width = m_width;
404 m_renderPassBeginInfo.renderArea.extent.height = m_height;
405 m_renderPassBeginInfo.attachmentCount = m_renderPassClearValues.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600406 m_renderPassBeginInfo.pAttachmentClearValues = m_renderPassClearValues.data();
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600407}
408
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600409
410
Tony Barbourd1c35722015-04-16 15:59:00 -0600411VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600412 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800413{
414 init();
415
Chia-I Wu999f0482015-07-03 10:32:05 +0800416 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600417 queue_props = phy().queue_properties().data();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800418}
419
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600420VkDeviceObj::VkDeviceObj(uint32_t id,
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600421 VkPhysicalDevice obj, std::vector<const char *> &layer_names,
422 std::vector<const char *> &extension_names) :
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600423 vk_testing::Device(obj), id(id)
424{
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600425 init(layer_names, extension_names);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600426
Chia-I Wu999f0482015-07-03 10:32:05 +0800427 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600428 queue_props = phy().queue_properties().data();
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600429}
430
Tony Barbour6918cd52015-04-09 12:58:51 -0600431void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800432{
433 ASSERT_NE(true, graphics_queues().empty());
Chia-I Wudf12ffd2015-07-03 10:53:18 +0800434 m_queue = graphics_queues()[0]->handle();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800435}
436
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600437VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
438 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700439{
Tony Barboure2c58df2014-11-25 13:18:32 -0700440
441}
442
Tony Barbour6918cd52015-04-09 12:58:51 -0600443VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700444{
Chia-I Wu11078b02015-01-04 16:27:24 +0800445 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700446}
Tony Barbour82c39522014-12-04 14:33:33 -0700447
Tony Barbour6918cd52015-04-09 12:58:51 -0600448int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700449{
Chia-I Wu11078b02015-01-04 16:27:24 +0800450 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600451 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600452 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800453 tc.count = 1;
454 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700455
Chia-I Wu11078b02015-01-04 16:27:24 +0800456 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700457}
Tony Barbour82c39522014-12-04 14:33:33 -0700458
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600459int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700460{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600461 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800462 tc.type = type;
463 tc.count = 1;
464 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700465
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800466 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
467 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600468
Chia-I Wu11078b02015-01-04 16:27:24 +0800469 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700470}
Tony Barbour82c39522014-12-04 14:33:33 -0700471
Tony Barbour6918cd52015-04-09 12:58:51 -0600472int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700473{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600474 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600475 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800476 tc.count = 1;
477 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700478
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800479 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Chia-I Wu8c721c62015-07-03 11:49:42 +0800480 tmp.sampler = sampler->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800481 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700482
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800483 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
484 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700485
Chia-I Wu11078b02015-01-04 16:27:24 +0800486 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700487}
Chia-I Wu11078b02015-01-04 16:27:24 +0800488
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500489VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700490{
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800491 return m_pipeline_layout.handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700492}
493
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600494VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700495{
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800496 return m_set->handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700497}
Tony Barboure2c58df2014-11-25 13:18:32 -0700498
Tony Barbour6918cd52015-04-09 12:58:51 -0600499void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700500{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600501 // create VkDescriptorPool
502 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600503 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800504 pool.count = m_type_counts.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600505 pool.pTypeCount = m_type_counts.data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600506 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700507
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600508 // create VkDescriptorSetLayout
509 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800510 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800511 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800512 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800513 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600514 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800515 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700516 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800517
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600518 // create VkDescriptorSetLayout
519 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600520 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800521 layout.count = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600522 layout.pBinding = bindings.data();
Chia-I Wub41393e2015-03-26 15:04:41 +0800523
Chia-I Wu41126e52015-03-26 15:27:55 +0800524 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600525 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800526 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500527
528 // create VkPipelineLayout
529 VkPipelineLayoutCreateInfo pipeline_layout = {};
530 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
531 pipeline_layout.descriptorSetCount = layouts.size();
532 pipeline_layout.pSetLayouts = NULL;
533
534 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700535
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600536 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500537 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800538
Chia-I Wu41126e52015-03-26 15:27:55 +0800539 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800540 size_t imageSamplerCount = 0;
541 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
542 it != m_writes.end(); it++) {
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800543 it->destSet = m_set->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800544 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
545 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800546 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800547
548 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800549 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700550}
Tony Barboure2c58df2014-11-25 13:18:32 -0700551
Tony Barbour6918cd52015-04-09 12:58:51 -0600552VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800553{
554 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800555 m_descriptorInfo.imageView = VK_NULL_HANDLE;
556 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800557}
558
Tony Barbour6918cd52015-04-09 12:58:51 -0600559void VkImageObj::ImageMemoryBarrier(
560 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600561 VkImageAspect aspect,
562 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600563 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600564 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
565 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
566 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
567 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600568 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600569 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600570 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
571 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
572 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
573 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
574 VK_MEMORY_INPUT_SHADER_READ_BIT |
575 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
576 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
577 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600578 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600579{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600580 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
581 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600582 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
583 subresourceRange);
584
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600585 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600586
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600587 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
588 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600589
590 // write barrier to the command buffer
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -0600591 vkCmdPipelineBarrier(cmd_buf->handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600592}
593
Tony Barbour6918cd52015-04-09 12:58:51 -0600594void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600595 VkImageAspect aspect,
596 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600597{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600598 VkFlags output_mask, input_mask;
599 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600600 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600601 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
602 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
603 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600604 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600605 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600606 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600607 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
608 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
609 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
610 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
611 VK_MEMORY_INPUT_SHADER_READ_BIT |
612 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
613 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600614 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600615
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800616 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600617 return;
618 }
619
620 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600621 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600622 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
623 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600624 break;
625
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600626 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600627 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
628 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600629 break;
630
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600631 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600632 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
633 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600634 break;
635
636 default:
637 output_mask = all_cache_outputs;
638 input_mask = all_cache_inputs;
639 break;
640 }
641
642 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800643 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600644}
645
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600646void VkImageObj::SetLayout(VkImageAspect aspect,
647 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600648{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600649 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600650
651 if (image_layout == m_descriptorInfo.imageLayout) {
652 return;
653 }
654
Tony Barbourfe3351b2015-07-28 10:17:20 -0600655 VkCmdPoolCreateInfo cmd_pool_info = {};
656 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
657 cmd_pool_info.pNext = NULL;
658 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
659 cmd_pool_info.flags = 0;
660 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
661 VkCommandBufferObj cmd_buf(m_device, pool.handle());
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600662
663 /* Build command buffer to set image layout in the driver */
664 err = cmd_buf.BeginCommandBuffer();
665 assert(!err);
666
667 SetLayout(&cmd_buf, aspect, image_layout);
668
669 err = cmd_buf.EndCommandBuffer();
670 assert(!err);
671
672 cmd_buf.QueueCommandBuffer();
673}
674
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600675bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600676{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600677 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600678 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600679 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600680
Tony Barbour579f7802015-04-03 15:11:43 -0600681 return true;
682}
683
Tony Barbour6918cd52015-04-09 12:58:51 -0600684void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600685 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600686 VkImageTiling requested_tiling,
687 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800688{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600689 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600690 VkFormatProperties image_fmt;
691 VkImageTiling tiling;
692 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600693
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800694 mipCount = 0;
695
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600696 uint32_t _w = w;
697 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800698 while( ( _w > 0 ) || ( _h > 0 ) )
699 {
700 _w >>= 1;
701 _h >>= 1;
702 mipCount++;
703 }
704
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600705 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), fmt, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600706 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600707
Tony Barbourd1c35722015-04-16 15:59:00 -0600708 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600709 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600710 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600711 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600712 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600713 } else {
714 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
715 }
716 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600717 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600718 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600719 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600720 } else {
721 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
722 }
723
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600724 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600725 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800726 imageCreateInfo.format = fmt;
727 imageCreateInfo.extent.width = w;
728 imageCreateInfo.extent.height = h;
729 imageCreateInfo.mipLevels = mipCount;
730 imageCreateInfo.tiling = tiling;
731
732 imageCreateInfo.usage = usage;
733
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600734 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800735
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600736 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600737 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600738 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600739 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600740 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800741}
742
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600743VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600744{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600745 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600746 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600747
Tony Barbourfe3351b2015-07-28 10:17:20 -0600748 VkCmdPoolCreateInfo cmd_pool_info = {};
749 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
750 cmd_pool_info.pNext = NULL;
751 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
752 cmd_pool_info.flags = 0;
753 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
754 VkCommandBufferObj cmd_buf(m_device, pool.handle());
755
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600756 /* Build command buffer to copy staging texture to usable texture */
757 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600758 assert(!err);
759
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600760 /* TODO: Can we determine image aspect from image object? */
761 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600762 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600763
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600764 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600765 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600766
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600767 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600768 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600769 copy_region.srcSubresource.arraySlice = 0;
770 copy_region.srcSubresource.mipLevel = 0;
771 copy_region.srcOffset.x = 0;
772 copy_region.srcOffset.y = 0;
773 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600774 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600775 copy_region.destSubresource.arraySlice = 0;
776 copy_region.destSubresource.mipLevel = 0;
777 copy_region.destOffset.x = 0;
778 copy_region.destOffset.y = 0;
779 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600780 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600781
Chia-I Wube2b9172015-07-03 11:49:42 +0800782 vkCmdCopyImage(cmd_buf.handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +0800783 src_image.handle(), src_image.layout(),
784 handle(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600785 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600786
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600787 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600788
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600789 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600790
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600791 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600792 assert(!err);
793
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600794 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600795
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600796 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600797}
798
Tony Barbour6918cd52015-04-09 12:58:51 -0600799VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
800 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700801{
802 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600803 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600804 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600805 void *data;
806 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600807 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600808 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600809
Tony Barboure65788f2015-07-21 17:01:42 -0600810 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 -0600811 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600812
813 if (colors == NULL)
814 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700815
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800816 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700817
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600818 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600819 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600820 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600821 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600822 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600823 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600824 view.channels.r = VK_CHANNEL_SWIZZLE_R;
825 view.channels.g = VK_CHANNEL_SWIZZLE_G;
826 view.channels.b = VK_CHANNEL_SWIZZLE_B;
827 view.channels.a = VK_CHANNEL_SWIZZLE_A;
828 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600829 view.subresourceRange.baseMipLevel = 0;
830 view.subresourceRange.mipLevels = 1;
831 view.subresourceRange.baseArraySlice = 0;
832 view.subresourceRange.arraySize = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700833
Tony Barboure2c58df2014-11-25 13:18:32 -0700834 /* create image */
Tony Barboure65788f2015-07-21 17:01:42 -0600835 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 -0700836
837 /* create image view */
Chia-I Wu681d7a02015-07-03 13:44:34 +0800838 view.image = handle();
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800839 m_textureView.init(*m_device, view);
Chia-I Wu3158bf32015-07-03 11:49:42 +0800840 m_descriptorInfo.imageView = m_textureView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700841
Chia-I Wu681d7a02015-07-03 13:44:34 +0800842 data = stagingImage.MapMemory();
Tony Barboure2c58df2014-11-25 13:18:32 -0700843
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800844 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700845 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800846 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600847 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700848 }
Chia-I Wu681d7a02015-07-03 13:44:34 +0800849 stagingImage.UnmapMemory();
Tony Barbour6918cd52015-04-09 12:58:51 -0600850 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700851}
Tony Barbour82c39522014-12-04 14:33:33 -0700852
Tony Barbour6918cd52015-04-09 12:58:51 -0600853VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700854{
Tony Barboure2c58df2014-11-25 13:18:32 -0700855 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700856
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600857 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800858 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600859 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
860 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
861 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600862 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600863 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
864 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
865 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800866 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600867 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600868 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800869 samplerCreateInfo.minLod = 0.0;
870 samplerCreateInfo.maxLod = 0.0;
Tony Barbour26b17f82015-06-25 16:56:44 -0600871 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700872
Chia-I Wue9864b52014-12-28 16:32:24 +0800873 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700874}
Tony Barboure2c58df2014-11-25 13:18:32 -0700875
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700876/*
877 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
878 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600879VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700880{
881 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700882 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700883
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800884 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700885}
886
Tony Barbour6918cd52015-04-09 12:58:51 -0600887VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600888{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600889 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600890 if (m_commandBuffer) {
Tony Barbourfe3351b2015-07-28 10:17:20 -0600891 delete m_cmdPool;
Tony Barbour044703b2015-03-26 12:37:52 -0600892 delete m_commandBuffer;
893 }
894}
895
Tony Barbour6918cd52015-04-09 12:58:51 -0600896VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700897{
Tony Barboure2c58df2014-11-25 13:18:32 -0700898 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800899 m_commandBuffer = 0;
900
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800901 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700902 m_numVertices = constantCount;
903 m_stride = constantSize;
904
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600905 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800906 const size_t allocationSize = constantCount * constantSize;
Cody Northrop7fb43862015-06-22 14:56:14 -0600907 init_as_src_and_dst(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700908
Chia-I Wu681d7a02015-07-03 13:44:34 +0800909 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +0800910 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800911 memory().unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700912
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800913 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600914 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600915 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu681d7a02015-07-03 13:44:34 +0800916 view_info.buffer = handle();
Tony Barbourd1c35722015-04-16 15:59:00 -0600917 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800918 view_info.offset = 0;
919 view_info.range = allocationSize;
920 m_bufferView.init(*m_device, view_info);
921
Chia-I Wu3158bf32015-07-03 11:49:42 +0800922 this->m_descriptorInfo.bufferView = m_bufferView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700923}
Tony Barbour82c39522014-12-04 14:33:33 -0700924
Tony Barbourd1c35722015-04-16 15:59:00 -0600925void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700926{
Chia-I Wu681d7a02015-07-03 13:44:34 +0800927 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &handle(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700928}
929
930
Tony Barbour6918cd52015-04-09 12:58:51 -0600931void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600932 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600933 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600934 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
935 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
936 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
937 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600938 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600939 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600940 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
941 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
942 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
943 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
944 VK_MEMORY_INPUT_SHADER_READ_BIT |
945 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
946 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
947 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700948{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600949 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700950
Tony Barbour38422802014-12-10 14:36:31 -0700951 if (!m_commandBuffer)
952 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600953 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbourfe3351b2015-07-28 10:17:20 -0600954 VkCmdPoolCreateInfo cmd_pool_info = {};
955 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
956 cmd_pool_info.pNext = NULL;
957 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
958 cmd_pool_info.flags = 0;
959 m_cmdPool = new vk_testing::CmdPool(*m_device, cmd_pool_info);
960 m_commandBuffer = new VkCommandBufferObj(m_device, m_cmdPool->handle());
Tony Barbour38422802014-12-10 14:36:31 -0700961 }
962 else
963 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800964 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700965 }
966
Tony Barboure2c58df2014-11-25 13:18:32 -0700967 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600968 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600969 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600970 cmd_buf_info.pNext = NULL;
971 cmd_buf_info.flags = 0;
972
Jon Ashburnc4164b12014-12-31 17:10:47 -0700973 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600974 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700975
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600976 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000977 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600978 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700979
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600980 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
981 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000982
983 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600984 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700985
986 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700987 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600988 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700989
Tony Barboure2c58df2014-11-25 13:18:32 -0700990 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600991 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700992 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wud9e8e822015-07-03 11:45:55 +0800993 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.handle() );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600994 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700995}
996
Tony Barbour6918cd52015-04-09 12:58:51 -0600997VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
998 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700999{
1000
1001}
1002
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001003void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001004{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001005 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001006
1007 m_numVertices = numIndexes;
1008 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001009 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -06001010 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001011 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -06001012 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001013 break;
Tony Barbourd1c35722015-04-16 15:59:00 -06001014 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001015 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -06001016 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001017 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001018 default:
1019 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -06001020 m_stride = 2;
1021 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001022 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001023 }
1024
Chia-I Wua07fee62014-12-28 15:26:08 +08001025 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -06001026 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northrop7fb43862015-06-22 14:56:14 -06001027 init_as_src_and_dst(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001028
Chia-I Wu681d7a02015-07-03 13:44:34 +08001029 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +08001030 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +08001031 memory().unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001032
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001033 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001034 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001035 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001036 view_info.buffer = handle();
Tony Barbourd1c35722015-04-16 15:59:00 -06001037 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001038 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001039 view_info.offset = 0;
1040 view_info.range = allocationSize;
1041 m_bufferView.init(*m_device, view_info);
1042
Chia-I Wu3158bf32015-07-03 11:49:42 +08001043 this->m_descriptorInfo.bufferView = m_bufferView.handle();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001044}
1045
Tony Barbourd1c35722015-04-16 15:59:00 -06001046void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001047{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001048 vkCmdBindIndexBuffer(cmdBuffer, handle(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001049}
Tony Barboure2c58df2014-11-25 13:18:32 -07001050
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001051VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001052{
1053 return m_indexType;
1054}
1055
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001056VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001057{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001058 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001059 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1060 stageInfo->stage = m_stage;
Chia-I Wu4d0c7922015-07-03 11:49:42 +08001061 stageInfo->shader = handle();
Tony Barboure2c58df2014-11-25 13:18:32 -07001062
Tony Barboure2c58df2014-11-25 13:18:32 -07001063 return stageInfo;
1064}
1065
Tony Barbourd1c35722015-04-16 15:59:00 -06001066VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001067{
Tony Barbourb5d2c942015-07-14 13:34:05 -06001068 VkResult U_ASSERT_ONLY err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001069 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001070 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001071 VkShaderModuleCreateInfo moduleCreateInfo;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001072 vk_testing::ShaderModule *module = new vk_testing::ShaderModule();
Tony Barboure2c58df2014-11-25 13:18:32 -07001073 size_t shader_len;
1074
1075 m_stage = stage;
1076 m_device = device;
1077
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001078 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1079 moduleCreateInfo.pNext = NULL;
1080
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001081 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001082 createInfo.pNext = NULL;
1083
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001084 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001085
1086 shader_len = strlen(shader_code);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001087 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1088 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1089 moduleCreateInfo.flags = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001090
Tony Barbourd1c35722015-04-16 15:59:00 -06001091 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001092 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1093 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1094 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1095 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barboure2c58df2014-11-25 13:18:32 -07001096
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001097 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001098
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001099 // Use Reference GLSL to SPV compiler
1100 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001101 moduleCreateInfo.pCode = spv.data();
1102 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1103 moduleCreateInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001104 }
Cody Northrop475663c2015-04-15 11:19:06 -06001105
Tony Barbour823e6ca2015-07-27 13:31:04 -06001106 err = module->init_try(*m_device, moduleCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001107 assert(VK_SUCCESS == err);
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001108
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001109 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1110 createInfo.pNext = NULL;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001111 createInfo.module = module->handle();
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001112 createInfo.pName = "main";
1113 createInfo.flags = 0;
1114
1115 err = init_try(*m_device, createInfo);
Cody Northrop475663c2015-04-15 11:19:06 -06001116 assert(VK_SUCCESS == err);
Tony Barbour823e6ca2015-07-27 13:31:04 -06001117 framework->m_shader_modules.push_back(module);
Tony Barbourf325bf12014-12-03 15:59:38 -07001118}
Tony Barbour82c39522014-12-04 14:33:33 -07001119
Tony Barbour6918cd52015-04-09 12:58:51 -06001120VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001121{
Tony Barboure2c58df2014-11-25 13:18:32 -07001122 m_device = device;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001123
1124 m_vi_state.pNext = VK_NULL_HANDLE;
1125 m_vi_state.bindingCount = 0;
1126 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1127 m_vi_state.attributeCount = 0;
1128 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1129
Tony Barboure2c58df2014-11-25 13:18:32 -07001130 m_vertexBufferCount = 0;
1131
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001132 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001133 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001134 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001135 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001136
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001137 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001138 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001139 m_rs_state.depthClipEnable = VK_FALSE;
1140 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001141 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001142 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1143 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001144
Tony Barboure2c58df2014-11-25 13:18:32 -07001145 memset(&m_cb_state,0,sizeof(m_cb_state));
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001146 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001147 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001148 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1149 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001150
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001151 m_ms_state.pNext = VK_NULL_HANDLE;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001152 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -07001153 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
Tony Barbourdfd533a2015-06-26 10:18:34 -06001154 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001155 m_ms_state.minSampleShading = 0;
1156 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001157
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001158 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001159 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001160 m_vp_state.viewportCount = 1;
Tony Barbourd81b8882015-05-15 09:37:57 -06001161
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001162 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001163 m_ds_state.pNext = VK_NULL_HANDLE,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001164 m_ds_state.depthTestEnable = VK_FALSE;
1165 m_ds_state.depthWriteEnable = VK_FALSE;
1166 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001167 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001168 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1169 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1170 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001171 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001172 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001173 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001174};
1175
Tony Barbour6918cd52015-04-09 12:58:51 -06001176void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001177{
1178 m_shaderObjs.push_back(shader);
1179}
1180
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001181void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001182{
1183 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1184 m_vi_state.attributeCount = count;
1185}
1186
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001187void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001188{
1189 m_vi_state.pVertexBindingDescriptions = vi_binding;
1190 m_vi_state.bindingCount = count;
1191}
1192
Tony Barbour6918cd52015-04-09 12:58:51 -06001193void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001194{
1195 m_vertexBufferObjs.push_back(vertexDataBuffer);
1196 m_vertexBufferBindings.push_back(binding);
1197 m_vertexBufferCount++;
1198}
1199
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001200void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineColorBlendAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001201{
Tony Barbourf52346d2015-01-16 14:27:35 -07001202 if (binding+1 > m_colorAttachments.size())
1203 {
1204 m_colorAttachments.resize(binding+1);
1205 }
1206 m_colorAttachments[binding] = *att;
1207}
1208
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001209void VkPipelineObj::SetDepthStencil(VkPipelineDepthStencilStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001210{
Tony Barbourf52346d2015-01-16 14:27:35 -07001211 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1212 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1213 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001214 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001215 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1216 m_ds_state.back = ds_state->back;
1217 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001218}
1219
Chia-I Wu08accc62015-07-07 11:50:03 +08001220VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet, VkRenderPass render_pass)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001221{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001222 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001223
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001224 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001225
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001226 info.stageCount = m_shaderObjs.size();
1227 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1228
Tony Barbour976e1cf2014-12-17 11:57:31 -07001229 for (int i=0; i<m_shaderObjs.size(); i++)
1230 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001231 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001232 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001233 }
1234
Tony Barboure815fd72015-07-27 09:36:24 -06001235 if (m_vi_state.attributeCount && m_vi_state.bindingCount) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001236 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barboure815fd72015-07-27 09:36:24 -06001237 info.pVertexInputState = &m_vi_state;
1238 } else {
1239 info.pVertexInputState = NULL;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001240 }
1241
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001242 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001243 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001244 info.flags = 0;
1245 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001246
Tony Barbourf52346d2015-01-16 14:27:35 -07001247 m_cb_state.attachmentCount = m_colorAttachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -06001248 m_cb_state.pAttachments = m_colorAttachments.data();
Tony Barbourf52346d2015-01-16 14:27:35 -07001249
Chia-I Wu08accc62015-07-07 11:50:03 +08001250 info.renderPass = render_pass;
1251 info.subpass = 0;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001252 info.pTessellationState = NULL;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001253 info.pInputAssemblyState = &m_ia_state;
1254 info.pViewportState = &m_vp_state;
1255 info.pRasterState = &m_rs_state;
1256 info.pMultisampleState = &m_ms_state;
1257 info.pDepthStencilState = &m_ds_state;
1258 info.pColorBlendState = &m_cb_state;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001259
Chris Forbes95292b12015-05-25 11:13:26 +12001260 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001261}
Chia-I Wu2648d092014-12-29 14:24:14 +08001262
Tony Barbourfe3351b2015-07-28 10:17:20 -06001263VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device, VkCmdPool pool)
Tony Barbour6d047bf2014-12-10 14:34:45 -07001264{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001265 m_device = device;
Courtney Goeltzenleuchter0c8385b2015-07-13 12:57:11 -06001266
Tony Barbourfe3351b2015-07-28 10:17:20 -06001267 init(*device, vk_testing::CmdBuffer::create_info(pool));
Tony Barbour6d047bf2014-12-10 14:34:45 -07001268}
Tony Barbour471338d2014-12-10 17:28:39 -07001269
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001270VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001271{
Chia-I Wube2b9172015-07-03 11:49:42 +08001272 return handle();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001273}
Tony Barbour471338d2014-12-10 17:28:39 -07001274
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001275VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001276{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001277 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001278 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001279}
1280
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001281VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001282{
1283 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001284 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001285}
1286
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001287VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001288{
Chia-I Wud28343c2014-12-28 15:12:48 +08001289 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001290 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001291}
1292
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001293void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, VkBool32 byRegion, uint32_t memBarrierCount, const void* const* ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001294{
Chia-I Wube2b9172015-07-03 11:49:42 +08001295 vkCmdPipelineBarrier(handle(), src_stages, dest_stages, byRegion, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001296}
1297
Chris Forbesf0796e12015-06-24 14:34:53 +12001298void VkCommandBufferObj::ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001299 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001300{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001301 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001302 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001303 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001304 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1305 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1306 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001307 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001308 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001309
1310 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001311 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001312 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001313 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001314 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001315 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001316 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001317
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001318 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001319 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001320 memory_barrier.outputMask = output_mask;
1321 memory_barrier.inputMask = input_mask;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001322 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001323 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001324 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001325
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001326 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1327 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001328
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001329 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001330 memory_barrier.image = m_renderTargets[i]->image();
1331 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001332 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001333 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001334
Chia-I Wube2b9172015-07-03 11:49:42 +08001335 vkCmdClearColorImage(handle(),
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001336 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001337 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001338
Tony Barbour30cc9e82014-12-17 11:53:55 -07001339 }
1340
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001341 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001342 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001343 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001344 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001345 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001346 dsRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001347 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001348 dsRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001349
1350 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001351
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001352 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001353 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001354 memory_barrier.image = depthStencilObj->handle();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001355 memory_barrier.subresourceRange = dsRange;
1356
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001357 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001358
Chia-I Wube2b9172015-07-03 11:49:42 +08001359 vkCmdClearDepthStencilImage(handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +08001360 depthStencilObj->handle(), VK_IMAGE_LAYOUT_GENERAL,
Chris Forbesd9be82b2015-06-22 17:21:59 +12001361 depth_clear_color, stencil_clear_color,
1362 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001363
1364 // prepare depth buffer for rendering
Chia-I Wu681d7a02015-07-03 13:44:34 +08001365 memory_barrier.image = depthStencilObj->handle();
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001366 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001367 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001368 memory_barrier.subresourceRange = dsRange;
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001369 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001370 }
1371}
1372
Tony Barbourc1eb1a52015-07-20 13:00:10 -06001373void VkCommandBufferObj::FillBuffer(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize fill_size, uint32_t data)
1374{
1375 vkCmdFillBuffer( handle(), buffer, offset, fill_size, data);
1376}
1377
Tony Barbour6918cd52015-04-09 12:58:51 -06001378void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001379{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001380 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001381 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001382 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001383 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1384 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1385 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001386 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001387 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001388 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001389 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1390 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1391 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1392 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1393 VK_MEMORY_INPUT_SHADER_READ_BIT |
1394 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1395 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001396 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001397
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001398 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001399 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001400 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001401 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001402 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001403 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001404
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001405 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001406 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001407 memory_barrier.outputMask = output_mask;
1408 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001409 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001410 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001411 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001412
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001413 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1414 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001415
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001416 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001417 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001418 memory_barrier.image = m_renderTargets[i]->image();
1419 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001420 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001421 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001422 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001423}
1424
Chia-I Wu08accc62015-07-07 11:50:03 +08001425void VkCommandBufferObj::BeginRenderPass(const VkRenderPassBeginInfo &info)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001426{
Chia-I Wube2b9172015-07-03 11:49:42 +08001427 vkCmdBeginRenderPass( handle(), &info, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001428}
1429
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001430void VkCommandBufferObj::EndRenderPass()
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001431{
Chia-I Wube2b9172015-07-03 11:49:42 +08001432 vkCmdEndRenderPass(handle());
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001433}
1434
Tony Barbour67e99152015-07-10 14:10:27 -06001435void VkCommandBufferObj::BindDynamicViewportState(VkDynamicViewportState viewportState)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001436{
Tony Barbour67e99152015-07-10 14:10:27 -06001437 vkCmdBindDynamicViewportState( handle(), viewportState);
1438}
1439
1440void VkCommandBufferObj::BindDynamicRasterState(VkDynamicRasterState rasterState)
1441{
1442 vkCmdBindDynamicRasterState( handle(), rasterState);
1443}
1444
1445void VkCommandBufferObj::BindDynamicColorBlendState(VkDynamicColorBlendState colorBlendState)
1446{
1447 vkCmdBindDynamicColorBlendState( handle(), colorBlendState);
1448}
1449
1450void VkCommandBufferObj::BindDynamicDepthStencilState(VkDynamicDepthStencilState depthStencilState)
1451{
1452 vkCmdBindDynamicDepthStencilState( handle(), depthStencilState);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001453}
1454
Tony Barbour6918cd52015-04-09 12:58:51 -06001455void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001456{
1457 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001458}
1459
Tony Barbour6918cd52015-04-09 12:58:51 -06001460void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001461{
Chia-I Wube2b9172015-07-03 11:49:42 +08001462 vkCmdDrawIndexed(handle(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001463}
1464
Tony Barbour6918cd52015-04-09 12:58:51 -06001465void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001466{
Chia-I Wube2b9172015-07-03 11:49:42 +08001467 vkCmdDraw(handle(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001468}
1469
Tony Barbour6918cd52015-04-09 12:58:51 -06001470void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001471{
Tony Barbour67e99152015-07-10 14:10:27 -06001472 VkFence nullFence = { VK_NULL_HANDLE };
1473 QueueCommandBuffer(nullFence);
Tony Barbour09b7ac32015-04-08 10:11:29 -06001474}
1475
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001476void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001477{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001478 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001479
1480 // submit the command buffer to the universal queue
Chia-I Wube2b9172015-07-03 11:49:42 +08001481 err = vkQueueSubmit( m_device->m_queue, 1, &handle(), fence );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001482 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001483
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001484 err = vkQueueWaitIdle( m_device->m_queue );
1485 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001486
1487 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001488 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001489}
1490
Tony Barbour6918cd52015-04-09 12:58:51 -06001491void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001492{
Chia-I Wube2b9172015-07-03 11:49:42 +08001493 vkCmdBindPipeline( handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.handle() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001494}
1495
Tony Barbour6918cd52015-04-09 12:58:51 -06001496void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001497{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001498 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001499
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001500 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wube2b9172015-07-03 11:49:42 +08001501 vkCmdBindDescriptorSets(handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northropd4c1a502015-04-16 13:41:56 -06001502 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001503}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001504
Tony Barbour6918cd52015-04-09 12:58:51 -06001505void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001506{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001507 vkCmdBindIndexBuffer(handle(), indexBuffer->handle(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001508}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001509
Tony Barbourd1c35722015-04-16 15:59:00 -06001510void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001511{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001512 vkCmdBindVertexBuffers(handle(), binding, 1, &vertexBuffer->handle(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001513}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001514
Tony Barbour6918cd52015-04-09 12:58:51 -06001515VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001516{
1517 m_initialized = false;
1518}
Tony Barbour6918cd52015-04-09 12:58:51 -06001519bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001520{
1521 return m_initialized;
1522}
1523
Chia-I Wu08accc62015-07-07 11:50:03 +08001524VkAttachmentBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001525{
Chia-I Wu08accc62015-07-07 11:50:03 +08001526 return &m_attachmentBindInfo;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001527}
1528
Chia-I Wu08accc62015-07-07 11:50:03 +08001529void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height, VkFormat format)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001530{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001531 VkImageCreateInfo image_info;
Chia-I Wu08accc62015-07-07 11:50:03 +08001532 VkAttachmentViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001533
1534 m_device = device;
1535 m_initialized = true;
Chia-I Wu08accc62015-07-07 11:50:03 +08001536 m_depth_stencil_fmt = format;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001537
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001538 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001539 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001540 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001541 image_info.format = m_depth_stencil_fmt;
1542 image_info.extent.width = width;
1543 image_info.extent.height = height;
1544 image_info.extent.depth = 1;
1545 image_info.mipLevels = 1;
1546 image_info.arraySize = 1;
1547 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001548 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001549 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001550 image_info.flags = 0;
1551 init(*m_device, image_info);
1552
Chia-I Wu08accc62015-07-07 11:50:03 +08001553 view_info.sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001554 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001555 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001556 view_info.mipLevel = 0;
1557 view_info.baseArraySlice = 0;
1558 view_info.arraySize = 1;
1559 view_info.flags = 0;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001560 view_info.image = handle();
Chia-I Wu08accc62015-07-07 11:50:03 +08001561 m_attachmentView.init(*m_device, view_info);
Tony Barbour1c45ce02015-03-27 17:03:18 -06001562
Chia-I Wu3158bf32015-07-03 11:49:42 +08001563 m_attachmentBindInfo.view = m_attachmentView.handle();
Chia-I Wu08accc62015-07-07 11:50:03 +08001564 m_attachmentBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001565}