blob: acd2c98955c789290d0bf1fee3b566f0688f46e1 [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;
189 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
190 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
191 .pNext = NULL,
192 .colorAttachmentCount = m_renderTargetCount,
193 .pColorAttachments = m_colorBindings,
194 .pDepthStencilAttachment = dsBinding,
195 .sampleCount = 1,
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600196 .width = (uint32_t)m_width,
197 .height = (uint32_t)m_height,
Mark Lobodzinskidffa1202015-01-27 13:24:03 -0600198 .layers = 1,
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700199 };
200 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;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800384
Tony Barbour579f7802015-04-03 15:11:43 -0600385 XGL_FORMAT_PROPERTIES image_fmt;
386 XGL_IMAGE_TILING tiling;
387 XGL_RESULT err;
388 size_t size;
389
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800390 mipCount = 0;
391
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600392 uint32_t _w = w;
393 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800394 while( ( _w > 0 ) || ( _h > 0 ) )
395 {
396 _w >>= 1;
397 _h >>= 1;
398 mipCount++;
399 }
400
Tony Barbour579f7802015-04-03 15:11:43 -0600401 size = sizeof(image_fmt);
402 err = xglGetFormatInfo(m_device->obj(), fmt,
403 XGL_INFO_TYPE_FORMAT_PROPERTIES,
404 &size, &image_fmt);
405 ASSERT_XGL_SUCCESS(err);
406
407 if (requested_tiling == XGL_LINEAR_TILING) {
408 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
409 tiling = XGL_LINEAR_TILING;
410 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
411 tiling = XGL_OPTIMAL_TILING;
412 } else {
413 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
414 }
415 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
416 tiling = XGL_OPTIMAL_TILING;
417 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
418 tiling = XGL_LINEAR_TILING;
419 } else {
420 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
421 }
422
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800423 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
424 imageCreateInfo.imageType = XGL_IMAGE_2D;
425 imageCreateInfo.format = fmt;
426 imageCreateInfo.extent.width = w;
427 imageCreateInfo.extent.height = h;
428 imageCreateInfo.mipLevels = mipCount;
429 imageCreateInfo.tiling = tiling;
430
431 imageCreateInfo.usage = usage;
432
433 xgl_testing::Image::init(*m_device, imageCreateInfo);
434
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000435 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800436
437 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO createView = {
438 XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
439 XGL_NULL_HANDLE,
440 obj(),
Tony Barboura53a6942015-02-25 11:25:11 -0700441 XGL_FMT_B8G8R8A8_UNORM,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800442 0,
443 0,
444 1
445 };
446
447 m_targetView.init(*m_device, createView);
448}
449
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600450XGL_RESULT XglImage::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800451{
452 *ptr = map();
453 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
454}
455
456XGL_RESULT XglImage::UnmapMemory()
457{
458 unmap();
459 return XGL_SUCCESS;
460}
461
Tony Barbourebc093f2015-04-01 16:38:10 -0600462XglTextureObj::XglTextureObj(XglDevice *device, uint32_t *colors)
Tony Barboure2c58df2014-11-25 13:18:32 -0700463{
464 m_device = device;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700465 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600466 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
467
468 if (colors == NULL)
469 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700470
471 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
472
473 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
474
475 const XGL_IMAGE_CREATE_INFO image = {
476 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
477 .pNext = NULL,
478 .imageType = XGL_IMAGE_2D,
479 .format = tex_format,
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800480 .extent = { 16, 16, 1 },
Tony Barboure2c58df2014-11-25 13:18:32 -0700481 .mipLevels = 1,
482 .arraySize = 1,
483 .samples = 1,
484 .tiling = XGL_LINEAR_TILING,
485 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
486 .flags = 0,
487 };
488
Tony Barboure2c58df2014-11-25 13:18:32 -0700489 XGL_IMAGE_VIEW_CREATE_INFO view;
490 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
491 view.pNext = NULL;
492 view.image = XGL_NULL_HANDLE;
493 view.viewType = XGL_IMAGE_VIEW_2D;
494 view.format = image.format;
495 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
496 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
497 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
498 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
499 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
500 view.subresourceRange.baseMipLevel = 0;
501 view.subresourceRange.mipLevels = 1;
502 view.subresourceRange.baseArraySlice = 0;
503 view.subresourceRange.arraySize = 1;
504 view.minLod = 0.0f;
505
Tony Barboure2c58df2014-11-25 13:18:32 -0700506 /* create image */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800507 init(*m_device, image);
Tony Barboure2c58df2014-11-25 13:18:32 -0700508
509 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800510 view.image = obj();
511 m_textureView.init(*m_device, view);
Tony Barboure2c58df2014-11-25 13:18:32 -0700512
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800513 XGL_SUBRESOURCE_LAYOUT layout =
514 subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
515 m_rowPitch = layout.rowPitch;
Tony Barboure2c58df2014-11-25 13:18:32 -0700516
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600517 void *data;
518 int32_t x, y;
Tony Barboure2c58df2014-11-25 13:18:32 -0700519
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800520 data = map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700521
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800522 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700523 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800524 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600525 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700526 }
527
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800528 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700529
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800530 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700531
532}
Tony Barbour82c39522014-12-04 14:33:33 -0700533
Tony Barboure2c58df2014-11-25 13:18:32 -0700534XglSamplerObj::XglSamplerObj(XglDevice *device)
535{
Tony Barboure2c58df2014-11-25 13:18:32 -0700536 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700537
Chia-I Wue9864b52014-12-28 16:32:24 +0800538 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
539 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
540 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
541 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
542 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
543 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
544 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
545 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
546 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
547 samplerCreateInfo.mipLodBias = 0.0;
548 samplerCreateInfo.maxAnisotropy = 0.0;
549 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
550 samplerCreateInfo.minLod = 0.0;
551 samplerCreateInfo.maxLod = 0.0;
552 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700553
Chia-I Wue9864b52014-12-28 16:32:24 +0800554 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700555}
Tony Barboure2c58df2014-11-25 13:18:32 -0700556
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700557/*
558 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
559 */
560XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
561{
562 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700563 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700564
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800565 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700566}
567
Tony Barboure2c58df2014-11-25 13:18:32 -0700568XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
569{
Tony Barboure2c58df2014-11-25 13:18:32 -0700570 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800571 m_commandBuffer = 0;
572
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800573 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700574 m_numVertices = constantCount;
575 m_stride = constantSize;
576
Chia-I Wua07fee62014-12-28 15:26:08 +0800577 const size_t allocationSize = constantCount * constantSize;
578 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700579
Chia-I Wua07fee62014-12-28 15:26:08 +0800580 void *pData = map();
581 memcpy(pData, data, allocationSize);
582 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700583
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800584 // set up the buffer view for the constant buffer
585 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
586 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
587 view_info.buffer = obj();
Chia-I Wubb0c8d22015-01-16 22:31:25 +0800588 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800589 view_info.offset = 0;
590 view_info.range = allocationSize;
591 m_bufferView.init(*m_device, view_info);
592
593 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
594 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700595}
Tony Barbour82c39522014-12-04 14:33:33 -0700596
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600597void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700598{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800599 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700600}
601
602
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000603void XglConstantBufferObj::BufferMemoryBarrier(
604 XGL_FLAGS outputMask /*=
605 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
606 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
607 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
608 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
609 XGL_MEMORY_OUTPUT_COPY_BIT*/,
610 XGL_FLAGS inputMask /*=
611 XGL_MEMORY_INPUT_CPU_READ_BIT |
612 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
613 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
614 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
615 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
616 XGL_MEMORY_INPUT_SHADER_READ_BIT |
617 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
618 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
619 XGL_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700620{
Tony Barbour38422802014-12-10 14:36:31 -0700621 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700622
Tony Barbour38422802014-12-10 14:36:31 -0700623 if (!m_commandBuffer)
624 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800625 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700626
627 m_commandBuffer = new XglCommandBufferObj(m_device);
628
629 }
630 else
631 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800632 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700633 }
634
Tony Barboure2c58df2014-11-25 13:18:32 -0700635 // open the command buffer
Jon Ashburnc4164b12014-12-31 17:10:47 -0700636 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
637 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
638 .pNext = NULL,
639 .flags = 0,
640 };
641 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700642 ASSERT_XGL_SUCCESS(err);
643
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000644 XGL_BUFFER_MEMORY_BARRIER memory_barrier =
645 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Mark Lobodzinski837ef922015-01-29 14:24:14 -0600646 XGL_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700647
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000648 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
649 XGL_PIPELINE_BARRIER pipeline_barrier = {};
650 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
651 pipeline_barrier.eventCount = 1;
652 pipeline_barrier.pEvents = set_events;
653 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
654 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -0600655 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000656
657 // write barrier to the command buffer
658 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700659
660 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700661 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700662 ASSERT_XGL_SUCCESS(err);
663
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600664 uint32_t numMemRefs=1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700665 XGL_MEMORY_REF memRefs;
666 // this command buffer only uses the vertex buffer memory
667 memRefs.flags = 0;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800668 memRefs.mem = memories()[0];
Tony Barboure2c58df2014-11-25 13:18:32 -0700669
670 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700671 XGL_CMD_BUFFER bufferArray[1];
672 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800673 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700674 ASSERT_XGL_SUCCESS(err);
675}
676
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700677XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
678 : XglConstantBufferObj(device)
679{
680
681}
682
683void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
684{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700685 XGL_FORMAT viewFormat;
686
687 m_numVertices = numIndexes;
688 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700689 switch (indexType) {
690 case XGL_INDEX_8:
691 m_stride = 1;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700692 viewFormat = XGL_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700693 break;
694 case XGL_INDEX_16:
695 m_stride = 2;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700696 viewFormat = XGL_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700697 break;
698 case XGL_INDEX_32:
699 m_stride = 4;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700700 viewFormat = XGL_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700701 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800702 default:
703 assert(!"unknown index type");
704 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700705 }
706
Chia-I Wua07fee62014-12-28 15:26:08 +0800707 const size_t allocationSize = numIndexes * m_stride;
708 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700709
Chia-I Wua07fee62014-12-28 15:26:08 +0800710 void *pData = map();
711 memcpy(pData, data, allocationSize);
712 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700713
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800714 // set up the buffer view for the constant buffer
715 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
716 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
717 view_info.buffer = obj();
718 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
719 view_info.stride = m_stride;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700720 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800721 view_info.channels.r = XGL_CHANNEL_SWIZZLE_R;
722 view_info.channels.g = XGL_CHANNEL_SWIZZLE_G;
723 view_info.channels.b = XGL_CHANNEL_SWIZZLE_B;
724 view_info.channels.a = XGL_CHANNEL_SWIZZLE_A;
725 view_info.offset = 0;
726 view_info.range = allocationSize;
727 m_bufferView.init(*m_device, view_info);
728
729 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
730 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700731}
732
733void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
734{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800735 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700736}
Tony Barboure2c58df2014-11-25 13:18:32 -0700737
Tony Barbouraf1f9192014-12-17 10:57:58 -0700738XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
739{
740 return m_indexType;
741}
742
Chia-I Wu11078b02015-01-04 16:27:24 +0800743XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700744{
Tony Barboure2c58df2014-11-25 13:18:32 -0700745 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
746 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
747 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800748 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700749 stageInfo->shader.linkConstBufferCount = 0;
750 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700751
Tony Barboure2c58df2014-11-25 13:18:32 -0700752 return stageInfo;
753}
754
Tony Barboure2c58df2014-11-25 13:18:32 -0700755XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
756{
757 XGL_RESULT err = XGL_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600758 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700759 XGL_SHADER_CREATE_INFO createInfo;
760 size_t shader_len;
761
762 m_stage = stage;
763 m_device = device;
764
765 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
766 createInfo.pNext = NULL;
767
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600768 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700769
770 shader_len = strlen(shader_code);
771 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
772 createInfo.pCode = malloc(createInfo.codeSize);
773 createInfo.flags = 0;
774
775 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600776 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700777 ((uint32_t *) createInfo.pCode)[1] = 0;
778 ((uint32_t *) createInfo.pCode)[2] = stage;
779 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
780
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800781 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700782 }
783
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600784 if (framework->m_use_spv || err) {
785 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700786 err = XGL_SUCCESS;
787
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600788 // Use Reference GLSL to SPV compiler
789 framework->GLSLtoSPV(stage, shader_code, spv);
790 createInfo.pCode = spv.data();
791 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700792 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700793
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800794 init(*m_device, createInfo);
795 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700796}
Tony Barbour82c39522014-12-04 14:33:33 -0700797
Tony Barboure2c58df2014-11-25 13:18:32 -0700798XglPipelineObj::XglPipelineObj(XglDevice *device)
799{
Tony Barboure2c58df2014-11-25 13:18:32 -0700800 m_device = device;
801 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
802 m_vertexBufferCount = 0;
803
804 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
805 m_ia_state.pNext = XGL_NULL_HANDLE;
806 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
807 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700808 m_ia_state.primitiveRestartEnable = XGL_FALSE;
809 m_ia_state.primitiveRestartIndex = 0;
810
811 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
812 m_rs_state.pNext = &m_ia_state;
813 m_rs_state.depthClipEnable = XGL_FALSE;
814 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Chia-I Wuc8d1ec52015-03-24 11:01:50 +0800815 m_rs_state.programPointSize = XGL_FALSE;
816 m_rs_state.pointOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700817 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
818 m_rs_state.fillMode = XGL_FILL_SOLID;
819 m_rs_state.cullMode = XGL_CULL_NONE;
820 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700821
Tony Barboure2c58df2014-11-25 13:18:32 -0700822 memset(&m_cb_state,0,sizeof(m_cb_state));
823 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
824 m_cb_state.pNext = &m_rs_state;
825 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700826 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
827
Tony Barbourf52346d2015-01-16 14:27:35 -0700828 m_ms_state.pNext = &m_cb_state;
829 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
830 m_ms_state.multisampleEnable = XGL_FALSE;
831 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
832 m_ms_state.samples = 1;
833 m_ms_state.minSampleShading = 0;
834 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700835
Tony Barbourf52346d2015-01-16 14:27:35 -0700836 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
837 m_ds_state.pNext = &m_ms_state,
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700838 m_ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700839 m_ds_state.depthTestEnable = XGL_FALSE;
840 m_ds_state.depthWriteEnable = XGL_FALSE;
841 m_ds_state.depthBoundsEnable = XGL_FALSE;
842 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
843 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
844 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
845 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
846 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
847 m_ds_state.stencilTestEnable = XGL_FALSE;
848 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -0700849
Tony Barbourf52346d2015-01-16 14:27:35 -0700850 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
851 att.blendEnable = XGL_FALSE;
Tony Barboura53a6942015-02-25 11:25:11 -0700852 att.format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -0700853 att.channelWriteMask = 0xf;
854 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -0700855
856};
857
858void XglPipelineObj::AddShader(XglShaderObj* shader)
859{
860 m_shaderObjs.push_back(shader);
861}
862
863void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
864{
865 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
866 m_vi_state.attributeCount = count;
867}
868
869void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
870{
871 m_vi_state.pVertexBindingDescriptions = vi_binding;
872 m_vi_state.bindingCount = count;
873}
874
875void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
876{
877 m_vertexBufferObjs.push_back(vertexDataBuffer);
878 m_vertexBufferBindings.push_back(binding);
879 m_vertexBufferCount++;
880}
881
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600882void XglPipelineObj::AddColorAttachment(uint32_t binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +0800883{
Tony Barbourf52346d2015-01-16 14:27:35 -0700884 if (binding+1 > m_colorAttachments.size())
885 {
886 m_colorAttachments.resize(binding+1);
887 }
888 m_colorAttachments[binding] = *att;
889}
890
891void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
892{
893 m_ds_state.format = ds_state->format;
894 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
895 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
896 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
897 m_ds_state.depthFunc = ds_state->depthFunc;
898 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
899 m_ds_state.back = ds_state->back;
900 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +0800901}
902
Tony Barbour976e1cf2014-12-17 11:57:31 -0700903void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
904{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600905 void* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -0700906 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
907
908 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
909
910 for (int i=0; i<m_shaderObjs.size(); i++)
911 {
Chia-I Wu11078b02015-01-04 16:27:24 +0800912 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700913 shaderCreateInfo->pNext = head_ptr;
914 head_ptr = shaderCreateInfo;
915 }
916
917 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
918 {
919 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
920 m_vi_state.pNext = head_ptr;
921 head_ptr = &m_vi_state;
922 }
923
924 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
925 info.pNext = head_ptr;
926 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +0800927 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700928
Tony Barbourf52346d2015-01-16 14:27:35 -0700929 m_cb_state.attachmentCount = m_colorAttachments.size();
930 m_cb_state.pAttachments = &m_colorAttachments[0];
931
Chia-I Wu2648d092014-12-29 14:24:14 +0800932 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -0700933}
Chia-I Wu2648d092014-12-29 14:24:14 +0800934
Tony Barbour976e1cf2014-12-17 11:57:31 -0700935XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
936{
Chia-I Wu2648d092014-12-29 14:24:14 +0800937 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700938}
939
Tony Barbour5420af02014-12-03 13:58:15 -0700940void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700941{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600942 void* head_ptr = &m_ds_state;
Tony Barboure2c58df2014-11-25 13:18:32 -0700943 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
944
945 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700946
947 for (int i=0; i<m_shaderObjs.size(); i++)
948 {
Chia-I Wu11078b02015-01-04 16:27:24 +0800949 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barboure2c58df2014-11-25 13:18:32 -0700950 shaderCreateInfo->pNext = head_ptr;
951 head_ptr = shaderCreateInfo;
952 }
953
954 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
955 {
956 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
957 m_vi_state.pNext = head_ptr;
958 head_ptr = &m_vi_state;
959 }
960
961 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
962 info.pNext = head_ptr;
963 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +0800964 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barboure2c58df2014-11-25 13:18:32 -0700965
Chia-I Wu2648d092014-12-29 14:24:14 +0800966 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700967
Chia-I Wu2648d092014-12-29 14:24:14 +0800968 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700969
970
971 for (int i=0; i < m_vertexBufferCount; i++)
972 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800973 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700974 }
Tony Barboure2c58df2014-11-25 13:18:32 -0700975}
Tony Barbour82c39522014-12-04 14:33:33 -0700976
Tony Barboure2c58df2014-11-25 13:18:32 -0700977XglMemoryRefManager::XglMemoryRefManager() {
978
979}
Tony Barbour82c39522014-12-04 14:33:33 -0700980
Tony Barboure2c58df2014-11-25 13:18:32 -0700981void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800982 const std::vector<XGL_GPU_MEMORY> mems = constantBuffer->memories();
983 if (!mems.empty())
984 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700985}
Tony Barbour82c39522014-12-04 14:33:33 -0700986
Tony Barboure2c58df2014-11-25 13:18:32 -0700987void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800988 const std::vector<XGL_GPU_MEMORY> mems = texture->memories();
989 if (!mems.empty())
990 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700991}
Tony Barbour82c39522014-12-04 14:33:33 -0700992
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600993void XglMemoryRefManager::AddMemoryRef(XGL_GPU_MEMORY *mem, uint32_t refCount) {
994 for (size_t i = 0; i < refCount; i++) {
995 m_bufferObjs.push_back(mem[i]);
996 }
997}
998
999void XglMemoryRefManager::AddRTMemoryRefs(vector<XglImage*>images, uint32_t rtCount) {
1000 for (uint32_t i = 0; i < rtCount; i++) {
1001 const std::vector<XGL_GPU_MEMORY> mems = images[i]->memories();
1002 if (!mems.empty())
1003 m_bufferObjs.push_back(mems[0]);
1004 }
1005}
1006
Tony Barboure2c58df2014-11-25 13:18:32 -07001007XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
1008
1009 XGL_MEMORY_REF *localRefs;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001010 uint32_t numRefs=m_bufferObjs.size();
Tony Barboure2c58df2014-11-25 13:18:32 -07001011
1012 if (numRefs <= 0)
1013 return NULL;
1014
1015 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
1016 for (int i=0; i<numRefs; i++)
1017 {
1018 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +08001019 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -07001020 }
1021 return localRefs;
1022}
1023int XglMemoryRefManager::GetNumRefs() {
1024 return m_bufferObjs.size();
1025}
Tony Barbour6d047bf2014-12-10 14:34:45 -07001026
1027XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Chia-I Wud28343c2014-12-28 15:12:48 +08001028 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(XGL_QUEUE_TYPE_GRAPHICS))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001029{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001030 m_device = device;
Chia-I Wud28343c2014-12-28 15:12:48 +08001031 m_renderTargetCount = 0;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001032}
Tony Barbour471338d2014-12-10 17:28:39 -07001033
Tony Barbour6d047bf2014-12-10 14:34:45 -07001034XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1035{
Chia-I Wud28343c2014-12-28 15:12:48 +08001036 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001037}
Tony Barbour471338d2014-12-10 17:28:39 -07001038
Jon Ashburnc4164b12014-12-31 17:10:47 -07001039XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001040{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001041 begin(pInfo);
1042 return XGL_SUCCESS;
1043}
1044
1045XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj)
1046{
1047 begin(renderpass_obj);
1048 return XGL_SUCCESS;
1049}
1050
1051XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
1052{
1053 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +08001054 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001055}
1056
1057XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1058{
Chia-I Wud28343c2014-12-28 15:12:48 +08001059 end();
1060 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001061}
1062
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001063void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001064{
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001065 xglCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001066}
1067
Tony Barbour30cc9e82014-12-17 11:53:55 -07001068void XglCommandBufferObj::ClearAllBuffers(XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
1069{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001070 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001071 const XGL_FLAGS output_mask =
1072 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1073 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1074 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1075 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1076 XGL_MEMORY_OUTPUT_COPY_BIT;
1077 const XGL_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001078
1079 // whatever we want to do, we do it to the whole buffer
1080 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1081 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1082 srRange.baseMipLevel = 0;
1083 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1084 srRange.baseArraySlice = 0;
1085 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1086
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001087 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1088 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1089 memory_barrier.outputMask = output_mask;
1090 memory_barrier.inputMask = input_mask;
1091 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1092 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001093 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001094
1095 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1096 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1097 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1098 pipeline_barrier.eventCount = 1;
1099 pipeline_barrier.pEvents = set_events;
1100 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1101 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001102 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001103
Tony Barbour30cc9e82014-12-17 11:53:55 -07001104 // clear the back buffer to dark grey
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001105 uint32_t clearColor[4] = {64, 64, 64, 0};
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001106
Tony Barbour30cc9e82014-12-17 11:53:55 -07001107 for (i = 0; i < m_renderTargetCount; i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001108 memory_barrier.image = m_renderTargets[i]->image();
1109 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1110 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1111 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001112
Chia-I Wud28343c2014-12-28 15:12:48 +08001113 xglCmdClearColorImageRaw( obj(), m_renderTargets[i]->image(), clearColor, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001114 }
1115
1116 if (depthStencilImage)
1117 {
1118 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1119 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1120 dsRange.baseMipLevel = 0;
1121 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1122 dsRange.baseArraySlice = 0;
1123 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1124
1125 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001126
1127 memory_barrier.oldLayout = depthStencilBinding->layout;
1128 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1129 memory_barrier.image = depthStencilImage;
1130 memory_barrier.subresourceRange = dsRange;
1131
1132 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1133 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001134
Chia-I Wud28343c2014-12-28 15:12:48 +08001135 xglCmdClearDepthStencil(obj(), depthStencilImage, 1.0f, 0, 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001136
1137 // prepare depth buffer for rendering
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001138 memory_barrier.image = depthStencilImage;
1139 memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1140 memory_barrier.newLayout = depthStencilBinding->layout;
1141 memory_barrier.subresourceRange = dsRange;
1142 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1143 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001144 }
1145}
1146
Jon Ashburncdc40be2015-01-02 18:27:14 -07001147void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001148{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001149 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001150 const XGL_FLAGS output_mask =
1151 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1152 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1153 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1154 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1155 XGL_MEMORY_OUTPUT_COPY_BIT;
1156 const XGL_FLAGS input_mask =
1157 XGL_MEMORY_INPUT_CPU_READ_BIT |
1158 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1159 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
1160 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1161 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
1162 XGL_MEMORY_INPUT_SHADER_READ_BIT |
1163 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1164 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1165 XGL_MEMORY_INPUT_COPY_BIT;
1166
Tony Barbour30cc9e82014-12-17 11:53:55 -07001167 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1168 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1169 srRange.baseMipLevel = 0;
1170 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1171 srRange.baseArraySlice = 0;
1172 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1173
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001174 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1175 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1176 memory_barrier.outputMask = output_mask;
1177 memory_barrier.inputMask = input_mask;
1178 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1179 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001180 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001181
1182 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1183 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1184 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1185 pipeline_barrier.eventCount = 1;
1186 pipeline_barrier.pEvents = set_events;
1187 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1188 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001189 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001190
Tony Barbour30cc9e82014-12-17 11:53:55 -07001191 for(i=0; i<m_renderTargetCount; i++)
1192 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001193 memory_barrier.image = m_renderTargets[i]->image();
1194 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1195 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1196 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001197 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001198}
1199
Tony Barbourf52346d2015-01-16 14:27:35 -07001200void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001201{
Tony Barbourf52346d2015-01-16 14:27:35 -07001202 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001203}
1204
1205void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1206{
1207 m_renderTargets.push_back(renderTarget);
1208 m_renderTargetCount++;
1209}
1210
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001211void XglCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001212{
Chia-I Wud28343c2014-12-28 15:12:48 +08001213 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001214}
1215
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001216void XglCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001217{
Chia-I Wud28343c2014-12-28 15:12:48 +08001218 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001219}
1220
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001221void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, uint32_t numMemRefs)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001222{
1223 XGL_RESULT err = XGL_SUCCESS;
1224
1225 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001226 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001227 ASSERT_XGL_SUCCESS( err );
1228
1229 err = xglQueueWaitIdle( m_device->m_queue );
1230 ASSERT_XGL_SUCCESS( err );
1231
1232 // Wait for work to finish before cleaning up.
1233 xglDeviceWaitIdle(m_device->device());
1234
1235}
1236void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1237{
Chia-I Wud28343c2014-12-28 15:12:48 +08001238 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001239}
1240
1241void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1242{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001243 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wu11078b02015-01-04 16:27:24 +08001244 xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001245}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001246void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001247{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001248 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001249}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001250void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001251{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001252 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001253}