blob: 1eedd3cb12ce9a1c762e165f606d1e40dd31029c [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
2 * XGL Tests
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
28#include "xglrenderframework.h"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060030XglRenderFramework::XglRenderFramework() :
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060031 m_cmdBuffer( XGL_NULL_HANDLE ),
Chia-I Wu837f9952014-12-15 23:29:34 +080032 m_stateRaster( XGL_NULL_HANDLE ),
33 m_colorBlend( XGL_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060034 m_stateViewport( XGL_NULL_HANDLE ),
Chia-I Wu837f9952014-12-15 23:29:34 +080035 m_stateDepthStencil( XGL_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060036 m_width( 256.0 ), // default window width
37 m_height( 256.0 ) // default window height
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060038{
Chia-I Wuecebf752014-12-05 10:45:15 +080039 m_renderTargetCount = 1;
40
Tony Barboura53a6942015-02-25 11:25:11 -070041 m_render_target_fmt = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060042
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060043 m_depthStencilBinding.view = XGL_NULL_HANDLE;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060044}
45
46XglRenderFramework::~XglRenderFramework()
47{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060048
49}
50
51void XglRenderFramework::InitFramework()
52{
53 XGL_RESULT err;
54
Jon Ashburn1e464892015-01-29 15:48:00 -070055 err = xglCreateInstance(&app_info, NULL, &this->inst);
56 ASSERT_XGL_SUCCESS(err);
57 err = xglEnumerateGpus(inst, XGL_MAX_PHYSICAL_GPUS, &this->gpu_count,
58 objs);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060059 ASSERT_XGL_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070060 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060061
62 m_device = new XglDevice(0, objs[0]);
63 m_device->get_device_queue();
64}
65
66void XglRenderFramework::ShutdownFramework()
67{
68 if (m_colorBlend) xglDestroyObject(m_colorBlend);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060069 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
70 if (m_stateRaster) xglDestroyObject(m_stateRaster);
71 if (m_cmdBuffer) xglDestroyObject(m_cmdBuffer);
Tobin Ehlis976fc162015-03-26 08:23:25 -060072 if (m_frameBuffer) xglDestroyObject(m_frameBuffer);
73 if (m_renderPass) xglDestroyObject(m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060074
75 if (m_stateViewport) {
76 xglDestroyObject(m_stateViewport);
77 }
Tobin Ehlis976fc162015-03-26 08:23:25 -060078 while (!m_renderTargets.empty()) {
79 xglDestroyObject(m_renderTargets.back()->targetView());
80 xglBindObjectMemory(m_renderTargets.back()->image(), 0, XGL_NULL_HANDLE, 0);
81 xglDestroyObject(m_renderTargets.back()->image());
82 xglFreeMemory(m_renderTargets.back()->memory());
83 m_renderTargets.pop_back();
84 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060085
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060086 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +080087 delete m_device;
Jon Ashburn1e464892015-01-29 15:48:00 -070088 xglDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060089}
90
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060091void XglRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060092{
93 XGL_RESULT err;
94
Tony Barboura53a6942015-02-25 11:25:11 -070095 m_render_target_fmt = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060096
97 // create a raster state (solid, back-face culling)
Tony Barbourf52346d2015-01-16 14:27:35 -070098 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster = {};
99 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
100 raster.pointSize = 1.0;
101
102 err = xglCreateDynamicRasterState( device(), &raster, &m_stateRaster );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600103 ASSERT_XGL_SUCCESS(err);
104
Tony Barbourf52346d2015-01-16 14:27:35 -0700105 XGL_DYNAMIC_CB_STATE_CREATE_INFO blend = {};
106 blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
107 err = xglCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600108 ASSERT_XGL_SUCCESS( err );
109
Tony Barbourf52346d2015-01-16 14:27:35 -0700110 XGL_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {};
111 depthStencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600112 depthStencil.minDepth = 0.f;
113 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700114 depthStencil.stencilFrontRef = 0;
115 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600116
Tony Barbourf52346d2015-01-16 14:27:35 -0700117 err = xglCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600118 ASSERT_XGL_SUCCESS( err );
119
120 XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {};
121
122 cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
123 cmdInfo.queueType = XGL_QUEUE_TYPE_GRAPHICS;
124 err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
125 ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed";
126}
127
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600128void XglRenderFramework::InitViewport(float width, float height)
129{
130 XGL_RESULT err;
131
Tony Barbourf52346d2015-01-16 14:27:35 -0700132 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700133 XGL_RECT scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600134
Tony Barbourf52346d2015-01-16 14:27:35 -0700135 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewportCreate = {};
136 viewportCreate.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700137 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700138 viewport.originX = 0;
139 viewport.originY = 0;
140 viewport.width = 1.f * width;
141 viewport.height = 1.f * height;
142 viewport.minDepth = 0.f;
143 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700144 scissor.extent.width = width;
145 scissor.extent.height = height;
146 scissor.offset.x = 0;
147 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700148 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700149 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700150
151 err = xglCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600152 ASSERT_XGL_SUCCESS( err );
153 m_width = width;
154 m_height = height;
155}
156
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600157void XglRenderFramework::InitViewport()
158{
159 InitViewport(m_width, m_height);
160}
161
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600162void XglRenderFramework::InitRenderTarget()
163{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600164 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800165
166 for (i = 0; i < m_renderTargetCount; i++) {
Chia-I Wuf50ee212014-12-29 14:31:52 +0800167 XglImage *img = new XglImage(m_device);
168 img->init(m_width, m_height, m_render_target_fmt,
169 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
170 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700171 m_colorBindings[i].view = img->targetView();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000172 m_colorBindings[i].layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700173 m_renderTargets.push_back(img);
Chia-I Wuecebf752014-12-05 10:45:15 +0800174 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700175 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
176 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_LOAD;
177 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_STORE;
Tobin Ehlis5de72572015-03-06 13:16:37 -0700178 XGL_CLEAR_COLOR clear_color;
179 clear_color.color.rawColor[0] = 64;
180 clear_color.color.rawColor[1] = 64;
181 clear_color.color.rawColor[2] = 64;
182 clear_color.color.rawColor[3] = 0;
183 clear_color.useRawValue = XGL_TRUE;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700184 XGL_DEPTH_STENCIL_BIND_INFO *dsBinding;
185 if (m_depthStencilBinding.view)
186 dsBinding = &m_depthStencilBinding;
187 else
188 dsBinding = NULL;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600189 XGL_FRAMEBUFFER_CREATE_INFO fb_info = {};
190 fb_info.sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
191 fb_info.pNext = NULL;
192 fb_info.colorAttachmentCount = m_renderTargetCount;
193 fb_info.pColorAttachments = m_colorBindings;
194 fb_info.pDepthStencilAttachment = dsBinding;
195 fb_info.sampleCount = 1;
196 fb_info.width = (uint32_t)m_width;
197 fb_info.height = (uint32_t)m_height;
198 fb_info.layers = 1;
199
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700200 XGL_RENDER_PASS_CREATE_INFO rp_info;
201 memset(&rp_info, 0 , sizeof(rp_info));
Tobin Ehlis976fc162015-03-26 08:23:25 -0600202 xglCreateFramebuffer(device(), &fb_info, &(m_frameBuffer));
203 rp_info.framebuffer = m_frameBuffer;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700204 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
205 rp_info.renderArea.extent.width = m_width;
206 rp_info.renderArea.extent.height = m_height;
Courtney Goeltzenleuchter3db6d9b2015-02-10 14:06:25 -0700207 rp_info.colorAttachmentCount = m_renderTargetCount;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700208 rp_info.pColorLoadOps = &load_op;
209 rp_info.pColorStoreOps = &store_op;
Tobin Ehlis5de72572015-03-06 13:16:37 -0700210 rp_info.pColorLoadClearValues = &clear_color;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700211 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
212 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
213 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
214 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
215 xglCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600216}
217
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600218
219
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600220XglDevice::XglDevice(uint32_t id, XGL_PHYSICAL_GPU obj) :
Chia-I Wufb1459b2014-12-29 15:23:20 +0800221 xgl_testing::Device(obj), id(id)
222{
223 init();
224
225 props = gpu().properties();
226 queue_props = &gpu().queue_properties()[0];
227}
228
229void XglDevice::get_device_queue()
230{
231 ASSERT_NE(true, graphics_queues().empty());
232 m_queue = graphics_queues()[0]->obj();
233}
234
Tony Barboure2c58df2014-11-25 13:18:32 -0700235XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
236{
237 m_device = device;
238 m_nextSlot = 0;
239
240}
241
Chia-I Wu11078b02015-01-04 16:27:24 +0800242XglDescriptorSetObj::~XglDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700243{
Chia-I Wu11078b02015-01-04 16:27:24 +0800244 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700245}
Tony Barbour82c39522014-12-04 14:33:33 -0700246
Chia-I Wu11078b02015-01-04 16:27:24 +0800247int XglDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700248{
Chia-I Wu11078b02015-01-04 16:27:24 +0800249 /* request a descriptor but do not update it */
250 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
251 tc.type = XGL_DESCRIPTOR_TYPE_RAW_BUFFER;
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 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700256}
Tony Barbour82c39522014-12-04 14:33:33 -0700257
Chia-I Wu11078b02015-01-04 16:27:24 +0800258int XglDescriptorSetObj::AppendBuffer(XGL_DESCRIPTOR_TYPE type, XglConstantBufferObj *constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700259{
Chia-I Wu11078b02015-01-04 16:27:24 +0800260 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
261 tc.type = type;
262 tc.count = 1;
263 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700264
Chia-I Wu11078b02015-01-04 16:27:24 +0800265 m_bufferInfo.push_back(&constantBuffer->m_bufferViewInfo);
266
267 m_updateBuffers.push_back(xgl_testing::DescriptorSet::update(type, m_nextSlot, 1,
268 (const XGL_BUFFER_VIEW_ATTACH_INFO **) NULL));
269
270 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700271}
Tony Barbour82c39522014-12-04 14:33:33 -0700272
Chia-I Wu11078b02015-01-04 16:27:24 +0800273int XglDescriptorSetObj::AppendSamplerTexture( XglSamplerObj* sampler, XglTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700274{
Chia-I Wu11078b02015-01-04 16:27:24 +0800275 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
276 tc.type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE;
277 tc.count = 1;
278 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700279
Chia-I Wu11078b02015-01-04 16:27:24 +0800280 XGL_SAMPLER_IMAGE_VIEW_INFO tmp = {};
281 tmp.pSampler = sampler->obj();
282 tmp.pImageView = &texture->m_textureViewInfo;
283 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700284
Chia-I Wu11078b02015-01-04 16:27:24 +0800285 m_updateSamplerTextures.push_back(xgl_testing::DescriptorSet::update(m_nextSlot, 1,
286 (const XGL_SAMPLER_IMAGE_VIEW_INFO *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700287
Chia-I Wu11078b02015-01-04 16:27:24 +0800288 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700289}
Chia-I Wu11078b02015-01-04 16:27:24 +0800290
291XGL_DESCRIPTOR_SET_LAYOUT XglDescriptorSetObj::GetLayout()
Tony Barbourb5f4d082014-12-17 10:54:03 -0700292{
Chia-I Wu11078b02015-01-04 16:27:24 +0800293 return m_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700294}
295
296XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle()
297{
Chia-I Wu11078b02015-01-04 16:27:24 +0800298 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700299}
Tony Barboure2c58df2014-11-25 13:18:32 -0700300
Chia-I Wu11078b02015-01-04 16:27:24 +0800301void XglDescriptorSetObj::CreateXGLDescriptorSet(XglCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700302{
Chia-I Wu11078b02015-01-04 16:27:24 +0800303 // create XGL_DESCRIPTOR_REGION
304 XGL_DESCRIPTOR_REGION_CREATE_INFO region = {};
305 region.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO;
306 region.count = m_type_counts.size();
307 region.pTypeCount = &m_type_counts[0];
308 init(*m_device, XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1, region);
Tony Barbour824b7712014-12-18 17:06:21 -0700309
Chia-I Wu11078b02015-01-04 16:27:24 +0800310 // create XGL_DESCRIPTOR_SET_LAYOUT
311 vector<XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO> layout;
312 layout.resize(m_type_counts.size());
313 for (int i = 0; i < m_type_counts.size(); i++) {
314 layout[i].sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
315 layout[i].descriptorType = m_type_counts[i].type;
316 layout[i].count = m_type_counts[i].count;
317 layout[i].stageFlags = XGL_SHADER_STAGE_FLAGS_ALL;
318 layout[i].immutableSampler = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700319
Chia-I Wu11078b02015-01-04 16:27:24 +0800320 if (i < m_type_counts.size() - 1)
321 layout[i].pNext = &layout[i + 1];
322 else
323 layout[i].pNext = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700324 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800325
Chia-I Wu11078b02015-01-04 16:27:24 +0800326 m_layout.init(*m_device, 0, layout[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700327
Chia-I Wu11078b02015-01-04 16:27:24 +0800328 // create XGL_DESCRIPTOR_SET
329 m_set = alloc_sets(XGL_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
330
331 // build the update chain
332 for (int i = 0; i < m_updateBuffers.size(); i++) {
333 m_updateBuffers[i].pBufferViews = &m_bufferInfo[i];
334
335 if (i < m_updateBuffers.size() - 1)
336 m_updateBuffers[i].pNext = &m_updateBuffers[i + 1];
337 else if (m_updateSamplerTextures.empty())
338 m_updateBuffers[i].pNext = NULL;
339 else
340 m_updateBuffers[i].pNext = &m_updateSamplerTextures[0];
341 }
342 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
343 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
344
345 if (i < m_updateSamplerTextures.size() - 1)
346 m_updateSamplerTextures[i].pNext = &m_updateSamplerTextures[i + 1];
347 else
348 m_updateSamplerTextures[i].pNext = NULL;
349 }
350 const void *chain = (!m_updateBuffers.empty()) ? (const void *) &m_updateBuffers[0] :
351 (!m_updateSamplerTextures.empty()) ? (const void *) &m_updateSamplerTextures[0] :
352 NULL;
353
354 // do the updates
355 m_device->begin_descriptor_region_update(XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
356 clear_sets(*m_set);
357 m_set->update(chain);
358 m_device->end_descriptor_region_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700359}
Tony Barboure2c58df2014-11-25 13:18:32 -0700360
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800361XglImage::XglImage(XglDevice *dev)
362{
363 m_device = dev;
364 m_imageInfo.view = XGL_NULL_HANDLE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000365 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800366}
367
Tony Barbour579f7802015-04-03 15:11:43 -0600368static bool IsCompatible(XGL_FLAGS usage, XGL_FLAGS features)
369{
370 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) &&
371 !(features & XGL_FORMAT_IMAGE_SHADER_READ_BIT))
372 return false;
373 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT) &&
374 !(features & XGL_FORMAT_IMAGE_SHADER_WRITE_BIT))
375 return false;
376 return true;
377}
378
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600379void XglImage::init(uint32_t w, uint32_t h,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800380 XGL_FORMAT fmt, XGL_FLAGS usage,
Tony Barbour579f7802015-04-03 15:11:43 -0600381 XGL_IMAGE_TILING requested_tiling)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800382{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600383 uint32_t mipCount;
Tony Barbour579f7802015-04-03 15:11:43 -0600384 XGL_FORMAT_PROPERTIES image_fmt;
385 XGL_IMAGE_TILING tiling;
386 XGL_RESULT err;
387 size_t size;
388
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800389 mipCount = 0;
390
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600391 uint32_t _w = w;
392 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800393 while( ( _w > 0 ) || ( _h > 0 ) )
394 {
395 _w >>= 1;
396 _h >>= 1;
397 mipCount++;
398 }
399
Tony Barbour579f7802015-04-03 15:11:43 -0600400 size = sizeof(image_fmt);
401 err = xglGetFormatInfo(m_device->obj(), fmt,
402 XGL_INFO_TYPE_FORMAT_PROPERTIES,
403 &size, &image_fmt);
404 ASSERT_XGL_SUCCESS(err);
405
406 if (requested_tiling == XGL_LINEAR_TILING) {
407 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
408 tiling = XGL_LINEAR_TILING;
409 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
410 tiling = XGL_OPTIMAL_TILING;
411 } else {
412 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
413 }
414 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
415 tiling = XGL_OPTIMAL_TILING;
416 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
417 tiling = XGL_LINEAR_TILING;
418 } else {
419 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
420 }
421
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800422 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
423 imageCreateInfo.imageType = XGL_IMAGE_2D;
424 imageCreateInfo.format = fmt;
425 imageCreateInfo.extent.width = w;
426 imageCreateInfo.extent.height = h;
427 imageCreateInfo.mipLevels = mipCount;
428 imageCreateInfo.tiling = tiling;
429
430 imageCreateInfo.usage = usage;
431
432 xgl_testing::Image::init(*m_device, imageCreateInfo);
433
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000434 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800435
436 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO createView = {
437 XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
438 XGL_NULL_HANDLE,
439 obj(),
Tony Barboura53a6942015-02-25 11:25:11 -0700440 XGL_FMT_B8G8R8A8_UNORM,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800441 0,
442 0,
443 1
444 };
445
446 m_targetView.init(*m_device, createView);
447}
448
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600449XGL_RESULT XglImage::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800450{
451 *ptr = map();
452 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
453}
454
455XGL_RESULT XglImage::UnmapMemory()
456{
457 unmap();
458 return XGL_SUCCESS;
459}
460
Tony Barbour5dc515d2015-04-01 17:47:06 -0600461XGL_RESULT XglImage::CopyImage(XglImage &fromImage)
462{
463 XGL_RESULT err;
464
465 XGL_CMD_BUFFER cmd_buf;
466 XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {};
467 cmd_buf_create_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
468 cmd_buf_create_info.pNext = NULL;
469 cmd_buf_create_info.queueType = XGL_QUEUE_TYPE_GRAPHICS;
470 cmd_buf_create_info.flags = 0;
471
472 err = xglCreateCommandBuffer(m_device->device(), &cmd_buf_create_info, &cmd_buf);
473 assert(!err);
474
475 /* Copy staging texture to usable texture */
476 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {};
477 cmd_buf_begin_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
478 cmd_buf_begin_info.pNext = NULL;
479 cmd_buf_begin_info.flags = 0;
480
481 err = xglResetCommandBuffer(cmd_buf);
482 assert(!err);
483
484 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_begin_info);
485 assert(!err);
486
487 XGL_IMAGE_COPY copy_region = {};
488 copy_region.srcSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
489 copy_region.srcSubresource.arraySlice = 0;
490 copy_region.srcSubresource.mipLevel = 0;
491 copy_region.srcOffset.x = 0;
492 copy_region.srcOffset.y = 0;
493 copy_region.srcOffset.z = 0;
494 copy_region.destSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
495 copy_region.destSubresource.arraySlice = 0;
496 copy_region.destSubresource.mipLevel = 0;
497 copy_region.destOffset.x = 0;
498 copy_region.destOffset.y = 0;
499 copy_region.destOffset.z = 0;
500 copy_region.extent = fromImage.extent();
501
502 xglCmdCopyImage(cmd_buf, fromImage.obj(), obj(), 1, &copy_region);
503
504 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {};
505 image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
506 image_memory_barrier.pNext = NULL;
507 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT;
508 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
509 image_memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_GENERAL;
510 image_memory_barrier.newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL;
511 image_memory_barrier.image = fromImage.obj();
512 image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
513 image_memory_barrier.subresourceRange.baseMipLevel = 0;
514 image_memory_barrier.subresourceRange.mipLevels = 1;
515 image_memory_barrier.subresourceRange.baseArraySlice = 0;
516 image_memory_barrier.subresourceRange.arraySize = 0;
517
518 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
519
520 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
521 XGL_PIPELINE_BARRIER pipeline_barrier;
522 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
523 pipeline_barrier.pNext = NULL;
524 pipeline_barrier.eventCount = 1;
525 pipeline_barrier.pEvents = set_events;
526 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
527 pipeline_barrier.memBarrierCount = 1;
528 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
529
530 // write barrier to the command buffer
531 xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier);
532
533 err = xglEndCommandBuffer(cmd_buf);
534 assert(!err);
535
536 const XGL_CMD_BUFFER cmd_bufs[] = { cmd_buf };
537 XGL_MEMORY_REF mem_refs[16];
538 uint32_t num_refs = 0;
539 const std::vector<XGL_GPU_MEMORY> from_mems = fromImage.memories();
540 const std::vector<XGL_GPU_MEMORY> to_mems = memories();
541
542 for (uint32_t j = 0; j < from_mems.size(); j++) {
543 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
544 mem_refs[num_refs].mem = from_mems[j];
545 num_refs++;
546 assert(num_refs < 16);
547 }
548
549 for (uint32_t j = 0; j < to_mems.size(); j++) {
550 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
551 mem_refs[num_refs].mem = to_mems[j];
552 num_refs++;
553 assert(num_refs < 16);
554 }
555
556 err = xglQueueSubmit(m_device->m_queue, 1, cmd_bufs,
557 num_refs, mem_refs, XGL_NULL_HANDLE);
558 assert(!err);
559
560 err = xglQueueWaitIdle(m_device->m_queue);
561 assert(!err);
562
563 xglDestroyObject(cmd_buf);
564
565 return XGL_SUCCESS;
566}
567
Tony Barbourebc093f2015-04-01 16:38:10 -0600568XglTextureObj::XglTextureObj(XglDevice *device, uint32_t *colors)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600569 :XglImage(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700570{
571 m_device = device;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700572 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600573 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600574 void *data;
575 int32_t x, y;
576 XglImage stagingImage(device);
577
578 stagingImage.init(16, 16, tex_format, 0, XGL_LINEAR_TILING);
579 XGL_SUBRESOURCE_LAYOUT layout = stagingImage.subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600580
581 if (colors == NULL)
582 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700583
584 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
585
586 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
587
Tony Barbour5dc515d2015-04-01 17:47:06 -0600588 XGL_IMAGE_VIEW_CREATE_INFO view = {};
Tony Barbourbdf0a312015-04-01 17:10:07 -0600589 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
590 view.pNext = NULL;
591 view.image = XGL_NULL_HANDLE;
592 view.viewType = XGL_IMAGE_VIEW_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600593 view.format = tex_format;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600594 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
595 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
596 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
597 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
598 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
599 view.subresourceRange.baseMipLevel = 0;
600 view.subresourceRange.mipLevels = 1;
601 view.subresourceRange.baseArraySlice = 0;
602 view.subresourceRange.arraySize = 1;
603 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700604
Tony Barboure2c58df2014-11-25 13:18:32 -0700605 /* create image */
Tony Barbour5dc515d2015-04-01 17:47:06 -0600606 init(16, 16, tex_format, XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, XGL_OPTIMAL_TILING);
Tony Barboure2c58df2014-11-25 13:18:32 -0700607
608 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800609 view.image = obj();
610 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600611 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700612
Tony Barbour5dc515d2015-04-01 17:47:06 -0600613 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700614
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800615 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700616 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800617 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600618 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700619 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600620 stagingImage.unmap();
621 XglImage::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700622}
Tony Barbour82c39522014-12-04 14:33:33 -0700623
Tony Barboure2c58df2014-11-25 13:18:32 -0700624XglSamplerObj::XglSamplerObj(XglDevice *device)
625{
Tony Barboure2c58df2014-11-25 13:18:32 -0700626 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700627
Chia-I Wue9864b52014-12-28 16:32:24 +0800628 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
629 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
630 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
631 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
632 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
633 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
634 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
635 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
636 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
637 samplerCreateInfo.mipLodBias = 0.0;
638 samplerCreateInfo.maxAnisotropy = 0.0;
639 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
640 samplerCreateInfo.minLod = 0.0;
641 samplerCreateInfo.maxLod = 0.0;
642 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700643
Chia-I Wue9864b52014-12-28 16:32:24 +0800644 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700645}
Tony Barboure2c58df2014-11-25 13:18:32 -0700646
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700647/*
648 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
649 */
650XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
651{
652 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700653 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700654
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800655 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700656}
657
Tony Barboure2c58df2014-11-25 13:18:32 -0700658XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
659{
Tony Barboure2c58df2014-11-25 13:18:32 -0700660 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800661 m_commandBuffer = 0;
662
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800663 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700664 m_numVertices = constantCount;
665 m_stride = constantSize;
666
Chia-I Wua07fee62014-12-28 15:26:08 +0800667 const size_t allocationSize = constantCount * constantSize;
668 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700669
Chia-I Wua07fee62014-12-28 15:26:08 +0800670 void *pData = map();
671 memcpy(pData, data, allocationSize);
672 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700673
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800674 // set up the buffer view for the constant buffer
675 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
676 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
677 view_info.buffer = obj();
Chia-I Wubb0c8d22015-01-16 22:31:25 +0800678 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800679 view_info.offset = 0;
680 view_info.range = allocationSize;
681 m_bufferView.init(*m_device, view_info);
682
683 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
684 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700685}
Tony Barbour82c39522014-12-04 14:33:33 -0700686
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600687void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700688{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800689 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700690}
691
692
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000693void XglConstantBufferObj::BufferMemoryBarrier(
694 XGL_FLAGS outputMask /*=
695 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
696 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
697 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
698 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
699 XGL_MEMORY_OUTPUT_COPY_BIT*/,
700 XGL_FLAGS inputMask /*=
701 XGL_MEMORY_INPUT_CPU_READ_BIT |
702 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
703 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
704 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
705 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
706 XGL_MEMORY_INPUT_SHADER_READ_BIT |
707 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
708 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
709 XGL_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700710{
Tony Barbour38422802014-12-10 14:36:31 -0700711 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700712
Tony Barbour38422802014-12-10 14:36:31 -0700713 if (!m_commandBuffer)
714 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800715 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700716
717 m_commandBuffer = new XglCommandBufferObj(m_device);
718
719 }
720 else
721 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800722 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700723 }
724
Tony Barboure2c58df2014-11-25 13:18:32 -0700725 // open the command buffer
Tony Barbourbdf0a312015-04-01 17:10:07 -0600726 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {};
727 cmd_buf_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
728 cmd_buf_info.pNext = NULL;
729 cmd_buf_info.flags = 0;
730
Jon Ashburnc4164b12014-12-31 17:10:47 -0700731 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700732 ASSERT_XGL_SUCCESS(err);
733
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000734 XGL_BUFFER_MEMORY_BARRIER memory_barrier =
735 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Mark Lobodzinski837ef922015-01-29 14:24:14 -0600736 XGL_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700737
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000738 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
739 XGL_PIPELINE_BARRIER pipeline_barrier = {};
740 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
741 pipeline_barrier.eventCount = 1;
742 pipeline_barrier.pEvents = set_events;
743 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
744 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -0600745 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000746
747 // write barrier to the command buffer
748 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700749
750 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700751 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700752 ASSERT_XGL_SUCCESS(err);
753
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600754 uint32_t numMemRefs=1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700755 XGL_MEMORY_REF memRefs;
756 // this command buffer only uses the vertex buffer memory
757 memRefs.flags = 0;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800758 memRefs.mem = memories()[0];
Tony Barboure2c58df2014-11-25 13:18:32 -0700759
760 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700761 XGL_CMD_BUFFER bufferArray[1];
762 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800763 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700764 ASSERT_XGL_SUCCESS(err);
765}
766
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700767XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
768 : XglConstantBufferObj(device)
769{
770
771}
772
773void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
774{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700775 XGL_FORMAT viewFormat;
776
777 m_numVertices = numIndexes;
778 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700779 switch (indexType) {
780 case XGL_INDEX_8:
781 m_stride = 1;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700782 viewFormat = XGL_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700783 break;
784 case XGL_INDEX_16:
785 m_stride = 2;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700786 viewFormat = XGL_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700787 break;
788 case XGL_INDEX_32:
789 m_stride = 4;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700790 viewFormat = XGL_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700791 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800792 default:
793 assert(!"unknown index type");
794 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700795 }
796
Chia-I Wua07fee62014-12-28 15:26:08 +0800797 const size_t allocationSize = numIndexes * m_stride;
798 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700799
Chia-I Wua07fee62014-12-28 15:26:08 +0800800 void *pData = map();
801 memcpy(pData, data, allocationSize);
802 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700803
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800804 // set up the buffer view for the constant buffer
805 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
806 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
807 view_info.buffer = obj();
808 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
809 view_info.stride = m_stride;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700810 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800811 view_info.channels.r = XGL_CHANNEL_SWIZZLE_R;
812 view_info.channels.g = XGL_CHANNEL_SWIZZLE_G;
813 view_info.channels.b = XGL_CHANNEL_SWIZZLE_B;
814 view_info.channels.a = XGL_CHANNEL_SWIZZLE_A;
815 view_info.offset = 0;
816 view_info.range = allocationSize;
817 m_bufferView.init(*m_device, view_info);
818
819 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
820 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700821}
822
823void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
824{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800825 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700826}
Tony Barboure2c58df2014-11-25 13:18:32 -0700827
Tony Barbouraf1f9192014-12-17 10:57:58 -0700828XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
829{
830 return m_indexType;
831}
832
Chia-I Wu11078b02015-01-04 16:27:24 +0800833XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700834{
Tony Barboure2c58df2014-11-25 13:18:32 -0700835 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
836 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
837 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800838 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700839 stageInfo->shader.linkConstBufferCount = 0;
840 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700841
Tony Barboure2c58df2014-11-25 13:18:32 -0700842 return stageInfo;
843}
844
Tony Barboure2c58df2014-11-25 13:18:32 -0700845XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
846{
847 XGL_RESULT err = XGL_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600848 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700849 XGL_SHADER_CREATE_INFO createInfo;
850 size_t shader_len;
851
852 m_stage = stage;
853 m_device = device;
854
855 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
856 createInfo.pNext = NULL;
857
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600858 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700859
860 shader_len = strlen(shader_code);
861 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
862 createInfo.pCode = malloc(createInfo.codeSize);
863 createInfo.flags = 0;
864
865 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600866 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700867 ((uint32_t *) createInfo.pCode)[1] = 0;
868 ((uint32_t *) createInfo.pCode)[2] = stage;
869 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
870
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800871 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700872 }
873
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600874 if (framework->m_use_spv || err) {
875 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700876 err = XGL_SUCCESS;
877
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600878 // Use Reference GLSL to SPV compiler
879 framework->GLSLtoSPV(stage, shader_code, spv);
880 createInfo.pCode = spv.data();
881 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700882 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700883
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800884 init(*m_device, createInfo);
885 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700886}
Tony Barbour82c39522014-12-04 14:33:33 -0700887
Tony Barboure2c58df2014-11-25 13:18:32 -0700888XglPipelineObj::XglPipelineObj(XglDevice *device)
889{
Tony Barboure2c58df2014-11-25 13:18:32 -0700890 m_device = device;
891 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
892 m_vertexBufferCount = 0;
893
894 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
895 m_ia_state.pNext = XGL_NULL_HANDLE;
896 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
897 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700898 m_ia_state.primitiveRestartEnable = XGL_FALSE;
899 m_ia_state.primitiveRestartIndex = 0;
900
901 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
902 m_rs_state.pNext = &m_ia_state;
903 m_rs_state.depthClipEnable = XGL_FALSE;
904 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Chia-I Wuc8d1ec52015-03-24 11:01:50 +0800905 m_rs_state.programPointSize = XGL_FALSE;
906 m_rs_state.pointOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700907 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
908 m_rs_state.fillMode = XGL_FILL_SOLID;
909 m_rs_state.cullMode = XGL_CULL_NONE;
910 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700911
Tony Barboure2c58df2014-11-25 13:18:32 -0700912 memset(&m_cb_state,0,sizeof(m_cb_state));
913 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
914 m_cb_state.pNext = &m_rs_state;
915 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700916 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
917
Tony Barbourf52346d2015-01-16 14:27:35 -0700918 m_ms_state.pNext = &m_cb_state;
919 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
920 m_ms_state.multisampleEnable = XGL_FALSE;
921 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
922 m_ms_state.samples = 1;
923 m_ms_state.minSampleShading = 0;
924 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700925
Tony Barbourf52346d2015-01-16 14:27:35 -0700926 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
927 m_ds_state.pNext = &m_ms_state,
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700928 m_ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700929 m_ds_state.depthTestEnable = XGL_FALSE;
930 m_ds_state.depthWriteEnable = XGL_FALSE;
931 m_ds_state.depthBoundsEnable = XGL_FALSE;
932 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
933 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
934 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
935 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
936 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
937 m_ds_state.stencilTestEnable = XGL_FALSE;
938 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -0700939
Tony Barbourf52346d2015-01-16 14:27:35 -0700940 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
941 att.blendEnable = XGL_FALSE;
Tony Barboura53a6942015-02-25 11:25:11 -0700942 att.format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -0700943 att.channelWriteMask = 0xf;
944 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -0700945
946};
947
948void XglPipelineObj::AddShader(XglShaderObj* shader)
949{
950 m_shaderObjs.push_back(shader);
951}
952
953void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
954{
955 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
956 m_vi_state.attributeCount = count;
957}
958
959void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
960{
961 m_vi_state.pVertexBindingDescriptions = vi_binding;
962 m_vi_state.bindingCount = count;
963}
964
965void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
966{
967 m_vertexBufferObjs.push_back(vertexDataBuffer);
968 m_vertexBufferBindings.push_back(binding);
969 m_vertexBufferCount++;
970}
971
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600972void XglPipelineObj::AddColorAttachment(uint32_t binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +0800973{
Tony Barbourf52346d2015-01-16 14:27:35 -0700974 if (binding+1 > m_colorAttachments.size())
975 {
976 m_colorAttachments.resize(binding+1);
977 }
978 m_colorAttachments[binding] = *att;
979}
980
981void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
982{
983 m_ds_state.format = ds_state->format;
984 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
985 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
986 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
987 m_ds_state.depthFunc = ds_state->depthFunc;
988 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
989 m_ds_state.back = ds_state->back;
990 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +0800991}
992
Tony Barbour976e1cf2014-12-17 11:57:31 -0700993void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
994{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600995 void* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -0700996 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
997
998 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
999
1000 for (int i=0; i<m_shaderObjs.size(); i++)
1001 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001002 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001003 shaderCreateInfo->pNext = head_ptr;
1004 head_ptr = shaderCreateInfo;
1005 }
1006
1007 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1008 {
1009 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1010 m_vi_state.pNext = head_ptr;
1011 head_ptr = &m_vi_state;
1012 }
1013
1014 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1015 info.pNext = head_ptr;
1016 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +08001017 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001018
Tony Barbourf52346d2015-01-16 14:27:35 -07001019 m_cb_state.attachmentCount = m_colorAttachments.size();
1020 m_cb_state.pAttachments = &m_colorAttachments[0];
1021
Chia-I Wu2648d092014-12-29 14:24:14 +08001022 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001023}
Chia-I Wu2648d092014-12-29 14:24:14 +08001024
Tony Barbour976e1cf2014-12-17 11:57:31 -07001025XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
1026{
Chia-I Wu2648d092014-12-29 14:24:14 +08001027 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001028}
1029
Tony Barbour5420af02014-12-03 13:58:15 -07001030void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -07001031{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001032 void* head_ptr = &m_ds_state;
Tony Barboure2c58df2014-11-25 13:18:32 -07001033 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1034
1035 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -07001036
1037 for (int i=0; i<m_shaderObjs.size(); i++)
1038 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001039 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barboure2c58df2014-11-25 13:18:32 -07001040 shaderCreateInfo->pNext = head_ptr;
1041 head_ptr = shaderCreateInfo;
1042 }
1043
1044 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1045 {
1046 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1047 m_vi_state.pNext = head_ptr;
1048 head_ptr = &m_vi_state;
1049 }
1050
1051 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1052 info.pNext = head_ptr;
1053 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +08001054 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barboure2c58df2014-11-25 13:18:32 -07001055
Chia-I Wu2648d092014-12-29 14:24:14 +08001056 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -07001057
Chia-I Wu2648d092014-12-29 14:24:14 +08001058 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -07001059
1060
1061 for (int i=0; i < m_vertexBufferCount; i++)
1062 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001063 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001064 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001065}
Tony Barbour82c39522014-12-04 14:33:33 -07001066
Tony Barboure2c58df2014-11-25 13:18:32 -07001067XglMemoryRefManager::XglMemoryRefManager() {
1068
1069}
Tony Barbour82c39522014-12-04 14:33:33 -07001070
Tony Barboure2c58df2014-11-25 13:18:32 -07001071void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001072 const std::vector<XGL_GPU_MEMORY> mems = constantBuffer->memories();
1073 if (!mems.empty())
1074 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001075}
Tony Barbour82c39522014-12-04 14:33:33 -07001076
Tony Barboure2c58df2014-11-25 13:18:32 -07001077void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
Chia-I Wu13a3aa82014-12-28 15:55:09 +08001078 const std::vector<XGL_GPU_MEMORY> mems = texture->memories();
1079 if (!mems.empty())
1080 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001081}
Tony Barbour82c39522014-12-04 14:33:33 -07001082
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001083void XglMemoryRefManager::AddMemoryRef(XGL_GPU_MEMORY *mem, uint32_t refCount) {
1084 for (size_t i = 0; i < refCount; i++) {
1085 m_bufferObjs.push_back(mem[i]);
1086 }
1087}
1088
1089void XglMemoryRefManager::AddRTMemoryRefs(vector<XglImage*>images, uint32_t rtCount) {
1090 for (uint32_t i = 0; i < rtCount; i++) {
1091 const std::vector<XGL_GPU_MEMORY> mems = images[i]->memories();
1092 if (!mems.empty())
1093 m_bufferObjs.push_back(mems[0]);
1094 }
1095}
1096
Tony Barboure2c58df2014-11-25 13:18:32 -07001097XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
1098
1099 XGL_MEMORY_REF *localRefs;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001100 uint32_t numRefs=m_bufferObjs.size();
Tony Barboure2c58df2014-11-25 13:18:32 -07001101
1102 if (numRefs <= 0)
1103 return NULL;
1104
1105 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
1106 for (int i=0; i<numRefs; i++)
1107 {
1108 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +08001109 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -07001110 }
1111 return localRefs;
1112}
1113int XglMemoryRefManager::GetNumRefs() {
1114 return m_bufferObjs.size();
1115}
Tony Barbour6d047bf2014-12-10 14:34:45 -07001116
1117XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Chia-I Wud28343c2014-12-28 15:12:48 +08001118 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(XGL_QUEUE_TYPE_GRAPHICS))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001119{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001120 m_device = device;
Chia-I Wud28343c2014-12-28 15:12:48 +08001121 m_renderTargetCount = 0;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001122}
Tony Barbour471338d2014-12-10 17:28:39 -07001123
Tony Barbour6d047bf2014-12-10 14:34:45 -07001124XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1125{
Chia-I Wud28343c2014-12-28 15:12:48 +08001126 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001127}
Tony Barbour471338d2014-12-10 17:28:39 -07001128
Jon Ashburnc4164b12014-12-31 17:10:47 -07001129XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001130{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001131 begin(pInfo);
1132 return XGL_SUCCESS;
1133}
1134
1135XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj)
1136{
1137 begin(renderpass_obj);
1138 return XGL_SUCCESS;
1139}
1140
1141XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
1142{
1143 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +08001144 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001145}
1146
1147XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1148{
Chia-I Wud28343c2014-12-28 15:12:48 +08001149 end();
1150 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001151}
1152
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001153void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001154{
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001155 xglCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001156}
1157
Tony Barbour30cc9e82014-12-17 11:53:55 -07001158void XglCommandBufferObj::ClearAllBuffers(XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
1159{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001160 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001161 const XGL_FLAGS output_mask =
1162 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1163 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1164 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1165 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1166 XGL_MEMORY_OUTPUT_COPY_BIT;
1167 const XGL_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001168
1169 // whatever we want to do, we do it to the whole buffer
1170 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1171 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1172 srRange.baseMipLevel = 0;
1173 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1174 srRange.baseArraySlice = 0;
1175 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1176
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001177 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1178 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1179 memory_barrier.outputMask = output_mask;
1180 memory_barrier.inputMask = input_mask;
1181 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1182 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001183 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001184
1185 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1186 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1187 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1188 pipeline_barrier.eventCount = 1;
1189 pipeline_barrier.pEvents = set_events;
1190 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1191 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001192 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001193
Tony Barbour30cc9e82014-12-17 11:53:55 -07001194 // clear the back buffer to dark grey
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001195 uint32_t clearColor[4] = {64, 64, 64, 0};
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001196
Tony Barbour30cc9e82014-12-17 11:53:55 -07001197 for (i = 0; i < m_renderTargetCount; i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001198 memory_barrier.image = m_renderTargets[i]->image();
1199 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1200 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1201 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001202
Chia-I Wud28343c2014-12-28 15:12:48 +08001203 xglCmdClearColorImageRaw( obj(), m_renderTargets[i]->image(), clearColor, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001204 }
1205
1206 if (depthStencilImage)
1207 {
1208 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1209 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1210 dsRange.baseMipLevel = 0;
1211 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1212 dsRange.baseArraySlice = 0;
1213 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1214
1215 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001216
1217 memory_barrier.oldLayout = depthStencilBinding->layout;
1218 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1219 memory_barrier.image = depthStencilImage;
1220 memory_barrier.subresourceRange = dsRange;
1221
1222 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1223 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001224
Chia-I Wud28343c2014-12-28 15:12:48 +08001225 xglCmdClearDepthStencil(obj(), depthStencilImage, 1.0f, 0, 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001226
1227 // prepare depth buffer for rendering
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001228 memory_barrier.image = depthStencilImage;
1229 memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1230 memory_barrier.newLayout = depthStencilBinding->layout;
1231 memory_barrier.subresourceRange = dsRange;
1232 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1233 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001234 }
1235}
1236
Jon Ashburncdc40be2015-01-02 18:27:14 -07001237void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001238{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001239 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001240 const XGL_FLAGS output_mask =
1241 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1242 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1243 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1244 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1245 XGL_MEMORY_OUTPUT_COPY_BIT;
1246 const XGL_FLAGS input_mask =
1247 XGL_MEMORY_INPUT_CPU_READ_BIT |
1248 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1249 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
1250 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1251 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
1252 XGL_MEMORY_INPUT_SHADER_READ_BIT |
1253 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1254 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1255 XGL_MEMORY_INPUT_COPY_BIT;
1256
Tony Barbour30cc9e82014-12-17 11:53:55 -07001257 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1258 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1259 srRange.baseMipLevel = 0;
1260 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1261 srRange.baseArraySlice = 0;
1262 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1263
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001264 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1265 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1266 memory_barrier.outputMask = output_mask;
1267 memory_barrier.inputMask = input_mask;
1268 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1269 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001270 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001271
1272 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1273 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1274 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1275 pipeline_barrier.eventCount = 1;
1276 pipeline_barrier.pEvents = set_events;
1277 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1278 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001279 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001280
Tony Barbour30cc9e82014-12-17 11:53:55 -07001281 for(i=0; i<m_renderTargetCount; i++)
1282 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001283 memory_barrier.image = m_renderTargets[i]->image();
1284 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1285 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1286 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001287 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001288}
1289
Tony Barbourf52346d2015-01-16 14:27:35 -07001290void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001291{
Tony Barbourf52346d2015-01-16 14:27:35 -07001292 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001293}
1294
1295void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1296{
1297 m_renderTargets.push_back(renderTarget);
1298 m_renderTargetCount++;
1299}
1300
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001301void XglCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001302{
Chia-I Wud28343c2014-12-28 15:12:48 +08001303 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001304}
1305
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001306void XglCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001307{
Chia-I Wud28343c2014-12-28 15:12:48 +08001308 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001309}
1310
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001311void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, uint32_t numMemRefs)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001312{
1313 XGL_RESULT err = XGL_SUCCESS;
1314
1315 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001316 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001317 ASSERT_XGL_SUCCESS( err );
1318
1319 err = xglQueueWaitIdle( m_device->m_queue );
1320 ASSERT_XGL_SUCCESS( err );
1321
1322 // Wait for work to finish before cleaning up.
1323 xglDeviceWaitIdle(m_device->device());
1324
1325}
1326void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1327{
Chia-I Wud28343c2014-12-28 15:12:48 +08001328 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001329}
1330
1331void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1332{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001333 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wu11078b02015-01-04 16:27:24 +08001334 xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001335}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001336void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001337{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001338 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001339}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001340void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001341{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001342 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001343}