blob: f52acd071d7e3079b712702a87738e75e5174389 [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Tony Barbour6a3faf02015-07-23 10:36:18 -060029#include <vk_wsi_swapchain.h>
30#include <vk_wsi_device_swapchain.h>
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060031
Jon Ashburn83a64252015-04-15 11:31:12 -060032#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
Tony Barbour6a3faf02015-07-23 10:36:18 -060033#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
34{ \
35 fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
36 assert(fp##entrypoint != NULL); \
37}
Jon Ashburn83a64252015-04-15 11:31:12 -060038
Tony Barbour6918cd52015-04-09 12:58:51 -060039VkRenderFramework::VkRenderFramework() :
Tony Barbourfe3351b2015-07-28 10:17:20 -060040 m_cmdBuffer(),
Tony Barbourf77fd652015-04-09 11:07:02 -060041 m_renderPass(VK_NULL_HANDLE),
42 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060043 m_stateRaster( VK_NULL_HANDLE ),
44 m_colorBlend( VK_NULL_HANDLE ),
45 m_stateViewport( VK_NULL_HANDLE ),
46 m_stateDepthStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060047 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070048 m_height( 256.0 ), // default window height
Tony Barbourd1c35722015-04-16 15:59:00 -060049 m_render_target_fmt( VK_FORMAT_R8G8B8A8_UNORM ),
50 m_depth_stencil_fmt( VK_FORMAT_UNDEFINED ),
Tony Barbour71bd4b32015-07-21 17:00:26 -060051 m_clear_via_load_op( true ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070052 m_depth_clear_color( 1.0 ),
Courtney Goeltzenleuchterb6c9aca2015-06-08 16:19:37 -060053 m_stencil_clear_color( 0 ),
54 m_depthStencil( NULL ),
55 m_dbgCreateMsgCallback( VK_NULL_HANDLE ),
56 m_dbgDestroyMsgCallback( VK_NULL_HANDLE ),
57 m_globalMsgCallback( VK_NULL_HANDLE ),
58 m_devMsgCallback( VK_NULL_HANDLE )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060059{
Tony Barbour1c45ce02015-03-27 17:03:18 -060060
Chia-I Wu08accc62015-07-07 11:50:03 +080061 memset(&m_renderPassBeginInfo, 0, sizeof(m_renderPassBeginInfo));
62 m_renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
63
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070064 // clear the back buffer to dark grey
Chris Forbesf0796e12015-06-24 14:34:53 +120065 m_clear_color.f32[0] = 0.25f;
66 m_clear_color.f32[1] = 0.25f;
67 m_clear_color.f32[2] = 0.25f;
68 m_clear_color.f32[3] = 0.0f;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060069}
70
Tony Barbour6918cd52015-04-09 12:58:51 -060071VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060072{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060073
74}
75
Tony Barbour6918cd52015-04-09 12:58:51 -060076void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060077{
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060078 std::vector<const char*> instance_layer_names;
79 std::vector<const char*> device_layer_names;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060080 std::vector<const char*> instance_extension_names;
81 std::vector<const char*> device_extension_names;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060082 InitFramework(
83 instance_layer_names, device_layer_names,
84 instance_extension_names, device_extension_names);
Tony Barbour3fdff9e2015-04-23 12:55:36 -060085}
86
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060087void VkRenderFramework::InitFramework(
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -060088 std::vector<const char *> instance_layer_names,
89 std::vector<const char *> device_layer_names,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060090 std::vector<const char *> instance_extension_names,
91 std::vector<const char *> device_extension_names,
92 PFN_vkDbgMsgCallback dbgFunction,
93 void *userData)
Tony Barbour3fdff9e2015-04-23 12:55:36 -060094{
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -060095 VkInstanceCreateInfo instInfo = {};
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060096 std::vector<VkExtensionProperties> instance_extensions;
97 std::vector<VkExtensionProperties> device_extensions;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060098 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060099
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600100 /* TODO: Verify requested extensions are available */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600101
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600102 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -0600103 instInfo.pNext = NULL;
104 instInfo.pAppInfo = &app_info;
105 instInfo.pAllocCb = NULL;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600106 instInfo.layerCount = instance_layer_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600107 instInfo.ppEnabledLayerNames = instance_layer_names.data();
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600108 instInfo.extensionCount = instance_extension_names.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600109 instInfo.ppEnabledExtensionNames = instance_extension_names.data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600110 err = vkCreateInstance(&instInfo, &this->inst);
111 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600112
Jon Ashburn83a64252015-04-15 11:31:12 -0600113 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
114 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many gpus";
115 ASSERT_VK_SUCCESS(err);
116 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600117 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700118 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Tony Barbour15524c32015-04-29 17:34:29 -0600119 if (dbgFunction) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600120 m_dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgCreateMsgCallback");
121 ASSERT_NE(m_dbgCreateMsgCallback, (PFN_vkDbgCreateMsgCallback) NULL) << "Did not get function pointer for DbgCreateMsgCallback";
122 if (m_dbgCreateMsgCallback) {
123 err = m_dbgCreateMsgCallback(this->inst,
124 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
125 dbgFunction,
126 userData,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600127 &m_globalMsgCallback);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600128 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600129
130 m_dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(this->inst, "vkDbgDestroyMsgCallback");
131 ASSERT_NE(m_dbgDestroyMsgCallback, (PFN_vkDbgDestroyMsgCallback) NULL) << "Did not get function pointer for DbgDestroyMsgCallback";
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600132 }
Tony Barbour15524c32015-04-29 17:34:29 -0600133 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600134
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600135 /* TODO: Verify requested physical device extensions are available */
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600136 m_device = new VkDeviceObj(0, objs[0], device_layer_names, device_extension_names);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600137
138 /* Now register callback on device */
139 if (0) {
140 if (m_dbgCreateMsgCallback) {
141 err = m_dbgCreateMsgCallback(this->inst,
142 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
143 dbgFunction,
144 userData,
145 &m_devMsgCallback);
146 ASSERT_VK_SUCCESS(err);
147 }
148 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600149 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -0600150
Tony Barbour6918cd52015-04-09 12:58:51 -0600151 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600152}
153
Tony Barbour6918cd52015-04-09 12:58:51 -0600154void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600155{
Tony Barbour67e99152015-07-10 14:10:27 -0600156 if (m_colorBlend) vkDestroyDynamicColorBlendState(device(), m_colorBlend);
157 if (m_stateDepthStencil) vkDestroyDynamicDepthStencilState(device(), m_stateDepthStencil);
158 if (m_stateRaster) vkDestroyDynamicRasterState(device(), m_stateRaster);
Tony Barbourfe3351b2015-07-28 10:17:20 -0600159 if (m_cmdBuffer)
160 delete m_cmdBuffer;
Cody Northrope62183e2015-07-09 18:08:05 -0600161 if (m_cmdPool) vkDestroyCommandPool(device(), m_cmdPool);
Tony Barbour67e99152015-07-10 14:10:27 -0600162 if (m_framebuffer) vkDestroyFramebuffer(device(), m_framebuffer);
163 if (m_renderPass) vkDestroyRenderPass(device(), m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600164
Courtney Goeltzenleuchter7e46b622015-06-08 15:16:04 -0600165 if (m_globalMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_globalMsgCallback);
166 if (m_devMsgCallback) m_dbgDestroyMsgCallback(this->inst, m_devMsgCallback);
167
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600168 if (m_stateViewport) {
Tony Barbour67e99152015-07-10 14:10:27 -0600169 vkDestroyDynamicViewportState(device(), m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600170 }
Tobin Ehlis976fc162015-03-26 08:23:25 -0600171 while (!m_renderTargets.empty()) {
Tony Barbour6a3faf02015-07-23 10:36:18 -0600172 vkDestroyAttachmentView(device(), m_renderTargets.back()->targetView(m_render_target_fmt));
Tony Barbour67e99152015-07-10 14:10:27 -0600173 vkDestroyImage(device(), m_renderTargets.back()->image());
Mike Stroyanb050c682015-04-17 12:36:38 -0600174 vkFreeMemory(device(), m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -0600175 m_renderTargets.pop_back();
176 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600177
Tony Barbour1c45ce02015-03-27 17:03:18 -0600178 delete m_depthStencil;
Tony Barbour823e6ca2015-07-27 13:31:04 -0600179 while (!m_shader_modules.empty())
180 {
181 delete m_shader_modules.back();
182 m_shader_modules.pop_back();
183 }
Tony Barbour1c45ce02015-03-27 17:03:18 -0600184
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600185 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800186 delete m_device;
Chris Forbesbe2fae62015-07-07 11:02:22 +1200187 if (this->inst) vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600188}
189
Tony Barbour6918cd52015-04-09 12:58:51 -0600190void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600191{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600192 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600193
Tony Barbour6a3faf02015-07-23 10:36:18 -0600194 // Get the list of VkFormat's that are supported:
195 PFN_vkGetSurfaceInfoWSI fpGetSurfaceInfoWSI;
196 size_t formatsSize;
197 VkSurfaceDescriptionWSI surface_description;
198 surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
199 surface_description.pNext = NULL;
200 GET_DEVICE_PROC_ADDR(device(), GetSurfaceInfoWSI);
201 err = fpGetSurfaceInfoWSI( device(),
202 (VkSurfaceDescriptionWSI *) &surface_description,
203 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
204 &formatsSize, NULL);
205 ASSERT_VK_SUCCESS(err);
206 VkSurfaceFormatPropertiesWSI *surfFormats = (VkSurfaceFormatPropertiesWSI *)malloc(formatsSize);
207 err = fpGetSurfaceInfoWSI(device(),
208 (VkSurfaceDescriptionWSI *) &surface_description,
209 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
210 &formatsSize, surfFormats);
211 ASSERT_VK_SUCCESS(err);
212 m_render_target_fmt = surfFormats[0].format;
213 free(surfFormats);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600214
215 // create a raster state (solid, back-face culling)
Tony Barbour67e99152015-07-10 14:10:27 -0600216 VkDynamicRasterStateCreateInfo raster = {};
217 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700218
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600219 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
220 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600221
Tony Barbour67e99152015-07-10 14:10:27 -0600222 VkDynamicColorBlendStateCreateInfo blend = {};
223 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Tony Barbourd81b8882015-05-15 09:37:57 -0600224 blend.blendConst[0] = 1.0f;
225 blend.blendConst[1] = 1.0f;
226 blend.blendConst[2] = 1.0f;
227 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600228 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
229 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600230
Tony Barbour67e99152015-07-10 14:10:27 -0600231 VkDynamicDepthStencilStateCreateInfo depthStencil = {};
232 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski365feea2015-06-12 11:14:17 -0600233 depthStencil.minDepthBounds = 0.f;
234 depthStencil.maxDepthBounds = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700235 depthStencil.stencilFrontRef = 0;
236 depthStencil.stencilBackRef = 0;
Tony Barbourd81b8882015-05-15 09:37:57 -0600237 depthStencil.stencilReadMask = 0xff;
238 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600239
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600240 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
241 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600242
Cody Northrope62183e2015-07-09 18:08:05 -0600243 VkCmdPoolCreateInfo cmd_pool_info;
244 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
245 cmd_pool_info.pNext = NULL,
246 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
247 cmd_pool_info.flags = 0,
248 err = vkCreateCommandPool(device(), &cmd_pool_info, &m_cmdPool);
249 assert(!err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600250
Tony Barbourfe3351b2015-07-28 10:17:20 -0600251 m_cmdBuffer = new VkCommandBufferObj(m_device, m_cmdPool);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600252}
253
Tony Barbour6918cd52015-04-09 12:58:51 -0600254void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600255{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600256 VkResult err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600257
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600258 VkViewport viewport;
Chris Forbesd9be82b2015-06-22 17:21:59 +1200259 VkRect2D scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600260
Tony Barbour67e99152015-07-10 14:10:27 -0600261 VkDynamicViewportStateCreateInfo viewportCreate = {};
262 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700263 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700264 viewport.originX = 0;
265 viewport.originY = 0;
266 viewport.width = 1.f * width;
267 viewport.height = 1.f * height;
268 viewport.minDepth = 0.f;
269 viewport.maxDepth = 1.f;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600270 scissor.extent.width = (int32_t) width;
271 scissor.extent.height = (int32_t) height;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700272 scissor.offset.x = 0;
273 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700274 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700275 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700276
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600277 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
278 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600279 m_width = width;
280 m_height = height;
281}
282
Tony Barbour6918cd52015-04-09 12:58:51 -0600283void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600284{
285 InitViewport(m_width, m_height);
286}
Tony Barbour6918cd52015-04-09 12:58:51 -0600287void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600288{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700289 InitRenderTarget(1);
290}
291
Tony Barbour6918cd52015-04-09 12:58:51 -0600292void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700293{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600294 InitRenderTarget(targets, NULL);
295}
296
Cody Northrop3cc85e92015-08-04 10:47:08 -0600297void VkRenderFramework::InitRenderTarget(VkAttachmentView *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600298{
299 InitRenderTarget(1, dsBinding);
300}
301
Cody Northrop3cc85e92015-08-04 10:47:08 -0600302void VkRenderFramework::InitRenderTarget(uint32_t targets, VkAttachmentView *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600303{
Chia-I Wu08accc62015-07-07 11:50:03 +0800304 std::vector<VkAttachmentDescription> attachments;
305 std::vector<VkAttachmentReference> color_references;
Cody Northrop3cc85e92015-08-04 10:47:08 -0600306 std::vector<VkAttachmentView> bindings;
Chia-I Wu08accc62015-07-07 11:50:03 +0800307 attachments.reserve(targets + (bool) dsBinding);
308 color_references.reserve(targets);
309 bindings.reserve(targets + (bool) dsBinding);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600310
Chia-I Wu08accc62015-07-07 11:50:03 +0800311 VkAttachmentDescription att = {};
312 att.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION;
313 att.pNext = NULL;
314 att.format = m_render_target_fmt;
315 att.samples = 1;
316 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
317 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
318 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
319 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
320 att.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
321 att.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Chia-I Wuecebf752014-12-05 10:45:15 +0800322
Chia-I Wu08accc62015-07-07 11:50:03 +0800323 VkAttachmentReference ref = {};
324 ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
325
326 m_renderPassClearValues.clear();
327 VkClearValue clear = {};
328 clear.color = m_clear_color;
329
Cody Northrop3cc85e92015-08-04 10:47:08 -0600330 VkAttachmentView bind = {};
Chia-I Wu08accc62015-07-07 11:50:03 +0800331
332 for (uint32_t i = 0; i < targets; i++) {
333 attachments.push_back(att);
334
335 ref.attachment = i;
336 color_references.push_back(ref);
337
338 m_renderPassClearValues.push_back(clear);
339
Tony Barbour6918cd52015-04-09 12:58:51 -0600340 VkImageObj *img = new VkImageObj(m_device);
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600341
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600342 VkFormatProperties props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600343 VkResult err;
344
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600345 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), m_render_target_fmt, &props);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600346 ASSERT_VK_SUCCESS(err);
347
348 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600349 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600350 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
351 VK_IMAGE_TILING_LINEAR);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600352 }
353 else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600354 img->init((uint32_t)m_width, (uint32_t)m_height, m_render_target_fmt,
Tony Barboure65788f2015-07-21 17:01:42 -0600355 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
356 VK_IMAGE_TILING_OPTIMAL);
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600357 }
358 else {
359 FAIL() << "Neither Linear nor Optimal allowed for render target";
360 }
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600361
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600362 m_renderTargets.push_back(img);
Tony Barbour6a3faf02015-07-23 10:36:18 -0600363 bind = img->targetView(m_render_target_fmt);
Chia-I Wu08accc62015-07-07 11:50:03 +0800364 bindings.push_back(bind);
Chia-I Wuecebf752014-12-05 10:45:15 +0800365 }
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600366
Chia-I Wu08accc62015-07-07 11:50:03 +0800367 VkSubpassDescription subpass = {};
368 subpass.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION;
369 subpass.pNext = NULL;
370 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
371 subpass.flags = 0;
372 subpass.inputCount = 0;
Cody Northropa505dda2015-08-04 11:16:41 -0600373 subpass.pInputAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800374 subpass.colorCount = targets;
Cody Northropa505dda2015-08-04 11:16:41 -0600375 subpass.pColorAttachments = color_references.data();
376 subpass.pResolveAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800377
378 if (dsBinding) {
379 att.format = m_depth_stencil_fmt;
Tony Barbour71bd4b32015-07-21 17:00:26 -0600380 att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;;
Chia-I Wu08accc62015-07-07 11:50:03 +0800381 att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
382 att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
383 att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
Chia-I Wu08accc62015-07-07 11:50:03 +0800384 attachments.push_back(att);
385
386 clear.ds.depth = m_depth_clear_color;
387 clear.ds.stencil = m_stencil_clear_color;
388 m_renderPassClearValues.push_back(clear);
389
390 bindings.push_back(*dsBinding);
391
392 subpass.depthStencilAttachment.attachment = targets;
Chia-I Wu08accc62015-07-07 11:50:03 +0800393 } else {
394 subpass.depthStencilAttachment.attachment = VK_ATTACHMENT_UNUSED;
395 }
396
397 subpass.preserveCount = 0;
Cody Northropa505dda2015-08-04 11:16:41 -0600398 subpass.pPreserveAttachments = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800399
400 VkRenderPassCreateInfo rp_info = {};
401 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
402 rp_info.attachmentCount = attachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600403 rp_info.pAttachments = attachments.data();
Chia-I Wu08accc62015-07-07 11:50:03 +0800404 rp_info.subpassCount = 1;
405 rp_info.pSubpasses = &subpass;
406
407 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
408
409 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600410 VkFramebufferCreateInfo fb_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600411 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600412 fb_info.pNext = NULL;
Chia-I Wu08accc62015-07-07 11:50:03 +0800413 fb_info.renderPass = m_renderPass;
414 fb_info.attachmentCount = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600415 fb_info.pAttachments = bindings.data();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600416 fb_info.width = (uint32_t)m_width;
417 fb_info.height = (uint32_t)m_height;
418 fb_info.layers = 1;
419
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600420 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600421
Chia-I Wu08accc62015-07-07 11:50:03 +0800422 m_renderPassBeginInfo.renderPass = m_renderPass;
423 m_renderPassBeginInfo.framebuffer = m_framebuffer;
424 m_renderPassBeginInfo.renderArea.extent.width = m_width;
425 m_renderPassBeginInfo.renderArea.extent.height = m_height;
Cody Northrop23dd89d2015-08-04 11:51:03 -0600426 m_renderPassBeginInfo.clearValueCount = m_renderPassClearValues.size();
427 m_renderPassBeginInfo.pClearValues = m_renderPassClearValues.data();
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600428}
429
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600430
431
Tony Barbourd1c35722015-04-16 15:59:00 -0600432VkDeviceObj::VkDeviceObj(uint32_t id, VkPhysicalDevice obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600433 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800434{
435 init();
436
Chia-I Wu999f0482015-07-03 10:32:05 +0800437 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600438 queue_props = phy().queue_properties().data();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800439}
440
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600441VkDeviceObj::VkDeviceObj(uint32_t id,
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600442 VkPhysicalDevice obj, std::vector<const char *> &layer_names,
443 std::vector<const char *> &extension_names) :
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600444 vk_testing::Device(obj), id(id)
445{
Courtney Goeltzenleuchter5bac6092015-07-07 11:47:33 -0600446 init(layer_names, extension_names);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600447
Chia-I Wu999f0482015-07-03 10:32:05 +0800448 props = phy().properties();
Tony Barbour482c6092015-07-27 09:37:48 -0600449 queue_props = phy().queue_properties().data();
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600450}
451
Tony Barbour6918cd52015-04-09 12:58:51 -0600452void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800453{
454 ASSERT_NE(true, graphics_queues().empty());
Chia-I Wudf12ffd2015-07-03 10:53:18 +0800455 m_queue = graphics_queues()[0]->handle();
Chia-I Wufb1459b2014-12-29 15:23:20 +0800456}
457
Tobin Ehlis3ab1f872015-05-27 14:33:27 -0600458VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device) :
459 m_device(device), m_nextSlot(0)
Tony Barboure2c58df2014-11-25 13:18:32 -0700460{
Tony Barboure2c58df2014-11-25 13:18:32 -0700461
462}
463
Tony Barbour6918cd52015-04-09 12:58:51 -0600464VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700465{
Chia-I Wu11078b02015-01-04 16:27:24 +0800466 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700467}
Tony Barbour82c39522014-12-04 14:33:33 -0700468
Tony Barbour6918cd52015-04-09 12:58:51 -0600469int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700470{
Chia-I Wu11078b02015-01-04 16:27:24 +0800471 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600472 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600473 tc.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800474 tc.count = 1;
475 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700476
Chia-I Wu11078b02015-01-04 16:27:24 +0800477 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700478}
Tony Barbour82c39522014-12-04 14:33:33 -0700479
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600480int VkDescriptorSetObj::AppendBuffer(VkDescriptorType type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700481{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600482 VkDescriptorTypeCount tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800483 tc.type = type;
484 tc.count = 1;
485 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700486
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800487 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
488 m_nextSlot, 0, type, 1, &constantBuffer.m_descriptorInfo));
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600489
Chia-I Wu11078b02015-01-04 16:27:24 +0800490 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700491}
Tony Barbour82c39522014-12-04 14:33:33 -0700492
Tony Barbour6918cd52015-04-09 12:58:51 -0600493int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700494{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600495 VkDescriptorTypeCount tc = {};
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600496 tc.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800497 tc.count = 1;
498 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700499
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800500 VkDescriptorInfo tmp = texture->m_descriptorInfo;
Chia-I Wu8c721c62015-07-03 11:49:42 +0800501 tmp.sampler = sampler->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800502 m_imageSamplerDescriptors.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700503
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800504 m_writes.push_back(vk_testing::Device::write_descriptor_set(vk_testing::DescriptorSet(),
505 m_nextSlot, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700506
Chia-I Wu11078b02015-01-04 16:27:24 +0800507 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700508}
Chia-I Wu11078b02015-01-04 16:27:24 +0800509
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500510VkPipelineLayout VkDescriptorSetObj::GetPipelineLayout() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700511{
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800512 return m_pipeline_layout.handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700513}
514
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600515VkDescriptorSet VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700516{
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800517 return m_set->handle();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700518}
Tony Barboure2c58df2014-11-25 13:18:32 -0700519
Tony Barbour6918cd52015-04-09 12:58:51 -0600520void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700521{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600522 // create VkDescriptorPool
523 VkDescriptorPoolCreateInfo pool = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600524 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800525 pool.count = m_type_counts.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600526 pool.pTypeCount = m_type_counts.data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600527 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700528
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600529 // create VkDescriptorSetLayout
530 vector<VkDescriptorSetLayoutBinding> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800531 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800532 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800533 bindings[i].descriptorType = m_type_counts[i].type;
Chia-I Wu712bb5d2015-05-25 16:22:52 +0800534 bindings[i].arraySize = m_type_counts[i].count;
Tony Barbourd1c35722015-04-16 15:59:00 -0600535 bindings[i].stageFlags = VK_SHADER_STAGE_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800536 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700537 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800538
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600539 // create VkDescriptorSetLayout
540 VkDescriptorSetLayoutCreateInfo layout = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600541 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800542 layout.count = bindings.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600543 layout.pBinding = bindings.data();
Chia-I Wub41393e2015-03-26 15:04:41 +0800544
Chia-I Wu41126e52015-03-26 15:27:55 +0800545 m_layout.init(*m_device, layout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600546 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800547 layouts.push_back(&m_layout);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500548
549 // create VkPipelineLayout
550 VkPipelineLayoutCreateInfo pipeline_layout = {};
551 pipeline_layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
552 pipeline_layout.descriptorSetCount = layouts.size();
553 pipeline_layout.pSetLayouts = NULL;
554
555 m_pipeline_layout.init(*m_device, pipeline_layout, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700556
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600557 // create VkDescriptorSet
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500558 m_set = alloc_sets(*m_device, VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800559
Chia-I Wu41126e52015-03-26 15:27:55 +0800560 // build the update array
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800561 size_t imageSamplerCount = 0;
562 for (std::vector<VkWriteDescriptorSet>::iterator it = m_writes.begin();
563 it != m_writes.end(); it++) {
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800564 it->destSet = m_set->handle();
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800565 if (it->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
566 it->pDescriptors = &m_imageSamplerDescriptors[imageSamplerCount++];
Chia-I Wu11078b02015-01-04 16:27:24 +0800567 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800568
569 // do the updates
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800570 m_device->update_descriptor_sets(m_writes);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700571}
Tony Barboure2c58df2014-11-25 13:18:32 -0700572
Tony Barbour6918cd52015-04-09 12:58:51 -0600573VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800574{
575 m_device = dev;
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800576 m_descriptorInfo.imageView = VK_NULL_HANDLE;
577 m_descriptorInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800578}
579
Tony Barbour6918cd52015-04-09 12:58:51 -0600580void VkImageObj::ImageMemoryBarrier(
581 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600582 VkImageAspect aspect,
583 VkFlags output_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600584 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600585 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
586 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
587 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
588 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600589 VkFlags input_mask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600590 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600591 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
592 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
593 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
594 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
595 VK_MEMORY_INPUT_SHADER_READ_BIT |
596 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
597 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
598 VK_MEMORY_INPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600599 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600600{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600601 const VkImageSubresourceRange subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
602 VkImageMemoryBarrier barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600603 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
604 subresourceRange);
605
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600606 VkImageMemoryBarrier *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600607
Tony Barbour0b2cfb22015-06-29 16:20:35 -0600608 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
609 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600610
611 // write barrier to the command buffer
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -0600612 vkCmdPipelineBarrier(cmd_buf->handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600613}
614
Tony Barbour6918cd52015-04-09 12:58:51 -0600615void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600616 VkImageAspect aspect,
617 VkImageLayout image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600618{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600619 VkFlags output_mask, input_mask;
620 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600621 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600622 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
623 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
624 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600625 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600626 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600627 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600628 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
629 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
630 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
631 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
632 VK_MEMORY_INPUT_SHADER_READ_BIT |
633 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
634 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600635 VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600636
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800637 if (image_layout == m_descriptorInfo.imageLayout) {
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600638 return;
639 }
640
641 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600642 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600643 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
644 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600645 break;
646
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600647 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600648 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
649 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600650 break;
651
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600652 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600653 output_mask = VK_MEMORY_OUTPUT_TRANSFER_BIT;
654 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600655 break;
656
657 default:
658 output_mask = all_cache_outputs;
659 input_mask = all_cache_inputs;
660 break;
661 }
662
663 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800664 m_descriptorInfo.imageLayout = image_layout;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600665}
666
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600667void VkImageObj::SetLayout(VkImageAspect aspect,
668 VkImageLayout image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600669{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600670 VkResult U_ASSERT_ONLY err;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600671
672 if (image_layout == m_descriptorInfo.imageLayout) {
673 return;
674 }
675
Tony Barbourfe3351b2015-07-28 10:17:20 -0600676 VkCmdPoolCreateInfo cmd_pool_info = {};
677 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
678 cmd_pool_info.pNext = NULL;
679 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
680 cmd_pool_info.flags = 0;
681 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
682 VkCommandBufferObj cmd_buf(m_device, pool.handle());
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600683
684 /* Build command buffer to set image layout in the driver */
685 err = cmd_buf.BeginCommandBuffer();
686 assert(!err);
687
688 SetLayout(&cmd_buf, aspect, image_layout);
689
690 err = cmd_buf.EndCommandBuffer();
691 assert(!err);
692
693 cmd_buf.QueueCommandBuffer();
694}
695
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600696bool VkImageObj::IsCompatible(VkFlags usage, VkFlags features)
Tony Barbour579f7802015-04-03 15:11:43 -0600697{
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600698 if ((usage & VK_IMAGE_USAGE_SAMPLED_BIT) &&
Tony Barbourd1c35722015-04-16 15:59:00 -0600699 !(features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600700 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600701
Tony Barbour579f7802015-04-03 15:11:43 -0600702 return true;
703}
704
Tony Barbour6918cd52015-04-09 12:58:51 -0600705void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600706 VkFormat fmt, VkFlags usage,
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600707 VkImageTiling requested_tiling,
708 VkMemoryPropertyFlags reqs)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800709{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600710 uint32_t mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600711 VkFormatProperties image_fmt;
712 VkImageTiling tiling;
713 VkResult err;
Tony Barbour579f7802015-04-03 15:11:43 -0600714
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800715 mipCount = 0;
716
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600717 uint32_t _w = w;
718 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800719 while( ( _w > 0 ) || ( _h > 0 ) )
720 {
721 _w >>= 1;
722 _h >>= 1;
723 mipCount++;
724 }
725
Courtney Goeltzenleuchter2caec862015-07-12 12:52:09 -0600726 err = vkGetPhysicalDeviceFormatProperties(m_device->phy().handle(), fmt, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600727 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600728
Tony Barbourd1c35722015-04-16 15:59:00 -0600729 if (requested_tiling == VK_IMAGE_TILING_LINEAR) {
Tony Barbour579f7802015-04-03 15:11:43 -0600730 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600731 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600732 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600733 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600734 } else {
735 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
736 }
737 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600738 tiling = VK_IMAGE_TILING_OPTIMAL;
Tony Barbour579f7802015-04-03 15:11:43 -0600739 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600740 tiling = VK_IMAGE_TILING_LINEAR;
Tony Barbour579f7802015-04-03 15:11:43 -0600741 } else {
742 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
743 }
744
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600745 VkImageCreateInfo imageCreateInfo = vk_testing::Image::create_info();
Tony Barbourd1c35722015-04-16 15:59:00 -0600746 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800747 imageCreateInfo.format = fmt;
748 imageCreateInfo.extent.width = w;
749 imageCreateInfo.extent.height = h;
750 imageCreateInfo.mipLevels = mipCount;
751 imageCreateInfo.tiling = tiling;
752
753 imageCreateInfo.usage = usage;
754
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600755 vk_testing::Image::init(*m_device, imageCreateInfo, reqs);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800756
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -0600757 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600758 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600759 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600760 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600761 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800762}
763
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600764VkResult VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600765{
Tony Barbourf20f87b2015-04-22 09:02:32 -0600766 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600767 VkImageLayout src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600768
Tony Barbourfe3351b2015-07-28 10:17:20 -0600769 VkCmdPoolCreateInfo cmd_pool_info = {};
770 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
771 cmd_pool_info.pNext = NULL;
772 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
773 cmd_pool_info.flags = 0;
774 vk_testing::CmdPool pool(*m_device, cmd_pool_info);
775 VkCommandBufferObj cmd_buf(m_device, pool.handle());
776
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600777 /* Build command buffer to copy staging texture to usable texture */
778 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600779 assert(!err);
780
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600781 /* TODO: Can we determine image aspect from image object? */
782 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600783 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600784
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600785 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600786 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600787
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600788 VkImageCopy copy_region = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600789 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600790 copy_region.srcSubresource.arraySlice = 0;
791 copy_region.srcSubresource.mipLevel = 0;
792 copy_region.srcOffset.x = 0;
793 copy_region.srcOffset.y = 0;
794 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600795 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600796 copy_region.destSubresource.arraySlice = 0;
797 copy_region.destSubresource.mipLevel = 0;
798 copy_region.destOffset.x = 0;
799 copy_region.destOffset.y = 0;
800 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600801 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600802
Chia-I Wube2b9172015-07-03 11:49:42 +0800803 vkCmdCopyImage(cmd_buf.handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +0800804 src_image.handle(), src_image.layout(),
805 handle(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600806 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600807
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600808 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600809
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600810 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600811
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600812 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600813 assert(!err);
814
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600815 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600816
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600817 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600818}
819
Tony Barbour6918cd52015-04-09 12:58:51 -0600820VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
821 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700822{
823 m_device = device;
Tony Barbourd1c35722015-04-16 15:59:00 -0600824 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600825 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600826 void *data;
827 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600828 VkImageObj stagingImage(device);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600829 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600830
Tony Barboure65788f2015-07-21 17:01:42 -0600831 stagingImage.init(16, 16, tex_format, VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_IMAGE_TILING_LINEAR, reqs);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600832 VkSubresourceLayout layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600833
834 if (colors == NULL)
835 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700836
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800837 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700838
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600839 VkImageViewCreateInfo view = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600840 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600841 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600842 view.image = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -0600843 view.viewType = VK_IMAGE_VIEW_TYPE_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600844 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600845 view.channels.r = VK_CHANNEL_SWIZZLE_R;
846 view.channels.g = VK_CHANNEL_SWIZZLE_G;
847 view.channels.b = VK_CHANNEL_SWIZZLE_B;
848 view.channels.a = VK_CHANNEL_SWIZZLE_A;
849 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600850 view.subresourceRange.baseMipLevel = 0;
851 view.subresourceRange.mipLevels = 1;
852 view.subresourceRange.baseArraySlice = 0;
853 view.subresourceRange.arraySize = 1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700854
Tony Barboure2c58df2014-11-25 13:18:32 -0700855 /* create image */
Tony Barboure65788f2015-07-21 17:01:42 -0600856 init(16, 16, tex_format, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT, VK_IMAGE_TILING_OPTIMAL);
Tony Barboure2c58df2014-11-25 13:18:32 -0700857
858 /* create image view */
Chia-I Wu681d7a02015-07-03 13:44:34 +0800859 view.image = handle();
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800860 m_textureView.init(*m_device, view);
Chia-I Wu3158bf32015-07-03 11:49:42 +0800861 m_descriptorInfo.imageView = m_textureView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700862
Chia-I Wu681d7a02015-07-03 13:44:34 +0800863 data = stagingImage.MapMemory();
Tony Barboure2c58df2014-11-25 13:18:32 -0700864
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800865 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700866 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800867 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600868 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700869 }
Chia-I Wu681d7a02015-07-03 13:44:34 +0800870 stagingImage.UnmapMemory();
Tony Barbour6918cd52015-04-09 12:58:51 -0600871 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700872}
Tony Barbour82c39522014-12-04 14:33:33 -0700873
Tony Barbour6918cd52015-04-09 12:58:51 -0600874VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700875{
Tony Barboure2c58df2014-11-25 13:18:32 -0700876 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700877
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600878 VkSamplerCreateInfo samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800879 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600880 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
881 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
882 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
Tony Barbourd1c35722015-04-16 15:59:00 -0600883 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_MODE_BASE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600884 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
885 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
886 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800887 samplerCreateInfo.mipLodBias = 0.0;
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600888 samplerCreateInfo.maxAnisotropy = 0;
Tony Barbourd1c35722015-04-16 15:59:00 -0600889 samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800890 samplerCreateInfo.minLod = 0.0;
891 samplerCreateInfo.maxLod = 0.0;
Tony Barbour26b17f82015-06-25 16:56:44 -0600892 samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700893
Chia-I Wue9864b52014-12-28 16:32:24 +0800894 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700895}
Tony Barboure2c58df2014-11-25 13:18:32 -0700896
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700897/*
898 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
899 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600900VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700901{
902 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700903 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700904
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800905 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700906}
907
Tony Barbour6918cd52015-04-09 12:58:51 -0600908VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600909{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600910 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600911 if (m_commandBuffer) {
912 delete m_commandBuffer;
Tony Barbour3d83f492015-07-28 10:23:02 -0600913 delete m_cmdPool;
Tony Barbour044703b2015-03-26 12:37:52 -0600914 }
915}
916
Tony Barbour6918cd52015-04-09 12:58:51 -0600917VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700918{
Tony Barboure2c58df2014-11-25 13:18:32 -0700919 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800920 m_commandBuffer = 0;
921
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800922 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700923 m_numVertices = constantCount;
924 m_stride = constantSize;
925
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600926 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wua07fee62014-12-28 15:26:08 +0800927 const size_t allocationSize = constantCount * constantSize;
Cody Northrop7fb43862015-06-22 14:56:14 -0600928 init_as_src_and_dst(*m_device, allocationSize, reqs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700929
Chia-I Wu681d7a02015-07-03 13:44:34 +0800930 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +0800931 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800932 memory().unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700933
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800934 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600935 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600936 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu681d7a02015-07-03 13:44:34 +0800937 view_info.buffer = handle();
Tony Barbourd1c35722015-04-16 15:59:00 -0600938 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800939 view_info.offset = 0;
940 view_info.range = allocationSize;
941 m_bufferView.init(*m_device, view_info);
942
Chia-I Wu3158bf32015-07-03 11:49:42 +0800943 this->m_descriptorInfo.bufferView = m_bufferView.handle();
Tony Barboure2c58df2014-11-25 13:18:32 -0700944}
Tony Barbour82c39522014-12-04 14:33:33 -0700945
Tony Barbourd1c35722015-04-16 15:59:00 -0600946void VkConstantBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700947{
Chia-I Wu681d7a02015-07-03 13:44:34 +0800948 vkCmdBindVertexBuffers(cmdBuffer, binding, 1, &handle(), &offset);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700949}
950
951
Tony Barbour6918cd52015-04-09 12:58:51 -0600952void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600953 VkFlags outputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600954 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600955 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
956 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
957 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
958 VK_MEMORY_OUTPUT_COPY_BIT*/,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600959 VkFlags inputMask /*=
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -0600960 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600961 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
962 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
963 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
964 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
965 VK_MEMORY_INPUT_SHADER_READ_BIT |
966 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
967 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
968 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700969{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600970 VkResult err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700971
Tony Barbour38422802014-12-10 14:36:31 -0700972 if (!m_commandBuffer)
973 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600974 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbourfe3351b2015-07-28 10:17:20 -0600975 VkCmdPoolCreateInfo cmd_pool_info = {};
976 cmd_pool_info.sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO;
977 cmd_pool_info.pNext = NULL;
978 cmd_pool_info.queueFamilyIndex = m_device->graphics_queue_node_index_;
979 cmd_pool_info.flags = 0;
980 m_cmdPool = new vk_testing::CmdPool(*m_device, cmd_pool_info);
981 m_commandBuffer = new VkCommandBufferObj(m_device, m_cmdPool->handle());
Tony Barbour38422802014-12-10 14:36:31 -0700982 }
983 else
984 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800985 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700986 }
987
Tony Barboure2c58df2014-11-25 13:18:32 -0700988 // open the command buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600989 VkCmdBufferBeginInfo cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600990 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600991 cmd_buf_info.pNext = NULL;
992 cmd_buf_info.flags = 0;
993
Jon Ashburnc4164b12014-12-31 17:10:47 -0700994 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600995 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700996
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600997 VkBufferMemoryBarrier memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000998 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600999 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -07001000
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001001 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1002 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001003
1004 // write barrier to the command buffer
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001005 m_commandBuffer->PipelineBarrier(src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -07001006
1007 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -07001008 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001009 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001010
Tony Barboure2c58df2014-11-25 13:18:32 -07001011 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001012 VkCmdBuffer bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -07001013 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wud9e8e822015-07-03 11:45:55 +08001014 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.handle() );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001015 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001016}
1017
Tony Barbour6918cd52015-04-09 12:58:51 -06001018VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
1019 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001020{
1021
1022}
1023
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001024void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VkIndexType indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001025{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001026 VkFormat viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001027
1028 m_numVertices = numIndexes;
1029 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001030 switch (indexType) {
Tony Barbourd1c35722015-04-16 15:59:00 -06001031 case VK_INDEX_TYPE_UINT16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001032 m_stride = 2;
Tony Barbourd1c35722015-04-16 15:59:00 -06001033 viewFormat = VK_FORMAT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001034 break;
Tony Barbourd1c35722015-04-16 15:59:00 -06001035 case VK_INDEX_TYPE_UINT32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001036 m_stride = 4;
Tony Barbourd1c35722015-04-16 15:59:00 -06001037 viewFormat = VK_FORMAT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001038 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001039 default:
1040 assert(!"unknown index type");
Tony Barbourf20f87b2015-04-22 09:02:32 -06001041 m_stride = 2;
1042 viewFormat = VK_FORMAT_R16_UINT;
Chia-I Wub4c2aa42014-12-15 23:50:11 +08001043 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001044 }
1045
Chia-I Wua07fee62014-12-28 15:26:08 +08001046 const size_t allocationSize = numIndexes * m_stride;
Tony Barbour4c97d7a2015-04-22 15:10:33 -06001047 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northrop7fb43862015-06-22 14:56:14 -06001048 init_as_src_and_dst(*m_device, allocationSize, reqs);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001049
Chia-I Wu681d7a02015-07-03 13:44:34 +08001050 void *pData = memory().map();
Chia-I Wua07fee62014-12-28 15:26:08 +08001051 memcpy(pData, data, allocationSize);
Chia-I Wu681d7a02015-07-03 13:44:34 +08001052 memory().unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001053
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001054 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001055 VkBufferViewCreateInfo view_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001056 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001057 view_info.buffer = handle();
Tony Barbourd1c35722015-04-16 15:59:00 -06001058 view_info.viewType = VK_BUFFER_VIEW_TYPE_FORMATTED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001059 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001060 view_info.offset = 0;
1061 view_info.range = allocationSize;
1062 m_bufferView.init(*m_device, view_info);
1063
Chia-I Wu3158bf32015-07-03 11:49:42 +08001064 this->m_descriptorInfo.bufferView = m_bufferView.handle();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001065}
1066
Tony Barbourd1c35722015-04-16 15:59:00 -06001067void VkIndexBufferObj::Bind(VkCmdBuffer cmdBuffer, VkDeviceSize offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001068{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001069 vkCmdBindIndexBuffer(cmdBuffer, handle(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -07001070}
Tony Barboure2c58df2014-11-25 13:18:32 -07001071
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001072VkIndexType VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -07001073{
1074 return m_indexType;
1075}
1076
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001077VkPipelineShaderStageCreateInfo* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -07001078{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001079 VkPipelineShaderStageCreateInfo *stageInfo = (VkPipelineShaderStageCreateInfo*) calloc( 1,sizeof(VkPipelineShaderStageCreateInfo) );
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001080 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1081 stageInfo->stage = m_stage;
Chia-I Wu4d0c7922015-07-03 11:49:42 +08001082 stageInfo->shader = handle();
Tony Barboure2c58df2014-11-25 13:18:32 -07001083
Tony Barboure2c58df2014-11-25 13:18:32 -07001084 return stageInfo;
1085}
1086
Tony Barbourd1c35722015-04-16 15:59:00 -06001087VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VkShaderStage stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -07001088{
Tony Barbourb5d2c942015-07-14 13:34:05 -06001089 VkResult U_ASSERT_ONLY err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001090 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001091 VkShaderCreateInfo createInfo;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001092 VkShaderModuleCreateInfo moduleCreateInfo;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001093 vk_testing::ShaderModule *module = new vk_testing::ShaderModule();
Tony Barboure2c58df2014-11-25 13:18:32 -07001094 size_t shader_len;
1095
1096 m_stage = stage;
1097 m_device = device;
1098
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001099 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1100 moduleCreateInfo.pNext = NULL;
1101
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001102 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001103 createInfo.pNext = NULL;
1104
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001105 if (framework->m_use_glsl) {
Tony Barboure2c58df2014-11-25 13:18:32 -07001106
1107 shader_len = strlen(shader_code);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001108 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
1109 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1110 moduleCreateInfo.flags = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001111
Tony Barbourd1c35722015-04-16 15:59:00 -06001112 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001113 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1114 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1115 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1116 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), shader_code, shader_len + 1);
Tony Barboure2c58df2014-11-25 13:18:32 -07001117
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001118 } else {
Tony Barboure2c58df2014-11-25 13:18:32 -07001119
Cody Northrop3bfd27c2015-03-17 15:55:58 -06001120 // Use Reference GLSL to SPV compiler
1121 framework->GLSLtoSPV(stage, shader_code, spv);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001122 moduleCreateInfo.pCode = spv.data();
1123 moduleCreateInfo.codeSize = spv.size() * sizeof(unsigned int);
1124 moduleCreateInfo.flags = 0;
Chia-I Wubabc0fd2014-12-29 14:14:03 +08001125 }
Cody Northrop475663c2015-04-15 11:19:06 -06001126
Tony Barbour823e6ca2015-07-27 13:31:04 -06001127 err = module->init_try(*m_device, moduleCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001128 assert(VK_SUCCESS == err);
Cody Northrop50a2a4b2015-06-03 16:49:20 -06001129
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001130 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1131 createInfo.pNext = NULL;
Tony Barbour823e6ca2015-07-27 13:31:04 -06001132 createInfo.module = module->handle();
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001133 createInfo.pName = "main";
1134 createInfo.flags = 0;
1135
1136 err = init_try(*m_device, createInfo);
Cody Northrop475663c2015-04-15 11:19:06 -06001137 assert(VK_SUCCESS == err);
Tony Barbour823e6ca2015-07-27 13:31:04 -06001138 framework->m_shader_modules.push_back(module);
Tony Barbourf325bf12014-12-03 15:59:38 -07001139}
Tony Barbour82c39522014-12-04 14:33:33 -07001140
Tony Barbour6918cd52015-04-09 12:58:51 -06001141VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -07001142{
Tony Barboure2c58df2014-11-25 13:18:32 -07001143 m_device = device;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001144
1145 m_vi_state.pNext = VK_NULL_HANDLE;
1146 m_vi_state.bindingCount = 0;
1147 m_vi_state.pVertexBindingDescriptions = VK_NULL_HANDLE;
1148 m_vi_state.attributeCount = 0;
1149 m_vi_state.pVertexAttributeDescriptions = VK_NULL_HANDLE;
1150
Tony Barboure2c58df2014-11-25 13:18:32 -07001151 m_vertexBufferCount = 0;
1152
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001153 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001154 m_ia_state.pNext = VK_NULL_HANDLE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001155 m_ia_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001156 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001157
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001158 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001159 m_rs_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001160 m_rs_state.depthClipEnable = VK_FALSE;
1161 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001162 m_rs_state.fillMode = VK_FILL_MODE_SOLID;
Courtney Goeltzenleuchter43fb2c72015-04-30 17:44:12 -06001163 m_rs_state.cullMode = VK_CULL_MODE_BACK;
1164 m_rs_state.frontFace = VK_FRONT_FACE_CW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001165
Tony Barboure2c58df2014-11-25 13:18:32 -07001166 memset(&m_cb_state,0,sizeof(m_cb_state));
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001167 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001168 m_cb_state.pNext = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001169 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1170 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001171
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001172 m_ms_state.pNext = VK_NULL_HANDLE;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001173 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop02e61ed2015-08-04 14:34:54 -06001174 m_ms_state.pSampleMask = NULL;
Tony Barbourdfd533a2015-06-26 10:18:34 -06001175 m_ms_state.rasterSamples = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -07001176 m_ms_state.minSampleShading = 0;
1177 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001178
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001179 m_vp_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001180 m_vp_state.pNext = VK_NULL_HANDLE;
Tony Barbourd81b8882015-05-15 09:37:57 -06001181 m_vp_state.viewportCount = 1;
Tony Barbourd81b8882015-05-15 09:37:57 -06001182
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001183 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001184 m_ds_state.pNext = VK_NULL_HANDLE,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001185 m_ds_state.depthTestEnable = VK_FALSE;
1186 m_ds_state.depthWriteEnable = VK_FALSE;
1187 m_ds_state.depthBoundsEnable = VK_FALSE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001188 m_ds_state.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001189 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1190 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1191 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbourd1c35722015-04-16 15:59:00 -06001192 m_ds_state.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001193 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001194 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001195};
1196
Tony Barbour6918cd52015-04-09 12:58:51 -06001197void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001198{
1199 m_shaderObjs.push_back(shader);
1200}
1201
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001202void VkPipelineObj::AddVertexInputAttribs(VkVertexInputAttributeDescription* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001203{
1204 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1205 m_vi_state.attributeCount = count;
1206}
1207
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001208void VkPipelineObj::AddVertexInputBindings(VkVertexInputBindingDescription* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001209{
1210 m_vi_state.pVertexBindingDescriptions = vi_binding;
1211 m_vi_state.bindingCount = count;
1212}
1213
Tony Barbour6918cd52015-04-09 12:58:51 -06001214void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001215{
1216 m_vertexBufferObjs.push_back(vertexDataBuffer);
1217 m_vertexBufferBindings.push_back(binding);
1218 m_vertexBufferCount++;
1219}
1220
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001221void VkPipelineObj::AddColorAttachment(uint32_t binding, const VkPipelineColorBlendAttachmentState *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001222{
Tony Barbourf52346d2015-01-16 14:27:35 -07001223 if (binding+1 > m_colorAttachments.size())
1224 {
1225 m_colorAttachments.resize(binding+1);
1226 }
1227 m_colorAttachments[binding] = *att;
1228}
1229
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001230void VkPipelineObj::SetDepthStencil(VkPipelineDepthStencilStateCreateInfo *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001231{
Tony Barbourf52346d2015-01-16 14:27:35 -07001232 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1233 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1234 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
Tony Barbourd1c35722015-04-16 15:59:00 -06001235 m_ds_state.depthCompareOp = ds_state->depthCompareOp;
Tony Barbourf52346d2015-01-16 14:27:35 -07001236 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1237 m_ds_state.back = ds_state->back;
1238 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001239}
1240
Tony Barbour0ec8cd52015-08-06 09:27:11 -06001241void VkPipelineObj::SetMSAA(VkPipelineMultisampleStateCreateInfo *ms_state)
1242{
1243 memcpy(&m_ms_state, ms_state, sizeof(VkPipelineMultisampleStateCreateInfo));
1244}
1245
Tony Barbour5781e8f2015-08-04 16:23:11 -06001246VkResult VkPipelineObj::CreateVKPipeline(VkPipelineLayout layout, VkRenderPass render_pass)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001247{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001248 VkGraphicsPipelineCreateInfo info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001249
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001250 VkPipelineShaderStageCreateInfo* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001251
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001252 info.stageCount = m_shaderObjs.size();
1253 info.pStages = new VkPipelineShaderStageCreateInfo[info.stageCount];
1254
Tony Barbour976e1cf2014-12-17 11:57:31 -07001255 for (int i=0; i<m_shaderObjs.size(); i++)
1256 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001257 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001258 memcpy((void*)&info.pStages[i], shaderCreateInfo, sizeof(VkPipelineShaderStageCreateInfo));
Tony Barbour976e1cf2014-12-17 11:57:31 -07001259 }
1260
Tony Barboure815fd72015-07-27 09:36:24 -06001261 if (m_vi_state.attributeCount && m_vi_state.bindingCount) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001262 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Tony Barboure815fd72015-07-27 09:36:24 -06001263 info.pVertexInputState = &m_vi_state;
1264 } else {
1265 info.pVertexInputState = NULL;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001266 }
1267
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001268 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001269 info.pNext = NULL;
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -05001270 info.flags = 0;
Tony Barbour5781e8f2015-08-04 16:23:11 -06001271 info.layout = layout;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001272
Tony Barbourf52346d2015-01-16 14:27:35 -07001273 m_cb_state.attachmentCount = m_colorAttachments.size();
Tony Barbour482c6092015-07-27 09:37:48 -06001274 m_cb_state.pAttachments = m_colorAttachments.data();
Tony Barbourf52346d2015-01-16 14:27:35 -07001275
Chia-I Wu08accc62015-07-07 11:50:03 +08001276 info.renderPass = render_pass;
1277 info.subpass = 0;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001278 info.pTessellationState = NULL;
Tony Barbourdd6e32e2015-07-10 15:29:03 -06001279 info.pInputAssemblyState = &m_ia_state;
1280 info.pViewportState = &m_vp_state;
1281 info.pRasterState = &m_rs_state;
1282 info.pMultisampleState = &m_ms_state;
1283 info.pDepthStencilState = &m_ds_state;
1284 info.pColorBlendState = &m_cb_state;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001285
Chris Forbes95292b12015-05-25 11:13:26 +12001286 return init_try(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001287}
Chia-I Wu2648d092014-12-29 14:24:14 +08001288
Tony Barbourfe3351b2015-07-28 10:17:20 -06001289VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device, VkCmdPool pool)
Tony Barbour6d047bf2014-12-10 14:34:45 -07001290{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001291 m_device = device;
Courtney Goeltzenleuchter0c8385b2015-07-13 12:57:11 -06001292
Tony Barbourfe3351b2015-07-28 10:17:20 -06001293 init(*device, vk_testing::CmdBuffer::create_info(pool));
Tony Barbour6d047bf2014-12-10 14:34:45 -07001294}
Tony Barbour471338d2014-12-10 17:28:39 -07001295
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001296VkCmdBuffer VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001297{
Chia-I Wube2b9172015-07-03 11:49:42 +08001298 return handle();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001299}
Tony Barbour471338d2014-12-10 17:28:39 -07001300
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001301VkResult VkCommandBufferObj::BeginCommandBuffer(VkCmdBufferBeginInfo *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001302{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001303 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001304 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001305}
1306
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001307VkResult VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001308{
1309 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001310 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001311}
1312
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001313VkResult VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001314{
Chia-I Wud28343c2014-12-28 15:12:48 +08001315 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001316 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001317}
1318
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001319void VkCommandBufferObj::PipelineBarrier(VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages, VkBool32 byRegion, uint32_t memBarrierCount, const void* const* ppMemBarriers)
Tony Barbour471338d2014-12-10 17:28:39 -07001320{
Chia-I Wube2b9172015-07-03 11:49:42 +08001321 vkCmdPipelineBarrier(handle(), src_stages, dest_stages, byRegion, memBarrierCount, ppMemBarriers);
Tony Barbour471338d2014-12-10 17:28:39 -07001322}
1323
Chris Forbesf0796e12015-06-24 14:34:53 +12001324void VkCommandBufferObj::ClearAllBuffers(VkClearColorValue clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Tony Barbour6918cd52015-04-09 12:58:51 -06001325 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001326{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001327 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001328 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001329 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001330 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1331 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1332 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001333 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001334 const VkFlags input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001335
1336 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001337 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001338 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001339 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001340 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001341 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001342 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001343
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001344 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001345 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001346 memory_barrier.outputMask = output_mask;
1347 memory_barrier.inputMask = input_mask;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001348 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001349 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001350 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001351
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001352 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1353 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001354
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001355 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001356 memory_barrier.image = m_renderTargets[i]->image();
1357 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001358 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001359 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001360
Chia-I Wube2b9172015-07-03 11:49:42 +08001361 vkCmdClearColorImage(handle(),
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001362 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterd7a5cff2015-04-23 17:49:22 -06001363 &clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001364
Tony Barbour30cc9e82014-12-17 11:53:55 -07001365 }
1366
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001367 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001368 {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001369 VkImageSubresourceRange dsRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001370 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001371 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001372 dsRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001373 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001374 dsRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001375
1376 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001377
Cody Northrop3cc85e92015-08-04 10:47:08 -06001378 memory_barrier.oldLayout = memory_barrier.newLayout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001379 memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001380 memory_barrier.image = depthStencilObj->handle();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001381 memory_barrier.subresourceRange = dsRange;
1382
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001383 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001384
Chia-I Wube2b9172015-07-03 11:49:42 +08001385 vkCmdClearDepthStencilImage(handle(),
Chia-I Wu681d7a02015-07-03 13:44:34 +08001386 depthStencilObj->handle(), VK_IMAGE_LAYOUT_GENERAL,
Chris Forbesd9be82b2015-06-22 17:21:59 +12001387 depth_clear_color, stencil_clear_color,
1388 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001389
1390 // prepare depth buffer for rendering
Chia-I Wu681d7a02015-07-03 13:44:34 +08001391 memory_barrier.image = depthStencilObj->handle();
Cody Northrop3cc85e92015-08-04 10:47:08 -06001392 memory_barrier.newLayout = memory_barrier.oldLayout;
Mark Lobodzinski2b3fa3d2015-07-01 15:18:26 -06001393 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001394 memory_barrier.subresourceRange = dsRange;
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001395 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001396 }
1397}
1398
Tony Barbourc1eb1a52015-07-20 13:00:10 -06001399void VkCommandBufferObj::FillBuffer(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize fill_size, uint32_t data)
1400{
1401 vkCmdFillBuffer( handle(), buffer, offset, fill_size, data);
1402}
1403
Tony Barbour6918cd52015-04-09 12:58:51 -06001404void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001405{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001406 uint32_t i;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001407 const VkFlags output_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001408 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001409 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1410 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1411 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001412 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001413 const VkFlags input_mask =
Courtney Goeltzenleuchterf69f8a22015-04-29 17:16:21 -06001414 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001415 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1416 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1417 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1418 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1419 VK_MEMORY_INPUT_SHADER_READ_BIT |
1420 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1421 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterb9776ab2015-04-15 15:29:59 -06001422 VK_MEMORY_INPUT_TRANSFER_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001423
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001424 VkImageSubresourceRange srRange = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001425 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001426 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001427 srRange.mipLevels = VK_LAST_MIP_LEVEL;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001428 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchter62b2d432015-06-04 11:35:43 -06001429 srRange.arraySize = VK_LAST_ARRAY_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001430
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001431 VkImageMemoryBarrier memory_barrier = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001432 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001433 memory_barrier.outputMask = output_mask;
1434 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001435 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001436 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001437 VkImageMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001438
Tony Barbour0b2cfb22015-06-29 16:20:35 -06001439 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1440 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001441
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001442 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001443 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001444 memory_barrier.image = m_renderTargets[i]->image();
1445 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterceebbb12015-07-12 13:07:46 -06001446 vkCmdPipelineBarrier( handle(), src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001447 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001448 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001449}
1450
Chia-I Wu08accc62015-07-07 11:50:03 +08001451void VkCommandBufferObj::BeginRenderPass(const VkRenderPassBeginInfo &info)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001452{
Chia-I Wube2b9172015-07-03 11:49:42 +08001453 vkCmdBeginRenderPass( handle(), &info, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001454}
1455
Chia-I Wu0b50a1c2015-06-26 15:34:39 +08001456void VkCommandBufferObj::EndRenderPass()
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001457{
Chia-I Wube2b9172015-07-03 11:49:42 +08001458 vkCmdEndRenderPass(handle());
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001459}
1460
Tony Barbour67e99152015-07-10 14:10:27 -06001461void VkCommandBufferObj::BindDynamicViewportState(VkDynamicViewportState viewportState)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001462{
Tony Barbour67e99152015-07-10 14:10:27 -06001463 vkCmdBindDynamicViewportState( handle(), viewportState);
1464}
1465
1466void VkCommandBufferObj::BindDynamicRasterState(VkDynamicRasterState rasterState)
1467{
1468 vkCmdBindDynamicRasterState( handle(), rasterState);
1469}
1470
1471void VkCommandBufferObj::BindDynamicColorBlendState(VkDynamicColorBlendState colorBlendState)
1472{
1473 vkCmdBindDynamicColorBlendState( handle(), colorBlendState);
1474}
1475
1476void VkCommandBufferObj::BindDynamicDepthStencilState(VkDynamicDepthStencilState depthStencilState)
1477{
1478 vkCmdBindDynamicDepthStencilState( handle(), depthStencilState);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001479}
1480
Tony Barbour6918cd52015-04-09 12:58:51 -06001481void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001482{
1483 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001484}
1485
Tony Barbour6918cd52015-04-09 12:58:51 -06001486void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001487{
Chia-I Wube2b9172015-07-03 11:49:42 +08001488 vkCmdDrawIndexed(handle(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001489}
1490
Tony Barbour6918cd52015-04-09 12:58:51 -06001491void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001492{
Chia-I Wube2b9172015-07-03 11:49:42 +08001493 vkCmdDraw(handle(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001494}
1495
Tony Barbour6918cd52015-04-09 12:58:51 -06001496void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001497{
Tony Barbour67e99152015-07-10 14:10:27 -06001498 VkFence nullFence = { VK_NULL_HANDLE };
1499 QueueCommandBuffer(nullFence);
Tony Barbour09b7ac32015-04-08 10:11:29 -06001500}
1501
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001502void VkCommandBufferObj::QueueCommandBuffer(VkFence fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001503{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001504 VkResult err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001505
1506 // submit the command buffer to the universal queue
Chia-I Wube2b9172015-07-03 11:49:42 +08001507 err = vkQueueSubmit( m_device->m_queue, 1, &handle(), fence );
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001508 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001509
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001510 err = vkQueueWaitIdle( m_device->m_queue );
1511 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001512
1513 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001514 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001515}
1516
Tony Barbour6918cd52015-04-09 12:58:51 -06001517void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001518{
Chia-I Wube2b9172015-07-03 11:49:42 +08001519 vkCmdBindPipeline( handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.handle() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001520}
1521
Tony Barbour6918cd52015-04-09 12:58:51 -06001522void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001523{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001524 VkDescriptorSet set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001525
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001526 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wube2b9172015-07-03 11:49:42 +08001527 vkCmdBindDescriptorSets(handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet.GetPipelineLayout(),
Cody Northropd4c1a502015-04-16 13:41:56 -06001528 0, 1, &set_obj, 0, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001529}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001530
Tony Barbour6918cd52015-04-09 12:58:51 -06001531void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001532{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001533 vkCmdBindIndexBuffer(handle(), indexBuffer->handle(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001534}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001535
Tony Barbourd1c35722015-04-16 15:59:00 -06001536void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, VkDeviceSize offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001537{
Chia-I Wu681d7a02015-07-03 13:44:34 +08001538 vkCmdBindVertexBuffers(handle(), binding, 1, &vertexBuffer->handle(), &offset);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001539}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001540
Tony Barbour6918cd52015-04-09 12:58:51 -06001541VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001542{
1543 m_initialized = false;
1544}
Tony Barbour6918cd52015-04-09 12:58:51 -06001545bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001546{
1547 return m_initialized;
1548}
1549
Cody Northrop3cc85e92015-08-04 10:47:08 -06001550VkAttachmentView* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001551{
Chia-I Wu08accc62015-07-07 11:50:03 +08001552 return &m_attachmentBindInfo;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001553}
1554
Chia-I Wu08accc62015-07-07 11:50:03 +08001555void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height, VkFormat format)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001556{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001557 VkImageCreateInfo image_info;
Chia-I Wu08accc62015-07-07 11:50:03 +08001558 VkAttachmentViewCreateInfo view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001559
1560 m_device = device;
1561 m_initialized = true;
Chia-I Wu08accc62015-07-07 11:50:03 +08001562 m_depth_stencil_fmt = format;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001563
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001564 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001565 image_info.pNext = NULL;
Tony Barbourd1c35722015-04-16 15:59:00 -06001566 image_info.imageType = VK_IMAGE_TYPE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001567 image_info.format = m_depth_stencil_fmt;
1568 image_info.extent.width = width;
1569 image_info.extent.height = height;
1570 image_info.extent.depth = 1;
1571 image_info.mipLevels = 1;
1572 image_info.arraySize = 1;
1573 image_info.samples = 1;
Tony Barbourd1c35722015-04-16 15:59:00 -06001574 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001575 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001576 image_info.flags = 0;
Tony Barbour3d83f492015-07-28 10:23:02 -06001577 image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1578 image_info.queueFamilyCount = 0;
1579 image_info.pQueueFamilyIndices = NULL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001580 init(*m_device, image_info);
1581
Chia-I Wu08accc62015-07-07 11:50:03 +08001582 view_info.sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001583 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001584 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001585 view_info.mipLevel = 0;
1586 view_info.baseArraySlice = 0;
1587 view_info.arraySize = 1;
1588 view_info.flags = 0;
Tony Barbour3d83f492015-07-28 10:23:02 -06001589 view_info.format = m_depth_stencil_fmt;
Chia-I Wu681d7a02015-07-03 13:44:34 +08001590 view_info.image = handle();
Chia-I Wu08accc62015-07-07 11:50:03 +08001591 m_attachmentView.init(*m_device, view_info);
Tony Barbour1c45ce02015-03-27 17:03:18 -06001592
Cody Northrop3cc85e92015-08-04 10:47:08 -06001593 m_attachmentBindInfo = m_attachmentView.handle();
Tony Barbour1c45ce02015-03-27 17:03:18 -06001594}