blob: 3721816f3517f03f040b98de1dbf13c81d5978f6 [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
508 xglCmdCopyImage(cmd_buf, fromImage.obj(), obj(), 1, &copy_region);
509
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
526 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
527 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 Barboure2c58df2014-11-25 13:18:32 -0700664XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
665{
Tony Barboure2c58df2014-11-25 13:18:32 -0700666 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800667 m_commandBuffer = 0;
668
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800669 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700670 m_numVertices = constantCount;
671 m_stride = constantSize;
672
Chia-I Wua07fee62014-12-28 15:26:08 +0800673 const size_t allocationSize = constantCount * constantSize;
674 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700675
Chia-I Wua07fee62014-12-28 15:26:08 +0800676 void *pData = map();
677 memcpy(pData, data, allocationSize);
678 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700679
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800680 // set up the buffer view for the constant buffer
681 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
682 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
683 view_info.buffer = obj();
Chia-I Wubb0c8d22015-01-16 22:31:25 +0800684 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800685 view_info.offset = 0;
686 view_info.range = allocationSize;
687 m_bufferView.init(*m_device, view_info);
688
689 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
690 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700691}
Tony Barbour82c39522014-12-04 14:33:33 -0700692
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600693void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700694{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800695 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700696}
697
698
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000699void XglConstantBufferObj::BufferMemoryBarrier(
700 XGL_FLAGS outputMask /*=
701 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
702 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
703 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
704 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
705 XGL_MEMORY_OUTPUT_COPY_BIT*/,
706 XGL_FLAGS inputMask /*=
707 XGL_MEMORY_INPUT_CPU_READ_BIT |
708 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
709 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
710 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
711 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
712 XGL_MEMORY_INPUT_SHADER_READ_BIT |
713 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
714 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
715 XGL_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700716{
Tony Barbour38422802014-12-10 14:36:31 -0700717 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700718
Tony Barbour38422802014-12-10 14:36:31 -0700719 if (!m_commandBuffer)
720 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800721 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700722
723 m_commandBuffer = new XglCommandBufferObj(m_device);
724
725 }
726 else
727 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800728 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700729 }
730
Tony Barboure2c58df2014-11-25 13:18:32 -0700731 // open the command buffer
Tony Barbourbdf0a312015-04-01 17:10:07 -0600732 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {};
733 cmd_buf_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
734 cmd_buf_info.pNext = NULL;
735 cmd_buf_info.flags = 0;
736
Jon Ashburnc4164b12014-12-31 17:10:47 -0700737 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700738 ASSERT_XGL_SUCCESS(err);
739
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000740 XGL_BUFFER_MEMORY_BARRIER memory_barrier =
741 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Mark Lobodzinski837ef922015-01-29 14:24:14 -0600742 XGL_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700743
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000744 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
745 XGL_PIPELINE_BARRIER pipeline_barrier = {};
746 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
747 pipeline_barrier.eventCount = 1;
748 pipeline_barrier.pEvents = set_events;
749 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
750 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -0600751 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000752
753 // write barrier to the command buffer
754 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700755
756 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700757 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700758 ASSERT_XGL_SUCCESS(err);
759
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600760 uint32_t numMemRefs=1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700761 XGL_MEMORY_REF memRefs;
762 // this command buffer only uses the vertex buffer memory
763 memRefs.flags = 0;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800764 memRefs.mem = memories()[0];
Tony Barboure2c58df2014-11-25 13:18:32 -0700765
766 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700767 XGL_CMD_BUFFER bufferArray[1];
768 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800769 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700770 ASSERT_XGL_SUCCESS(err);
771}
772
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700773XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
774 : XglConstantBufferObj(device)
775{
776
777}
778
779void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
780{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700781 XGL_FORMAT viewFormat;
782
783 m_numVertices = numIndexes;
784 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700785 switch (indexType) {
786 case XGL_INDEX_8:
787 m_stride = 1;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700788 viewFormat = XGL_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700789 break;
790 case XGL_INDEX_16:
791 m_stride = 2;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700792 viewFormat = XGL_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700793 break;
794 case XGL_INDEX_32:
795 m_stride = 4;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700796 viewFormat = XGL_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700797 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800798 default:
799 assert(!"unknown index type");
800 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700801 }
802
Chia-I Wua07fee62014-12-28 15:26:08 +0800803 const size_t allocationSize = numIndexes * m_stride;
804 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700805
Chia-I Wua07fee62014-12-28 15:26:08 +0800806 void *pData = map();
807 memcpy(pData, data, allocationSize);
808 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700809
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800810 // set up the buffer view for the constant buffer
811 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
812 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
813 view_info.buffer = obj();
814 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
815 view_info.stride = m_stride;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700816 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800817 view_info.offset = 0;
818 view_info.range = allocationSize;
819 m_bufferView.init(*m_device, view_info);
820
821 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
822 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700823}
824
825void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
826{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800827 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700828}
Tony Barboure2c58df2014-11-25 13:18:32 -0700829
Tony Barbouraf1f9192014-12-17 10:57:58 -0700830XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
831{
832 return m_indexType;
833}
834
Chia-I Wu11078b02015-01-04 16:27:24 +0800835XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700836{
Tony Barboure2c58df2014-11-25 13:18:32 -0700837 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
838 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
839 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800840 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700841 stageInfo->shader.linkConstBufferCount = 0;
842 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700843
Tony Barboure2c58df2014-11-25 13:18:32 -0700844 return stageInfo;
845}
846
Tony Barboure2c58df2014-11-25 13:18:32 -0700847XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
848{
849 XGL_RESULT err = XGL_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600850 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700851 XGL_SHADER_CREATE_INFO createInfo;
852 size_t shader_len;
853
854 m_stage = stage;
855 m_device = device;
856
857 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
858 createInfo.pNext = NULL;
859
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600860 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700861
862 shader_len = strlen(shader_code);
863 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
864 createInfo.pCode = malloc(createInfo.codeSize);
865 createInfo.flags = 0;
866
867 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600868 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700869 ((uint32_t *) createInfo.pCode)[1] = 0;
870 ((uint32_t *) createInfo.pCode)[2] = stage;
871 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
872
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800873 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700874 }
875
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600876 if (framework->m_use_spv || err) {
877 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700878 err = XGL_SUCCESS;
879
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600880 // Use Reference GLSL to SPV compiler
881 framework->GLSLtoSPV(stage, shader_code, spv);
882 createInfo.pCode = spv.data();
883 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700884 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700885
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800886 init(*m_device, createInfo);
887 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700888}
Tony Barbour82c39522014-12-04 14:33:33 -0700889
Tony Barboure2c58df2014-11-25 13:18:32 -0700890XglPipelineObj::XglPipelineObj(XglDevice *device)
891{
Tony Barboure2c58df2014-11-25 13:18:32 -0700892 m_device = device;
893 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
894 m_vertexBufferCount = 0;
895
896 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
897 m_ia_state.pNext = XGL_NULL_HANDLE;
898 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
899 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700900 m_ia_state.primitiveRestartEnable = XGL_FALSE;
901 m_ia_state.primitiveRestartIndex = 0;
902
903 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
904 m_rs_state.pNext = &m_ia_state;
905 m_rs_state.depthClipEnable = XGL_FALSE;
906 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Chia-I Wuc8d1ec52015-03-24 11:01:50 +0800907 m_rs_state.programPointSize = XGL_FALSE;
908 m_rs_state.pointOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700909 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
910 m_rs_state.fillMode = XGL_FILL_SOLID;
911 m_rs_state.cullMode = XGL_CULL_NONE;
912 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700913
Tony Barboure2c58df2014-11-25 13:18:32 -0700914 memset(&m_cb_state,0,sizeof(m_cb_state));
915 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
916 m_cb_state.pNext = &m_rs_state;
917 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700918 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
919
Tony Barbourf52346d2015-01-16 14:27:35 -0700920 m_ms_state.pNext = &m_cb_state;
921 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
922 m_ms_state.multisampleEnable = XGL_FALSE;
923 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
924 m_ms_state.samples = 1;
925 m_ms_state.minSampleShading = 0;
926 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700927
Tony Barbourf52346d2015-01-16 14:27:35 -0700928 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
929 m_ds_state.pNext = &m_ms_state,
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700930 m_ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700931 m_ds_state.depthTestEnable = XGL_FALSE;
932 m_ds_state.depthWriteEnable = XGL_FALSE;
933 m_ds_state.depthBoundsEnable = XGL_FALSE;
934 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
935 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
936 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
937 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
938 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
939 m_ds_state.stencilTestEnable = XGL_FALSE;
940 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -0700941
Tony Barbourf52346d2015-01-16 14:27:35 -0700942 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
943 att.blendEnable = XGL_FALSE;
Tony Barboura53a6942015-02-25 11:25:11 -0700944 att.format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -0700945 att.channelWriteMask = 0xf;
946 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -0700947
948};
949
950void XglPipelineObj::AddShader(XglShaderObj* shader)
951{
952 m_shaderObjs.push_back(shader);
953}
954
955void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
956{
957 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
958 m_vi_state.attributeCount = count;
959}
960
961void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
962{
963 m_vi_state.pVertexBindingDescriptions = vi_binding;
964 m_vi_state.bindingCount = count;
965}
966
967void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
968{
969 m_vertexBufferObjs.push_back(vertexDataBuffer);
970 m_vertexBufferBindings.push_back(binding);
971 m_vertexBufferCount++;
972}
973
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600974void XglPipelineObj::AddColorAttachment(uint32_t binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +0800975{
Tony Barbourf52346d2015-01-16 14:27:35 -0700976 if (binding+1 > m_colorAttachments.size())
977 {
978 m_colorAttachments.resize(binding+1);
979 }
980 m_colorAttachments[binding] = *att;
981}
982
983void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
984{
985 m_ds_state.format = ds_state->format;
986 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
987 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
988 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
989 m_ds_state.depthFunc = ds_state->depthFunc;
990 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
991 m_ds_state.back = ds_state->back;
992 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +0800993}
994
Tony Barbour976e1cf2014-12-17 11:57:31 -0700995void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
996{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600997 void* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -0700998 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
999
1000 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
1001
1002 for (int i=0; i<m_shaderObjs.size(); i++)
1003 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001004 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001005 shaderCreateInfo->pNext = head_ptr;
1006 head_ptr = shaderCreateInfo;
1007 }
1008
1009 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1010 {
1011 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1012 m_vi_state.pNext = head_ptr;
1013 head_ptr = &m_vi_state;
1014 }
1015
1016 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1017 info.pNext = head_ptr;
1018 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +08001019 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001020
Tony Barbourf52346d2015-01-16 14:27:35 -07001021 m_cb_state.attachmentCount = m_colorAttachments.size();
1022 m_cb_state.pAttachments = &m_colorAttachments[0];
1023
Chia-I Wu2648d092014-12-29 14:24:14 +08001024 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001025}
Chia-I Wu2648d092014-12-29 14:24:14 +08001026
Tony Barbour976e1cf2014-12-17 11:57:31 -07001027XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
1028{
Chia-I Wu2648d092014-12-29 14:24:14 +08001029 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001030}
1031
Tony Barbour5420af02014-12-03 13:58:15 -07001032void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -07001033{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001034 void* head_ptr = &m_ds_state;
Tony Barboure2c58df2014-11-25 13:18:32 -07001035 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1036
1037 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -07001038
1039 for (int i=0; i<m_shaderObjs.size(); i++)
1040 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001041 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barboure2c58df2014-11-25 13:18:32 -07001042 shaderCreateInfo->pNext = head_ptr;
1043 head_ptr = shaderCreateInfo;
1044 }
1045
1046 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1047 {
1048 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1049 m_vi_state.pNext = head_ptr;
1050 head_ptr = &m_vi_state;
1051 }
1052
1053 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1054 info.pNext = head_ptr;
1055 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +08001056 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barboure2c58df2014-11-25 13:18:32 -07001057
Chia-I Wu2648d092014-12-29 14:24:14 +08001058 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -07001059
Chia-I Wu2648d092014-12-29 14:24:14 +08001060 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -07001061
1062
1063 for (int i=0; i < m_vertexBufferCount; i++)
1064 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001065 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001066 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001067}
Tony Barbour82c39522014-12-04 14:33:33 -07001068
Tony Barboure2c58df2014-11-25 13:18:32 -07001069XglMemoryRefManager::XglMemoryRefManager() {
1070
1071}
Tony Barbour82c39522014-12-04 14:33:33 -07001072
Tony Barboure2c58df2014-11-25 13:18:32 -07001073void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001074 const std::vector<XGL_GPU_MEMORY> mems = constantBuffer->memories();
1075 if (!mems.empty())
1076 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001077}
Tony Barbour82c39522014-12-04 14:33:33 -07001078
Tony Barboure2c58df2014-11-25 13:18:32 -07001079void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
Chia-I Wu13a3aa82014-12-28 15:55:09 +08001080 const std::vector<XGL_GPU_MEMORY> mems = texture->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
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001085void XglMemoryRefManager::AddMemoryRef(XGL_GPU_MEMORY *mem, uint32_t refCount) {
1086 for (size_t i = 0; i < refCount; i++) {
1087 m_bufferObjs.push_back(mem[i]);
1088 }
1089}
1090
1091void XglMemoryRefManager::AddRTMemoryRefs(vector<XglImage*>images, uint32_t rtCount) {
1092 for (uint32_t i = 0; i < rtCount; i++) {
1093 const std::vector<XGL_GPU_MEMORY> mems = images[i]->memories();
1094 if (!mems.empty())
1095 m_bufferObjs.push_back(mems[0]);
1096 }
1097}
1098
Tony Barboure2c58df2014-11-25 13:18:32 -07001099XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
1100
1101 XGL_MEMORY_REF *localRefs;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001102 uint32_t numRefs=m_bufferObjs.size();
Tony Barboure2c58df2014-11-25 13:18:32 -07001103
1104 if (numRefs <= 0)
1105 return NULL;
1106
1107 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
1108 for (int i=0; i<numRefs; i++)
1109 {
1110 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +08001111 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -07001112 }
1113 return localRefs;
1114}
1115int XglMemoryRefManager::GetNumRefs() {
1116 return m_bufferObjs.size();
1117}
Tony Barbour6d047bf2014-12-10 14:34:45 -07001118
1119XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -07001120 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001121{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001122 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001123}
Tony Barbour471338d2014-12-10 17:28:39 -07001124
Tony Barbour6d047bf2014-12-10 14:34:45 -07001125XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1126{
Chia-I Wud28343c2014-12-28 15:12:48 +08001127 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001128}
Tony Barbour471338d2014-12-10 17:28:39 -07001129
Jon Ashburnc4164b12014-12-31 17:10:47 -07001130XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001131{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001132 begin(pInfo);
1133 return XGL_SUCCESS;
1134}
1135
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001136XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj, XGL_FRAMEBUFFER framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001137{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001138 begin(renderpass_obj, framebuffer_obj);
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001139 return XGL_SUCCESS;
1140}
1141
1142XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
1143{
1144 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +08001145 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001146}
1147
1148XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1149{
Chia-I Wud28343c2014-12-28 15:12:48 +08001150 end();
1151 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001152}
1153
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001154void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001155{
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001156 xglCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001157}
1158
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001159void XglCommandBufferObj::ClearAllBuffers(XGL_CLEAR_COLOR clear_color, float depth_clear_color, uint32_t stencil_clear_color,
1160 XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001161{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001162 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001163 const XGL_FLAGS output_mask =
1164 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1165 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1166 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1167 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1168 XGL_MEMORY_OUTPUT_COPY_BIT;
1169 const XGL_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001170
1171 // whatever we want to do, we do it to the whole buffer
1172 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1173 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1174 srRange.baseMipLevel = 0;
1175 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1176 srRange.baseArraySlice = 0;
1177 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1178
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001179 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1180 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1181 memory_barrier.outputMask = output_mask;
1182 memory_barrier.inputMask = input_mask;
1183 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1184 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001185 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001186
1187 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1188 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1189 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1190 pipeline_barrier.eventCount = 1;
1191 pipeline_barrier.pEvents = set_events;
1192 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1193 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001194 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001195
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001196 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001197 memory_barrier.image = m_renderTargets[i]->image();
1198 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1199 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1200 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001201
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001202 xglCmdClearColorImage( obj(), m_renderTargets[i]->image(), clear_color, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001203 }
1204
1205 if (depthStencilImage)
1206 {
1207 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1208 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1209 dsRange.baseMipLevel = 0;
1210 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1211 dsRange.baseArraySlice = 0;
1212 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1213
1214 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001215
1216 memory_barrier.oldLayout = depthStencilBinding->layout;
1217 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1218 memory_barrier.image = depthStencilImage;
1219 memory_barrier.subresourceRange = dsRange;
1220
1221 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1222 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001223
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001224 xglCmdClearDepthStencil(obj(), depthStencilImage,
1225 depth_clear_color, stencil_clear_color,
1226 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001227
1228 // prepare depth buffer for rendering
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001229 memory_barrier.image = depthStencilImage;
1230 memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1231 memory_barrier.newLayout = depthStencilBinding->layout;
1232 memory_barrier.subresourceRange = dsRange;
1233 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1234 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001235 }
1236}
1237
Jon Ashburncdc40be2015-01-02 18:27:14 -07001238void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001239{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001240 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001241 const XGL_FLAGS output_mask =
1242 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1243 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1244 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1245 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1246 XGL_MEMORY_OUTPUT_COPY_BIT;
1247 const XGL_FLAGS input_mask =
1248 XGL_MEMORY_INPUT_CPU_READ_BIT |
1249 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1250 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
1251 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1252 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
1253 XGL_MEMORY_INPUT_SHADER_READ_BIT |
1254 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1255 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1256 XGL_MEMORY_INPUT_COPY_BIT;
1257
Tony Barbour30cc9e82014-12-17 11:53:55 -07001258 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1259 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1260 srRange.baseMipLevel = 0;
1261 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1262 srRange.baseArraySlice = 0;
1263 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1264
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001265 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1266 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1267 memory_barrier.outputMask = output_mask;
1268 memory_barrier.inputMask = input_mask;
1269 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1270 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001271 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001272
1273 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1274 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1275 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1276 pipeline_barrier.eventCount = 1;
1277 pipeline_barrier.pEvents = set_events;
1278 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1279 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001280 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001281
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001282 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001283 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001284 memory_barrier.image = m_renderTargets[i]->image();
1285 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1286 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1287 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001288 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001289}
1290
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001291void XglCommandBufferObj::BeginRenderPass(XGL_RENDER_PASS renderpass, XGL_FRAMEBUFFER framebuffer)
1292{
1293 XGL_RENDER_PASS_BEGIN rp_begin = {
1294 renderpass,
1295 framebuffer,
1296 };
1297
1298 xglCmdBeginRenderPass( obj(), &rp_begin);
1299}
1300
1301void XglCommandBufferObj::EndRenderPass(XGL_RENDER_PASS renderpass)
1302{
1303 xglCmdEndRenderPass( obj(), renderpass);
1304}
1305
Tony Barbourf52346d2015-01-16 14:27:35 -07001306void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001307{
Tony Barbourf52346d2015-01-16 14:27:35 -07001308 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001309}
1310
1311void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1312{
1313 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001314}
1315
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001316void XglCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001317{
Chia-I Wud28343c2014-12-28 15:12:48 +08001318 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001319}
1320
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001321void XglCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001322{
Chia-I Wud28343c2014-12-28 15:12:48 +08001323 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001324}
1325
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001326void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, uint32_t numMemRefs)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001327{
1328 XGL_RESULT err = XGL_SUCCESS;
1329
1330 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001331 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001332 ASSERT_XGL_SUCCESS( err );
1333
1334 err = xglQueueWaitIdle( m_device->m_queue );
1335 ASSERT_XGL_SUCCESS( err );
1336
1337 // Wait for work to finish before cleaning up.
1338 xglDeviceWaitIdle(m_device->device());
1339
1340}
1341void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1342{
Chia-I Wud28343c2014-12-28 15:12:48 +08001343 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001344}
1345
1346void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1347{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001348 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wu11078b02015-01-04 16:27:24 +08001349 xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001350}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001351void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001352{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001353 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001354}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001355void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001356{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001357 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001358}