blob: 4eecc763f2442c92a53969230aa12e76de1b7f4d [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
37 m_height( 256.0 ) // default window height
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060038{
Chia-I Wuecebf752014-12-05 10:45:15 +080039 m_renderTargetCount = 1;
40
Tony Barboura53a6942015-02-25 11:25:11 -070041 m_render_target_fmt = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060042
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060043 m_depthStencilBinding.view = XGL_NULL_HANDLE;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060044}
45
46XglRenderFramework::~XglRenderFramework()
47{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060048
49}
50
51void XglRenderFramework::InitFramework()
52{
53 XGL_RESULT err;
54
Jon Ashburn1e464892015-01-29 15:48:00 -070055 err = xglCreateInstance(&app_info, NULL, &this->inst);
56 ASSERT_XGL_SUCCESS(err);
57 err = xglEnumerateGpus(inst, XGL_MAX_PHYSICAL_GPUS, &this->gpu_count,
58 objs);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060059 ASSERT_XGL_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070060 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060061
62 m_device = new XglDevice(0, objs[0]);
63 m_device->get_device_queue();
64}
65
66void XglRenderFramework::ShutdownFramework()
67{
68 if (m_colorBlend) xglDestroyObject(m_colorBlend);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060069 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
70 if (m_stateRaster) xglDestroyObject(m_stateRaster);
71 if (m_cmdBuffer) xglDestroyObject(m_cmdBuffer);
Tobin Ehlis976fc162015-03-26 08:23:25 -060072 if (m_frameBuffer) xglDestroyObject(m_frameBuffer);
73 if (m_renderPass) xglDestroyObject(m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060074
75 if (m_stateViewport) {
76 xglDestroyObject(m_stateViewport);
77 }
Tobin Ehlis976fc162015-03-26 08:23:25 -060078 while (!m_renderTargets.empty()) {
79 xglDestroyObject(m_renderTargets.back()->targetView());
80 xglBindObjectMemory(m_renderTargets.back()->image(), 0, XGL_NULL_HANDLE, 0);
81 xglDestroyObject(m_renderTargets.back()->image());
82 xglFreeMemory(m_renderTargets.back()->memory());
83 m_renderTargets.pop_back();
84 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060085
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060086 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +080087 delete m_device;
Jon Ashburn1e464892015-01-29 15:48:00 -070088 xglDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060089}
90
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060091void XglRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060092{
93 XGL_RESULT err;
94
Tony Barboura53a6942015-02-25 11:25:11 -070095 m_render_target_fmt = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060096
97 // create a raster state (solid, back-face culling)
Tony Barbourf52346d2015-01-16 14:27:35 -070098 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster = {};
99 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
100 raster.pointSize = 1.0;
101
102 err = xglCreateDynamicRasterState( device(), &raster, &m_stateRaster );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600103 ASSERT_XGL_SUCCESS(err);
104
Tony Barbourf52346d2015-01-16 14:27:35 -0700105 XGL_DYNAMIC_CB_STATE_CREATE_INFO blend = {};
106 blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
107 err = xglCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600108 ASSERT_XGL_SUCCESS( err );
109
Tony Barbourf52346d2015-01-16 14:27:35 -0700110 XGL_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {};
111 depthStencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600112 depthStencil.minDepth = 0.f;
113 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700114 depthStencil.stencilFrontRef = 0;
115 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600116
Tony Barbourf52346d2015-01-16 14:27:35 -0700117 err = xglCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600118 ASSERT_XGL_SUCCESS( err );
119
120 XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {};
121
122 cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700123 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
124
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600125 err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
126 ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed";
127}
128
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600129void XglRenderFramework::InitViewport(float width, float height)
130{
131 XGL_RESULT err;
132
Tony Barbourf52346d2015-01-16 14:27:35 -0700133 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700134 XGL_RECT scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600135
Tony Barbourf52346d2015-01-16 14:27:35 -0700136 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewportCreate = {};
137 viewportCreate.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700138 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700139 viewport.originX = 0;
140 viewport.originY = 0;
141 viewport.width = 1.f * width;
142 viewport.height = 1.f * height;
143 viewport.minDepth = 0.f;
144 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700145 scissor.extent.width = width;
146 scissor.extent.height = height;
147 scissor.offset.x = 0;
148 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700149 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700150 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700151
152 err = xglCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600153 ASSERT_XGL_SUCCESS( err );
154 m_width = width;
155 m_height = height;
156}
157
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600158void XglRenderFramework::InitViewport()
159{
160 InitViewport(m_width, m_height);
161}
162
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600163void XglRenderFramework::InitRenderTarget()
164{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600165 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800166
167 for (i = 0; i < m_renderTargetCount; i++) {
Chia-I Wuf50ee212014-12-29 14:31:52 +0800168 XglImage *img = new XglImage(m_device);
169 img->init(m_width, m_height, m_render_target_fmt,
170 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
171 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700172 m_colorBindings[i].view = img->targetView();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000173 m_colorBindings[i].layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700174 m_renderTargets.push_back(img);
Chia-I Wuecebf752014-12-05 10:45:15 +0800175 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700176 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
177 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_LOAD;
178 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_STORE;
Tobin Ehlis5de72572015-03-06 13:16:37 -0700179 XGL_CLEAR_COLOR clear_color;
180 clear_color.color.rawColor[0] = 64;
181 clear_color.color.rawColor[1] = 64;
182 clear_color.color.rawColor[2] = 64;
183 clear_color.color.rawColor[3] = 0;
184 clear_color.useRawValue = XGL_TRUE;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700185 XGL_DEPTH_STENCIL_BIND_INFO *dsBinding;
186 if (m_depthStencilBinding.view)
187 dsBinding = &m_depthStencilBinding;
188 else
189 dsBinding = NULL;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600190 XGL_FRAMEBUFFER_CREATE_INFO fb_info = {};
191 fb_info.sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
192 fb_info.pNext = NULL;
193 fb_info.colorAttachmentCount = m_renderTargetCount;
194 fb_info.pColorAttachments = m_colorBindings;
195 fb_info.pDepthStencilAttachment = dsBinding;
196 fb_info.sampleCount = 1;
197 fb_info.width = (uint32_t)m_width;
198 fb_info.height = (uint32_t)m_height;
199 fb_info.layers = 1;
200
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700201 XGL_RENDER_PASS_CREATE_INFO rp_info;
202 memset(&rp_info, 0 , sizeof(rp_info));
Tobin Ehlis976fc162015-03-26 08:23:25 -0600203 xglCreateFramebuffer(device(), &fb_info, &(m_frameBuffer));
204 rp_info.framebuffer = m_frameBuffer;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700205 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
206 rp_info.renderArea.extent.width = m_width;
207 rp_info.renderArea.extent.height = m_height;
Courtney Goeltzenleuchter3db6d9b2015-02-10 14:06:25 -0700208 rp_info.colorAttachmentCount = m_renderTargetCount;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700209 rp_info.pColorLoadOps = &load_op;
210 rp_info.pColorStoreOps = &store_op;
Tobin Ehlis5de72572015-03-06 13:16:37 -0700211 rp_info.pColorLoadClearValues = &clear_color;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700212 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
213 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
214 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
215 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
216 xglCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600217}
218
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600219
220
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600221XglDevice::XglDevice(uint32_t id, XGL_PHYSICAL_GPU obj) :
Chia-I Wufb1459b2014-12-29 15:23:20 +0800222 xgl_testing::Device(obj), id(id)
223{
224 init();
225
226 props = gpu().properties();
227 queue_props = &gpu().queue_properties()[0];
228}
229
230void XglDevice::get_device_queue()
231{
232 ASSERT_NE(true, graphics_queues().empty());
233 m_queue = graphics_queues()[0]->obj();
234}
235
Tony Barboure2c58df2014-11-25 13:18:32 -0700236XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
237{
238 m_device = device;
239 m_nextSlot = 0;
240
241}
242
Chia-I Wu11078b02015-01-04 16:27:24 +0800243XglDescriptorSetObj::~XglDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700244{
Chia-I Wu11078b02015-01-04 16:27:24 +0800245 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700246}
Tony Barbour82c39522014-12-04 14:33:33 -0700247
Chia-I Wu11078b02015-01-04 16:27:24 +0800248int XglDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700249{
Chia-I Wu11078b02015-01-04 16:27:24 +0800250 /* request a descriptor but do not update it */
251 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
252 tc.type = XGL_DESCRIPTOR_TYPE_RAW_BUFFER;
253 tc.count = 1;
254 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700255
Chia-I Wu11078b02015-01-04 16:27:24 +0800256 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700257}
Tony Barbour82c39522014-12-04 14:33:33 -0700258
Chia-I Wu11078b02015-01-04 16:27:24 +0800259int XglDescriptorSetObj::AppendBuffer(XGL_DESCRIPTOR_TYPE type, XglConstantBufferObj *constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700260{
Chia-I Wu11078b02015-01-04 16:27:24 +0800261 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
262 tc.type = type;
263 tc.count = 1;
264 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700265
Chia-I Wu11078b02015-01-04 16:27:24 +0800266 m_bufferInfo.push_back(&constantBuffer->m_bufferViewInfo);
267
268 m_updateBuffers.push_back(xgl_testing::DescriptorSet::update(type, m_nextSlot, 1,
269 (const XGL_BUFFER_VIEW_ATTACH_INFO **) NULL));
270
271 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700272}
Tony Barbour82c39522014-12-04 14:33:33 -0700273
Chia-I Wu11078b02015-01-04 16:27:24 +0800274int XglDescriptorSetObj::AppendSamplerTexture( XglSamplerObj* sampler, XglTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700275{
Chia-I Wu11078b02015-01-04 16:27:24 +0800276 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
277 tc.type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE;
278 tc.count = 1;
279 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700280
Chia-I Wu11078b02015-01-04 16:27:24 +0800281 XGL_SAMPLER_IMAGE_VIEW_INFO tmp = {};
282 tmp.pSampler = sampler->obj();
283 tmp.pImageView = &texture->m_textureViewInfo;
284 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700285
Chia-I Wu11078b02015-01-04 16:27:24 +0800286 m_updateSamplerTextures.push_back(xgl_testing::DescriptorSet::update(m_nextSlot, 1,
287 (const XGL_SAMPLER_IMAGE_VIEW_INFO *) NULL));
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}
Chia-I Wu11078b02015-01-04 16:27:24 +0800291
292XGL_DESCRIPTOR_SET_LAYOUT XglDescriptorSetObj::GetLayout()
Tony Barbourb5f4d082014-12-17 10:54:03 -0700293{
Chia-I Wu11078b02015-01-04 16:27:24 +0800294 return m_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700295}
296
297XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle()
298{
Chia-I Wu11078b02015-01-04 16:27:24 +0800299 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700300}
Tony Barboure2c58df2014-11-25 13:18:32 -0700301
Chia-I Wu11078b02015-01-04 16:27:24 +0800302void XglDescriptorSetObj::CreateXGLDescriptorSet(XglCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700303{
Chia-I Wu11078b02015-01-04 16:27:24 +0800304 // create XGL_DESCRIPTOR_REGION
305 XGL_DESCRIPTOR_REGION_CREATE_INFO region = {};
306 region.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO;
307 region.count = m_type_counts.size();
308 region.pTypeCount = &m_type_counts[0];
309 init(*m_device, XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1, region);
Tony Barbour824b7712014-12-18 17:06:21 -0700310
Chia-I Wu11078b02015-01-04 16:27:24 +0800311 // create XGL_DESCRIPTOR_SET_LAYOUT
312 vector<XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO> layout;
313 layout.resize(m_type_counts.size());
314 for (int i = 0; i < m_type_counts.size(); i++) {
315 layout[i].sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
316 layout[i].descriptorType = m_type_counts[i].type;
317 layout[i].count = m_type_counts[i].count;
318 layout[i].stageFlags = XGL_SHADER_STAGE_FLAGS_ALL;
319 layout[i].immutableSampler = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700320
Chia-I Wu11078b02015-01-04 16:27:24 +0800321 if (i < m_type_counts.size() - 1)
322 layout[i].pNext = &layout[i + 1];
323 else
324 layout[i].pNext = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700325 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800326
Chia-I Wu11078b02015-01-04 16:27:24 +0800327 m_layout.init(*m_device, 0, layout[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700328
Chia-I Wu11078b02015-01-04 16:27:24 +0800329 // create XGL_DESCRIPTOR_SET
330 m_set = alloc_sets(XGL_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
331
332 // build the update chain
333 for (int i = 0; i < m_updateBuffers.size(); i++) {
334 m_updateBuffers[i].pBufferViews = &m_bufferInfo[i];
335
336 if (i < m_updateBuffers.size() - 1)
337 m_updateBuffers[i].pNext = &m_updateBuffers[i + 1];
338 else if (m_updateSamplerTextures.empty())
339 m_updateBuffers[i].pNext = NULL;
340 else
341 m_updateBuffers[i].pNext = &m_updateSamplerTextures[0];
342 }
343 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
344 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
345
346 if (i < m_updateSamplerTextures.size() - 1)
347 m_updateSamplerTextures[i].pNext = &m_updateSamplerTextures[i + 1];
348 else
349 m_updateSamplerTextures[i].pNext = NULL;
350 }
351 const void *chain = (!m_updateBuffers.empty()) ? (const void *) &m_updateBuffers[0] :
352 (!m_updateSamplerTextures.empty()) ? (const void *) &m_updateSamplerTextures[0] :
353 NULL;
354
355 // do the updates
356 m_device->begin_descriptor_region_update(XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
357 clear_sets(*m_set);
358 m_set->update(chain);
359 m_device->end_descriptor_region_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700360}
Tony Barboure2c58df2014-11-25 13:18:32 -0700361
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800362XglImage::XglImage(XglDevice *dev)
363{
364 m_device = dev;
365 m_imageInfo.view = XGL_NULL_HANDLE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000366 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800367}
368
Tony Barbour579f7802015-04-03 15:11:43 -0600369static bool IsCompatible(XGL_FLAGS usage, XGL_FLAGS features)
370{
371 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) &&
372 !(features & XGL_FORMAT_IMAGE_SHADER_READ_BIT))
373 return false;
374 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT) &&
375 !(features & XGL_FORMAT_IMAGE_SHADER_WRITE_BIT))
376 return false;
377 return true;
378}
379
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600380void XglImage::init(uint32_t w, uint32_t h,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800381 XGL_FORMAT fmt, XGL_FLAGS usage,
Tony Barbour579f7802015-04-03 15:11:43 -0600382 XGL_IMAGE_TILING requested_tiling)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800383{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600384 uint32_t mipCount;
Tony Barbour579f7802015-04-03 15:11:43 -0600385 XGL_FORMAT_PROPERTIES image_fmt;
386 XGL_IMAGE_TILING tiling;
387 XGL_RESULT err;
388 size_t size;
389
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800390 mipCount = 0;
391
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600392 uint32_t _w = w;
393 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800394 while( ( _w > 0 ) || ( _h > 0 ) )
395 {
396 _w >>= 1;
397 _h >>= 1;
398 mipCount++;
399 }
400
Tony Barbour579f7802015-04-03 15:11:43 -0600401 size = sizeof(image_fmt);
402 err = xglGetFormatInfo(m_device->obj(), fmt,
403 XGL_INFO_TYPE_FORMAT_PROPERTIES,
404 &size, &image_fmt);
405 ASSERT_XGL_SUCCESS(err);
406
407 if (requested_tiling == XGL_LINEAR_TILING) {
408 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
409 tiling = XGL_LINEAR_TILING;
410 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
411 tiling = XGL_OPTIMAL_TILING;
412 } else {
413 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
414 }
415 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
416 tiling = XGL_OPTIMAL_TILING;
417 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
418 tiling = XGL_LINEAR_TILING;
419 } else {
420 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
421 }
422
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800423 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
424 imageCreateInfo.imageType = XGL_IMAGE_2D;
425 imageCreateInfo.format = fmt;
426 imageCreateInfo.extent.width = w;
427 imageCreateInfo.extent.height = h;
428 imageCreateInfo.mipLevels = mipCount;
429 imageCreateInfo.tiling = tiling;
430
431 imageCreateInfo.usage = usage;
432
433 xgl_testing::Image::init(*m_device, imageCreateInfo);
434
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000435 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800436}
437
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600438XGL_RESULT XglImage::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800439{
440 *ptr = map();
441 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
442}
443
444XGL_RESULT XglImage::UnmapMemory()
445{
446 unmap();
447 return XGL_SUCCESS;
448}
449
Tony Barbour5dc515d2015-04-01 17:47:06 -0600450XGL_RESULT XglImage::CopyImage(XglImage &fromImage)
451{
452 XGL_RESULT err;
453
454 XGL_CMD_BUFFER cmd_buf;
455 XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {};
456 cmd_buf_create_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
457 cmd_buf_create_info.pNext = NULL;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700458 cmd_buf_create_info.queueNodeIndex = 0;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600459 cmd_buf_create_info.flags = 0;
460
461 err = xglCreateCommandBuffer(m_device->device(), &cmd_buf_create_info, &cmd_buf);
462 assert(!err);
463
464 /* Copy staging texture to usable texture */
465 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {};
466 cmd_buf_begin_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
467 cmd_buf_begin_info.pNext = NULL;
468 cmd_buf_begin_info.flags = 0;
469
470 err = xglResetCommandBuffer(cmd_buf);
471 assert(!err);
472
473 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_begin_info);
474 assert(!err);
475
476 XGL_IMAGE_COPY copy_region = {};
477 copy_region.srcSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
478 copy_region.srcSubresource.arraySlice = 0;
479 copy_region.srcSubresource.mipLevel = 0;
480 copy_region.srcOffset.x = 0;
481 copy_region.srcOffset.y = 0;
482 copy_region.srcOffset.z = 0;
483 copy_region.destSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
484 copy_region.destSubresource.arraySlice = 0;
485 copy_region.destSubresource.mipLevel = 0;
486 copy_region.destOffset.x = 0;
487 copy_region.destOffset.y = 0;
488 copy_region.destOffset.z = 0;
489 copy_region.extent = fromImage.extent();
490
491 xglCmdCopyImage(cmd_buf, fromImage.obj(), obj(), 1, &copy_region);
492
493 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {};
494 image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
495 image_memory_barrier.pNext = NULL;
496 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT;
497 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
498 image_memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_GENERAL;
499 image_memory_barrier.newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL;
500 image_memory_barrier.image = fromImage.obj();
501 image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
502 image_memory_barrier.subresourceRange.baseMipLevel = 0;
503 image_memory_barrier.subresourceRange.mipLevels = 1;
504 image_memory_barrier.subresourceRange.baseArraySlice = 0;
505 image_memory_barrier.subresourceRange.arraySize = 0;
506
507 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
508
509 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
510 XGL_PIPELINE_BARRIER pipeline_barrier;
511 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
512 pipeline_barrier.pNext = NULL;
513 pipeline_barrier.eventCount = 1;
514 pipeline_barrier.pEvents = set_events;
515 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
516 pipeline_barrier.memBarrierCount = 1;
517 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
518
519 // write barrier to the command buffer
520 xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier);
521
522 err = xglEndCommandBuffer(cmd_buf);
523 assert(!err);
524
525 const XGL_CMD_BUFFER cmd_bufs[] = { cmd_buf };
526 XGL_MEMORY_REF mem_refs[16];
527 uint32_t num_refs = 0;
528 const std::vector<XGL_GPU_MEMORY> from_mems = fromImage.memories();
529 const std::vector<XGL_GPU_MEMORY> to_mems = memories();
530
531 for (uint32_t j = 0; j < from_mems.size(); j++) {
532 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
533 mem_refs[num_refs].mem = from_mems[j];
534 num_refs++;
535 assert(num_refs < 16);
536 }
537
538 for (uint32_t j = 0; j < to_mems.size(); j++) {
539 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
540 mem_refs[num_refs].mem = to_mems[j];
541 num_refs++;
542 assert(num_refs < 16);
543 }
544
545 err = xglQueueSubmit(m_device->m_queue, 1, cmd_bufs,
546 num_refs, mem_refs, XGL_NULL_HANDLE);
547 assert(!err);
548
549 err = xglQueueWaitIdle(m_device->m_queue);
550 assert(!err);
551
552 xglDestroyObject(cmd_buf);
553
554 return XGL_SUCCESS;
555}
556
Tony Barbourebc093f2015-04-01 16:38:10 -0600557XglTextureObj::XglTextureObj(XglDevice *device, uint32_t *colors)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600558 :XglImage(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700559{
560 m_device = device;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700561 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600562 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600563 void *data;
564 int32_t x, y;
565 XglImage stagingImage(device);
566
567 stagingImage.init(16, 16, tex_format, 0, XGL_LINEAR_TILING);
568 XGL_SUBRESOURCE_LAYOUT layout = stagingImage.subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600569
570 if (colors == NULL)
571 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700572
573 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
574
575 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
576
Tony Barbour5dc515d2015-04-01 17:47:06 -0600577 XGL_IMAGE_VIEW_CREATE_INFO view = {};
Tony Barbourbdf0a312015-04-01 17:10:07 -0600578 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
579 view.pNext = NULL;
580 view.image = XGL_NULL_HANDLE;
581 view.viewType = XGL_IMAGE_VIEW_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600582 view.format = tex_format;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600583 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
584 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
585 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
586 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
587 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
588 view.subresourceRange.baseMipLevel = 0;
589 view.subresourceRange.mipLevels = 1;
590 view.subresourceRange.baseArraySlice = 0;
591 view.subresourceRange.arraySize = 1;
592 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700593
Tony Barboure2c58df2014-11-25 13:18:32 -0700594 /* create image */
Tony Barbour5dc515d2015-04-01 17:47:06 -0600595 init(16, 16, tex_format, XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, XGL_OPTIMAL_TILING);
Tony Barboure2c58df2014-11-25 13:18:32 -0700596
597 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800598 view.image = obj();
599 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600600 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700601
Tony Barbour5dc515d2015-04-01 17:47:06 -0600602 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700603
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800604 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700605 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800606 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600607 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700608 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600609 stagingImage.unmap();
610 XglImage::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700611}
Tony Barbour82c39522014-12-04 14:33:33 -0700612
Tony Barboure2c58df2014-11-25 13:18:32 -0700613XglSamplerObj::XglSamplerObj(XglDevice *device)
614{
Tony Barboure2c58df2014-11-25 13:18:32 -0700615 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700616
Chia-I Wue9864b52014-12-28 16:32:24 +0800617 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
618 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
619 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
620 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
621 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
622 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
623 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
624 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
625 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
626 samplerCreateInfo.mipLodBias = 0.0;
627 samplerCreateInfo.maxAnisotropy = 0.0;
628 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
629 samplerCreateInfo.minLod = 0.0;
630 samplerCreateInfo.maxLod = 0.0;
631 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700632
Chia-I Wue9864b52014-12-28 16:32:24 +0800633 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700634}
Tony Barboure2c58df2014-11-25 13:18:32 -0700635
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700636/*
637 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
638 */
639XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
640{
641 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700642 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700643
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800644 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700645}
646
Tony Barboure2c58df2014-11-25 13:18:32 -0700647XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
648{
Tony Barboure2c58df2014-11-25 13:18:32 -0700649 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800650 m_commandBuffer = 0;
651
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800652 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700653 m_numVertices = constantCount;
654 m_stride = constantSize;
655
Chia-I Wua07fee62014-12-28 15:26:08 +0800656 const size_t allocationSize = constantCount * constantSize;
657 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700658
Chia-I Wua07fee62014-12-28 15:26:08 +0800659 void *pData = map();
660 memcpy(pData, data, allocationSize);
661 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700662
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800663 // set up the buffer view for the constant buffer
664 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
665 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
666 view_info.buffer = obj();
Chia-I Wubb0c8d22015-01-16 22:31:25 +0800667 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800668 view_info.offset = 0;
669 view_info.range = allocationSize;
670 m_bufferView.init(*m_device, view_info);
671
672 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
673 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700674}
Tony Barbour82c39522014-12-04 14:33:33 -0700675
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600676void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700677{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800678 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700679}
680
681
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000682void XglConstantBufferObj::BufferMemoryBarrier(
683 XGL_FLAGS outputMask /*=
684 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
685 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
686 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
687 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
688 XGL_MEMORY_OUTPUT_COPY_BIT*/,
689 XGL_FLAGS inputMask /*=
690 XGL_MEMORY_INPUT_CPU_READ_BIT |
691 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
692 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
693 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
694 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
695 XGL_MEMORY_INPUT_SHADER_READ_BIT |
696 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
697 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
698 XGL_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700699{
Tony Barbour38422802014-12-10 14:36:31 -0700700 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700701
Tony Barbour38422802014-12-10 14:36:31 -0700702 if (!m_commandBuffer)
703 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800704 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700705
706 m_commandBuffer = new XglCommandBufferObj(m_device);
707
708 }
709 else
710 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800711 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700712 }
713
Tony Barboure2c58df2014-11-25 13:18:32 -0700714 // open the command buffer
Tony Barbourbdf0a312015-04-01 17:10:07 -0600715 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {};
716 cmd_buf_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
717 cmd_buf_info.pNext = NULL;
718 cmd_buf_info.flags = 0;
719
Jon Ashburnc4164b12014-12-31 17:10:47 -0700720 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700721 ASSERT_XGL_SUCCESS(err);
722
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000723 XGL_BUFFER_MEMORY_BARRIER memory_barrier =
724 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Mark Lobodzinski837ef922015-01-29 14:24:14 -0600725 XGL_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700726
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000727 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
728 XGL_PIPELINE_BARRIER pipeline_barrier = {};
729 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
730 pipeline_barrier.eventCount = 1;
731 pipeline_barrier.pEvents = set_events;
732 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
733 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -0600734 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000735
736 // write barrier to the command buffer
737 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700738
739 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700740 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700741 ASSERT_XGL_SUCCESS(err);
742
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600743 uint32_t numMemRefs=1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700744 XGL_MEMORY_REF memRefs;
745 // this command buffer only uses the vertex buffer memory
746 memRefs.flags = 0;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800747 memRefs.mem = memories()[0];
Tony Barboure2c58df2014-11-25 13:18:32 -0700748
749 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700750 XGL_CMD_BUFFER bufferArray[1];
751 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800752 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700753 ASSERT_XGL_SUCCESS(err);
754}
755
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700756XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
757 : XglConstantBufferObj(device)
758{
759
760}
761
762void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
763{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700764 XGL_FORMAT viewFormat;
765
766 m_numVertices = numIndexes;
767 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700768 switch (indexType) {
769 case XGL_INDEX_8:
770 m_stride = 1;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700771 viewFormat = XGL_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700772 break;
773 case XGL_INDEX_16:
774 m_stride = 2;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700775 viewFormat = XGL_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700776 break;
777 case XGL_INDEX_32:
778 m_stride = 4;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700779 viewFormat = XGL_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700780 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800781 default:
782 assert(!"unknown index type");
783 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700784 }
785
Chia-I Wua07fee62014-12-28 15:26:08 +0800786 const size_t allocationSize = numIndexes * m_stride;
787 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700788
Chia-I Wua07fee62014-12-28 15:26:08 +0800789 void *pData = map();
790 memcpy(pData, data, allocationSize);
791 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700792
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800793 // set up the buffer view for the constant buffer
794 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
795 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
796 view_info.buffer = obj();
797 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
798 view_info.stride = m_stride;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700799 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800800 view_info.channels.r = XGL_CHANNEL_SWIZZLE_R;
801 view_info.channels.g = XGL_CHANNEL_SWIZZLE_G;
802 view_info.channels.b = XGL_CHANNEL_SWIZZLE_B;
803 view_info.channels.a = XGL_CHANNEL_SWIZZLE_A;
804 view_info.offset = 0;
805 view_info.range = allocationSize;
806 m_bufferView.init(*m_device, view_info);
807
808 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
809 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700810}
811
812void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
813{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800814 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700815}
Tony Barboure2c58df2014-11-25 13:18:32 -0700816
Tony Barbouraf1f9192014-12-17 10:57:58 -0700817XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
818{
819 return m_indexType;
820}
821
Chia-I Wu11078b02015-01-04 16:27:24 +0800822XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700823{
Tony Barboure2c58df2014-11-25 13:18:32 -0700824 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
825 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
826 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800827 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700828 stageInfo->shader.linkConstBufferCount = 0;
829 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700830
Tony Barboure2c58df2014-11-25 13:18:32 -0700831 return stageInfo;
832}
833
Tony Barboure2c58df2014-11-25 13:18:32 -0700834XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
835{
836 XGL_RESULT err = XGL_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600837 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700838 XGL_SHADER_CREATE_INFO createInfo;
839 size_t shader_len;
840
841 m_stage = stage;
842 m_device = device;
843
844 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
845 createInfo.pNext = NULL;
846
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600847 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700848
849 shader_len = strlen(shader_code);
850 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
851 createInfo.pCode = malloc(createInfo.codeSize);
852 createInfo.flags = 0;
853
854 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600855 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700856 ((uint32_t *) createInfo.pCode)[1] = 0;
857 ((uint32_t *) createInfo.pCode)[2] = stage;
858 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
859
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800860 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700861 }
862
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600863 if (framework->m_use_spv || err) {
864 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700865 err = XGL_SUCCESS;
866
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600867 // Use Reference GLSL to SPV compiler
868 framework->GLSLtoSPV(stage, shader_code, spv);
869 createInfo.pCode = spv.data();
870 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700871 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700872
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800873 init(*m_device, createInfo);
874 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700875}
Tony Barbour82c39522014-12-04 14:33:33 -0700876
Tony Barboure2c58df2014-11-25 13:18:32 -0700877XglPipelineObj::XglPipelineObj(XglDevice *device)
878{
Tony Barboure2c58df2014-11-25 13:18:32 -0700879 m_device = device;
880 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
881 m_vertexBufferCount = 0;
882
883 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
884 m_ia_state.pNext = XGL_NULL_HANDLE;
885 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
886 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700887 m_ia_state.primitiveRestartEnable = XGL_FALSE;
888 m_ia_state.primitiveRestartIndex = 0;
889
890 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
891 m_rs_state.pNext = &m_ia_state;
892 m_rs_state.depthClipEnable = XGL_FALSE;
893 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Chia-I Wuc8d1ec52015-03-24 11:01:50 +0800894 m_rs_state.programPointSize = XGL_FALSE;
895 m_rs_state.pointOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700896 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
897 m_rs_state.fillMode = XGL_FILL_SOLID;
898 m_rs_state.cullMode = XGL_CULL_NONE;
899 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700900
Tony Barboure2c58df2014-11-25 13:18:32 -0700901 memset(&m_cb_state,0,sizeof(m_cb_state));
902 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
903 m_cb_state.pNext = &m_rs_state;
904 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700905 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
906
Tony Barbourf52346d2015-01-16 14:27:35 -0700907 m_ms_state.pNext = &m_cb_state;
908 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
909 m_ms_state.multisampleEnable = XGL_FALSE;
910 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
911 m_ms_state.samples = 1;
912 m_ms_state.minSampleShading = 0;
913 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700914
Tony Barbourf52346d2015-01-16 14:27:35 -0700915 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
916 m_ds_state.pNext = &m_ms_state,
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700917 m_ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700918 m_ds_state.depthTestEnable = XGL_FALSE;
919 m_ds_state.depthWriteEnable = XGL_FALSE;
920 m_ds_state.depthBoundsEnable = XGL_FALSE;
921 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
922 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
923 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
924 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
925 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
926 m_ds_state.stencilTestEnable = XGL_FALSE;
927 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -0700928
Tony Barbourf52346d2015-01-16 14:27:35 -0700929 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
930 att.blendEnable = XGL_FALSE;
Tony Barboura53a6942015-02-25 11:25:11 -0700931 att.format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -0700932 att.channelWriteMask = 0xf;
933 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -0700934
935};
936
937void XglPipelineObj::AddShader(XglShaderObj* shader)
938{
939 m_shaderObjs.push_back(shader);
940}
941
942void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
943{
944 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
945 m_vi_state.attributeCount = count;
946}
947
948void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
949{
950 m_vi_state.pVertexBindingDescriptions = vi_binding;
951 m_vi_state.bindingCount = count;
952}
953
954void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
955{
956 m_vertexBufferObjs.push_back(vertexDataBuffer);
957 m_vertexBufferBindings.push_back(binding);
958 m_vertexBufferCount++;
959}
960
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600961void XglPipelineObj::AddColorAttachment(uint32_t binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +0800962{
Tony Barbourf52346d2015-01-16 14:27:35 -0700963 if (binding+1 > m_colorAttachments.size())
964 {
965 m_colorAttachments.resize(binding+1);
966 }
967 m_colorAttachments[binding] = *att;
968}
969
970void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
971{
972 m_ds_state.format = ds_state->format;
973 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
974 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
975 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
976 m_ds_state.depthFunc = ds_state->depthFunc;
977 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
978 m_ds_state.back = ds_state->back;
979 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +0800980}
981
Tony Barbour976e1cf2014-12-17 11:57:31 -0700982void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
983{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600984 void* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -0700985 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
986
987 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
988
989 for (int i=0; i<m_shaderObjs.size(); i++)
990 {
Chia-I Wu11078b02015-01-04 16:27:24 +0800991 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700992 shaderCreateInfo->pNext = head_ptr;
993 head_ptr = shaderCreateInfo;
994 }
995
996 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
997 {
998 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
999 m_vi_state.pNext = head_ptr;
1000 head_ptr = &m_vi_state;
1001 }
1002
1003 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1004 info.pNext = head_ptr;
1005 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +08001006 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001007
Tony Barbourf52346d2015-01-16 14:27:35 -07001008 m_cb_state.attachmentCount = m_colorAttachments.size();
1009 m_cb_state.pAttachments = &m_colorAttachments[0];
1010
Chia-I Wu2648d092014-12-29 14:24:14 +08001011 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001012}
Chia-I Wu2648d092014-12-29 14:24:14 +08001013
Tony Barbour976e1cf2014-12-17 11:57:31 -07001014XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
1015{
Chia-I Wu2648d092014-12-29 14:24:14 +08001016 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001017}
1018
Tony Barbour5420af02014-12-03 13:58:15 -07001019void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -07001020{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001021 void* head_ptr = &m_ds_state;
Tony Barboure2c58df2014-11-25 13:18:32 -07001022 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1023
1024 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -07001025
1026 for (int i=0; i<m_shaderObjs.size(); i++)
1027 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001028 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barboure2c58df2014-11-25 13:18:32 -07001029 shaderCreateInfo->pNext = head_ptr;
1030 head_ptr = shaderCreateInfo;
1031 }
1032
1033 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1034 {
1035 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1036 m_vi_state.pNext = head_ptr;
1037 head_ptr = &m_vi_state;
1038 }
1039
1040 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1041 info.pNext = head_ptr;
1042 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +08001043 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barboure2c58df2014-11-25 13:18:32 -07001044
Chia-I Wu2648d092014-12-29 14:24:14 +08001045 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -07001046
Chia-I Wu2648d092014-12-29 14:24:14 +08001047 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -07001048
1049
1050 for (int i=0; i < m_vertexBufferCount; i++)
1051 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001052 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001053 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001054}
Tony Barbour82c39522014-12-04 14:33:33 -07001055
Tony Barboure2c58df2014-11-25 13:18:32 -07001056XglMemoryRefManager::XglMemoryRefManager() {
1057
1058}
Tony Barbour82c39522014-12-04 14:33:33 -07001059
Tony Barboure2c58df2014-11-25 13:18:32 -07001060void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001061 const std::vector<XGL_GPU_MEMORY> mems = constantBuffer->memories();
1062 if (!mems.empty())
1063 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001064}
Tony Barbour82c39522014-12-04 14:33:33 -07001065
Tony Barboure2c58df2014-11-25 13:18:32 -07001066void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
Chia-I Wu13a3aa82014-12-28 15:55:09 +08001067 const std::vector<XGL_GPU_MEMORY> mems = texture->memories();
1068 if (!mems.empty())
1069 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001070}
Tony Barbour82c39522014-12-04 14:33:33 -07001071
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001072void XglMemoryRefManager::AddMemoryRef(XGL_GPU_MEMORY *mem, uint32_t refCount) {
1073 for (size_t i = 0; i < refCount; i++) {
1074 m_bufferObjs.push_back(mem[i]);
1075 }
1076}
1077
1078void XglMemoryRefManager::AddRTMemoryRefs(vector<XglImage*>images, uint32_t rtCount) {
1079 for (uint32_t i = 0; i < rtCount; i++) {
1080 const std::vector<XGL_GPU_MEMORY> mems = images[i]->memories();
1081 if (!mems.empty())
1082 m_bufferObjs.push_back(mems[0]);
1083 }
1084}
1085
Tony Barboure2c58df2014-11-25 13:18:32 -07001086XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
1087
1088 XGL_MEMORY_REF *localRefs;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001089 uint32_t numRefs=m_bufferObjs.size();
Tony Barboure2c58df2014-11-25 13:18:32 -07001090
1091 if (numRefs <= 0)
1092 return NULL;
1093
1094 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
1095 for (int i=0; i<numRefs; i++)
1096 {
1097 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +08001098 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -07001099 }
1100 return localRefs;
1101}
1102int XglMemoryRefManager::GetNumRefs() {
1103 return m_bufferObjs.size();
1104}
Tony Barbour6d047bf2014-12-10 14:34:45 -07001105
1106XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -07001107 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001108{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001109 m_device = device;
Chia-I Wud28343c2014-12-28 15:12:48 +08001110 m_renderTargetCount = 0;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001111}
Tony Barbour471338d2014-12-10 17:28:39 -07001112
Tony Barbour6d047bf2014-12-10 14:34:45 -07001113XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1114{
Chia-I Wud28343c2014-12-28 15:12:48 +08001115 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001116}
Tony Barbour471338d2014-12-10 17:28:39 -07001117
Jon Ashburnc4164b12014-12-31 17:10:47 -07001118XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001119{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001120 begin(pInfo);
1121 return XGL_SUCCESS;
1122}
1123
1124XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj)
1125{
1126 begin(renderpass_obj);
1127 return XGL_SUCCESS;
1128}
1129
1130XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
1131{
1132 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +08001133 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001134}
1135
1136XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1137{
Chia-I Wud28343c2014-12-28 15:12:48 +08001138 end();
1139 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001140}
1141
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001142void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001143{
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001144 xglCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001145}
1146
Tony Barbour30cc9e82014-12-17 11:53:55 -07001147void XglCommandBufferObj::ClearAllBuffers(XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
1148{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001149 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001150 const XGL_FLAGS output_mask =
1151 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1152 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1153 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1154 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1155 XGL_MEMORY_OUTPUT_COPY_BIT;
1156 const XGL_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001157
1158 // whatever we want to do, we do it to the whole buffer
1159 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1160 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1161 srRange.baseMipLevel = 0;
1162 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1163 srRange.baseArraySlice = 0;
1164 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1165
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001166 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1167 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1168 memory_barrier.outputMask = output_mask;
1169 memory_barrier.inputMask = input_mask;
1170 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1171 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001172 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001173
1174 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1175 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1176 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1177 pipeline_barrier.eventCount = 1;
1178 pipeline_barrier.pEvents = set_events;
1179 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1180 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001181 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001182
Tony Barbour30cc9e82014-12-17 11:53:55 -07001183 // clear the back buffer to dark grey
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001184 uint32_t clearColor[4] = {64, 64, 64, 0};
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001185
Tony Barbour30cc9e82014-12-17 11:53:55 -07001186 for (i = 0; i < m_renderTargetCount; i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001187 memory_barrier.image = m_renderTargets[i]->image();
1188 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1189 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1190 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001191
Chia-I Wud28343c2014-12-28 15:12:48 +08001192 xglCmdClearColorImageRaw( obj(), m_renderTargets[i]->image(), clearColor, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001193 }
1194
1195 if (depthStencilImage)
1196 {
1197 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1198 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1199 dsRange.baseMipLevel = 0;
1200 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1201 dsRange.baseArraySlice = 0;
1202 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1203
1204 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001205
1206 memory_barrier.oldLayout = depthStencilBinding->layout;
1207 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1208 memory_barrier.image = depthStencilImage;
1209 memory_barrier.subresourceRange = dsRange;
1210
1211 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1212 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001213
Chia-I Wud28343c2014-12-28 15:12:48 +08001214 xglCmdClearDepthStencil(obj(), depthStencilImage, 1.0f, 0, 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001215
1216 // prepare depth buffer for rendering
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001217 memory_barrier.image = depthStencilImage;
1218 memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1219 memory_barrier.newLayout = depthStencilBinding->layout;
1220 memory_barrier.subresourceRange = dsRange;
1221 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1222 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001223 }
1224}
1225
Jon Ashburncdc40be2015-01-02 18:27:14 -07001226void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001227{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001228 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001229 const XGL_FLAGS output_mask =
1230 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1231 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1232 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1233 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1234 XGL_MEMORY_OUTPUT_COPY_BIT;
1235 const XGL_FLAGS input_mask =
1236 XGL_MEMORY_INPUT_CPU_READ_BIT |
1237 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1238 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
1239 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1240 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
1241 XGL_MEMORY_INPUT_SHADER_READ_BIT |
1242 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1243 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1244 XGL_MEMORY_INPUT_COPY_BIT;
1245
Tony Barbour30cc9e82014-12-17 11:53:55 -07001246 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1247 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1248 srRange.baseMipLevel = 0;
1249 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1250 srRange.baseArraySlice = 0;
1251 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1252
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001253 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1254 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1255 memory_barrier.outputMask = output_mask;
1256 memory_barrier.inputMask = input_mask;
1257 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1258 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001259 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001260
1261 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1262 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1263 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1264 pipeline_barrier.eventCount = 1;
1265 pipeline_barrier.pEvents = set_events;
1266 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1267 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001268 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001269
Tony Barbour30cc9e82014-12-17 11:53:55 -07001270 for(i=0; i<m_renderTargetCount; i++)
1271 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001272 memory_barrier.image = m_renderTargets[i]->image();
1273 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1274 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1275 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001276 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001277}
1278
Tony Barbourf52346d2015-01-16 14:27:35 -07001279void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001280{
Tony Barbourf52346d2015-01-16 14:27:35 -07001281 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001282}
1283
1284void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1285{
1286 m_renderTargets.push_back(renderTarget);
1287 m_renderTargetCount++;
1288}
1289
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001290void XglCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001291{
Chia-I Wud28343c2014-12-28 15:12:48 +08001292 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001293}
1294
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001295void XglCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001296{
Chia-I Wud28343c2014-12-28 15:12:48 +08001297 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001298}
1299
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001300void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, uint32_t numMemRefs)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001301{
1302 XGL_RESULT err = XGL_SUCCESS;
1303
1304 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001305 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001306 ASSERT_XGL_SUCCESS( err );
1307
1308 err = xglQueueWaitIdle( m_device->m_queue );
1309 ASSERT_XGL_SUCCESS( err );
1310
1311 // Wait for work to finish before cleaning up.
1312 xglDeviceWaitIdle(m_device->device());
1313
1314}
1315void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1316{
Chia-I Wud28343c2014-12-28 15:12:48 +08001317 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001318}
1319
1320void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1321{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001322 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wu11078b02015-01-04 16:27:24 +08001323 xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001324}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001325void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001326{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001327 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001328}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001329void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001330{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001331 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001332}