blob: 7361e290bd1c46ef5ce4e9b557b22c049dd4c2ec [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() :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060033 m_cmdBuffer( VK_NULL_HANDLE ),
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 ),
Chris Forbese182fe02015-06-15 09:32:35 +120044 m_clear_via_load_op( false ),
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();
100 instInfo.ppEnabledLayerNames =(instance_layer_names.size()) ? &instance_layer_names[0] : NULL;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600101 instInfo.extensionCount = instance_extension_names.size();
102 instInfo.ppEnabledExtensionNames = (instance_extension_names.size()) ? &instance_extension_names[0] : NULL;
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{
Mike Stroyanb050c682015-04-17 12:36:38 -0600149 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
150 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
151 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
152 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
153 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
154 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600155
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600156 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
157 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
158
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600159 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600160 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600161 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600162 while (!m_renderTargets.empty()) {
Chia-I Wu08accc62015-07-07 11:50:03 +0800163 vkDestroyObject(device(), VK_OBJECT_TYPE_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
Mike Stroyanb050c682015-04-17 12:36:38 -0600164 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
165 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600166 m_renderTargets.pop_back();
167 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600168
Tony Barbour1c45ce02015-03-27 17:03:18 -0600169 delete m_depthStencil;
170
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600171 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800172 delete m_device;
Chris Forbesbe2fae62015-07-07 11:02:22 +1200173 if (this->inst) vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600174}
175
Tony Barbour6918cd52015-04-09 12:58:51 -0600176void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600177{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600178 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600179
Tony Barbourd1c35722015-04-16 15:59:00 -0600180 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600181
182 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600183 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600184 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700185
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600186 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
187 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600188
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600189 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600190 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600191 blend.blendConst[0] = 1.0f;
192 blend.blendConst[1] = 1.0f;
193 blend.blendConst[2] = 1.0f;
194 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600195 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
196 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600197
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600198 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600199 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski365feea2015-06-12 11:14:17 -0600200 depthStencil.minDepthBounds = 0.f;
201 depthStencil.maxDepthBounds = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700202 depthStencil.stencilFrontRef = 0;
203 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600204 depthStencil.stencilReadMask = 0xff;
205 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600206
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600207 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
208 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600209
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600210 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600211
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600212 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700213 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
214
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600215 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
216 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600217}
218
Tony Barbour6918cd52015-04-09 12:58:51 -0600219void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600220{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600221 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600222
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600223 VkViewport viewport;
Chris Forbesd9be82b2015-06-22 17:21:59 +1200224 VkRect2D scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600225
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600226 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600227 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700228 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700229 viewport.originX = 0;
230 viewport.originY = 0;
231 viewport.width = 1.f * width;
232 viewport.height = 1.f * height;
233 viewport.minDepth = 0.f;
234 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600235 scissor.extent.width = (int32_t) width;
236 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700237 scissor.offset.x = 0;
238 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700239 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700240 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700241
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600242 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
243 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600244 m_width = width;
245 m_height = height;
246}
247
Tony Barbour6918cd52015-04-09 12:58:51 -0600248void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600249{
250 InitViewport(m_width, m_height);
251}
Tony Barbour6918cd52015-04-09 12:58:51 -0600252void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600253{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700254 InitRenderTarget(1);
255}
256
Tony Barbour6918cd52015-04-09 12:58:51 -0600257void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700258{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600259 InitRenderTarget(targets, NULL);
260}
261
Chia-I Wu08accc62015-07-07 11:50:03 +0800262void VkRenderFramework::InitRenderTarget(VkAttachmentBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600263{
264 InitRenderTarget(1, dsBinding);
265}
266
Chia-I Wu08accc62015-07-07 11:50:03 +0800267void VkRenderFramework::InitRenderTarget(uint32_t targets, VkAttachmentBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600268{
Chia-I Wu08accc62015-07-07 11:50:03 +0800269 std::vector<VkAttachmentDescription> attachments;
270 std::vector<VkAttachmentReference> color_references;
271 std::vector<VkAttachmentBindInfo> bindings;
272 attachments.reserve(targets + (bool) dsBinding);
273 color_references.reserve(targets);
274 bindings.reserve(targets + (bool) dsBinding);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600275
Chia-I Wu08accc62015-07-07 11:50:03 +0800276 VkAttachmentDescription att = {};
277 att.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION;
278 att.pNext = NULL;
279 att.format = m_render_target_fmt;
280 att.samples = 1;
281 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
282 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
283 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
284 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
285 att.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
286 att.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chia-I Wuecebf752014-12-05 10:45:15 +0800287
Chia-I Wu08accc62015-07-07 11:50:03 +0800288 VkAttachmentReference ref = {};
289 ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
290
291 m_renderPassClearValues.clear();
292 VkClearValue clear = {};
293 clear.color = m_clear_color;
294
295 VkAttachmentBindInfo bind = {};
296 bind.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
297
298 for (uint32_t i = 0; i < targets; i++) {
299 attachments.push_back(att);
300
301 ref.attachment = i;
302 color_references.push_back(ref);
303
304 m_renderPassClearValues.push_back(clear);
305
Tony Barbour6918cd52015-04-09 12:58:51 -0600306 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600307
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600308 VkFormatProperties props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600309 VkResult err;
310
Chia-I Wu999f0482015-07-03 10:32:05 +0800311 err = vkGetPhysicalDeviceFormatInfo(m_device->phy().handle(), m_render_target_fmt, &props);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600312 ASSERT_VK_SUCCESS(err);
313
314 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600315 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600316 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_LINEAR);
317 }
318 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600319 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600320 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL);
321 }
322 else {
323 FAIL() << "Neither Linear nor Optimal allowed for render target";
324 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600325
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600326 m_renderTargets.push_back(img);
Chia-I Wu08accc62015-07-07 11:50:03 +0800327 bind.view = img->targetView();
328
329 bindings.push_back(bind);
Chia-I Wuecebf752014-12-05 10:45:15 +0800330 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600331
Chia-I Wu08accc62015-07-07 11:50:03 +0800332 VkSubpassDescription subpass = {};
333 subpass.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION;
334 subpass.pNext = NULL;
335 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
336 subpass.flags = 0;
337 subpass.inputCount = 0;
338 subpass.inputAttachments = NULL;
339 subpass.colorCount = targets;
340 subpass.colorAttachments = &color_references[0];
341 subpass.resolveAttachments = NULL;
342
343 if (dsBinding) {
344 att.format = m_depth_stencil_fmt;
345 att.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
346 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
347 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
348 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
349 att.initialLayout = dsBinding->layout;
350 att.finalLayout = dsBinding->layout;
351 attachments.push_back(att);
352
353 clear.ds.depth = m_depth_clear_color;
354 clear.ds.stencil = m_stencil_clear_color;
355 m_renderPassClearValues.push_back(clear);
356
357 bindings.push_back(*dsBinding);
358
359 subpass.depthStencilAttachment.attachment = targets;
360 subpass.depthStencilAttachment.layout = dsBinding->layout;
361 } else {
362 subpass.depthStencilAttachment.attachment = VK_ATTACHMENT_UNUSED;
363 }
364
365 subpass.preserveCount = 0;
366 subpass.preserveAttachments = NULL;
367
368 VkRenderPassCreateInfo rp_info = {};
369 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
370 rp_info.attachmentCount = attachments.size();
371 rp_info.pAttachments = &attachments[0];
372 rp_info.subpassCount = 1;
373 rp_info.pSubpasses = &subpass;
374
375 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
376
377 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600378 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600379 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600380 fb_info.pNext = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800381 fb_info.renderPass = m_renderPass;
382 fb_info.attachmentCount = bindings.size();
383 fb_info.pAttachments = &bindings[0];
Tony Barbourbdf0a312015-04-01 17:10:07 -0600384 fb_info.width = (uint32_t)m_width;
385 fb_info.height = (uint32_t)m_height;
386 fb_info.layers = 1;
387
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600388 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600389
Chia-I Wu08accc62015-07-07 11:50:03 +0800390 m_renderPassBeginInfo.renderPass = m_renderPass;
391 m_renderPassBeginInfo.framebuffer = m_framebuffer;
392 m_renderPassBeginInfo.renderArea.extent.width = m_width;
393 m_renderPassBeginInfo.renderArea.extent.height = m_height;
394 m_renderPassBeginInfo.attachmentCount = m_renderPassClearValues.size();
395 m_renderPassBeginInfo.pAttachmentClearValues = &m_renderPassClearValues[0];
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600396}
397
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600398
399
Tony Barbourd1c35722015-04-16 15:59:00 -0600400VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600401 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800402{
403 init();
404
Chia-I Wu999f0482015-07-03 10:32:05 +0800405 props = phy().properties();
406 queue_props = &phy().queue_properties()[0];
Chia-I Wufb1459b2014-12-29 15:23:20 +0800407}
408
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600409VkDeviceObj::VkDeviceObj(uint32_t id,
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600410 VkPhysicalDevice obj, std::vector<const char *> &layer_names,
411 std::vector<const char *> &extension_names) :
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600412 vk_testing::Device(obj), id(id)
413{
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600414 init(layer_names, extension_names);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600415
Chia-I Wu999f0482015-07-03 10:32:05 +0800416 props = phy().properties();
417 queue_props = &phy().queue_properties()[0];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600418}
419
Tony Barbour6918cd52015-04-09 12:58:51 -0600420void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800421{
422 ASSERT_NE(true, graphics_queues().empty());
423 m_queue = graphics_queues()[0]->obj();
424}
425
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600426VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
427 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700428{
Tony Barboure2c58df2014-11-25 13:18:32 -0700429
430}
431
Tony Barbour6918cd52015-04-09 12:58:51 -0600432VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700433{
Chia-I Wu11078b02015-01-04 16:27:24 +0800434 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700435}
Tony Barbour82c39522014-12-04 14:33:33 -0700436
Tony Barbour6918cd52015-04-09 12:58:51 -0600437int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700438{
Chia-I Wu11078b02015-01-04 16:27:24 +0800439 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600440 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600441 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800442 tc.count = 1;
443 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700444
Chia-I Wu11078b02015-01-04 16:27:24 +0800445 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700446}
Tony Barbour82c39522014-12-04 14:33:33 -0700447
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600448int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700449{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600450 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800451 tc.type = type;
452 tc.count = 1;
453 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700454
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800455 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
456 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600457
Chia-I Wu11078b02015-01-04 16:27:24 +0800458 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700459}
Tony Barbour82c39522014-12-04 14:33:33 -0700460
Tony Barbour6918cd52015-04-09 12:58:51 -0600461int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700462{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600463 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600464 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800465 tc.count = 1;
466 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700467
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800468 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600469 tmp.sampler = sampler->obj();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800470 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700471
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800472 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
473 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700474
Chia-I Wu11078b02015-01-04 16:27:24 +0800475 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700476}
Chia-I Wu11078b02015-01-04 16:27:24 +0800477
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500478VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700479{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500480 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700481}
482
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600483VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700484{
Chia-I Wu11078b02015-01-04 16:27:24 +0800485 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700486}
Tony Barboure2c58df2014-11-25 13:18:32 -0700487
Tony Barbour6918cd52015-04-09 12:58:51 -0600488void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700489{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600490 // create VkDescriptorPool
491 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600492 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800493 pool.count = m_type_counts.size();
494 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600495 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700496
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600497 // create VkDescriptorSetLayout
498 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800499 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800500 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800501 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800502 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600503 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800504 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700505 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800506
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600507 // create VkDescriptorSetLayout
508 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600509 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800510 layout.count = bindings.size();
511 layout.pBinding = &bindings[0];
512
Chia-I Wu41126e52015-03-26 15:27:55 +0800513 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600514 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800515 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500516
517 // create VkPipelineLayout
518 VkPipelineLayoutCreateInfo pipeline_layout = {};
519 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
520 pipeline_layout.descriptorSetCount = layouts.size();
521 pipeline_layout.pSetLayouts = NULL;
522
523 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700524
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600525 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500526 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800527
Chia-I Wu41126e52015-03-26 15:27:55 +0800528 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800529 size_t imageSamplerCount = 0;
530 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
531 it != m_writes.end(); it++) {
532 it->destSet = m_set->obj();
533 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
534 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800535 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800536
537 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800538 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700539}
Tony Barboure2c58df2014-11-25 13:18:32 -0700540
Tony Barbour6918cd52015-04-09 12:58:51 -0600541VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800542{
543 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800544 m_descriptorInfo.imageView = VK_NULL_HANDLE;
545 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800546}
547
Tony Barbour6918cd52015-04-09 12:58:51 -0600548void VkImageObj::ImageMemoryBarrier(
549 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600550 VkImageAspect aspect,
551 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600552 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600553 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
554 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
555 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
556 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600557 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600558 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600559 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
560 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
561 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
562 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
563 VK_MEMORY_INPUT_SHADER_READ_BIT |
564 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
565 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
566 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600567 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600568{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600569 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
570 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600571 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
572 subresourceRange);
573
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600574 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600575
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600576 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
577 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600578
579 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600580 vkCmdPipelineBarrier(cmd_buf->obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600581}
582
Tony Barbour6918cd52015-04-09 12:58:51 -0600583void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600584 VkImageAspect aspect,
585 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600586{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600587 VkFlags output_mask, input_mask;
588 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600589 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600590 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
591 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
592 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600593 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600594 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600595 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600596 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
597 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
598 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
599 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
600 VK_MEMORY_INPUT_SHADER_READ_BIT |
601 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
602 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600603 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600604
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800605 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600606 return;
607 }
608
609 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600610 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600611 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
612 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600613 break;
614
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600615 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600616 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
617 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600618 break;
619
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600620 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600621 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
622 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600623 break;
624
625 default:
626 output_mask = all_cache_outputs;
627 input_mask = all_cache_inputs;
628 break;
629 }
630
631 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800632 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600633}
634
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600635void VkImageObj::SetLayout(VkImageAspect aspect,
636 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600637{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600638 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600639
640 if (image_layout == m_descriptorInfo.imageLayout) {
641 return;
642 }
643
Tony Barbour6918cd52015-04-09 12:58:51 -0600644 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600645
646 /* Build command buffer to set image layout in the driver */
647 err = cmd_buf.BeginCommandBuffer();
648 assert(!err);
649
650 SetLayout(&cmd_buf, aspect, image_layout);
651
652 err = cmd_buf.EndCommandBuffer();
653 assert(!err);
654
655 cmd_buf.QueueCommandBuffer();
656}
657
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600658bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600659{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600660 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600661 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600662 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600663
Tony Barbour579f7802015-04-03 15:11:43 -0600664 return true;
665}
666
Tony Barbour6918cd52015-04-09 12:58:51 -0600667void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600668 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600669 VkImageTiling requested_tiling,
670 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800671{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600672 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600673 VkFormatProperties image_fmt;
674 VkImageTiling tiling;
675 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600676
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800677 mipCount = 0;
678
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600679 uint32_t _w = w;
680 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800681 while( ( _w > 0 ) || ( _h > 0 ) )
682 {
683 _w >>= 1;
684 _h >>= 1;
685 mipCount++;
686 }
687
Chia-I Wu999f0482015-07-03 10:32:05 +0800688 err = vkGetPhysicalDeviceFormatInfo(m_device->phy().handle(), fmt, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600689 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600690
Tony Barbourd1c35722015-04-16 15:59:00 -0600691 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600692 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600693 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600694 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600695 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600696 } else {
697 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
698 }
699 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600700 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600701 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600702 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600703 } else {
704 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
705 }
706
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600707 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600708 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800709 imageCreateInfo.format = fmt;
710 imageCreateInfo.extent.width = w;
711 imageCreateInfo.extent.height = h;
712 imageCreateInfo.mipLevels = mipCount;
713 imageCreateInfo.tiling = tiling;
714
715 imageCreateInfo.usage = usage;
716
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600717 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800718
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600719 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600720 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600721 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600722 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600723 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800724}
725
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600726VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800727{
728 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600729 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800730}
731
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600732VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800733{
734 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600735 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800736}
737
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600738VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600739{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600740 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600741 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600742 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600743
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600744 /* Build command buffer to copy staging texture to usable texture */
745 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600746 assert(!err);
747
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600748 /* TODO: Can we determine image aspect from image object? */
749 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600750 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600751
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600752 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600753 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600754
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600755 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600756 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600757 copy_region.srcSubresource.arraySlice = 0;
758 copy_region.srcSubresource.mipLevel = 0;
759 copy_region.srcOffset.x = 0;
760 copy_region.srcOffset.y = 0;
761 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600762 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600763 copy_region.destSubresource.arraySlice = 0;
764 copy_region.destSubresource.mipLevel = 0;
765 copy_region.destOffset.x = 0;
766 copy_region.destOffset.y = 0;
767 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600768 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600769
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600770 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600771 src_image.obj(), src_image.layout(),
772 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600773 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600774
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600775 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600776
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600777 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600778
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600779 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600780 assert(!err);
781
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600782 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600783
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600784 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600785}
786
Tony Barbour6918cd52015-04-09 12:58:51 -0600787VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
788 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700789{
790 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600791 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600792 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600793 void *data;
794 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600795 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600796 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600797
Cody Northropbb6fdb32015-06-17 08:28:19 -0600798 stagingImage.init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600799 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600800
801 if (colors == NULL)
802 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700803
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800804 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700805
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600806 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600807 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600808 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600809 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600810 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600811 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600812 view.channels.r = VK_CHANNEL_SWIZZLE_R;
813 view.channels.g = VK_CHANNEL_SWIZZLE_G;
814 view.channels.b = VK_CHANNEL_SWIZZLE_B;
815 view.channels.a = VK_CHANNEL_SWIZZLE_A;
816 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600817 view.subresourceRange.baseMipLevel = 0;
818 view.subresourceRange.mipLevels = 1;
819 view.subresourceRange.baseArraySlice = 0;
820 view.subresourceRange.arraySize = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700821
Tony Barboure2c58df2014-11-25 13:18:32 -0700822 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600823 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700824
825 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800826 view.image = obj();
827 m_textureView.init(*m_device, view);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800828 m_descriptorInfo.imageView = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700829
Tony Barbour5dc515d2015-04-01 17:47:06 -0600830 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700831
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800832 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700833 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800834 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600835 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700836 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600837 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600838 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700839}
Tony Barbour82c39522014-12-04 14:33:33 -0700840
Tony Barbour6918cd52015-04-09 12:58:51 -0600841VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700842{
Tony Barboure2c58df2014-11-25 13:18:32 -0700843 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700844
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600845 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800846 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600847 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
848 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
849 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600850 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600851 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
852 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
853 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800854 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600855 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600856 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800857 samplerCreateInfo.minLod = 0.0;
858 samplerCreateInfo.maxLod = 0.0;
Tony Barbour26b17f82015-06-25 16:56:44 -0600859 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700860
Chia-I Wue9864b52014-12-28 16:32:24 +0800861 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700862}
Tony Barboure2c58df2014-11-25 13:18:32 -0700863
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700864/*
865 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
866 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600867VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700868{
869 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700870 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700871
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800872 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700873}
874
Tony Barbour6918cd52015-04-09 12:58:51 -0600875VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600876{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600877 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600878 if (m_commandBuffer) {
879 delete m_commandBuffer;
880 }
881}
882
Tony Barbour6918cd52015-04-09 12:58:51 -0600883VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700884{
Tony Barboure2c58df2014-11-25 13:18:32 -0700885 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800886 m_commandBuffer = 0;
887
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800888 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700889 m_numVertices = constantCount;
890 m_stride = constantSize;
891
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600892 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800893 const size_t allocationSize = constantCount * constantSize;
Cody Northrop7fb43862015-06-22 14:56:14 -0600894 init_as_src_and_dst(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700895
Chia-I Wua07fee62014-12-28 15:26:08 +0800896 void *pData = map();
897 memcpy(pData, data, allocationSize);
898 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700899
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800900 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600901 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600902 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800903 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600904 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800905 view_info.offset = 0;
906 view_info.range = allocationSize;
907 m_bufferView.init(*m_device, view_info);
908
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800909 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700910}
Tony Barbour82c39522014-12-04 14:33:33 -0700911
Tony Barbourd1c35722015-04-16 15:59:00 -0600912void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700913{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600914 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700915}
916
917
Tony Barbour6918cd52015-04-09 12:58:51 -0600918void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600919 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600920 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600921 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
922 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
923 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
924 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600925 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600926 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600927 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
928 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
929 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
930 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
931 VK_MEMORY_INPUT_SHADER_READ_BIT |
932 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
933 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
934 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700935{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600936 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700937
Tony Barbour38422802014-12-10 14:36:31 -0700938 if (!m_commandBuffer)
939 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600940 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700941
Tony Barbour6918cd52015-04-09 12:58:51 -0600942 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700943 }
944 else
945 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800946 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700947 }
948
Tony Barboure2c58df2014-11-25 13:18:32 -0700949 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600950 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600951 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600952 cmd_buf_info.pNext = NULL;
953 cmd_buf_info.flags = 0;
954
Jon Ashburnc4164b12014-12-31 17:10:47 -0700955 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600956 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700957
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600958 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000959 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600960 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700961
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600962 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
963 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000964
965 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600966 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700967
968 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700969 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600970 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700971
Tony Barboure2c58df2014-11-25 13:18:32 -0700972 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600973 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700974 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600975 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
976 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700977}
978
Tony Barbour6918cd52015-04-09 12:58:51 -0600979VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
980 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700981{
982
983}
984
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600985void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700986{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600987 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700988
989 m_numVertices = numIndexes;
990 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700991 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600992 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700993 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600994 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700995 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600996 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700997 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600998 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700999 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001000 default:
1001 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -06001002 m_stride = 2;
1003 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001004 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001005 }
1006
Chia-I Wua07fee62014-12-28 15:26:08 +08001007 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -06001008 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northrop7fb43862015-06-22 14:56:14 -06001009 init_as_src_and_dst(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001010
Chia-I Wua07fee62014-12-28 15:26:08 +08001011 void *pData = map();
1012 memcpy(pData, data, allocationSize);
1013 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001014
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001015 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001016 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001017 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001018 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -06001019 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001020 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001021 view_info.offset = 0;
1022 view_info.range = allocationSize;
1023 m_bufferView.init(*m_device, view_info);
1024
Chia-I Wu9d00ed72015-05-25 16:27:55 +08001025 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001026}
1027
Tony Barbourd1c35722015-04-16 15:59:00 -06001028void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001029{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001030 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001031}
Tony Barboure2c58df2014-11-25 13:18:32 -07001032
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001033VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001034{
1035 return m_indexType;
1036}
1037
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001038VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001039{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001040 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001041 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1042 stageInfo->stage = m_stage;
1043 stageInfo->shader = obj();
1044 stageInfo->linkConstBufferCount = 0;
1045 stageInfo->pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001046
Tony Barboure2c58df2014-11-25 13:18:32 -07001047 return stageInfo;
1048}
1049
Tony Barbourd1c35722015-04-16 15:59:00 -06001050VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001051{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001052 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001053 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001054 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001055 VkShaderModuleCreateInfo moduleCreateInfo;
1056 vk_testing::ShaderModule module;
Tony Barboure2c58df2014-11-25 13:18:32 -07001057 size_t shader_len;
1058
1059 m_stage = stage;
1060 m_device = device;
1061
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001062 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1063 moduleCreateInfo.pNext = NULL;
1064
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001065 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001066 createInfo.pNext = NULL;
1067
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001068 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001069
1070 shader_len = strlen(shader_code);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001071 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1072 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1073 moduleCreateInfo.flags = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001074
Tony Barbourd1c35722015-04-16 15:59:00 -06001075 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001076 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1077 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1078 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1079 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barboure2c58df2014-11-25 13:18:32 -07001080
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001081 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001082
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001083 // Use Reference GLSL to SPV compiler
1084 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001085 moduleCreateInfo.pCode = spv.data();
1086 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1087 moduleCreateInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001088 }
Cody Northrop475663c2015-04-15 11:19:06 -06001089
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001090 err = module.init_try(*m_device, moduleCreateInfo);
1091 assert(VK_SUCCESS == err);
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001092
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001093 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1094 createInfo.pNext = NULL;
1095 createInfo.module = module.obj();
1096 createInfo.pName = "main";
1097 createInfo.flags = 0;
1098
1099 err = init_try(*m_device, createInfo);
Cody Northrop475663c2015-04-15 11:19:06 -06001100 assert(VK_SUCCESS == err);
Tony Barbourf325bf12014-12-03 15:59:38 -07001101}
Tony Barbour82c39522014-12-04 14:33:33 -07001102
Tony Barbour6918cd52015-04-09 12:58:51 -06001103VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001104{
Tony Barboure2c58df2014-11-25 13:18:32 -07001105 m_device = device;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001106
1107 m_vi_state.pNext = VK_NULL_HANDLE;
1108 m_vi_state.bindingCount = 0;
1109 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1110 m_vi_state.attributeCount = 0;
1111 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1112
Tony Barboure2c58df2014-11-25 13:18:32 -07001113 m_vertexBufferCount = 0;
1114
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001115 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1116 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001117 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001118 m_ia_state.disableVertexReuse = VK_FALSE;
1119 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001120 m_ia_state.primitiveRestartIndex = 0;
1121
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001122 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001123 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001124 m_rs_state.depthClipEnable = VK_FALSE;
1125 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001126 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1127 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001128 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001129 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1130 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001131
Tony Barboure2c58df2014-11-25 13:18:32 -07001132 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001133 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001134 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001135 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1136 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001137
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001138 m_ms_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001139 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1140 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001141 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
Tony Barbourdfd533a2015-06-26 10:18:34 -06001142 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001143 m_ms_state.minSampleShading = 0;
1144 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001145
Tony Barbourd81b8882015-05-15 09:37:57 -06001146 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001147 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001148 m_vp_state.viewportCount = 1;
1149 m_vp_state.depthMode = VK_DEPTH_MODE_ZERO_TO_ONE;
1150 m_vp_state.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1151
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001152 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001153 m_ds_state.pNext = VK_NULL_HANDLE,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001154 m_ds_state.depthTestEnable = VK_FALSE;
1155 m_ds_state.depthWriteEnable = VK_FALSE;
1156 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001157 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001158 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1159 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1160 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001161 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001162 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001163 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001164};
1165
Tony Barbour6918cd52015-04-09 12:58:51 -06001166void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001167{
1168 m_shaderObjs.push_back(shader);
1169}
1170
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001171void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001172{
1173 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1174 m_vi_state.attributeCount = count;
1175}
1176
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001177void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001178{
1179 m_vi_state.pVertexBindingDescriptions = vi_binding;
1180 m_vi_state.bindingCount = count;
1181}
1182
Tony Barbour6918cd52015-04-09 12:58:51 -06001183void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001184{
1185 m_vertexBufferObjs.push_back(vertexDataBuffer);
1186 m_vertexBufferBindings.push_back(binding);
1187 m_vertexBufferCount++;
1188}
1189
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001190void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001191{
Tony Barbourf52346d2015-01-16 14:27:35 -07001192 if (binding+1 > m_colorAttachments.size())
1193 {
1194 m_colorAttachments.resize(binding+1);
1195 }
1196 m_colorAttachments[binding] = *att;
1197}
1198
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001199void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001200{
Tony Barbourf52346d2015-01-16 14:27:35 -07001201 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1202 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1203 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001204 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001205 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1206 m_ds_state.back = ds_state->back;
1207 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001208}
1209
Chia-I Wu08accc62015-07-07 11:50:03 +08001210VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet, VkRenderPass render_pass)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001211{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001212 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001213
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001214 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001215
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001216 info.stageCount = m_shaderObjs.size();
1217 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1218
Tony Barbour976e1cf2014-12-17 11:57:31 -07001219 for (int i=0; i<m_shaderObjs.size(); i++)
1220 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001221 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001222 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001223 }
1224
1225 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1226 {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001227 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001228 }
1229
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001230 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001231 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001232 info.flags = 0;
1233 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001234
Tony Barbourf52346d2015-01-16 14:27:35 -07001235 m_cb_state.attachmentCount = m_colorAttachments.size();
1236 m_cb_state.pAttachments = &m_colorAttachments[0];
1237
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001238 info.pTessState = NULL;
1239 info.pVertexInputState = &m_vi_state;
1240 info.pIaState = &m_ia_state;
1241 info.pVpState = &m_vp_state;
1242 info.pRsState = &m_rs_state;
1243 info.pMsState = &m_ms_state;
1244 info.pDsState = &m_ds_state;
1245 info.pCbState = &m_cb_state;
Chia-I Wu08accc62015-07-07 11:50:03 +08001246 info.renderPass = render_pass;
1247 info.subpass = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001248
Chris Forbes95292b12015-05-25 11:13:26 +12001249 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001250}
Chia-I Wu2648d092014-12-29 14:24:14 +08001251
Tony Barbourd1c35722015-04-16 15:59:00 -06001252vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001253{
Tony Barbourd1c35722015-04-16 15:59:00 -06001254 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001255 if (this->mem_refs_.size()) {
1256 mems.reserve(this->mem_refs_.size());
1257 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1258 mems.push_back(this->mem_refs_[i]);
1259 }
1260
1261 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001262}
1263
Tony Barbour6918cd52015-04-09 12:58:51 -06001264VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001265 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001266{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001267 m_device = device;
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 Wud28343c2014-12-28 15:12:48 +08001272 return obj();
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 Goeltzenleuchtercd2a0992015-07-09 11:44:38 -06001293void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, VkBool32 byRegion, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001294{
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001295 vkCmdPipelineBarrier(obj(), 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();
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001332 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&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
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001335 vkCmdClearColorImage(obj(),
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;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001354 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001355 memory_barrier.subresourceRange = dsRange;
1356
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001357 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001358
Chris Forbesd9be82b2015-06-22 17:21:59 +12001359 vkCmdClearDepthStencilImage(obj(),
1360 depthStencilObj->obj(), VK_IMAGE_LAYOUT_GENERAL,
1361 depth_clear_color, stencil_clear_color,
1362 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001363
1364 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001365 memory_barrier.image = depthStencilObj->obj();
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;
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001369 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001370 }
1371}
1372
Tony Barbour6918cd52015-04-09 12:58:51 -06001373void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001374{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001375 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001376 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001377 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001378 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1379 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1380 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001381 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001382 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001383 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001384 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1385 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1386 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1387 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1388 VK_MEMORY_INPUT_SHADER_READ_BIT |
1389 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1390 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001391 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001392
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001393 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001394 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001395 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001396 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001397 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001398 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001399
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001400 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001401 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001402 memory_barrier.outputMask = output_mask;
1403 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001404 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001405 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001406 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001407
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001408 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1409 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001410
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001411 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001412 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001413 memory_barrier.image = m_renderTargets[i]->image();
1414 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001415 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001416 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001417 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001418}
1419
Chia-I Wu08accc62015-07-07 11:50:03 +08001420void VkCommandBufferObj::BeginRenderPass(const VkRenderPassBeginInfo &info)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001421{
Chia-I Wu08accc62015-07-07 11:50:03 +08001422 vkCmdBeginRenderPass( obj(), &info, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001423}
1424
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001425void VkCommandBufferObj::EndRenderPass()
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001426{
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001427 vkCmdEndRenderPass(obj());
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001428}
1429
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001430void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001431{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001432 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001433}
1434
Tony Barbour6918cd52015-04-09 12:58:51 -06001435void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001436{
1437 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001438}
1439
Tony Barbour6918cd52015-04-09 12:58:51 -06001440void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001441{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001442 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001443}
1444
Tony Barbour6918cd52015-04-09 12:58:51 -06001445void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001446{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001447 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001448}
1449
Tony Barbour6918cd52015-04-09 12:58:51 -06001450void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001451{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001452 QueueCommandBuffer(NULL);
1453}
1454
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001455void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001456{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001457 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001458
1459 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001460 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1461 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001462
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001463 err = vkQueueWaitIdle( m_device->m_queue );
1464 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001465
1466 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001467 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001468}
1469
Tony Barbour6918cd52015-04-09 12:58:51 -06001470void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001471{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001472 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001473}
1474
Tony Barbour6918cd52015-04-09 12:58:51 -06001475void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001476{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001477 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001478
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001479 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Mark Lobodzinskif2093b62015-06-15 13:21:21 -06001480 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northropd4c1a502015-04-16 13:41:56 -06001481 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001482}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001483
Tony Barbour6918cd52015-04-09 12:58:51 -06001484void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001485{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001486 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001487}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001488
Tony Barbourd1c35722015-04-16 15:59:00 -06001489void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001490{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001491 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001492}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001493
Tony Barbour6918cd52015-04-09 12:58:51 -06001494VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001495{
1496 m_initialized = false;
1497}
Tony Barbour6918cd52015-04-09 12:58:51 -06001498bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001499{
1500 return m_initialized;
1501}
1502
Chia-I Wu08accc62015-07-07 11:50:03 +08001503VkAttachmentBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001504{
Chia-I Wu08accc62015-07-07 11:50:03 +08001505 return &m_attachmentBindInfo;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001506}
1507
Chia-I Wu08accc62015-07-07 11:50:03 +08001508void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height, VkFormat format)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001509{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001510 VkImageCreateInfo image_info;
Chia-I Wu08accc62015-07-07 11:50:03 +08001511 VkAttachmentViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001512
1513 m_device = device;
1514 m_initialized = true;
Chia-I Wu08accc62015-07-07 11:50:03 +08001515 m_depth_stencil_fmt = format;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001516
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001517 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001518 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001519 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001520 image_info.format = m_depth_stencil_fmt;
1521 image_info.extent.width = width;
1522 image_info.extent.height = height;
1523 image_info.extent.depth = 1;
1524 image_info.mipLevels = 1;
1525 image_info.arraySize = 1;
1526 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001527 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001528 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001529 image_info.flags = 0;
1530 init(*m_device, image_info);
1531
Chia-I Wu08accc62015-07-07 11:50:03 +08001532 view_info.sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001533 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001534 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001535 view_info.mipLevel = 0;
1536 view_info.baseArraySlice = 0;
1537 view_info.arraySize = 1;
1538 view_info.flags = 0;
1539 view_info.image = obj();
Chia-I Wu08accc62015-07-07 11:50:03 +08001540 m_attachmentView.init(*m_device, view_info);
Tony Barbour1c45ce02015-03-27 17:03:18 -06001541
Chia-I Wu08accc62015-07-07 11:50:03 +08001542 m_attachmentBindInfo.view = m_attachmentView.obj();
1543 m_attachmentBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001544}