blob: 9d38aa258b39a30d51d7b89ddd5e929527cda3dd [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan Tests
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060028#include "vkrenderframework.h"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Tony Barbour6918cd52015-04-09 12:58:51 -060030VkRenderFramework::VkRenderFramework() :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060031 m_cmdBuffer( VK_NULL_HANDLE ),
Tony Barbourf77fd652015-04-09 11:07:02 -060032 m_renderPass(VK_NULL_HANDLE),
33 m_framebuffer(VK_NULL_HANDLE),
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060034 m_stateRaster( VK_NULL_HANDLE ),
35 m_colorBlend( VK_NULL_HANDLE ),
36 m_stateViewport( VK_NULL_HANDLE ),
37 m_stateDepthStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060038 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070039 m_height( 256.0 ), // default window height
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060040 m_render_target_fmt( VK_FMT_R8G8B8A8_UNORM ),
41 m_depth_stencil_fmt( VK_FMT_UNDEFINED ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070042 m_depth_clear_color( 1.0 ),
43 m_stencil_clear_color( 0 )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060044{
Tony Barbour1c45ce02015-03-27 17:03:18 -060045
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070046 // clear the back buffer to dark grey
47 m_clear_color.color.rawColor[0] = 64;
48 m_clear_color.color.rawColor[1] = 64;
49 m_clear_color.color.rawColor[2] = 64;
50 m_clear_color.color.rawColor[3] = 0;
51 m_clear_color.useRawValue = true;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060052}
53
Tony Barbour6918cd52015-04-09 12:58:51 -060054VkRenderFramework::~VkRenderFramework()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060055{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060056
57}
58
Tony Barbour6918cd52015-04-09 12:58:51 -060059void VkRenderFramework::InitFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060060{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060061 VK_RESULT err;
62 VK_INSTANCE_CREATE_INFO instInfo = {};
63 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060064 instInfo.pNext = NULL;
65 instInfo.pAppInfo = &app_info;
66 instInfo.pAllocCb = NULL;
67 instInfo.extensionCount = 0;
68 instInfo.ppEnabledExtensionNames = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060069 err = vkCreateInstance(&instInfo, &this->inst);
70 ASSERT_VK_SUCCESS(err);
71 err = vkEnumerateGpus(inst, VK_MAX_PHYSICAL_GPUS, &this->gpu_count,
Jon Ashburn1e464892015-01-29 15:48:00 -070072 objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060073 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070074 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060075
Tony Barbour6918cd52015-04-09 12:58:51 -060076 m_device = new VkDeviceObj(0, objs[0]);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060077 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -060078
Tony Barbour6918cd52015-04-09 12:58:51 -060079 m_depthStencil = new VkDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060080}
81
Tony Barbour6918cd52015-04-09 12:58:51 -060082void VkRenderFramework::ShutdownFramework()
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060083{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060084 if (m_colorBlend) vkDestroyObject(m_colorBlend);
85 if (m_stateDepthStencil) vkDestroyObject(m_stateDepthStencil);
86 if (m_stateRaster) vkDestroyObject(m_stateRaster);
87 if (m_cmdBuffer) vkDestroyObject(m_cmdBuffer);
88 if (m_framebuffer) vkDestroyObject(m_framebuffer);
89 if (m_renderPass) vkDestroyObject(m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060090
91 if (m_stateViewport) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060092 vkDestroyObject(m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060093 }
Tobin Ehlis976fc162015-03-26 08:23:25 -060094 while (!m_renderTargets.empty()) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060095 vkDestroyObject(m_renderTargets.back()->targetView());
96 vkBindObjectMemory(m_renderTargets.back()->image(), 0, VK_NULL_HANDLE, 0);
97 vkDestroyObject(m_renderTargets.back()->image());
98 vkFreeMemory(m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -060099 m_renderTargets.pop_back();
100 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600101
Tony Barbour1c45ce02015-03-27 17:03:18 -0600102 delete m_depthStencil;
103
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600104 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800105 delete m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600106 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600107}
108
Tony Barbour6918cd52015-04-09 12:58:51 -0600109void VkRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600110{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600111 VK_RESULT err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600112
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600113 m_render_target_fmt = VK_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600114
115 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600116 VK_DYNAMIC_RS_STATE_CREATE_INFO raster = {};
117 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700118 raster.pointSize = 1.0;
119
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600120 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
121 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600122
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600123 VK_DYNAMIC_CB_STATE_CREATE_INFO blend = {};
124 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
125 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
126 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600127
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600128 VK_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {};
129 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600130 depthStencil.minDepth = 0.f;
131 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700132 depthStencil.stencilFrontRef = 0;
133 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600134
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600135 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
136 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600137
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600138 VK_CMD_BUFFER_CREATE_INFO cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600139
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600140 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700141 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
142
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600143 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
144 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600145}
146
Tony Barbour6918cd52015-04-09 12:58:51 -0600147void VkRenderFramework::InitViewport(float width, float height)
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600148{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600149 VK_RESULT err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600150
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600151 VK_VIEWPORT viewport;
152 VK_RECT scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600153
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600154 VK_DYNAMIC_VP_STATE_CREATE_INFO viewportCreate = {};
155 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700156 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700157 viewport.originX = 0;
158 viewport.originY = 0;
159 viewport.width = 1.f * width;
160 viewport.height = 1.f * height;
161 viewport.minDepth = 0.f;
162 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700163 scissor.extent.width = width;
164 scissor.extent.height = height;
165 scissor.offset.x = 0;
166 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700167 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700168 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700169
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600170 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
171 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600172 m_width = width;
173 m_height = height;
174}
175
Tony Barbour6918cd52015-04-09 12:58:51 -0600176void VkRenderFramework::InitViewport()
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600177{
178 InitViewport(m_width, m_height);
179}
Tony Barbour6918cd52015-04-09 12:58:51 -0600180void VkRenderFramework::InitRenderTarget()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600181{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700182 InitRenderTarget(1);
183}
184
Tony Barbour6918cd52015-04-09 12:58:51 -0600185void VkRenderFramework::InitRenderTarget(uint32_t targets)
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700186{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600187 InitRenderTarget(targets, NULL);
188}
189
Tony Barbour6918cd52015-04-09 12:58:51 -0600190void VkRenderFramework::InitRenderTarget(VK_DEPTH_STENCIL_BIND_INFO *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600191{
192 InitRenderTarget(1, dsBinding);
193}
194
Tony Barbour6918cd52015-04-09 12:58:51 -0600195void VkRenderFramework::InitRenderTarget(uint32_t targets, VK_DEPTH_STENCIL_BIND_INFO *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600196{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600197 std::vector<VK_ATTACHMENT_LOAD_OP> load_ops;
198 std::vector<VK_ATTACHMENT_STORE_OP> store_ops;
199 std::vector<VK_CLEAR_COLOR> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600200
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600201 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800202
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700203 for (i = 0; i < targets; i++) {
Tony Barbour6918cd52015-04-09 12:58:51 -0600204 VkImageObj *img = new VkImageObj(m_device);
Chia-I Wuf50ee212014-12-29 14:31:52 +0800205 img->init(m_width, m_height, m_render_target_fmt,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600206 VK_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
207 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700208 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600209 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700210 m_renderTargets.push_back(img);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600211 load_ops.push_back(VK_ATTACHMENT_LOAD_OP_LOAD);
212 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600213 clear_colors.push_back(m_clear_color);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600214// m_mem_ref_mgr.AddMemoryRefs(*img);
Chia-I Wuecebf752014-12-05 10:45:15 +0800215 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700216 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600217 VK_FRAMEBUFFER_CREATE_INFO fb_info = {};
218 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600219 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700220 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600221 fb_info.pColorAttachments = m_colorBindings;
222 fb_info.pDepthStencilAttachment = dsBinding;
223 fb_info.sampleCount = 1;
224 fb_info.width = (uint32_t)m_width;
225 fb_info.height = (uint32_t)m_height;
226 fb_info.layers = 1;
227
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600228 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600229
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600230 VK_RENDER_PASS_CREATE_INFO rp_info = {};
231 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700232 rp_info.renderArea.extent.width = m_width;
233 rp_info.renderArea.extent.height = m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600234
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700235 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600236 rp_info.pColorFormats = &m_render_target_fmt;
237 rp_info.pColorLayouts = &m_colorBindings[0].layout;
238 rp_info.pColorLoadOps = &load_ops[0];
239 rp_info.pColorStoreOps = &store_ops[0];
240 rp_info.pColorLoadClearValues = &clear_colors[0];
241 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600242 if (dsBinding) {
243 rp_info.depthStencilLayout = dsBinding->layout;
244 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600245 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600246 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600247 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
248 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600249 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600250 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
251 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600252}
253
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600254
255
Tony Barbour6918cd52015-04-09 12:58:51 -0600256VkDeviceObj::VkDeviceObj(uint32_t id, VK_PHYSICAL_GPU obj) :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600257 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800258{
259 init();
260
261 props = gpu().properties();
262 queue_props = &gpu().queue_properties()[0];
263}
264
Tony Barbour6918cd52015-04-09 12:58:51 -0600265void VkDeviceObj::get_device_queue()
Chia-I Wufb1459b2014-12-29 15:23:20 +0800266{
267 ASSERT_NE(true, graphics_queues().empty());
268 m_queue = graphics_queues()[0]->obj();
269}
270
Tony Barbour6918cd52015-04-09 12:58:51 -0600271VkDescriptorSetObj::VkDescriptorSetObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700272{
273 m_device = device;
274 m_nextSlot = 0;
275
276}
277
Tony Barbour6918cd52015-04-09 12:58:51 -0600278VkDescriptorSetObj::~VkDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700279{
Chia-I Wu11078b02015-01-04 16:27:24 +0800280 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700281}
Tony Barbour82c39522014-12-04 14:33:33 -0700282
Tony Barbour6918cd52015-04-09 12:58:51 -0600283int VkDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700284{
Chia-I Wu11078b02015-01-04 16:27:24 +0800285 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600286 VK_DESCRIPTOR_TYPE_COUNT tc = {};
287 tc.type = VK_DESCRIPTOR_TYPE_SHADER_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800288 tc.count = 1;
289 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700290
Chia-I Wu11078b02015-01-04 16:27:24 +0800291 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700292}
Tony Barbour82c39522014-12-04 14:33:33 -0700293
Tony Barbour6918cd52015-04-09 12:58:51 -0600294int VkDescriptorSetObj::AppendBuffer(VK_DESCRIPTOR_TYPE type, VkConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700295{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600296 VK_DESCRIPTOR_TYPE_COUNT tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800297 tc.type = type;
298 tc.count = 1;
299 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700300
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600301 m_updateBuffers.push_back(vk_testing::DescriptorSet::update(type,
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600302 m_nextSlot, 0, 1, &constantBuffer.m_bufferViewInfo));
303
304 // Track mem references for this descriptor set object
305 mem_ref_mgr.AddMemoryRefs(constantBuffer);
Chia-I Wu11078b02015-01-04 16:27:24 +0800306
307 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700308}
Tony Barbour82c39522014-12-04 14:33:33 -0700309
Tony Barbour6918cd52015-04-09 12:58:51 -0600310int VkDescriptorSetObj::AppendSamplerTexture( VkSamplerObj* sampler, VkTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700311{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600312 VK_DESCRIPTOR_TYPE_COUNT tc = {};
313 tc.type = VK_DESCRIPTOR_TYPE_SAMPLER_TEXTURE;
Chia-I Wu11078b02015-01-04 16:27:24 +0800314 tc.count = 1;
315 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700316
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600317 VK_SAMPLER_IMAGE_VIEW_INFO tmp = {};
Courtney Goeltzenleuchterf39b3ec2015-04-09 11:43:10 -0600318 tmp.sampler = sampler->obj();
Chia-I Wu11078b02015-01-04 16:27:24 +0800319 tmp.pImageView = &texture->m_textureViewInfo;
320 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700321
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600322 m_updateSamplerTextures.push_back(vk_testing::DescriptorSet::update(m_nextSlot, 0, 1,
323 (const VK_SAMPLER_IMAGE_VIEW_INFO *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700324
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600325 // Track mem references for the texture referenced here
326 mem_ref_mgr.AddMemoryRefs(*texture);
327
Chia-I Wu11078b02015-01-04 16:27:24 +0800328 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700329}
Chia-I Wu11078b02015-01-04 16:27:24 +0800330
Tony Barbour6918cd52015-04-09 12:58:51 -0600331VK_DESCRIPTOR_SET_LAYOUT_CHAIN VkDescriptorSetObj::GetLayoutChain() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700332{
Chia-I Wu41126e52015-03-26 15:27:55 +0800333 return m_layout_chain.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700334}
335
Tony Barbour6918cd52015-04-09 12:58:51 -0600336VK_DESCRIPTOR_SET VkDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700337{
Chia-I Wu11078b02015-01-04 16:27:24 +0800338 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700339}
Tony Barboure2c58df2014-11-25 13:18:32 -0700340
Tony Barbour6918cd52015-04-09 12:58:51 -0600341void VkDescriptorSetObj::CreateVKDescriptorSet(VkCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700342{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600343 // create VK_DESCRIPTOR_POOL
344 VK_DESCRIPTOR_POOL_CREATE_INFO pool = {};
345 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800346 pool.count = m_type_counts.size();
347 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600348 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700349
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600350 // create VK_DESCRIPTOR_SET_LAYOUT
351 vector<VK_DESCRIPTOR_SET_LAYOUT_BINDING> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800352 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800353 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800354 bindings[i].descriptorType = m_type_counts[i].type;
355 bindings[i].count = m_type_counts[i].count;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600356 bindings[i].stageFlags = VK_SHADER_STAGE_FLAGS_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800357 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700358 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800359
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600360 // create VK_DESCRIPTOR_SET_LAYOUT
361 VK_DESCRIPTOR_SET_LAYOUT_CREATE_INFO layout = {};
362 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800363 layout.count = bindings.size();
364 layout.pBinding = &bindings[0];
365
Chia-I Wu41126e52015-03-26 15:27:55 +0800366 m_layout.init(*m_device, layout);
367
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600368 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800369 layouts.push_back(&m_layout);
370 m_layout_chain.init(*m_device, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700371
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600372 // create VK_DESCRIPTOR_SET
373 m_set = alloc_sets(VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800374
Chia-I Wu41126e52015-03-26 15:27:55 +0800375 // build the update array
376 vector<const void *> update_array;
Chia-I Wu11078b02015-01-04 16:27:24 +0800377
Chia-I Wu41126e52015-03-26 15:27:55 +0800378 for (int i = 0; i < m_updateBuffers.size(); i++) {
379 update_array.push_back(&m_updateBuffers[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800380 }
381 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
382 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
Chia-I Wu41126e52015-03-26 15:27:55 +0800383 update_array.push_back(&m_updateSamplerTextures[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800384 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800385
386 // do the updates
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600387 m_device->begin_descriptor_pool_update(VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu11078b02015-01-04 16:27:24 +0800388 clear_sets(*m_set);
Chia-I Wu41126e52015-03-26 15:27:55 +0800389 m_set->update(update_array);
Chia-I Wu985ba162015-03-26 13:14:16 +0800390 m_device->end_descriptor_pool_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700391}
Tony Barboure2c58df2014-11-25 13:18:32 -0700392
Tony Barbour6918cd52015-04-09 12:58:51 -0600393VkImageObj::VkImageObj(VkDeviceObj *dev)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800394{
395 m_device = dev;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600396 m_imageInfo.view = VK_NULL_HANDLE;
397 m_imageInfo.layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800398}
399
Tony Barbour6918cd52015-04-09 12:58:51 -0600400void VkImageObj::ImageMemoryBarrier(
401 VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600402 VK_IMAGE_ASPECT aspect,
403 VK_FLAGS output_mask /*=
404 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
405 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
406 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
407 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
408 VK_MEMORY_OUTPUT_COPY_BIT*/,
409 VK_FLAGS input_mask /*=
410 VK_MEMORY_INPUT_CPU_READ_BIT |
411 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
412 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
413 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
414 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
415 VK_MEMORY_INPUT_SHADER_READ_BIT |
416 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
417 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
418 VK_MEMORY_INPUT_COPY_BIT*/,
419 VK_IMAGE_LAYOUT image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600420{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600421 const VK_IMAGE_SUBRESOURCE_RANGE subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
422 VK_IMAGE_MEMORY_BARRIER barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600423 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
424 subresourceRange);
425
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600426 VK_IMAGE_MEMORY_BARRIER *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600427
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600428 VK_PIPE_EVENT pipe_events[] = { VK_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
429 VK_PIPELINE_BARRIER pipeline_barrier = {};
430 pipeline_barrier.sType = VK_STRUCTURE_TYPE_PIPELINE_BARRIER;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600431 pipeline_barrier.pNext = NULL;
432 pipeline_barrier.eventCount = 1;
433 pipeline_barrier.pEvents = pipe_events;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600434 pipeline_barrier.waitEvent = VK_WAIT_EVENT_TOP_OF_PIPE;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600435 pipeline_barrier.memBarrierCount = 1;
436 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
437
438 // write barrier to the command buffer
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600439 vkCmdPipelineBarrier(cmd_buf->obj(), &pipeline_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600440}
441
Tony Barbour6918cd52015-04-09 12:58:51 -0600442void VkImageObj::SetLayout(VkCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600443 VK_IMAGE_ASPECT aspect,
444 VK_IMAGE_LAYOUT image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600445{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600446 VK_FLAGS output_mask, input_mask;
447 const VK_FLAGS all_cache_outputs =
448 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
449 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
450 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
451 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
452 VK_MEMORY_OUTPUT_COPY_BIT;
453 const VK_FLAGS all_cache_inputs =
454 VK_MEMORY_INPUT_CPU_READ_BIT |
455 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
456 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
457 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
458 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
459 VK_MEMORY_INPUT_SHADER_READ_BIT |
460 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
461 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
462 VK_MEMORY_INPUT_COPY_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600463
464 if (image_layout == m_imageInfo.layout) {
465 return;
466 }
467
468 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600469 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
470 output_mask = VK_MEMORY_OUTPUT_COPY_BIT;
471 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_COPY_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600472 break;
473
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600474 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
475 output_mask = VK_MEMORY_OUTPUT_COPY_BIT;
476 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_COPY_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600477 break;
478
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600479 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600480 break;
481
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600482 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
483 output_mask = VK_MEMORY_OUTPUT_COPY_BIT;
484 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_COPY_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600485 break;
486
487 default:
488 output_mask = all_cache_outputs;
489 input_mask = all_cache_inputs;
490 break;
491 }
492
493 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
494 m_imageInfo.layout = image_layout;
495}
496
Tony Barbour6918cd52015-04-09 12:58:51 -0600497void VkImageObj::SetLayout(VK_IMAGE_ASPECT aspect,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600498 VK_IMAGE_LAYOUT image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600499{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600500 VK_RESULT err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600501 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600502
503 /* Build command buffer to set image layout in the driver */
504 err = cmd_buf.BeginCommandBuffer();
505 assert(!err);
506
507 SetLayout(&cmd_buf, aspect, image_layout);
508
509 err = cmd_buf.EndCommandBuffer();
510 assert(!err);
511
512 cmd_buf.QueueCommandBuffer();
513}
514
Tony Barbour6918cd52015-04-09 12:58:51 -0600515bool VkImageObj::IsCompatible(VK_FLAGS usage, VK_FLAGS features)
Tony Barbour579f7802015-04-03 15:11:43 -0600516{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600517 if ((usage & VK_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) &&
518 !(features & VK_FORMAT_IMAGE_SHADER_READ_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600519 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600520
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600521 if ((usage & VK_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT) &&
522 !(features & VK_FORMAT_IMAGE_SHADER_WRITE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600523 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600524
Tony Barbour579f7802015-04-03 15:11:43 -0600525 return true;
526}
527
Tony Barbour6918cd52015-04-09 12:58:51 -0600528void VkImageObj::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600529 VK_FORMAT fmt, VK_FLAGS usage,
530 VK_IMAGE_TILING requested_tiling)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800531{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600532 uint32_t mipCount;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600533 VK_FORMAT_PROPERTIES image_fmt;
534 VK_IMAGE_TILING tiling;
535 VK_RESULT err;
Tony Barbour579f7802015-04-03 15:11:43 -0600536 size_t size;
537
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800538 mipCount = 0;
539
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600540 uint32_t _w = w;
541 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800542 while( ( _w > 0 ) || ( _h > 0 ) )
543 {
544 _w >>= 1;
545 _h >>= 1;
546 mipCount++;
547 }
548
Tony Barbour579f7802015-04-03 15:11:43 -0600549 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600550 err = vkGetFormatInfo(m_device->obj(), fmt,
551 VK_INFO_TYPE_FORMAT_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600552 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600553 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600554
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600555 if (requested_tiling == VK_LINEAR_TILING) {
Tony Barbour579f7802015-04-03 15:11:43 -0600556 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600557 tiling = VK_LINEAR_TILING;
Tony Barbour579f7802015-04-03 15:11:43 -0600558 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600559 tiling = VK_OPTIMAL_TILING;
Tony Barbour579f7802015-04-03 15:11:43 -0600560 } else {
561 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
562 }
563 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600564 tiling = VK_OPTIMAL_TILING;
Tony Barbour579f7802015-04-03 15:11:43 -0600565 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600566 tiling = VK_LINEAR_TILING;
Tony Barbour579f7802015-04-03 15:11:43 -0600567 } else {
568 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
569 }
570
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600571 VK_IMAGE_CREATE_INFO imageCreateInfo = vk_testing::Image::create_info();
572 imageCreateInfo.imageType = VK_IMAGE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800573 imageCreateInfo.format = fmt;
574 imageCreateInfo.extent.width = w;
575 imageCreateInfo.extent.height = h;
576 imageCreateInfo.mipLevels = mipCount;
577 imageCreateInfo.tiling = tiling;
578
579 imageCreateInfo.usage = usage;
580
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600581 vk_testing::Image::init(*m_device, imageCreateInfo);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800582
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600583 if (usage & VK_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) {
584 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600585 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600586 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600587 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800588}
589
Tony Barbour6918cd52015-04-09 12:58:51 -0600590VK_RESULT VkImageObj::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800591{
592 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600593 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800594}
595
Tony Barbour6918cd52015-04-09 12:58:51 -0600596VK_RESULT VkImageObj::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800597{
598 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600599 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800600}
601
Tony Barbour6918cd52015-04-09 12:58:51 -0600602VK_RESULT VkImageObj::CopyImage(VkImageObj &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600603{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600604 VK_RESULT err;
Tony Barbour6918cd52015-04-09 12:58:51 -0600605 VkCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600606 VK_IMAGE_LAYOUT src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600607
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600608 /* Build command buffer to copy staging texture to usable texture */
609 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600610 assert(!err);
611
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600612 /* TODO: Can we determine image aspect from image object? */
613 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600614 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600615
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600616 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600617 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600618
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600619 VK_IMAGE_COPY copy_region = {};
620 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600621 copy_region.srcSubresource.arraySlice = 0;
622 copy_region.srcSubresource.mipLevel = 0;
623 copy_region.srcOffset.x = 0;
624 copy_region.srcOffset.y = 0;
625 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600626 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600627 copy_region.destSubresource.arraySlice = 0;
628 copy_region.destSubresource.mipLevel = 0;
629 copy_region.destOffset.x = 0;
630 copy_region.destOffset.y = 0;
631 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600632 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600633
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600634 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600635 src_image.obj(), src_image.layout(),
636 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600637 1, &copy_region);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600638 cmd_buf.mem_ref_mgr.AddMemoryRefs(src_image);
639 cmd_buf.mem_ref_mgr.AddMemoryRefs(*this);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600640
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600641 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600642
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600643 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600644
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600645 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600646 assert(!err);
647
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600648 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600649
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600650 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600651}
652
Tony Barbour6918cd52015-04-09 12:58:51 -0600653VkTextureObj::VkTextureObj(VkDeviceObj *device, uint32_t *colors)
654 :VkImageObj(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700655{
656 m_device = device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600657 const VK_FORMAT tex_format = VK_FMT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600658 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600659 void *data;
660 int32_t x, y;
Tony Barbour6918cd52015-04-09 12:58:51 -0600661 VkImageObj stagingImage(device);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600662
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600663 stagingImage.init(16, 16, tex_format, 0, VK_LINEAR_TILING);
664 VK_SUBRESOURCE_LAYOUT layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600665
666 if (colors == NULL)
667 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700668
669 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
670
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600671 m_textureViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700672
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600673 VK_IMAGE_VIEW_CREATE_INFO view = {};
674 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600675 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600676 view.image = VK_NULL_HANDLE;
677 view.viewType = VK_IMAGE_VIEW_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600678 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600679 view.channels.r = VK_CHANNEL_SWIZZLE_R;
680 view.channels.g = VK_CHANNEL_SWIZZLE_G;
681 view.channels.b = VK_CHANNEL_SWIZZLE_B;
682 view.channels.a = VK_CHANNEL_SWIZZLE_A;
683 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600684 view.subresourceRange.baseMipLevel = 0;
685 view.subresourceRange.mipLevels = 1;
686 view.subresourceRange.baseArraySlice = 0;
687 view.subresourceRange.arraySize = 1;
688 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700689
Tony Barboure2c58df2014-11-25 13:18:32 -0700690 /* create image */
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600691 init(16, 16, tex_format, VK_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, VK_OPTIMAL_TILING);
Tony Barboure2c58df2014-11-25 13:18:32 -0700692
693 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800694 view.image = obj();
695 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600696 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700697
Tony Barbour5dc515d2015-04-01 17:47:06 -0600698 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700699
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800700 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700701 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800702 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600703 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700704 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600705 stagingImage.unmap();
Tony Barbour6918cd52015-04-09 12:58:51 -0600706 VkImageObj::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700707}
Tony Barbour82c39522014-12-04 14:33:33 -0700708
Tony Barbour6918cd52015-04-09 12:58:51 -0600709VkSamplerObj::VkSamplerObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700710{
Tony Barboure2c58df2014-11-25 13:18:32 -0700711 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700712
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600713 VK_SAMPLER_CREATE_INFO samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800714 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600715 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
716 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
717 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
718 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_BASE;
719 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
720 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
721 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800722 samplerCreateInfo.mipLodBias = 0.0;
723 samplerCreateInfo.maxAnisotropy = 0.0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600724 samplerCreateInfo.compareFunc = VK_COMPARE_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800725 samplerCreateInfo.minLod = 0.0;
726 samplerCreateInfo.maxLod = 0.0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600727 samplerCreateInfo.borderColorType = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700728
Chia-I Wue9864b52014-12-28 16:32:24 +0800729 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700730}
Tony Barboure2c58df2014-11-25 13:18:32 -0700731
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700732/*
733 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
734 */
Tony Barbour6918cd52015-04-09 12:58:51 -0600735VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700736{
737 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700738 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700739
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800740 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700741}
742
Tony Barbour6918cd52015-04-09 12:58:51 -0600743VkConstantBufferObj::~VkConstantBufferObj()
Tony Barbour044703b2015-03-26 12:37:52 -0600744{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600745 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600746 if (m_commandBuffer) {
747 delete m_commandBuffer;
748 }
749}
750
Tony Barbour6918cd52015-04-09 12:58:51 -0600751VkConstantBufferObj::VkConstantBufferObj(VkDeviceObj *device, int constantCount, int constantSize, const void* data)
Tony Barboure2c58df2014-11-25 13:18:32 -0700752{
Tony Barboure2c58df2014-11-25 13:18:32 -0700753 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800754 m_commandBuffer = 0;
755
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800756 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700757 m_numVertices = constantCount;
758 m_stride = constantSize;
759
Chia-I Wua07fee62014-12-28 15:26:08 +0800760 const size_t allocationSize = constantCount * constantSize;
761 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700762
Chia-I Wua07fee62014-12-28 15:26:08 +0800763 void *pData = map();
764 memcpy(pData, data, allocationSize);
765 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700766
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800767 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600768 VK_BUFFER_VIEW_CREATE_INFO view_info = {};
769 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800770 view_info.buffer = obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600771 view_info.viewType = VK_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800772 view_info.offset = 0;
773 view_info.range = allocationSize;
774 m_bufferView.init(*m_device, view_info);
775
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600776 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800777 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700778}
Tony Barbour82c39522014-12-04 14:33:33 -0700779
Tony Barbour6918cd52015-04-09 12:58:51 -0600780void VkConstantBufferObj::Bind(VK_CMD_BUFFER cmdBuffer, VK_GPU_SIZE offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700781{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600782 vkCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700783}
784
785
Tony Barbour6918cd52015-04-09 12:58:51 -0600786void VkConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600787 VK_FLAGS outputMask /*=
788 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
789 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
790 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
791 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
792 VK_MEMORY_OUTPUT_COPY_BIT*/,
793 VK_FLAGS inputMask /*=
794 VK_MEMORY_INPUT_CPU_READ_BIT |
795 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
796 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
797 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
798 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
799 VK_MEMORY_INPUT_SHADER_READ_BIT |
800 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
801 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
802 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700803{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600804 VK_RESULT err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700805
Tony Barbour38422802014-12-10 14:36:31 -0700806 if (!m_commandBuffer)
807 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600808 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700809
Tony Barbour6918cd52015-04-09 12:58:51 -0600810 m_commandBuffer = new VkCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700811 }
812 else
813 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800814 m_device->wait(m_fence);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600815 m_commandBuffer->mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour38422802014-12-10 14:36:31 -0700816 }
817
Tony Barboure2c58df2014-11-25 13:18:32 -0700818 // open the command buffer
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600819 VK_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {};
820 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600821 cmd_buf_info.pNext = NULL;
822 cmd_buf_info.flags = 0;
823
Jon Ashburnc4164b12014-12-31 17:10:47 -0700824 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600825 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700826
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600827 VK_BUFFER_MEMORY_BARRIER memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000828 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600829 VK_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700830
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600831 VK_PIPE_EVENT set_events[] = { VK_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
832 VK_PIPELINE_BARRIER pipeline_barrier = {};
833 pipeline_barrier.sType = VK_STRUCTURE_TYPE_PIPELINE_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000834 pipeline_barrier.eventCount = 1;
835 pipeline_barrier.pEvents = set_events;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600836 pipeline_barrier.waitEvent = VK_WAIT_EVENT_TOP_OF_PIPE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000837 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -0600838 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000839
840 // write barrier to the command buffer
841 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700842
843 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700844 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600845 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700846
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600847 /*
848 * Tell driver about memory references made in this command buffer
849 * Note: Since this command buffer only has a PipelineBarrier
850 * command there really aren't any memory refs to worry about.
851 */
852 m_commandBuffer->mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
Tony Barboure2c58df2014-11-25 13:18:32 -0700853
854 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600855 VK_CMD_BUFFER bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700856 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600857 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
858 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700859}
860
Tony Barbour6918cd52015-04-09 12:58:51 -0600861VkIndexBufferObj::VkIndexBufferObj(VkDeviceObj *device)
862 : VkConstantBufferObj(device)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700863{
864
865}
866
Tony Barbour6918cd52015-04-09 12:58:51 -0600867void VkIndexBufferObj::CreateAndInitBuffer(int numIndexes, VK_INDEX_TYPE indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700868{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600869 VK_FORMAT viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700870
871 m_numVertices = numIndexes;
872 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700873 switch (indexType) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600874 case VK_INDEX_8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700875 m_stride = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600876 viewFormat = VK_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700877 break;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600878 case VK_INDEX_16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700879 m_stride = 2;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600880 viewFormat = VK_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700881 break;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600882 case VK_INDEX_32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700883 m_stride = 4;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600884 viewFormat = VK_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700885 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800886 default:
887 assert(!"unknown index type");
888 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700889 }
890
Chia-I Wua07fee62014-12-28 15:26:08 +0800891 const size_t allocationSize = numIndexes * m_stride;
892 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700893
Chia-I Wua07fee62014-12-28 15:26:08 +0800894 void *pData = map();
895 memcpy(pData, data, allocationSize);
896 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700897
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800898 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600899 VK_BUFFER_VIEW_CREATE_INFO view_info = {};
900 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800901 view_info.buffer = obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600902 view_info.viewType = VK_BUFFER_VIEW_TYPED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700903 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800904 view_info.offset = 0;
905 view_info.range = allocationSize;
906 m_bufferView.init(*m_device, view_info);
907
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600908 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800909 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700910}
911
Tony Barbour6918cd52015-04-09 12:58:51 -0600912void VkIndexBufferObj::Bind(VK_CMD_BUFFER cmdBuffer, VK_GPU_SIZE offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700913{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600914 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700915}
Tony Barboure2c58df2014-11-25 13:18:32 -0700916
Tony Barbour6918cd52015-04-09 12:58:51 -0600917VK_INDEX_TYPE VkIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -0700918{
919 return m_indexType;
920}
921
Tony Barbour6918cd52015-04-09 12:58:51 -0600922VK_PIPELINE_SHADER_STAGE_CREATE_INFO* VkShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700923{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600924 VK_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (VK_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(VK_PIPELINE_SHADER_STAGE_CREATE_INFO) );
925 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700926 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800927 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700928 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600929 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700930
Tony Barboure2c58df2014-11-25 13:18:32 -0700931 return stageInfo;
932}
933
Tony Barbour6918cd52015-04-09 12:58:51 -0600934VkShaderObj::VkShaderObj(VkDeviceObj *device, const char * shader_code, VK_PIPELINE_SHADER_STAGE stage, VkRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -0700935{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600936 VK_RESULT err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600937 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600938 VK_SHADER_CREATE_INFO createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700939 size_t shader_len;
940
941 m_stage = stage;
942 m_device = device;
943
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600944 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700945 createInfo.pNext = NULL;
946
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600947 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700948
949 shader_len = strlen(shader_code);
950 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
951 createInfo.pCode = malloc(createInfo.codeSize);
952 createInfo.flags = 0;
953
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600954 /* try version 0 first: VK_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600955 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700956 ((uint32_t *) createInfo.pCode)[1] = 0;
957 ((uint32_t *) createInfo.pCode)[2] = stage;
958 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
959
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800960 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700961 }
962
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600963 if (framework->m_use_spv || err) {
964 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600965 err = VK_SUCCESS;
Tony Barboure2c58df2014-11-25 13:18:32 -0700966
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600967 // Use Reference GLSL to SPV compiler
968 framework->GLSLtoSPV(stage, shader_code, spv);
969 createInfo.pCode = spv.data();
970 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700971 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700972
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800973 init(*m_device, createInfo);
974 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700975}
Tony Barbour82c39522014-12-04 14:33:33 -0700976
Tony Barbour6918cd52015-04-09 12:58:51 -0600977VkPipelineObj::VkPipelineObj(VkDeviceObj *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700978{
Tony Barboure2c58df2014-11-25 13:18:32 -0700979 m_device = device;
980 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
981 m_vertexBufferCount = 0;
982
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600983 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
984 m_ia_state.pNext = VK_NULL_HANDLE;
985 m_ia_state.topology = VK_TOPOLOGY_TRIANGLE_LIST;
986 m_ia_state.disableVertexReuse = VK_FALSE;
987 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700988 m_ia_state.primitiveRestartIndex = 0;
989
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600990 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700991 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600992 m_rs_state.depthClipEnable = VK_FALSE;
993 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
994 m_rs_state.programPointSize = VK_FALSE;
995 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
996 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
997 m_rs_state.fillMode = VK_FILL_SOLID;
998 m_rs_state.cullMode = VK_CULL_NONE;
999 m_rs_state.frontFace = VK_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001000
Tony Barboure2c58df2014-11-25 13:18:32 -07001001 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001002 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001003 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001004 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1005 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001006
Tony Barbourf52346d2015-01-16 14:27:35 -07001007 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001008 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1009 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001010 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1011 m_ms_state.samples = 1;
1012 m_ms_state.minSampleShading = 0;
1013 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001014
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001015 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -07001016 m_ds_state.pNext = &m_ms_state,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001017 m_ds_state.format = VK_FMT_D32_SFLOAT;
1018 m_ds_state.depthTestEnable = VK_FALSE;
1019 m_ds_state.depthWriteEnable = VK_FALSE;
1020 m_ds_state.depthBoundsEnable = VK_FALSE;
1021 m_ds_state.depthFunc = VK_COMPARE_LESS_EQUAL;
1022 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1023 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1024 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
1025 m_ds_state.back.stencilFunc = VK_COMPARE_ALWAYS;
1026 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001027 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001028
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001029 VK_PIPELINE_CB_ATTACHMENT_STATE att = {};
1030 att.blendEnable = VK_FALSE;
1031 att.format = VK_FMT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001032 att.channelWriteMask = 0xf;
1033 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001034
1035};
1036
Tony Barbour6918cd52015-04-09 12:58:51 -06001037void VkPipelineObj::AddShader(VkShaderObj* shader)
Tony Barboure2c58df2014-11-25 13:18:32 -07001038{
1039 m_shaderObjs.push_back(shader);
1040}
1041
Tony Barbour6918cd52015-04-09 12:58:51 -06001042void VkPipelineObj::AddVertexInputAttribs(VK_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001043{
1044 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1045 m_vi_state.attributeCount = count;
1046}
1047
Tony Barbour6918cd52015-04-09 12:58:51 -06001048void VkPipelineObj::AddVertexInputBindings(VK_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001049{
1050 m_vi_state.pVertexBindingDescriptions = vi_binding;
1051 m_vi_state.bindingCount = count;
1052}
1053
Tony Barbour6918cd52015-04-09 12:58:51 -06001054void VkPipelineObj::AddVertexDataBuffer(VkConstantBufferObj* vertexDataBuffer, int binding)
Tony Barboure2c58df2014-11-25 13:18:32 -07001055{
1056 m_vertexBufferObjs.push_back(vertexDataBuffer);
1057 m_vertexBufferBindings.push_back(binding);
1058 m_vertexBufferCount++;
1059}
1060
Tony Barbour6918cd52015-04-09 12:58:51 -06001061void VkPipelineObj::AddColorAttachment(uint32_t binding, const VK_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001062{
Tony Barbourf52346d2015-01-16 14:27:35 -07001063 if (binding+1 > m_colorAttachments.size())
1064 {
1065 m_colorAttachments.resize(binding+1);
1066 }
1067 m_colorAttachments[binding] = *att;
1068}
1069
Tony Barbour6918cd52015-04-09 12:58:51 -06001070void VkPipelineObj::SetDepthStencil(VK_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001071{
1072 m_ds_state.format = ds_state->format;
1073 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1074 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1075 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
1076 m_ds_state.depthFunc = ds_state->depthFunc;
1077 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1078 m_ds_state.back = ds_state->back;
1079 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001080}
1081
Tony Barbour6918cd52015-04-09 12:58:51 -06001082void VkPipelineObj::CreateVKPipeline(VkDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001083{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001084 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001085 VK_GRAPHICS_PIPELINE_CREATE_INFO info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001086
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001087 VK_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001088
1089 for (int i=0; i<m_shaderObjs.size(); i++)
1090 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001091 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001092 shaderCreateInfo->pNext = head_ptr;
1093 head_ptr = shaderCreateInfo;
1094 }
1095
1096 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1097 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001098 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001099 m_vi_state.pNext = head_ptr;
1100 head_ptr = &m_vi_state;
1101 }
1102
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001103 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001104 info.pNext = head_ptr;
1105 info.flags = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001106 info.pSetLayoutChain = descriptorSet.GetLayoutChain();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001107
Tony Barbourf52346d2015-01-16 14:27:35 -07001108 m_cb_state.attachmentCount = m_colorAttachments.size();
1109 m_cb_state.pAttachments = &m_colorAttachments[0];
1110
Chia-I Wu2648d092014-12-29 14:24:14 +08001111 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001112}
Chia-I Wu2648d092014-12-29 14:24:14 +08001113
Tony Barbour6918cd52015-04-09 12:58:51 -06001114vector<VK_GPU_MEMORY> VkMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001115{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001116 std::vector<VK_GPU_MEMORY> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001117 if (this->mem_refs_.size()) {
1118 mems.reserve(this->mem_refs_.size());
1119 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1120 mems.push_back(this->mem_refs_[i]);
1121 }
1122
1123 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001124}
1125
Tony Barbour6918cd52015-04-09 12:58:51 -06001126void VkMemoryRefManager::AddMemoryRefs(vk_testing::Object &vkObject)
Tony Barboure2c58df2014-11-25 13:18:32 -07001127{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001128 const std::vector<VK_GPU_MEMORY> mems = vkObject.memories();
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001129 AddMemoryRefs(mems);
1130}
Tony Barboure2c58df2014-11-25 13:18:32 -07001131
Tony Barbour6918cd52015-04-09 12:58:51 -06001132void VkMemoryRefManager::AddMemoryRefs(vector<VK_GPU_MEMORY> mem)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001133{
1134 for (size_t i = 0; i < mem.size(); i++) {
1135 if (mem[i] != NULL) {
1136 this->mem_refs_.push_back(mem[i]);
1137 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001138 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001139}
Tony Barbour82c39522014-12-04 14:33:33 -07001140
Tony Barbour6918cd52015-04-09 12:58:51 -06001141void VkMemoryRefManager::EmitAddMemoryRefs(VK_QUEUE queue)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001142{
1143 for (uint32_t i = 0; i < mem_refs_.size(); i++) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001144 vkQueueAddMemReference(queue, mem_refs_[i]);
Tony Barbour1c45ce02015-03-27 17:03:18 -06001145 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001146}
Tony Barbour82c39522014-12-04 14:33:33 -07001147
Tony Barbour6918cd52015-04-09 12:58:51 -06001148void VkMemoryRefManager::EmitRemoveMemoryRefs(VK_QUEUE queue)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001149{
1150 for (uint32_t i = 0; i < mem_refs_.size(); i++) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001151 vkQueueRemoveMemReference(queue, mem_refs_[i]);
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001152 }
1153}
1154
Tony Barbour6918cd52015-04-09 12:58:51 -06001155VkCommandBufferObj::VkCommandBufferObj(VkDeviceObj *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001156 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001157{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001158 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001159}
Tony Barbour471338d2014-12-10 17:28:39 -07001160
Tony Barbour6918cd52015-04-09 12:58:51 -06001161VK_CMD_BUFFER VkCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001162{
Chia-I Wud28343c2014-12-28 15:12:48 +08001163 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001164}
Tony Barbour471338d2014-12-10 17:28:39 -07001165
Tony Barbour6918cd52015-04-09 12:58:51 -06001166VK_RESULT VkCommandBufferObj::BeginCommandBuffer(VK_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001167{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001168 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001169 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001170}
1171
Tony Barbour6918cd52015-04-09 12:58:51 -06001172VK_RESULT VkCommandBufferObj::BeginCommandBuffer(VK_RENDER_PASS renderpass_obj, VK_FRAMEBUFFER framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001173{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001174 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001175 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001176}
1177
Tony Barbour6918cd52015-04-09 12:58:51 -06001178VK_RESULT VkCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001179{
1180 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001181 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001182}
1183
Tony Barbour6918cd52015-04-09 12:58:51 -06001184VK_RESULT VkCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001185{
Chia-I Wud28343c2014-12-28 15:12:48 +08001186 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001187 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001188}
1189
Tony Barbour6918cd52015-04-09 12:58:51 -06001190void VkCommandBufferObj::PipelineBarrier(VK_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001191{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001192 vkCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001193}
1194
Tony Barbour6918cd52015-04-09 12:58:51 -06001195void VkCommandBufferObj::ClearAllBuffers(VK_CLEAR_COLOR clear_color, float depth_clear_color, uint32_t stencil_clear_color,
1196 VkDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001197{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001198 uint32_t i;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001199 const VK_FLAGS output_mask =
1200 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
1201 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1202 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1203 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1204 VK_MEMORY_OUTPUT_COPY_BIT;
1205 const VK_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001206
1207 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001208 VK_IMAGE_SUBRESOURCE_RANGE srRange = {};
1209 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001210 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001211 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001212 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001213 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001214
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001215 VK_IMAGE_MEMORY_BARRIER memory_barrier = {};
1216 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001217 memory_barrier.outputMask = output_mask;
1218 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001219 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001220 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001221 VK_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001222
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001223 VK_PIPE_EVENT set_events[] = { VK_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
1224 VK_PIPELINE_BARRIER pipeline_barrier = {};
1225 pipeline_barrier.sType = VK_STRUCTURE_TYPE_PIPELINE_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001226 pipeline_barrier.eventCount = 1;
1227 pipeline_barrier.pEvents = set_events;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001228 pipeline_barrier.waitEvent = VK_WAIT_EVENT_TOP_OF_PIPE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001229 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001230 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001231
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001232 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001233 memory_barrier.image = m_renderTargets[i]->image();
1234 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001235 vkCmdPipelineBarrier( obj(), &pipeline_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001236 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001237
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001238 vkCmdClearColorImage(obj(),
1239 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -06001240 clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001241
1242 mem_ref_mgr.AddMemoryRefs(*m_renderTargets[i]);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001243 }
1244
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001245 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001246 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001247 VK_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1248 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001249 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001250 dsRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001251 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001252 dsRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001253
1254 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001255
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001256 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001257 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001258 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001259 memory_barrier.subresourceRange = dsRange;
1260
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001261 vkCmdPipelineBarrier( obj(), &pipeline_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001262
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001263 vkCmdClearDepthStencil(obj(),
1264 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001265 depth_clear_color, stencil_clear_color,
1266 1, &dsRange);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001267 mem_ref_mgr.AddMemoryRefs(*depthStencilObj);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001268
1269 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001270 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001271 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001272 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001273 memory_barrier.subresourceRange = dsRange;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001274 vkCmdPipelineBarrier( obj(), &pipeline_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001275 }
1276}
1277
Tony Barbour6918cd52015-04-09 12:58:51 -06001278void VkCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001279{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001280 uint32_t i;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001281 const VK_FLAGS output_mask =
1282 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
1283 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1284 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1285 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1286 VK_MEMORY_OUTPUT_COPY_BIT;
1287 const VK_FLAGS input_mask =
1288 VK_MEMORY_INPUT_CPU_READ_BIT |
1289 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1290 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1291 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1292 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1293 VK_MEMORY_INPUT_SHADER_READ_BIT |
1294 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1295 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1296 VK_MEMORY_INPUT_COPY_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001297
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001298 VK_IMAGE_SUBRESOURCE_RANGE srRange = {};
1299 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001300 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001301 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001302 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001303 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001304
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001305 VK_IMAGE_MEMORY_BARRIER memory_barrier = {};
1306 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001307 memory_barrier.outputMask = output_mask;
1308 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001309 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001310 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001311 VK_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001312
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001313 VK_PIPE_EVENT set_events[] = { VK_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
1314 VK_PIPELINE_BARRIER pipeline_barrier = {};
1315 pipeline_barrier.sType = VK_STRUCTURE_TYPE_PIPELINE_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001316 pipeline_barrier.eventCount = 1;
1317 pipeline_barrier.pEvents = set_events;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001318 pipeline_barrier.waitEvent = VK_WAIT_EVENT_TOP_OF_PIPE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001319 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001320 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001321
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001322 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001323 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001324 memory_barrier.image = m_renderTargets[i]->image();
1325 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001326 vkCmdPipelineBarrier( obj(), &pipeline_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001327 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001328 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001329}
1330
Tony Barbour6918cd52015-04-09 12:58:51 -06001331void VkCommandBufferObj::BeginRenderPass(VK_RENDER_PASS renderpass, VK_FRAMEBUFFER framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001332{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001333 VK_RENDER_PASS_BEGIN rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001334 renderpass,
1335 framebuffer,
1336 };
1337
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001338 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001339}
1340
Tony Barbour6918cd52015-04-09 12:58:51 -06001341void VkCommandBufferObj::EndRenderPass(VK_RENDER_PASS renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001342{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001343 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001344}
1345
Tony Barbour6918cd52015-04-09 12:58:51 -06001346void VkCommandBufferObj::BindStateObject(VK_STATE_BIND_POINT stateBindPoint, VK_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001347{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001348 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001349}
1350
Tony Barbour6918cd52015-04-09 12:58:51 -06001351void VkCommandBufferObj::AddRenderTarget(VkImageObj *renderTarget)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001352{
1353 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001354}
1355
Tony Barbour6918cd52015-04-09 12:58:51 -06001356void VkCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001357{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001358 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001359}
1360
Tony Barbour6918cd52015-04-09 12:58:51 -06001361void VkCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001362{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001363 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001364}
1365
Tony Barbour6918cd52015-04-09 12:58:51 -06001366void VkCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001367{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001368 QueueCommandBuffer(NULL);
1369}
1370
Tony Barbour6918cd52015-04-09 12:58:51 -06001371void VkCommandBufferObj::QueueCommandBuffer(VK_FENCE fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001372{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001373 VK_RESULT err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001374
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001375 mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
1376
Tony Barbour30cc9e82014-12-17 11:53:55 -07001377 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001378 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1379 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001380
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001381 err = vkQueueWaitIdle( m_device->m_queue );
1382 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001383
1384 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001385 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001386
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001387 /*
1388 * Now that processing on this command buffer is complete
1389 * we can remove the memory references.
1390 */
1391 mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001392}
1393
Tony Barbour6918cd52015-04-09 12:58:51 -06001394void VkCommandBufferObj::BindPipeline(VkPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001395{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001396 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001397 mem_ref_mgr.AddMemoryRefs(pipeline);
1398}
1399
Tony Barbour6918cd52015-04-09 12:58:51 -06001400void VkCommandBufferObj::BindDescriptorSet(VkDescriptorSetObj &descriptorSet)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001401{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001402 VK_DESCRIPTOR_SET set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001403
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001404 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001405 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001406 descriptorSet.GetLayoutChain(), 0, 1, &set_obj, NULL );
1407
1408 // Add descriptor set mem refs to command buffer's list
1409 mem_ref_mgr.AddMemoryRefs(descriptorSet.memories());
1410 mem_ref_mgr.AddMemoryRefs(descriptorSet.mem_ref_mgr.mem_refs());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001411}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001412
Tony Barbour6918cd52015-04-09 12:58:51 -06001413void VkCommandBufferObj::BindIndexBuffer(VkIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001414{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001415 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001416 mem_ref_mgr.AddMemoryRefs(*indexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001417}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001418
Tony Barbour6918cd52015-04-09 12:58:51 -06001419void VkCommandBufferObj::BindVertexBuffer(VkConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001420{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001421 vkCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001422 mem_ref_mgr.AddMemoryRefs(*vertexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001423}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001424
Tony Barbour6918cd52015-04-09 12:58:51 -06001425VkDepthStencilObj::VkDepthStencilObj()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001426{
1427 m_initialized = false;
1428}
Tony Barbour6918cd52015-04-09 12:58:51 -06001429bool VkDepthStencilObj::Initialized()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001430{
1431 return m_initialized;
1432}
1433
Tony Barbour6918cd52015-04-09 12:58:51 -06001434VK_DEPTH_STENCIL_BIND_INFO* VkDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001435{
1436 return &m_depthStencilBindInfo;
1437}
1438
Tony Barbour6918cd52015-04-09 12:58:51 -06001439void VkDepthStencilObj::Init(VkDeviceObj *device, int32_t width, int32_t height)
Tony Barbour1c45ce02015-03-27 17:03:18 -06001440{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001441 VK_IMAGE_CREATE_INFO image_info;
1442 VK_DEPTH_STENCIL_VIEW_CREATE_INFO view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001443
1444 m_device = device;
1445 m_initialized = true;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001446 m_depth_stencil_fmt = VK_FMT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001447
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001448 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001449 image_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001450 image_info.imageType = VK_IMAGE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001451 image_info.format = m_depth_stencil_fmt;
1452 image_info.extent.width = width;
1453 image_info.extent.height = height;
1454 image_info.extent.depth = 1;
1455 image_info.mipLevels = 1;
1456 image_info.arraySize = 1;
1457 image_info.samples = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001458 image_info.tiling = VK_OPTIMAL_TILING;
1459 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001460 image_info.flags = 0;
1461 init(*m_device, image_info);
1462
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001463 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001464 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001465 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001466 view_info.mipLevel = 0;
1467 view_info.baseArraySlice = 0;
1468 view_info.arraySize = 1;
1469 view_info.flags = 0;
1470 view_info.image = obj();
1471 m_depthStencilView.init(*m_device, view_info);
1472
1473 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001474 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001475}