blob: 9a46f78ea37e31d28a6d6ad5544620d87d9a921d [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
2 * XGL Tests
3 *
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
28#include "xglrenderframework.h"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060030XglRenderFramework::XglRenderFramework() :
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060031 m_cmdBuffer( XGL_NULL_HANDLE ),
Chia-I Wu837f9952014-12-15 23:29:34 +080032 m_stateRaster( XGL_NULL_HANDLE ),
33 m_colorBlend( XGL_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060034 m_stateViewport( XGL_NULL_HANDLE ),
Chia-I Wu837f9952014-12-15 23:29:34 +080035 m_stateDepthStencil( XGL_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
38 m_render_target_fmt( XGL_FMT_R8G8B8A8_UNORM ),
39 m_depth_stencil_fmt( XGL_FMT_UNDEFINED ),
40 m_depth_clear_color( 1.0 ),
41 m_stencil_clear_color( 0 )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060042{
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070043 // clear the back buffer to dark grey
44 m_clear_color.color.rawColor[0] = 64;
45 m_clear_color.color.rawColor[1] = 64;
46 m_clear_color.color.rawColor[2] = 64;
47 m_clear_color.color.rawColor[3] = 0;
48 m_clear_color.useRawValue = true;
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060049
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060050 m_depthStencilBinding.view = XGL_NULL_HANDLE;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060051}
52
53XglRenderFramework::~XglRenderFramework()
54{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060055
56}
57
58void XglRenderFramework::InitFramework()
59{
60 XGL_RESULT err;
61
Jon Ashburn1e464892015-01-29 15:48:00 -070062 err = xglCreateInstance(&app_info, NULL, &this->inst);
63 ASSERT_XGL_SUCCESS(err);
64 err = xglEnumerateGpus(inst, XGL_MAX_PHYSICAL_GPUS, &this->gpu_count,
65 objs);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060066 ASSERT_XGL_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070067 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060068
69 m_device = new XglDevice(0, objs[0]);
70 m_device->get_device_queue();
71}
72
73void XglRenderFramework::ShutdownFramework()
74{
75 if (m_colorBlend) xglDestroyObject(m_colorBlend);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060076 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
77 if (m_stateRaster) xglDestroyObject(m_stateRaster);
78 if (m_cmdBuffer) xglDestroyObject(m_cmdBuffer);
Tobin Ehlis976fc162015-03-26 08:23:25 -060079 if (m_frameBuffer) xglDestroyObject(m_frameBuffer);
80 if (m_renderPass) xglDestroyObject(m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060081
82 if (m_stateViewport) {
83 xglDestroyObject(m_stateViewport);
84 }
Tobin Ehlis976fc162015-03-26 08:23:25 -060085 while (!m_renderTargets.empty()) {
86 xglDestroyObject(m_renderTargets.back()->targetView());
87 xglBindObjectMemory(m_renderTargets.back()->image(), 0, XGL_NULL_HANDLE, 0);
88 xglDestroyObject(m_renderTargets.back()->image());
89 xglFreeMemory(m_renderTargets.back()->memory());
90 m_renderTargets.pop_back();
91 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060092
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060093 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +080094 delete m_device;
Jon Ashburn1e464892015-01-29 15:48:00 -070095 xglDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060096}
97
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060098void XglRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060099{
100 XGL_RESULT err;
101
Tony Barboura53a6942015-02-25 11:25:11 -0700102 m_render_target_fmt = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600103
104 // create a raster state (solid, back-face culling)
Tony Barbourf52346d2015-01-16 14:27:35 -0700105 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster = {};
106 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
107 raster.pointSize = 1.0;
108
109 err = xglCreateDynamicRasterState( device(), &raster, &m_stateRaster );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600110 ASSERT_XGL_SUCCESS(err);
111
Tony Barbourf52346d2015-01-16 14:27:35 -0700112 XGL_DYNAMIC_CB_STATE_CREATE_INFO blend = {};
113 blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
114 err = xglCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600115 ASSERT_XGL_SUCCESS( err );
116
Tony Barbourf52346d2015-01-16 14:27:35 -0700117 XGL_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {};
118 depthStencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600119 depthStencil.minDepth = 0.f;
120 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700121 depthStencil.stencilFrontRef = 0;
122 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600123
Tony Barbourf52346d2015-01-16 14:27:35 -0700124 err = xglCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600125 ASSERT_XGL_SUCCESS( err );
126
127 XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {};
128
129 cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700130 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
131
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600132 err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
133 ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed";
134}
135
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600136void XglRenderFramework::InitViewport(float width, float height)
137{
138 XGL_RESULT err;
139
Tony Barbourf52346d2015-01-16 14:27:35 -0700140 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700141 XGL_RECT scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600142
Tony Barbourf52346d2015-01-16 14:27:35 -0700143 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewportCreate = {};
144 viewportCreate.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700145 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700146 viewport.originX = 0;
147 viewport.originY = 0;
148 viewport.width = 1.f * width;
149 viewport.height = 1.f * height;
150 viewport.minDepth = 0.f;
151 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700152 scissor.extent.width = width;
153 scissor.extent.height = height;
154 scissor.offset.x = 0;
155 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700156 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700157 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700158
159 err = xglCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600160 ASSERT_XGL_SUCCESS( err );
161 m_width = width;
162 m_height = height;
163}
164
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600165void XglRenderFramework::InitViewport()
166{
167 InitViewport(m_width, m_height);
168}
169
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600170void XglRenderFramework::InitRenderTarget()
171{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700172 InitRenderTarget(1);
173}
174
175void XglRenderFramework::InitRenderTarget(uint32_t targets)
176{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600177 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800178
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700179 for (i = 0; i < targets; i++) {
Chia-I Wuf50ee212014-12-29 14:31:52 +0800180 XglImage *img = new XglImage(m_device);
181 img->init(m_width, m_height, m_render_target_fmt,
182 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
183 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700184 m_colorBindings[i].view = img->targetView();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000185 m_colorBindings[i].layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700186 m_renderTargets.push_back(img);
Chia-I Wuecebf752014-12-05 10:45:15 +0800187 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700188 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
189 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_LOAD;
190 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_STORE;
Tobin Ehlis5de72572015-03-06 13:16:37 -0700191 XGL_CLEAR_COLOR clear_color;
192 clear_color.color.rawColor[0] = 64;
193 clear_color.color.rawColor[1] = 64;
194 clear_color.color.rawColor[2] = 64;
195 clear_color.color.rawColor[3] = 0;
196 clear_color.useRawValue = XGL_TRUE;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700197 XGL_DEPTH_STENCIL_BIND_INFO *dsBinding;
198 if (m_depthStencilBinding.view)
199 dsBinding = &m_depthStencilBinding;
200 else
201 dsBinding = NULL;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600202 XGL_FRAMEBUFFER_CREATE_INFO fb_info = {};
203 fb_info.sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
204 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700205 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600206 fb_info.pColorAttachments = m_colorBindings;
207 fb_info.pDepthStencilAttachment = dsBinding;
208 fb_info.sampleCount = 1;
209 fb_info.width = (uint32_t)m_width;
210 fb_info.height = (uint32_t)m_height;
211 fb_info.layers = 1;
212
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700213 XGL_RENDER_PASS_CREATE_INFO rp_info;
214 memset(&rp_info, 0 , sizeof(rp_info));
Tobin Ehlis976fc162015-03-26 08:23:25 -0600215 xglCreateFramebuffer(device(), &fb_info, &(m_frameBuffer));
216 rp_info.framebuffer = m_frameBuffer;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700217 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
218 rp_info.renderArea.extent.width = m_width;
219 rp_info.renderArea.extent.height = m_height;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700220 rp_info.colorAttachmentCount = m_renderTargets.size();
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700221 rp_info.pColorLoadOps = &load_op;
222 rp_info.pColorStoreOps = &store_op;
Tobin Ehlis5de72572015-03-06 13:16:37 -0700223 rp_info.pColorLoadClearValues = &clear_color;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700224 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
225 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
226 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
227 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
228 xglCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600229}
230
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600231
232
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600233XglDevice::XglDevice(uint32_t id, XGL_PHYSICAL_GPU obj) :
Chia-I Wufb1459b2014-12-29 15:23:20 +0800234 xgl_testing::Device(obj), id(id)
235{
236 init();
237
238 props = gpu().properties();
239 queue_props = &gpu().queue_properties()[0];
240}
241
242void XglDevice::get_device_queue()
243{
244 ASSERT_NE(true, graphics_queues().empty());
245 m_queue = graphics_queues()[0]->obj();
246}
247
Tony Barboure2c58df2014-11-25 13:18:32 -0700248XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
249{
250 m_device = device;
251 m_nextSlot = 0;
252
253}
254
Chia-I Wu11078b02015-01-04 16:27:24 +0800255XglDescriptorSetObj::~XglDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700256{
Chia-I Wu11078b02015-01-04 16:27:24 +0800257 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700258}
Tony Barbour82c39522014-12-04 14:33:33 -0700259
Chia-I Wu11078b02015-01-04 16:27:24 +0800260int XglDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700261{
Chia-I Wu11078b02015-01-04 16:27:24 +0800262 /* request a descriptor but do not update it */
263 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
264 tc.type = XGL_DESCRIPTOR_TYPE_RAW_BUFFER;
265 tc.count = 1;
266 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700267
Chia-I Wu11078b02015-01-04 16:27:24 +0800268 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700269}
Tony Barbour82c39522014-12-04 14:33:33 -0700270
Chia-I Wu11078b02015-01-04 16:27:24 +0800271int XglDescriptorSetObj::AppendBuffer(XGL_DESCRIPTOR_TYPE type, XglConstantBufferObj *constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700272{
Chia-I Wu11078b02015-01-04 16:27:24 +0800273 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
274 tc.type = type;
275 tc.count = 1;
276 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700277
Chia-I Wu11078b02015-01-04 16:27:24 +0800278 m_bufferInfo.push_back(&constantBuffer->m_bufferViewInfo);
279
280 m_updateBuffers.push_back(xgl_testing::DescriptorSet::update(type, m_nextSlot, 1,
281 (const XGL_BUFFER_VIEW_ATTACH_INFO **) NULL));
282
283 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700284}
Tony Barbour82c39522014-12-04 14:33:33 -0700285
Chia-I Wu11078b02015-01-04 16:27:24 +0800286int XglDescriptorSetObj::AppendSamplerTexture( XglSamplerObj* sampler, XglTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700287{
Chia-I Wu11078b02015-01-04 16:27:24 +0800288 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
289 tc.type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE;
290 tc.count = 1;
291 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700292
Chia-I Wu11078b02015-01-04 16:27:24 +0800293 XGL_SAMPLER_IMAGE_VIEW_INFO tmp = {};
294 tmp.pSampler = sampler->obj();
295 tmp.pImageView = &texture->m_textureViewInfo;
296 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700297
Chia-I Wu11078b02015-01-04 16:27:24 +0800298 m_updateSamplerTextures.push_back(xgl_testing::DescriptorSet::update(m_nextSlot, 1,
299 (const XGL_SAMPLER_IMAGE_VIEW_INFO *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700300
Chia-I Wu11078b02015-01-04 16:27:24 +0800301 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700302}
Chia-I Wu11078b02015-01-04 16:27:24 +0800303
304XGL_DESCRIPTOR_SET_LAYOUT XglDescriptorSetObj::GetLayout()
Tony Barbourb5f4d082014-12-17 10:54:03 -0700305{
Chia-I Wu11078b02015-01-04 16:27:24 +0800306 return m_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700307}
308
309XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle()
310{
Chia-I Wu11078b02015-01-04 16:27:24 +0800311 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700312}
Tony Barboure2c58df2014-11-25 13:18:32 -0700313
Chia-I Wu11078b02015-01-04 16:27:24 +0800314void XglDescriptorSetObj::CreateXGLDescriptorSet(XglCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700315{
Chia-I Wu11078b02015-01-04 16:27:24 +0800316 // create XGL_DESCRIPTOR_REGION
317 XGL_DESCRIPTOR_REGION_CREATE_INFO region = {};
318 region.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO;
319 region.count = m_type_counts.size();
320 region.pTypeCount = &m_type_counts[0];
321 init(*m_device, XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1, region);
Tony Barbour824b7712014-12-18 17:06:21 -0700322
Chia-I Wu11078b02015-01-04 16:27:24 +0800323 // create XGL_DESCRIPTOR_SET_LAYOUT
324 vector<XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO> layout;
325 layout.resize(m_type_counts.size());
326 for (int i = 0; i < m_type_counts.size(); i++) {
327 layout[i].sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
328 layout[i].descriptorType = m_type_counts[i].type;
329 layout[i].count = m_type_counts[i].count;
330 layout[i].stageFlags = XGL_SHADER_STAGE_FLAGS_ALL;
331 layout[i].immutableSampler = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700332
Chia-I Wu11078b02015-01-04 16:27:24 +0800333 if (i < m_type_counts.size() - 1)
334 layout[i].pNext = &layout[i + 1];
335 else
336 layout[i].pNext = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700337 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800338
Chia-I Wu11078b02015-01-04 16:27:24 +0800339 m_layout.init(*m_device, 0, layout[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700340
Chia-I Wu11078b02015-01-04 16:27:24 +0800341 // create XGL_DESCRIPTOR_SET
342 m_set = alloc_sets(XGL_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
343
344 // build the update chain
345 for (int i = 0; i < m_updateBuffers.size(); i++) {
346 m_updateBuffers[i].pBufferViews = &m_bufferInfo[i];
347
348 if (i < m_updateBuffers.size() - 1)
349 m_updateBuffers[i].pNext = &m_updateBuffers[i + 1];
350 else if (m_updateSamplerTextures.empty())
351 m_updateBuffers[i].pNext = NULL;
352 else
353 m_updateBuffers[i].pNext = &m_updateSamplerTextures[0];
354 }
355 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
356 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
357
358 if (i < m_updateSamplerTextures.size() - 1)
359 m_updateSamplerTextures[i].pNext = &m_updateSamplerTextures[i + 1];
360 else
361 m_updateSamplerTextures[i].pNext = NULL;
362 }
363 const void *chain = (!m_updateBuffers.empty()) ? (const void *) &m_updateBuffers[0] :
364 (!m_updateSamplerTextures.empty()) ? (const void *) &m_updateSamplerTextures[0] :
365 NULL;
366
367 // do the updates
368 m_device->begin_descriptor_region_update(XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
369 clear_sets(*m_set);
370 m_set->update(chain);
371 m_device->end_descriptor_region_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700372}
Tony Barboure2c58df2014-11-25 13:18:32 -0700373
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800374XglImage::XglImage(XglDevice *dev)
375{
376 m_device = dev;
377 m_imageInfo.view = XGL_NULL_HANDLE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000378 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800379}
380
Tony Barbour579f7802015-04-03 15:11:43 -0600381static bool IsCompatible(XGL_FLAGS usage, XGL_FLAGS features)
382{
383 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) &&
384 !(features & XGL_FORMAT_IMAGE_SHADER_READ_BIT))
385 return false;
386 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT) &&
387 !(features & XGL_FORMAT_IMAGE_SHADER_WRITE_BIT))
388 return false;
389 return true;
390}
391
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600392void XglImage::init(uint32_t w, uint32_t h,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800393 XGL_FORMAT fmt, XGL_FLAGS usage,
Tony Barbour579f7802015-04-03 15:11:43 -0600394 XGL_IMAGE_TILING requested_tiling)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800395{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600396 uint32_t mipCount;
Tony Barbour579f7802015-04-03 15:11:43 -0600397 XGL_FORMAT_PROPERTIES image_fmt;
398 XGL_IMAGE_TILING tiling;
399 XGL_RESULT err;
400 size_t size;
401
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800402 mipCount = 0;
403
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600404 uint32_t _w = w;
405 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800406 while( ( _w > 0 ) || ( _h > 0 ) )
407 {
408 _w >>= 1;
409 _h >>= 1;
410 mipCount++;
411 }
412
Tony Barbour579f7802015-04-03 15:11:43 -0600413 size = sizeof(image_fmt);
414 err = xglGetFormatInfo(m_device->obj(), fmt,
415 XGL_INFO_TYPE_FORMAT_PROPERTIES,
416 &size, &image_fmt);
417 ASSERT_XGL_SUCCESS(err);
418
419 if (requested_tiling == XGL_LINEAR_TILING) {
420 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
421 tiling = XGL_LINEAR_TILING;
422 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
423 tiling = XGL_OPTIMAL_TILING;
424 } else {
425 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
426 }
427 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
428 tiling = XGL_OPTIMAL_TILING;
429 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
430 tiling = XGL_LINEAR_TILING;
431 } else {
432 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
433 }
434
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800435 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
436 imageCreateInfo.imageType = XGL_IMAGE_2D;
437 imageCreateInfo.format = fmt;
438 imageCreateInfo.extent.width = w;
439 imageCreateInfo.extent.height = h;
440 imageCreateInfo.mipLevels = mipCount;
441 imageCreateInfo.tiling = tiling;
442
443 imageCreateInfo.usage = usage;
444
445 xgl_testing::Image::init(*m_device, imageCreateInfo);
446
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000447 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800448}
449
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600450XGL_RESULT XglImage::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800451{
452 *ptr = map();
453 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
454}
455
456XGL_RESULT XglImage::UnmapMemory()
457{
458 unmap();
459 return XGL_SUCCESS;
460}
461
Tony Barbour5dc515d2015-04-01 17:47:06 -0600462XGL_RESULT XglImage::CopyImage(XglImage &fromImage)
463{
464 XGL_RESULT err;
465
466 XGL_CMD_BUFFER cmd_buf;
467 XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {};
468 cmd_buf_create_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
469 cmd_buf_create_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700470 cmd_buf_create_info.queueNodeIndex = m_device->graphics_queue_node_index_;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600471 cmd_buf_create_info.flags = 0;
472
473 err = xglCreateCommandBuffer(m_device->device(), &cmd_buf_create_info, &cmd_buf);
474 assert(!err);
475
476 /* Copy staging texture to usable texture */
477 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {};
478 cmd_buf_begin_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
479 cmd_buf_begin_info.pNext = NULL;
480 cmd_buf_begin_info.flags = 0;
481
482 err = xglResetCommandBuffer(cmd_buf);
483 assert(!err);
484
485 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_begin_info);
486 assert(!err);
487
488 XGL_IMAGE_COPY copy_region = {};
489 copy_region.srcSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
490 copy_region.srcSubresource.arraySlice = 0;
491 copy_region.srcSubresource.mipLevel = 0;
492 copy_region.srcOffset.x = 0;
493 copy_region.srcOffset.y = 0;
494 copy_region.srcOffset.z = 0;
495 copy_region.destSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
496 copy_region.destSubresource.arraySlice = 0;
497 copy_region.destSubresource.mipLevel = 0;
498 copy_region.destOffset.x = 0;
499 copy_region.destOffset.y = 0;
500 copy_region.destOffset.z = 0;
501 copy_region.extent = fromImage.extent();
502
503 xglCmdCopyImage(cmd_buf, fromImage.obj(), obj(), 1, &copy_region);
504
505 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {};
506 image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
507 image_memory_barrier.pNext = NULL;
508 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT;
509 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
510 image_memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_GENERAL;
511 image_memory_barrier.newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL;
512 image_memory_barrier.image = fromImage.obj();
513 image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
514 image_memory_barrier.subresourceRange.baseMipLevel = 0;
515 image_memory_barrier.subresourceRange.mipLevels = 1;
516 image_memory_barrier.subresourceRange.baseArraySlice = 0;
517 image_memory_barrier.subresourceRange.arraySize = 0;
518
519 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
520
521 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
522 XGL_PIPELINE_BARRIER pipeline_barrier;
523 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
524 pipeline_barrier.pNext = NULL;
525 pipeline_barrier.eventCount = 1;
526 pipeline_barrier.pEvents = set_events;
527 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
528 pipeline_barrier.memBarrierCount = 1;
529 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
530
531 // write barrier to the command buffer
532 xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier);
533
534 err = xglEndCommandBuffer(cmd_buf);
535 assert(!err);
536
537 const XGL_CMD_BUFFER cmd_bufs[] = { cmd_buf };
538 XGL_MEMORY_REF mem_refs[16];
539 uint32_t num_refs = 0;
540 const std::vector<XGL_GPU_MEMORY> from_mems = fromImage.memories();
541 const std::vector<XGL_GPU_MEMORY> to_mems = memories();
542
543 for (uint32_t j = 0; j < from_mems.size(); j++) {
544 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
545 mem_refs[num_refs].mem = from_mems[j];
546 num_refs++;
547 assert(num_refs < 16);
548 }
549
550 for (uint32_t j = 0; j < to_mems.size(); j++) {
551 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
552 mem_refs[num_refs].mem = to_mems[j];
553 num_refs++;
554 assert(num_refs < 16);
555 }
556
557 err = xglQueueSubmit(m_device->m_queue, 1, cmd_bufs,
558 num_refs, mem_refs, XGL_NULL_HANDLE);
559 assert(!err);
560
561 err = xglQueueWaitIdle(m_device->m_queue);
562 assert(!err);
563
564 xglDestroyObject(cmd_buf);
565
566 return XGL_SUCCESS;
567}
568
Tony Barbourebc093f2015-04-01 16:38:10 -0600569XglTextureObj::XglTextureObj(XglDevice *device, uint32_t *colors)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600570 :XglImage(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700571{
572 m_device = device;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700573 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600574 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600575 void *data;
576 int32_t x, y;
577 XglImage stagingImage(device);
578
579 stagingImage.init(16, 16, tex_format, 0, XGL_LINEAR_TILING);
580 XGL_SUBRESOURCE_LAYOUT layout = stagingImage.subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600581
582 if (colors == NULL)
583 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700584
585 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
586
587 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
588
Tony Barbour5dc515d2015-04-01 17:47:06 -0600589 XGL_IMAGE_VIEW_CREATE_INFO view = {};
Tony Barbourbdf0a312015-04-01 17:10:07 -0600590 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
591 view.pNext = NULL;
592 view.image = XGL_NULL_HANDLE;
593 view.viewType = XGL_IMAGE_VIEW_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600594 view.format = tex_format;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600595 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
596 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
597 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
598 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
599 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
600 view.subresourceRange.baseMipLevel = 0;
601 view.subresourceRange.mipLevels = 1;
602 view.subresourceRange.baseArraySlice = 0;
603 view.subresourceRange.arraySize = 1;
604 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700605
Tony Barboure2c58df2014-11-25 13:18:32 -0700606 /* create image */
Tony Barbour5dc515d2015-04-01 17:47:06 -0600607 init(16, 16, tex_format, XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, XGL_OPTIMAL_TILING);
Tony Barboure2c58df2014-11-25 13:18:32 -0700608
609 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800610 view.image = obj();
611 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600612 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700613
Tony Barbour5dc515d2015-04-01 17:47:06 -0600614 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700615
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800616 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700617 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800618 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600619 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700620 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600621 stagingImage.unmap();
622 XglImage::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700623}
Tony Barbour82c39522014-12-04 14:33:33 -0700624
Tony Barboure2c58df2014-11-25 13:18:32 -0700625XglSamplerObj::XglSamplerObj(XglDevice *device)
626{
Tony Barboure2c58df2014-11-25 13:18:32 -0700627 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700628
Chia-I Wue9864b52014-12-28 16:32:24 +0800629 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
630 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
631 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
632 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
633 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
634 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
635 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
636 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
637 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
638 samplerCreateInfo.mipLodBias = 0.0;
639 samplerCreateInfo.maxAnisotropy = 0.0;
640 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
641 samplerCreateInfo.minLod = 0.0;
642 samplerCreateInfo.maxLod = 0.0;
643 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700644
Chia-I Wue9864b52014-12-28 16:32:24 +0800645 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700646}
Tony Barboure2c58df2014-11-25 13:18:32 -0700647
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700648/*
649 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
650 */
651XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
652{
653 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700654 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700655
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800656 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700657}
658
Tony Barboure2c58df2014-11-25 13:18:32 -0700659XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
660{
Tony Barboure2c58df2014-11-25 13:18:32 -0700661 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800662 m_commandBuffer = 0;
663
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800664 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700665 m_numVertices = constantCount;
666 m_stride = constantSize;
667
Chia-I Wua07fee62014-12-28 15:26:08 +0800668 const size_t allocationSize = constantCount * constantSize;
669 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700670
Chia-I Wua07fee62014-12-28 15:26:08 +0800671 void *pData = map();
672 memcpy(pData, data, allocationSize);
673 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700674
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800675 // set up the buffer view for the constant buffer
676 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
677 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
678 view_info.buffer = obj();
Chia-I Wubb0c8d22015-01-16 22:31:25 +0800679 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800680 view_info.offset = 0;
681 view_info.range = allocationSize;
682 m_bufferView.init(*m_device, view_info);
683
684 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
685 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700686}
Tony Barbour82c39522014-12-04 14:33:33 -0700687
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600688void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700689{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800690 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700691}
692
693
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000694void XglConstantBufferObj::BufferMemoryBarrier(
695 XGL_FLAGS outputMask /*=
696 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
697 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
698 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
699 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
700 XGL_MEMORY_OUTPUT_COPY_BIT*/,
701 XGL_FLAGS inputMask /*=
702 XGL_MEMORY_INPUT_CPU_READ_BIT |
703 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
704 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
705 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
706 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
707 XGL_MEMORY_INPUT_SHADER_READ_BIT |
708 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
709 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
710 XGL_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700711{
Tony Barbour38422802014-12-10 14:36:31 -0700712 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700713
Tony Barbour38422802014-12-10 14:36:31 -0700714 if (!m_commandBuffer)
715 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800716 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700717
718 m_commandBuffer = new XglCommandBufferObj(m_device);
719
720 }
721 else
722 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800723 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700724 }
725
Tony Barboure2c58df2014-11-25 13:18:32 -0700726 // open the command buffer
Tony Barbourbdf0a312015-04-01 17:10:07 -0600727 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {};
728 cmd_buf_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
729 cmd_buf_info.pNext = NULL;
730 cmd_buf_info.flags = 0;
731
Jon Ashburnc4164b12014-12-31 17:10:47 -0700732 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700733 ASSERT_XGL_SUCCESS(err);
734
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000735 XGL_BUFFER_MEMORY_BARRIER memory_barrier =
736 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Mark Lobodzinski837ef922015-01-29 14:24:14 -0600737 XGL_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700738
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000739 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
740 XGL_PIPELINE_BARRIER pipeline_barrier = {};
741 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
742 pipeline_barrier.eventCount = 1;
743 pipeline_barrier.pEvents = set_events;
744 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
745 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -0600746 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000747
748 // write barrier to the command buffer
749 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700750
751 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700752 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700753 ASSERT_XGL_SUCCESS(err);
754
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600755 uint32_t numMemRefs=1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700756 XGL_MEMORY_REF memRefs;
757 // this command buffer only uses the vertex buffer memory
758 memRefs.flags = 0;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800759 memRefs.mem = memories()[0];
Tony Barboure2c58df2014-11-25 13:18:32 -0700760
761 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700762 XGL_CMD_BUFFER bufferArray[1];
763 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800764 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700765 ASSERT_XGL_SUCCESS(err);
766}
767
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700768XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
769 : XglConstantBufferObj(device)
770{
771
772}
773
774void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
775{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700776 XGL_FORMAT viewFormat;
777
778 m_numVertices = numIndexes;
779 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700780 switch (indexType) {
781 case XGL_INDEX_8:
782 m_stride = 1;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700783 viewFormat = XGL_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700784 break;
785 case XGL_INDEX_16:
786 m_stride = 2;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700787 viewFormat = XGL_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700788 break;
789 case XGL_INDEX_32:
790 m_stride = 4;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700791 viewFormat = XGL_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700792 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800793 default:
794 assert(!"unknown index type");
795 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700796 }
797
Chia-I Wua07fee62014-12-28 15:26:08 +0800798 const size_t allocationSize = numIndexes * m_stride;
799 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700800
Chia-I Wua07fee62014-12-28 15:26:08 +0800801 void *pData = map();
802 memcpy(pData, data, allocationSize);
803 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700804
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800805 // set up the buffer view for the constant buffer
806 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
807 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
808 view_info.buffer = obj();
809 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
810 view_info.stride = m_stride;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700811 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800812 view_info.offset = 0;
813 view_info.range = allocationSize;
814 m_bufferView.init(*m_device, view_info);
815
816 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
817 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700818}
819
820void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
821{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800822 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700823}
Tony Barboure2c58df2014-11-25 13:18:32 -0700824
Tony Barbouraf1f9192014-12-17 10:57:58 -0700825XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
826{
827 return m_indexType;
828}
829
Chia-I Wu11078b02015-01-04 16:27:24 +0800830XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700831{
Tony Barboure2c58df2014-11-25 13:18:32 -0700832 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
833 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
834 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800835 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700836 stageInfo->shader.linkConstBufferCount = 0;
837 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700838
Tony Barboure2c58df2014-11-25 13:18:32 -0700839 return stageInfo;
840}
841
Tony Barboure2c58df2014-11-25 13:18:32 -0700842XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
843{
844 XGL_RESULT err = XGL_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600845 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700846 XGL_SHADER_CREATE_INFO createInfo;
847 size_t shader_len;
848
849 m_stage = stage;
850 m_device = device;
851
852 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
853 createInfo.pNext = NULL;
854
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600855 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700856
857 shader_len = strlen(shader_code);
858 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
859 createInfo.pCode = malloc(createInfo.codeSize);
860 createInfo.flags = 0;
861
862 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600863 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700864 ((uint32_t *) createInfo.pCode)[1] = 0;
865 ((uint32_t *) createInfo.pCode)[2] = stage;
866 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
867
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800868 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700869 }
870
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600871 if (framework->m_use_spv || err) {
872 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700873 err = XGL_SUCCESS;
874
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600875 // Use Reference GLSL to SPV compiler
876 framework->GLSLtoSPV(stage, shader_code, spv);
877 createInfo.pCode = spv.data();
878 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700879 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700880
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800881 init(*m_device, createInfo);
882 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700883}
Tony Barbour82c39522014-12-04 14:33:33 -0700884
Tony Barboure2c58df2014-11-25 13:18:32 -0700885XglPipelineObj::XglPipelineObj(XglDevice *device)
886{
Tony Barboure2c58df2014-11-25 13:18:32 -0700887 m_device = device;
888 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
889 m_vertexBufferCount = 0;
890
891 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
892 m_ia_state.pNext = XGL_NULL_HANDLE;
893 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
894 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700895 m_ia_state.primitiveRestartEnable = XGL_FALSE;
896 m_ia_state.primitiveRestartIndex = 0;
897
898 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
899 m_rs_state.pNext = &m_ia_state;
900 m_rs_state.depthClipEnable = XGL_FALSE;
901 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Chia-I Wuc8d1ec52015-03-24 11:01:50 +0800902 m_rs_state.programPointSize = XGL_FALSE;
903 m_rs_state.pointOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700904 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
905 m_rs_state.fillMode = XGL_FILL_SOLID;
906 m_rs_state.cullMode = XGL_CULL_NONE;
907 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700908
Tony Barboure2c58df2014-11-25 13:18:32 -0700909 memset(&m_cb_state,0,sizeof(m_cb_state));
910 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
911 m_cb_state.pNext = &m_rs_state;
912 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700913 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
914
Tony Barbourf52346d2015-01-16 14:27:35 -0700915 m_ms_state.pNext = &m_cb_state;
916 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
917 m_ms_state.multisampleEnable = XGL_FALSE;
918 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
919 m_ms_state.samples = 1;
920 m_ms_state.minSampleShading = 0;
921 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700922
Tony Barbourf52346d2015-01-16 14:27:35 -0700923 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
924 m_ds_state.pNext = &m_ms_state,
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700925 m_ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700926 m_ds_state.depthTestEnable = XGL_FALSE;
927 m_ds_state.depthWriteEnable = XGL_FALSE;
928 m_ds_state.depthBoundsEnable = XGL_FALSE;
929 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
930 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
931 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
932 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
933 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
934 m_ds_state.stencilTestEnable = XGL_FALSE;
935 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -0700936
Tony Barbourf52346d2015-01-16 14:27:35 -0700937 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
938 att.blendEnable = XGL_FALSE;
Tony Barboura53a6942015-02-25 11:25:11 -0700939 att.format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -0700940 att.channelWriteMask = 0xf;
941 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -0700942
943};
944
945void XglPipelineObj::AddShader(XglShaderObj* shader)
946{
947 m_shaderObjs.push_back(shader);
948}
949
950void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
951{
952 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
953 m_vi_state.attributeCount = count;
954}
955
956void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
957{
958 m_vi_state.pVertexBindingDescriptions = vi_binding;
959 m_vi_state.bindingCount = count;
960}
961
962void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
963{
964 m_vertexBufferObjs.push_back(vertexDataBuffer);
965 m_vertexBufferBindings.push_back(binding);
966 m_vertexBufferCount++;
967}
968
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600969void XglPipelineObj::AddColorAttachment(uint32_t binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +0800970{
Tony Barbourf52346d2015-01-16 14:27:35 -0700971 if (binding+1 > m_colorAttachments.size())
972 {
973 m_colorAttachments.resize(binding+1);
974 }
975 m_colorAttachments[binding] = *att;
976}
977
978void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
979{
980 m_ds_state.format = ds_state->format;
981 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
982 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
983 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
984 m_ds_state.depthFunc = ds_state->depthFunc;
985 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
986 m_ds_state.back = ds_state->back;
987 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +0800988}
989
Tony Barbour976e1cf2014-12-17 11:57:31 -0700990void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
991{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600992 void* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -0700993 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
994
995 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
996
997 for (int i=0; i<m_shaderObjs.size(); i++)
998 {
Chia-I Wu11078b02015-01-04 16:27:24 +0800999 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001000 shaderCreateInfo->pNext = head_ptr;
1001 head_ptr = shaderCreateInfo;
1002 }
1003
1004 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1005 {
1006 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1007 m_vi_state.pNext = head_ptr;
1008 head_ptr = &m_vi_state;
1009 }
1010
1011 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1012 info.pNext = head_ptr;
1013 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +08001014 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001015
Tony Barbourf52346d2015-01-16 14:27:35 -07001016 m_cb_state.attachmentCount = m_colorAttachments.size();
1017 m_cb_state.pAttachments = &m_colorAttachments[0];
1018
Chia-I Wu2648d092014-12-29 14:24:14 +08001019 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001020}
Chia-I Wu2648d092014-12-29 14:24:14 +08001021
Tony Barbour976e1cf2014-12-17 11:57:31 -07001022XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
1023{
Chia-I Wu2648d092014-12-29 14:24:14 +08001024 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001025}
1026
Tony Barbour5420af02014-12-03 13:58:15 -07001027void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -07001028{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001029 void* head_ptr = &m_ds_state;
Tony Barboure2c58df2014-11-25 13:18:32 -07001030 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1031
1032 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -07001033
1034 for (int i=0; i<m_shaderObjs.size(); i++)
1035 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001036 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barboure2c58df2014-11-25 13:18:32 -07001037 shaderCreateInfo->pNext = head_ptr;
1038 head_ptr = shaderCreateInfo;
1039 }
1040
1041 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1042 {
1043 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1044 m_vi_state.pNext = head_ptr;
1045 head_ptr = &m_vi_state;
1046 }
1047
1048 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1049 info.pNext = head_ptr;
1050 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +08001051 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barboure2c58df2014-11-25 13:18:32 -07001052
Chia-I Wu2648d092014-12-29 14:24:14 +08001053 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -07001054
Chia-I Wu2648d092014-12-29 14:24:14 +08001055 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -07001056
1057
1058 for (int i=0; i < m_vertexBufferCount; i++)
1059 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001060 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001061 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001062}
Tony Barbour82c39522014-12-04 14:33:33 -07001063
Tony Barboure2c58df2014-11-25 13:18:32 -07001064XglMemoryRefManager::XglMemoryRefManager() {
1065
1066}
Tony Barbour82c39522014-12-04 14:33:33 -07001067
Tony Barboure2c58df2014-11-25 13:18:32 -07001068void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001069 const std::vector<XGL_GPU_MEMORY> mems = constantBuffer->memories();
1070 if (!mems.empty())
1071 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001072}
Tony Barbour82c39522014-12-04 14:33:33 -07001073
Tony Barboure2c58df2014-11-25 13:18:32 -07001074void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
Chia-I Wu13a3aa82014-12-28 15:55:09 +08001075 const std::vector<XGL_GPU_MEMORY> mems = texture->memories();
1076 if (!mems.empty())
1077 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001078}
Tony Barbour82c39522014-12-04 14:33:33 -07001079
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001080void XglMemoryRefManager::AddMemoryRef(XGL_GPU_MEMORY *mem, uint32_t refCount) {
1081 for (size_t i = 0; i < refCount; i++) {
1082 m_bufferObjs.push_back(mem[i]);
1083 }
1084}
1085
1086void XglMemoryRefManager::AddRTMemoryRefs(vector<XglImage*>images, uint32_t rtCount) {
1087 for (uint32_t i = 0; i < rtCount; i++) {
1088 const std::vector<XGL_GPU_MEMORY> mems = images[i]->memories();
1089 if (!mems.empty())
1090 m_bufferObjs.push_back(mems[0]);
1091 }
1092}
1093
Tony Barboure2c58df2014-11-25 13:18:32 -07001094XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
1095
1096 XGL_MEMORY_REF *localRefs;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001097 uint32_t numRefs=m_bufferObjs.size();
Tony Barboure2c58df2014-11-25 13:18:32 -07001098
1099 if (numRefs <= 0)
1100 return NULL;
1101
1102 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
1103 for (int i=0; i<numRefs; i++)
1104 {
1105 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +08001106 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -07001107 }
1108 return localRefs;
1109}
1110int XglMemoryRefManager::GetNumRefs() {
1111 return m_bufferObjs.size();
1112}
Tony Barbour6d047bf2014-12-10 14:34:45 -07001113
1114XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -07001115 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001116{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001117 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001118}
Tony Barbour471338d2014-12-10 17:28:39 -07001119
Tony Barbour6d047bf2014-12-10 14:34:45 -07001120XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1121{
Chia-I Wud28343c2014-12-28 15:12:48 +08001122 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001123}
Tony Barbour471338d2014-12-10 17:28:39 -07001124
Jon Ashburnc4164b12014-12-31 17:10:47 -07001125XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001126{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001127 begin(pInfo);
1128 return XGL_SUCCESS;
1129}
1130
1131XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj)
1132{
1133 begin(renderpass_obj);
1134 return XGL_SUCCESS;
1135}
1136
1137XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
1138{
1139 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +08001140 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001141}
1142
1143XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1144{
Chia-I Wud28343c2014-12-28 15:12:48 +08001145 end();
1146 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001147}
1148
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001149void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001150{
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001151 xglCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001152}
1153
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001154void XglCommandBufferObj::ClearAllBuffers(XGL_CLEAR_COLOR clear_color, float depth_clear_color, uint32_t stencil_clear_color,
1155 XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001156{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001157 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001158 const XGL_FLAGS output_mask =
1159 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1160 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1161 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1162 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1163 XGL_MEMORY_OUTPUT_COPY_BIT;
1164 const XGL_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001165
1166 // whatever we want to do, we do it to the whole buffer
1167 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1168 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1169 srRange.baseMipLevel = 0;
1170 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1171 srRange.baseArraySlice = 0;
1172 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1173
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001174 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1175 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1176 memory_barrier.outputMask = output_mask;
1177 memory_barrier.inputMask = input_mask;
1178 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1179 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001180 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001181
1182 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1183 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1184 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1185 pipeline_barrier.eventCount = 1;
1186 pipeline_barrier.pEvents = set_events;
1187 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1188 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001189 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001190
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001191 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001192 memory_barrier.image = m_renderTargets[i]->image();
1193 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1194 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1195 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001196
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001197 xglCmdClearColorImage( obj(), m_renderTargets[i]->image(), clear_color, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001198 }
1199
1200 if (depthStencilImage)
1201 {
1202 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1203 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1204 dsRange.baseMipLevel = 0;
1205 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1206 dsRange.baseArraySlice = 0;
1207 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1208
1209 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001210
1211 memory_barrier.oldLayout = depthStencilBinding->layout;
1212 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1213 memory_barrier.image = depthStencilImage;
1214 memory_barrier.subresourceRange = dsRange;
1215
1216 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1217 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001218
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001219 xglCmdClearDepthStencil(obj(), depthStencilImage,
1220 depth_clear_color, stencil_clear_color,
1221 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001222
1223 // prepare depth buffer for rendering
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001224 memory_barrier.image = depthStencilImage;
1225 memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1226 memory_barrier.newLayout = depthStencilBinding->layout;
1227 memory_barrier.subresourceRange = dsRange;
1228 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1229 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001230 }
1231}
1232
Jon Ashburncdc40be2015-01-02 18:27:14 -07001233void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001234{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001235 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001236 const XGL_FLAGS output_mask =
1237 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1238 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1239 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1240 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1241 XGL_MEMORY_OUTPUT_COPY_BIT;
1242 const XGL_FLAGS input_mask =
1243 XGL_MEMORY_INPUT_CPU_READ_BIT |
1244 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1245 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
1246 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1247 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
1248 XGL_MEMORY_INPUT_SHADER_READ_BIT |
1249 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1250 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1251 XGL_MEMORY_INPUT_COPY_BIT;
1252
Tony Barbour30cc9e82014-12-17 11:53:55 -07001253 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1254 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1255 srRange.baseMipLevel = 0;
1256 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1257 srRange.baseArraySlice = 0;
1258 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1259
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001260 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1261 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1262 memory_barrier.outputMask = output_mask;
1263 memory_barrier.inputMask = input_mask;
1264 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1265 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001266 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001267
1268 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1269 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1270 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1271 pipeline_barrier.eventCount = 1;
1272 pipeline_barrier.pEvents = set_events;
1273 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1274 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001275 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001276
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001277 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001278 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001279 memory_barrier.image = m_renderTargets[i]->image();
1280 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1281 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1282 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001283 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001284}
1285
Tony Barbourf52346d2015-01-16 14:27:35 -07001286void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001287{
Tony Barbourf52346d2015-01-16 14:27:35 -07001288 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001289}
1290
1291void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1292{
1293 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001294}
1295
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001296void XglCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001297{
Chia-I Wud28343c2014-12-28 15:12:48 +08001298 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001299}
1300
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001301void XglCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001302{
Chia-I Wud28343c2014-12-28 15:12:48 +08001303 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001304}
1305
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001306void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, uint32_t numMemRefs)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001307{
1308 XGL_RESULT err = XGL_SUCCESS;
1309
1310 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001311 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001312 ASSERT_XGL_SUCCESS( err );
1313
1314 err = xglQueueWaitIdle( m_device->m_queue );
1315 ASSERT_XGL_SUCCESS( err );
1316
1317 // Wait for work to finish before cleaning up.
1318 xglDeviceWaitIdle(m_device->device());
1319
1320}
1321void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1322{
Chia-I Wud28343c2014-12-28 15:12:48 +08001323 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001324}
1325
1326void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1327{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001328 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wu11078b02015-01-04 16:27:24 +08001329 xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001330}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001331void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001332{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001333 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001334}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001335void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001336{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001337 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001338}