blob: 007d1474f158febf5d8a31ebd1a74d9f5a4444ea [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);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -060079 if (m_framebuffer) xglDestroyObject(m_framebuffer);
Tobin Ehlis976fc162015-03-26 08:23:25 -060080 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{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600177 std::vector<XGL_ATTACHMENT_LOAD_OP> load_ops;
178 std::vector<XGL_ATTACHMENT_STORE_OP> store_ops;
179 std::vector<XGL_CLEAR_COLOR> clear_colors;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600180 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800181
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700182 for (i = 0; i < targets; i++) {
Chia-I Wuf50ee212014-12-29 14:31:52 +0800183 XglImage *img = new XglImage(m_device);
184 img->init(m_width, m_height, m_render_target_fmt,
185 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
186 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700187 m_colorBindings[i].view = img->targetView();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000188 m_colorBindings[i].layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700189 m_renderTargets.push_back(img);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600190 load_ops.push_back(XGL_ATTACHMENT_LOAD_OP_LOAD);
191 store_ops.push_back(XGL_ATTACHMENT_STORE_OP_STORE);
192 clear_colors.push_back(m_clear_color);
Chia-I Wuecebf752014-12-05 10:45:15 +0800193 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700194 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700195 XGL_DEPTH_STENCIL_BIND_INFO *dsBinding;
196 if (m_depthStencilBinding.view)
197 dsBinding = &m_depthStencilBinding;
198 else
199 dsBinding = NULL;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600200 XGL_FRAMEBUFFER_CREATE_INFO fb_info = {};
201 fb_info.sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
202 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700203 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600204 fb_info.pColorAttachments = m_colorBindings;
205 fb_info.pDepthStencilAttachment = dsBinding;
206 fb_info.sampleCount = 1;
207 fb_info.width = (uint32_t)m_width;
208 fb_info.height = (uint32_t)m_height;
209 fb_info.layers = 1;
210
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700211 XGL_RENDER_PASS_CREATE_INFO rp_info;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600212
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700213 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600214 xglCreateFramebuffer(device(), &fb_info, &m_framebuffer);
215
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700216 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
217 rp_info.renderArea.extent.width = m_width;
218 rp_info.renderArea.extent.height = m_height;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700219 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600220 rp_info.pColorFormats = &m_render_target_fmt;
221 rp_info.pColorLayouts = &m_colorBindings[0].layout;
222 rp_info.pColorLoadOps = &load_ops[0];
223 rp_info.pColorStoreOps = &store_ops[0];
224 rp_info.pColorLoadClearValues = &clear_colors[0];
225 rp_info.depthStencilFormat = m_depth_stencil_fmt;
226 rp_info.depthStencilLayout = m_depthStencilBinding.layout;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700227 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600228 rp_info.depthLoadClearValue = m_depth_clear_color;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700229 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
230 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600231 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700232 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
233 xglCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600234}
235
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600236
237
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600238XglDevice::XglDevice(uint32_t id, XGL_PHYSICAL_GPU obj) :
Chia-I Wufb1459b2014-12-29 15:23:20 +0800239 xgl_testing::Device(obj), id(id)
240{
241 init();
242
243 props = gpu().properties();
244 queue_props = &gpu().queue_properties()[0];
245}
246
247void XglDevice::get_device_queue()
248{
249 ASSERT_NE(true, graphics_queues().empty());
250 m_queue = graphics_queues()[0]->obj();
251}
252
Tony Barboure2c58df2014-11-25 13:18:32 -0700253XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
254{
255 m_device = device;
256 m_nextSlot = 0;
257
258}
259
Chia-I Wu11078b02015-01-04 16:27:24 +0800260XglDescriptorSetObj::~XglDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700261{
Chia-I Wu11078b02015-01-04 16:27:24 +0800262 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700263}
Tony Barbour82c39522014-12-04 14:33:33 -0700264
Chia-I Wu11078b02015-01-04 16:27:24 +0800265int XglDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700266{
Chia-I Wu11078b02015-01-04 16:27:24 +0800267 /* request a descriptor but do not update it */
268 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
269 tc.type = XGL_DESCRIPTOR_TYPE_RAW_BUFFER;
270 tc.count = 1;
271 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700272
Chia-I Wu11078b02015-01-04 16:27:24 +0800273 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700274}
Tony Barbour82c39522014-12-04 14:33:33 -0700275
Chia-I Wu11078b02015-01-04 16:27:24 +0800276int XglDescriptorSetObj::AppendBuffer(XGL_DESCRIPTOR_TYPE type, XglConstantBufferObj *constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700277{
Chia-I Wu11078b02015-01-04 16:27:24 +0800278 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
279 tc.type = type;
280 tc.count = 1;
281 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700282
Chia-I Wu11078b02015-01-04 16:27:24 +0800283 m_bufferInfo.push_back(&constantBuffer->m_bufferViewInfo);
284
285 m_updateBuffers.push_back(xgl_testing::DescriptorSet::update(type, m_nextSlot, 1,
286 (const XGL_BUFFER_VIEW_ATTACH_INFO **) NULL));
287
288 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700289}
Tony Barbour82c39522014-12-04 14:33:33 -0700290
Chia-I Wu11078b02015-01-04 16:27:24 +0800291int XglDescriptorSetObj::AppendSamplerTexture( XglSamplerObj* sampler, XglTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700292{
Chia-I Wu11078b02015-01-04 16:27:24 +0800293 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
294 tc.type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE;
295 tc.count = 1;
296 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700297
Chia-I Wu11078b02015-01-04 16:27:24 +0800298 XGL_SAMPLER_IMAGE_VIEW_INFO tmp = {};
299 tmp.pSampler = sampler->obj();
300 tmp.pImageView = &texture->m_textureViewInfo;
301 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700302
Chia-I Wu11078b02015-01-04 16:27:24 +0800303 m_updateSamplerTextures.push_back(xgl_testing::DescriptorSet::update(m_nextSlot, 1,
304 (const XGL_SAMPLER_IMAGE_VIEW_INFO *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700305
Chia-I Wu11078b02015-01-04 16:27:24 +0800306 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700307}
Chia-I Wu11078b02015-01-04 16:27:24 +0800308
309XGL_DESCRIPTOR_SET_LAYOUT XglDescriptorSetObj::GetLayout()
Tony Barbourb5f4d082014-12-17 10:54:03 -0700310{
Chia-I Wu11078b02015-01-04 16:27:24 +0800311 return m_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700312}
313
314XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle()
315{
Chia-I Wu11078b02015-01-04 16:27:24 +0800316 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700317}
Tony Barboure2c58df2014-11-25 13:18:32 -0700318
Chia-I Wu11078b02015-01-04 16:27:24 +0800319void XglDescriptorSetObj::CreateXGLDescriptorSet(XglCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700320{
Chia-I Wu11078b02015-01-04 16:27:24 +0800321 // create XGL_DESCRIPTOR_REGION
322 XGL_DESCRIPTOR_REGION_CREATE_INFO region = {};
323 region.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO;
324 region.count = m_type_counts.size();
325 region.pTypeCount = &m_type_counts[0];
326 init(*m_device, XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1, region);
Tony Barbour824b7712014-12-18 17:06:21 -0700327
Chia-I Wu11078b02015-01-04 16:27:24 +0800328 // create XGL_DESCRIPTOR_SET_LAYOUT
329 vector<XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO> layout;
330 layout.resize(m_type_counts.size());
331 for (int i = 0; i < m_type_counts.size(); i++) {
332 layout[i].sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
333 layout[i].descriptorType = m_type_counts[i].type;
334 layout[i].count = m_type_counts[i].count;
335 layout[i].stageFlags = XGL_SHADER_STAGE_FLAGS_ALL;
336 layout[i].immutableSampler = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700337
Chia-I Wu11078b02015-01-04 16:27:24 +0800338 if (i < m_type_counts.size() - 1)
339 layout[i].pNext = &layout[i + 1];
340 else
341 layout[i].pNext = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700342 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800343
Chia-I Wu11078b02015-01-04 16:27:24 +0800344 m_layout.init(*m_device, 0, layout[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700345
Chia-I Wu11078b02015-01-04 16:27:24 +0800346 // create XGL_DESCRIPTOR_SET
347 m_set = alloc_sets(XGL_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
348
349 // build the update chain
350 for (int i = 0; i < m_updateBuffers.size(); i++) {
351 m_updateBuffers[i].pBufferViews = &m_bufferInfo[i];
352
353 if (i < m_updateBuffers.size() - 1)
354 m_updateBuffers[i].pNext = &m_updateBuffers[i + 1];
355 else if (m_updateSamplerTextures.empty())
356 m_updateBuffers[i].pNext = NULL;
357 else
358 m_updateBuffers[i].pNext = &m_updateSamplerTextures[0];
359 }
360 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
361 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
362
363 if (i < m_updateSamplerTextures.size() - 1)
364 m_updateSamplerTextures[i].pNext = &m_updateSamplerTextures[i + 1];
365 else
366 m_updateSamplerTextures[i].pNext = NULL;
367 }
368 const void *chain = (!m_updateBuffers.empty()) ? (const void *) &m_updateBuffers[0] :
369 (!m_updateSamplerTextures.empty()) ? (const void *) &m_updateSamplerTextures[0] :
370 NULL;
371
372 // do the updates
373 m_device->begin_descriptor_region_update(XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
374 clear_sets(*m_set);
375 m_set->update(chain);
376 m_device->end_descriptor_region_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700377}
Tony Barboure2c58df2014-11-25 13:18:32 -0700378
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800379XglImage::XglImage(XglDevice *dev)
380{
381 m_device = dev;
382 m_imageInfo.view = XGL_NULL_HANDLE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000383 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800384}
385
Tony Barbour579f7802015-04-03 15:11:43 -0600386static bool IsCompatible(XGL_FLAGS usage, XGL_FLAGS features)
387{
388 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) &&
389 !(features & XGL_FORMAT_IMAGE_SHADER_READ_BIT))
390 return false;
391 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT) &&
392 !(features & XGL_FORMAT_IMAGE_SHADER_WRITE_BIT))
393 return false;
394 return true;
395}
396
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600397void XglImage::init(uint32_t w, uint32_t h,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800398 XGL_FORMAT fmt, XGL_FLAGS usage,
Tony Barbour579f7802015-04-03 15:11:43 -0600399 XGL_IMAGE_TILING requested_tiling)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800400{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600401 uint32_t mipCount;
Tony Barbour579f7802015-04-03 15:11:43 -0600402 XGL_FORMAT_PROPERTIES image_fmt;
403 XGL_IMAGE_TILING tiling;
404 XGL_RESULT err;
405 size_t size;
406
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800407 mipCount = 0;
408
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600409 uint32_t _w = w;
410 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800411 while( ( _w > 0 ) || ( _h > 0 ) )
412 {
413 _w >>= 1;
414 _h >>= 1;
415 mipCount++;
416 }
417
Tony Barbour579f7802015-04-03 15:11:43 -0600418 size = sizeof(image_fmt);
419 err = xglGetFormatInfo(m_device->obj(), fmt,
420 XGL_INFO_TYPE_FORMAT_PROPERTIES,
421 &size, &image_fmt);
422 ASSERT_XGL_SUCCESS(err);
423
424 if (requested_tiling == XGL_LINEAR_TILING) {
425 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
426 tiling = XGL_LINEAR_TILING;
427 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
428 tiling = XGL_OPTIMAL_TILING;
429 } else {
430 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
431 }
432 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
433 tiling = XGL_OPTIMAL_TILING;
434 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
435 tiling = XGL_LINEAR_TILING;
436 } else {
437 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
438 }
439
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800440 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
441 imageCreateInfo.imageType = XGL_IMAGE_2D;
442 imageCreateInfo.format = fmt;
443 imageCreateInfo.extent.width = w;
444 imageCreateInfo.extent.height = h;
445 imageCreateInfo.mipLevels = mipCount;
446 imageCreateInfo.tiling = tiling;
447
448 imageCreateInfo.usage = usage;
449
450 xgl_testing::Image::init(*m_device, imageCreateInfo);
451
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000452 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800453}
454
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600455XGL_RESULT XglImage::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800456{
457 *ptr = map();
458 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
459}
460
461XGL_RESULT XglImage::UnmapMemory()
462{
463 unmap();
464 return XGL_SUCCESS;
465}
466
Tony Barbour5dc515d2015-04-01 17:47:06 -0600467XGL_RESULT XglImage::CopyImage(XglImage &fromImage)
468{
469 XGL_RESULT err;
470
471 XGL_CMD_BUFFER cmd_buf;
472 XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {};
473 cmd_buf_create_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
474 cmd_buf_create_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700475 cmd_buf_create_info.queueNodeIndex = m_device->graphics_queue_node_index_;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600476 cmd_buf_create_info.flags = 0;
477
478 err = xglCreateCommandBuffer(m_device->device(), &cmd_buf_create_info, &cmd_buf);
479 assert(!err);
480
481 /* Copy staging texture to usable texture */
482 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {};
483 cmd_buf_begin_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
484 cmd_buf_begin_info.pNext = NULL;
485 cmd_buf_begin_info.flags = 0;
486
487 err = xglResetCommandBuffer(cmd_buf);
488 assert(!err);
489
490 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_begin_info);
491 assert(!err);
492
493 XGL_IMAGE_COPY copy_region = {};
494 copy_region.srcSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
495 copy_region.srcSubresource.arraySlice = 0;
496 copy_region.srcSubresource.mipLevel = 0;
497 copy_region.srcOffset.x = 0;
498 copy_region.srcOffset.y = 0;
499 copy_region.srcOffset.z = 0;
500 copy_region.destSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
501 copy_region.destSubresource.arraySlice = 0;
502 copy_region.destSubresource.mipLevel = 0;
503 copy_region.destOffset.x = 0;
504 copy_region.destOffset.y = 0;
505 copy_region.destOffset.z = 0;
506 copy_region.extent = fromImage.extent();
507
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -0600508 xglCmdCopyImage(cmd_buf, fromImage.obj(), fromImage.layout(), obj(), layout(), 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600509
510 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {};
511 image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
512 image_memory_barrier.pNext = NULL;
513 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT;
514 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
515 image_memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_GENERAL;
516 image_memory_barrier.newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL;
517 image_memory_barrier.image = fromImage.obj();
518 image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
519 image_memory_barrier.subresourceRange.baseMipLevel = 0;
520 image_memory_barrier.subresourceRange.mipLevels = 1;
521 image_memory_barrier.subresourceRange.baseArraySlice = 0;
522 image_memory_barrier.subresourceRange.arraySize = 0;
523
524 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
525
Courtney Goeltzenleuchter1e8f3be2015-03-24 18:02:34 -0600526 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600527 XGL_PIPELINE_BARRIER pipeline_barrier;
528 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
529 pipeline_barrier.pNext = NULL;
530 pipeline_barrier.eventCount = 1;
531 pipeline_barrier.pEvents = set_events;
532 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
533 pipeline_barrier.memBarrierCount = 1;
534 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
535
536 // write barrier to the command buffer
537 xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier);
538
539 err = xglEndCommandBuffer(cmd_buf);
540 assert(!err);
541
542 const XGL_CMD_BUFFER cmd_bufs[] = { cmd_buf };
543 XGL_MEMORY_REF mem_refs[16];
544 uint32_t num_refs = 0;
545 const std::vector<XGL_GPU_MEMORY> from_mems = fromImage.memories();
546 const std::vector<XGL_GPU_MEMORY> to_mems = memories();
547
548 for (uint32_t j = 0; j < from_mems.size(); j++) {
549 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
550 mem_refs[num_refs].mem = from_mems[j];
551 num_refs++;
552 assert(num_refs < 16);
553 }
554
555 for (uint32_t j = 0; j < to_mems.size(); j++) {
556 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
557 mem_refs[num_refs].mem = to_mems[j];
558 num_refs++;
559 assert(num_refs < 16);
560 }
561
562 err = xglQueueSubmit(m_device->m_queue, 1, cmd_bufs,
563 num_refs, mem_refs, XGL_NULL_HANDLE);
564 assert(!err);
565
566 err = xglQueueWaitIdle(m_device->m_queue);
567 assert(!err);
568
569 xglDestroyObject(cmd_buf);
570
571 return XGL_SUCCESS;
572}
573
Tony Barbourebc093f2015-04-01 16:38:10 -0600574XglTextureObj::XglTextureObj(XglDevice *device, uint32_t *colors)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600575 :XglImage(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700576{
577 m_device = device;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700578 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600579 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600580 void *data;
581 int32_t x, y;
582 XglImage stagingImage(device);
583
584 stagingImage.init(16, 16, tex_format, 0, XGL_LINEAR_TILING);
585 XGL_SUBRESOURCE_LAYOUT layout = stagingImage.subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600586
587 if (colors == NULL)
588 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700589
590 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
591
592 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
593
Tony Barbour5dc515d2015-04-01 17:47:06 -0600594 XGL_IMAGE_VIEW_CREATE_INFO view = {};
Tony Barbourbdf0a312015-04-01 17:10:07 -0600595 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
596 view.pNext = NULL;
597 view.image = XGL_NULL_HANDLE;
598 view.viewType = XGL_IMAGE_VIEW_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600599 view.format = tex_format;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600600 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
601 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
602 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
603 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
604 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
605 view.subresourceRange.baseMipLevel = 0;
606 view.subresourceRange.mipLevels = 1;
607 view.subresourceRange.baseArraySlice = 0;
608 view.subresourceRange.arraySize = 1;
609 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700610
Tony Barboure2c58df2014-11-25 13:18:32 -0700611 /* create image */
Tony Barbour5dc515d2015-04-01 17:47:06 -0600612 init(16, 16, tex_format, XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, XGL_OPTIMAL_TILING);
Tony Barboure2c58df2014-11-25 13:18:32 -0700613
614 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800615 view.image = obj();
616 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600617 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700618
Tony Barbour5dc515d2015-04-01 17:47:06 -0600619 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700620
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800621 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700622 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800623 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600624 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700625 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600626 stagingImage.unmap();
627 XglImage::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700628}
Tony Barbour82c39522014-12-04 14:33:33 -0700629
Tony Barboure2c58df2014-11-25 13:18:32 -0700630XglSamplerObj::XglSamplerObj(XglDevice *device)
631{
Tony Barboure2c58df2014-11-25 13:18:32 -0700632 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700633
Chia-I Wue9864b52014-12-28 16:32:24 +0800634 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
635 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
636 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
637 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
638 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
639 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
640 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
641 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
642 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
643 samplerCreateInfo.mipLodBias = 0.0;
644 samplerCreateInfo.maxAnisotropy = 0.0;
645 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
646 samplerCreateInfo.minLod = 0.0;
647 samplerCreateInfo.maxLod = 0.0;
648 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700649
Chia-I Wue9864b52014-12-28 16:32:24 +0800650 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700651}
Tony Barboure2c58df2014-11-25 13:18:32 -0700652
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700653/*
654 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
655 */
656XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
657{
658 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700659 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700660
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800661 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700662}
663
Tony Barbour044703b2015-03-26 12:37:52 -0600664XglConstantBufferObj::~XglConstantBufferObj()
665{
666 if (m_commandBuffer) {
667 delete m_commandBuffer;
668 }
669}
670
Tony Barboure2c58df2014-11-25 13:18:32 -0700671XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
672{
Tony Barboure2c58df2014-11-25 13:18:32 -0700673 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800674 m_commandBuffer = 0;
675
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800676 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700677 m_numVertices = constantCount;
678 m_stride = constantSize;
679
Chia-I Wua07fee62014-12-28 15:26:08 +0800680 const size_t allocationSize = constantCount * constantSize;
681 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700682
Chia-I Wua07fee62014-12-28 15:26:08 +0800683 void *pData = map();
684 memcpy(pData, data, allocationSize);
685 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700686
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800687 // set up the buffer view for the constant buffer
688 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
689 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
690 view_info.buffer = obj();
Chia-I Wubb0c8d22015-01-16 22:31:25 +0800691 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800692 view_info.offset = 0;
693 view_info.range = allocationSize;
694 m_bufferView.init(*m_device, view_info);
695
696 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
697 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700698}
Tony Barbour82c39522014-12-04 14:33:33 -0700699
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600700void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700701{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800702 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700703}
704
705
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000706void XglConstantBufferObj::BufferMemoryBarrier(
707 XGL_FLAGS outputMask /*=
708 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
709 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
710 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
711 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
712 XGL_MEMORY_OUTPUT_COPY_BIT*/,
713 XGL_FLAGS inputMask /*=
714 XGL_MEMORY_INPUT_CPU_READ_BIT |
715 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
716 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
717 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
718 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
719 XGL_MEMORY_INPUT_SHADER_READ_BIT |
720 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
721 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
722 XGL_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700723{
Tony Barbour38422802014-12-10 14:36:31 -0700724 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700725
Tony Barbour38422802014-12-10 14:36:31 -0700726 if (!m_commandBuffer)
727 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800728 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700729
730 m_commandBuffer = new XglCommandBufferObj(m_device);
731
732 }
733 else
734 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800735 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700736 }
737
Tony Barboure2c58df2014-11-25 13:18:32 -0700738 // open the command buffer
Tony Barbourbdf0a312015-04-01 17:10:07 -0600739 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {};
740 cmd_buf_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
741 cmd_buf_info.pNext = NULL;
742 cmd_buf_info.flags = 0;
743
Jon Ashburnc4164b12014-12-31 17:10:47 -0700744 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700745 ASSERT_XGL_SUCCESS(err);
746
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000747 XGL_BUFFER_MEMORY_BARRIER memory_barrier =
748 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Mark Lobodzinski837ef922015-01-29 14:24:14 -0600749 XGL_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700750
Courtney Goeltzenleuchter1e8f3be2015-03-24 18:02:34 -0600751 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000752 XGL_PIPELINE_BARRIER pipeline_barrier = {};
753 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
754 pipeline_barrier.eventCount = 1;
755 pipeline_barrier.pEvents = set_events;
756 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
757 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -0600758 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000759
760 // write barrier to the command buffer
761 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700762
763 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700764 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700765 ASSERT_XGL_SUCCESS(err);
766
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600767 uint32_t numMemRefs=1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700768 XGL_MEMORY_REF memRefs;
769 // this command buffer only uses the vertex buffer memory
770 memRefs.flags = 0;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800771 memRefs.mem = memories()[0];
Tony Barboure2c58df2014-11-25 13:18:32 -0700772
773 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700774 XGL_CMD_BUFFER bufferArray[1];
775 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800776 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700777 ASSERT_XGL_SUCCESS(err);
778}
779
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700780XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
781 : XglConstantBufferObj(device)
782{
783
784}
785
786void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
787{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700788 XGL_FORMAT viewFormat;
789
790 m_numVertices = numIndexes;
791 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700792 switch (indexType) {
793 case XGL_INDEX_8:
794 m_stride = 1;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700795 viewFormat = XGL_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700796 break;
797 case XGL_INDEX_16:
798 m_stride = 2;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700799 viewFormat = XGL_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700800 break;
801 case XGL_INDEX_32:
802 m_stride = 4;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700803 viewFormat = XGL_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700804 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800805 default:
806 assert(!"unknown index type");
807 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700808 }
809
Chia-I Wua07fee62014-12-28 15:26:08 +0800810 const size_t allocationSize = numIndexes * m_stride;
811 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700812
Chia-I Wua07fee62014-12-28 15:26:08 +0800813 void *pData = map();
814 memcpy(pData, data, allocationSize);
815 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700816
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800817 // set up the buffer view for the constant buffer
818 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
819 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
820 view_info.buffer = obj();
821 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700822 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800823 view_info.offset = 0;
824 view_info.range = allocationSize;
825 m_bufferView.init(*m_device, view_info);
826
827 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
828 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700829}
830
831void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
832{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800833 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700834}
Tony Barboure2c58df2014-11-25 13:18:32 -0700835
Tony Barbouraf1f9192014-12-17 10:57:58 -0700836XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
837{
838 return m_indexType;
839}
840
Chia-I Wu11078b02015-01-04 16:27:24 +0800841XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700842{
Tony Barboure2c58df2014-11-25 13:18:32 -0700843 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
844 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
845 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800846 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700847 stageInfo->shader.linkConstBufferCount = 0;
848 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700849
Tony Barboure2c58df2014-11-25 13:18:32 -0700850 return stageInfo;
851}
852
Tony Barboure2c58df2014-11-25 13:18:32 -0700853XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
854{
855 XGL_RESULT err = XGL_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600856 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700857 XGL_SHADER_CREATE_INFO createInfo;
858 size_t shader_len;
859
860 m_stage = stage;
861 m_device = device;
862
863 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
864 createInfo.pNext = NULL;
865
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600866 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700867
868 shader_len = strlen(shader_code);
869 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
870 createInfo.pCode = malloc(createInfo.codeSize);
871 createInfo.flags = 0;
872
873 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600874 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700875 ((uint32_t *) createInfo.pCode)[1] = 0;
876 ((uint32_t *) createInfo.pCode)[2] = stage;
877 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
878
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800879 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700880 }
881
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600882 if (framework->m_use_spv || err) {
883 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700884 err = XGL_SUCCESS;
885
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600886 // Use Reference GLSL to SPV compiler
887 framework->GLSLtoSPV(stage, shader_code, spv);
888 createInfo.pCode = spv.data();
889 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700890 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700891
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800892 init(*m_device, createInfo);
893 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700894}
Tony Barbour82c39522014-12-04 14:33:33 -0700895
Tony Barboure2c58df2014-11-25 13:18:32 -0700896XglPipelineObj::XglPipelineObj(XglDevice *device)
897{
Tony Barboure2c58df2014-11-25 13:18:32 -0700898 m_device = device;
899 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
900 m_vertexBufferCount = 0;
901
902 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
903 m_ia_state.pNext = XGL_NULL_HANDLE;
904 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
905 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700906 m_ia_state.primitiveRestartEnable = XGL_FALSE;
907 m_ia_state.primitiveRestartIndex = 0;
908
909 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
910 m_rs_state.pNext = &m_ia_state;
911 m_rs_state.depthClipEnable = XGL_FALSE;
912 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Chia-I Wuc8d1ec52015-03-24 11:01:50 +0800913 m_rs_state.programPointSize = XGL_FALSE;
914 m_rs_state.pointOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700915 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
916 m_rs_state.fillMode = XGL_FILL_SOLID;
917 m_rs_state.cullMode = XGL_CULL_NONE;
918 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700919
Tony Barboure2c58df2014-11-25 13:18:32 -0700920 memset(&m_cb_state,0,sizeof(m_cb_state));
921 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
922 m_cb_state.pNext = &m_rs_state;
923 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700924 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
925
Tony Barbourf52346d2015-01-16 14:27:35 -0700926 m_ms_state.pNext = &m_cb_state;
927 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
928 m_ms_state.multisampleEnable = XGL_FALSE;
929 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
930 m_ms_state.samples = 1;
931 m_ms_state.minSampleShading = 0;
932 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700933
Tony Barbourf52346d2015-01-16 14:27:35 -0700934 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
935 m_ds_state.pNext = &m_ms_state,
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700936 m_ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700937 m_ds_state.depthTestEnable = XGL_FALSE;
938 m_ds_state.depthWriteEnable = XGL_FALSE;
939 m_ds_state.depthBoundsEnable = XGL_FALSE;
940 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
941 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
942 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
943 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
944 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
945 m_ds_state.stencilTestEnable = XGL_FALSE;
946 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -0700947
Tony Barbourf52346d2015-01-16 14:27:35 -0700948 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
949 att.blendEnable = XGL_FALSE;
Tony Barboura53a6942015-02-25 11:25:11 -0700950 att.format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -0700951 att.channelWriteMask = 0xf;
952 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -0700953
954};
955
956void XglPipelineObj::AddShader(XglShaderObj* shader)
957{
958 m_shaderObjs.push_back(shader);
959}
960
961void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
962{
963 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
964 m_vi_state.attributeCount = count;
965}
966
967void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
968{
969 m_vi_state.pVertexBindingDescriptions = vi_binding;
970 m_vi_state.bindingCount = count;
971}
972
973void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
974{
975 m_vertexBufferObjs.push_back(vertexDataBuffer);
976 m_vertexBufferBindings.push_back(binding);
977 m_vertexBufferCount++;
978}
979
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600980void XglPipelineObj::AddColorAttachment(uint32_t binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +0800981{
Tony Barbourf52346d2015-01-16 14:27:35 -0700982 if (binding+1 > m_colorAttachments.size())
983 {
984 m_colorAttachments.resize(binding+1);
985 }
986 m_colorAttachments[binding] = *att;
987}
988
989void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
990{
991 m_ds_state.format = ds_state->format;
992 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
993 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
994 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
995 m_ds_state.depthFunc = ds_state->depthFunc;
996 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
997 m_ds_state.back = ds_state->back;
998 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +0800999}
1000
Tony Barbour976e1cf2014-12-17 11:57:31 -07001001void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
1002{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001003 void* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001004 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1005
1006 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
1007
1008 for (int i=0; i<m_shaderObjs.size(); i++)
1009 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001010 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001011 shaderCreateInfo->pNext = head_ptr;
1012 head_ptr = shaderCreateInfo;
1013 }
1014
1015 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1016 {
1017 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1018 m_vi_state.pNext = head_ptr;
1019 head_ptr = &m_vi_state;
1020 }
1021
1022 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1023 info.pNext = head_ptr;
1024 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +08001025 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001026
Tony Barbourf52346d2015-01-16 14:27:35 -07001027 m_cb_state.attachmentCount = m_colorAttachments.size();
1028 m_cb_state.pAttachments = &m_colorAttachments[0];
1029
Chia-I Wu2648d092014-12-29 14:24:14 +08001030 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001031}
Chia-I Wu2648d092014-12-29 14:24:14 +08001032
Tony Barbour976e1cf2014-12-17 11:57:31 -07001033XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
1034{
Chia-I Wu2648d092014-12-29 14:24:14 +08001035 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001036}
1037
Tony Barbour5420af02014-12-03 13:58:15 -07001038void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -07001039{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001040 void* head_ptr = &m_ds_state;
Tony Barboure2c58df2014-11-25 13:18:32 -07001041 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1042
1043 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -07001044
1045 for (int i=0; i<m_shaderObjs.size(); i++)
1046 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001047 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barboure2c58df2014-11-25 13:18:32 -07001048 shaderCreateInfo->pNext = head_ptr;
1049 head_ptr = shaderCreateInfo;
1050 }
1051
1052 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1053 {
1054 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1055 m_vi_state.pNext = head_ptr;
1056 head_ptr = &m_vi_state;
1057 }
1058
1059 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1060 info.pNext = head_ptr;
1061 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +08001062 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barboure2c58df2014-11-25 13:18:32 -07001063
Chia-I Wu2648d092014-12-29 14:24:14 +08001064 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -07001065
Chia-I Wu2648d092014-12-29 14:24:14 +08001066 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -07001067
1068
1069 for (int i=0; i < m_vertexBufferCount; i++)
1070 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001071 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001072 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001073}
Tony Barbour82c39522014-12-04 14:33:33 -07001074
Tony Barboure2c58df2014-11-25 13:18:32 -07001075XglMemoryRefManager::XglMemoryRefManager() {
1076
1077}
Tony Barbour82c39522014-12-04 14:33:33 -07001078
Tony Barboure2c58df2014-11-25 13:18:32 -07001079void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001080 const std::vector<XGL_GPU_MEMORY> mems = constantBuffer->memories();
1081 if (!mems.empty())
1082 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001083}
Tony Barbour82c39522014-12-04 14:33:33 -07001084
Tony Barboure2c58df2014-11-25 13:18:32 -07001085void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
Chia-I Wu13a3aa82014-12-28 15:55:09 +08001086 const std::vector<XGL_GPU_MEMORY> mems = texture->memories();
1087 if (!mems.empty())
1088 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001089}
Tony Barbour82c39522014-12-04 14:33:33 -07001090
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001091void XglMemoryRefManager::AddMemoryRef(XGL_GPU_MEMORY *mem, uint32_t refCount) {
1092 for (size_t i = 0; i < refCount; i++) {
1093 m_bufferObjs.push_back(mem[i]);
1094 }
1095}
1096
1097void XglMemoryRefManager::AddRTMemoryRefs(vector<XglImage*>images, uint32_t rtCount) {
1098 for (uint32_t i = 0; i < rtCount; i++) {
1099 const std::vector<XGL_GPU_MEMORY> mems = images[i]->memories();
1100 if (!mems.empty())
1101 m_bufferObjs.push_back(mems[0]);
1102 }
1103}
1104
Tony Barboure2c58df2014-11-25 13:18:32 -07001105XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
1106
1107 XGL_MEMORY_REF *localRefs;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001108 uint32_t numRefs=m_bufferObjs.size();
Tony Barboure2c58df2014-11-25 13:18:32 -07001109
1110 if (numRefs <= 0)
1111 return NULL;
1112
1113 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
1114 for (int i=0; i<numRefs; i++)
1115 {
1116 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +08001117 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -07001118 }
1119 return localRefs;
1120}
1121int XglMemoryRefManager::GetNumRefs() {
1122 return m_bufferObjs.size();
1123}
Tony Barbour6d047bf2014-12-10 14:34:45 -07001124
1125XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -07001126 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001127{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001128 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001129}
Tony Barbour471338d2014-12-10 17:28:39 -07001130
Tony Barbour6d047bf2014-12-10 14:34:45 -07001131XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1132{
Chia-I Wud28343c2014-12-28 15:12:48 +08001133 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001134}
Tony Barbour471338d2014-12-10 17:28:39 -07001135
Jon Ashburnc4164b12014-12-31 17:10:47 -07001136XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001137{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001138 begin(pInfo);
1139 return XGL_SUCCESS;
1140}
1141
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001142XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj, XGL_FRAMEBUFFER framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001143{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001144 begin(renderpass_obj, framebuffer_obj);
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001145 return XGL_SUCCESS;
1146}
1147
1148XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
1149{
1150 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +08001151 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001152}
1153
1154XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1155{
Chia-I Wud28343c2014-12-28 15:12:48 +08001156 end();
1157 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001158}
1159
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001160void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001161{
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001162 xglCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001163}
1164
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001165void XglCommandBufferObj::ClearAllBuffers(XGL_CLEAR_COLOR clear_color, float depth_clear_color, uint32_t stencil_clear_color,
1166 XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001167{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001168 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001169 const XGL_FLAGS output_mask =
1170 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1171 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1172 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1173 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1174 XGL_MEMORY_OUTPUT_COPY_BIT;
1175 const XGL_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001176
1177 // whatever we want to do, we do it to the whole buffer
1178 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1179 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1180 srRange.baseMipLevel = 0;
1181 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1182 srRange.baseArraySlice = 0;
1183 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1184
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001185 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1186 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1187 memory_barrier.outputMask = output_mask;
1188 memory_barrier.inputMask = input_mask;
1189 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1190 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001191 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001192
Courtney Goeltzenleuchter1e8f3be2015-03-24 18:02:34 -06001193 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001194 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1195 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1196 pipeline_barrier.eventCount = 1;
1197 pipeline_barrier.pEvents = set_events;
1198 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1199 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001200 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001201
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001202 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001203 memory_barrier.image = m_renderTargets[i]->image();
1204 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1205 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1206 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001207
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -06001208 xglCmdClearColorImage(obj(),
1209 m_renderTargets[i]->image(), XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
1210 clear_color, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001211 }
1212
1213 if (depthStencilImage)
1214 {
1215 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1216 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1217 dsRange.baseMipLevel = 0;
1218 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1219 dsRange.baseArraySlice = 0;
1220 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1221
1222 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001223
1224 memory_barrier.oldLayout = depthStencilBinding->layout;
1225 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1226 memory_barrier.image = depthStencilImage;
1227 memory_barrier.subresourceRange = dsRange;
1228
1229 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1230 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001231
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -06001232 xglCmdClearDepthStencil(obj(),
1233 depthStencilImage, XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001234 depth_clear_color, stencil_clear_color,
1235 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001236
1237 // prepare depth buffer for rendering
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001238 memory_barrier.image = depthStencilImage;
1239 memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1240 memory_barrier.newLayout = depthStencilBinding->layout;
1241 memory_barrier.subresourceRange = dsRange;
1242 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1243 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001244 }
1245}
1246
Jon Ashburncdc40be2015-01-02 18:27:14 -07001247void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001248{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001249 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001250 const XGL_FLAGS output_mask =
1251 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1252 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1253 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1254 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1255 XGL_MEMORY_OUTPUT_COPY_BIT;
1256 const XGL_FLAGS input_mask =
1257 XGL_MEMORY_INPUT_CPU_READ_BIT |
1258 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1259 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
1260 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1261 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
1262 XGL_MEMORY_INPUT_SHADER_READ_BIT |
1263 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1264 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1265 XGL_MEMORY_INPUT_COPY_BIT;
1266
Tony Barbour30cc9e82014-12-17 11:53:55 -07001267 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1268 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1269 srRange.baseMipLevel = 0;
1270 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1271 srRange.baseArraySlice = 0;
1272 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1273
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001274 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1275 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1276 memory_barrier.outputMask = output_mask;
1277 memory_barrier.inputMask = input_mask;
1278 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1279 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001280 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001281
Courtney Goeltzenleuchter1e8f3be2015-03-24 18:02:34 -06001282 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001283 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1284 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1285 pipeline_barrier.eventCount = 1;
1286 pipeline_barrier.pEvents = set_events;
1287 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1288 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001289 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001290
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001291 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001292 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001293 memory_barrier.image = m_renderTargets[i]->image();
1294 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1295 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1296 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001297 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001298}
1299
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001300void XglCommandBufferObj::BeginRenderPass(XGL_RENDER_PASS renderpass, XGL_FRAMEBUFFER framebuffer)
1301{
1302 XGL_RENDER_PASS_BEGIN rp_begin = {
1303 renderpass,
1304 framebuffer,
1305 };
1306
1307 xglCmdBeginRenderPass( obj(), &rp_begin);
1308}
1309
1310void XglCommandBufferObj::EndRenderPass(XGL_RENDER_PASS renderpass)
1311{
1312 xglCmdEndRenderPass( obj(), renderpass);
1313}
1314
Tony Barbourf52346d2015-01-16 14:27:35 -07001315void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001316{
Tony Barbourf52346d2015-01-16 14:27:35 -07001317 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001318}
1319
1320void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1321{
1322 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001323}
1324
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001325void XglCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001326{
Chia-I Wud28343c2014-12-28 15:12:48 +08001327 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001328}
1329
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001330void XglCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001331{
Chia-I Wud28343c2014-12-28 15:12:48 +08001332 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001333}
1334
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001335void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, uint32_t numMemRefs)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001336{
1337 XGL_RESULT err = XGL_SUCCESS;
1338
1339 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001340 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001341 ASSERT_XGL_SUCCESS( err );
1342
1343 err = xglQueueWaitIdle( m_device->m_queue );
1344 ASSERT_XGL_SUCCESS( err );
1345
1346 // Wait for work to finish before cleaning up.
1347 xglDeviceWaitIdle(m_device->device());
1348
1349}
1350void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1351{
Chia-I Wud28343c2014-12-28 15:12:48 +08001352 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001353}
1354
1355void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1356{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001357 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wu11078b02015-01-04 16:27:24 +08001358 xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001359}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001360void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001361{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001362 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001363}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001364void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001365{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001366 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001367}