blob: 49a2bc3cace607cb7a0515b9f5355569bc9ea2a9 [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 ),
Tony Barbour71bd4b32015-07-21 17:00:26 -060044 m_clear_via_load_op( true ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070045 m_depth_clear_color( 1.0 ),
Courtney Goeltzenleuchterb6c9aca2015-06-08 16:19:37 -060046 m_stencil_clear_color( 0 ),
47 m_depthStencil( NULL ),
48 m_dbgCreateMsgCallback( VK_NULL_HANDLE ),
49 m_dbgDestroyMsgCallback( VK_NULL_HANDLE ),
50 m_globalMsgCallback( VK_NULL_HANDLE ),
51 m_devMsgCallback( VK_NULL_HANDLE )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060052{
Tony Barbour1c45ce02015-03-27 17:03:18 -060053
Chia-I Wu08accc62015-07-07 11:50:03 +080054 memset(&m_renderPassBeginInfo, 0, sizeof(m_renderPassBeginInfo));
55 m_renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
56
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070057 // clear the back buffer to dark grey
Chris Forbesf0796e12015-06-24 14:34:53 +120058 m_clear_color.f32[0] = 0.25f;
59 m_clear_color.f32[1] = 0.25f;
60 m_clear_color.f32[2] = 0.25f;
61 m_clear_color.f32[3] = 0.0f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060062}
63
Tony Barbour6918cd52015-04-09 12:58:51 -060064VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060065{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060066
67}
68
Tony Barbour6918cd52015-04-09 12:58:51 -060069void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060070{
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060071 std::vector<const char*> instance_layer_names;
72 std::vector<const char*> device_layer_names;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060073 std::vector<const char*> instance_extension_names;
74 std::vector<const char*> device_extension_names;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060075 InitFramework(
76 instance_layer_names, device_layer_names,
77 instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060078}
79
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060080void VkRenderFramework::InitFramework(
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060081 std::vector<const char *> instance_layer_names,
82 std::vector<const char *> device_layer_names,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060083 std::vector<const char *> instance_extension_names,
84 std::vector<const char *> device_extension_names,
85 PFN_vkDbgMsgCallback dbgFunction,
86 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060087{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060088 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060089 std::vector<VkExtensionProperties> instance_extensions;
90 std::vector<VkExtensionProperties> device_extensions;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060091 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060092
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -060093 /* TODO: Verify requested extensions are available */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060094
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060095 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060096 instInfo.pNext = NULL;
97 instInfo.pAppInfo = &app_info;
98 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060099 instInfo.layerCount = instance_layer_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600100 instInfo.ppEnabledLayerNames = instance_layer_names.data();
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600101 instInfo.extensionCount = instance_extension_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600102 instInfo.ppEnabledExtensionNames = instance_extension_names.data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600103 err = vkCreateInstance(&instInfo, &this->inst);
104 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600105
Jon Ashburn83a64252015-04-15 11:31:12 -0600106 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
107 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
108 ASSERT_VK_SUCCESS(err);
109 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600110 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700111 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600112 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600113 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
114 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
115 if (m_dbgCreateMsgCallback) {
116 err = m_dbgCreateMsgCallback(this->inst,
117 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
118 dbgFunction,
119 userData,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600120 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600121 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600122
123 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
124 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600125 }
Tony Barbour15524c32015-04-29 17:34:29 -0600126 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600127
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600128 /* TODO: Verify requested physical device extensions are available */
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600129 m_device = new VkDeviceObj(0, objs[0], device_layer_names, device_extension_names);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600130
131 /* Now register callback on device */
132 if (0) {
133 if (m_dbgCreateMsgCallback) {
134 err = m_dbgCreateMsgCallback(this->inst,
135 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
136 dbgFunction,
137 userData,
138 &m_devMsgCallback);
139 ASSERT_VK_SUCCESS(err);
140 }
141 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600142 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600143
Tony Barbour6918cd52015-04-09 12:58:51 -0600144 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600145}
146
Tony Barbour6918cd52015-04-09 12:58:51 -0600147void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600148{
Tony Barbour67e99152015-07-10 14:10:27 -0600149 if (m_colorBlend) vkDestroyDynamicColorBlendState(device(), m_colorBlend);
150 if (m_stateDepthStencil) vkDestroyDynamicDepthStencilState(device(), m_stateDepthStencil);
151 if (m_stateRaster) vkDestroyDynamicRasterState(device(), m_stateRaster);
152 if (m_cmdBuffer) vkDestroyCommandBuffer(device(), m_cmdBuffer);
Cody Northrope62183e2015-07-09 18:08:05 -0600153 if (m_cmdPool) vkDestroyCommandPool(device(), m_cmdPool);
Tony Barbour67e99152015-07-10 14:10:27 -0600154 if (m_framebuffer) vkDestroyFramebuffer(device(), m_framebuffer);
155 if (m_renderPass) vkDestroyRenderPass(device(), m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600156
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600157 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
158 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
159
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600160 if (m_stateViewport) {
Tony Barbour67e99152015-07-10 14:10:27 -0600161 vkDestroyDynamicViewportState(device(), m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600162 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600163 while (!m_renderTargets.empty()) {
Tony Barbour67e99152015-07-10 14:10:27 -0600164 vkDestroyAttachmentView(device(), m_renderTargets.back()->targetView());
165 vkDestroyImage(device(), m_renderTargets.back()->image());
Mike Stroyanb050c682015-04-17 12:36:38 -0600166 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600167 m_renderTargets.pop_back();
168 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600169
Tony Barbour1c45ce02015-03-27 17:03:18 -0600170 delete m_depthStencil;
Tony Barbour823e6ca2015-07-27 13:31:04 -0600171 while (!m_shader_modules.empty())
172 {
173 delete m_shader_modules.back();
174 m_shader_modules.pop_back();
175 }
Tony Barbour1c45ce02015-03-27 17:03:18 -0600176
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600177 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800178 delete m_device;
Chris Forbesbe2fae62015-07-07 11:02:22 +1200179 if (this->inst) vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600180}
181
Tony Barbour6918cd52015-04-09 12:58:51 -0600182void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600183{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600184 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600185
Tony Barbourd1c35722015-04-16 15:59:00 -0600186 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600187
188 // create a raster state (solid, back-face culling)
Tony Barbour67e99152015-07-10 14:10:27 -0600189 VkDynamicRasterStateCreateInfo raster = {};
190 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700191
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600192 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
193 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600194
Tony Barbour67e99152015-07-10 14:10:27 -0600195 VkDynamicColorBlendStateCreateInfo blend = {};
196 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600197 blend.blendConst[0] = 1.0f;
198 blend.blendConst[1] = 1.0f;
199 blend.blendConst[2] = 1.0f;
200 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600201 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
202 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600203
Tony Barbour67e99152015-07-10 14:10:27 -0600204 VkDynamicDepthStencilStateCreateInfo depthStencil = {};
205 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski365feea2015-06-12 11:14:17 -0600206 depthStencil.minDepthBounds = 0.f;
207 depthStencil.maxDepthBounds = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700208 depthStencil.stencilFrontRef = 0;
209 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600210 depthStencil.stencilReadMask = 0xff;
211 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600212
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600213 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
214 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600215
Cody Northrope62183e2015-07-09 18:08:05 -0600216 VkCmdPoolCreateInfo cmd_pool_info;
217 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
218 cmd_pool_info.pNext = NULL,
219 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
220 cmd_pool_info.flags = 0,
221 err = vkCreateCommandPool(device(), &cmd_pool_info, &m_cmdPool);
222 assert(!err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600223
Cody Northrope62183e2015-07-09 18:08:05 -0600224 VkCmdBufferCreateInfo cmdInfo = vk_testing::CmdBuffer::create_info(m_cmdPool);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700225
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600226 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
227 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600228}
229
Tony Barbour6918cd52015-04-09 12:58:51 -0600230void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600231{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600232 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600233
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600234 VkViewport viewport;
Chris Forbesd9be82b2015-06-22 17:21:59 +1200235 VkRect2D scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600236
Tony Barbour67e99152015-07-10 14:10:27 -0600237 VkDynamicViewportStateCreateInfo viewportCreate = {};
238 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700239 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700240 viewport.originX = 0;
241 viewport.originY = 0;
242 viewport.width = 1.f * width;
243 viewport.height = 1.f * height;
244 viewport.minDepth = 0.f;
245 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600246 scissor.extent.width = (int32_t) width;
247 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700248 scissor.offset.x = 0;
249 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700250 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700251 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700252
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600253 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
254 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600255 m_width = width;
256 m_height = height;
257}
258
Tony Barbour6918cd52015-04-09 12:58:51 -0600259void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600260{
261 InitViewport(m_width, m_height);
262}
Tony Barbour6918cd52015-04-09 12:58:51 -0600263void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600264{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700265 InitRenderTarget(1);
266}
267
Tony Barbour6918cd52015-04-09 12:58:51 -0600268void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700269{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600270 InitRenderTarget(targets, NULL);
271}
272
Chia-I Wu08accc62015-07-07 11:50:03 +0800273void VkRenderFramework::InitRenderTarget(VkAttachmentBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600274{
275 InitRenderTarget(1, dsBinding);
276}
277
Chia-I Wu08accc62015-07-07 11:50:03 +0800278void VkRenderFramework::InitRenderTarget(uint32_t targets, VkAttachmentBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600279{
Chia-I Wu08accc62015-07-07 11:50:03 +0800280 std::vector<VkAttachmentDescription> attachments;
281 std::vector<VkAttachmentReference> color_references;
282 std::vector<VkAttachmentBindInfo> bindings;
283 attachments.reserve(targets + (bool) dsBinding);
284 color_references.reserve(targets);
285 bindings.reserve(targets + (bool) dsBinding);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600286
Chia-I Wu08accc62015-07-07 11:50:03 +0800287 VkAttachmentDescription att = {};
288 att.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION;
289 att.pNext = NULL;
290 att.format = m_render_target_fmt;
291 att.samples = 1;
292 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
293 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
294 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
295 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
296 att.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
297 att.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chia-I Wuecebf752014-12-05 10:45:15 +0800298
Chia-I Wu08accc62015-07-07 11:50:03 +0800299 VkAttachmentReference ref = {};
300 ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
301
302 m_renderPassClearValues.clear();
303 VkClearValue clear = {};
304 clear.color = m_clear_color;
305
306 VkAttachmentBindInfo bind = {};
307 bind.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
308
309 for (uint32_t i = 0; i < targets; i++) {
310 attachments.push_back(att);
311
312 ref.attachment = i;
313 color_references.push_back(ref);
314
315 m_renderPassClearValues.push_back(clear);
316
Tony Barbour6918cd52015-04-09 12:58:51 -0600317 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600318
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600319 VkFormatProperties props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600320 VkResult err;
321
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600322 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), m_render_target_fmt, &props);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600323 ASSERT_VK_SUCCESS(err);
324
325 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600326 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600327 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
328 VK_IMAGE_TILING_LINEAR);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600329 }
330 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600331 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600332 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
333 VK_IMAGE_TILING_OPTIMAL);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600334 }
335 else {
336 FAIL() << "Neither Linear nor Optimal allowed for render target";
337 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600338
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600339 m_renderTargets.push_back(img);
Chia-I Wu08accc62015-07-07 11:50:03 +0800340 bind.view = img->targetView();
341
342 bindings.push_back(bind);
Chia-I Wuecebf752014-12-05 10:45:15 +0800343 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600344
Chia-I Wu08accc62015-07-07 11:50:03 +0800345 VkSubpassDescription subpass = {};
346 subpass.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION;
347 subpass.pNext = NULL;
348 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
349 subpass.flags = 0;
350 subpass.inputCount = 0;
351 subpass.inputAttachments = NULL;
352 subpass.colorCount = targets;
Tony Barbour482c6092015-07-27 09:37:48 -0600353 subpass.colorAttachments = color_references.data();
Chia-I Wu08accc62015-07-07 11:50:03 +0800354 subpass.resolveAttachments = NULL;
355
356 if (dsBinding) {
357 att.format = m_depth_stencil_fmt;
Tony Barbour71bd4b32015-07-21 17:00:26 -0600358 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;;
Chia-I Wu08accc62015-07-07 11:50:03 +0800359 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
360 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
361 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
362 att.initialLayout = dsBinding->layout;
363 att.finalLayout = dsBinding->layout;
364 attachments.push_back(att);
365
366 clear.ds.depth = m_depth_clear_color;
367 clear.ds.stencil = m_stencil_clear_color;
368 m_renderPassClearValues.push_back(clear);
369
370 bindings.push_back(*dsBinding);
371
372 subpass.depthStencilAttachment.attachment = targets;
373 subpass.depthStencilAttachment.layout = dsBinding->layout;
374 } else {
375 subpass.depthStencilAttachment.attachment = VK_ATTACHMENT_UNUSED;
376 }
377
378 subpass.preserveCount = 0;
379 subpass.preserveAttachments = NULL;
380
381 VkRenderPassCreateInfo rp_info = {};
382 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
383 rp_info.attachmentCount = attachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600384 rp_info.pAttachments = attachments.data();
Chia-I Wu08accc62015-07-07 11:50:03 +0800385 rp_info.subpassCount = 1;
386 rp_info.pSubpasses = &subpass;
387
388 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
389
390 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600391 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600392 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600393 fb_info.pNext = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800394 fb_info.renderPass = m_renderPass;
395 fb_info.attachmentCount = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600396 fb_info.pAttachments = bindings.data();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600397 fb_info.width = (uint32_t)m_width;
398 fb_info.height = (uint32_t)m_height;
399 fb_info.layers = 1;
400
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600401 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600402
Chia-I Wu08accc62015-07-07 11:50:03 +0800403 m_renderPassBeginInfo.renderPass = m_renderPass;
404 m_renderPassBeginInfo.framebuffer = m_framebuffer;
405 m_renderPassBeginInfo.renderArea.extent.width = m_width;
406 m_renderPassBeginInfo.renderArea.extent.height = m_height;
407 m_renderPassBeginInfo.attachmentCount = m_renderPassClearValues.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600408 m_renderPassBeginInfo.pAttachmentClearValues = m_renderPassClearValues.data();
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600409}
410
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600411
412
Tony Barbourd1c35722015-04-16 15:59:00 -0600413VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600414 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800415{
416 init();
417
Chia-I Wu999f0482015-07-03 10:32:05 +0800418 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600419 queue_props = phy().queue_properties().data();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800420}
421
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600422VkDeviceObj::VkDeviceObj(uint32_t id,
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600423 VkPhysicalDevice obj, std::vector<const char *> &layer_names,
424 std::vector<const char *> &extension_names) :
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600425 vk_testing::Device(obj), id(id)
426{
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600427 init(layer_names, extension_names);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600428
Chia-I Wu999f0482015-07-03 10:32:05 +0800429 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600430 queue_props = phy().queue_properties().data();
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600431}
432
Tony Barbour6918cd52015-04-09 12:58:51 -0600433void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800434{
435 ASSERT_NE(true, graphics_queues().empty());
Chia-I Wudf12ffd2015-07-03 10:53:18 +0800436 m_queue = graphics_queues()[0]->handle();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800437}
438
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600439VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
440 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700441{
Tony Barboure2c58df2014-11-25 13:18:32 -0700442
443}
444
Tony Barbour6918cd52015-04-09 12:58:51 -0600445VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700446{
Chia-I Wu11078b02015-01-04 16:27:24 +0800447 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700448}
Tony Barbour82c39522014-12-04 14:33:33 -0700449
Tony Barbour6918cd52015-04-09 12:58:51 -0600450int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700451{
Chia-I Wu11078b02015-01-04 16:27:24 +0800452 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600453 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600454 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800455 tc.count = 1;
456 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700457
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
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600461int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700462{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600463 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800464 tc.type = type;
465 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 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
469 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600470
Chia-I Wu11078b02015-01-04 16:27:24 +0800471 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700472}
Tony Barbour82c39522014-12-04 14:33:33 -0700473
Tony Barbour6918cd52015-04-09 12:58:51 -0600474int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700475{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600476 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600477 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800478 tc.count = 1;
479 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700480
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800481 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Chia-I Wu8c721c62015-07-03 11:49:42 +0800482 tmp.sampler = sampler->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800483 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700484
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800485 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
486 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700487
Chia-I Wu11078b02015-01-04 16:27:24 +0800488 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700489}
Chia-I Wu11078b02015-01-04 16:27:24 +0800490
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500491VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700492{
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800493 return m_pipeline_layout.handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700494}
495
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600496VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700497{
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800498 return m_set->handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700499}
Tony Barboure2c58df2014-11-25 13:18:32 -0700500
Tony Barbour6918cd52015-04-09 12:58:51 -0600501void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700502{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600503 // create VkDescriptorPool
504 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600505 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800506 pool.count = m_type_counts.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600507 pool.pTypeCount = m_type_counts.data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600508 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700509
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600510 // create VkDescriptorSetLayout
511 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800512 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800513 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800514 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800515 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600516 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800517 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700518 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800519
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600520 // create VkDescriptorSetLayout
521 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600522 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800523 layout.count = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600524 layout.pBinding = bindings.data();
Chia-I Wub41393e2015-03-26 15:04:41 +0800525
Chia-I Wu41126e52015-03-26 15:27:55 +0800526 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600527 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800528 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500529
530 // create VkPipelineLayout
531 VkPipelineLayoutCreateInfo pipeline_layout = {};
532 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
533 pipeline_layout.descriptorSetCount = layouts.size();
534 pipeline_layout.pSetLayouts = NULL;
535
536 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700537
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600538 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500539 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800540
Chia-I Wu41126e52015-03-26 15:27:55 +0800541 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800542 size_t imageSamplerCount = 0;
543 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
544 it != m_writes.end(); it++) {
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800545 it->destSet = m_set->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800546 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
547 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800548 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800549
550 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800551 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700552}
Tony Barboure2c58df2014-11-25 13:18:32 -0700553
Tony Barbour6918cd52015-04-09 12:58:51 -0600554VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800555{
556 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800557 m_descriptorInfo.imageView = VK_NULL_HANDLE;
558 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800559}
560
Tony Barbour6918cd52015-04-09 12:58:51 -0600561void VkImageObj::ImageMemoryBarrier(
562 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600563 VkImageAspect aspect,
564 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600565 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600566 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
567 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
568 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
569 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600570 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600571 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600572 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
573 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
574 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
575 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
576 VK_MEMORY_INPUT_SHADER_READ_BIT |
577 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
578 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
579 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600580 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600581{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600582 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
583 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600584 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
585 subresourceRange);
586
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600587 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600588
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600589 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
590 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600591
592 // write barrier to the command buffer
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -0600593 vkCmdPipelineBarrier(cmd_buf->handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600594}
595
Tony Barbour6918cd52015-04-09 12:58:51 -0600596void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600597 VkImageAspect aspect,
598 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600599{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600600 VkFlags output_mask, input_mask;
601 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600602 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600603 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
604 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
605 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600606 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600607 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600608 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600609 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
610 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
611 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
612 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
613 VK_MEMORY_INPUT_SHADER_READ_BIT |
614 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
615 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600616 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600617
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800618 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600619 return;
620 }
621
622 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600623 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600624 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
625 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600626 break;
627
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600628 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600629 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
630 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600631 break;
632
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600633 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600634 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
635 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600636 break;
637
638 default:
639 output_mask = all_cache_outputs;
640 input_mask = all_cache_inputs;
641 break;
642 }
643
644 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800645 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600646}
647
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600648void VkImageObj::SetLayout(VkImageAspect aspect,
649 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600650{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600651 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600652
653 if (image_layout == m_descriptorInfo.imageLayout) {
654 return;
655 }
656
Tony Barbour6918cd52015-04-09 12:58:51 -0600657 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600658
659 /* Build command buffer to set image layout in the driver */
660 err = cmd_buf.BeginCommandBuffer();
661 assert(!err);
662
663 SetLayout(&cmd_buf, aspect, image_layout);
664
665 err = cmd_buf.EndCommandBuffer();
666 assert(!err);
667
668 cmd_buf.QueueCommandBuffer();
669}
670
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600671bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600672{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600673 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600674 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600675 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600676
Tony Barbour579f7802015-04-03 15:11:43 -0600677 return true;
678}
679
Tony Barbour6918cd52015-04-09 12:58:51 -0600680void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600681 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600682 VkImageTiling requested_tiling,
683 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800684{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600685 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600686 VkFormatProperties image_fmt;
687 VkImageTiling tiling;
688 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600689
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800690 mipCount = 0;
691
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600692 uint32_t _w = w;
693 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800694 while( ( _w > 0 ) || ( _h > 0 ) )
695 {
696 _w >>= 1;
697 _h >>= 1;
698 mipCount++;
699 }
700
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600701 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), fmt, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600702 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600703
Tony Barbourd1c35722015-04-16 15:59:00 -0600704 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600705 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600706 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600707 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600708 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600709 } else {
710 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
711 }
712 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600713 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600714 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600715 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600716 } else {
717 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
718 }
719
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600720 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600721 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800722 imageCreateInfo.format = fmt;
723 imageCreateInfo.extent.width = w;
724 imageCreateInfo.extent.height = h;
725 imageCreateInfo.mipLevels = mipCount;
726 imageCreateInfo.tiling = tiling;
727
728 imageCreateInfo.usage = usage;
729
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600730 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800731
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600732 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600733 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600734 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600735 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600736 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800737}
738
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600739VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600740{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600741 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600742 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600743 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600744
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600745 /* Build command buffer to copy staging texture to usable texture */
746 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600747 assert(!err);
748
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600749 /* TODO: Can we determine image aspect from image object? */
750 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600751 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600752
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600753 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600754 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600755
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600756 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600757 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600758 copy_region.srcSubresource.arraySlice = 0;
759 copy_region.srcSubresource.mipLevel = 0;
760 copy_region.srcOffset.x = 0;
761 copy_region.srcOffset.y = 0;
762 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600763 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600764 copy_region.destSubresource.arraySlice = 0;
765 copy_region.destSubresource.mipLevel = 0;
766 copy_region.destOffset.x = 0;
767 copy_region.destOffset.y = 0;
768 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600769 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600770
Chia-I Wube2b9172015-07-03 11:49:42 +0800771 vkCmdCopyImage(cmd_buf.handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +0800772 src_image.handle(), src_image.layout(),
773 handle(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600774 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600775
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600776 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600777
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600778 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600779
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600780 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600781 assert(!err);
782
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600783 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600784
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600785 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600786}
787
Tony Barbour6918cd52015-04-09 12:58:51 -0600788VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
789 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700790{
791 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600792 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600793 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600794 void *data;
795 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600796 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600797 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600798
Tony Barboure65788f2015-07-21 17:01:42 -0600799 stagingImage.init(16, 16, tex_format, VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600800 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600801
802 if (colors == NULL)
803 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700804
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800805 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700806
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600807 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600808 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600809 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600810 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600811 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600812 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600813 view.channels.r = VK_CHANNEL_SWIZZLE_R;
814 view.channels.g = VK_CHANNEL_SWIZZLE_G;
815 view.channels.b = VK_CHANNEL_SWIZZLE_B;
816 view.channels.a = VK_CHANNEL_SWIZZLE_A;
817 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600818 view.subresourceRange.baseMipLevel = 0;
819 view.subresourceRange.mipLevels = 1;
820 view.subresourceRange.baseArraySlice = 0;
821 view.subresourceRange.arraySize = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700822
Tony Barboure2c58df2014-11-25 13:18:32 -0700823 /* create image */
Tony Barboure65788f2015-07-21 17:01:42 -0600824 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700825
826 /* create image view */
Chia-I Wu681d7a02015-07-03 13:44:34 +0800827 view.image = handle();
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800828 m_textureView.init(*m_device, view);
Chia-I Wu3158bf32015-07-03 11:49:42 +0800829 m_descriptorInfo.imageView = m_textureView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700830
Chia-I Wu681d7a02015-07-03 13:44:34 +0800831 data = stagingImage.MapMemory();
Tony Barboure2c58df2014-11-25 13:18:32 -0700832
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800833 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700834 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800835 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600836 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700837 }
Chia-I Wu681d7a02015-07-03 13:44:34 +0800838 stagingImage.UnmapMemory();
Tony Barbour6918cd52015-04-09 12:58:51 -0600839 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700840}
Tony Barbour82c39522014-12-04 14:33:33 -0700841
Tony Barbour6918cd52015-04-09 12:58:51 -0600842VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700843{
Tony Barboure2c58df2014-11-25 13:18:32 -0700844 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700845
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600846 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800847 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600848 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
849 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
850 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600851 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600852 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
853 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
854 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800855 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600856 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600857 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800858 samplerCreateInfo.minLod = 0.0;
859 samplerCreateInfo.maxLod = 0.0;
Tony Barbour26b17f82015-06-25 16:56:44 -0600860 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700861
Chia-I Wue9864b52014-12-28 16:32:24 +0800862 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700863}
Tony Barboure2c58df2014-11-25 13:18:32 -0700864
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700865/*
866 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
867 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600868VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700869{
870 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700871 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700872
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800873 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700874}
875
Tony Barbour6918cd52015-04-09 12:58:51 -0600876VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600877{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600878 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600879 if (m_commandBuffer) {
880 delete m_commandBuffer;
881 }
882}
883
Tony Barbour6918cd52015-04-09 12:58:51 -0600884VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700885{
Tony Barboure2c58df2014-11-25 13:18:32 -0700886 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800887 m_commandBuffer = 0;
888
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800889 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700890 m_numVertices = constantCount;
891 m_stride = constantSize;
892
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600893 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800894 const size_t allocationSize = constantCount * constantSize;
Cody Northrop7fb43862015-06-22 14:56:14 -0600895 init_as_src_and_dst(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700896
Chia-I Wu681d7a02015-07-03 13:44:34 +0800897 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +0800898 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800899 memory().unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700900
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800901 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600902 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600903 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu681d7a02015-07-03 13:44:34 +0800904 view_info.buffer = handle();
Tony Barbourd1c35722015-04-16 15:59:00 -0600905 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800906 view_info.offset = 0;
907 view_info.range = allocationSize;
908 m_bufferView.init(*m_device, view_info);
909
Chia-I Wu3158bf32015-07-03 11:49:42 +0800910 this->m_descriptorInfo.bufferView = m_bufferView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700911}
Tony Barbour82c39522014-12-04 14:33:33 -0700912
Tony Barbourd1c35722015-04-16 15:59:00 -0600913void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700914{
Chia-I Wu681d7a02015-07-03 13:44:34 +0800915 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &handle(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700916}
917
918
Tony Barbour6918cd52015-04-09 12:58:51 -0600919void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600920 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600921 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600922 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
923 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
924 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
925 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600926 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600927 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600928 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
929 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
930 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
931 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
932 VK_MEMORY_INPUT_SHADER_READ_BIT |
933 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
934 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
935 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700936{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600937 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700938
Tony Barbour38422802014-12-10 14:36:31 -0700939 if (!m_commandBuffer)
940 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600941 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700942
Tony Barbour6918cd52015-04-09 12:58:51 -0600943 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700944 }
945 else
946 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800947 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700948 }
949
Tony Barboure2c58df2014-11-25 13:18:32 -0700950 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600951 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600952 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600953 cmd_buf_info.pNext = NULL;
954 cmd_buf_info.flags = 0;
955
Jon Ashburnc4164b12014-12-31 17:10:47 -0700956 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600957 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700958
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600959 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000960 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600961 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700962
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600963 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
964 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000965
966 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600967 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700968
969 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700970 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600971 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700972
Tony Barboure2c58df2014-11-25 13:18:32 -0700973 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600974 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700975 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wud9e8e822015-07-03 11:45:55 +0800976 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.handle() );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600977 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700978}
979
Tony Barbour6918cd52015-04-09 12:58:51 -0600980VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
981 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700982{
983
984}
985
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600986void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700987{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600988 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700989
990 m_numVertices = numIndexes;
991 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700992 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600993 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700994 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600995 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700996 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600997 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700998 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600999 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001000 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001001 default:
1002 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -06001003 m_stride = 2;
1004 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001005 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001006 }
1007
Chia-I Wua07fee62014-12-28 15:26:08 +08001008 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -06001009 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northrop7fb43862015-06-22 14:56:14 -06001010 init_as_src_and_dst(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001011
Chia-I Wu681d7a02015-07-03 13:44:34 +08001012 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +08001013 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +08001014 memory().unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001015
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001016 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001017 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001018 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001019 view_info.buffer = handle();
Tony Barbourd1c35722015-04-16 15:59:00 -06001020 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001021 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001022 view_info.offset = 0;
1023 view_info.range = allocationSize;
1024 m_bufferView.init(*m_device, view_info);
1025
Chia-I Wu3158bf32015-07-03 11:49:42 +08001026 this->m_descriptorInfo.bufferView = m_bufferView.handle();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001027}
1028
Tony Barbourd1c35722015-04-16 15:59:00 -06001029void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001030{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001031 vkCmdBindIndexBuffer(cmdBuffer, handle(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001032}
Tony Barboure2c58df2014-11-25 13:18:32 -07001033
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001034VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001035{
1036 return m_indexType;
1037}
1038
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001039VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001040{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001041 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001042 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1043 stageInfo->stage = m_stage;
Chia-I Wu4d0c7922015-07-03 11:49:42 +08001044 stageInfo->shader = handle();
Tony Barboure2c58df2014-11-25 13:18:32 -07001045
Tony Barboure2c58df2014-11-25 13:18:32 -07001046 return stageInfo;
1047}
1048
Tony Barbourd1c35722015-04-16 15:59:00 -06001049VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001050{
Tony Barbourb5d2c942015-07-14 13:34:05 -06001051 VkResult U_ASSERT_ONLY err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001052 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001053 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001054 VkShaderModuleCreateInfo moduleCreateInfo;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001055 vk_testing::ShaderModule *module = new vk_testing::ShaderModule();
Tony Barboure2c58df2014-11-25 13:18:32 -07001056 size_t shader_len;
1057
1058 m_stage = stage;
1059 m_device = device;
1060
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001061 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1062 moduleCreateInfo.pNext = NULL;
1063
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001064 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001065 createInfo.pNext = NULL;
1066
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001067 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001068
1069 shader_len = strlen(shader_code);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001070 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1071 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1072 moduleCreateInfo.flags = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001073
Tony Barbourd1c35722015-04-16 15:59:00 -06001074 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001075 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1076 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1077 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1078 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barboure2c58df2014-11-25 13:18:32 -07001079
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001080 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001081
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001082 // Use Reference GLSL to SPV compiler
1083 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001084 moduleCreateInfo.pCode = spv.data();
1085 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1086 moduleCreateInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001087 }
Cody Northrop475663c2015-04-15 11:19:06 -06001088
Tony Barbour823e6ca2015-07-27 13:31:04 -06001089 err = module->init_try(*m_device, moduleCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001090 assert(VK_SUCCESS == err);
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001091
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001092 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1093 createInfo.pNext = NULL;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001094 createInfo.module = module->handle();
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001095 createInfo.pName = "main";
1096 createInfo.flags = 0;
1097
1098 err = init_try(*m_device, createInfo);
Cody Northrop475663c2015-04-15 11:19:06 -06001099 assert(VK_SUCCESS == err);
Tony Barbour823e6ca2015-07-27 13:31:04 -06001100 framework->m_shader_modules.push_back(module);
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
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001115 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001116 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.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001119
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001120 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001121 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001122 m_rs_state.depthClipEnable = VK_FALSE;
1123 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001124 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001125 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1126 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001127
Tony Barboure2c58df2014-11-25 13:18:32 -07001128 memset(&m_cb_state,0,sizeof(m_cb_state));
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001129 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001130 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001131 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1132 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001133
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001134 m_ms_state.pNext = VK_NULL_HANDLE;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001135 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -07001136 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
Tony Barbourdfd533a2015-06-26 10:18:34 -06001137 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001138 m_ms_state.minSampleShading = 0;
1139 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001140
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001141 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001142 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001143 m_vp_state.viewportCount = 1;
Tony Barbourd81b8882015-05-15 09:37:57 -06001144
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001145 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001146 m_ds_state.pNext = VK_NULL_HANDLE,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001147 m_ds_state.depthTestEnable = VK_FALSE;
1148 m_ds_state.depthWriteEnable = VK_FALSE;
1149 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001150 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001151 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1152 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1153 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001154 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001155 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001156 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001157};
1158
Tony Barbour6918cd52015-04-09 12:58:51 -06001159void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001160{
1161 m_shaderObjs.push_back(shader);
1162}
1163
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001164void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001165{
1166 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1167 m_vi_state.attributeCount = count;
1168}
1169
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001170void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001171{
1172 m_vi_state.pVertexBindingDescriptions = vi_binding;
1173 m_vi_state.bindingCount = count;
1174}
1175
Tony Barbour6918cd52015-04-09 12:58:51 -06001176void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001177{
1178 m_vertexBufferObjs.push_back(vertexDataBuffer);
1179 m_vertexBufferBindings.push_back(binding);
1180 m_vertexBufferCount++;
1181}
1182
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001183void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineColorBlendAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001184{
Tony Barbourf52346d2015-01-16 14:27:35 -07001185 if (binding+1 > m_colorAttachments.size())
1186 {
1187 m_colorAttachments.resize(binding+1);
1188 }
1189 m_colorAttachments[binding] = *att;
1190}
1191
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001192void VkPipelineObj::SetDepthStencil(VkPipelineDepthStencilStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001193{
Tony Barbourf52346d2015-01-16 14:27:35 -07001194 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1195 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1196 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001197 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001198 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1199 m_ds_state.back = ds_state->back;
1200 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001201}
1202
Chia-I Wu08accc62015-07-07 11:50:03 +08001203VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet, VkRenderPass render_pass)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001204{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001205 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001206
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001207 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001208
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001209 info.stageCount = m_shaderObjs.size();
1210 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1211
Tony Barbour976e1cf2014-12-17 11:57:31 -07001212 for (int i=0; i<m_shaderObjs.size(); i++)
1213 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001214 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001215 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001216 }
1217
Tony Barboure815fd72015-07-27 09:36:24 -06001218 if (m_vi_state.attributeCount && m_vi_state.bindingCount) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001219 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barboure815fd72015-07-27 09:36:24 -06001220 info.pVertexInputState = &m_vi_state;
1221 } else {
1222 info.pVertexInputState = NULL;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001223 }
1224
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001225 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001226 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001227 info.flags = 0;
1228 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001229
Tony Barbourf52346d2015-01-16 14:27:35 -07001230 m_cb_state.attachmentCount = m_colorAttachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -06001231 m_cb_state.pAttachments = m_colorAttachments.data();
Tony Barbourf52346d2015-01-16 14:27:35 -07001232
Chia-I Wu08accc62015-07-07 11:50:03 +08001233 info.renderPass = render_pass;
1234 info.subpass = 0;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001235 info.pTessellationState = NULL;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001236 info.pInputAssemblyState = &m_ia_state;
1237 info.pViewportState = &m_vp_state;
1238 info.pRasterState = &m_rs_state;
1239 info.pMultisampleState = &m_ms_state;
1240 info.pDepthStencilState = &m_ds_state;
1241 info.pColorBlendState = &m_cb_state;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001242
Chris Forbes95292b12015-05-25 11:13:26 +12001243 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001244}
Chia-I Wu2648d092014-12-29 14:24:14 +08001245
Tony Barbour6918cd52015-04-09 12:58:51 -06001246VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Tony Barbour6d047bf2014-12-10 14:34:45 -07001247{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001248 m_device = device;
Courtney Goeltzenleuchter0c8385b2015-07-13 12:57:11 -06001249
1250 m_cmdPool.init(*device, vk_testing::CmdPool::create_info(device->graphics_queue_node_index_));
1251 init(*device, vk_testing::CmdBuffer::create_info(m_cmdPool.handle()));
Tony Barbour6d047bf2014-12-10 14:34:45 -07001252}
Tony Barbour471338d2014-12-10 17:28:39 -07001253
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001254VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001255{
Chia-I Wube2b9172015-07-03 11:49:42 +08001256 return handle();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001257}
Tony Barbour471338d2014-12-10 17:28:39 -07001258
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001259VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001260{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001261 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001262 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001263}
1264
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001265VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001266{
1267 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001268 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001269}
1270
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001271VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001272{
Chia-I Wud28343c2014-12-28 15:12:48 +08001273 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001274 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001275}
1276
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001277void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, VkBool32 byRegion, uint32_t memBarrierCount, const void* const* ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001278{
Chia-I Wube2b9172015-07-03 11:49:42 +08001279 vkCmdPipelineBarrier(handle(), src_stages, dest_stages, byRegion, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001280}
1281
Chris Forbesf0796e12015-06-24 14:34:53 +12001282void VkCommandBufferObj::ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001283 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001284{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001285 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001286 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001287 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001288 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1289 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1290 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001291 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001292 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001293
1294 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001295 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001296 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001297 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001298 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001299 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001300 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001301
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001302 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001303 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001304 memory_barrier.outputMask = output_mask;
1305 memory_barrier.inputMask = input_mask;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001306 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001307 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001308 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001309
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001310 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1311 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001312
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001313 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001314 memory_barrier.image = m_renderTargets[i]->image();
1315 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001316 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001317 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001318
Chia-I Wube2b9172015-07-03 11:49:42 +08001319 vkCmdClearColorImage(handle(),
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001320 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001321 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001322
Tony Barbour30cc9e82014-12-17 11:53:55 -07001323 }
1324
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001325 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001326 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001327 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001328 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001329 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001330 dsRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001331 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001332 dsRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001333
1334 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001335
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001336 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001337 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001338 memory_barrier.image = depthStencilObj->handle();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001339 memory_barrier.subresourceRange = dsRange;
1340
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001341 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001342
Chia-I Wube2b9172015-07-03 11:49:42 +08001343 vkCmdClearDepthStencilImage(handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +08001344 depthStencilObj->handle(), VK_IMAGE_LAYOUT_GENERAL,
Chris Forbesd9be82b2015-06-22 17:21:59 +12001345 depth_clear_color, stencil_clear_color,
1346 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001347
1348 // prepare depth buffer for rendering
Chia-I Wu681d7a02015-07-03 13:44:34 +08001349 memory_barrier.image = depthStencilObj->handle();
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001350 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001351 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001352 memory_barrier.subresourceRange = dsRange;
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001353 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001354 }
1355}
1356
Tony Barbourc1eb1a52015-07-20 13:00:10 -06001357void VkCommandBufferObj::FillBuffer(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize fill_size, uint32_t data)
1358{
1359 vkCmdFillBuffer( handle(), buffer, offset, fill_size, data);
1360}
1361
Tony Barbour6918cd52015-04-09 12:58:51 -06001362void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001363{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001364 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001365 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001366 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001367 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1368 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1369 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001370 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001371 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001372 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001373 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1374 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1375 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1376 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1377 VK_MEMORY_INPUT_SHADER_READ_BIT |
1378 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1379 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001380 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001381
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001382 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001383 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001384 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001385 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001386 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001387 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001388
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001389 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001390 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001391 memory_barrier.outputMask = output_mask;
1392 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001393 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001394 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001395 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001396
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001397 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1398 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001399
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001400 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001401 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001402 memory_barrier.image = m_renderTargets[i]->image();
1403 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001404 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001405 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001406 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001407}
1408
Chia-I Wu08accc62015-07-07 11:50:03 +08001409void VkCommandBufferObj::BeginRenderPass(const VkRenderPassBeginInfo &info)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001410{
Chia-I Wube2b9172015-07-03 11:49:42 +08001411 vkCmdBeginRenderPass( handle(), &info, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001412}
1413
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001414void VkCommandBufferObj::EndRenderPass()
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001415{
Chia-I Wube2b9172015-07-03 11:49:42 +08001416 vkCmdEndRenderPass(handle());
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001417}
1418
Tony Barbour67e99152015-07-10 14:10:27 -06001419void VkCommandBufferObj::BindDynamicViewportState(VkDynamicViewportState viewportState)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001420{
Tony Barbour67e99152015-07-10 14:10:27 -06001421 vkCmdBindDynamicViewportState( handle(), viewportState);
1422}
1423
1424void VkCommandBufferObj::BindDynamicRasterState(VkDynamicRasterState rasterState)
1425{
1426 vkCmdBindDynamicRasterState( handle(), rasterState);
1427}
1428
1429void VkCommandBufferObj::BindDynamicColorBlendState(VkDynamicColorBlendState colorBlendState)
1430{
1431 vkCmdBindDynamicColorBlendState( handle(), colorBlendState);
1432}
1433
1434void VkCommandBufferObj::BindDynamicDepthStencilState(VkDynamicDepthStencilState depthStencilState)
1435{
1436 vkCmdBindDynamicDepthStencilState( handle(), depthStencilState);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001437}
1438
Tony Barbour6918cd52015-04-09 12:58:51 -06001439void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001440{
1441 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001442}
1443
Tony Barbour6918cd52015-04-09 12:58:51 -06001444void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001445{
Chia-I Wube2b9172015-07-03 11:49:42 +08001446 vkCmdDrawIndexed(handle(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001447}
1448
Tony Barbour6918cd52015-04-09 12:58:51 -06001449void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001450{
Chia-I Wube2b9172015-07-03 11:49:42 +08001451 vkCmdDraw(handle(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001452}
1453
Tony Barbour6918cd52015-04-09 12:58:51 -06001454void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001455{
Tony Barbour67e99152015-07-10 14:10:27 -06001456 VkFence nullFence = { VK_NULL_HANDLE };
1457 QueueCommandBuffer(nullFence);
Tony Barbour09b7ac32015-04-08 10:11:29 -06001458}
1459
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001460void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001461{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001462 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001463
1464 // submit the command buffer to the universal queue
Chia-I Wube2b9172015-07-03 11:49:42 +08001465 err = vkQueueSubmit( m_device->m_queue, 1, &handle(), fence );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001466 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001467
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001468 err = vkQueueWaitIdle( m_device->m_queue );
1469 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001470
1471 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001472 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001473}
1474
Tony Barbour6918cd52015-04-09 12:58:51 -06001475void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001476{
Chia-I Wube2b9172015-07-03 11:49:42 +08001477 vkCmdBindPipeline( handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.handle() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001478}
1479
Tony Barbour6918cd52015-04-09 12:58:51 -06001480void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001481{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001482 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001483
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001484 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wube2b9172015-07-03 11:49:42 +08001485 vkCmdBindDescriptorSets(handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northropd4c1a502015-04-16 13:41:56 -06001486 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001487}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001488
Tony Barbour6918cd52015-04-09 12:58:51 -06001489void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001490{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001491 vkCmdBindIndexBuffer(handle(), indexBuffer->handle(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001492}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001493
Tony Barbourd1c35722015-04-16 15:59:00 -06001494void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001495{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001496 vkCmdBindVertexBuffers(handle(), binding, 1, &vertexBuffer->handle(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001497}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001498
Tony Barbour6918cd52015-04-09 12:58:51 -06001499VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001500{
1501 m_initialized = false;
1502}
Tony Barbour6918cd52015-04-09 12:58:51 -06001503bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001504{
1505 return m_initialized;
1506}
1507
Chia-I Wu08accc62015-07-07 11:50:03 +08001508VkAttachmentBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001509{
Chia-I Wu08accc62015-07-07 11:50:03 +08001510 return &m_attachmentBindInfo;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001511}
1512
Chia-I Wu08accc62015-07-07 11:50:03 +08001513void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height, VkFormat format)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001514{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001515 VkImageCreateInfo image_info;
Chia-I Wu08accc62015-07-07 11:50:03 +08001516 VkAttachmentViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001517
1518 m_device = device;
1519 m_initialized = true;
Chia-I Wu08accc62015-07-07 11:50:03 +08001520 m_depth_stencil_fmt = format;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001521
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001522 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001523 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001524 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001525 image_info.format = m_depth_stencil_fmt;
1526 image_info.extent.width = width;
1527 image_info.extent.height = height;
1528 image_info.extent.depth = 1;
1529 image_info.mipLevels = 1;
1530 image_info.arraySize = 1;
1531 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001532 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001533 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001534 image_info.flags = 0;
1535 init(*m_device, image_info);
1536
Chia-I Wu08accc62015-07-07 11:50:03 +08001537 view_info.sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001538 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001539 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001540 view_info.mipLevel = 0;
1541 view_info.baseArraySlice = 0;
1542 view_info.arraySize = 1;
1543 view_info.flags = 0;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001544 view_info.image = handle();
Chia-I Wu08accc62015-07-07 11:50:03 +08001545 m_attachmentView.init(*m_device, view_info);
Tony Barbour1c45ce02015-03-27 17:03:18 -06001546
Chia-I Wu3158bf32015-07-03 11:49:42 +08001547 m_attachmentBindInfo.view = m_attachmentView.handle();
Chia-I Wu08accc62015-07-07 11:50:03 +08001548 m_attachmentBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001549}