blob: a2ad76fcc5cf2f195356e553c2aebd20187524bc [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
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070037 m_height( 256.0 ), // default window height
38 m_render_target_fmt( XGL_FMT_R8G8B8A8_UNORM ),
39 m_depth_stencil_fmt( XGL_FMT_UNDEFINED ),
40 m_depth_clear_color( 1.0 ),
41 m_stencil_clear_color( 0 )
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060042{
Tony Barbour1c45ce02015-03-27 17:03:18 -060043
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -070044 // clear the back buffer to dark grey
45 m_clear_color.color.rawColor[0] = 64;
46 m_clear_color.color.rawColor[1] = 64;
47 m_clear_color.color.rawColor[2] = 64;
48 m_clear_color.color.rawColor[3] = 0;
49 m_clear_color.useRawValue = true;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060050}
51
52XglRenderFramework::~XglRenderFramework()
53{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060054
55}
56
57void XglRenderFramework::InitFramework()
58{
59 XGL_RESULT err;
60
Jon Ashburn1e464892015-01-29 15:48:00 -070061 err = xglCreateInstance(&app_info, NULL, &this->inst);
62 ASSERT_XGL_SUCCESS(err);
63 err = xglEnumerateGpus(inst, XGL_MAX_PHYSICAL_GPUS, &this->gpu_count,
64 objs);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060065 ASSERT_XGL_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070066 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060067
68 m_device = new XglDevice(0, objs[0]);
69 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -060070
71 m_depthStencil = new XglDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060072}
73
74void XglRenderFramework::ShutdownFramework()
75{
76 if (m_colorBlend) xglDestroyObject(m_colorBlend);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060077 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
78 if (m_stateRaster) xglDestroyObject(m_stateRaster);
79 if (m_cmdBuffer) xglDestroyObject(m_cmdBuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -060080 if (m_framebuffer) xglDestroyObject(m_framebuffer);
Tobin Ehlis976fc162015-03-26 08:23:25 -060081 if (m_renderPass) xglDestroyObject(m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060082
83 if (m_stateViewport) {
84 xglDestroyObject(m_stateViewport);
85 }
Tobin Ehlis976fc162015-03-26 08:23:25 -060086 while (!m_renderTargets.empty()) {
87 xglDestroyObject(m_renderTargets.back()->targetView());
88 xglBindObjectMemory(m_renderTargets.back()->image(), 0, XGL_NULL_HANDLE, 0);
89 xglDestroyObject(m_renderTargets.back()->image());
90 xglFreeMemory(m_renderTargets.back()->memory());
91 m_renderTargets.pop_back();
92 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060093
Tony Barbour1c45ce02015-03-27 17:03:18 -060094 delete m_depthStencil;
95
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060096 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +080097 delete m_device;
Jon Ashburn1e464892015-01-29 15:48:00 -070098 xglDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060099}
100
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600101void XglRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600102{
103 XGL_RESULT err;
104
Tony Barboura53a6942015-02-25 11:25:11 -0700105 m_render_target_fmt = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600106
107 // create a raster state (solid, back-face culling)
Tony Barbourf52346d2015-01-16 14:27:35 -0700108 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster = {};
109 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
110 raster.pointSize = 1.0;
111
112 err = xglCreateDynamicRasterState( device(), &raster, &m_stateRaster );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600113 ASSERT_XGL_SUCCESS(err);
114
Tony Barbourf52346d2015-01-16 14:27:35 -0700115 XGL_DYNAMIC_CB_STATE_CREATE_INFO blend = {};
116 blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
117 err = xglCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600118 ASSERT_XGL_SUCCESS( err );
119
Tony Barbourf52346d2015-01-16 14:27:35 -0700120 XGL_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {};
121 depthStencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600122 depthStencil.minDepth = 0.f;
123 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700124 depthStencil.stencilFrontRef = 0;
125 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600126
Tony Barbourf52346d2015-01-16 14:27:35 -0700127 err = xglCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600128 ASSERT_XGL_SUCCESS( err );
129
130 XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {};
131
132 cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700133 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
134
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600135 err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
136 ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed";
137}
138
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600139void XglRenderFramework::InitViewport(float width, float height)
140{
141 XGL_RESULT err;
142
Tony Barbourf52346d2015-01-16 14:27:35 -0700143 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700144 XGL_RECT scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600145
Tony Barbourf52346d2015-01-16 14:27:35 -0700146 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewportCreate = {};
147 viewportCreate.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700148 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700149 viewport.originX = 0;
150 viewport.originY = 0;
151 viewport.width = 1.f * width;
152 viewport.height = 1.f * height;
153 viewport.minDepth = 0.f;
154 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700155 scissor.extent.width = width;
156 scissor.extent.height = height;
157 scissor.offset.x = 0;
158 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700159 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700160 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700161
162 err = xglCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600163 ASSERT_XGL_SUCCESS( err );
164 m_width = width;
165 m_height = height;
166}
167
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600168void XglRenderFramework::InitViewport()
169{
170 InitViewport(m_width, m_height);
171}
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600172void XglRenderFramework::InitRenderTarget()
173{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600174
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700175 InitRenderTarget(1);
176}
177
178void XglRenderFramework::InitRenderTarget(uint32_t targets)
179{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600180 InitRenderTarget(targets, NULL);
181}
182
183void XglRenderFramework::InitRenderTarget(XGL_DEPTH_STENCIL_BIND_INFO *dsBinding)
184{
185 InitRenderTarget(1, dsBinding);
186}
187
188void XglRenderFramework::InitRenderTarget(uint32_t targets, XGL_DEPTH_STENCIL_BIND_INFO *dsBinding)
189{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600190 std::vector<XGL_ATTACHMENT_LOAD_OP> load_ops;
191 std::vector<XGL_ATTACHMENT_STORE_OP> store_ops;
192 std::vector<XGL_CLEAR_COLOR> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600193
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600194 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800195
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700196 for (i = 0; i < targets; i++) {
Chia-I Wuf50ee212014-12-29 14:31:52 +0800197 XglImage *img = new XglImage(m_device);
198 img->init(m_width, m_height, m_render_target_fmt,
199 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
200 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700201 m_colorBindings[i].view = img->targetView();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000202 m_colorBindings[i].layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700203 m_renderTargets.push_back(img);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600204 load_ops.push_back(XGL_ATTACHMENT_LOAD_OP_LOAD);
205 store_ops.push_back(XGL_ATTACHMENT_STORE_OP_STORE);
206 clear_colors.push_back(m_clear_color);
Chia-I Wuecebf752014-12-05 10:45:15 +0800207 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700208 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Tony Barbourbdf0a312015-04-01 17:10:07 -0600209 XGL_FRAMEBUFFER_CREATE_INFO fb_info = {};
210 fb_info.sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
211 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700212 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600213 fb_info.pColorAttachments = m_colorBindings;
214 fb_info.pDepthStencilAttachment = dsBinding;
215 fb_info.sampleCount = 1;
216 fb_info.width = (uint32_t)m_width;
217 fb_info.height = (uint32_t)m_height;
218 fb_info.layers = 1;
219
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600220 xglCreateFramebuffer(device(), &fb_info, &m_framebuffer);
221
Tony Barbour1c45ce02015-03-27 17:03:18 -0600222 XGL_RENDER_PASS_CREATE_INFO rp_info = {};
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700223 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
224 rp_info.renderArea.extent.width = m_width;
225 rp_info.renderArea.extent.height = m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600226
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700227 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600228 rp_info.pColorFormats = &m_render_target_fmt;
229 rp_info.pColorLayouts = &m_colorBindings[0].layout;
230 rp_info.pColorLoadOps = &load_ops[0];
231 rp_info.pColorStoreOps = &store_ops[0];
232 rp_info.pColorLoadClearValues = &clear_colors[0];
233 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600234 if (dsBinding) {
235 rp_info.depthStencilLayout = dsBinding->layout;
236 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700237 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600238 rp_info.depthLoadClearValue = m_depth_clear_color;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700239 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
240 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600241 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700242 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
243 xglCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600244}
245
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600246
247
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600248XglDevice::XglDevice(uint32_t id, XGL_PHYSICAL_GPU obj) :
Chia-I Wufb1459b2014-12-29 15:23:20 +0800249 xgl_testing::Device(obj), id(id)
250{
251 init();
252
253 props = gpu().properties();
254 queue_props = &gpu().queue_properties()[0];
255}
256
257void XglDevice::get_device_queue()
258{
259 ASSERT_NE(true, graphics_queues().empty());
260 m_queue = graphics_queues()[0]->obj();
261}
262
Tony Barboure2c58df2014-11-25 13:18:32 -0700263XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
264{
265 m_device = device;
266 m_nextSlot = 0;
267
268}
269
Chia-I Wu11078b02015-01-04 16:27:24 +0800270XglDescriptorSetObj::~XglDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700271{
Chia-I Wu11078b02015-01-04 16:27:24 +0800272 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700273}
Tony Barbour82c39522014-12-04 14:33:33 -0700274
Chia-I Wu11078b02015-01-04 16:27:24 +0800275int XglDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700276{
Chia-I Wu11078b02015-01-04 16:27:24 +0800277 /* request a descriptor but do not update it */
278 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
279 tc.type = XGL_DESCRIPTOR_TYPE_RAW_BUFFER;
280 tc.count = 1;
281 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700282
Chia-I Wu11078b02015-01-04 16:27:24 +0800283 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700284}
Tony Barbour82c39522014-12-04 14:33:33 -0700285
Chia-I Wu11078b02015-01-04 16:27:24 +0800286int XglDescriptorSetObj::AppendBuffer(XGL_DESCRIPTOR_TYPE type, XglConstantBufferObj *constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700287{
Chia-I Wu11078b02015-01-04 16:27:24 +0800288 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
289 tc.type = type;
290 tc.count = 1;
291 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700292
Chia-I Wu11078b02015-01-04 16:27:24 +0800293 m_bufferInfo.push_back(&constantBuffer->m_bufferViewInfo);
294
295 m_updateBuffers.push_back(xgl_testing::DescriptorSet::update(type, m_nextSlot, 1,
296 (const XGL_BUFFER_VIEW_ATTACH_INFO **) NULL));
297
298 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700299}
Tony Barbour82c39522014-12-04 14:33:33 -0700300
Chia-I Wu11078b02015-01-04 16:27:24 +0800301int XglDescriptorSetObj::AppendSamplerTexture( XglSamplerObj* sampler, XglTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700302{
Chia-I Wu11078b02015-01-04 16:27:24 +0800303 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
304 tc.type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE;
305 tc.count = 1;
306 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700307
Chia-I Wu11078b02015-01-04 16:27:24 +0800308 XGL_SAMPLER_IMAGE_VIEW_INFO tmp = {};
309 tmp.pSampler = sampler->obj();
310 tmp.pImageView = &texture->m_textureViewInfo;
311 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700312
Chia-I Wu11078b02015-01-04 16:27:24 +0800313 m_updateSamplerTextures.push_back(xgl_testing::DescriptorSet::update(m_nextSlot, 1,
314 (const XGL_SAMPLER_IMAGE_VIEW_INFO *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700315
Chia-I Wu11078b02015-01-04 16:27:24 +0800316 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700317}
Chia-I Wu11078b02015-01-04 16:27:24 +0800318
319XGL_DESCRIPTOR_SET_LAYOUT XglDescriptorSetObj::GetLayout()
Tony Barbourb5f4d082014-12-17 10:54:03 -0700320{
Chia-I Wu11078b02015-01-04 16:27:24 +0800321 return m_layout.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700322}
323
324XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle()
325{
Chia-I Wu11078b02015-01-04 16:27:24 +0800326 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700327}
Tony Barboure2c58df2014-11-25 13:18:32 -0700328
Chia-I Wu11078b02015-01-04 16:27:24 +0800329void XglDescriptorSetObj::CreateXGLDescriptorSet(XglCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700330{
Chia-I Wu11078b02015-01-04 16:27:24 +0800331 // create XGL_DESCRIPTOR_REGION
332 XGL_DESCRIPTOR_REGION_CREATE_INFO region = {};
333 region.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO;
334 region.count = m_type_counts.size();
335 region.pTypeCount = &m_type_counts[0];
336 init(*m_device, XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1, region);
Tony Barbour824b7712014-12-18 17:06:21 -0700337
Chia-I Wu11078b02015-01-04 16:27:24 +0800338 // create XGL_DESCRIPTOR_SET_LAYOUT
339 vector<XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO> layout;
340 layout.resize(m_type_counts.size());
341 for (int i = 0; i < m_type_counts.size(); i++) {
342 layout[i].sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
343 layout[i].descriptorType = m_type_counts[i].type;
344 layout[i].count = m_type_counts[i].count;
345 layout[i].stageFlags = XGL_SHADER_STAGE_FLAGS_ALL;
346 layout[i].immutableSampler = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700347
Chia-I Wu11078b02015-01-04 16:27:24 +0800348 if (i < m_type_counts.size() - 1)
349 layout[i].pNext = &layout[i + 1];
350 else
351 layout[i].pNext = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700352 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800353
Chia-I Wu11078b02015-01-04 16:27:24 +0800354 m_layout.init(*m_device, 0, layout[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700355
Chia-I Wu11078b02015-01-04 16:27:24 +0800356 // create XGL_DESCRIPTOR_SET
357 m_set = alloc_sets(XGL_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
358
359 // build the update chain
360 for (int i = 0; i < m_updateBuffers.size(); i++) {
361 m_updateBuffers[i].pBufferViews = &m_bufferInfo[i];
362
363 if (i < m_updateBuffers.size() - 1)
364 m_updateBuffers[i].pNext = &m_updateBuffers[i + 1];
365 else if (m_updateSamplerTextures.empty())
366 m_updateBuffers[i].pNext = NULL;
367 else
368 m_updateBuffers[i].pNext = &m_updateSamplerTextures[0];
369 }
370 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
371 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
372
373 if (i < m_updateSamplerTextures.size() - 1)
374 m_updateSamplerTextures[i].pNext = &m_updateSamplerTextures[i + 1];
375 else
376 m_updateSamplerTextures[i].pNext = NULL;
377 }
378 const void *chain = (!m_updateBuffers.empty()) ? (const void *) &m_updateBuffers[0] :
379 (!m_updateSamplerTextures.empty()) ? (const void *) &m_updateSamplerTextures[0] :
380 NULL;
381
382 // do the updates
383 m_device->begin_descriptor_region_update(XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
384 clear_sets(*m_set);
385 m_set->update(chain);
386 m_device->end_descriptor_region_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700387}
Tony Barboure2c58df2014-11-25 13:18:32 -0700388
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800389XglImage::XglImage(XglDevice *dev)
390{
391 m_device = dev;
392 m_imageInfo.view = XGL_NULL_HANDLE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000393 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800394}
395
Tony Barbour579f7802015-04-03 15:11:43 -0600396static bool IsCompatible(XGL_FLAGS usage, XGL_FLAGS features)
397{
398 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) &&
399 !(features & XGL_FORMAT_IMAGE_SHADER_READ_BIT))
400 return false;
401 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT) &&
402 !(features & XGL_FORMAT_IMAGE_SHADER_WRITE_BIT))
403 return false;
404 return true;
405}
406
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600407void XglImage::init(uint32_t w, uint32_t h,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800408 XGL_FORMAT fmt, XGL_FLAGS usage,
Tony Barbour579f7802015-04-03 15:11:43 -0600409 XGL_IMAGE_TILING requested_tiling)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800410{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600411 uint32_t mipCount;
Tony Barbour579f7802015-04-03 15:11:43 -0600412 XGL_FORMAT_PROPERTIES image_fmt;
413 XGL_IMAGE_TILING tiling;
414 XGL_RESULT err;
415 size_t size;
416
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800417 mipCount = 0;
418
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600419 uint32_t _w = w;
420 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800421 while( ( _w > 0 ) || ( _h > 0 ) )
422 {
423 _w >>= 1;
424 _h >>= 1;
425 mipCount++;
426 }
427
Tony Barbour579f7802015-04-03 15:11:43 -0600428 size = sizeof(image_fmt);
429 err = xglGetFormatInfo(m_device->obj(), fmt,
430 XGL_INFO_TYPE_FORMAT_PROPERTIES,
431 &size, &image_fmt);
432 ASSERT_XGL_SUCCESS(err);
433
434 if (requested_tiling == XGL_LINEAR_TILING) {
435 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
436 tiling = XGL_LINEAR_TILING;
437 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
438 tiling = XGL_OPTIMAL_TILING;
439 } else {
440 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
441 }
442 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
443 tiling = XGL_OPTIMAL_TILING;
444 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
445 tiling = XGL_LINEAR_TILING;
446 } else {
447 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
448 }
449
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800450 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
451 imageCreateInfo.imageType = XGL_IMAGE_2D;
452 imageCreateInfo.format = fmt;
453 imageCreateInfo.extent.width = w;
454 imageCreateInfo.extent.height = h;
455 imageCreateInfo.mipLevels = mipCount;
456 imageCreateInfo.tiling = tiling;
457
458 imageCreateInfo.usage = usage;
459
460 xgl_testing::Image::init(*m_device, imageCreateInfo);
461
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000462 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800463}
464
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600465XGL_RESULT XglImage::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800466{
467 *ptr = map();
468 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
469}
470
471XGL_RESULT XglImage::UnmapMemory()
472{
473 unmap();
474 return XGL_SUCCESS;
475}
476
Tony Barbour5dc515d2015-04-01 17:47:06 -0600477XGL_RESULT XglImage::CopyImage(XglImage &fromImage)
478{
479 XGL_RESULT err;
480
481 XGL_CMD_BUFFER cmd_buf;
482 XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {};
483 cmd_buf_create_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
484 cmd_buf_create_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700485 cmd_buf_create_info.queueNodeIndex = m_device->graphics_queue_node_index_;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600486 cmd_buf_create_info.flags = 0;
487
488 err = xglCreateCommandBuffer(m_device->device(), &cmd_buf_create_info, &cmd_buf);
489 assert(!err);
490
491 /* Copy staging texture to usable texture */
492 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {};
493 cmd_buf_begin_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
494 cmd_buf_begin_info.pNext = NULL;
495 cmd_buf_begin_info.flags = 0;
496
497 err = xglResetCommandBuffer(cmd_buf);
498 assert(!err);
499
500 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_begin_info);
501 assert(!err);
502
Tony Barbour1c45ce02015-03-27 17:03:18 -0600503 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {};
504 image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
505 image_memory_barrier.pNext = NULL;
506 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT;
507 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
508 image_memory_barrier.oldLayout = fromImage.layout();
509 image_memory_barrier.newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL;
510 image_memory_barrier.image = fromImage.obj();
511 image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
512 image_memory_barrier.subresourceRange.baseMipLevel = 0;
513 image_memory_barrier.subresourceRange.mipLevels = 1;
514 image_memory_barrier.subresourceRange.baseArraySlice = 0;
515 image_memory_barrier.subresourceRange.arraySize = 0;
516
517 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
518
519 XGL_PIPE_EVENT pipe_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
520 XGL_PIPELINE_BARRIER pipeline_barrier;
521 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
522 pipeline_barrier.pNext = NULL;
523 pipeline_barrier.eventCount = 1;
524 pipeline_barrier.pEvents = pipe_events;
525 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
526 pipeline_barrier.memBarrierCount = 1;
527 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
528
529 // write barrier to the command buffer
530 xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier);
531
532 image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
533 image_memory_barrier.pNext = NULL;
534 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT;
535 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
536 image_memory_barrier.oldLayout = this->layout();
537 image_memory_barrier.newLayout = XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL;
538 image_memory_barrier.image = this->obj();
539 image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
540 image_memory_barrier.subresourceRange.baseMipLevel = 0;
541 image_memory_barrier.subresourceRange.mipLevels = 1;
542 image_memory_barrier.subresourceRange.baseArraySlice = 0;
543 image_memory_barrier.subresourceRange.arraySize = 0;
544
545 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
546 pipeline_barrier.pNext = NULL;
547 pipeline_barrier.eventCount = 1;
548 pipeline_barrier.pEvents = pipe_events;
549 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
550 pipeline_barrier.memBarrierCount = 1;
551 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
552
553 // write barrier to the command buffer
554 xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier);
555
Tony Barbour5dc515d2015-04-01 17:47:06 -0600556 XGL_IMAGE_COPY copy_region = {};
557 copy_region.srcSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
558 copy_region.srcSubresource.arraySlice = 0;
559 copy_region.srcSubresource.mipLevel = 0;
560 copy_region.srcOffset.x = 0;
561 copy_region.srcOffset.y = 0;
562 copy_region.srcOffset.z = 0;
563 copy_region.destSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
564 copy_region.destSubresource.arraySlice = 0;
565 copy_region.destSubresource.mipLevel = 0;
566 copy_region.destOffset.x = 0;
567 copy_region.destOffset.y = 0;
568 copy_region.destOffset.z = 0;
569 copy_region.extent = fromImage.extent();
570
Tony Barbour1c45ce02015-03-27 17:03:18 -0600571 xglCmdCopyImage(cmd_buf, fromImage.obj(), fromImage.layout(),
572 obj(), XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
573 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600574
Tony Barbour5dc515d2015-04-01 17:47:06 -0600575 image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
576 image_memory_barrier.pNext = NULL;
577 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT;
578 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600579 image_memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL;
580 image_memory_barrier.newLayout = fromImage.layout();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600581 image_memory_barrier.image = fromImage.obj();
582 image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
583 image_memory_barrier.subresourceRange.baseMipLevel = 0;
584 image_memory_barrier.subresourceRange.mipLevels = 1;
585 image_memory_barrier.subresourceRange.baseArraySlice = 0;
586 image_memory_barrier.subresourceRange.arraySize = 0;
587
Tony Barbour5dc515d2015-04-01 17:47:06 -0600588 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
589 pipeline_barrier.pNext = NULL;
590 pipeline_barrier.eventCount = 1;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600591 pipeline_barrier.pEvents = pipe_events;
592 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
593 pipeline_barrier.memBarrierCount = 1;
594 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
595
596 // write barrier to the command buffer
597 xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier);
598
599 image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
600 image_memory_barrier.pNext = NULL;
601 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT;
602 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
603 image_memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL;
604 image_memory_barrier.newLayout = this->layout();
605 image_memory_barrier.image = this->obj();
606 image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
607 image_memory_barrier.subresourceRange.baseMipLevel = 0;
608 image_memory_barrier.subresourceRange.mipLevels = 1;
609 image_memory_barrier.subresourceRange.baseArraySlice = 0;
610 image_memory_barrier.subresourceRange.arraySize = 0;
611
612 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
613 pipeline_barrier.pNext = NULL;
614 pipeline_barrier.eventCount = 1;
615 pipeline_barrier.pEvents = pipe_events;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600616 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
617 pipeline_barrier.memBarrierCount = 1;
618 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
619
620 // write barrier to the command buffer
621 xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier);
622
623 err = xglEndCommandBuffer(cmd_buf);
624 assert(!err);
625
626 const XGL_CMD_BUFFER cmd_bufs[] = { cmd_buf };
627 XGL_MEMORY_REF mem_refs[16];
628 uint32_t num_refs = 0;
629 const std::vector<XGL_GPU_MEMORY> from_mems = fromImage.memories();
630 const std::vector<XGL_GPU_MEMORY> to_mems = memories();
631
632 for (uint32_t j = 0; j < from_mems.size(); j++) {
633 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
634 mem_refs[num_refs].mem = from_mems[j];
635 num_refs++;
636 assert(num_refs < 16);
637 }
638
639 for (uint32_t j = 0; j < to_mems.size(); j++) {
640 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
641 mem_refs[num_refs].mem = to_mems[j];
642 num_refs++;
643 assert(num_refs < 16);
644 }
645
646 err = xglQueueSubmit(m_device->m_queue, 1, cmd_bufs,
647 num_refs, mem_refs, XGL_NULL_HANDLE);
648 assert(!err);
649
650 err = xglQueueWaitIdle(m_device->m_queue);
651 assert(!err);
652
653 xglDestroyObject(cmd_buf);
654
655 return XGL_SUCCESS;
656}
657
Tony Barbourebc093f2015-04-01 16:38:10 -0600658XglTextureObj::XglTextureObj(XglDevice *device, uint32_t *colors)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600659 :XglImage(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700660{
661 m_device = device;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700662 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600663 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600664 void *data;
665 int32_t x, y;
666 XglImage stagingImage(device);
667
668 stagingImage.init(16, 16, tex_format, 0, XGL_LINEAR_TILING);
669 XGL_SUBRESOURCE_LAYOUT layout = stagingImage.subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600670
671 if (colors == NULL)
672 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700673
674 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
675
676 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
677
Tony Barbour5dc515d2015-04-01 17:47:06 -0600678 XGL_IMAGE_VIEW_CREATE_INFO view = {};
Tony Barbourbdf0a312015-04-01 17:10:07 -0600679 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
680 view.pNext = NULL;
681 view.image = XGL_NULL_HANDLE;
682 view.viewType = XGL_IMAGE_VIEW_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600683 view.format = tex_format;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600684 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
685 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
686 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
687 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
688 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
689 view.subresourceRange.baseMipLevel = 0;
690 view.subresourceRange.mipLevels = 1;
691 view.subresourceRange.baseArraySlice = 0;
692 view.subresourceRange.arraySize = 1;
693 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700694
Tony Barboure2c58df2014-11-25 13:18:32 -0700695 /* create image */
Tony Barbour5dc515d2015-04-01 17:47:06 -0600696 init(16, 16, tex_format, XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, XGL_OPTIMAL_TILING);
Tony Barboure2c58df2014-11-25 13:18:32 -0700697
698 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800699 view.image = obj();
700 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600701 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700702
Tony Barbour5dc515d2015-04-01 17:47:06 -0600703 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700704
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800705 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700706 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800707 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600708 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700709 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600710 stagingImage.unmap();
711 XglImage::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700712}
Tony Barbour82c39522014-12-04 14:33:33 -0700713
Tony Barboure2c58df2014-11-25 13:18:32 -0700714XglSamplerObj::XglSamplerObj(XglDevice *device)
715{
Tony Barboure2c58df2014-11-25 13:18:32 -0700716 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700717
Chia-I Wue9864b52014-12-28 16:32:24 +0800718 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
719 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
720 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
721 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
722 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
723 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
724 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
725 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
726 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
727 samplerCreateInfo.mipLodBias = 0.0;
728 samplerCreateInfo.maxAnisotropy = 0.0;
729 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
730 samplerCreateInfo.minLod = 0.0;
731 samplerCreateInfo.maxLod = 0.0;
732 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700733
Chia-I Wue9864b52014-12-28 16:32:24 +0800734 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700735}
Tony Barboure2c58df2014-11-25 13:18:32 -0700736
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700737/*
738 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
739 */
740XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
741{
742 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700743 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700744
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800745 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700746}
747
Tony Barbour044703b2015-03-26 12:37:52 -0600748XglConstantBufferObj::~XglConstantBufferObj()
749{
750 if (m_commandBuffer) {
751 delete m_commandBuffer;
752 }
753}
754
Tony Barboure2c58df2014-11-25 13:18:32 -0700755XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
756{
Tony Barboure2c58df2014-11-25 13:18:32 -0700757 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800758 m_commandBuffer = 0;
759
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800760 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700761 m_numVertices = constantCount;
762 m_stride = constantSize;
763
Chia-I Wua07fee62014-12-28 15:26:08 +0800764 const size_t allocationSize = constantCount * constantSize;
765 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700766
Chia-I Wua07fee62014-12-28 15:26:08 +0800767 void *pData = map();
768 memcpy(pData, data, allocationSize);
769 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700770
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800771 // set up the buffer view for the constant buffer
772 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
773 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
774 view_info.buffer = obj();
Chia-I Wubb0c8d22015-01-16 22:31:25 +0800775 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800776 view_info.offset = 0;
777 view_info.range = allocationSize;
778 m_bufferView.init(*m_device, view_info);
779
780 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
781 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700782}
Tony Barbour82c39522014-12-04 14:33:33 -0700783
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600784void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700785{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800786 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700787}
788
789
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000790void XglConstantBufferObj::BufferMemoryBarrier(
791 XGL_FLAGS outputMask /*=
792 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
793 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
794 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
795 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
796 XGL_MEMORY_OUTPUT_COPY_BIT*/,
797 XGL_FLAGS inputMask /*=
798 XGL_MEMORY_INPUT_CPU_READ_BIT |
799 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
800 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
801 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
802 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
803 XGL_MEMORY_INPUT_SHADER_READ_BIT |
804 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
805 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
806 XGL_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700807{
Tony Barbour38422802014-12-10 14:36:31 -0700808 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700809
Tony Barbour38422802014-12-10 14:36:31 -0700810 if (!m_commandBuffer)
811 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800812 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700813
814 m_commandBuffer = new XglCommandBufferObj(m_device);
815
816 }
817 else
818 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800819 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700820 }
821
Tony Barboure2c58df2014-11-25 13:18:32 -0700822 // open the command buffer
Tony Barbourbdf0a312015-04-01 17:10:07 -0600823 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {};
824 cmd_buf_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
825 cmd_buf_info.pNext = NULL;
826 cmd_buf_info.flags = 0;
827
Jon Ashburnc4164b12014-12-31 17:10:47 -0700828 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700829 ASSERT_XGL_SUCCESS(err);
830
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000831 XGL_BUFFER_MEMORY_BARRIER memory_barrier =
832 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Mark Lobodzinski837ef922015-01-29 14:24:14 -0600833 XGL_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700834
Courtney Goeltzenleuchter1e8f3be2015-03-24 18:02:34 -0600835 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000836 XGL_PIPELINE_BARRIER pipeline_barrier = {};
837 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
838 pipeline_barrier.eventCount = 1;
839 pipeline_barrier.pEvents = set_events;
840 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
841 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -0600842 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000843
844 // write barrier to the command buffer
845 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700846
847 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700848 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700849 ASSERT_XGL_SUCCESS(err);
850
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600851 uint32_t numMemRefs=1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700852 XGL_MEMORY_REF memRefs;
853 // this command buffer only uses the vertex buffer memory
854 memRefs.flags = 0;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800855 memRefs.mem = memories()[0];
Tony Barboure2c58df2014-11-25 13:18:32 -0700856
857 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700858 XGL_CMD_BUFFER bufferArray[1];
859 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800860 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700861 ASSERT_XGL_SUCCESS(err);
862}
863
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700864XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
865 : XglConstantBufferObj(device)
866{
867
868}
869
870void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
871{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700872 XGL_FORMAT viewFormat;
873
874 m_numVertices = numIndexes;
875 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700876 switch (indexType) {
877 case XGL_INDEX_8:
878 m_stride = 1;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700879 viewFormat = XGL_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700880 break;
881 case XGL_INDEX_16:
882 m_stride = 2;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700883 viewFormat = XGL_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700884 break;
885 case XGL_INDEX_32:
886 m_stride = 4;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700887 viewFormat = XGL_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700888 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800889 default:
890 assert(!"unknown index type");
891 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700892 }
893
Chia-I Wua07fee62014-12-28 15:26:08 +0800894 const size_t allocationSize = numIndexes * m_stride;
895 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700896
Chia-I Wua07fee62014-12-28 15:26:08 +0800897 void *pData = map();
898 memcpy(pData, data, allocationSize);
899 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700900
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800901 // set up the buffer view for the constant buffer
902 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
903 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
904 view_info.buffer = obj();
905 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700906 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800907 view_info.offset = 0;
908 view_info.range = allocationSize;
909 m_bufferView.init(*m_device, view_info);
910
911 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
912 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700913}
914
915void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
916{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800917 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700918}
Tony Barboure2c58df2014-11-25 13:18:32 -0700919
Tony Barbouraf1f9192014-12-17 10:57:58 -0700920XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
921{
922 return m_indexType;
923}
924
Chia-I Wu11078b02015-01-04 16:27:24 +0800925XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700926{
Tony Barboure2c58df2014-11-25 13:18:32 -0700927 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
928 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
929 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800930 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700931 stageInfo->shader.linkConstBufferCount = 0;
932 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700933
Tony Barboure2c58df2014-11-25 13:18:32 -0700934 return stageInfo;
935}
936
Tony Barboure2c58df2014-11-25 13:18:32 -0700937XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
938{
939 XGL_RESULT err = XGL_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600940 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700941 XGL_SHADER_CREATE_INFO createInfo;
942 size_t shader_len;
943
944 m_stage = stage;
945 m_device = device;
946
947 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
948 createInfo.pNext = NULL;
949
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600950 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700951
952 shader_len = strlen(shader_code);
953 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
954 createInfo.pCode = malloc(createInfo.codeSize);
955 createInfo.flags = 0;
956
957 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600958 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700959 ((uint32_t *) createInfo.pCode)[1] = 0;
960 ((uint32_t *) createInfo.pCode)[2] = stage;
961 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
962
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800963 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700964 }
965
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600966 if (framework->m_use_spv || err) {
967 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700968 err = XGL_SUCCESS;
969
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600970 // Use Reference GLSL to SPV compiler
971 framework->GLSLtoSPV(stage, shader_code, spv);
972 createInfo.pCode = spv.data();
973 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700974 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700975
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800976 init(*m_device, createInfo);
977 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700978}
Tony Barbour82c39522014-12-04 14:33:33 -0700979
Tony Barboure2c58df2014-11-25 13:18:32 -0700980XglPipelineObj::XglPipelineObj(XglDevice *device)
981{
Tony Barboure2c58df2014-11-25 13:18:32 -0700982 m_device = device;
983 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
984 m_vertexBufferCount = 0;
985
986 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
987 m_ia_state.pNext = XGL_NULL_HANDLE;
988 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
989 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700990 m_ia_state.primitiveRestartEnable = XGL_FALSE;
991 m_ia_state.primitiveRestartIndex = 0;
992
993 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
994 m_rs_state.pNext = &m_ia_state;
995 m_rs_state.depthClipEnable = XGL_FALSE;
996 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Chia-I Wuc8d1ec52015-03-24 11:01:50 +0800997 m_rs_state.programPointSize = XGL_FALSE;
998 m_rs_state.pointOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700999 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
1000 m_rs_state.fillMode = XGL_FILL_SOLID;
1001 m_rs_state.cullMode = XGL_CULL_NONE;
1002 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -07001003
Tony Barboure2c58df2014-11-25 13:18:32 -07001004 memset(&m_cb_state,0,sizeof(m_cb_state));
1005 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
1006 m_cb_state.pNext = &m_rs_state;
1007 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001008 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
1009
Tony Barbourf52346d2015-01-16 14:27:35 -07001010 m_ms_state.pNext = &m_cb_state;
1011 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1012 m_ms_state.multisampleEnable = XGL_FALSE;
1013 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1014 m_ms_state.samples = 1;
1015 m_ms_state.minSampleShading = 0;
1016 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001017
Tony Barbourf52346d2015-01-16 14:27:35 -07001018 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1019 m_ds_state.pNext = &m_ms_state,
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001020 m_ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourf52346d2015-01-16 14:27:35 -07001021 m_ds_state.depthTestEnable = XGL_FALSE;
1022 m_ds_state.depthWriteEnable = XGL_FALSE;
1023 m_ds_state.depthBoundsEnable = XGL_FALSE;
1024 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
1025 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
1026 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1027 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1028 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
1029 m_ds_state.stencilTestEnable = XGL_FALSE;
1030 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001031
Tony Barbourf52346d2015-01-16 14:27:35 -07001032 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
1033 att.blendEnable = XGL_FALSE;
Tony Barboura53a6942015-02-25 11:25:11 -07001034 att.format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001035 att.channelWriteMask = 0xf;
1036 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001037
1038};
1039
1040void XglPipelineObj::AddShader(XglShaderObj* shader)
1041{
1042 m_shaderObjs.push_back(shader);
1043}
1044
1045void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
1046{
1047 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1048 m_vi_state.attributeCount = count;
1049}
1050
1051void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
1052{
1053 m_vi_state.pVertexBindingDescriptions = vi_binding;
1054 m_vi_state.bindingCount = count;
1055}
1056
1057void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
1058{
1059 m_vertexBufferObjs.push_back(vertexDataBuffer);
1060 m_vertexBufferBindings.push_back(binding);
1061 m_vertexBufferCount++;
1062}
1063
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001064void XglPipelineObj::AddColorAttachment(uint32_t binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001065{
Tony Barbourf52346d2015-01-16 14:27:35 -07001066 if (binding+1 > m_colorAttachments.size())
1067 {
1068 m_colorAttachments.resize(binding+1);
1069 }
1070 m_colorAttachments[binding] = *att;
1071}
1072
1073void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
1074{
1075 m_ds_state.format = ds_state->format;
1076 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1077 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1078 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
1079 m_ds_state.depthFunc = ds_state->depthFunc;
1080 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1081 m_ds_state.back = ds_state->back;
1082 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001083}
1084
Tony Barbour976e1cf2014-12-17 11:57:31 -07001085void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
1086{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001087 void* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001088 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1089
1090 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
1091
1092 for (int i=0; i<m_shaderObjs.size(); i++)
1093 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001094 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001095 shaderCreateInfo->pNext = head_ptr;
1096 head_ptr = shaderCreateInfo;
1097 }
1098
1099 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1100 {
1101 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1102 m_vi_state.pNext = head_ptr;
1103 head_ptr = &m_vi_state;
1104 }
1105
1106 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1107 info.pNext = head_ptr;
1108 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +08001109 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001110
Tony Barbourf52346d2015-01-16 14:27:35 -07001111 m_cb_state.attachmentCount = m_colorAttachments.size();
1112 m_cb_state.pAttachments = &m_colorAttachments[0];
1113
Chia-I Wu2648d092014-12-29 14:24:14 +08001114 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001115}
Chia-I Wu2648d092014-12-29 14:24:14 +08001116
Tony Barbour976e1cf2014-12-17 11:57:31 -07001117XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
1118{
Chia-I Wu2648d092014-12-29 14:24:14 +08001119 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001120}
1121
Tony Barbour5420af02014-12-03 13:58:15 -07001122void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -07001123{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001124 void* head_ptr = &m_ds_state;
Tony Barboure2c58df2014-11-25 13:18:32 -07001125 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1126
1127 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -07001128
1129 for (int i=0; i<m_shaderObjs.size(); i++)
1130 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001131 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barboure2c58df2014-11-25 13:18:32 -07001132 shaderCreateInfo->pNext = head_ptr;
1133 head_ptr = shaderCreateInfo;
1134 }
1135
1136 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1137 {
1138 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1139 m_vi_state.pNext = head_ptr;
1140 head_ptr = &m_vi_state;
1141 }
1142
1143 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1144 info.pNext = head_ptr;
1145 info.flags = 0;
Chia-I Wu11078b02015-01-04 16:27:24 +08001146 info.lastSetLayout = descriptorSet->GetLayout();
Tony Barboure2c58df2014-11-25 13:18:32 -07001147
Chia-I Wu2648d092014-12-29 14:24:14 +08001148 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -07001149
Chia-I Wu2648d092014-12-29 14:24:14 +08001150 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -07001151
1152
1153 for (int i=0; i < m_vertexBufferCount; i++)
1154 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001155 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001156 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001157}
Tony Barbour82c39522014-12-04 14:33:33 -07001158
Tony Barboure2c58df2014-11-25 13:18:32 -07001159XglMemoryRefManager::XglMemoryRefManager() {
1160
1161}
Tony Barbour82c39522014-12-04 14:33:33 -07001162
Tony Barbour1c45ce02015-03-27 17:03:18 -06001163void XglMemoryRefManager::AddMemoryRef(xgl_testing::Object *xglObject) {
1164 const std::vector<XGL_GPU_MEMORY> mems = xglObject->memories();
1165 for (size_t i = 0; i < mems.size(); i++) {
1166 m_bufferObjs.push_back(mems[i]);
1167 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001168}
Tony Barbour82c39522014-12-04 14:33:33 -07001169
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001170void XglMemoryRefManager::AddMemoryRef(XGL_GPU_MEMORY *mem, uint32_t refCount) {
1171 for (size_t i = 0; i < refCount; i++) {
1172 m_bufferObjs.push_back(mem[i]);
1173 }
1174}
1175
1176void XglMemoryRefManager::AddRTMemoryRefs(vector<XglImage*>images, uint32_t rtCount) {
1177 for (uint32_t i = 0; i < rtCount; i++) {
1178 const std::vector<XGL_GPU_MEMORY> mems = images[i]->memories();
1179 if (!mems.empty())
1180 m_bufferObjs.push_back(mems[0]);
1181 }
1182}
1183
Tony Barboure2c58df2014-11-25 13:18:32 -07001184XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
1185
1186 XGL_MEMORY_REF *localRefs;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001187 uint32_t numRefs=m_bufferObjs.size();
Tony Barboure2c58df2014-11-25 13:18:32 -07001188
1189 if (numRefs <= 0)
1190 return NULL;
1191
1192 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
1193 for (int i=0; i<numRefs; i++)
1194 {
1195 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +08001196 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -07001197 }
1198 return localRefs;
1199}
1200int XglMemoryRefManager::GetNumRefs() {
1201 return m_bufferObjs.size();
1202}
Tony Barbour6d047bf2014-12-10 14:34:45 -07001203
1204XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -07001205 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001206{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001207 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001208}
Tony Barbour471338d2014-12-10 17:28:39 -07001209
Tony Barbour6d047bf2014-12-10 14:34:45 -07001210XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1211{
Chia-I Wud28343c2014-12-28 15:12:48 +08001212 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001213}
Tony Barbour471338d2014-12-10 17:28:39 -07001214
Jon Ashburnc4164b12014-12-31 17:10:47 -07001215XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001216{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001217 begin(pInfo);
1218 return XGL_SUCCESS;
1219}
1220
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001221XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj, XGL_FRAMEBUFFER framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001222{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001223 begin(renderpass_obj, framebuffer_obj);
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001224 return XGL_SUCCESS;
1225}
1226
1227XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
1228{
1229 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +08001230 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001231}
1232
1233XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1234{
Chia-I Wud28343c2014-12-28 15:12:48 +08001235 end();
1236 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001237}
1238
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001239void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001240{
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001241 xglCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001242}
1243
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001244void XglCommandBufferObj::ClearAllBuffers(XGL_CLEAR_COLOR clear_color, float depth_clear_color, uint32_t stencil_clear_color,
1245 XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001246{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001247 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001248 const XGL_FLAGS output_mask =
1249 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1250 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1251 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1252 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1253 XGL_MEMORY_OUTPUT_COPY_BIT;
1254 const XGL_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001255
1256 // whatever we want to do, we do it to the whole buffer
1257 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1258 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1259 srRange.baseMipLevel = 0;
1260 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1261 srRange.baseArraySlice = 0;
1262 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1263
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001264 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1265 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1266 memory_barrier.outputMask = output_mask;
1267 memory_barrier.inputMask = input_mask;
1268 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1269 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001270 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001271
Courtney Goeltzenleuchter1e8f3be2015-03-24 18:02:34 -06001272 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001273 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1274 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1275 pipeline_barrier.eventCount = 1;
1276 pipeline_barrier.pEvents = set_events;
1277 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1278 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001279 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001280
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001281 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001282 memory_barrier.image = m_renderTargets[i]->image();
1283 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1284 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1285 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001286
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -06001287 xglCmdClearColorImage(obj(),
1288 m_renderTargets[i]->image(), XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
1289 clear_color, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001290 }
1291
1292 if (depthStencilImage)
1293 {
1294 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1295 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1296 dsRange.baseMipLevel = 0;
1297 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1298 dsRange.baseArraySlice = 0;
1299 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1300
1301 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001302
1303 memory_barrier.oldLayout = depthStencilBinding->layout;
1304 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1305 memory_barrier.image = depthStencilImage;
1306 memory_barrier.subresourceRange = dsRange;
1307
1308 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1309 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001310
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -06001311 xglCmdClearDepthStencil(obj(),
1312 depthStencilImage, XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001313 depth_clear_color, stencil_clear_color,
1314 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001315
1316 // prepare depth buffer for rendering
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001317 memory_barrier.image = depthStencilImage;
1318 memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1319 memory_barrier.newLayout = depthStencilBinding->layout;
1320 memory_barrier.subresourceRange = dsRange;
1321 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1322 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001323 }
1324}
1325
Jon Ashburncdc40be2015-01-02 18:27:14 -07001326void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001327{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001328 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001329 const XGL_FLAGS output_mask =
1330 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1331 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1332 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1333 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1334 XGL_MEMORY_OUTPUT_COPY_BIT;
1335 const XGL_FLAGS input_mask =
1336 XGL_MEMORY_INPUT_CPU_READ_BIT |
1337 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1338 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
1339 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1340 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
1341 XGL_MEMORY_INPUT_SHADER_READ_BIT |
1342 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1343 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1344 XGL_MEMORY_INPUT_COPY_BIT;
1345
Tony Barbour30cc9e82014-12-17 11:53:55 -07001346 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1347 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1348 srRange.baseMipLevel = 0;
1349 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1350 srRange.baseArraySlice = 0;
1351 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1352
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001353 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1354 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1355 memory_barrier.outputMask = output_mask;
1356 memory_barrier.inputMask = input_mask;
1357 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1358 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001359 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001360
Courtney Goeltzenleuchter1e8f3be2015-03-24 18:02:34 -06001361 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001362 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1363 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1364 pipeline_barrier.eventCount = 1;
1365 pipeline_barrier.pEvents = set_events;
1366 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1367 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001368 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001369
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001370 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001371 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001372 memory_barrier.image = m_renderTargets[i]->image();
1373 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1374 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1375 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001376 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001377}
1378
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001379void XglCommandBufferObj::BeginRenderPass(XGL_RENDER_PASS renderpass, XGL_FRAMEBUFFER framebuffer)
1380{
1381 XGL_RENDER_PASS_BEGIN rp_begin = {
1382 renderpass,
1383 framebuffer,
1384 };
1385
1386 xglCmdBeginRenderPass( obj(), &rp_begin);
1387}
1388
1389void XglCommandBufferObj::EndRenderPass(XGL_RENDER_PASS renderpass)
1390{
1391 xglCmdEndRenderPass( obj(), renderpass);
1392}
1393
Tony Barbourf52346d2015-01-16 14:27:35 -07001394void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001395{
Tony Barbourf52346d2015-01-16 14:27:35 -07001396 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001397}
1398
1399void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1400{
1401 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001402}
1403
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001404void XglCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001405{
Chia-I Wud28343c2014-12-28 15:12:48 +08001406 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001407}
1408
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001409void XglCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001410{
Chia-I Wud28343c2014-12-28 15:12:48 +08001411 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001412}
1413
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001414void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, uint32_t numMemRefs)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001415{
1416 XGL_RESULT err = XGL_SUCCESS;
1417
1418 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001419 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001420 ASSERT_XGL_SUCCESS( err );
1421
1422 err = xglQueueWaitIdle( m_device->m_queue );
1423 ASSERT_XGL_SUCCESS( err );
1424
1425 // Wait for work to finish before cleaning up.
1426 xglDeviceWaitIdle(m_device->device());
1427
1428}
1429void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1430{
Chia-I Wud28343c2014-12-28 15:12:48 +08001431 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001432}
1433
1434void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1435{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001436 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wu11078b02015-01-04 16:27:24 +08001437 xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001438}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001439void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001440{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001441 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001442}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001443void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001444{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001445 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001446}
Tony Barbour1c45ce02015-03-27 17:03:18 -06001447XglDepthStencilObj::XglDepthStencilObj()
1448{
1449 m_initialized = false;
1450}
1451bool XglDepthStencilObj::Initialized()
1452{
1453 return m_initialized;
1454}
1455
1456XGL_DEPTH_STENCIL_BIND_INFO* XglDepthStencilObj::BindInfo()
1457{
1458 return &m_depthStencilBindInfo;
1459}
1460
1461void XglDepthStencilObj::Init(XglDevice *device, int32_t width, int32_t height)
1462{
1463 XGL_IMAGE_CREATE_INFO image_info;
1464 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view_info;
1465
1466 m_device = device;
1467 m_initialized = true;
1468 m_depth_stencil_fmt = XGL_FMT_D16_UNORM;
1469
1470 image_info.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1471 image_info.pNext = NULL;
1472 image_info.imageType = XGL_IMAGE_2D;
1473 image_info.format = m_depth_stencil_fmt;
1474 image_info.extent.width = width;
1475 image_info.extent.height = height;
1476 image_info.extent.depth = 1;
1477 image_info.mipLevels = 1;
1478 image_info.arraySize = 1;
1479 image_info.samples = 1;
1480 image_info.tiling = XGL_OPTIMAL_TILING;
1481 image_info.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
1482 image_info.flags = 0;
1483 init(*m_device, image_info);
1484
1485 view_info.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
1486 view_info.pNext = NULL;
1487 view_info.image = XGL_NULL_HANDLE;
1488 view_info.mipLevel = 0;
1489 view_info.baseArraySlice = 0;
1490 view_info.arraySize = 1;
1491 view_info.flags = 0;
1492 view_info.image = obj();
1493 m_depthStencilView.init(*m_device, view_info);
1494
1495 m_depthStencilBindInfo.view = m_depthStencilView.obj();
1496 m_depthStencilBindInfo.layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
1497}