blob: 68f7baa2889f7beaa821cd5b0794aede674bbfd1 [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
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060055 err = xglInitAndEnumerateGpus(&app_info, NULL,
Chia-I Wuaf11d922014-12-28 14:37:25 +080056 XGL_MAX_PHYSICAL_GPUS, &this->gpu_count, objs);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060057 ASSERT_XGL_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070058 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060059
60 m_device = new XglDevice(0, objs[0]);
61 m_device->get_device_queue();
62}
63
64void XglRenderFramework::ShutdownFramework()
65{
66 if (m_colorBlend) xglDestroyObject(m_colorBlend);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060067 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
68 if (m_stateRaster) xglDestroyObject(m_stateRaster);
69 if (m_cmdBuffer) xglDestroyObject(m_cmdBuffer);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060070
71 if (m_stateViewport) {
72 xglDestroyObject(m_stateViewport);
73 }
74
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060075 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +080076 delete m_device;
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060077 xglInitAndEnumerateGpus(&this->app_info, XGL_NULL_HANDLE, 0, &gpu_count, XGL_NULL_HANDLE);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060078}
79
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060080void XglRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060081{
82 XGL_RESULT err;
83
Jeremy Hayesa058eee2015-01-23 08:51:43 -070084 m_render_target_fmt = XGL_FMT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060085
86 // create a raster state (solid, back-face culling)
Tony Barbourf52346d2015-01-16 14:27:35 -070087 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster = {};
88 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
89 raster.pointSize = 1.0;
90
91 err = xglCreateDynamicRasterState( device(), &raster, &m_stateRaster );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060092 ASSERT_XGL_SUCCESS(err);
93
Tony Barbourf52346d2015-01-16 14:27:35 -070094 XGL_DYNAMIC_CB_STATE_CREATE_INFO blend = {};
95 blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
96 err = xglCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060097 ASSERT_XGL_SUCCESS( err );
98
Tony Barbourf52346d2015-01-16 14:27:35 -070099 XGL_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {};
100 depthStencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600101 depthStencil.minDepth = 0.f;
102 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700103 depthStencil.stencilFrontRef = 0;
104 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600105
Tony Barbourf52346d2015-01-16 14:27:35 -0700106 err = xglCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600107 ASSERT_XGL_SUCCESS( err );
108
109 XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {};
110
111 cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
112 cmdInfo.queueType = XGL_QUEUE_TYPE_GRAPHICS;
113 err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
114 ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed";
115}
116
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600117void XglRenderFramework::InitViewport(float width, float height)
118{
119 XGL_RESULT err;
120
Tony Barbourf52346d2015-01-16 14:27:35 -0700121 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600122
Tony Barbourf52346d2015-01-16 14:27:35 -0700123 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewportCreate = {};
124 viewportCreate.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
125 viewportCreate.viewportCount = 1;
126 viewportCreate.scissorCount = 0;
127 viewport.originX = 0;
128 viewport.originY = 0;
129 viewport.width = 1.f * width;
130 viewport.height = 1.f * height;
131 viewport.minDepth = 0.f;
132 viewport.maxDepth = 1.f;
133 viewportCreate.pViewports = &viewport;
134
135 err = xglCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600136 ASSERT_XGL_SUCCESS( err );
137 m_width = width;
138 m_height = height;
139}
140
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600141void XglRenderFramework::InitViewport()
142{
143 InitViewport(m_width, m_height);
144}
145
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600146void XglRenderFramework::InitRenderTarget()
147{
Chia-I Wuecebf752014-12-05 10:45:15 +0800148 XGL_UINT i;
149
150 for (i = 0; i < m_renderTargetCount; i++) {
Chia-I Wuf50ee212014-12-29 14:31:52 +0800151 XglImage *img = new XglImage(m_device);
152 img->init(m_width, m_height, m_render_target_fmt,
153 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
154 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700155 m_colorBindings[i].view = img->targetView();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000156 m_colorBindings[i].layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700157 m_renderTargets.push_back(img);
Chia-I Wuecebf752014-12-05 10:45:15 +0800158 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700159 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
160 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_LOAD;
161 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_STORE;
162 XGL_DEPTH_STENCIL_BIND_INFO *dsBinding;
163 if (m_depthStencilBinding.view)
164 dsBinding = &m_depthStencilBinding;
165 else
166 dsBinding = NULL;
167 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
168 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
169 .pNext = NULL,
170 .colorAttachmentCount = m_renderTargetCount,
171 .pColorAttachments = m_colorBindings,
172 .pDepthStencilAttachment = dsBinding,
173 .sampleCount = 1,
174 };
175 XGL_RENDER_PASS_CREATE_INFO rp_info;
176 memset(&rp_info, 0 , sizeof(rp_info));
177 xglCreateFramebuffer(device(), &fb_info, &(rp_info.framebuffer));
178 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
179 rp_info.renderArea.extent.width = m_width;
180 rp_info.renderArea.extent.height = m_height;
181 rp_info.pColorLoadOps = &load_op;
182 rp_info.pColorStoreOps = &store_op;
183 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
184 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
185 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
186 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
187 xglCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600188}
189
Chia-I Wufb1459b2014-12-29 15:23:20 +0800190XglDevice::XglDevice(XGL_UINT id, XGL_PHYSICAL_GPU obj) :
191 xgl_testing::Device(obj), id(id)
192{
193 init();
194
195 props = gpu().properties();
196 queue_props = &gpu().queue_properties()[0];
197}
198
199void XglDevice::get_device_queue()
200{
201 ASSERT_NE(true, graphics_queues().empty());
202 m_queue = graphics_queues()[0]->obj();
203}
204
Tony Barboure2c58df2014-11-25 13:18:32 -0700205XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
206{
207 m_device = device;
208 m_nextSlot = 0;
209
210}
211
Chia-I Wu11078b02015-01-04 16:27:24 +0800212XglDescriptorSetObj::~XglDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700213{
Chia-I Wu11078b02015-01-04 16:27:24 +0800214 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700215}
Tony Barbour82c39522014-12-04 14:33:33 -0700216
Chia-I Wu11078b02015-01-04 16:27:24 +0800217int XglDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700218{
Chia-I Wu11078b02015-01-04 16:27:24 +0800219 /* request a descriptor but do not update it */
220 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
221 tc.type = XGL_DESCRIPTOR_TYPE_RAW_BUFFER;
222 tc.count = 1;
223 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700224
Chia-I Wu11078b02015-01-04 16:27:24 +0800225 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700226}
Tony Barbour82c39522014-12-04 14:33:33 -0700227
Chia-I Wu11078b02015-01-04 16:27:24 +0800228int XglDescriptorSetObj::AppendBuffer(XGL_DESCRIPTOR_TYPE type, XglConstantBufferObj *constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700229{
Chia-I Wu11078b02015-01-04 16:27:24 +0800230 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
231 tc.type = type;
232 tc.count = 1;
233 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700234
Chia-I Wu11078b02015-01-04 16:27:24 +0800235 m_bufferInfo.push_back(&constantBuffer->m_bufferViewInfo);
236
237 m_updateBuffers.push_back(xgl_testing::DescriptorSet::update(type, m_nextSlot, 1,
238 (const XGL_BUFFER_VIEW_ATTACH_INFO **) NULL));
239
240 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700241}
Tony Barbour82c39522014-12-04 14:33:33 -0700242
Chia-I Wu11078b02015-01-04 16:27:24 +0800243int XglDescriptorSetObj::AppendSamplerTexture( XglSamplerObj* sampler, XglTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700244{
Chia-I Wu11078b02015-01-04 16:27:24 +0800245 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
246 tc.type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE;
247 tc.count = 1;
248 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700249
Chia-I Wu11078b02015-01-04 16:27:24 +0800250 XGL_SAMPLER_IMAGE_VIEW_INFO tmp = {};
251 tmp.pSampler = sampler->obj();
252 tmp.pImageView = &texture->m_textureViewInfo;
253 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700254
Chia-I Wu11078b02015-01-04 16:27:24 +0800255 m_updateSamplerTextures.push_back(xgl_testing::DescriptorSet::update(m_nextSlot, 1,
256 (const XGL_SAMPLER_IMAGE_VIEW_INFO *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700257
Chia-I Wu11078b02015-01-04 16:27:24 +0800258 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700259}
Chia-I Wu11078b02015-01-04 16:27:24 +0800260
261XGL_DESCRIPTOR_SET_LAYOUT XglDescriptorSetObj::GetLayout()
Tony Barbourb5f4d082014-12-17 10:54:03 -0700262{
Chia-I Wu11078b02015-01-04 16:27:24 +0800263 return m_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700264}
265
266XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle()
267{
Chia-I Wu11078b02015-01-04 16:27:24 +0800268 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700269}
Tony Barboure2c58df2014-11-25 13:18:32 -0700270
Chia-I Wu11078b02015-01-04 16:27:24 +0800271void XglDescriptorSetObj::CreateXGLDescriptorSet(XglCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700272{
Chia-I Wu11078b02015-01-04 16:27:24 +0800273 // create XGL_DESCRIPTOR_REGION
274 XGL_DESCRIPTOR_REGION_CREATE_INFO region = {};
275 region.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO;
276 region.count = m_type_counts.size();
277 region.pTypeCount = &m_type_counts[0];
278 init(*m_device, XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1, region);
Tony Barbour824b7712014-12-18 17:06:21 -0700279
Chia-I Wu11078b02015-01-04 16:27:24 +0800280 // create XGL_DESCRIPTOR_SET_LAYOUT
281 vector<XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO> layout;
282 layout.resize(m_type_counts.size());
283 for (int i = 0; i < m_type_counts.size(); i++) {
284 layout[i].sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
285 layout[i].descriptorType = m_type_counts[i].type;
286 layout[i].count = m_type_counts[i].count;
287 layout[i].stageFlags = XGL_SHADER_STAGE_FLAGS_ALL;
288 layout[i].immutableSampler = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700289
Chia-I Wu11078b02015-01-04 16:27:24 +0800290 if (i < m_type_counts.size() - 1)
291 layout[i].pNext = &layout[i + 1];
292 else
293 layout[i].pNext = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700294 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800295
Chia-I Wu11078b02015-01-04 16:27:24 +0800296 m_layout.init(*m_device, 0, layout[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700297
Chia-I Wu11078b02015-01-04 16:27:24 +0800298 // create XGL_DESCRIPTOR_SET
299 m_set = alloc_sets(XGL_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
300
301 // build the update chain
302 for (int i = 0; i < m_updateBuffers.size(); i++) {
303 m_updateBuffers[i].pBufferViews = &m_bufferInfo[i];
304
305 if (i < m_updateBuffers.size() - 1)
306 m_updateBuffers[i].pNext = &m_updateBuffers[i + 1];
307 else if (m_updateSamplerTextures.empty())
308 m_updateBuffers[i].pNext = NULL;
309 else
310 m_updateBuffers[i].pNext = &m_updateSamplerTextures[0];
311 }
312 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
313 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
314
315 if (i < m_updateSamplerTextures.size() - 1)
316 m_updateSamplerTextures[i].pNext = &m_updateSamplerTextures[i + 1];
317 else
318 m_updateSamplerTextures[i].pNext = NULL;
319 }
320 const void *chain = (!m_updateBuffers.empty()) ? (const void *) &m_updateBuffers[0] :
321 (!m_updateSamplerTextures.empty()) ? (const void *) &m_updateSamplerTextures[0] :
322 NULL;
323
324 // do the updates
325 m_device->begin_descriptor_region_update(XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
326 clear_sets(*m_set);
327 m_set->update(chain);
328 m_device->end_descriptor_region_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700329}
Tony Barboure2c58df2014-11-25 13:18:32 -0700330
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800331XglImage::XglImage(XglDevice *dev)
332{
333 m_device = dev;
334 m_imageInfo.view = XGL_NULL_HANDLE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000335 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800336}
337
338void XglImage::init(XGL_UINT32 w, XGL_UINT32 h,
339 XGL_FORMAT fmt, XGL_FLAGS usage,
340 XGL_IMAGE_TILING tiling)
341{
342 XGL_UINT mipCount;
343
344 mipCount = 0;
345
346 XGL_UINT _w = w;
347 XGL_UINT _h = h;
348 while( ( _w > 0 ) || ( _h > 0 ) )
349 {
350 _w >>= 1;
351 _h >>= 1;
352 mipCount++;
353 }
354
355 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
356 imageCreateInfo.imageType = XGL_IMAGE_2D;
357 imageCreateInfo.format = fmt;
358 imageCreateInfo.extent.width = w;
359 imageCreateInfo.extent.height = h;
360 imageCreateInfo.mipLevels = mipCount;
361 imageCreateInfo.tiling = tiling;
362
363 imageCreateInfo.usage = usage;
364
365 xgl_testing::Image::init(*m_device, imageCreateInfo);
366
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000367 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800368
369 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO createView = {
370 XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
371 XGL_NULL_HANDLE,
372 obj(),
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700373 XGL_FMT_R8G8B8A8_UNORM,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800374 0,
375 0,
376 1
377 };
378
379 m_targetView.init(*m_device, createView);
380}
381
382XGL_RESULT XglImage::MapMemory(XGL_VOID** ptr)
383{
384 *ptr = map();
385 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
386}
387
388XGL_RESULT XglImage::UnmapMemory()
389{
390 unmap();
391 return XGL_SUCCESS;
392}
393
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800394XglTextureObj::XglTextureObj(XglDevice *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700395{
396 m_device = device;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700397 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barboure2c58df2014-11-25 13:18:32 -0700398 const uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barboure2c58df2014-11-25 13:18:32 -0700399
400 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
401
402 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
403
404 const XGL_IMAGE_CREATE_INFO image = {
405 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
406 .pNext = NULL,
407 .imageType = XGL_IMAGE_2D,
408 .format = tex_format,
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800409 .extent = { 16, 16, 1 },
Tony Barboure2c58df2014-11-25 13:18:32 -0700410 .mipLevels = 1,
411 .arraySize = 1,
412 .samples = 1,
413 .tiling = XGL_LINEAR_TILING,
414 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
415 .flags = 0,
416 };
417
Tony Barboure2c58df2014-11-25 13:18:32 -0700418 XGL_IMAGE_VIEW_CREATE_INFO view;
419 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
420 view.pNext = NULL;
421 view.image = XGL_NULL_HANDLE;
422 view.viewType = XGL_IMAGE_VIEW_2D;
423 view.format = image.format;
424 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
425 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
426 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
427 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
428 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
429 view.subresourceRange.baseMipLevel = 0;
430 view.subresourceRange.mipLevels = 1;
431 view.subresourceRange.baseArraySlice = 0;
432 view.subresourceRange.arraySize = 1;
433 view.minLod = 0.0f;
434
Tony Barboure2c58df2014-11-25 13:18:32 -0700435 /* create image */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800436 init(*m_device, image);
Tony Barboure2c58df2014-11-25 13:18:32 -0700437
438 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800439 view.image = obj();
440 m_textureView.init(*m_device, view);
Tony Barboure2c58df2014-11-25 13:18:32 -0700441
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800442 XGL_SUBRESOURCE_LAYOUT layout =
443 subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
444 m_rowPitch = layout.rowPitch;
Tony Barboure2c58df2014-11-25 13:18:32 -0700445
Tony Barboure2c58df2014-11-25 13:18:32 -0700446 XGL_VOID *data;
447 XGL_INT x, y;
448
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800449 data = map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700450
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800451 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700452 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800453 for (x = 0; x < extent().width; x++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700454 row[x] = tex_colors[(x & 1) ^ (y & 1)];
455 }
456
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800457 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700458
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800459 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700460
461}
Tony Barbour82c39522014-12-04 14:33:33 -0700462
Tony Barboure2c58df2014-11-25 13:18:32 -0700463void XglTextureObj::ChangeColors(uint32_t color1, uint32_t color2)
464{
Tony Barboure2c58df2014-11-25 13:18:32 -0700465 const uint32_t tex_colors[2] = { color1, color2 };
466 XGL_VOID *data;
467
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800468 data = map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700469
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800470 for (int y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700471 uint32_t *row = (uint32_t *) ((char *) data + m_rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800472 for (int x = 0; x < extent().width; x++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700473 row[x] = tex_colors[(x & 1) ^ (y & 1)];
474 }
475
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800476 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700477}
478
479XglSamplerObj::XglSamplerObj(XglDevice *device)
480{
Tony Barboure2c58df2014-11-25 13:18:32 -0700481 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700482
Chia-I Wue9864b52014-12-28 16:32:24 +0800483 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
484 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
485 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
486 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
487 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
488 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
489 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
490 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
491 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
492 samplerCreateInfo.mipLodBias = 0.0;
493 samplerCreateInfo.maxAnisotropy = 0.0;
494 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
495 samplerCreateInfo.minLod = 0.0;
496 samplerCreateInfo.maxLod = 0.0;
497 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700498
Chia-I Wue9864b52014-12-28 16:32:24 +0800499 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700500}
Tony Barboure2c58df2014-11-25 13:18:32 -0700501
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700502/*
503 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
504 */
505XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
506{
507 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700508 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700509
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800510 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700511}
512
Tony Barboure2c58df2014-11-25 13:18:32 -0700513XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
514{
Tony Barboure2c58df2014-11-25 13:18:32 -0700515 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800516 m_commandBuffer = 0;
517
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800518 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700519 m_numVertices = constantCount;
520 m_stride = constantSize;
521
Chia-I Wua07fee62014-12-28 15:26:08 +0800522 const size_t allocationSize = constantCount * constantSize;
523 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700524
Chia-I Wua07fee62014-12-28 15:26:08 +0800525 void *pData = map();
526 memcpy(pData, data, allocationSize);
527 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700528
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800529 // set up the buffer view for the constant buffer
530 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
531 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
532 view_info.buffer = obj();
Chia-I Wubb0c8d22015-01-16 22:31:25 +0800533 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800534 view_info.offset = 0;
535 view_info.range = allocationSize;
536 m_bufferView.init(*m_device, view_info);
537
538 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
539 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700540}
Tony Barbour82c39522014-12-04 14:33:33 -0700541
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700542void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, XGL_UINT binding)
543{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800544 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700545}
546
547
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000548void XglConstantBufferObj::BufferMemoryBarrier(
549 XGL_FLAGS outputMask /*=
550 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
551 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
552 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
553 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
554 XGL_MEMORY_OUTPUT_COPY_BIT*/,
555 XGL_FLAGS inputMask /*=
556 XGL_MEMORY_INPUT_CPU_READ_BIT |
557 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
558 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
559 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
560 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
561 XGL_MEMORY_INPUT_SHADER_READ_BIT |
562 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
563 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
564 XGL_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700565{
Tony Barbour38422802014-12-10 14:36:31 -0700566 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700567
Tony Barbour38422802014-12-10 14:36:31 -0700568 if (!m_commandBuffer)
569 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800570 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700571
572 m_commandBuffer = new XglCommandBufferObj(m_device);
573
574 }
575 else
576 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800577 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700578 }
579
Tony Barboure2c58df2014-11-25 13:18:32 -0700580 // open the command buffer
Jon Ashburnc4164b12014-12-31 17:10:47 -0700581 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
582 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
583 .pNext = NULL,
584 .flags = 0,
585 };
586 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700587 ASSERT_XGL_SUCCESS(err);
588
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000589 XGL_BUFFER_MEMORY_BARRIER memory_barrier =
590 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Tony Barboure2c58df2014-11-25 13:18:32 -0700591
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000592 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
593 XGL_PIPELINE_BARRIER pipeline_barrier = {};
594 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
595 pipeline_barrier.eventCount = 1;
596 pipeline_barrier.pEvents = set_events;
597 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
598 pipeline_barrier.memBarrierCount = 1;
599 pipeline_barrier.pMemBarriers = &memory_barrier;
600
601 // write barrier to the command buffer
602 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700603
604 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700605 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700606 ASSERT_XGL_SUCCESS(err);
607
608 XGL_UINT32 numMemRefs=1;
609 XGL_MEMORY_REF memRefs;
610 // this command buffer only uses the vertex buffer memory
611 memRefs.flags = 0;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800612 memRefs.mem = memories()[0];
Tony Barboure2c58df2014-11-25 13:18:32 -0700613
614 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700615 XGL_CMD_BUFFER bufferArray[1];
616 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800617 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700618 ASSERT_XGL_SUCCESS(err);
619}
620
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700621XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
622 : XglConstantBufferObj(device)
623{
624
625}
626
627void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
628{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700629 XGL_FORMAT viewFormat;
630
631 m_numVertices = numIndexes;
632 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700633 switch (indexType) {
634 case XGL_INDEX_8:
635 m_stride = 1;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700636 viewFormat = XGL_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700637 break;
638 case XGL_INDEX_16:
639 m_stride = 2;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700640 viewFormat = XGL_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700641 break;
642 case XGL_INDEX_32:
643 m_stride = 4;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700644 viewFormat = XGL_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700645 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800646 default:
647 assert(!"unknown index type");
648 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700649 }
650
Chia-I Wua07fee62014-12-28 15:26:08 +0800651 const size_t allocationSize = numIndexes * m_stride;
652 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700653
Chia-I Wua07fee62014-12-28 15:26:08 +0800654 void *pData = map();
655 memcpy(pData, data, allocationSize);
656 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700657
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800658 // set up the buffer view for the constant buffer
659 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
660 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
661 view_info.buffer = obj();
662 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
663 view_info.stride = m_stride;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700664 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800665 view_info.channels.r = XGL_CHANNEL_SWIZZLE_R;
666 view_info.channels.g = XGL_CHANNEL_SWIZZLE_G;
667 view_info.channels.b = XGL_CHANNEL_SWIZZLE_B;
668 view_info.channels.a = XGL_CHANNEL_SWIZZLE_A;
669 view_info.offset = 0;
670 view_info.range = allocationSize;
671 m_bufferView.init(*m_device, view_info);
672
673 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
674 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700675}
676
677void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
678{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800679 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700680}
Tony Barboure2c58df2014-11-25 13:18:32 -0700681
Tony Barbouraf1f9192014-12-17 10:57:58 -0700682XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
683{
684 return m_indexType;
685}
686
Chia-I Wu11078b02015-01-04 16:27:24 +0800687XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700688{
Tony Barboure2c58df2014-11-25 13:18:32 -0700689 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
690 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
691 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800692 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700693 stageInfo->shader.linkConstBufferCount = 0;
694 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700695
Tony Barboure2c58df2014-11-25 13:18:32 -0700696 return stageInfo;
697}
698
Tony Barboure2c58df2014-11-25 13:18:32 -0700699XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
700{
701 XGL_RESULT err = XGL_SUCCESS;
702 std::vector<unsigned int> bil;
703 XGL_SHADER_CREATE_INFO createInfo;
704 size_t shader_len;
705
706 m_stage = stage;
707 m_device = device;
708
709 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
710 createInfo.pNext = NULL;
711
712 if (!framework->m_use_bil) {
713
714 shader_len = strlen(shader_code);
715 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
716 createInfo.pCode = malloc(createInfo.codeSize);
717 createInfo.flags = 0;
718
719 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
720 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
721 ((uint32_t *) createInfo.pCode)[1] = 0;
722 ((uint32_t *) createInfo.pCode)[2] = stage;
723 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
724
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800725 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700726 }
727
728 if (framework->m_use_bil || err) {
729 std::vector<unsigned int> bil;
730 err = XGL_SUCCESS;
731
732 // Use Reference GLSL to BIL compiler
733 framework->GLSLtoBIL(stage, shader_code, bil);
734 createInfo.pCode = bil.data();
735 createInfo.codeSize = bil.size() * sizeof(unsigned int);
736 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700737
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800738 init(*m_device, createInfo);
739 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700740}
Tony Barbour82c39522014-12-04 14:33:33 -0700741
Tony Barboure2c58df2014-11-25 13:18:32 -0700742XglPipelineObj::XglPipelineObj(XglDevice *device)
743{
Tony Barboure2c58df2014-11-25 13:18:32 -0700744 m_device = device;
745 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
746 m_vertexBufferCount = 0;
747
748 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
749 m_ia_state.pNext = XGL_NULL_HANDLE;
750 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
751 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700752 m_ia_state.primitiveRestartEnable = XGL_FALSE;
753 m_ia_state.primitiveRestartIndex = 0;
754
755 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
756 m_rs_state.pNext = &m_ia_state;
757 m_rs_state.depthClipEnable = XGL_FALSE;
758 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -0700759 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
760 m_rs_state.fillMode = XGL_FILL_SOLID;
761 m_rs_state.cullMode = XGL_CULL_NONE;
762 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700763
Tony Barboure2c58df2014-11-25 13:18:32 -0700764 memset(&m_cb_state,0,sizeof(m_cb_state));
765 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
766 m_cb_state.pNext = &m_rs_state;
767 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
768 m_cb_state.dualSourceBlendEnable = XGL_FALSE;
769 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
770
Tony Barbourf52346d2015-01-16 14:27:35 -0700771 m_ms_state.pNext = &m_cb_state;
772 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
773 m_ms_state.multisampleEnable = XGL_FALSE;
774 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
775 m_ms_state.samples = 1;
776 m_ms_state.minSampleShading = 0;
777 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700778
Tony Barbourf52346d2015-01-16 14:27:35 -0700779 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
780 m_ds_state.pNext = &m_ms_state,
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700781 m_ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700782 m_ds_state.depthTestEnable = XGL_FALSE;
783 m_ds_state.depthWriteEnable = XGL_FALSE;
784 m_ds_state.depthBoundsEnable = XGL_FALSE;
785 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
786 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
787 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
788 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
789 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
790 m_ds_state.stencilTestEnable = XGL_FALSE;
791 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -0700792
Tony Barbourf52346d2015-01-16 14:27:35 -0700793 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
794 att.blendEnable = XGL_FALSE;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700795 att.format = XGL_FMT_R8G8B8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -0700796 att.channelWriteMask = 0xf;
797 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -0700798
799};
800
801void XglPipelineObj::AddShader(XglShaderObj* shader)
802{
803 m_shaderObjs.push_back(shader);
804}
805
806void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
807{
808 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
809 m_vi_state.attributeCount = count;
810}
811
812void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
813{
814 m_vi_state.pVertexBindingDescriptions = vi_binding;
815 m_vi_state.bindingCount = count;
816}
817
818void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
819{
820 m_vertexBufferObjs.push_back(vertexDataBuffer);
821 m_vertexBufferBindings.push_back(binding);
822 m_vertexBufferCount++;
823}
824
Tony Barbourf52346d2015-01-16 14:27:35 -0700825void XglPipelineObj::AddColorAttachment(XGL_UINT binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +0800826{
Tony Barbourf52346d2015-01-16 14:27:35 -0700827 if (binding+1 > m_colorAttachments.size())
828 {
829 m_colorAttachments.resize(binding+1);
830 }
831 m_colorAttachments[binding] = *att;
832}
833
834void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
835{
836 m_ds_state.format = ds_state->format;
837 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
838 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
839 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
840 m_ds_state.depthFunc = ds_state->depthFunc;
841 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
842 m_ds_state.back = ds_state->back;
843 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +0800844}
845
Tony Barbour976e1cf2014-12-17 11:57:31 -0700846void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
847{
Tony Barbourf52346d2015-01-16 14:27:35 -0700848 XGL_VOID* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -0700849 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
850
851 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
852
853 for (int i=0; i<m_shaderObjs.size(); i++)
854 {
Chia-I Wu11078b02015-01-04 16:27:24 +0800855 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700856 shaderCreateInfo->pNext = head_ptr;
857 head_ptr = shaderCreateInfo;
858 }
859
860 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
861 {
862 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
863 m_vi_state.pNext = head_ptr;
864 head_ptr = &m_vi_state;
865 }
866
867 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
868 info.pNext = head_ptr;
869 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +0800870 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700871
Tony Barbourf52346d2015-01-16 14:27:35 -0700872 m_cb_state.attachmentCount = m_colorAttachments.size();
873 m_cb_state.pAttachments = &m_colorAttachments[0];
874
Chia-I Wu2648d092014-12-29 14:24:14 +0800875 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -0700876}
Chia-I Wu2648d092014-12-29 14:24:14 +0800877
Tony Barbour976e1cf2014-12-17 11:57:31 -0700878XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
879{
Chia-I Wu2648d092014-12-29 14:24:14 +0800880 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700881}
882
Tony Barbour5420af02014-12-03 13:58:15 -0700883void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700884{
Tony Barbourf52346d2015-01-16 14:27:35 -0700885 XGL_VOID* head_ptr = &m_ds_state;
Tony Barboure2c58df2014-11-25 13:18:32 -0700886 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
887
888 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700889
890 for (int i=0; i<m_shaderObjs.size(); i++)
891 {
Chia-I Wu11078b02015-01-04 16:27:24 +0800892 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barboure2c58df2014-11-25 13:18:32 -0700893 shaderCreateInfo->pNext = head_ptr;
894 head_ptr = shaderCreateInfo;
895 }
896
897 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
898 {
899 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
900 m_vi_state.pNext = head_ptr;
901 head_ptr = &m_vi_state;
902 }
903
904 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
905 info.pNext = head_ptr;
906 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +0800907 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barboure2c58df2014-11-25 13:18:32 -0700908
Chia-I Wu2648d092014-12-29 14:24:14 +0800909 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700910
Chia-I Wu2648d092014-12-29 14:24:14 +0800911 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700912
913
914 for (int i=0; i < m_vertexBufferCount; i++)
915 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800916 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700917 }
Tony Barboure2c58df2014-11-25 13:18:32 -0700918}
Tony Barbour82c39522014-12-04 14:33:33 -0700919
Tony Barboure2c58df2014-11-25 13:18:32 -0700920XglMemoryRefManager::XglMemoryRefManager() {
921
922}
Tony Barbour82c39522014-12-04 14:33:33 -0700923
Tony Barboure2c58df2014-11-25 13:18:32 -0700924void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800925 const std::vector<XGL_GPU_MEMORY> mems = constantBuffer->memories();
926 if (!mems.empty())
927 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700928}
Tony Barbour82c39522014-12-04 14:33:33 -0700929
Tony Barboure2c58df2014-11-25 13:18:32 -0700930void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800931 const std::vector<XGL_GPU_MEMORY> mems = texture->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 -0700936XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
937
938 XGL_MEMORY_REF *localRefs;
939 XGL_UINT32 numRefs=m_bufferObjs.size();
940
941 if (numRefs <= 0)
942 return NULL;
943
944 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
945 for (int i=0; i<numRefs; i++)
946 {
947 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +0800948 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -0700949 }
950 return localRefs;
951}
952int XglMemoryRefManager::GetNumRefs() {
953 return m_bufferObjs.size();
954}
Tony Barbour6d047bf2014-12-10 14:34:45 -0700955
956XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Chia-I Wud28343c2014-12-28 15:12:48 +0800957 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(XGL_QUEUE_TYPE_GRAPHICS))
Tony Barbour6d047bf2014-12-10 14:34:45 -0700958{
Tony Barbour6d047bf2014-12-10 14:34:45 -0700959 m_device = device;
Chia-I Wud28343c2014-12-28 15:12:48 +0800960 m_renderTargetCount = 0;
Tony Barbour6d047bf2014-12-10 14:34:45 -0700961}
Tony Barbour471338d2014-12-10 17:28:39 -0700962
Tony Barbour6d047bf2014-12-10 14:34:45 -0700963XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
964{
Chia-I Wud28343c2014-12-28 15:12:48 +0800965 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -0700966}
Tony Barbour471338d2014-12-10 17:28:39 -0700967
Jon Ashburnc4164b12014-12-31 17:10:47 -0700968XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -0700969{
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700970 begin(pInfo);
971 return XGL_SUCCESS;
972}
973
974XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj)
975{
976 begin(renderpass_obj);
977 return XGL_SUCCESS;
978}
979
980XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
981{
982 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +0800983 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -0700984}
985
986XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
987{
Chia-I Wud28343c2014-12-28 15:12:48 +0800988 end();
989 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -0700990}
991
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000992void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -0700993{
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000994 xglCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -0700995}
996
Tony Barbour30cc9e82014-12-17 11:53:55 -0700997void XglCommandBufferObj::ClearAllBuffers(XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
998{
999 XGL_UINT i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001000 const XGL_FLAGS output_mask =
1001 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1002 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1003 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1004 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1005 XGL_MEMORY_OUTPUT_COPY_BIT;
1006 const XGL_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001007
1008 // whatever we want to do, we do it to the whole buffer
1009 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1010 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1011 srRange.baseMipLevel = 0;
1012 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1013 srRange.baseArraySlice = 0;
1014 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1015
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001016 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1017 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1018 memory_barrier.outputMask = output_mask;
1019 memory_barrier.inputMask = input_mask;
1020 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1021 memory_barrier.subresourceRange = srRange;
1022
1023 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1024 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1025 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1026 pipeline_barrier.eventCount = 1;
1027 pipeline_barrier.pEvents = set_events;
1028 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1029 pipeline_barrier.memBarrierCount = 1;
1030 pipeline_barrier.pMemBarriers = &memory_barrier;
1031
Tony Barbour30cc9e82014-12-17 11:53:55 -07001032 // clear the back buffer to dark grey
1033 XGL_UINT clearColor[4] = {64, 64, 64, 0};
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001034
Tony Barbour30cc9e82014-12-17 11:53:55 -07001035 for (i = 0; i < m_renderTargetCount; i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001036 memory_barrier.image = m_renderTargets[i]->image();
1037 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1038 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1039 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001040
Chia-I Wud28343c2014-12-28 15:12:48 +08001041 xglCmdClearColorImageRaw( obj(), m_renderTargets[i]->image(), clearColor, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001042 }
1043
1044 if (depthStencilImage)
1045 {
1046 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1047 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1048 dsRange.baseMipLevel = 0;
1049 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1050 dsRange.baseArraySlice = 0;
1051 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1052
1053 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001054
1055 memory_barrier.oldLayout = depthStencilBinding->layout;
1056 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1057 memory_barrier.image = depthStencilImage;
1058 memory_barrier.subresourceRange = dsRange;
1059
1060 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1061 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001062
Chia-I Wud28343c2014-12-28 15:12:48 +08001063 xglCmdClearDepthStencil(obj(), depthStencilImage, 1.0f, 0, 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001064
1065 // prepare depth buffer for rendering
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001066 memory_barrier.image = depthStencilImage;
1067 memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1068 memory_barrier.newLayout = depthStencilBinding->layout;
1069 memory_barrier.subresourceRange = dsRange;
1070 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1071 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001072 }
1073}
1074
Jon Ashburncdc40be2015-01-02 18:27:14 -07001075void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001076{
1077 XGL_UINT i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001078 const XGL_FLAGS output_mask =
1079 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1080 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1081 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1082 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1083 XGL_MEMORY_OUTPUT_COPY_BIT;
1084 const XGL_FLAGS input_mask =
1085 XGL_MEMORY_INPUT_CPU_READ_BIT |
1086 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1087 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
1088 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1089 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
1090 XGL_MEMORY_INPUT_SHADER_READ_BIT |
1091 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1092 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1093 XGL_MEMORY_INPUT_COPY_BIT;
1094
Tony Barbour30cc9e82014-12-17 11:53:55 -07001095 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1096 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1097 srRange.baseMipLevel = 0;
1098 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1099 srRange.baseArraySlice = 0;
1100 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1101
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001102 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1103 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1104 memory_barrier.outputMask = output_mask;
1105 memory_barrier.inputMask = input_mask;
1106 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1107 memory_barrier.subresourceRange = srRange;
1108
1109 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1110 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1111 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1112 pipeline_barrier.eventCount = 1;
1113 pipeline_barrier.pEvents = set_events;
1114 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1115 pipeline_barrier.memBarrierCount = 1;
1116 pipeline_barrier.pMemBarriers = &memory_barrier;
1117
Tony Barbour30cc9e82014-12-17 11:53:55 -07001118 for(i=0; i<m_renderTargetCount; i++)
1119 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001120 memory_barrier.image = m_renderTargets[i]->image();
1121 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1122 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1123 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001124 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001125}
1126
Tony Barbourf52346d2015-01-16 14:27:35 -07001127void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001128{
Tony Barbourf52346d2015-01-16 14:27:35 -07001129 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001130}
1131
1132void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1133{
1134 m_renderTargets.push_back(renderTarget);
1135 m_renderTargetCount++;
1136}
1137
1138void XglCommandBufferObj::DrawIndexed(XGL_UINT firstIndex, XGL_UINT indexCount, XGL_INT vertexOffset, XGL_UINT firstInstance, XGL_UINT instanceCount)
1139{
Chia-I Wud28343c2014-12-28 15:12:48 +08001140 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001141}
1142
1143void XglCommandBufferObj::Draw(XGL_UINT firstVertex, XGL_UINT vertexCount, XGL_UINT firstInstance, XGL_UINT instanceCount)
1144{
Chia-I Wud28343c2014-12-28 15:12:48 +08001145 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001146}
1147
1148void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs)
1149{
1150 XGL_RESULT err = XGL_SUCCESS;
1151
1152 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001153 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001154 ASSERT_XGL_SUCCESS( err );
1155
1156 err = xglQueueWaitIdle( m_device->m_queue );
1157 ASSERT_XGL_SUCCESS( err );
1158
1159 // Wait for work to finish before cleaning up.
1160 xglDeviceWaitIdle(m_device->device());
1161
1162}
1163void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1164{
Chia-I Wud28343c2014-12-28 15:12:48 +08001165 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001166}
1167
1168void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1169{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001170 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wu11078b02015-01-04 16:27:24 +08001171 xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001172}
1173void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, XGL_UINT offset)
1174{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001175 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001176}
1177void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, XGL_UINT offset, XGL_UINT binding)
1178{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001179 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001180}