blob: 9acd5f36709ca7d875ba0df7a32990973b4937f9 [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
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060030XglRenderFramework::XglRenderFramework() :
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060031 m_cmdBuffer( VK_NULL_HANDLE ),
32 m_stateRaster( VK_NULL_HANDLE ),
33 m_colorBlend( VK_NULL_HANDLE ),
34 m_stateViewport( VK_NULL_HANDLE ),
35 m_stateDepthStencil( VK_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060036 m_width( 256.0 ), // default window width
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070037 m_height( 256.0 ), // default window height
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060038 m_render_target_fmt( VK_FMT_R8G8B8A8_UNORM ),
39 m_depth_stencil_fmt( VK_FMT_UNDEFINED ),
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070040 m_depth_clear_color( 1.0 ),
41 m_stencil_clear_color( 0 )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060042{
Tony Barbour1c45ce02015-03-27 17:03:18 -060043
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070044 // clear the back buffer to dark grey
45 m_clear_color.color.rawColor[0] = 64;
46 m_clear_color.color.rawColor[1] = 64;
47 m_clear_color.color.rawColor[2] = 64;
48 m_clear_color.color.rawColor[3] = 0;
49 m_clear_color.useRawValue = true;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060050}
51
52XglRenderFramework::~XglRenderFramework()
53{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060054
55}
56
57void XglRenderFramework::InitFramework()
58{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060059 VK_RESULT err;
60 VK_INSTANCE_CREATE_INFO instInfo = {};
61 instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -060062 instInfo.pNext = NULL;
63 instInfo.pAppInfo = &app_info;
64 instInfo.pAllocCb = NULL;
65 instInfo.extensionCount = 0;
66 instInfo.ppEnabledExtensionNames = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060067 err = vkCreateInstance(&instInfo, &this->inst);
68 ASSERT_VK_SUCCESS(err);
69 err = vkEnumerateGpus(inst, VK_MAX_PHYSICAL_GPUS, &this->gpu_count,
Jon Ashburn1e464892015-01-29 15:48:00 -070070 objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060071 ASSERT_VK_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070072 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060073
74 m_device = new XglDevice(0, objs[0]);
75 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -060076
77 m_depthStencil = new XglDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060078}
79
80void XglRenderFramework::ShutdownFramework()
81{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060082 if (m_colorBlend) vkDestroyObject(m_colorBlend);
83 if (m_stateDepthStencil) vkDestroyObject(m_stateDepthStencil);
84 if (m_stateRaster) vkDestroyObject(m_stateRaster);
85 if (m_cmdBuffer) vkDestroyObject(m_cmdBuffer);
86 if (m_framebuffer) vkDestroyObject(m_framebuffer);
87 if (m_renderPass) vkDestroyObject(m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060088
89 if (m_stateViewport) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060090 vkDestroyObject(m_stateViewport);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060091 }
Tobin Ehlis976fc162015-03-26 08:23:25 -060092 while (!m_renderTargets.empty()) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060093 vkDestroyObject(m_renderTargets.back()->targetView());
94 vkBindObjectMemory(m_renderTargets.back()->image(), 0, VK_NULL_HANDLE, 0);
95 vkDestroyObject(m_renderTargets.back()->image());
96 vkFreeMemory(m_renderTargets.back()->memory());
Tobin Ehlis976fc162015-03-26 08:23:25 -060097 m_renderTargets.pop_back();
98 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060099
Tony Barbour1c45ce02015-03-27 17:03:18 -0600100 delete m_depthStencil;
101
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600102 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800103 delete m_device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600104 vkDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600105}
106
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600107void XglRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600108{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600109 VK_RESULT err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600110
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600111 m_render_target_fmt = VK_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600112
113 // create a raster state (solid, back-face culling)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600114 VK_DYNAMIC_RS_STATE_CREATE_INFO raster = {};
115 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -0700116 raster.pointSize = 1.0;
117
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600118 err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster );
119 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600120
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600121 VK_DYNAMIC_CB_STATE_CREATE_INFO blend = {};
122 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
123 err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
124 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600125
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600126 VK_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {};
127 depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600128 depthStencil.minDepth = 0.f;
129 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700130 depthStencil.stencilFrontRef = 0;
131 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600132
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600133 err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
134 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600135
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600136 VK_CMD_BUFFER_CREATE_INFO cmdInfo = {};
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600137
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600138 cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700139 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
140
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600141 err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
142 ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed";
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600143}
144
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600145void XglRenderFramework::InitViewport(float width, float height)
146{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600147 VK_RESULT err;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600148
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600149 VK_VIEWPORT viewport;
150 VK_RECT scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600151
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600152 VK_DYNAMIC_VP_STATE_CREATE_INFO viewportCreate = {};
153 viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700154 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700155 viewport.originX = 0;
156 viewport.originY = 0;
157 viewport.width = 1.f * width;
158 viewport.height = 1.f * height;
159 viewport.minDepth = 0.f;
160 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700161 scissor.extent.width = width;
162 scissor.extent.height = height;
163 scissor.offset.x = 0;
164 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700165 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700166 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700167
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600168 err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
169 ASSERT_VK_SUCCESS( err );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600170 m_width = width;
171 m_height = height;
172}
173
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600174void XglRenderFramework::InitViewport()
175{
176 InitViewport(m_width, m_height);
177}
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600178void XglRenderFramework::InitRenderTarget()
179{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700180 InitRenderTarget(1);
181}
182
183void XglRenderFramework::InitRenderTarget(uint32_t targets)
184{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600185 InitRenderTarget(targets, NULL);
186}
187
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600188void XglRenderFramework::InitRenderTarget(VK_DEPTH_STENCIL_BIND_INFO *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600189{
190 InitRenderTarget(1, dsBinding);
191}
192
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600193void XglRenderFramework::InitRenderTarget(uint32_t targets, VK_DEPTH_STENCIL_BIND_INFO *dsBinding)
Tony Barbour1c45ce02015-03-27 17:03:18 -0600194{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600195 std::vector<VK_ATTACHMENT_LOAD_OP> load_ops;
196 std::vector<VK_ATTACHMENT_STORE_OP> store_ops;
197 std::vector<VK_CLEAR_COLOR> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600198
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600199 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800200
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700201 for (i = 0; i < targets; i++) {
Chia-I Wuf50ee212014-12-29 14:31:52 +0800202 XglImage *img = new XglImage(m_device);
203 img->init(m_width, m_height, m_render_target_fmt,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600204 VK_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
205 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700206 m_colorBindings[i].view = img->targetView();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600207 m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700208 m_renderTargets.push_back(img);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600209 load_ops.push_back(VK_ATTACHMENT_LOAD_OP_LOAD);
210 store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600211 clear_colors.push_back(m_clear_color);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600212// m_mem_ref_mgr.AddMemoryRefs(*img);
Chia-I Wuecebf752014-12-05 10:45:15 +0800213 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700214 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600215 VK_FRAMEBUFFER_CREATE_INFO fb_info = {};
216 fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600217 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700218 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600219 fb_info.pColorAttachments = m_colorBindings;
220 fb_info.pDepthStencilAttachment = dsBinding;
221 fb_info.sampleCount = 1;
222 fb_info.width = (uint32_t)m_width;
223 fb_info.height = (uint32_t)m_height;
224 fb_info.layers = 1;
225
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600226 vkCreateFramebuffer(device(), &fb_info, &m_framebuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600227
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600228 VK_RENDER_PASS_CREATE_INFO rp_info = {};
229 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700230 rp_info.renderArea.extent.width = m_width;
231 rp_info.renderArea.extent.height = m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600232
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700233 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600234 rp_info.pColorFormats = &m_render_target_fmt;
235 rp_info.pColorLayouts = &m_colorBindings[0].layout;
236 rp_info.pColorLoadOps = &load_ops[0];
237 rp_info.pColorStoreOps = &store_ops[0];
238 rp_info.pColorLoadClearValues = &clear_colors[0];
239 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600240 if (dsBinding) {
241 rp_info.depthStencilLayout = dsBinding->layout;
242 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600243 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600244 rp_info.depthLoadClearValue = m_depth_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600245 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
246 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600247 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600248 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
249 vkCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600250}
251
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600252
253
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600254XglDevice::XglDevice(uint32_t id, VK_PHYSICAL_GPU obj) :
255 vk_testing::Device(obj), id(id)
Chia-I Wufb1459b2014-12-29 15:23:20 +0800256{
257 init();
258
259 props = gpu().properties();
260 queue_props = &gpu().queue_properties()[0];
261}
262
263void XglDevice::get_device_queue()
264{
265 ASSERT_NE(true, graphics_queues().empty());
266 m_queue = graphics_queues()[0]->obj();
267}
268
Tony Barboure2c58df2014-11-25 13:18:32 -0700269XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
270{
271 m_device = device;
272 m_nextSlot = 0;
273
274}
275
Chia-I Wu11078b02015-01-04 16:27:24 +0800276XglDescriptorSetObj::~XglDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700277{
Chia-I Wu11078b02015-01-04 16:27:24 +0800278 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700279}
Tony Barbour82c39522014-12-04 14:33:33 -0700280
Chia-I Wu11078b02015-01-04 16:27:24 +0800281int XglDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700282{
Chia-I Wu11078b02015-01-04 16:27:24 +0800283 /* request a descriptor but do not update it */
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600284 VK_DESCRIPTOR_TYPE_COUNT tc = {};
285 tc.type = VK_DESCRIPTOR_TYPE_SHADER_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800286 tc.count = 1;
287 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700288
Chia-I Wu11078b02015-01-04 16:27:24 +0800289 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700290}
Tony Barbour82c39522014-12-04 14:33:33 -0700291
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600292int XglDescriptorSetObj::AppendBuffer(VK_DESCRIPTOR_TYPE type, XglConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700293{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600294 VK_DESCRIPTOR_TYPE_COUNT tc = {};
Chia-I Wu11078b02015-01-04 16:27:24 +0800295 tc.type = type;
296 tc.count = 1;
297 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700298
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600299 m_updateBuffers.push_back(vk_testing::DescriptorSet::update(type,
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600300 m_nextSlot, 0, 1, &constantBuffer.m_bufferViewInfo));
301
302 // Track mem references for this descriptor set object
303 mem_ref_mgr.AddMemoryRefs(constantBuffer);
Chia-I Wu11078b02015-01-04 16:27:24 +0800304
305 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700306}
Tony Barbour82c39522014-12-04 14:33:33 -0700307
Chia-I Wu11078b02015-01-04 16:27:24 +0800308int XglDescriptorSetObj::AppendSamplerTexture( XglSamplerObj* sampler, XglTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700309{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600310 VK_DESCRIPTOR_TYPE_COUNT tc = {};
311 tc.type = VK_DESCRIPTOR_TYPE_SAMPLER_TEXTURE;
Chia-I Wu11078b02015-01-04 16:27:24 +0800312 tc.count = 1;
313 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700314
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600315 VK_SAMPLER_IMAGE_VIEW_INFO tmp = {};
Courtney Goeltzenleuchterf53c3cb2015-04-14 14:55:44 -0600316 tmp.pSampler = sampler->obj();
Chia-I Wu11078b02015-01-04 16:27:24 +0800317 tmp.pImageView = &texture->m_textureViewInfo;
318 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700319
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600320 m_updateSamplerTextures.push_back(vk_testing::DescriptorSet::update(m_nextSlot, 0, 1,
321 (const VK_SAMPLER_IMAGE_VIEW_INFO *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700322
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600323 // Track mem references for the texture referenced here
324 mem_ref_mgr.AddMemoryRefs(*texture);
325
Chia-I Wu11078b02015-01-04 16:27:24 +0800326 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700327}
Chia-I Wu11078b02015-01-04 16:27:24 +0800328
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600329VK_DESCRIPTOR_SET_LAYOUT_CHAIN XglDescriptorSetObj::GetLayoutChain() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700330{
Chia-I Wu41126e52015-03-26 15:27:55 +0800331 return m_layout_chain.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700332}
333
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600334VK_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700335{
Chia-I Wu11078b02015-01-04 16:27:24 +0800336 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700337}
Tony Barboure2c58df2014-11-25 13:18:32 -0700338
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600339void XglDescriptorSetObj::CreateVKDescriptorSet(XglCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700340{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600341 // create VK_DESCRIPTOR_POOL
342 VK_DESCRIPTOR_POOL_CREATE_INFO pool = {};
343 pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Chia-I Wu985ba162015-03-26 13:14:16 +0800344 pool.count = m_type_counts.size();
345 pool.pTypeCount = &m_type_counts[0];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600346 init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700347
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600348 // create VK_DESCRIPTOR_SET_LAYOUT
349 vector<VK_DESCRIPTOR_SET_LAYOUT_BINDING> bindings;
Chia-I Wub41393e2015-03-26 15:04:41 +0800350 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800351 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800352 bindings[i].descriptorType = m_type_counts[i].type;
353 bindings[i].count = m_type_counts[i].count;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600354 bindings[i].stageFlags = VK_SHADER_STAGE_FLAGS_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800355 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700356 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800357
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600358 // create VK_DESCRIPTOR_SET_LAYOUT
359 VK_DESCRIPTOR_SET_LAYOUT_CREATE_INFO layout = {};
360 layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
Chia-I Wub41393e2015-03-26 15:04:41 +0800361 layout.count = bindings.size();
362 layout.pBinding = &bindings[0];
363
Chia-I Wu41126e52015-03-26 15:27:55 +0800364 m_layout.init(*m_device, layout);
365
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600366 vector<const vk_testing::DescriptorSetLayout *> layouts;
Chia-I Wu41126e52015-03-26 15:27:55 +0800367 layouts.push_back(&m_layout);
368 m_layout_chain.init(*m_device, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700369
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600370 // create VK_DESCRIPTOR_SET
371 m_set = alloc_sets(VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
Chia-I Wu11078b02015-01-04 16:27:24 +0800372
Chia-I Wu41126e52015-03-26 15:27:55 +0800373 // build the update array
374 vector<const void *> update_array;
Chia-I Wu11078b02015-01-04 16:27:24 +0800375
Chia-I Wu41126e52015-03-26 15:27:55 +0800376 for (int i = 0; i < m_updateBuffers.size(); i++) {
377 update_array.push_back(&m_updateBuffers[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800378 }
379 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
380 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
Chia-I Wu41126e52015-03-26 15:27:55 +0800381 update_array.push_back(&m_updateSamplerTextures[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800382 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800383
384 // do the updates
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600385 m_device->begin_descriptor_pool_update(VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu11078b02015-01-04 16:27:24 +0800386 clear_sets(*m_set);
Chia-I Wu41126e52015-03-26 15:27:55 +0800387 m_set->update(update_array);
Chia-I Wu985ba162015-03-26 13:14:16 +0800388 m_device->end_descriptor_pool_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700389}
Tony Barboure2c58df2014-11-25 13:18:32 -0700390
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800391XglImage::XglImage(XglDevice *dev)
392{
393 m_device = dev;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600394 m_imageInfo.view = VK_NULL_HANDLE;
395 m_imageInfo.layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800396}
397
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600398void XglImage::ImageMemoryBarrier(
399 XglCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600400 VK_IMAGE_ASPECT aspect,
401 VK_FLAGS output_mask /*=
402 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
403 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
404 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
405 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
406 VK_MEMORY_OUTPUT_COPY_BIT*/,
407 VK_FLAGS input_mask /*=
408 VK_MEMORY_INPUT_CPU_READ_BIT |
409 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
410 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
411 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
412 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
413 VK_MEMORY_INPUT_SHADER_READ_BIT |
414 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
415 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
416 VK_MEMORY_INPUT_COPY_BIT*/,
417 VK_IMAGE_LAYOUT image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600418{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600419 const VK_IMAGE_SUBRESOURCE_RANGE subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
420 VK_IMAGE_MEMORY_BARRIER barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600421 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
422 subresourceRange);
423
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600424 VK_IMAGE_MEMORY_BARRIER *pmemory_barrier = &barrier;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600425
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600426 VK_PIPE_EVENT pipe_events[] = { VK_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
427 VK_PIPELINE_BARRIER pipeline_barrier = {};
428 pipeline_barrier.sType = VK_STRUCTURE_TYPE_PIPELINE_BARRIER;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600429 pipeline_barrier.pNext = NULL;
430 pipeline_barrier.eventCount = 1;
431 pipeline_barrier.pEvents = pipe_events;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600432 pipeline_barrier.waitEvent = VK_WAIT_EVENT_TOP_OF_PIPE;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600433 pipeline_barrier.memBarrierCount = 1;
434 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
435
436 // write barrier to the command buffer
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600437 vkCmdPipelineBarrier(cmd_buf->obj(), &pipeline_barrier);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600438}
439
440void XglImage::SetLayout(XglCommandBufferObj *cmd_buf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600441 VK_IMAGE_ASPECT aspect,
442 VK_IMAGE_LAYOUT image_layout)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600443{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600444 VK_FLAGS output_mask, input_mask;
445 const VK_FLAGS all_cache_outputs =
446 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
447 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
448 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
449 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
450 VK_MEMORY_OUTPUT_COPY_BIT;
451 const VK_FLAGS all_cache_inputs =
452 VK_MEMORY_INPUT_CPU_READ_BIT |
453 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
454 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
455 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
456 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
457 VK_MEMORY_INPUT_SHADER_READ_BIT |
458 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
459 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
460 VK_MEMORY_INPUT_COPY_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600461
462 if (image_layout == m_imageInfo.layout) {
463 return;
464 }
465
466 switch (image_layout) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600467 case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
468 output_mask = VK_MEMORY_OUTPUT_COPY_BIT;
469 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_COPY_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600470 break;
471
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600472 case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
473 output_mask = VK_MEMORY_OUTPUT_COPY_BIT;
474 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_COPY_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600475 break;
476
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600477 case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL:
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600478 break;
479
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600480 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
481 output_mask = VK_MEMORY_OUTPUT_COPY_BIT;
482 input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_COPY_BIT;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600483 break;
484
485 default:
486 output_mask = all_cache_outputs;
487 input_mask = all_cache_inputs;
488 break;
489 }
490
491 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
492 m_imageInfo.layout = image_layout;
493}
494
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600495void XglImage::SetLayout(VK_IMAGE_ASPECT aspect,
496 VK_IMAGE_LAYOUT image_layout)
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600497{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600498 VK_RESULT err;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600499 XglCommandBufferObj cmd_buf(m_device);
500
501 /* Build command buffer to set image layout in the driver */
502 err = cmd_buf.BeginCommandBuffer();
503 assert(!err);
504
505 SetLayout(&cmd_buf, aspect, image_layout);
506
507 err = cmd_buf.EndCommandBuffer();
508 assert(!err);
509
510 cmd_buf.QueueCommandBuffer();
511}
512
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600513bool XglImage::IsCompatible(VK_FLAGS usage, VK_FLAGS features)
Tony Barbour579f7802015-04-03 15:11:43 -0600514{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600515 if ((usage & VK_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) &&
516 !(features & VK_FORMAT_IMAGE_SHADER_READ_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600517 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600518
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600519 if ((usage & VK_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT) &&
520 !(features & VK_FORMAT_IMAGE_SHADER_WRITE_BIT))
Tony Barbour579f7802015-04-03 15:11:43 -0600521 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600522
Tony Barbour579f7802015-04-03 15:11:43 -0600523 return true;
524}
525
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600526void XglImage::init(uint32_t w, uint32_t h,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600527 VK_FORMAT fmt, VK_FLAGS usage,
528 VK_IMAGE_TILING requested_tiling)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800529{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600530 uint32_t mipCount;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600531 VK_FORMAT_PROPERTIES image_fmt;
532 VK_IMAGE_TILING tiling;
533 VK_RESULT err;
Tony Barbour579f7802015-04-03 15:11:43 -0600534 size_t size;
535
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800536 mipCount = 0;
537
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600538 uint32_t _w = w;
539 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800540 while( ( _w > 0 ) || ( _h > 0 ) )
541 {
542 _w >>= 1;
543 _h >>= 1;
544 mipCount++;
545 }
546
Tony Barbour579f7802015-04-03 15:11:43 -0600547 size = sizeof(image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600548 err = vkGetFormatInfo(m_device->obj(), fmt,
549 VK_INFO_TYPE_FORMAT_PROPERTIES,
Tony Barbour579f7802015-04-03 15:11:43 -0600550 &size, &image_fmt);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600551 ASSERT_VK_SUCCESS(err);
Tony Barbour579f7802015-04-03 15:11:43 -0600552
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600553 if (requested_tiling == VK_LINEAR_TILING) {
Tony Barbour579f7802015-04-03 15:11:43 -0600554 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600555 tiling = VK_LINEAR_TILING;
Tony Barbour579f7802015-04-03 15:11:43 -0600556 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600557 tiling = VK_OPTIMAL_TILING;
Tony Barbour579f7802015-04-03 15:11:43 -0600558 } else {
559 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
560 }
561 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600562 tiling = VK_OPTIMAL_TILING;
Tony Barbour579f7802015-04-03 15:11:43 -0600563 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600564 tiling = VK_LINEAR_TILING;
Tony Barbour579f7802015-04-03 15:11:43 -0600565 } else {
566 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
567 }
568
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600569 VK_IMAGE_CREATE_INFO imageCreateInfo = vk_testing::Image::create_info();
570 imageCreateInfo.imageType = VK_IMAGE_2D;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800571 imageCreateInfo.format = fmt;
572 imageCreateInfo.extent.width = w;
573 imageCreateInfo.extent.height = h;
574 imageCreateInfo.mipLevels = mipCount;
575 imageCreateInfo.tiling = tiling;
576
577 imageCreateInfo.usage = usage;
578
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600579 vk_testing::Image::init(*m_device, imageCreateInfo);
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800580
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600581 if (usage & VK_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) {
582 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600583 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600584 SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL);
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600585 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800586}
587
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600588VK_RESULT XglImage::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800589{
590 *ptr = map();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600591 return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800592}
593
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600594VK_RESULT XglImage::UnmapMemory()
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800595{
596 unmap();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600597 return VK_SUCCESS;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800598}
599
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600600VK_RESULT XglImage::CopyImage(XglImage &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600601{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600602 VK_RESULT err;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600603 XglCommandBufferObj cmd_buf(m_device);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600604 VK_IMAGE_LAYOUT src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600605
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600606 /* Build command buffer to copy staging texture to usable texture */
607 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600608 assert(!err);
609
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600610 /* TODO: Can we determine image aspect from image object? */
611 src_image_layout = src_image.layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600612 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600613
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600614 dest_image_layout = this->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600615 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600616
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600617 VK_IMAGE_COPY copy_region = {};
618 copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600619 copy_region.srcSubresource.arraySlice = 0;
620 copy_region.srcSubresource.mipLevel = 0;
621 copy_region.srcOffset.x = 0;
622 copy_region.srcOffset.y = 0;
623 copy_region.srcOffset.z = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600624 copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600625 copy_region.destSubresource.arraySlice = 0;
626 copy_region.destSubresource.mipLevel = 0;
627 copy_region.destOffset.x = 0;
628 copy_region.destOffset.y = 0;
629 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600630 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600631
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600632 vkCmdCopyImage(cmd_buf.obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600633 src_image.obj(), src_image.layout(),
634 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600635 1, &copy_region);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600636 cmd_buf.mem_ref_mgr.AddMemoryRefs(src_image);
637 cmd_buf.mem_ref_mgr.AddMemoryRefs(*this);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600638
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600639 src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600640
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600641 this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600642
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600643 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600644 assert(!err);
645
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600646 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600647
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600648 return VK_SUCCESS;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600649}
650
Tony Barbourebc093f2015-04-01 16:38:10 -0600651XglTextureObj::XglTextureObj(XglDevice *device, uint32_t *colors)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600652 :XglImage(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700653{
654 m_device = device;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600655 const VK_FORMAT tex_format = VK_FMT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600656 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600657 void *data;
658 int32_t x, y;
659 XglImage stagingImage(device);
660
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600661 stagingImage.init(16, 16, tex_format, 0, VK_LINEAR_TILING);
662 VK_SUBRESOURCE_LAYOUT layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600663
664 if (colors == NULL)
665 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700666
667 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
668
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600669 m_textureViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700670
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600671 VK_IMAGE_VIEW_CREATE_INFO view = {};
672 view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600673 view.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600674 view.image = VK_NULL_HANDLE;
675 view.viewType = VK_IMAGE_VIEW_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600676 view.format = tex_format;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600677 view.channels.r = VK_CHANNEL_SWIZZLE_R;
678 view.channels.g = VK_CHANNEL_SWIZZLE_G;
679 view.channels.b = VK_CHANNEL_SWIZZLE_B;
680 view.channels.a = VK_CHANNEL_SWIZZLE_A;
681 view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600682 view.subresourceRange.baseMipLevel = 0;
683 view.subresourceRange.mipLevels = 1;
684 view.subresourceRange.baseArraySlice = 0;
685 view.subresourceRange.arraySize = 1;
686 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700687
Tony Barboure2c58df2014-11-25 13:18:32 -0700688 /* create image */
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600689 init(16, 16, tex_format, VK_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, VK_OPTIMAL_TILING);
Tony Barboure2c58df2014-11-25 13:18:32 -0700690
691 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800692 view.image = obj();
693 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600694 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700695
Tony Barbour5dc515d2015-04-01 17:47:06 -0600696 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700697
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800698 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700699 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800700 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600701 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700702 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600703 stagingImage.unmap();
704 XglImage::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700705}
Tony Barbour82c39522014-12-04 14:33:33 -0700706
Tony Barboure2c58df2014-11-25 13:18:32 -0700707XglSamplerObj::XglSamplerObj(XglDevice *device)
708{
Tony Barboure2c58df2014-11-25 13:18:32 -0700709 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700710
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600711 VK_SAMPLER_CREATE_INFO samplerCreateInfo;
Chia-I Wue9864b52014-12-28 16:32:24 +0800712 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600713 samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
714 samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST;
715 samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST;
716 samplerCreateInfo.mipMode = VK_TEX_MIPMAP_BASE;
717 samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP;
718 samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP;
719 samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP;
Chia-I Wue9864b52014-12-28 16:32:24 +0800720 samplerCreateInfo.mipLodBias = 0.0;
721 samplerCreateInfo.maxAnisotropy = 0.0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600722 samplerCreateInfo.compareFunc = VK_COMPARE_NEVER;
Chia-I Wue9864b52014-12-28 16:32:24 +0800723 samplerCreateInfo.minLod = 0.0;
724 samplerCreateInfo.maxLod = 0.0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600725 samplerCreateInfo.borderColorType = VK_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700726
Chia-I Wue9864b52014-12-28 16:32:24 +0800727 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700728}
Tony Barboure2c58df2014-11-25 13:18:32 -0700729
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700730/*
731 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
732 */
733XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
734{
735 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700736 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700737
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800738 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700739}
740
Tony Barbour044703b2015-03-26 12:37:52 -0600741XglConstantBufferObj::~XglConstantBufferObj()
742{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600743 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600744 if (m_commandBuffer) {
745 delete m_commandBuffer;
746 }
747}
748
Tony Barboure2c58df2014-11-25 13:18:32 -0700749XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
750{
Tony Barboure2c58df2014-11-25 13:18:32 -0700751 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800752 m_commandBuffer = 0;
753
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800754 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700755 m_numVertices = constantCount;
756 m_stride = constantSize;
757
Chia-I Wua07fee62014-12-28 15:26:08 +0800758 const size_t allocationSize = constantCount * constantSize;
759 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700760
Chia-I Wua07fee62014-12-28 15:26:08 +0800761 void *pData = map();
762 memcpy(pData, data, allocationSize);
763 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700764
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800765 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600766 VK_BUFFER_VIEW_CREATE_INFO view_info = {};
767 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800768 view_info.buffer = obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600769 view_info.viewType = VK_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800770 view_info.offset = 0;
771 view_info.range = allocationSize;
772 m_bufferView.init(*m_device, view_info);
773
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600774 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800775 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700776}
Tony Barbour82c39522014-12-04 14:33:33 -0700777
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600778void XglConstantBufferObj::Bind(VK_CMD_BUFFER cmdBuffer, VK_GPU_SIZE offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700779{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600780 vkCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700781}
782
783
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000784void XglConstantBufferObj::BufferMemoryBarrier(
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600785 VK_FLAGS outputMask /*=
786 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
787 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
788 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
789 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
790 VK_MEMORY_OUTPUT_COPY_BIT*/,
791 VK_FLAGS inputMask /*=
792 VK_MEMORY_INPUT_CPU_READ_BIT |
793 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
794 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
795 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
796 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
797 VK_MEMORY_INPUT_SHADER_READ_BIT |
798 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
799 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
800 VK_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700801{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600802 VK_RESULT err = VK_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700803
Tony Barbour38422802014-12-10 14:36:31 -0700804 if (!m_commandBuffer)
805 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600806 m_fence.init(*m_device, vk_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700807
808 m_commandBuffer = new XglCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700809 }
810 else
811 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800812 m_device->wait(m_fence);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600813 m_commandBuffer->mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour38422802014-12-10 14:36:31 -0700814 }
815
Tony Barboure2c58df2014-11-25 13:18:32 -0700816 // open the command buffer
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600817 VK_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {};
818 cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600819 cmd_buf_info.pNext = NULL;
820 cmd_buf_info.flags = 0;
821
Jon Ashburnc4164b12014-12-31 17:10:47 -0700822 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600823 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700824
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600825 VK_BUFFER_MEMORY_BARRIER memory_barrier =
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000826 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600827 VK_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700828
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600829 VK_PIPE_EVENT set_events[] = { VK_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
830 VK_PIPELINE_BARRIER pipeline_barrier = {};
831 pipeline_barrier.sType = VK_STRUCTURE_TYPE_PIPELINE_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000832 pipeline_barrier.eventCount = 1;
833 pipeline_barrier.pEvents = set_events;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600834 pipeline_barrier.waitEvent = VK_WAIT_EVENT_TOP_OF_PIPE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000835 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -0600836 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000837
838 // write barrier to the command buffer
839 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700840
841 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700842 err = m_commandBuffer->EndCommandBuffer();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600843 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700844
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600845 /*
846 * Tell driver about memory references made in this command buffer
847 * Note: Since this command buffer only has a PipelineBarrier
848 * command there really aren't any memory refs to worry about.
849 */
850 m_commandBuffer->mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
Tony Barboure2c58df2014-11-25 13:18:32 -0700851
852 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600853 VK_CMD_BUFFER bufferArray[1];
Tony Barbour471338d2014-12-10 17:28:39 -0700854 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600855 err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
856 ASSERT_VK_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700857}
858
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700859XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
860 : XglConstantBufferObj(device)
861{
862
863}
864
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600865void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, VK_INDEX_TYPE indexType, const void* data)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700866{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600867 VK_FORMAT viewFormat;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700868
869 m_numVertices = numIndexes;
870 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700871 switch (indexType) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600872 case VK_INDEX_8:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700873 m_stride = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600874 viewFormat = VK_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700875 break;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600876 case VK_INDEX_16:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700877 m_stride = 2;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600878 viewFormat = VK_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700879 break;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600880 case VK_INDEX_32:
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700881 m_stride = 4;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600882 viewFormat = VK_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700883 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800884 default:
885 assert(!"unknown index type");
886 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700887 }
888
Chia-I Wua07fee62014-12-28 15:26:08 +0800889 const size_t allocationSize = numIndexes * m_stride;
890 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700891
Chia-I Wua07fee62014-12-28 15:26:08 +0800892 void *pData = map();
893 memcpy(pData, data, allocationSize);
894 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700895
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800896 // set up the buffer view for the constant buffer
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600897 VK_BUFFER_VIEW_CREATE_INFO view_info = {};
898 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800899 view_info.buffer = obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600900 view_info.viewType = VK_BUFFER_VIEW_TYPED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700901 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800902 view_info.offset = 0;
903 view_info.range = allocationSize;
904 m_bufferView.init(*m_device, view_info);
905
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600906 this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800907 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700908}
909
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600910void XglIndexBufferObj::Bind(VK_CMD_BUFFER cmdBuffer, VK_GPU_SIZE offset)
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700911{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600912 vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700913}
Tony Barboure2c58df2014-11-25 13:18:32 -0700914
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600915VK_INDEX_TYPE XglIndexBufferObj::GetIndexType()
Tony Barbouraf1f9192014-12-17 10:57:58 -0700916{
917 return m_indexType;
918}
919
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600920VK_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700921{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600922 VK_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (VK_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(VK_PIPELINE_SHADER_STAGE_CREATE_INFO) );
923 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700924 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800925 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700926 stageInfo->shader.linkConstBufferCount = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600927 stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700928
Tony Barboure2c58df2014-11-25 13:18:32 -0700929 return stageInfo;
930}
931
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600932XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, VK_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
Tony Barboure2c58df2014-11-25 13:18:32 -0700933{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600934 VK_RESULT err = VK_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600935 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600936 VK_SHADER_CREATE_INFO createInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700937 size_t shader_len;
938
939 m_stage = stage;
940 m_device = device;
941
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600942 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700943 createInfo.pNext = NULL;
944
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600945 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700946
947 shader_len = strlen(shader_code);
948 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
949 createInfo.pCode = malloc(createInfo.codeSize);
950 createInfo.flags = 0;
951
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600952 /* try version 0 first: VK_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600953 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700954 ((uint32_t *) createInfo.pCode)[1] = 0;
955 ((uint32_t *) createInfo.pCode)[2] = stage;
956 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
957
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800958 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700959 }
960
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600961 if (framework->m_use_spv || err) {
962 std::vector<unsigned int> spv;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600963 err = VK_SUCCESS;
Tony Barboure2c58df2014-11-25 13:18:32 -0700964
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600965 // Use Reference GLSL to SPV compiler
966 framework->GLSLtoSPV(stage, shader_code, spv);
967 createInfo.pCode = spv.data();
968 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700969 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700970
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800971 init(*m_device, createInfo);
972 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700973}
Tony Barbour82c39522014-12-04 14:33:33 -0700974
Tony Barboure2c58df2014-11-25 13:18:32 -0700975XglPipelineObj::XglPipelineObj(XglDevice *device)
976{
Tony Barboure2c58df2014-11-25 13:18:32 -0700977 m_device = device;
978 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
979 m_vertexBufferCount = 0;
980
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600981 m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
982 m_ia_state.pNext = VK_NULL_HANDLE;
983 m_ia_state.topology = VK_TOPOLOGY_TRIANGLE_LIST;
984 m_ia_state.disableVertexReuse = VK_FALSE;
985 m_ia_state.primitiveRestartEnable = VK_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700986 m_ia_state.primitiveRestartIndex = 0;
987
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600988 m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -0700989 m_rs_state.pNext = &m_ia_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600990 m_rs_state.depthClipEnable = VK_FALSE;
991 m_rs_state.rasterizerDiscardEnable = VK_FALSE;
992 m_rs_state.programPointSize = VK_FALSE;
993 m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
994 m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST;
995 m_rs_state.fillMode = VK_FILL_SOLID;
996 m_rs_state.cullMode = VK_CULL_NONE;
997 m_rs_state.frontFace = VK_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700998
Tony Barboure2c58df2014-11-25 13:18:32 -0700999 memset(&m_cb_state,0,sizeof(m_cb_state));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001000 m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barboure2c58df2014-11-25 13:18:32 -07001001 m_cb_state.pNext = &m_rs_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001002 m_cb_state.alphaToCoverageEnable = VK_FALSE;
1003 m_cb_state.logicOp = VK_LOGIC_OP_COPY;
Tony Barboure2c58df2014-11-25 13:18:32 -07001004
Tony Barbourf52346d2015-01-16 14:27:35 -07001005 m_ms_state.pNext = &m_cb_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001006 m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1007 m_ms_state.multisampleEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001008 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1009 m_ms_state.samples = 1;
1010 m_ms_state.minSampleShading = 0;
1011 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001012
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001013 m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourf52346d2015-01-16 14:27:35 -07001014 m_ds_state.pNext = &m_ms_state,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001015 m_ds_state.format = VK_FMT_D32_SFLOAT;
1016 m_ds_state.depthTestEnable = VK_FALSE;
1017 m_ds_state.depthWriteEnable = VK_FALSE;
1018 m_ds_state.depthBoundsEnable = VK_FALSE;
1019 m_ds_state.depthFunc = VK_COMPARE_LESS_EQUAL;
1020 m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
1021 m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1022 m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP;
1023 m_ds_state.back.stencilFunc = VK_COMPARE_ALWAYS;
1024 m_ds_state.stencilTestEnable = VK_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -07001025 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001026
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001027 VK_PIPELINE_CB_ATTACHMENT_STATE att = {};
1028 att.blendEnable = VK_FALSE;
1029 att.format = VK_FMT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001030 att.channelWriteMask = 0xf;
1031 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001032
1033};
1034
1035void XglPipelineObj::AddShader(XglShaderObj* shader)
1036{
1037 m_shaderObjs.push_back(shader);
1038}
1039
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001040void XglPipelineObj::AddVertexInputAttribs(VK_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001041{
1042 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1043 m_vi_state.attributeCount = count;
1044}
1045
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001046void XglPipelineObj::AddVertexInputBindings(VK_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
Tony Barboure2c58df2014-11-25 13:18:32 -07001047{
1048 m_vi_state.pVertexBindingDescriptions = vi_binding;
1049 m_vi_state.bindingCount = count;
1050}
1051
1052void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
1053{
1054 m_vertexBufferObjs.push_back(vertexDataBuffer);
1055 m_vertexBufferBindings.push_back(binding);
1056 m_vertexBufferCount++;
1057}
1058
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001059void XglPipelineObj::AddColorAttachment(uint32_t binding, const VK_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001060{
Tony Barbourf52346d2015-01-16 14:27:35 -07001061 if (binding+1 > m_colorAttachments.size())
1062 {
1063 m_colorAttachments.resize(binding+1);
1064 }
1065 m_colorAttachments[binding] = *att;
1066}
1067
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001068void XglPipelineObj::SetDepthStencil(VK_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
Tony Barbourf52346d2015-01-16 14:27:35 -07001069{
1070 m_ds_state.format = ds_state->format;
1071 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1072 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1073 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
1074 m_ds_state.depthFunc = ds_state->depthFunc;
1075 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1076 m_ds_state.back = ds_state->back;
1077 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001078}
1079
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001080void XglPipelineObj::CreateVKPipeline(XglDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001081{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001082 void* head_ptr = &m_ds_state;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001083 VK_GRAPHICS_PIPELINE_CREATE_INFO info = {};
Tony Barbour976e1cf2014-12-17 11:57:31 -07001084
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001085 VK_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001086
1087 for (int i=0; i<m_shaderObjs.size(); i++)
1088 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001089 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001090 shaderCreateInfo->pNext = head_ptr;
1091 head_ptr = shaderCreateInfo;
1092 }
1093
1094 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1095 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001096 m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001097 m_vi_state.pNext = head_ptr;
1098 head_ptr = &m_vi_state;
1099 }
1100
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001101 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001102 info.pNext = head_ptr;
1103 info.flags = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001104 info.pSetLayoutChain = descriptorSet.GetLayoutChain();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001105
Tony Barbourf52346d2015-01-16 14:27:35 -07001106 m_cb_state.attachmentCount = m_colorAttachments.size();
1107 m_cb_state.pAttachments = &m_colorAttachments[0];
1108
Chia-I Wu2648d092014-12-29 14:24:14 +08001109 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001110}
Chia-I Wu2648d092014-12-29 14:24:14 +08001111
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001112vector<VK_GPU_MEMORY> XglMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001113{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001114 std::vector<VK_GPU_MEMORY> mems;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001115 if (this->mem_refs_.size()) {
1116 mems.reserve(this->mem_refs_.size());
1117 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1118 mems.push_back(this->mem_refs_[i]);
1119 }
1120
1121 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001122}
1123
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001124void XglMemoryRefManager::AddMemoryRefs(vk_testing::Object &vkObject)
Tony Barboure2c58df2014-11-25 13:18:32 -07001125{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001126 const std::vector<VK_GPU_MEMORY> mems = vkObject.memories();
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001127 AddMemoryRefs(mems);
1128}
Tony Barboure2c58df2014-11-25 13:18:32 -07001129
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001130void XglMemoryRefManager::AddMemoryRefs(vector<VK_GPU_MEMORY> mem)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001131{
1132 for (size_t i = 0; i < mem.size(); i++) {
1133 if (mem[i] != NULL) {
1134 this->mem_refs_.push_back(mem[i]);
1135 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001136 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001137}
Tony Barbour82c39522014-12-04 14:33:33 -07001138
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001139void XglMemoryRefManager::EmitAddMemoryRefs(VK_QUEUE queue)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001140{
1141 for (uint32_t i = 0; i < mem_refs_.size(); i++) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001142 vkQueueAddMemReference(queue, mem_refs_[i]);
Tony Barbour1c45ce02015-03-27 17:03:18 -06001143 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001144}
Tony Barbour82c39522014-12-04 14:33:33 -07001145
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001146void XglMemoryRefManager::EmitRemoveMemoryRefs(VK_QUEUE queue)
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001147{
1148 for (uint32_t i = 0; i < mem_refs_.size(); i++) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001149 vkQueueRemoveMemReference(queue, mem_refs_[i]);
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001150 }
1151}
1152
Tony Barbour6d047bf2014-12-10 14:34:45 -07001153XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001154 : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001155{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001156 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001157}
Tony Barbour471338d2014-12-10 17:28:39 -07001158
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001159VK_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
Tony Barbour6d047bf2014-12-10 14:34:45 -07001160{
Chia-I Wud28343c2014-12-28 15:12:48 +08001161 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001162}
Tony Barbour471338d2014-12-10 17:28:39 -07001163
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001164VK_RESULT XglCommandBufferObj::BeginCommandBuffer(VK_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001165{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001166 begin(pInfo);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001167 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001168}
1169
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001170VK_RESULT XglCommandBufferObj::BeginCommandBuffer(VK_RENDER_PASS renderpass_obj, VK_FRAMEBUFFER framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001171{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001172 begin(renderpass_obj, framebuffer_obj);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001173 return VK_SUCCESS;
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001174}
1175
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001176VK_RESULT XglCommandBufferObj::BeginCommandBuffer()
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001177{
1178 begin();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001179 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001180}
1181
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001182VK_RESULT XglCommandBufferObj::EndCommandBuffer()
Tony Barbour471338d2014-12-10 17:28:39 -07001183{
Chia-I Wud28343c2014-12-28 15:12:48 +08001184 end();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001185 return VK_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001186}
1187
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001188void XglCommandBufferObj::PipelineBarrier(VK_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001189{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001190 vkCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001191}
1192
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001193void XglCommandBufferObj::ClearAllBuffers(VK_CLEAR_COLOR clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001194 XglDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001195{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001196 uint32_t i;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001197 const VK_FLAGS output_mask =
1198 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
1199 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1200 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1201 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1202 VK_MEMORY_OUTPUT_COPY_BIT;
1203 const VK_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001204
1205 // whatever we want to do, we do it to the whole buffer
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001206 VK_IMAGE_SUBRESOURCE_RANGE srRange = {};
1207 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001208 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001209 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001210 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001211 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001212
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001213 VK_IMAGE_MEMORY_BARRIER memory_barrier = {};
1214 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001215 memory_barrier.outputMask = output_mask;
1216 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001217 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001218 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001219 VK_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001220
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001221 VK_PIPE_EVENT set_events[] = { VK_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
1222 VK_PIPELINE_BARRIER pipeline_barrier = {};
1223 pipeline_barrier.sType = VK_STRUCTURE_TYPE_PIPELINE_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001224 pipeline_barrier.eventCount = 1;
1225 pipeline_barrier.pEvents = set_events;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001226 pipeline_barrier.waitEvent = VK_WAIT_EVENT_TOP_OF_PIPE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001227 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001228 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001229
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001230 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001231 memory_barrier.image = m_renderTargets[i]->image();
1232 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001233 vkCmdPipelineBarrier( obj(), &pipeline_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001234 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001235
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001236 vkCmdClearColorImage(obj(),
1237 m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -06001238 clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001239
1240 mem_ref_mgr.AddMemoryRefs(*m_renderTargets[i]);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001241 }
1242
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001243 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001244 {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001245 VK_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1246 dsRange.aspect = VK_IMAGE_ASPECT_DEPTH;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001247 dsRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001248 dsRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001249 dsRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001250 dsRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001251
1252 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001253
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001254 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001255 memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001256 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001257 memory_barrier.subresourceRange = dsRange;
1258
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001259 vkCmdPipelineBarrier( obj(), &pipeline_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001260
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001261 vkCmdClearDepthStencil(obj(),
1262 depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001263 depth_clear_color, stencil_clear_color,
1264 1, &dsRange);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001265 mem_ref_mgr.AddMemoryRefs(*depthStencilObj);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001266
1267 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001268 memory_barrier.image = depthStencilObj->obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001269 memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001270 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001271 memory_barrier.subresourceRange = dsRange;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001272 vkCmdPipelineBarrier( obj(), &pipeline_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001273 }
1274}
1275
Jon Ashburncdc40be2015-01-02 18:27:14 -07001276void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001277{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001278 uint32_t i;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001279 const VK_FLAGS output_mask =
1280 VK_MEMORY_OUTPUT_CPU_WRITE_BIT |
1281 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1282 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1283 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1284 VK_MEMORY_OUTPUT_COPY_BIT;
1285 const VK_FLAGS input_mask =
1286 VK_MEMORY_INPUT_CPU_READ_BIT |
1287 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1288 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1289 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1290 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1291 VK_MEMORY_INPUT_SHADER_READ_BIT |
1292 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1293 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1294 VK_MEMORY_INPUT_COPY_BIT;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001295
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001296 VK_IMAGE_SUBRESOURCE_RANGE srRange = {};
1297 srRange.aspect = VK_IMAGE_ASPECT_COLOR;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001298 srRange.baseMipLevel = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001299 srRange.mipLevels = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001300 srRange.baseArraySlice = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001301 srRange.arraySize = VK_LAST_MIP_OR_SLICE;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001302
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001303 VK_IMAGE_MEMORY_BARRIER memory_barrier = {};
1304 memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001305 memory_barrier.outputMask = output_mask;
1306 memory_barrier.inputMask = input_mask;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001307 memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001308 memory_barrier.subresourceRange = srRange;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001309 VK_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001310
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001311 VK_PIPE_EVENT set_events[] = { VK_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
1312 VK_PIPELINE_BARRIER pipeline_barrier = {};
1313 pipeline_barrier.sType = VK_STRUCTURE_TYPE_PIPELINE_BARRIER;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001314 pipeline_barrier.eventCount = 1;
1315 pipeline_barrier.pEvents = set_events;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001316 pipeline_barrier.waitEvent = VK_WAIT_EVENT_TOP_OF_PIPE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001317 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001318 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001319
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001320 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001321 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001322 memory_barrier.image = m_renderTargets[i]->image();
1323 memory_barrier.oldLayout = m_renderTargets[i]->layout();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001324 vkCmdPipelineBarrier( obj(), &pipeline_barrier);
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001325 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001326 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001327}
1328
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001329void XglCommandBufferObj::BeginRenderPass(VK_RENDER_PASS renderpass, VK_FRAMEBUFFER framebuffer)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001330{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001331 VK_RENDER_PASS_BEGIN rp_begin = {
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001332 renderpass,
1333 framebuffer,
1334 };
1335
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001336 vkCmdBeginRenderPass( obj(), &rp_begin);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001337}
1338
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001339void XglCommandBufferObj::EndRenderPass(VK_RENDER_PASS renderpass)
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001340{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001341 vkCmdEndRenderPass( obj(), renderpass);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001342}
1343
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001344void XglCommandBufferObj::BindStateObject(VK_STATE_BIND_POINT stateBindPoint, VK_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001345{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001346 vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001347}
1348
1349void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1350{
1351 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001352}
1353
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001354void XglCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001355{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001356 vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001357}
1358
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001359void XglCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001360{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001361 vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001362}
1363
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001364void XglCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001365{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001366 QueueCommandBuffer(NULL);
1367}
1368
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001369void XglCommandBufferObj::QueueCommandBuffer(VK_FENCE fence)
Tony Barbour09b7ac32015-04-08 10:11:29 -06001370{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001371 VK_RESULT err = VK_SUCCESS;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001372
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001373 mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
1374
Tony Barbour30cc9e82014-12-17 11:53:55 -07001375 // submit the command buffer to the universal queue
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001376 err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence );
1377 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001378
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001379 err = vkQueueWaitIdle( m_device->m_queue );
1380 ASSERT_VK_SUCCESS( err );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001381
1382 // Wait for work to finish before cleaning up.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001383 vkDeviceWaitIdle(m_device->device());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001384
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001385 /*
1386 * Now that processing on this command buffer is complete
1387 * we can remove the memory references.
1388 */
1389 mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001390}
1391
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001392void XglCommandBufferObj::BindPipeline(XglPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001393{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001394 vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001395 mem_ref_mgr.AddMemoryRefs(pipeline);
1396}
1397
1398void XglCommandBufferObj::BindDescriptorSet(XglDescriptorSetObj &descriptorSet)
1399{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001400 VK_DESCRIPTOR_SET set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001401
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001402 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001403 vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001404 descriptorSet.GetLayoutChain(), 0, 1, &set_obj, NULL );
1405
1406 // Add descriptor set mem refs to command buffer's list
1407 mem_ref_mgr.AddMemoryRefs(descriptorSet.memories());
1408 mem_ref_mgr.AddMemoryRefs(descriptorSet.mem_ref_mgr.mem_refs());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001409}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001410
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001411void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001412{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001413 vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001414 mem_ref_mgr.AddMemoryRefs(*indexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001415}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001416
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001417void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001418{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001419 vkCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001420 mem_ref_mgr.AddMemoryRefs(*vertexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001421}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001422
Tony Barbour1c45ce02015-03-27 17:03:18 -06001423XglDepthStencilObj::XglDepthStencilObj()
1424{
1425 m_initialized = false;
1426}
1427bool XglDepthStencilObj::Initialized()
1428{
1429 return m_initialized;
1430}
1431
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001432VK_DEPTH_STENCIL_BIND_INFO* XglDepthStencilObj::BindInfo()
Tony Barbour1c45ce02015-03-27 17:03:18 -06001433{
1434 return &m_depthStencilBindInfo;
1435}
1436
1437void XglDepthStencilObj::Init(XglDevice *device, int32_t width, int32_t height)
1438{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001439 VK_IMAGE_CREATE_INFO image_info;
1440 VK_DEPTH_STENCIL_VIEW_CREATE_INFO view_info;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001441
1442 m_device = device;
1443 m_initialized = true;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001444 m_depth_stencil_fmt = VK_FMT_D16_UNORM;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001445
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001446 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001447 image_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001448 image_info.imageType = VK_IMAGE_2D;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001449 image_info.format = m_depth_stencil_fmt;
1450 image_info.extent.width = width;
1451 image_info.extent.height = height;
1452 image_info.extent.depth = 1;
1453 image_info.mipLevels = 1;
1454 image_info.arraySize = 1;
1455 image_info.samples = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001456 image_info.tiling = VK_OPTIMAL_TILING;
1457 image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001458 image_info.flags = 0;
1459 init(*m_device, image_info);
1460
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001461 view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001462 view_info.pNext = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001463 view_info.image = VK_NULL_HANDLE;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001464 view_info.mipLevel = 0;
1465 view_info.baseArraySlice = 0;
1466 view_info.arraySize = 1;
1467 view_info.flags = 0;
1468 view_info.image = obj();
1469 m_depthStencilView.init(*m_device, view_info);
1470
1471 m_depthStencilBindInfo.view = m_depthStencilView.obj();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001472 m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Tony Barbour1c45ce02015-03-27 17:03:18 -06001473}