blob: c11d350c12cd84a0edb56a2f64c177e5f5dd6d3b [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
2 * XGL Tests
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
28#include "xglrenderframework.h"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060030XglRenderFramework::XglRenderFramework() :
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060031 m_cmdBuffer( XGL_NULL_HANDLE ),
Chia-I Wu837f9952014-12-15 23:29:34 +080032 m_stateRaster( XGL_NULL_HANDLE ),
33 m_colorBlend( XGL_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060034 m_stateViewport( XGL_NULL_HANDLE ),
Chia-I Wu837f9952014-12-15 23:29:34 +080035 m_stateDepthStencil( XGL_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060036 m_width( 256.0 ), // default window width
37 m_height( 256.0 ) // default window height
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060038{
Chia-I Wuecebf752014-12-05 10:45:15 +080039 m_renderTargetCount = 1;
40
Jeremy Hayesa058eee2015-01-23 08:51:43 -070041 m_render_target_fmt = XGL_FMT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060042
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060043 m_depthStencilBinding.view = XGL_NULL_HANDLE;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060044}
45
46XglRenderFramework::~XglRenderFramework()
47{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060048
49}
50
51void XglRenderFramework::InitFramework()
52{
53 XGL_RESULT err;
54
Jon Ashburn1e464892015-01-29 15:48:00 -070055 err = xglCreateInstance(&app_info, NULL, &this->inst);
56 ASSERT_XGL_SUCCESS(err);
57 err = xglEnumerateGpus(inst, XGL_MAX_PHYSICAL_GPUS, &this->gpu_count,
58 objs);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060059 ASSERT_XGL_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070060 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060061
62 m_device = new XglDevice(0, objs[0]);
63 m_device->get_device_queue();
64}
65
66void XglRenderFramework::ShutdownFramework()
67{
68 if (m_colorBlend) xglDestroyObject(m_colorBlend);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060069 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
70 if (m_stateRaster) xglDestroyObject(m_stateRaster);
71 if (m_cmdBuffer) xglDestroyObject(m_cmdBuffer);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060072
73 if (m_stateViewport) {
74 xglDestroyObject(m_stateViewport);
75 }
76
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060077 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +080078 delete m_device;
Jon Ashburn1e464892015-01-29 15:48:00 -070079 xglDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060080}
81
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060082void XglRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060083{
84 XGL_RESULT err;
85
Jeremy Hayesa058eee2015-01-23 08:51:43 -070086 m_render_target_fmt = XGL_FMT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060087
88 // create a raster state (solid, back-face culling)
Tony Barbourf52346d2015-01-16 14:27:35 -070089 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster = {};
90 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
91 raster.pointSize = 1.0;
92
93 err = xglCreateDynamicRasterState( device(), &raster, &m_stateRaster );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060094 ASSERT_XGL_SUCCESS(err);
95
Tony Barbourf52346d2015-01-16 14:27:35 -070096 XGL_DYNAMIC_CB_STATE_CREATE_INFO blend = {};
97 blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
98 err = xglCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060099 ASSERT_XGL_SUCCESS( err );
100
Tony Barbourf52346d2015-01-16 14:27:35 -0700101 XGL_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {};
102 depthStencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600103 depthStencil.minDepth = 0.f;
104 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700105 depthStencil.stencilFrontRef = 0;
106 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600107
Tony Barbourf52346d2015-01-16 14:27:35 -0700108 err = xglCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600109 ASSERT_XGL_SUCCESS( err );
110
111 XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {};
112
113 cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
114 cmdInfo.queueType = XGL_QUEUE_TYPE_GRAPHICS;
115 err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
116 ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed";
117}
118
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600119void XglRenderFramework::InitViewport(float width, float height)
120{
121 XGL_RESULT err;
122
Tony Barbourf52346d2015-01-16 14:27:35 -0700123 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600124
Tony Barbourf52346d2015-01-16 14:27:35 -0700125 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewportCreate = {};
126 viewportCreate.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
127 viewportCreate.viewportCount = 1;
128 viewportCreate.scissorCount = 0;
129 viewport.originX = 0;
130 viewport.originY = 0;
131 viewport.width = 1.f * width;
132 viewport.height = 1.f * height;
133 viewport.minDepth = 0.f;
134 viewport.maxDepth = 1.f;
135 viewportCreate.pViewports = &viewport;
136
137 err = xglCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600138 ASSERT_XGL_SUCCESS( err );
139 m_width = width;
140 m_height = height;
141}
142
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600143void XglRenderFramework::InitViewport()
144{
145 InitViewport(m_width, m_height);
146}
147
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600148void XglRenderFramework::InitRenderTarget()
149{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600150 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800151
152 for (i = 0; i < m_renderTargetCount; i++) {
Chia-I Wuf50ee212014-12-29 14:31:52 +0800153 XglImage *img = new XglImage(m_device);
154 img->init(m_width, m_height, m_render_target_fmt,
155 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
156 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700157 m_colorBindings[i].view = img->targetView();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000158 m_colorBindings[i].layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700159 m_renderTargets.push_back(img);
Chia-I Wuecebf752014-12-05 10:45:15 +0800160 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700161 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
162 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_LOAD;
163 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_STORE;
164 XGL_DEPTH_STENCIL_BIND_INFO *dsBinding;
165 if (m_depthStencilBinding.view)
166 dsBinding = &m_depthStencilBinding;
167 else
168 dsBinding = NULL;
169 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
170 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
171 .pNext = NULL,
172 .colorAttachmentCount = m_renderTargetCount,
173 .pColorAttachments = m_colorBindings,
174 .pDepthStencilAttachment = dsBinding,
175 .sampleCount = 1,
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600176 .width = (uint32_t)m_width,
177 .height = (uint32_t)m_height,
Mark Lobodzinskidffa1202015-01-27 13:24:03 -0600178 .layers = 1,
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700179 };
180 XGL_RENDER_PASS_CREATE_INFO rp_info;
181 memset(&rp_info, 0 , sizeof(rp_info));
182 xglCreateFramebuffer(device(), &fb_info, &(rp_info.framebuffer));
183 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
184 rp_info.renderArea.extent.width = m_width;
185 rp_info.renderArea.extent.height = m_height;
186 rp_info.pColorLoadOps = &load_op;
187 rp_info.pColorStoreOps = &store_op;
188 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
189 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
190 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
191 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
192 xglCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600193}
194
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600195XglDevice::XglDevice(uint32_t id, XGL_PHYSICAL_GPU obj) :
Chia-I Wufb1459b2014-12-29 15:23:20 +0800196 xgl_testing::Device(obj), id(id)
197{
198 init();
199
200 props = gpu().properties();
201 queue_props = &gpu().queue_properties()[0];
202}
203
204void XglDevice::get_device_queue()
205{
206 ASSERT_NE(true, graphics_queues().empty());
207 m_queue = graphics_queues()[0]->obj();
208}
209
Tony Barboure2c58df2014-11-25 13:18:32 -0700210XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
211{
212 m_device = device;
213 m_nextSlot = 0;
214
215}
216
Chia-I Wu11078b02015-01-04 16:27:24 +0800217XglDescriptorSetObj::~XglDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700218{
Chia-I Wu11078b02015-01-04 16:27:24 +0800219 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700220}
Tony Barbour82c39522014-12-04 14:33:33 -0700221
Chia-I Wu11078b02015-01-04 16:27:24 +0800222int XglDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700223{
Chia-I Wu11078b02015-01-04 16:27:24 +0800224 /* request a descriptor but do not update it */
225 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
226 tc.type = XGL_DESCRIPTOR_TYPE_RAW_BUFFER;
227 tc.count = 1;
228 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700229
Chia-I Wu11078b02015-01-04 16:27:24 +0800230 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700231}
Tony Barbour82c39522014-12-04 14:33:33 -0700232
Chia-I Wu11078b02015-01-04 16:27:24 +0800233int XglDescriptorSetObj::AppendBuffer(XGL_DESCRIPTOR_TYPE type, XglConstantBufferObj *constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700234{
Chia-I Wu11078b02015-01-04 16:27:24 +0800235 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
236 tc.type = type;
237 tc.count = 1;
238 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700239
Chia-I Wu11078b02015-01-04 16:27:24 +0800240 m_bufferInfo.push_back(&constantBuffer->m_bufferViewInfo);
241
242 m_updateBuffers.push_back(xgl_testing::DescriptorSet::update(type, m_nextSlot, 1,
243 (const XGL_BUFFER_VIEW_ATTACH_INFO **) NULL));
244
245 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700246}
Tony Barbour82c39522014-12-04 14:33:33 -0700247
Chia-I Wu11078b02015-01-04 16:27:24 +0800248int XglDescriptorSetObj::AppendSamplerTexture( XglSamplerObj* sampler, XglTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700249{
Chia-I Wu11078b02015-01-04 16:27:24 +0800250 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
251 tc.type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE;
252 tc.count = 1;
253 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700254
Chia-I Wu11078b02015-01-04 16:27:24 +0800255 XGL_SAMPLER_IMAGE_VIEW_INFO tmp = {};
256 tmp.pSampler = sampler->obj();
257 tmp.pImageView = &texture->m_textureViewInfo;
258 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700259
Chia-I Wu11078b02015-01-04 16:27:24 +0800260 m_updateSamplerTextures.push_back(xgl_testing::DescriptorSet::update(m_nextSlot, 1,
261 (const XGL_SAMPLER_IMAGE_VIEW_INFO *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700262
Chia-I Wu11078b02015-01-04 16:27:24 +0800263 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700264}
Chia-I Wu11078b02015-01-04 16:27:24 +0800265
266XGL_DESCRIPTOR_SET_LAYOUT XglDescriptorSetObj::GetLayout()
Tony Barbourb5f4d082014-12-17 10:54:03 -0700267{
Chia-I Wu11078b02015-01-04 16:27:24 +0800268 return m_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700269}
270
271XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle()
272{
Chia-I Wu11078b02015-01-04 16:27:24 +0800273 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700274}
Tony Barboure2c58df2014-11-25 13:18:32 -0700275
Chia-I Wu11078b02015-01-04 16:27:24 +0800276void XglDescriptorSetObj::CreateXGLDescriptorSet(XglCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700277{
Chia-I Wu11078b02015-01-04 16:27:24 +0800278 // create XGL_DESCRIPTOR_REGION
279 XGL_DESCRIPTOR_REGION_CREATE_INFO region = {};
280 region.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO;
281 region.count = m_type_counts.size();
282 region.pTypeCount = &m_type_counts[0];
283 init(*m_device, XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1, region);
Tony Barbour824b7712014-12-18 17:06:21 -0700284
Chia-I Wu11078b02015-01-04 16:27:24 +0800285 // create XGL_DESCRIPTOR_SET_LAYOUT
286 vector<XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO> layout;
287 layout.resize(m_type_counts.size());
288 for (int i = 0; i < m_type_counts.size(); i++) {
289 layout[i].sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
290 layout[i].descriptorType = m_type_counts[i].type;
291 layout[i].count = m_type_counts[i].count;
292 layout[i].stageFlags = XGL_SHADER_STAGE_FLAGS_ALL;
293 layout[i].immutableSampler = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700294
Chia-I Wu11078b02015-01-04 16:27:24 +0800295 if (i < m_type_counts.size() - 1)
296 layout[i].pNext = &layout[i + 1];
297 else
298 layout[i].pNext = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700299 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800300
Chia-I Wu11078b02015-01-04 16:27:24 +0800301 m_layout.init(*m_device, 0, layout[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700302
Chia-I Wu11078b02015-01-04 16:27:24 +0800303 // create XGL_DESCRIPTOR_SET
304 m_set = alloc_sets(XGL_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
305
306 // build the update chain
307 for (int i = 0; i < m_updateBuffers.size(); i++) {
308 m_updateBuffers[i].pBufferViews = &m_bufferInfo[i];
309
310 if (i < m_updateBuffers.size() - 1)
311 m_updateBuffers[i].pNext = &m_updateBuffers[i + 1];
312 else if (m_updateSamplerTextures.empty())
313 m_updateBuffers[i].pNext = NULL;
314 else
315 m_updateBuffers[i].pNext = &m_updateSamplerTextures[0];
316 }
317 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
318 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
319
320 if (i < m_updateSamplerTextures.size() - 1)
321 m_updateSamplerTextures[i].pNext = &m_updateSamplerTextures[i + 1];
322 else
323 m_updateSamplerTextures[i].pNext = NULL;
324 }
325 const void *chain = (!m_updateBuffers.empty()) ? (const void *) &m_updateBuffers[0] :
326 (!m_updateSamplerTextures.empty()) ? (const void *) &m_updateSamplerTextures[0] :
327 NULL;
328
329 // do the updates
330 m_device->begin_descriptor_region_update(XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
331 clear_sets(*m_set);
332 m_set->update(chain);
333 m_device->end_descriptor_region_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700334}
Tony Barboure2c58df2014-11-25 13:18:32 -0700335
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800336XglImage::XglImage(XglDevice *dev)
337{
338 m_device = dev;
339 m_imageInfo.view = XGL_NULL_HANDLE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000340 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800341}
342
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600343void XglImage::init(uint32_t w, uint32_t h,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800344 XGL_FORMAT fmt, XGL_FLAGS usage,
345 XGL_IMAGE_TILING tiling)
346{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600347 uint32_t mipCount;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800348
349 mipCount = 0;
350
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600351 uint32_t _w = w;
352 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800353 while( ( _w > 0 ) || ( _h > 0 ) )
354 {
355 _w >>= 1;
356 _h >>= 1;
357 mipCount++;
358 }
359
360 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
361 imageCreateInfo.imageType = XGL_IMAGE_2D;
362 imageCreateInfo.format = fmt;
363 imageCreateInfo.extent.width = w;
364 imageCreateInfo.extent.height = h;
365 imageCreateInfo.mipLevels = mipCount;
366 imageCreateInfo.tiling = tiling;
367
368 imageCreateInfo.usage = usage;
369
370 xgl_testing::Image::init(*m_device, imageCreateInfo);
371
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000372 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800373
374 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO createView = {
375 XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
376 XGL_NULL_HANDLE,
377 obj(),
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700378 XGL_FMT_R8G8B8A8_UNORM,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800379 0,
380 0,
381 1
382 };
383
384 m_targetView.init(*m_device, createView);
385}
386
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600387XGL_RESULT XglImage::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800388{
389 *ptr = map();
390 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
391}
392
393XGL_RESULT XglImage::UnmapMemory()
394{
395 unmap();
396 return XGL_SUCCESS;
397}
398
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800399XglTextureObj::XglTextureObj(XglDevice *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700400{
401 m_device = device;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700402 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barboure2c58df2014-11-25 13:18:32 -0700403 const uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barboure2c58df2014-11-25 13:18:32 -0700404
405 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
406
407 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
408
409 const XGL_IMAGE_CREATE_INFO image = {
410 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
411 .pNext = NULL,
412 .imageType = XGL_IMAGE_2D,
413 .format = tex_format,
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800414 .extent = { 16, 16, 1 },
Tony Barboure2c58df2014-11-25 13:18:32 -0700415 .mipLevels = 1,
416 .arraySize = 1,
417 .samples = 1,
418 .tiling = XGL_LINEAR_TILING,
419 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
420 .flags = 0,
421 };
422
Tony Barboure2c58df2014-11-25 13:18:32 -0700423 XGL_IMAGE_VIEW_CREATE_INFO view;
424 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
425 view.pNext = NULL;
426 view.image = XGL_NULL_HANDLE;
427 view.viewType = XGL_IMAGE_VIEW_2D;
428 view.format = image.format;
429 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
430 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
431 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
432 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
433 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
434 view.subresourceRange.baseMipLevel = 0;
435 view.subresourceRange.mipLevels = 1;
436 view.subresourceRange.baseArraySlice = 0;
437 view.subresourceRange.arraySize = 1;
438 view.minLod = 0.0f;
439
Tony Barboure2c58df2014-11-25 13:18:32 -0700440 /* create image */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800441 init(*m_device, image);
Tony Barboure2c58df2014-11-25 13:18:32 -0700442
443 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800444 view.image = obj();
445 m_textureView.init(*m_device, view);
Tony Barboure2c58df2014-11-25 13:18:32 -0700446
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800447 XGL_SUBRESOURCE_LAYOUT layout =
448 subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
449 m_rowPitch = layout.rowPitch;
Tony Barboure2c58df2014-11-25 13:18:32 -0700450
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600451 void *data;
452 int32_t x, y;
Tony Barboure2c58df2014-11-25 13:18:32 -0700453
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800454 data = map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700455
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800456 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700457 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800458 for (x = 0; x < extent().width; x++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700459 row[x] = tex_colors[(x & 1) ^ (y & 1)];
460 }
461
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800462 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700463
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800464 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700465
466}
Tony Barbour82c39522014-12-04 14:33:33 -0700467
Tony Barboure2c58df2014-11-25 13:18:32 -0700468void XglTextureObj::ChangeColors(uint32_t color1, uint32_t color2)
469{
Tony Barboure2c58df2014-11-25 13:18:32 -0700470 const uint32_t tex_colors[2] = { color1, color2 };
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600471 void *data;
Tony Barboure2c58df2014-11-25 13:18:32 -0700472
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800473 data = map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700474
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800475 for (int y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700476 uint32_t *row = (uint32_t *) ((char *) data + m_rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800477 for (int x = 0; x < extent().width; x++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700478 row[x] = tex_colors[(x & 1) ^ (y & 1)];
479 }
480
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800481 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700482}
483
484XglSamplerObj::XglSamplerObj(XglDevice *device)
485{
Tony Barboure2c58df2014-11-25 13:18:32 -0700486 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700487
Chia-I Wue9864b52014-12-28 16:32:24 +0800488 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
489 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
490 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
491 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
492 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
493 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
494 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
495 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
496 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
497 samplerCreateInfo.mipLodBias = 0.0;
498 samplerCreateInfo.maxAnisotropy = 0.0;
499 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
500 samplerCreateInfo.minLod = 0.0;
501 samplerCreateInfo.maxLod = 0.0;
502 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700503
Chia-I Wue9864b52014-12-28 16:32:24 +0800504 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700505}
Tony Barboure2c58df2014-11-25 13:18:32 -0700506
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700507/*
508 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
509 */
510XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
511{
512 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700513 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700514
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800515 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700516}
517
Tony Barboure2c58df2014-11-25 13:18:32 -0700518XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
519{
Tony Barboure2c58df2014-11-25 13:18:32 -0700520 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800521 m_commandBuffer = 0;
522
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800523 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700524 m_numVertices = constantCount;
525 m_stride = constantSize;
526
Chia-I Wua07fee62014-12-28 15:26:08 +0800527 const size_t allocationSize = constantCount * constantSize;
528 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700529
Chia-I Wua07fee62014-12-28 15:26:08 +0800530 void *pData = map();
531 memcpy(pData, data, allocationSize);
532 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700533
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800534 // set up the buffer view for the constant buffer
535 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
536 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
537 view_info.buffer = obj();
Chia-I Wubb0c8d22015-01-16 22:31:25 +0800538 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800539 view_info.offset = 0;
540 view_info.range = allocationSize;
541 m_bufferView.init(*m_device, view_info);
542
543 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
544 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700545}
Tony Barbour82c39522014-12-04 14:33:33 -0700546
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600547void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700548{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800549 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700550}
551
552
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000553void XglConstantBufferObj::BufferMemoryBarrier(
554 XGL_FLAGS outputMask /*=
555 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
556 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
557 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
558 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
559 XGL_MEMORY_OUTPUT_COPY_BIT*/,
560 XGL_FLAGS inputMask /*=
561 XGL_MEMORY_INPUT_CPU_READ_BIT |
562 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
563 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
564 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
565 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
566 XGL_MEMORY_INPUT_SHADER_READ_BIT |
567 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
568 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
569 XGL_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700570{
Tony Barbour38422802014-12-10 14:36:31 -0700571 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700572
Tony Barbour38422802014-12-10 14:36:31 -0700573 if (!m_commandBuffer)
574 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800575 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700576
577 m_commandBuffer = new XglCommandBufferObj(m_device);
578
579 }
580 else
581 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800582 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700583 }
584
Tony Barboure2c58df2014-11-25 13:18:32 -0700585 // open the command buffer
Jon Ashburnc4164b12014-12-31 17:10:47 -0700586 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
587 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
588 .pNext = NULL,
589 .flags = 0,
590 };
591 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700592 ASSERT_XGL_SUCCESS(err);
593
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000594 XGL_BUFFER_MEMORY_BARRIER memory_barrier =
595 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Mark Lobodzinski837ef922015-01-29 14:24:14 -0600596 XGL_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700597
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000598 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
599 XGL_PIPELINE_BARRIER pipeline_barrier = {};
600 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
601 pipeline_barrier.eventCount = 1;
602 pipeline_barrier.pEvents = set_events;
603 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
604 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinski837ef922015-01-29 14:24:14 -0600605 pipeline_barrier.pMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000606
607 // write barrier to the command buffer
608 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700609
610 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700611 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700612 ASSERT_XGL_SUCCESS(err);
613
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600614 uint32_t numMemRefs=1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700615 XGL_MEMORY_REF memRefs;
616 // this command buffer only uses the vertex buffer memory
617 memRefs.flags = 0;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800618 memRefs.mem = memories()[0];
Tony Barboure2c58df2014-11-25 13:18:32 -0700619
620 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700621 XGL_CMD_BUFFER bufferArray[1];
622 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800623 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700624 ASSERT_XGL_SUCCESS(err);
625}
626
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700627XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
628 : XglConstantBufferObj(device)
629{
630
631}
632
633void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
634{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700635 XGL_FORMAT viewFormat;
636
637 m_numVertices = numIndexes;
638 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700639 switch (indexType) {
640 case XGL_INDEX_8:
641 m_stride = 1;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700642 viewFormat = XGL_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700643 break;
644 case XGL_INDEX_16:
645 m_stride = 2;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700646 viewFormat = XGL_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700647 break;
648 case XGL_INDEX_32:
649 m_stride = 4;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700650 viewFormat = XGL_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700651 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800652 default:
653 assert(!"unknown index type");
654 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700655 }
656
Chia-I Wua07fee62014-12-28 15:26:08 +0800657 const size_t allocationSize = numIndexes * m_stride;
658 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700659
Chia-I Wua07fee62014-12-28 15:26:08 +0800660 void *pData = map();
661 memcpy(pData, data, allocationSize);
662 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700663
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800664 // set up the buffer view for the constant buffer
665 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
666 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
667 view_info.buffer = obj();
668 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
669 view_info.stride = m_stride;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700670 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800671 view_info.channels.r = XGL_CHANNEL_SWIZZLE_R;
672 view_info.channels.g = XGL_CHANNEL_SWIZZLE_G;
673 view_info.channels.b = XGL_CHANNEL_SWIZZLE_B;
674 view_info.channels.a = XGL_CHANNEL_SWIZZLE_A;
675 view_info.offset = 0;
676 view_info.range = allocationSize;
677 m_bufferView.init(*m_device, view_info);
678
679 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
680 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700681}
682
683void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
684{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800685 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700686}
Tony Barboure2c58df2014-11-25 13:18:32 -0700687
Tony Barbouraf1f9192014-12-17 10:57:58 -0700688XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
689{
690 return m_indexType;
691}
692
Chia-I Wu11078b02015-01-04 16:27:24 +0800693XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700694{
Tony Barboure2c58df2014-11-25 13:18:32 -0700695 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
696 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
697 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800698 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700699 stageInfo->shader.linkConstBufferCount = 0;
700 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700701
Tony Barboure2c58df2014-11-25 13:18:32 -0700702 return stageInfo;
703}
704
Tony Barboure2c58df2014-11-25 13:18:32 -0700705XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
706{
707 XGL_RESULT err = XGL_SUCCESS;
708 std::vector<unsigned int> bil;
709 XGL_SHADER_CREATE_INFO createInfo;
710 size_t shader_len;
711
712 m_stage = stage;
713 m_device = device;
714
715 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
716 createInfo.pNext = NULL;
717
718 if (!framework->m_use_bil) {
719
720 shader_len = strlen(shader_code);
721 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
722 createInfo.pCode = malloc(createInfo.codeSize);
723 createInfo.flags = 0;
724
725 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
726 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
727 ((uint32_t *) createInfo.pCode)[1] = 0;
728 ((uint32_t *) createInfo.pCode)[2] = stage;
729 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
730
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800731 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700732 }
733
734 if (framework->m_use_bil || err) {
735 std::vector<unsigned int> bil;
736 err = XGL_SUCCESS;
737
738 // Use Reference GLSL to BIL compiler
739 framework->GLSLtoBIL(stage, shader_code, bil);
740 createInfo.pCode = bil.data();
741 createInfo.codeSize = bil.size() * sizeof(unsigned int);
742 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700743
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800744 init(*m_device, createInfo);
745 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700746}
Tony Barbour82c39522014-12-04 14:33:33 -0700747
Tony Barboure2c58df2014-11-25 13:18:32 -0700748XglPipelineObj::XglPipelineObj(XglDevice *device)
749{
Tony Barboure2c58df2014-11-25 13:18:32 -0700750 m_device = device;
751 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
752 m_vertexBufferCount = 0;
753
754 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
755 m_ia_state.pNext = XGL_NULL_HANDLE;
756 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
757 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700758 m_ia_state.primitiveRestartEnable = XGL_FALSE;
759 m_ia_state.primitiveRestartIndex = 0;
760
761 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
762 m_rs_state.pNext = &m_ia_state;
763 m_rs_state.depthClipEnable = XGL_FALSE;
764 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -0700765 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
766 m_rs_state.fillMode = XGL_FILL_SOLID;
767 m_rs_state.cullMode = XGL_CULL_NONE;
768 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700769
Tony Barboure2c58df2014-11-25 13:18:32 -0700770 memset(&m_cb_state,0,sizeof(m_cb_state));
771 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
772 m_cb_state.pNext = &m_rs_state;
773 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
774 m_cb_state.dualSourceBlendEnable = XGL_FALSE;
775 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
776
Tony Barbourf52346d2015-01-16 14:27:35 -0700777 m_ms_state.pNext = &m_cb_state;
778 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
779 m_ms_state.multisampleEnable = XGL_FALSE;
780 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
781 m_ms_state.samples = 1;
782 m_ms_state.minSampleShading = 0;
783 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700784
Tony Barbourf52346d2015-01-16 14:27:35 -0700785 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
786 m_ds_state.pNext = &m_ms_state,
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700787 m_ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700788 m_ds_state.depthTestEnable = XGL_FALSE;
789 m_ds_state.depthWriteEnable = XGL_FALSE;
790 m_ds_state.depthBoundsEnable = XGL_FALSE;
791 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
792 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
793 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
794 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
795 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
796 m_ds_state.stencilTestEnable = XGL_FALSE;
797 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -0700798
Tony Barbourf52346d2015-01-16 14:27:35 -0700799 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
800 att.blendEnable = XGL_FALSE;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700801 att.format = XGL_FMT_R8G8B8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -0700802 att.channelWriteMask = 0xf;
803 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -0700804
805};
806
807void XglPipelineObj::AddShader(XglShaderObj* shader)
808{
809 m_shaderObjs.push_back(shader);
810}
811
812void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
813{
814 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
815 m_vi_state.attributeCount = count;
816}
817
818void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
819{
820 m_vi_state.pVertexBindingDescriptions = vi_binding;
821 m_vi_state.bindingCount = count;
822}
823
824void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
825{
826 m_vertexBufferObjs.push_back(vertexDataBuffer);
827 m_vertexBufferBindings.push_back(binding);
828 m_vertexBufferCount++;
829}
830
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600831void XglPipelineObj::AddColorAttachment(uint32_t binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +0800832{
Tony Barbourf52346d2015-01-16 14:27:35 -0700833 if (binding+1 > m_colorAttachments.size())
834 {
835 m_colorAttachments.resize(binding+1);
836 }
837 m_colorAttachments[binding] = *att;
838}
839
840void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
841{
842 m_ds_state.format = ds_state->format;
843 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
844 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
845 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
846 m_ds_state.depthFunc = ds_state->depthFunc;
847 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
848 m_ds_state.back = ds_state->back;
849 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +0800850}
851
Tony Barbour976e1cf2014-12-17 11:57:31 -0700852void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
853{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600854 void* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -0700855 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
856
857 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
858
859 for (int i=0; i<m_shaderObjs.size(); i++)
860 {
Chia-I Wu11078b02015-01-04 16:27:24 +0800861 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700862 shaderCreateInfo->pNext = head_ptr;
863 head_ptr = shaderCreateInfo;
864 }
865
866 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
867 {
868 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
869 m_vi_state.pNext = head_ptr;
870 head_ptr = &m_vi_state;
871 }
872
873 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
874 info.pNext = head_ptr;
875 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +0800876 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700877
Tony Barbourf52346d2015-01-16 14:27:35 -0700878 m_cb_state.attachmentCount = m_colorAttachments.size();
879 m_cb_state.pAttachments = &m_colorAttachments[0];
880
Chia-I Wu2648d092014-12-29 14:24:14 +0800881 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -0700882}
Chia-I Wu2648d092014-12-29 14:24:14 +0800883
Tony Barbour976e1cf2014-12-17 11:57:31 -0700884XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
885{
Chia-I Wu2648d092014-12-29 14:24:14 +0800886 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700887}
888
Tony Barbour5420af02014-12-03 13:58:15 -0700889void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700890{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600891 void* head_ptr = &m_ds_state;
Tony Barboure2c58df2014-11-25 13:18:32 -0700892 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
893
894 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700895
896 for (int i=0; i<m_shaderObjs.size(); i++)
897 {
Chia-I Wu11078b02015-01-04 16:27:24 +0800898 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barboure2c58df2014-11-25 13:18:32 -0700899 shaderCreateInfo->pNext = head_ptr;
900 head_ptr = shaderCreateInfo;
901 }
902
903 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
904 {
905 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
906 m_vi_state.pNext = head_ptr;
907 head_ptr = &m_vi_state;
908 }
909
910 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
911 info.pNext = head_ptr;
912 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +0800913 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barboure2c58df2014-11-25 13:18:32 -0700914
Chia-I Wu2648d092014-12-29 14:24:14 +0800915 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700916
Chia-I Wu2648d092014-12-29 14:24:14 +0800917 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700918
919
920 for (int i=0; i < m_vertexBufferCount; i++)
921 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800922 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700923 }
Tony Barboure2c58df2014-11-25 13:18:32 -0700924}
Tony Barbour82c39522014-12-04 14:33:33 -0700925
Tony Barboure2c58df2014-11-25 13:18:32 -0700926XglMemoryRefManager::XglMemoryRefManager() {
927
928}
Tony Barbour82c39522014-12-04 14:33:33 -0700929
Tony Barboure2c58df2014-11-25 13:18:32 -0700930void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800931 const std::vector<XGL_GPU_MEMORY> mems = constantBuffer->memories();
932 if (!mems.empty())
933 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700934}
Tony Barbour82c39522014-12-04 14:33:33 -0700935
Tony Barboure2c58df2014-11-25 13:18:32 -0700936void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800937 const std::vector<XGL_GPU_MEMORY> mems = texture->memories();
938 if (!mems.empty())
939 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700940}
Tony Barbour82c39522014-12-04 14:33:33 -0700941
Tony Barboure2c58df2014-11-25 13:18:32 -0700942XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
943
944 XGL_MEMORY_REF *localRefs;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600945 uint32_t numRefs=m_bufferObjs.size();
Tony Barboure2c58df2014-11-25 13:18:32 -0700946
947 if (numRefs <= 0)
948 return NULL;
949
950 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
951 for (int i=0; i<numRefs; i++)
952 {
953 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +0800954 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -0700955 }
956 return localRefs;
957}
958int XglMemoryRefManager::GetNumRefs() {
959 return m_bufferObjs.size();
960}
Tony Barbour6d047bf2014-12-10 14:34:45 -0700961
962XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Chia-I Wud28343c2014-12-28 15:12:48 +0800963 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(XGL_QUEUE_TYPE_GRAPHICS))
Tony Barbour6d047bf2014-12-10 14:34:45 -0700964{
Tony Barbour6d047bf2014-12-10 14:34:45 -0700965 m_device = device;
Chia-I Wud28343c2014-12-28 15:12:48 +0800966 m_renderTargetCount = 0;
Tony Barbour6d047bf2014-12-10 14:34:45 -0700967}
Tony Barbour471338d2014-12-10 17:28:39 -0700968
Tony Barbour6d047bf2014-12-10 14:34:45 -0700969XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
970{
Chia-I Wud28343c2014-12-28 15:12:48 +0800971 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -0700972}
Tony Barbour471338d2014-12-10 17:28:39 -0700973
Jon Ashburnc4164b12014-12-31 17:10:47 -0700974XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -0700975{
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700976 begin(pInfo);
977 return XGL_SUCCESS;
978}
979
980XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj)
981{
982 begin(renderpass_obj);
983 return XGL_SUCCESS;
984}
985
986XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
987{
988 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +0800989 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -0700990}
991
992XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
993{
Chia-I Wud28343c2014-12-28 15:12:48 +0800994 end();
995 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -0700996}
997
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000998void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -0700999{
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001000 xglCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001001}
1002
Tony Barbour30cc9e82014-12-17 11:53:55 -07001003void XglCommandBufferObj::ClearAllBuffers(XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
1004{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001005 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001006 const XGL_FLAGS output_mask =
1007 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1008 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1009 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1010 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1011 XGL_MEMORY_OUTPUT_COPY_BIT;
1012 const XGL_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001013
1014 // whatever we want to do, we do it to the whole buffer
1015 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1016 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1017 srRange.baseMipLevel = 0;
1018 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1019 srRange.baseArraySlice = 0;
1020 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1021
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001022 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1023 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1024 memory_barrier.outputMask = output_mask;
1025 memory_barrier.inputMask = input_mask;
1026 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1027 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001028 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001029
1030 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1031 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1032 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1033 pipeline_barrier.eventCount = 1;
1034 pipeline_barrier.pEvents = set_events;
1035 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1036 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001037 pipeline_barrier.pMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001038
Tony Barbour30cc9e82014-12-17 11:53:55 -07001039 // clear the back buffer to dark grey
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001040 uint32_t clearColor[4] = {64, 64, 64, 0};
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001041
Tony Barbour30cc9e82014-12-17 11:53:55 -07001042 for (i = 0; i < m_renderTargetCount; i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001043 memory_barrier.image = m_renderTargets[i]->image();
1044 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1045 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1046 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001047
Chia-I Wud28343c2014-12-28 15:12:48 +08001048 xglCmdClearColorImageRaw( obj(), m_renderTargets[i]->image(), clearColor, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001049 }
1050
1051 if (depthStencilImage)
1052 {
1053 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1054 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1055 dsRange.baseMipLevel = 0;
1056 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1057 dsRange.baseArraySlice = 0;
1058 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1059
1060 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001061
1062 memory_barrier.oldLayout = depthStencilBinding->layout;
1063 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1064 memory_barrier.image = depthStencilImage;
1065 memory_barrier.subresourceRange = dsRange;
1066
1067 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1068 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001069
Chia-I Wud28343c2014-12-28 15:12:48 +08001070 xglCmdClearDepthStencil(obj(), depthStencilImage, 1.0f, 0, 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001071
1072 // prepare depth buffer for rendering
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001073 memory_barrier.image = depthStencilImage;
1074 memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1075 memory_barrier.newLayout = depthStencilBinding->layout;
1076 memory_barrier.subresourceRange = dsRange;
1077 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1078 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001079 }
1080}
1081
Jon Ashburncdc40be2015-01-02 18:27:14 -07001082void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001083{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001084 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001085 const XGL_FLAGS output_mask =
1086 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1087 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1088 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1089 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1090 XGL_MEMORY_OUTPUT_COPY_BIT;
1091 const XGL_FLAGS input_mask =
1092 XGL_MEMORY_INPUT_CPU_READ_BIT |
1093 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1094 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
1095 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1096 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
1097 XGL_MEMORY_INPUT_SHADER_READ_BIT |
1098 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1099 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1100 XGL_MEMORY_INPUT_COPY_BIT;
1101
Tony Barbour30cc9e82014-12-17 11:53:55 -07001102 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1103 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1104 srRange.baseMipLevel = 0;
1105 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1106 srRange.baseArraySlice = 0;
1107 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1108
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001109 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1110 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1111 memory_barrier.outputMask = output_mask;
1112 memory_barrier.inputMask = input_mask;
1113 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1114 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001115 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001116
1117 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1118 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1119 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1120 pipeline_barrier.eventCount = 1;
1121 pipeline_barrier.pEvents = set_events;
1122 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1123 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001124 pipeline_barrier.pMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001125
Tony Barbour30cc9e82014-12-17 11:53:55 -07001126 for(i=0; i<m_renderTargetCount; i++)
1127 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001128 memory_barrier.image = m_renderTargets[i]->image();
1129 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1130 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1131 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001132 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001133}
1134
Tony Barbourf52346d2015-01-16 14:27:35 -07001135void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001136{
Tony Barbourf52346d2015-01-16 14:27:35 -07001137 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001138}
1139
1140void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1141{
1142 m_renderTargets.push_back(renderTarget);
1143 m_renderTargetCount++;
1144}
1145
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001146void XglCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001147{
Chia-I Wud28343c2014-12-28 15:12:48 +08001148 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001149}
1150
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001151void XglCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001152{
Chia-I Wud28343c2014-12-28 15:12:48 +08001153 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001154}
1155
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001156void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, uint32_t numMemRefs)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001157{
1158 XGL_RESULT err = XGL_SUCCESS;
1159
1160 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001161 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001162 ASSERT_XGL_SUCCESS( err );
1163
1164 err = xglQueueWaitIdle( m_device->m_queue );
1165 ASSERT_XGL_SUCCESS( err );
1166
1167 // Wait for work to finish before cleaning up.
1168 xglDeviceWaitIdle(m_device->device());
1169
1170}
1171void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1172{
Chia-I Wud28343c2014-12-28 15:12:48 +08001173 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001174}
1175
1176void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1177{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001178 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wu11078b02015-01-04 16:27:24 +08001179 xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001180}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001181void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001182{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001183 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001184}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001185void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001186{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001187 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001188}