blob: b7b3742c0a9344b19b269e444228cf6e016d8d98 [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
2 * XGL Tests
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
28#include "xglrenderframework.h"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060030XglRenderFramework::XglRenderFramework() :
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060031 m_cmdBuffer( XGL_NULL_HANDLE ),
Chia-I Wu837f9952014-12-15 23:29:34 +080032 m_stateRaster( XGL_NULL_HANDLE ),
33 m_colorBlend( XGL_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060034 m_stateViewport( XGL_NULL_HANDLE ),
Chia-I Wu837f9952014-12-15 23:29:34 +080035 m_stateDepthStencil( XGL_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060036 m_width( 256.0 ), // default window width
37 m_height( 256.0 ) // default window height
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060038{
Chia-I Wuecebf752014-12-05 10:45:15 +080039 m_renderTargetCount = 1;
40
Jeremy Hayesa058eee2015-01-23 08:51:43 -070041 m_render_target_fmt = XGL_FMT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060042
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060043 m_depthStencilBinding.view = XGL_NULL_HANDLE;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060044}
45
46XglRenderFramework::~XglRenderFramework()
47{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060048
49}
50
51void XglRenderFramework::InitFramework()
52{
53 XGL_RESULT err;
54
Jon Ashburn1e464892015-01-29 15:48:00 -070055 err = xglCreateInstance(&app_info, NULL, &this->inst);
56 ASSERT_XGL_SUCCESS(err);
57 err = xglEnumerateGpus(inst, XGL_MAX_PHYSICAL_GPUS, &this->gpu_count,
58 objs);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060059 ASSERT_XGL_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070060 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060061
62 m_device = new XglDevice(0, objs[0]);
63 m_device->get_device_queue();
64}
65
66void XglRenderFramework::ShutdownFramework()
67{
68 if (m_colorBlend) xglDestroyObject(m_colorBlend);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060069 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
70 if (m_stateRaster) xglDestroyObject(m_stateRaster);
71 if (m_cmdBuffer) xglDestroyObject(m_cmdBuffer);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060072
73 if (m_stateViewport) {
74 xglDestroyObject(m_stateViewport);
75 }
76
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060077 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +080078 delete m_device;
Jon Ashburn1e464892015-01-29 15:48:00 -070079 xglDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060080}
81
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060082void XglRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060083{
84 XGL_RESULT err;
85
Jeremy Hayesa058eee2015-01-23 08:51:43 -070086 m_render_target_fmt = XGL_FMT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060087
88 // create a raster state (solid, back-face culling)
Tony Barbourf52346d2015-01-16 14:27:35 -070089 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster = {};
90 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
91 raster.pointSize = 1.0;
92
93 err = xglCreateDynamicRasterState( device(), &raster, &m_stateRaster );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060094 ASSERT_XGL_SUCCESS(err);
95
Tony Barbourf52346d2015-01-16 14:27:35 -070096 XGL_DYNAMIC_CB_STATE_CREATE_INFO blend = {};
97 blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
98 err = xglCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060099 ASSERT_XGL_SUCCESS( err );
100
Tony Barbourf52346d2015-01-16 14:27:35 -0700101 XGL_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {};
102 depthStencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600103 depthStencil.minDepth = 0.f;
104 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700105 depthStencil.stencilFrontRef = 0;
106 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600107
Tony Barbourf52346d2015-01-16 14:27:35 -0700108 err = xglCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600109 ASSERT_XGL_SUCCESS( err );
110
111 XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {};
112
113 cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
114 cmdInfo.queueType = XGL_QUEUE_TYPE_GRAPHICS;
115 err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
116 ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed";
117}
118
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600119void XglRenderFramework::InitViewport(float width, float height)
120{
121 XGL_RESULT err;
122
Tony Barbourf52346d2015-01-16 14:27:35 -0700123 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700124 XGL_RECT scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600125
Tony Barbourf52346d2015-01-16 14:27:35 -0700126 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewportCreate = {};
127 viewportCreate.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700128 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700129 viewport.originX = 0;
130 viewport.originY = 0;
131 viewport.width = 1.f * width;
132 viewport.height = 1.f * height;
133 viewport.minDepth = 0.f;
134 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700135 scissor.extent.width = width;
136 scissor.extent.height = height;
137 scissor.offset.x = 0;
138 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700139 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700140 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700141
142 err = xglCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600143 ASSERT_XGL_SUCCESS( err );
144 m_width = width;
145 m_height = height;
146}
147
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600148void XglRenderFramework::InitViewport()
149{
150 InitViewport(m_width, m_height);
151}
152
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600153void XglRenderFramework::InitRenderTarget()
154{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600155 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800156
157 for (i = 0; i < m_renderTargetCount; i++) {
Chia-I Wuf50ee212014-12-29 14:31:52 +0800158 XglImage *img = new XglImage(m_device);
159 img->init(m_width, m_height, m_render_target_fmt,
160 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
161 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700162 m_colorBindings[i].view = img->targetView();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000163 m_colorBindings[i].layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700164 m_renderTargets.push_back(img);
Chia-I Wuecebf752014-12-05 10:45:15 +0800165 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700166 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
167 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_LOAD;
168 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_STORE;
169 XGL_DEPTH_STENCIL_BIND_INFO *dsBinding;
170 if (m_depthStencilBinding.view)
171 dsBinding = &m_depthStencilBinding;
172 else
173 dsBinding = NULL;
174 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
175 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
176 .pNext = NULL,
177 .colorAttachmentCount = m_renderTargetCount,
178 .pColorAttachments = m_colorBindings,
179 .pDepthStencilAttachment = dsBinding,
180 .sampleCount = 1,
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600181 .width = (uint32_t)m_width,
182 .height = (uint32_t)m_height,
Mark Lobodzinskidffa1202015-01-27 13:24:03 -0600183 .layers = 1,
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700184 };
185 XGL_RENDER_PASS_CREATE_INFO rp_info;
186 memset(&rp_info, 0 , sizeof(rp_info));
187 xglCreateFramebuffer(device(), &fb_info, &(rp_info.framebuffer));
188 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
189 rp_info.renderArea.extent.width = m_width;
190 rp_info.renderArea.extent.height = m_height;
Courtney Goeltzenleuchter3db6d9b2015-02-10 14:06:25 -0700191 rp_info.colorAttachmentCount = m_renderTargetCount;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700192 rp_info.pColorLoadOps = &load_op;
193 rp_info.pColorStoreOps = &store_op;
194 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
195 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
196 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
197 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
198 xglCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600199}
200
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600201
202
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600203XglDevice::XglDevice(uint32_t id, XGL_PHYSICAL_GPU obj) :
Chia-I Wufb1459b2014-12-29 15:23:20 +0800204 xgl_testing::Device(obj), id(id)
205{
206 init();
207
208 props = gpu().properties();
209 queue_props = &gpu().queue_properties()[0];
210}
211
212void XglDevice::get_device_queue()
213{
214 ASSERT_NE(true, graphics_queues().empty());
215 m_queue = graphics_queues()[0]->obj();
216}
217
Tony Barboure2c58df2014-11-25 13:18:32 -0700218XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
219{
220 m_device = device;
221 m_nextSlot = 0;
222
223}
224
Chia-I Wu11078b02015-01-04 16:27:24 +0800225XglDescriptorSetObj::~XglDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700226{
Chia-I Wu11078b02015-01-04 16:27:24 +0800227 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700228}
Tony Barbour82c39522014-12-04 14:33:33 -0700229
Chia-I Wu11078b02015-01-04 16:27:24 +0800230int XglDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700231{
Chia-I Wu11078b02015-01-04 16:27:24 +0800232 /* request a descriptor but do not update it */
233 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
234 tc.type = XGL_DESCRIPTOR_TYPE_RAW_BUFFER;
235 tc.count = 1;
236 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700237
Chia-I Wu11078b02015-01-04 16:27:24 +0800238 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700239}
Tony Barbour82c39522014-12-04 14:33:33 -0700240
Chia-I Wu11078b02015-01-04 16:27:24 +0800241int XglDescriptorSetObj::AppendBuffer(XGL_DESCRIPTOR_TYPE type, XglConstantBufferObj *constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700242{
Chia-I Wu11078b02015-01-04 16:27:24 +0800243 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
244 tc.type = type;
245 tc.count = 1;
246 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700247
Chia-I Wu11078b02015-01-04 16:27:24 +0800248 m_bufferInfo.push_back(&constantBuffer->m_bufferViewInfo);
249
250 m_updateBuffers.push_back(xgl_testing::DescriptorSet::update(type, m_nextSlot, 1,
251 (const XGL_BUFFER_VIEW_ATTACH_INFO **) NULL));
252
253 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700254}
Tony Barbour82c39522014-12-04 14:33:33 -0700255
Chia-I Wu11078b02015-01-04 16:27:24 +0800256int XglDescriptorSetObj::AppendSamplerTexture( XglSamplerObj* sampler, XglTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700257{
Chia-I Wu11078b02015-01-04 16:27:24 +0800258 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
259 tc.type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE;
260 tc.count = 1;
261 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700262
Chia-I Wu11078b02015-01-04 16:27:24 +0800263 XGL_SAMPLER_IMAGE_VIEW_INFO tmp = {};
264 tmp.pSampler = sampler->obj();
265 tmp.pImageView = &texture->m_textureViewInfo;
266 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700267
Chia-I Wu11078b02015-01-04 16:27:24 +0800268 m_updateSamplerTextures.push_back(xgl_testing::DescriptorSet::update(m_nextSlot, 1,
269 (const XGL_SAMPLER_IMAGE_VIEW_INFO *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700270
Chia-I Wu11078b02015-01-04 16:27:24 +0800271 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700272}
Chia-I Wu11078b02015-01-04 16:27:24 +0800273
274XGL_DESCRIPTOR_SET_LAYOUT XglDescriptorSetObj::GetLayout()
Tony Barbourb5f4d082014-12-17 10:54:03 -0700275{
Chia-I Wu11078b02015-01-04 16:27:24 +0800276 return m_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700277}
278
279XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle()
280{
Chia-I Wu11078b02015-01-04 16:27:24 +0800281 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700282}
Tony Barboure2c58df2014-11-25 13:18:32 -0700283
Chia-I Wu11078b02015-01-04 16:27:24 +0800284void XglDescriptorSetObj::CreateXGLDescriptorSet(XglCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700285{
Chia-I Wu11078b02015-01-04 16:27:24 +0800286 // create XGL_DESCRIPTOR_REGION
287 XGL_DESCRIPTOR_REGION_CREATE_INFO region = {};
288 region.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO;
289 region.count = m_type_counts.size();
290 region.pTypeCount = &m_type_counts[0];
291 init(*m_device, XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1, region);
Tony Barbour824b7712014-12-18 17:06:21 -0700292
Chia-I Wu11078b02015-01-04 16:27:24 +0800293 // create XGL_DESCRIPTOR_SET_LAYOUT
294 vector<XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO> layout;
295 layout.resize(m_type_counts.size());
296 for (int i = 0; i < m_type_counts.size(); i++) {
297 layout[i].sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
298 layout[i].descriptorType = m_type_counts[i].type;
299 layout[i].count = m_type_counts[i].count;
300 layout[i].stageFlags = XGL_SHADER_STAGE_FLAGS_ALL;
301 layout[i].immutableSampler = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700302
Chia-I Wu11078b02015-01-04 16:27:24 +0800303 if (i < m_type_counts.size() - 1)
304 layout[i].pNext = &layout[i + 1];
305 else
306 layout[i].pNext = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700307 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800308
Chia-I Wu11078b02015-01-04 16:27:24 +0800309 m_layout.init(*m_device, 0, layout[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700310
Chia-I Wu11078b02015-01-04 16:27:24 +0800311 // create XGL_DESCRIPTOR_SET
312 m_set = alloc_sets(XGL_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
313
314 // build the update chain
315 for (int i = 0; i < m_updateBuffers.size(); i++) {
316 m_updateBuffers[i].pBufferViews = &m_bufferInfo[i];
317
318 if (i < m_updateBuffers.size() - 1)
319 m_updateBuffers[i].pNext = &m_updateBuffers[i + 1];
320 else if (m_updateSamplerTextures.empty())
321 m_updateBuffers[i].pNext = NULL;
322 else
323 m_updateBuffers[i].pNext = &m_updateSamplerTextures[0];
324 }
325 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
326 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
327
328 if (i < m_updateSamplerTextures.size() - 1)
329 m_updateSamplerTextures[i].pNext = &m_updateSamplerTextures[i + 1];
330 else
331 m_updateSamplerTextures[i].pNext = NULL;
332 }
333 const void *chain = (!m_updateBuffers.empty()) ? (const void *) &m_updateBuffers[0] :
334 (!m_updateSamplerTextures.empty()) ? (const void *) &m_updateSamplerTextures[0] :
335 NULL;
336
337 // do the updates
338 m_device->begin_descriptor_region_update(XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
339 clear_sets(*m_set);
340 m_set->update(chain);
341 m_device->end_descriptor_region_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700342}
Tony Barboure2c58df2014-11-25 13:18:32 -0700343
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800344XglImage::XglImage(XglDevice *dev)
345{
346 m_device = dev;
347 m_imageInfo.view = XGL_NULL_HANDLE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000348 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800349}
350
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600351void XglImage::init(uint32_t w, uint32_t h,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800352 XGL_FORMAT fmt, XGL_FLAGS usage,
353 XGL_IMAGE_TILING tiling)
354{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600355 uint32_t mipCount;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800356
357 mipCount = 0;
358
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600359 uint32_t _w = w;
360 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800361 while( ( _w > 0 ) || ( _h > 0 ) )
362 {
363 _w >>= 1;
364 _h >>= 1;
365 mipCount++;
366 }
367
368 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
369 imageCreateInfo.imageType = XGL_IMAGE_2D;
370 imageCreateInfo.format = fmt;
371 imageCreateInfo.extent.width = w;
372 imageCreateInfo.extent.height = h;
373 imageCreateInfo.mipLevels = mipCount;
374 imageCreateInfo.tiling = tiling;
375
376 imageCreateInfo.usage = usage;
377
378 xgl_testing::Image::init(*m_device, imageCreateInfo);
379
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000380 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800381
382 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO createView = {
383 XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
384 XGL_NULL_HANDLE,
385 obj(),
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700386 XGL_FMT_R8G8B8A8_UNORM,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800387 0,
388 0,
389 1
390 };
391
392 m_targetView.init(*m_device, createView);
393}
394
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600395XGL_RESULT XglImage::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800396{
397 *ptr = map();
398 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
399}
400
401XGL_RESULT XglImage::UnmapMemory()
402{
403 unmap();
404 return XGL_SUCCESS;
405}
406
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800407XglTextureObj::XglTextureObj(XglDevice *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700408{
409 m_device = device;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700410 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barboure2c58df2014-11-25 13:18:32 -0700411 const uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barboure2c58df2014-11-25 13:18:32 -0700412
413 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
414
415 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
416
417 const XGL_IMAGE_CREATE_INFO image = {
418 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
419 .pNext = NULL,
420 .imageType = XGL_IMAGE_2D,
421 .format = tex_format,
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800422 .extent = { 16, 16, 1 },
Tony Barboure2c58df2014-11-25 13:18:32 -0700423 .mipLevels = 1,
424 .arraySize = 1,
425 .samples = 1,
426 .tiling = XGL_LINEAR_TILING,
427 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
428 .flags = 0,
429 };
430
Tony Barboure2c58df2014-11-25 13:18:32 -0700431 XGL_IMAGE_VIEW_CREATE_INFO view;
432 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
433 view.pNext = NULL;
434 view.image = XGL_NULL_HANDLE;
435 view.viewType = XGL_IMAGE_VIEW_2D;
436 view.format = image.format;
437 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
438 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
439 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
440 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
441 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
442 view.subresourceRange.baseMipLevel = 0;
443 view.subresourceRange.mipLevels = 1;
444 view.subresourceRange.baseArraySlice = 0;
445 view.subresourceRange.arraySize = 1;
446 view.minLod = 0.0f;
447
Tony Barboure2c58df2014-11-25 13:18:32 -0700448 /* create image */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800449 init(*m_device, image);
Tony Barboure2c58df2014-11-25 13:18:32 -0700450
451 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800452 view.image = obj();
453 m_textureView.init(*m_device, view);
Tony Barboure2c58df2014-11-25 13:18:32 -0700454
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800455 XGL_SUBRESOURCE_LAYOUT layout =
456 subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
457 m_rowPitch = layout.rowPitch;
Tony Barboure2c58df2014-11-25 13:18:32 -0700458
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600459 void *data;
460 int32_t x, y;
Tony Barboure2c58df2014-11-25 13:18:32 -0700461
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800462 data = map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700463
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800464 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700465 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800466 for (x = 0; x < extent().width; x++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700467 row[x] = tex_colors[(x & 1) ^ (y & 1)];
468 }
469
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800470 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700471
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800472 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700473
474}
Tony Barbour82c39522014-12-04 14:33:33 -0700475
Tony Barboure2c58df2014-11-25 13:18:32 -0700476void XglTextureObj::ChangeColors(uint32_t color1, uint32_t color2)
477{
Tony Barboure2c58df2014-11-25 13:18:32 -0700478 const uint32_t tex_colors[2] = { color1, color2 };
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600479 void *data;
Tony Barboure2c58df2014-11-25 13:18:32 -0700480
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800481 data = map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700482
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800483 for (int y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700484 uint32_t *row = (uint32_t *) ((char *) data + m_rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800485 for (int x = 0; x < extent().width; x++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700486 row[x] = tex_colors[(x & 1) ^ (y & 1)];
487 }
488
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800489 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700490}
491
492XglSamplerObj::XglSamplerObj(XglDevice *device)
493{
Tony Barboure2c58df2014-11-25 13:18:32 -0700494 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700495
Chia-I Wue9864b52014-12-28 16:32:24 +0800496 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
497 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
498 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
499 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
500 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
501 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
502 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
503 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
504 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
505 samplerCreateInfo.mipLodBias = 0.0;
506 samplerCreateInfo.maxAnisotropy = 0.0;
507 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
508 samplerCreateInfo.minLod = 0.0;
509 samplerCreateInfo.maxLod = 0.0;
510 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700511
Chia-I Wue9864b52014-12-28 16:32:24 +0800512 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700513}
Tony Barboure2c58df2014-11-25 13:18:32 -0700514
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700515/*
516 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
517 */
518XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
519{
520 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700521 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700522
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800523 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700524}
525
Tony Barboure2c58df2014-11-25 13:18:32 -0700526XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
527{
Tony Barboure2c58df2014-11-25 13:18:32 -0700528 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800529 m_commandBuffer = 0;
530
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800531 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700532 m_numVertices = constantCount;
533 m_stride = constantSize;
534
Chia-I Wua07fee62014-12-28 15:26:08 +0800535 const size_t allocationSize = constantCount * constantSize;
536 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700537
Chia-I Wua07fee62014-12-28 15:26:08 +0800538 void *pData = map();
539 memcpy(pData, data, allocationSize);
540 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700541
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800542 // set up the buffer view for the constant buffer
543 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
544 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
545 view_info.buffer = obj();
Chia-I Wubb0c8d22015-01-16 22:31:25 +0800546 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800547 view_info.offset = 0;
548 view_info.range = allocationSize;
549 m_bufferView.init(*m_device, view_info);
550
551 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
552 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700553}
Tony Barbour82c39522014-12-04 14:33:33 -0700554
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600555void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700556{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800557 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700558}
559
560
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000561void XglConstantBufferObj::BufferMemoryBarrier(
562 XGL_FLAGS outputMask /*=
563 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
564 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
565 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
566 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
567 XGL_MEMORY_OUTPUT_COPY_BIT*/,
568 XGL_FLAGS inputMask /*=
569 XGL_MEMORY_INPUT_CPU_READ_BIT |
570 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
571 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
572 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
573 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
574 XGL_MEMORY_INPUT_SHADER_READ_BIT |
575 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
576 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
577 XGL_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700578{
Tony Barbour38422802014-12-10 14:36:31 -0700579 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700580
Tony Barbour38422802014-12-10 14:36:31 -0700581 if (!m_commandBuffer)
582 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800583 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700584
585 m_commandBuffer = new XglCommandBufferObj(m_device);
586
587 }
588 else
589 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800590 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700591 }
592
Tony Barboure2c58df2014-11-25 13:18:32 -0700593 // open the command buffer
Jon Ashburnc4164b12014-12-31 17:10:47 -0700594 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
595 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
596 .pNext = NULL,
597 .flags = 0,
598 };
599 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700600 ASSERT_XGL_SUCCESS(err);
601
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000602 XGL_BUFFER_MEMORY_BARRIER memory_barrier =
603 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Mark Lobodzinski837ef922015-01-29 14:24:14 -0600604 XGL_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700605
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000606 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
607 XGL_PIPELINE_BARRIER pipeline_barrier = {};
608 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
609 pipeline_barrier.eventCount = 1;
610 pipeline_barrier.pEvents = set_events;
611 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
612 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -0600613 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000614
615 // write barrier to the command buffer
616 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700617
618 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700619 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700620 ASSERT_XGL_SUCCESS(err);
621
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600622 uint32_t numMemRefs=1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700623 XGL_MEMORY_REF memRefs;
624 // this command buffer only uses the vertex buffer memory
625 memRefs.flags = 0;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800626 memRefs.mem = memories()[0];
Tony Barboure2c58df2014-11-25 13:18:32 -0700627
628 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700629 XGL_CMD_BUFFER bufferArray[1];
630 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800631 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700632 ASSERT_XGL_SUCCESS(err);
633}
634
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700635XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
636 : XglConstantBufferObj(device)
637{
638
639}
640
641void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
642{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700643 XGL_FORMAT viewFormat;
644
645 m_numVertices = numIndexes;
646 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700647 switch (indexType) {
648 case XGL_INDEX_8:
649 m_stride = 1;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700650 viewFormat = XGL_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700651 break;
652 case XGL_INDEX_16:
653 m_stride = 2;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700654 viewFormat = XGL_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700655 break;
656 case XGL_INDEX_32:
657 m_stride = 4;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700658 viewFormat = XGL_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700659 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800660 default:
661 assert(!"unknown index type");
662 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700663 }
664
Chia-I Wua07fee62014-12-28 15:26:08 +0800665 const size_t allocationSize = numIndexes * m_stride;
666 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700667
Chia-I Wua07fee62014-12-28 15:26:08 +0800668 void *pData = map();
669 memcpy(pData, data, allocationSize);
670 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700671
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800672 // set up the buffer view for the constant buffer
673 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
674 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
675 view_info.buffer = obj();
676 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
677 view_info.stride = m_stride;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700678 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800679 view_info.channels.r = XGL_CHANNEL_SWIZZLE_R;
680 view_info.channels.g = XGL_CHANNEL_SWIZZLE_G;
681 view_info.channels.b = XGL_CHANNEL_SWIZZLE_B;
682 view_info.channels.a = XGL_CHANNEL_SWIZZLE_A;
683 view_info.offset = 0;
684 view_info.range = allocationSize;
685 m_bufferView.init(*m_device, view_info);
686
687 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
688 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700689}
690
691void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
692{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800693 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700694}
Tony Barboure2c58df2014-11-25 13:18:32 -0700695
Tony Barbouraf1f9192014-12-17 10:57:58 -0700696XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
697{
698 return m_indexType;
699}
700
Chia-I Wu11078b02015-01-04 16:27:24 +0800701XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700702{
Tony Barboure2c58df2014-11-25 13:18:32 -0700703 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
704 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
705 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800706 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700707 stageInfo->shader.linkConstBufferCount = 0;
708 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700709
Tony Barboure2c58df2014-11-25 13:18:32 -0700710 return stageInfo;
711}
712
Tony Barboure2c58df2014-11-25 13:18:32 -0700713XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
714{
715 XGL_RESULT err = XGL_SUCCESS;
716 std::vector<unsigned int> bil;
717 XGL_SHADER_CREATE_INFO createInfo;
718 size_t shader_len;
719
720 m_stage = stage;
721 m_device = device;
722
723 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
724 createInfo.pNext = NULL;
725
726 if (!framework->m_use_bil) {
727
728 shader_len = strlen(shader_code);
729 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
730 createInfo.pCode = malloc(createInfo.codeSize);
731 createInfo.flags = 0;
732
733 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
734 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
735 ((uint32_t *) createInfo.pCode)[1] = 0;
736 ((uint32_t *) createInfo.pCode)[2] = stage;
737 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
738
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800739 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700740 }
741
742 if (framework->m_use_bil || err) {
743 std::vector<unsigned int> bil;
744 err = XGL_SUCCESS;
745
746 // Use Reference GLSL to BIL compiler
747 framework->GLSLtoBIL(stage, shader_code, bil);
748 createInfo.pCode = bil.data();
749 createInfo.codeSize = bil.size() * sizeof(unsigned int);
750 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700751
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800752 init(*m_device, createInfo);
753 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700754}
Tony Barbour82c39522014-12-04 14:33:33 -0700755
Tony Barboure2c58df2014-11-25 13:18:32 -0700756XglPipelineObj::XglPipelineObj(XglDevice *device)
757{
Tony Barboure2c58df2014-11-25 13:18:32 -0700758 m_device = device;
759 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
760 m_vertexBufferCount = 0;
761
762 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
763 m_ia_state.pNext = XGL_NULL_HANDLE;
764 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
765 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700766 m_ia_state.primitiveRestartEnable = XGL_FALSE;
767 m_ia_state.primitiveRestartIndex = 0;
768
769 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
770 m_rs_state.pNext = &m_ia_state;
771 m_rs_state.depthClipEnable = XGL_FALSE;
772 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -0700773 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
774 m_rs_state.fillMode = XGL_FILL_SOLID;
775 m_rs_state.cullMode = XGL_CULL_NONE;
776 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700777
Tony Barboure2c58df2014-11-25 13:18:32 -0700778 memset(&m_cb_state,0,sizeof(m_cb_state));
779 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
780 m_cb_state.pNext = &m_rs_state;
781 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700782 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
783
Tony Barbourf52346d2015-01-16 14:27:35 -0700784 m_ms_state.pNext = &m_cb_state;
785 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
786 m_ms_state.multisampleEnable = XGL_FALSE;
787 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
788 m_ms_state.samples = 1;
789 m_ms_state.minSampleShading = 0;
790 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700791
Tony Barbourf52346d2015-01-16 14:27:35 -0700792 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
793 m_ds_state.pNext = &m_ms_state,
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700794 m_ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700795 m_ds_state.depthTestEnable = XGL_FALSE;
796 m_ds_state.depthWriteEnable = XGL_FALSE;
797 m_ds_state.depthBoundsEnable = XGL_FALSE;
798 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
799 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
800 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
801 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
802 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
803 m_ds_state.stencilTestEnable = XGL_FALSE;
804 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -0700805
Tony Barbourf52346d2015-01-16 14:27:35 -0700806 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
807 att.blendEnable = XGL_FALSE;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700808 att.format = XGL_FMT_R8G8B8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -0700809 att.channelWriteMask = 0xf;
810 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -0700811
812};
813
814void XglPipelineObj::AddShader(XglShaderObj* shader)
815{
816 m_shaderObjs.push_back(shader);
817}
818
819void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
820{
821 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
822 m_vi_state.attributeCount = count;
823}
824
825void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
826{
827 m_vi_state.pVertexBindingDescriptions = vi_binding;
828 m_vi_state.bindingCount = count;
829}
830
831void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
832{
833 m_vertexBufferObjs.push_back(vertexDataBuffer);
834 m_vertexBufferBindings.push_back(binding);
835 m_vertexBufferCount++;
836}
837
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600838void XglPipelineObj::AddColorAttachment(uint32_t binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +0800839{
Tony Barbourf52346d2015-01-16 14:27:35 -0700840 if (binding+1 > m_colorAttachments.size())
841 {
842 m_colorAttachments.resize(binding+1);
843 }
844 m_colorAttachments[binding] = *att;
845}
846
847void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
848{
849 m_ds_state.format = ds_state->format;
850 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
851 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
852 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
853 m_ds_state.depthFunc = ds_state->depthFunc;
854 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
855 m_ds_state.back = ds_state->back;
856 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +0800857}
858
Tony Barbour976e1cf2014-12-17 11:57:31 -0700859void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
860{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600861 void* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -0700862 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
863
864 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
865
866 for (int i=0; i<m_shaderObjs.size(); i++)
867 {
Chia-I Wu11078b02015-01-04 16:27:24 +0800868 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700869 shaderCreateInfo->pNext = head_ptr;
870 head_ptr = shaderCreateInfo;
871 }
872
873 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
874 {
875 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
876 m_vi_state.pNext = head_ptr;
877 head_ptr = &m_vi_state;
878 }
879
880 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
881 info.pNext = head_ptr;
882 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +0800883 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700884
Tony Barbourf52346d2015-01-16 14:27:35 -0700885 m_cb_state.attachmentCount = m_colorAttachments.size();
886 m_cb_state.pAttachments = &m_colorAttachments[0];
887
Chia-I Wu2648d092014-12-29 14:24:14 +0800888 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -0700889}
Chia-I Wu2648d092014-12-29 14:24:14 +0800890
Tony Barbour976e1cf2014-12-17 11:57:31 -0700891XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
892{
Chia-I Wu2648d092014-12-29 14:24:14 +0800893 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700894}
895
Tony Barbour5420af02014-12-03 13:58:15 -0700896void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700897{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600898 void* head_ptr = &m_ds_state;
Tony Barboure2c58df2014-11-25 13:18:32 -0700899 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
900
901 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700902
903 for (int i=0; i<m_shaderObjs.size(); i++)
904 {
Chia-I Wu11078b02015-01-04 16:27:24 +0800905 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barboure2c58df2014-11-25 13:18:32 -0700906 shaderCreateInfo->pNext = head_ptr;
907 head_ptr = shaderCreateInfo;
908 }
909
910 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
911 {
912 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
913 m_vi_state.pNext = head_ptr;
914 head_ptr = &m_vi_state;
915 }
916
917 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
918 info.pNext = head_ptr;
919 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +0800920 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barboure2c58df2014-11-25 13:18:32 -0700921
Chia-I Wu2648d092014-12-29 14:24:14 +0800922 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700923
Chia-I Wu2648d092014-12-29 14:24:14 +0800924 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700925
926
927 for (int i=0; i < m_vertexBufferCount; i++)
928 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800929 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700930 }
Tony Barboure2c58df2014-11-25 13:18:32 -0700931}
Tony Barbour82c39522014-12-04 14:33:33 -0700932
Tony Barboure2c58df2014-11-25 13:18:32 -0700933XglMemoryRefManager::XglMemoryRefManager() {
934
935}
Tony Barbour82c39522014-12-04 14:33:33 -0700936
Tony Barboure2c58df2014-11-25 13:18:32 -0700937void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800938 const std::vector<XGL_GPU_MEMORY> mems = constantBuffer->memories();
939 if (!mems.empty())
940 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700941}
Tony Barbour82c39522014-12-04 14:33:33 -0700942
Tony Barboure2c58df2014-11-25 13:18:32 -0700943void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800944 const std::vector<XGL_GPU_MEMORY> mems = texture->memories();
945 if (!mems.empty())
946 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700947}
Tony Barbour82c39522014-12-04 14:33:33 -0700948
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600949void XglMemoryRefManager::AddMemoryRef(XGL_GPU_MEMORY *mem, uint32_t refCount) {
950 for (size_t i = 0; i < refCount; i++) {
951 m_bufferObjs.push_back(mem[i]);
952 }
953}
954
955void XglMemoryRefManager::AddRTMemoryRefs(vector<XglImage*>images, uint32_t rtCount) {
956 for (uint32_t i = 0; i < rtCount; i++) {
957 const std::vector<XGL_GPU_MEMORY> mems = images[i]->memories();
958 if (!mems.empty())
959 m_bufferObjs.push_back(mems[0]);
960 }
961}
962
Tony Barboure2c58df2014-11-25 13:18:32 -0700963XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
964
965 XGL_MEMORY_REF *localRefs;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600966 uint32_t numRefs=m_bufferObjs.size();
Tony Barboure2c58df2014-11-25 13:18:32 -0700967
968 if (numRefs <= 0)
969 return NULL;
970
971 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
972 for (int i=0; i<numRefs; i++)
973 {
974 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +0800975 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -0700976 }
977 return localRefs;
978}
979int XglMemoryRefManager::GetNumRefs() {
980 return m_bufferObjs.size();
981}
Tony Barbour6d047bf2014-12-10 14:34:45 -0700982
983XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Chia-I Wud28343c2014-12-28 15:12:48 +0800984 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(XGL_QUEUE_TYPE_GRAPHICS))
Tony Barbour6d047bf2014-12-10 14:34:45 -0700985{
Tony Barbour6d047bf2014-12-10 14:34:45 -0700986 m_device = device;
Chia-I Wud28343c2014-12-28 15:12:48 +0800987 m_renderTargetCount = 0;
Tony Barbour6d047bf2014-12-10 14:34:45 -0700988}
Tony Barbour471338d2014-12-10 17:28:39 -0700989
Tony Barbour6d047bf2014-12-10 14:34:45 -0700990XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
991{
Chia-I Wud28343c2014-12-28 15:12:48 +0800992 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -0700993}
Tony Barbour471338d2014-12-10 17:28:39 -0700994
Jon Ashburnc4164b12014-12-31 17:10:47 -0700995XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -0700996{
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700997 begin(pInfo);
998 return XGL_SUCCESS;
999}
1000
1001XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj)
1002{
1003 begin(renderpass_obj);
1004 return XGL_SUCCESS;
1005}
1006
1007XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
1008{
1009 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +08001010 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001011}
1012
1013XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1014{
Chia-I Wud28343c2014-12-28 15:12:48 +08001015 end();
1016 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001017}
1018
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001019void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001020{
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001021 xglCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001022}
1023
Tony Barbour30cc9e82014-12-17 11:53:55 -07001024void XglCommandBufferObj::ClearAllBuffers(XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
1025{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001026 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001027 const XGL_FLAGS output_mask =
1028 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1029 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1030 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1031 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1032 XGL_MEMORY_OUTPUT_COPY_BIT;
1033 const XGL_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001034
1035 // whatever we want to do, we do it to the whole buffer
1036 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1037 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1038 srRange.baseMipLevel = 0;
1039 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1040 srRange.baseArraySlice = 0;
1041 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1042
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001043 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1044 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1045 memory_barrier.outputMask = output_mask;
1046 memory_barrier.inputMask = input_mask;
1047 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1048 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001049 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001050
1051 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1052 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1053 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1054 pipeline_barrier.eventCount = 1;
1055 pipeline_barrier.pEvents = set_events;
1056 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1057 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001058 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001059
Tony Barbour30cc9e82014-12-17 11:53:55 -07001060 // clear the back buffer to dark grey
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001061 uint32_t clearColor[4] = {64, 64, 64, 0};
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001062
Tony Barbour30cc9e82014-12-17 11:53:55 -07001063 for (i = 0; i < m_renderTargetCount; i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001064 memory_barrier.image = m_renderTargets[i]->image();
1065 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1066 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1067 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001068
Chia-I Wud28343c2014-12-28 15:12:48 +08001069 xglCmdClearColorImageRaw( obj(), m_renderTargets[i]->image(), clearColor, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001070 }
1071
1072 if (depthStencilImage)
1073 {
1074 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1075 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1076 dsRange.baseMipLevel = 0;
1077 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1078 dsRange.baseArraySlice = 0;
1079 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1080
1081 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001082
1083 memory_barrier.oldLayout = depthStencilBinding->layout;
1084 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1085 memory_barrier.image = depthStencilImage;
1086 memory_barrier.subresourceRange = dsRange;
1087
1088 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1089 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001090
Chia-I Wud28343c2014-12-28 15:12:48 +08001091 xglCmdClearDepthStencil(obj(), depthStencilImage, 1.0f, 0, 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001092
1093 // prepare depth buffer for rendering
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001094 memory_barrier.image = depthStencilImage;
1095 memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1096 memory_barrier.newLayout = depthStencilBinding->layout;
1097 memory_barrier.subresourceRange = dsRange;
1098 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1099 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001100 }
1101}
1102
Jon Ashburncdc40be2015-01-02 18:27:14 -07001103void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001104{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001105 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001106 const XGL_FLAGS output_mask =
1107 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1108 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1109 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1110 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1111 XGL_MEMORY_OUTPUT_COPY_BIT;
1112 const XGL_FLAGS input_mask =
1113 XGL_MEMORY_INPUT_CPU_READ_BIT |
1114 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1115 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
1116 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1117 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
1118 XGL_MEMORY_INPUT_SHADER_READ_BIT |
1119 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1120 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1121 XGL_MEMORY_INPUT_COPY_BIT;
1122
Tony Barbour30cc9e82014-12-17 11:53:55 -07001123 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1124 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1125 srRange.baseMipLevel = 0;
1126 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1127 srRange.baseArraySlice = 0;
1128 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1129
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001130 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1131 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1132 memory_barrier.outputMask = output_mask;
1133 memory_barrier.inputMask = input_mask;
1134 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1135 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001136 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001137
1138 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1139 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1140 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1141 pipeline_barrier.eventCount = 1;
1142 pipeline_barrier.pEvents = set_events;
1143 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1144 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001145 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001146
Tony Barbour30cc9e82014-12-17 11:53:55 -07001147 for(i=0; i<m_renderTargetCount; i++)
1148 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001149 memory_barrier.image = m_renderTargets[i]->image();
1150 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1151 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1152 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001153 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001154}
1155
Tony Barbourf52346d2015-01-16 14:27:35 -07001156void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001157{
Tony Barbourf52346d2015-01-16 14:27:35 -07001158 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001159}
1160
1161void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1162{
1163 m_renderTargets.push_back(renderTarget);
1164 m_renderTargetCount++;
1165}
1166
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001167void XglCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001168{
Chia-I Wud28343c2014-12-28 15:12:48 +08001169 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001170}
1171
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001172void XglCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001173{
Chia-I Wud28343c2014-12-28 15:12:48 +08001174 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001175}
1176
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001177void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, uint32_t numMemRefs)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001178{
1179 XGL_RESULT err = XGL_SUCCESS;
1180
1181 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001182 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001183 ASSERT_XGL_SUCCESS( err );
1184
1185 err = xglQueueWaitIdle( m_device->m_queue );
1186 ASSERT_XGL_SUCCESS( err );
1187
1188 // Wait for work to finish before cleaning up.
1189 xglDeviceWaitIdle(m_device->device());
1190
1191}
1192void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1193{
Chia-I Wud28343c2014-12-28 15:12:48 +08001194 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001195}
1196
1197void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1198{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001199 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wu11078b02015-01-04 16:27:24 +08001200 xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001201}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001202void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001203{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001204 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001205}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001206void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001207{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001208 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001209}