blob: 456c656084485f0a9ef91ebd6dcfe90906a660e3 [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Jon Ashburn83a64252015-04-15 11:31:12 -060030#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
31
Tony Barbour6918cd52015-04-09 12:58:51 -060032VkRenderFramework::VkRenderFramework() :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060033 m_cmdBuffer( VK_NULL_HANDLE ),
Tony Barbourf77fd652015-04-09 11:07:02 -060034 m_renderPass(VK_NULL_HANDLE),
35 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060036 m_stateRaster( VK_NULL_HANDLE ),
37 m_colorBlend( VK_NULL_HANDLE ),
38 m_stateViewport( VK_NULL_HANDLE ),
39 m_stateDepthStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060040 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070041 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060042 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
43 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Chris Forbese182fe02015-06-15 09:32:35 +120044 m_clear_via_load_op( false ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070045 m_depth_clear_color( 1.0 ),
Courtney Goeltzenleuchterb6c9aca2015-06-08 16:19:37 -060046 m_stencil_clear_color( 0 ),
47 m_depthStencil( NULL ),
48 m_dbgCreateMsgCallback( VK_NULL_HANDLE ),
49 m_dbgDestroyMsgCallback( VK_NULL_HANDLE ),
50 m_globalMsgCallback( VK_NULL_HANDLE ),
51 m_devMsgCallback( VK_NULL_HANDLE )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060052{
Tony Barbour1c45ce02015-03-27 17:03:18 -060053
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070054 // clear the back buffer to dark grey
Chris Forbesf0796e12015-06-24 14:34:53 +120055 m_clear_color.f32[0] = 0.25f;
56 m_clear_color.f32[1] = 0.25f;
57 m_clear_color.f32[2] = 0.25f;
58 m_clear_color.f32[3] = 0.0f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060059}
60
Tony Barbour6918cd52015-04-09 12:58:51 -060061VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060062{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060063
64}
65
Tony Barbour6918cd52015-04-09 12:58:51 -060066void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060067{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060068 std::vector<const char*> instance_extension_names;
69 std::vector<const char*> device_extension_names;
70 InitFramework(instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060071}
72
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060073void VkRenderFramework::InitFramework(
74 std::vector<const char *> instance_extension_names,
75 std::vector<const char *> device_extension_names,
76 PFN_vkDbgMsgCallback dbgFunction,
77 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060078{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060079 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060080 std::vector<VkExtensionProperties> instance_extensions;
81 std::vector<VkExtensionProperties> device_extensions;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060082 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060083
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -060084 /* TODO: Verify requested extensions are available */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060085
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060086 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060087 instInfo.pNext = NULL;
88 instInfo.pAppInfo = &app_info;
89 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -060090 instInfo.extensionCount = instance_extension_names.size();
91 instInfo.ppEnabledExtensionNames = (instance_extension_names.size()) ? &instance_extension_names[0] : NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060092 err = vkCreateInstance(&instInfo, &this->inst);
93 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060094
Jon Ashburn83a64252015-04-15 11:31:12 -060095 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
96 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
97 ASSERT_VK_SUCCESS(err);
98 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060099 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700100 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600101 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600102 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
103 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
104 if (m_dbgCreateMsgCallback) {
105 err = m_dbgCreateMsgCallback(this->inst,
106 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
107 dbgFunction,
108 userData,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600109 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600110 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600111
112 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
113 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600114 }
Tony Barbour15524c32015-04-29 17:34:29 -0600115 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600116
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600117 /* TODO: Verify requested physical device extensions are available */
118 m_device = new VkDeviceObj(0, objs[0], device_extension_names);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600119
120 /* Now register callback on device */
121 if (0) {
122 if (m_dbgCreateMsgCallback) {
123 err = m_dbgCreateMsgCallback(this->inst,
124 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
125 dbgFunction,
126 userData,
127 &m_devMsgCallback);
128 ASSERT_VK_SUCCESS(err);
129 }
130 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600131 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600132
Tony Barbour6918cd52015-04-09 12:58:51 -0600133 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600134}
135
Tony Barbour6918cd52015-04-09 12:58:51 -0600136void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600137{
Mike Stroyanb050c682015-04-17 12:36:38 -0600138 if (m_colorBlend) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_CB_STATE, m_colorBlend);
139 if (m_stateDepthStencil) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_DS_STATE, m_stateDepthStencil);
140 if (m_stateRaster) vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_RS_STATE, m_stateRaster);
141 if (m_cmdBuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_COMMAND_BUFFER, m_cmdBuffer);
142 if (m_framebuffer) vkDestroyObject(device(), VK_OBJECT_TYPE_FRAMEBUFFER, m_framebuffer);
143 if (m_renderPass) vkDestroyObject(device(), VK_OBJECT_TYPE_RENDER_PASS, m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600144
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600145 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
146 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
147
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600148 if (m_stateViewport) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600149 vkDestroyObject(device(), VK_OBJECT_TYPE_DYNAMIC_VP_STATE, m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600150 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600151 while (!m_renderTargets.empty()) {
Mike Stroyanb050c682015-04-17 12:36:38 -0600152 vkDestroyObject(device(), VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, m_renderTargets.back()->targetView());
Mike Stroyanb050c682015-04-17 12:36:38 -0600153 vkDestroyObject(device(), VK_OBJECT_TYPE_IMAGE, m_renderTargets.back()->image());
154 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600155 m_renderTargets.pop_back();
156 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600157
Tony Barbour1c45ce02015-03-27 17:03:18 -0600158 delete m_depthStencil;
159
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600160 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800161 delete m_device;
Chris Forbesbe2fae62015-07-07 11:02:22 +1200162 if (this->inst) vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600163}
164
Tony Barbour6918cd52015-04-09 12:58:51 -0600165void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600166{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600167 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600168
Tony Barbourd1c35722015-04-16 15:59:00 -0600169 m_render_target_fmt = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600170
171 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600172 VkDynamicRsStateCreateInfo raster = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600173 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700174
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600175 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
176 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600177
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600178 VkDynamicCbStateCreateInfo blend = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600179 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600180 blend.blendConst[0] = 1.0f;
181 blend.blendConst[1] = 1.0f;
182 blend.blendConst[2] = 1.0f;
183 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600184 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
185 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600186
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600187 VkDynamicDsStateCreateInfo depthStencil = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600188 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski365feea2015-06-12 11:14:17 -0600189 depthStencil.minDepthBounds = 0.f;
190 depthStencil.maxDepthBounds = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700191 depthStencil.stencilFrontRef = 0;
192 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600193 depthStencil.stencilReadMask = 0xff;
194 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600195
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600196 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
197 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600198
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600199 VkCmdBufferCreateInfo cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600200
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600201 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700202 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
203
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600204 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
205 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600206}
207
Tony Barbour6918cd52015-04-09 12:58:51 -0600208void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600209{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600210 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600211
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600212 VkViewport viewport;
Chris Forbesd9be82b2015-06-22 17:21:59 +1200213 VkRect2D scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600214
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600215 VkDynamicVpStateCreateInfo viewportCreate = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600216 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700217 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700218 viewport.originX = 0;
219 viewport.originY = 0;
220 viewport.width = 1.f * width;
221 viewport.height = 1.f * height;
222 viewport.minDepth = 0.f;
223 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600224 scissor.extent.width = (int32_t) width;
225 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700226 scissor.offset.x = 0;
227 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700228 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700229 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700230
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600231 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
232 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600233 m_width = width;
234 m_height = height;
235}
236
Tony Barbour6918cd52015-04-09 12:58:51 -0600237void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600238{
239 InitViewport(m_width, m_height);
240}
Tony Barbour6918cd52015-04-09 12:58:51 -0600241void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600242{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700243 InitRenderTarget(1);
244}
245
Tony Barbour6918cd52015-04-09 12:58:51 -0600246void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700247{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600248 InitRenderTarget(targets, NULL);
249}
250
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600251void VkRenderFramework::InitRenderTarget(VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600252{
253 InitRenderTarget(1, dsBinding);
254}
255
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600256void VkRenderFramework::InitRenderTarget(uint32_t targets, VkDepthStencilBindInfo *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600257{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600258 std::vector<VkAttachmentLoadOp> load_ops;
259 std::vector<VkAttachmentStoreOp> store_ops;
Chris Forbesf0796e12015-06-24 14:34:53 +1200260 std::vector<VkClearColorValue> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600261
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600262 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800263
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700264 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600265 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600266
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600267 VkFormatProperties props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600268 VkResult err;
269
Chris Forbesbc0bb772015-06-21 22:55:02 +1200270 err = vkGetPhysicalDeviceFormatInfo(m_device->gpu().obj(), m_render_target_fmt, &props);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600271 ASSERT_VK_SUCCESS(err);
272
273 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600274 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600275 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_LINEAR);
276 }
277 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600278 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600279 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL);
280 }
281 else {
282 FAIL() << "Neither Linear nor Optimal allowed for render target";
283 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600284
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600285 m_renderTargets.push_back(img);
Tony Barbourf52346d2015-01-16 14:27:35 -0700286 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600287 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chris Forbese182fe02015-06-15 09:32:35 +1200288 load_ops.push_back(m_clear_via_load_op ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600289 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600290 clear_colors.push_back(m_clear_color);
Chia-I Wuecebf752014-12-05 10:45:15 +0800291 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600292
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700293 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600294 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600295 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600296 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700297 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600298 fb_info.pColorAttachments = m_colorBindings;
299 fb_info.pDepthStencilAttachment = dsBinding;
300 fb_info.sampleCount = 1;
301 fb_info.width = (uint32_t)m_width;
302 fb_info.height = (uint32_t)m_height;
303 fb_info.layers = 1;
304
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600305 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600306
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600307 VkRenderPassCreateInfo rp_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600308 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600309 rp_info.renderArea.extent.width = (uint32_t) m_width;
310 rp_info.renderArea.extent.height = (uint32_t) m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600311
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700312 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600313 rp_info.pColorFormats = &m_render_target_fmt;
314 rp_info.pColorLayouts = &m_colorBindings[0].layout;
315 rp_info.pColorLoadOps = &load_ops[0];
316 rp_info.pColorStoreOps = &store_ops[0];
317 rp_info.pColorLoadClearValues = &clear_colors[0];
318 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600319 if (dsBinding) {
320 rp_info.depthStencilLayout = dsBinding->layout;
321 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600322 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600323 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600324 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
325 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600326 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600327 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Tony Barbour51a05ce2015-06-03 11:40:51 -0600328 rp_info.sampleCount = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600329 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600330}
331
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600332
333
Tony Barbourd1c35722015-04-16 15:59:00 -0600334VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600335 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800336{
337 init();
338
339 props = gpu().properties();
340 queue_props = &gpu().queue_properties()[0];
341}
342
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600343VkDeviceObj::VkDeviceObj(uint32_t id,
344 VkPhysicalDevice obj,
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600345 std::vector<const char *> &extensions) :
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600346 vk_testing::Device(obj), id(id)
347{
348 init(extensions);
349
350 props = gpu().properties();
351 queue_props = &gpu().queue_properties()[0];
352}
353
Tony Barbour6918cd52015-04-09 12:58:51 -0600354void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800355{
356 ASSERT_NE(true, graphics_queues().empty());
357 m_queue = graphics_queues()[0]->obj();
358}
359
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600360VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
361 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700362{
Tony Barboure2c58df2014-11-25 13:18:32 -0700363
364}
365
Tony Barbour6918cd52015-04-09 12:58:51 -0600366VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700367{
Chia-I Wu11078b02015-01-04 16:27:24 +0800368 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700369}
Tony Barbour82c39522014-12-04 14:33:33 -0700370
Tony Barbour6918cd52015-04-09 12:58:51 -0600371int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700372{
Chia-I Wu11078b02015-01-04 16:27:24 +0800373 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600374 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600375 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800376 tc.count = 1;
377 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700378
Chia-I Wu11078b02015-01-04 16:27:24 +0800379 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700380}
Tony Barbour82c39522014-12-04 14:33:33 -0700381
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600382int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700383{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600384 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800385 tc.type = type;
386 tc.count = 1;
387 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700388
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800389 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
390 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600391
Chia-I Wu11078b02015-01-04 16:27:24 +0800392 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700393}
Tony Barbour82c39522014-12-04 14:33:33 -0700394
Tony Barbour6918cd52015-04-09 12:58:51 -0600395int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700396{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600397 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600398 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800399 tc.count = 1;
400 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700401
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800402 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600403 tmp.sampler = sampler->obj();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800404 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700405
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800406 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
407 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700408
Chia-I Wu11078b02015-01-04 16:27:24 +0800409 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700410}
Chia-I Wu11078b02015-01-04 16:27:24 +0800411
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500412VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700413{
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500414 return m_pipeline_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700415}
416
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600417VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700418{
Chia-I Wu11078b02015-01-04 16:27:24 +0800419 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700420}
Tony Barboure2c58df2014-11-25 13:18:32 -0700421
Tony Barbour6918cd52015-04-09 12:58:51 -0600422void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700423{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600424 // create VkDescriptorPool
425 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600426 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800427 pool.count = m_type_counts.size();
428 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600429 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700430
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600431 // create VkDescriptorSetLayout
432 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800433 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800434 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800435 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800436 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600437 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800438 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700439 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800440
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600441 // create VkDescriptorSetLayout
442 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600443 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800444 layout.count = bindings.size();
445 layout.pBinding = &bindings[0];
446
Chia-I Wu41126e52015-03-26 15:27:55 +0800447 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600448 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800449 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500450
451 // create VkPipelineLayout
452 VkPipelineLayoutCreateInfo pipeline_layout = {};
453 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
454 pipeline_layout.descriptorSetCount = layouts.size();
455 pipeline_layout.pSetLayouts = NULL;
456
457 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700458
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600459 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500460 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800461
Chia-I Wu41126e52015-03-26 15:27:55 +0800462 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800463 size_t imageSamplerCount = 0;
464 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
465 it != m_writes.end(); it++) {
466 it->destSet = m_set->obj();
467 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
468 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800469 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800470
471 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800472 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700473}
Tony Barboure2c58df2014-11-25 13:18:32 -0700474
Tony Barbour6918cd52015-04-09 12:58:51 -0600475VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800476{
477 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800478 m_descriptorInfo.imageView = VK_NULL_HANDLE;
479 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800480}
481
Tony Barbour6918cd52015-04-09 12:58:51 -0600482void VkImageObj::ImageMemoryBarrier(
483 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600484 VkImageAspect aspect,
485 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600486 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600487 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
488 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
489 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
490 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600491 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600492 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600493 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
494 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
495 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
496 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
497 VK_MEMORY_INPUT_SHADER_READ_BIT |
498 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
499 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
500 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600501 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600502{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600503 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
504 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600505 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
506 subresourceRange);
507
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600508 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600509
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600510 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
511 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600512
513 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600514 vkCmdPipelineBarrier(cmd_buf->obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600515}
516
Tony Barbour6918cd52015-04-09 12:58:51 -0600517void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600518 VkImageAspect aspect,
519 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600520{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600521 VkFlags output_mask, input_mask;
522 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600523 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600524 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
525 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
526 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600527 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600528 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600529 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600530 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
531 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
532 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
533 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
534 VK_MEMORY_INPUT_SHADER_READ_BIT |
535 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
536 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600537 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600538
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800539 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600540 return;
541 }
542
543 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600544 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600545 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
546 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600547 break;
548
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600549 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600550 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
551 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600552 break;
553
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600554 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600555 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
556 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600557 break;
558
559 default:
560 output_mask = all_cache_outputs;
561 input_mask = all_cache_inputs;
562 break;
563 }
564
565 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800566 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600567}
568
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600569void VkImageObj::SetLayout(VkImageAspect aspect,
570 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600571{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600572 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600573
574 if (image_layout == m_descriptorInfo.imageLayout) {
575 return;
576 }
577
Tony Barbour6918cd52015-04-09 12:58:51 -0600578 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600579
580 /* Build command buffer to set image layout in the driver */
581 err = cmd_buf.BeginCommandBuffer();
582 assert(!err);
583
584 SetLayout(&cmd_buf, aspect, image_layout);
585
586 err = cmd_buf.EndCommandBuffer();
587 assert(!err);
588
589 cmd_buf.QueueCommandBuffer();
590}
591
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600592bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600593{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600594 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600595 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600596 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600597
Tony Barbour579f7802015-04-03 15:11:43 -0600598 return true;
599}
600
Tony Barbour6918cd52015-04-09 12:58:51 -0600601void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600602 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600603 VkImageTiling requested_tiling,
604 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800605{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600606 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600607 VkFormatProperties image_fmt;
608 VkImageTiling tiling;
609 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600610
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800611 mipCount = 0;
612
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600613 uint32_t _w = w;
614 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800615 while( ( _w > 0 ) || ( _h > 0 ) )
616 {
617 _w >>= 1;
618 _h >>= 1;
619 mipCount++;
620 }
621
Chris Forbesbc0bb772015-06-21 22:55:02 +1200622 err = vkGetPhysicalDeviceFormatInfo(m_device->gpu().obj(), fmt, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600623 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600624
Tony Barbourd1c35722015-04-16 15:59:00 -0600625 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600626 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600627 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600628 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600629 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600630 } else {
631 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
632 }
633 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600634 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600635 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600636 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600637 } else {
638 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
639 }
640
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600641 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600642 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800643 imageCreateInfo.format = fmt;
644 imageCreateInfo.extent.width = w;
645 imageCreateInfo.extent.height = h;
646 imageCreateInfo.mipLevels = mipCount;
647 imageCreateInfo.tiling = tiling;
648
649 imageCreateInfo.usage = usage;
650
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600651 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800652
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600653 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600654 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600655 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600656 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600657 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800658}
659
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600660VkResult VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800661{
662 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600663 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800664}
665
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600666VkResult VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800667{
668 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600669 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800670}
671
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600672VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600673{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600674 VkResult U_ASSERT_ONLY err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600675 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600676 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600677
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600678 /* Build command buffer to copy staging texture to usable texture */
679 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600680 assert(!err);
681
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600682 /* TODO: Can we determine image aspect from image object? */
683 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600684 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600685
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600686 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600687 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600688
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600689 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600690 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600691 copy_region.srcSubresource.arraySlice = 0;
692 copy_region.srcSubresource.mipLevel = 0;
693 copy_region.srcOffset.x = 0;
694 copy_region.srcOffset.y = 0;
695 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600696 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600697 copy_region.destSubresource.arraySlice = 0;
698 copy_region.destSubresource.mipLevel = 0;
699 copy_region.destOffset.x = 0;
700 copy_region.destOffset.y = 0;
701 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600702 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600703
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600704 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600705 src_image.obj(), src_image.layout(),
706 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600707 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600708
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600709 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600710
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600711 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600712
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600713 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600714 assert(!err);
715
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600716 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600717
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600718 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600719}
720
Tony Barbour6918cd52015-04-09 12:58:51 -0600721VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
722 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700723{
724 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600725 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600726 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600727 void *data;
728 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600729 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600730 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600731
Cody Northropbb6fdb32015-06-17 08:28:19 -0600732 stagingImage.init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600733 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600734
735 if (colors == NULL)
736 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700737
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800738 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700739
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600740 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600741 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600742 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600743 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600744 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600745 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600746 view.channels.r = VK_CHANNEL_SWIZZLE_R;
747 view.channels.g = VK_CHANNEL_SWIZZLE_G;
748 view.channels.b = VK_CHANNEL_SWIZZLE_B;
749 view.channels.a = VK_CHANNEL_SWIZZLE_A;
750 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600751 view.subresourceRange.baseMipLevel = 0;
752 view.subresourceRange.mipLevels = 1;
753 view.subresourceRange.baseArraySlice = 0;
754 view.subresourceRange.arraySize = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700755
Tony Barboure2c58df2014-11-25 13:18:32 -0700756 /* create image */
Tony Barbourd1c35722015-04-16 15:59:00 -0600757 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700758
759 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800760 view.image = obj();
761 m_textureView.init(*m_device, view);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800762 m_descriptorInfo.imageView = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700763
Tony Barbour5dc515d2015-04-01 17:47:06 -0600764 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700765
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800766 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700767 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800768 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600769 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700770 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600771 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600772 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700773}
Tony Barbour82c39522014-12-04 14:33:33 -0700774
Tony Barbour6918cd52015-04-09 12:58:51 -0600775VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700776{
Tony Barboure2c58df2014-11-25 13:18:32 -0700777 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700778
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600779 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800780 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600781 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
782 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
783 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600784 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600785 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
786 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
787 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800788 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600789 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600790 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800791 samplerCreateInfo.minLod = 0.0;
792 samplerCreateInfo.maxLod = 0.0;
Tony Barbour26b17f82015-06-25 16:56:44 -0600793 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700794
Chia-I Wue9864b52014-12-28 16:32:24 +0800795 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700796}
Tony Barboure2c58df2014-11-25 13:18:32 -0700797
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700798/*
799 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
800 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600801VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700802{
803 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700804 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700805
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800806 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700807}
808
Tony Barbour6918cd52015-04-09 12:58:51 -0600809VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600810{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600811 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600812 if (m_commandBuffer) {
813 delete m_commandBuffer;
814 }
815}
816
Tony Barbour6918cd52015-04-09 12:58:51 -0600817VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700818{
Tony Barboure2c58df2014-11-25 13:18:32 -0700819 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800820 m_commandBuffer = 0;
821
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800822 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700823 m_numVertices = constantCount;
824 m_stride = constantSize;
825
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600826 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800827 const size_t allocationSize = constantCount * constantSize;
Cody Northrop7fb43862015-06-22 14:56:14 -0600828 init_as_src_and_dst(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700829
Chia-I Wua07fee62014-12-28 15:26:08 +0800830 void *pData = map();
831 memcpy(pData, data, allocationSize);
832 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700833
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800834 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600835 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600836 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800837 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600838 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800839 view_info.offset = 0;
840 view_info.range = allocationSize;
841 m_bufferView.init(*m_device, view_info);
842
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800843 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700844}
Tony Barbour82c39522014-12-04 14:33:33 -0700845
Tony Barbourd1c35722015-04-16 15:59:00 -0600846void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700847{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -0600848 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &obj(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700849}
850
851
Tony Barbour6918cd52015-04-09 12:58:51 -0600852void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600853 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600854 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600855 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
856 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
857 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
858 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600859 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600860 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600861 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
862 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
863 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
864 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
865 VK_MEMORY_INPUT_SHADER_READ_BIT |
866 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
867 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
868 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700869{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600870 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700871
Tony Barbour38422802014-12-10 14:36:31 -0700872 if (!m_commandBuffer)
873 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600874 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700875
Tony Barbour6918cd52015-04-09 12:58:51 -0600876 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700877 }
878 else
879 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800880 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700881 }
882
Tony Barboure2c58df2014-11-25 13:18:32 -0700883 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600884 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600885 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600886 cmd_buf_info.pNext = NULL;
887 cmd_buf_info.flags = 0;
888
Jon Ashburnc4164b12014-12-31 17:10:47 -0700889 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600890 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700891
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600892 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000893 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600894 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700895
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600896 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
897 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000898
899 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600900 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700901
902 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700903 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600904 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700905
Tony Barboure2c58df2014-11-25 13:18:32 -0700906 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600907 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700908 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600909 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
910 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700911}
912
Tony Barbour6918cd52015-04-09 12:58:51 -0600913VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
914 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700915{
916
917}
918
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600919void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700920{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600921 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700922
923 m_numVertices = numIndexes;
924 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700925 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600926 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700927 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -0600928 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700929 break;
Tony Barbourd1c35722015-04-16 15:59:00 -0600930 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700931 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -0600932 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700933 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800934 default:
935 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -0600936 m_stride = 2;
937 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800938 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700939 }
940
Chia-I Wua07fee62014-12-28 15:26:08 +0800941 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600942 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northrop7fb43862015-06-22 14:56:14 -0600943 init_as_src_and_dst(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700944
Chia-I Wua07fee62014-12-28 15:26:08 +0800945 void *pData = map();
946 memcpy(pData, data, allocationSize);
947 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700948
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800949 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600950 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600951 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800952 view_info.buffer = obj();
Tony Barbourd1c35722015-04-16 15:59:00 -0600953 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700954 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800955 view_info.offset = 0;
956 view_info.range = allocationSize;
957 m_bufferView.init(*m_device, view_info);
958
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800959 this->m_descriptorInfo.bufferView = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700960}
961
Tony Barbourd1c35722015-04-16 15:59:00 -0600962void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700963{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600964 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700965}
Tony Barboure2c58df2014-11-25 13:18:32 -0700966
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600967VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -0700968{
969 return m_indexType;
970}
971
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600972VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700973{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600974 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600975 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
976 stageInfo->stage = m_stage;
977 stageInfo->shader = obj();
978 stageInfo->linkConstBufferCount = 0;
979 stageInfo->pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700980
Tony Barboure2c58df2014-11-25 13:18:32 -0700981 return stageInfo;
982}
983
Tony Barbourd1c35722015-04-16 15:59:00 -0600984VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -0700985{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600986 VkResult err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600987 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600988 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600989 VkShaderModuleCreateInfo moduleCreateInfo;
990 vk_testing::ShaderModule module;
Tony Barboure2c58df2014-11-25 13:18:32 -0700991 size_t shader_len;
992
993 m_stage = stage;
994 m_device = device;
995
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600996 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
997 moduleCreateInfo.pNext = NULL;
998
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600999 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001000 createInfo.pNext = NULL;
1001
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001002 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001003
1004 shader_len = strlen(shader_code);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001005 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1006 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1007 moduleCreateInfo.flags = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001008
Tony Barbourd1c35722015-04-16 15:59:00 -06001009 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001010 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1011 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1012 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1013 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barboure2c58df2014-11-25 13:18:32 -07001014
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001015 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001016
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001017 // Use Reference GLSL to SPV compiler
1018 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001019 moduleCreateInfo.pCode = spv.data();
1020 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1021 moduleCreateInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001022 }
Cody Northrop475663c2015-04-15 11:19:06 -06001023
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001024 err = module.init_try(*m_device, moduleCreateInfo);
1025 assert(VK_SUCCESS == err);
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001026
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001027 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1028 createInfo.pNext = NULL;
1029 createInfo.module = module.obj();
1030 createInfo.pName = "main";
1031 createInfo.flags = 0;
1032
1033 err = init_try(*m_device, createInfo);
Cody Northrop475663c2015-04-15 11:19:06 -06001034 assert(VK_SUCCESS == err);
Tony Barbourf325bf12014-12-03 15:59:38 -07001035}
Tony Barbour82c39522014-12-04 14:33:33 -07001036
Tony Barbour6918cd52015-04-09 12:58:51 -06001037VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001038{
Tony Barboure2c58df2014-11-25 13:18:32 -07001039 m_device = device;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001040
1041 m_vi_state.pNext = VK_NULL_HANDLE;
1042 m_vi_state.bindingCount = 0;
1043 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1044 m_vi_state.attributeCount = 0;
1045 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1046
Tony Barboure2c58df2014-11-25 13:18:32 -07001047 m_vertexBufferCount = 0;
1048
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001049 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1050 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001051 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001052 m_ia_state.disableVertexReuse = VK_FALSE;
1053 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001054 m_ia_state.primitiveRestartIndex = 0;
1055
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001056 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001057 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001058 m_rs_state.depthClipEnable = VK_FALSE;
1059 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001060 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1061 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
Tony Barbourd1c35722015-04-16 15:59:00 -06001062 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001063 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1064 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001065
Tony Barboure2c58df2014-11-25 13:18:32 -07001066 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001067 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001068 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001069 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1070 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001071
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001072 m_ms_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001073 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1074 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001075 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
Tony Barbourdfd533a2015-06-26 10:18:34 -06001076 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001077 m_ms_state.minSampleShading = 0;
1078 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001079
Tony Barbourd81b8882015-05-15 09:37:57 -06001080 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001081 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001082 m_vp_state.viewportCount = 1;
1083 m_vp_state.depthMode = VK_DEPTH_MODE_ZERO_TO_ONE;
1084 m_vp_state.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
1085
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001086 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001087 m_ds_state.pNext = VK_NULL_HANDLE,
Tony Barbourd1c35722015-04-16 15:59:00 -06001088 m_ds_state.format = VK_FORMAT_D32_SFLOAT;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001089 m_ds_state.depthTestEnable = VK_FALSE;
1090 m_ds_state.depthWriteEnable = VK_FALSE;
1091 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001092 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001093 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1094 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1095 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001096 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001097 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001098 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001099
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001100 VkPipelineCbAttachmentState att = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001101 att.blendEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001102 att.format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001103 att.channelWriteMask = 0xf;
1104 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001105
1106};
1107
Tony Barbour6918cd52015-04-09 12:58:51 -06001108void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001109{
1110 m_shaderObjs.push_back(shader);
1111}
1112
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001113void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001114{
1115 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1116 m_vi_state.attributeCount = count;
1117}
1118
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001119void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001120{
1121 m_vi_state.pVertexBindingDescriptions = vi_binding;
1122 m_vi_state.bindingCount = count;
1123}
1124
Tony Barbour6918cd52015-04-09 12:58:51 -06001125void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001126{
1127 m_vertexBufferObjs.push_back(vertexDataBuffer);
1128 m_vertexBufferBindings.push_back(binding);
1129 m_vertexBufferCount++;
1130}
1131
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001132void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineCbAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001133{
Tony Barbourf52346d2015-01-16 14:27:35 -07001134 if (binding+1 > m_colorAttachments.size())
1135 {
1136 m_colorAttachments.resize(binding+1);
1137 }
1138 m_colorAttachments[binding] = *att;
1139}
1140
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001141void VkPipelineObj::SetDepthStencil(VkPipelineDsStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001142{
1143 m_ds_state.format = ds_state->format;
1144 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1145 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1146 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001147 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001148 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1149 m_ds_state.back = ds_state->back;
1150 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001151}
1152
Chris Forbes95292b12015-05-25 11:13:26 +12001153VkResult VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001154{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001155 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001156
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001157 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001158
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001159 info.stageCount = m_shaderObjs.size();
1160 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1161
Tony Barbour976e1cf2014-12-17 11:57:31 -07001162 for (int i=0; i<m_shaderObjs.size(); i++)
1163 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001164 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001165 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001166 }
1167
1168 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1169 {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001170 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001171 }
1172
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001173 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001174 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001175 info.flags = 0;
1176 info.layout = descriptorSet.GetPipelineLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001177
Tony Barbourf52346d2015-01-16 14:27:35 -07001178 m_cb_state.attachmentCount = m_colorAttachments.size();
1179 m_cb_state.pAttachments = &m_colorAttachments[0];
1180
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001181 info.pTessState = NULL;
1182 info.pVertexInputState = &m_vi_state;
1183 info.pIaState = &m_ia_state;
1184 info.pVpState = &m_vp_state;
1185 info.pRsState = &m_rs_state;
1186 info.pMsState = &m_ms_state;
1187 info.pDsState = &m_ds_state;
1188 info.pCbState = &m_cb_state;
1189
Chris Forbes95292b12015-05-25 11:13:26 +12001190 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001191}
Chia-I Wu2648d092014-12-29 14:24:14 +08001192
Tony Barbourd1c35722015-04-16 15:59:00 -06001193vector<VkDeviceMemory> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001194{
Tony Barbourd1c35722015-04-16 15:59:00 -06001195 std::vector<VkDeviceMemory> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001196 if (this->mem_refs_.size()) {
1197 mems.reserve(this->mem_refs_.size());
1198 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1199 mems.push_back(this->mem_refs_[i]);
1200 }
1201
1202 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001203}
1204
Tony Barbour6918cd52015-04-09 12:58:51 -06001205VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001206 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001207{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001208 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001209}
Tony Barbour471338d2014-12-10 17:28:39 -07001210
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001211VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001212{
Chia-I Wud28343c2014-12-28 15:12:48 +08001213 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001214}
Tony Barbour471338d2014-12-10 17:28:39 -07001215
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001216VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001217{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001218 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001219 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001220}
1221
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001222VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001223{
1224 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001225 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001226}
1227
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001228VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001229{
Chia-I Wud28343c2014-12-28 15:12:48 +08001230 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001231 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001232}
1233
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001234void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, bool32_t byRegion, uint32_t memBarrierCount, const void** ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001235{
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001236 vkCmdPipelineBarrier(obj(), src_stages, dest_stages, byRegion, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001237}
1238
Chris Forbesf0796e12015-06-24 14:34:53 +12001239void VkCommandBufferObj::ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001240 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001241{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001242 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001243 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001244 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001245 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1246 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1247 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001248 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001249 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001250
1251 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001252 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001253 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001254 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001255 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001256 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001257 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001258
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001259 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001260 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001261 memory_barrier.outputMask = output_mask;
1262 memory_barrier.inputMask = input_mask;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001263 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001264 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001265 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001266
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001267 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1268 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001269
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001270 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001271 memory_barrier.image = m_renderTargets[i]->image();
1272 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001273 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001274 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001275
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001276 vkCmdClearColorImage(obj(),
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001277 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001278 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001279
Tony Barbour30cc9e82014-12-17 11:53:55 -07001280 }
1281
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001282 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001283 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001284 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001285 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001286 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001287 dsRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001288 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001289 dsRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001290
1291 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001292
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001293 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001294 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001295 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001296 memory_barrier.subresourceRange = dsRange;
1297
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001298 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001299
Chris Forbesd9be82b2015-06-22 17:21:59 +12001300 vkCmdClearDepthStencilImage(obj(),
1301 depthStencilObj->obj(), VK_IMAGE_LAYOUT_GENERAL,
1302 depth_clear_color, stencil_clear_color,
1303 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001304
1305 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001306 memory_barrier.image = depthStencilObj->obj();
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001307 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001308 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001309 memory_barrier.subresourceRange = dsRange;
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001310 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001311 }
1312}
1313
Tony Barbour6918cd52015-04-09 12:58:51 -06001314void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001315{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001316 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001317 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001318 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001319 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1320 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1321 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001322 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001323 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001324 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001325 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1326 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1327 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1328 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1329 VK_MEMORY_INPUT_SHADER_READ_BIT |
1330 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1331 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001332 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001333
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001334 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001335 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001336 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001337 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001338 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001339 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001340
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001341 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001342 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001343 memory_barrier.outputMask = output_mask;
1344 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001345 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001346 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001347 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001348
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001349 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1350 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001351
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001352 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001353 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001354 memory_barrier.image = m_renderTargets[i]->image();
1355 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001356 vkCmdPipelineBarrier( obj(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001357 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001358 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001359}
1360
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001361void VkCommandBufferObj::BeginRenderPass(VkRenderPass renderpass, VkFramebuffer framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001362{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001363 VkRenderPassBegin rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001364 renderpass,
1365 framebuffer,
1366 };
1367
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001368 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001369}
1370
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001371void VkCommandBufferObj::EndRenderPass()
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001372{
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001373 vkCmdEndRenderPass(obj());
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001374}
1375
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001376void VkCommandBufferObj::BindStateObject(VkStateBindPoint stateBindPoint, VkDynamicStateObject stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001377{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001378 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001379}
1380
Tony Barbour6918cd52015-04-09 12:58:51 -06001381void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001382{
1383 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001384}
1385
Tony Barbour6918cd52015-04-09 12:58:51 -06001386void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001387{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001388 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001389}
1390
Tony Barbour6918cd52015-04-09 12:58:51 -06001391void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001392{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001393 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001394}
1395
Tony Barbour6918cd52015-04-09 12:58:51 -06001396void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001397{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001398 QueueCommandBuffer(NULL);
1399}
1400
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001401void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001402{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001403 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001404
1405 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001406 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1407 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001408
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001409 err = vkQueueWaitIdle( m_device->m_queue );
1410 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001411
1412 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001413 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001414}
1415
Tony Barbour6918cd52015-04-09 12:58:51 -06001416void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001417{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001418 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001419}
1420
Tony Barbour6918cd52015-04-09 12:58:51 -06001421void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001422{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001423 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001424
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001425 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Mark Lobodzinskif2093b62015-06-15 13:21:21 -06001426 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northropd4c1a502015-04-16 13:41:56 -06001427 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001428}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001429
Tony Barbour6918cd52015-04-09 12:58:51 -06001430void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001431{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001432 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001433}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001434
Tony Barbourd1c35722015-04-16 15:59:00 -06001435void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001436{
Courtney Goeltzenleuchterf68ad722015-04-16 13:38:46 -06001437 vkCmdBindVertexBuffers(obj(), binding, 1, &vertexBuffer->obj(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001438}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001439
Tony Barbour6918cd52015-04-09 12:58:51 -06001440VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001441{
1442 m_initialized = false;
1443}
Tony Barbour6918cd52015-04-09 12:58:51 -06001444bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001445{
1446 return m_initialized;
1447}
1448
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001449VkDepthStencilBindInfo* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001450{
1451 return &m_depthStencilBindInfo;
1452}
1453
Tony Barbour6918cd52015-04-09 12:58:51 -06001454void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001455{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001456 VkImageCreateInfo image_info;
1457 VkDepthStencilViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001458
1459 m_device = device;
1460 m_initialized = true;
Tony Barbourd1c35722015-04-16 15:59:00 -06001461 m_depth_stencil_fmt = VK_FORMAT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001462
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001463 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001464 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001465 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001466 image_info.format = m_depth_stencil_fmt;
1467 image_info.extent.width = width;
1468 image_info.extent.height = height;
1469 image_info.extent.depth = 1;
1470 image_info.mipLevels = 1;
1471 image_info.arraySize = 1;
1472 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001473 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001474 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001475 image_info.flags = 0;
1476 init(*m_device, image_info);
1477
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001478 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001479 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001480 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001481 view_info.mipLevel = 0;
1482 view_info.baseArraySlice = 0;
1483 view_info.arraySize = 1;
1484 view_info.flags = 0;
1485 view_info.image = obj();
1486 m_depthStencilView.init(*m_device, view_info);
1487
1488 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001489 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001490}