blob: cd7414c378df570a58791f2be0c99c5fc9cf59b1 [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 = {};
Chia-I Wu47a5d6f2015-03-26 15:26:05 +0800279 tc.type = XGL_DESCRIPTOR_TYPE_SHADER_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800280 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 Wu41126e52015-03-26 15:27:55 +0800293 m_updateBuffers.push_back(xgl_testing::DescriptorSet::update(type,
294 m_nextSlot, 0, 1, &constantBuffer->m_bufferViewInfo));
Chia-I Wu11078b02015-01-04 16:27:24 +0800295
296 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700297}
Tony Barbour82c39522014-12-04 14:33:33 -0700298
Chia-I Wu11078b02015-01-04 16:27:24 +0800299int XglDescriptorSetObj::AppendSamplerTexture( XglSamplerObj* sampler, XglTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700300{
Chia-I Wu11078b02015-01-04 16:27:24 +0800301 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
302 tc.type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE;
303 tc.count = 1;
304 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700305
Chia-I Wu11078b02015-01-04 16:27:24 +0800306 XGL_SAMPLER_IMAGE_VIEW_INFO tmp = {};
Chia-I Wu41126e52015-03-26 15:27:55 +0800307 tmp.sampler = sampler->obj();
Chia-I Wu11078b02015-01-04 16:27:24 +0800308 tmp.pImageView = &texture->m_textureViewInfo;
309 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700310
Chia-I Wu41126e52015-03-26 15:27:55 +0800311 m_updateSamplerTextures.push_back(xgl_testing::DescriptorSet::update(m_nextSlot, 0, 1,
Chia-I Wu11078b02015-01-04 16:27:24 +0800312 (const XGL_SAMPLER_IMAGE_VIEW_INFO *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700313
Chia-I Wu11078b02015-01-04 16:27:24 +0800314 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700315}
Chia-I Wu11078b02015-01-04 16:27:24 +0800316
Chia-I Wu53f07d72015-03-28 15:23:55 +0800317XGL_DESCRIPTOR_SET_LAYOUT_CHAIN XglDescriptorSetObj::GetLayoutChain() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700318{
Chia-I Wu41126e52015-03-26 15:27:55 +0800319 return m_layout_chain.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700320}
321
Chia-I Wu53f07d72015-03-28 15:23:55 +0800322XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700323{
Chia-I Wu11078b02015-01-04 16:27:24 +0800324 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700325}
Tony Barboure2c58df2014-11-25 13:18:32 -0700326
Chia-I Wu11078b02015-01-04 16:27:24 +0800327void XglDescriptorSetObj::CreateXGLDescriptorSet(XglCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700328{
Chia-I Wu985ba162015-03-26 13:14:16 +0800329 // create XGL_DESCRIPTOR_POOL
330 XGL_DESCRIPTOR_POOL_CREATE_INFO pool = {};
331 pool.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
332 pool.count = m_type_counts.size();
333 pool.pTypeCount = &m_type_counts[0];
334 init(*m_device, XGL_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700335
Chia-I Wu11078b02015-01-04 16:27:24 +0800336 // create XGL_DESCRIPTOR_SET_LAYOUT
Chia-I Wub41393e2015-03-26 15:04:41 +0800337 vector<XGL_DESCRIPTOR_SET_LAYOUT_BINDING> bindings;
338 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800339 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800340 bindings[i].descriptorType = m_type_counts[i].type;
341 bindings[i].count = m_type_counts[i].count;
342 bindings[i].stageFlags = XGL_SHADER_STAGE_FLAGS_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800343 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700344 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800345
Chia-I Wub41393e2015-03-26 15:04:41 +0800346 // create XGL_DESCRIPTOR_SET_LAYOUT
347 XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO layout = {};
348 layout.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
349 layout.count = bindings.size();
350 layout.pBinding = &bindings[0];
351
Chia-I Wu41126e52015-03-26 15:27:55 +0800352 m_layout.init(*m_device, layout);
353
354 vector<const xgl_testing::DescriptorSetLayout *> layouts;
355 layouts.push_back(&m_layout);
356 m_layout_chain.init(*m_device, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700357
Chia-I Wu11078b02015-01-04 16:27:24 +0800358 // create XGL_DESCRIPTOR_SET
359 m_set = alloc_sets(XGL_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
360
Chia-I Wu41126e52015-03-26 15:27:55 +0800361 // build the update array
362 vector<const void *> update_array;
Chia-I Wu11078b02015-01-04 16:27:24 +0800363
Chia-I Wu41126e52015-03-26 15:27:55 +0800364 for (int i = 0; i < m_updateBuffers.size(); i++) {
365 update_array.push_back(&m_updateBuffers[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800366 }
367 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
368 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
Chia-I Wu41126e52015-03-26 15:27:55 +0800369 update_array.push_back(&m_updateSamplerTextures[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800370 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800371
372 // do the updates
Chia-I Wu985ba162015-03-26 13:14:16 +0800373 m_device->begin_descriptor_pool_update(XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu11078b02015-01-04 16:27:24 +0800374 clear_sets(*m_set);
Chia-I Wu41126e52015-03-26 15:27:55 +0800375 m_set->update(update_array);
Chia-I Wu985ba162015-03-26 13:14:16 +0800376 m_device->end_descriptor_pool_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700377}
Tony Barboure2c58df2014-11-25 13:18:32 -0700378
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800379XglImage::XglImage(XglDevice *dev)
380{
381 m_device = dev;
382 m_imageInfo.view = XGL_NULL_HANDLE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000383 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800384}
385
Tony Barbour579f7802015-04-03 15:11:43 -0600386static bool IsCompatible(XGL_FLAGS usage, XGL_FLAGS features)
387{
388 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) &&
389 !(features & XGL_FORMAT_IMAGE_SHADER_READ_BIT))
390 return false;
391 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT) &&
392 !(features & XGL_FORMAT_IMAGE_SHADER_WRITE_BIT))
393 return false;
394 return true;
395}
396
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600397void XglImage::init(uint32_t w, uint32_t h,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800398 XGL_FORMAT fmt, XGL_FLAGS usage,
Tony Barbour579f7802015-04-03 15:11:43 -0600399 XGL_IMAGE_TILING requested_tiling)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800400{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600401 uint32_t mipCount;
Tony Barbour579f7802015-04-03 15:11:43 -0600402 XGL_FORMAT_PROPERTIES image_fmt;
403 XGL_IMAGE_TILING tiling;
404 XGL_RESULT err;
405 size_t size;
406
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800407 mipCount = 0;
408
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600409 uint32_t _w = w;
410 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800411 while( ( _w > 0 ) || ( _h > 0 ) )
412 {
413 _w >>= 1;
414 _h >>= 1;
415 mipCount++;
416 }
417
Tony Barbour579f7802015-04-03 15:11:43 -0600418 size = sizeof(image_fmt);
419 err = xglGetFormatInfo(m_device->obj(), fmt,
420 XGL_INFO_TYPE_FORMAT_PROPERTIES,
421 &size, &image_fmt);
422 ASSERT_XGL_SUCCESS(err);
423
424 if (requested_tiling == XGL_LINEAR_TILING) {
425 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
426 tiling = XGL_LINEAR_TILING;
427 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
428 tiling = XGL_OPTIMAL_TILING;
429 } else {
430 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
431 }
432 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
433 tiling = XGL_OPTIMAL_TILING;
434 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
435 tiling = XGL_LINEAR_TILING;
436 } else {
437 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
438 }
439
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800440 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
441 imageCreateInfo.imageType = XGL_IMAGE_2D;
442 imageCreateInfo.format = fmt;
443 imageCreateInfo.extent.width = w;
444 imageCreateInfo.extent.height = h;
445 imageCreateInfo.mipLevels = mipCount;
446 imageCreateInfo.tiling = tiling;
447
448 imageCreateInfo.usage = usage;
449
450 xgl_testing::Image::init(*m_device, imageCreateInfo);
451
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000452 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800453}
454
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600455XGL_RESULT XglImage::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800456{
457 *ptr = map();
458 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
459}
460
461XGL_RESULT XglImage::UnmapMemory()
462{
463 unmap();
464 return XGL_SUCCESS;
465}
466
Tony Barbour5dc515d2015-04-01 17:47:06 -0600467XGL_RESULT XglImage::CopyImage(XglImage &fromImage)
468{
469 XGL_RESULT err;
470
471 XGL_CMD_BUFFER cmd_buf;
472 XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {};
473 cmd_buf_create_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
474 cmd_buf_create_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700475 cmd_buf_create_info.queueNodeIndex = m_device->graphics_queue_node_index_;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600476 cmd_buf_create_info.flags = 0;
477
478 err = xglCreateCommandBuffer(m_device->device(), &cmd_buf_create_info, &cmd_buf);
479 assert(!err);
480
481 /* Copy staging texture to usable texture */
482 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {};
483 cmd_buf_begin_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
484 cmd_buf_begin_info.pNext = NULL;
485 cmd_buf_begin_info.flags = 0;
486
487 err = xglResetCommandBuffer(cmd_buf);
488 assert(!err);
489
490 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_begin_info);
491 assert(!err);
492
Tony Barbour1c45ce02015-03-27 17:03:18 -0600493 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {};
494 image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
495 image_memory_barrier.pNext = NULL;
496 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT;
497 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
498 image_memory_barrier.oldLayout = fromImage.layout();
499 image_memory_barrier.newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL;
500 image_memory_barrier.image = fromImage.obj();
501 image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
502 image_memory_barrier.subresourceRange.baseMipLevel = 0;
503 image_memory_barrier.subresourceRange.mipLevels = 1;
504 image_memory_barrier.subresourceRange.baseArraySlice = 0;
505 image_memory_barrier.subresourceRange.arraySize = 0;
506
507 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
508
509 XGL_PIPE_EVENT pipe_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
510 XGL_PIPELINE_BARRIER pipeline_barrier;
511 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
512 pipeline_barrier.pNext = NULL;
513 pipeline_barrier.eventCount = 1;
514 pipeline_barrier.pEvents = pipe_events;
515 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
516 pipeline_barrier.memBarrierCount = 1;
517 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
518
519 // write barrier to the command buffer
520 xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier);
521
522 image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
523 image_memory_barrier.pNext = NULL;
524 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT;
525 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
526 image_memory_barrier.oldLayout = this->layout();
527 image_memory_barrier.newLayout = XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL;
528 image_memory_barrier.image = this->obj();
529 image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
530 image_memory_barrier.subresourceRange.baseMipLevel = 0;
531 image_memory_barrier.subresourceRange.mipLevels = 1;
532 image_memory_barrier.subresourceRange.baseArraySlice = 0;
533 image_memory_barrier.subresourceRange.arraySize = 0;
534
535 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
536 pipeline_barrier.pNext = NULL;
537 pipeline_barrier.eventCount = 1;
538 pipeline_barrier.pEvents = pipe_events;
539 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
540 pipeline_barrier.memBarrierCount = 1;
541 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
542
543 // write barrier to the command buffer
544 xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier);
545
Tony Barbour5dc515d2015-04-01 17:47:06 -0600546 XGL_IMAGE_COPY copy_region = {};
547 copy_region.srcSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
548 copy_region.srcSubresource.arraySlice = 0;
549 copy_region.srcSubresource.mipLevel = 0;
550 copy_region.srcOffset.x = 0;
551 copy_region.srcOffset.y = 0;
552 copy_region.srcOffset.z = 0;
553 copy_region.destSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
554 copy_region.destSubresource.arraySlice = 0;
555 copy_region.destSubresource.mipLevel = 0;
556 copy_region.destOffset.x = 0;
557 copy_region.destOffset.y = 0;
558 copy_region.destOffset.z = 0;
559 copy_region.extent = fromImage.extent();
560
Tony Barbour1c45ce02015-03-27 17:03:18 -0600561 xglCmdCopyImage(cmd_buf, fromImage.obj(), fromImage.layout(),
562 obj(), XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
563 1, &copy_region);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600564
Tony Barbour5dc515d2015-04-01 17:47:06 -0600565 image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
566 image_memory_barrier.pNext = NULL;
567 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT;
568 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600569 image_memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL;
570 image_memory_barrier.newLayout = fromImage.layout();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600571 image_memory_barrier.image = fromImage.obj();
572 image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
573 image_memory_barrier.subresourceRange.baseMipLevel = 0;
574 image_memory_barrier.subresourceRange.mipLevels = 1;
575 image_memory_barrier.subresourceRange.baseArraySlice = 0;
576 image_memory_barrier.subresourceRange.arraySize = 0;
577
Tony Barbour5dc515d2015-04-01 17:47:06 -0600578 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
579 pipeline_barrier.pNext = NULL;
580 pipeline_barrier.eventCount = 1;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600581 pipeline_barrier.pEvents = pipe_events;
582 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
583 pipeline_barrier.memBarrierCount = 1;
584 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
585
586 // write barrier to the command buffer
587 xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier);
588
589 image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
590 image_memory_barrier.pNext = NULL;
591 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT;
592 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
593 image_memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL;
594 image_memory_barrier.newLayout = this->layout();
595 image_memory_barrier.image = this->obj();
596 image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
597 image_memory_barrier.subresourceRange.baseMipLevel = 0;
598 image_memory_barrier.subresourceRange.mipLevels = 1;
599 image_memory_barrier.subresourceRange.baseArraySlice = 0;
600 image_memory_barrier.subresourceRange.arraySize = 0;
601
602 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
603 pipeline_barrier.pNext = NULL;
604 pipeline_barrier.eventCount = 1;
605 pipeline_barrier.pEvents = pipe_events;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600606 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
607 pipeline_barrier.memBarrierCount = 1;
608 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
609
610 // write barrier to the command buffer
611 xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier);
612
613 err = xglEndCommandBuffer(cmd_buf);
614 assert(!err);
615
616 const XGL_CMD_BUFFER cmd_bufs[] = { cmd_buf };
617 XGL_MEMORY_REF mem_refs[16];
618 uint32_t num_refs = 0;
619 const std::vector<XGL_GPU_MEMORY> from_mems = fromImage.memories();
620 const std::vector<XGL_GPU_MEMORY> to_mems = memories();
621
622 for (uint32_t j = 0; j < from_mems.size(); j++) {
623 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
624 mem_refs[num_refs].mem = from_mems[j];
625 num_refs++;
626 assert(num_refs < 16);
627 }
628
629 for (uint32_t j = 0; j < to_mems.size(); j++) {
630 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
631 mem_refs[num_refs].mem = to_mems[j];
632 num_refs++;
633 assert(num_refs < 16);
634 }
635
636 err = xglQueueSubmit(m_device->m_queue, 1, cmd_bufs,
637 num_refs, mem_refs, XGL_NULL_HANDLE);
638 assert(!err);
639
640 err = xglQueueWaitIdle(m_device->m_queue);
641 assert(!err);
642
643 xglDestroyObject(cmd_buf);
644
645 return XGL_SUCCESS;
646}
647
Tony Barbourebc093f2015-04-01 16:38:10 -0600648XglTextureObj::XglTextureObj(XglDevice *device, uint32_t *colors)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600649 :XglImage(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700650{
651 m_device = device;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700652 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600653 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600654 void *data;
655 int32_t x, y;
656 XglImage stagingImage(device);
657
658 stagingImage.init(16, 16, tex_format, 0, XGL_LINEAR_TILING);
659 XGL_SUBRESOURCE_LAYOUT layout = stagingImage.subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600660
661 if (colors == NULL)
662 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700663
664 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
665
666 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
667
Tony Barbour5dc515d2015-04-01 17:47:06 -0600668 XGL_IMAGE_VIEW_CREATE_INFO view = {};
Tony Barbourbdf0a312015-04-01 17:10:07 -0600669 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
670 view.pNext = NULL;
671 view.image = XGL_NULL_HANDLE;
672 view.viewType = XGL_IMAGE_VIEW_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600673 view.format = tex_format;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600674 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
675 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
676 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
677 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
678 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
679 view.subresourceRange.baseMipLevel = 0;
680 view.subresourceRange.mipLevels = 1;
681 view.subresourceRange.baseArraySlice = 0;
682 view.subresourceRange.arraySize = 1;
683 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700684
Tony Barboure2c58df2014-11-25 13:18:32 -0700685 /* create image */
Tony Barbour5dc515d2015-04-01 17:47:06 -0600686 init(16, 16, tex_format, XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, XGL_OPTIMAL_TILING);
Tony Barboure2c58df2014-11-25 13:18:32 -0700687
688 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800689 view.image = obj();
690 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600691 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700692
Tony Barbour5dc515d2015-04-01 17:47:06 -0600693 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700694
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800695 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700696 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800697 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600698 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700699 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600700 stagingImage.unmap();
701 XglImage::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700702}
Tony Barbour82c39522014-12-04 14:33:33 -0700703
Tony Barboure2c58df2014-11-25 13:18:32 -0700704XglSamplerObj::XglSamplerObj(XglDevice *device)
705{
Tony Barboure2c58df2014-11-25 13:18:32 -0700706 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700707
Chia-I Wue9864b52014-12-28 16:32:24 +0800708 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
709 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
710 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
711 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
712 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
713 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
714 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
715 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
716 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
717 samplerCreateInfo.mipLodBias = 0.0;
718 samplerCreateInfo.maxAnisotropy = 0.0;
719 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
720 samplerCreateInfo.minLod = 0.0;
721 samplerCreateInfo.maxLod = 0.0;
722 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700723
Chia-I Wue9864b52014-12-28 16:32:24 +0800724 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700725}
Tony Barboure2c58df2014-11-25 13:18:32 -0700726
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700727/*
728 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
729 */
730XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
731{
732 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700733 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700734
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800735 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700736}
737
Tony Barbour044703b2015-03-26 12:37:52 -0600738XglConstantBufferObj::~XglConstantBufferObj()
739{
740 if (m_commandBuffer) {
741 delete m_commandBuffer;
742 }
743}
744
Tony Barboure2c58df2014-11-25 13:18:32 -0700745XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
746{
Tony Barboure2c58df2014-11-25 13:18:32 -0700747 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800748 m_commandBuffer = 0;
749
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800750 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700751 m_numVertices = constantCount;
752 m_stride = constantSize;
753
Chia-I Wua07fee62014-12-28 15:26:08 +0800754 const size_t allocationSize = constantCount * constantSize;
755 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700756
Chia-I Wua07fee62014-12-28 15:26:08 +0800757 void *pData = map();
758 memcpy(pData, data, allocationSize);
759 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700760
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800761 // set up the buffer view for the constant buffer
762 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
763 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
764 view_info.buffer = obj();
Chia-I Wubb0c8d22015-01-16 22:31:25 +0800765 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800766 view_info.offset = 0;
767 view_info.range = allocationSize;
768 m_bufferView.init(*m_device, view_info);
769
770 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
771 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700772}
Tony Barbour82c39522014-12-04 14:33:33 -0700773
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600774void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700775{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800776 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700777}
778
779
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000780void XglConstantBufferObj::BufferMemoryBarrier(
781 XGL_FLAGS outputMask /*=
782 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
783 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
784 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
785 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
786 XGL_MEMORY_OUTPUT_COPY_BIT*/,
787 XGL_FLAGS inputMask /*=
788 XGL_MEMORY_INPUT_CPU_READ_BIT |
789 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
790 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
791 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
792 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
793 XGL_MEMORY_INPUT_SHADER_READ_BIT |
794 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
795 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
796 XGL_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700797{
Tony Barbour38422802014-12-10 14:36:31 -0700798 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700799
Tony Barbour38422802014-12-10 14:36:31 -0700800 if (!m_commandBuffer)
801 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800802 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700803
804 m_commandBuffer = new XglCommandBufferObj(m_device);
805
806 }
807 else
808 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800809 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700810 }
811
Tony Barboure2c58df2014-11-25 13:18:32 -0700812 // open the command buffer
Tony Barbourbdf0a312015-04-01 17:10:07 -0600813 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {};
814 cmd_buf_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
815 cmd_buf_info.pNext = NULL;
816 cmd_buf_info.flags = 0;
817
Jon Ashburnc4164b12014-12-31 17:10:47 -0700818 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700819 ASSERT_XGL_SUCCESS(err);
820
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000821 XGL_BUFFER_MEMORY_BARRIER memory_barrier =
822 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Mark Lobodzinski837ef922015-01-29 14:24:14 -0600823 XGL_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700824
Courtney Goeltzenleuchter1e8f3be2015-03-24 18:02:34 -0600825 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000826 XGL_PIPELINE_BARRIER pipeline_barrier = {};
827 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
828 pipeline_barrier.eventCount = 1;
829 pipeline_barrier.pEvents = set_events;
830 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
831 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -0600832 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000833
834 // write barrier to the command buffer
835 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700836
837 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700838 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700839 ASSERT_XGL_SUCCESS(err);
840
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600841 uint32_t numMemRefs=1;
Tony Barboure2c58df2014-11-25 13:18:32 -0700842 XGL_MEMORY_REF memRefs;
843 // this command buffer only uses the vertex buffer memory
844 memRefs.flags = 0;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800845 memRefs.mem = memories()[0];
Tony Barboure2c58df2014-11-25 13:18:32 -0700846
847 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700848 XGL_CMD_BUFFER bufferArray[1];
849 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800850 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700851 ASSERT_XGL_SUCCESS(err);
852}
853
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700854XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
855 : XglConstantBufferObj(device)
856{
857
858}
859
860void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
861{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700862 XGL_FORMAT viewFormat;
863
864 m_numVertices = numIndexes;
865 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700866 switch (indexType) {
867 case XGL_INDEX_8:
868 m_stride = 1;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700869 viewFormat = XGL_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700870 break;
871 case XGL_INDEX_16:
872 m_stride = 2;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700873 viewFormat = XGL_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700874 break;
875 case XGL_INDEX_32:
876 m_stride = 4;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700877 viewFormat = XGL_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700878 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800879 default:
880 assert(!"unknown index type");
881 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700882 }
883
Chia-I Wua07fee62014-12-28 15:26:08 +0800884 const size_t allocationSize = numIndexes * m_stride;
885 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700886
Chia-I Wua07fee62014-12-28 15:26:08 +0800887 void *pData = map();
888 memcpy(pData, data, allocationSize);
889 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700890
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800891 // set up the buffer view for the constant buffer
892 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
893 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
894 view_info.buffer = obj();
895 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700896 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800897 view_info.offset = 0;
898 view_info.range = allocationSize;
899 m_bufferView.init(*m_device, view_info);
900
901 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
902 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700903}
904
905void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
906{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800907 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700908}
Tony Barboure2c58df2014-11-25 13:18:32 -0700909
Tony Barbouraf1f9192014-12-17 10:57:58 -0700910XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
911{
912 return m_indexType;
913}
914
Chia-I Wu11078b02015-01-04 16:27:24 +0800915XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700916{
Tony Barboure2c58df2014-11-25 13:18:32 -0700917 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
918 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
919 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800920 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700921 stageInfo->shader.linkConstBufferCount = 0;
922 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700923
Tony Barboure2c58df2014-11-25 13:18:32 -0700924 return stageInfo;
925}
926
Tony Barboure2c58df2014-11-25 13:18:32 -0700927XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
928{
929 XGL_RESULT err = XGL_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600930 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700931 XGL_SHADER_CREATE_INFO createInfo;
932 size_t shader_len;
933
934 m_stage = stage;
935 m_device = device;
936
937 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
938 createInfo.pNext = NULL;
939
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600940 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700941
942 shader_len = strlen(shader_code);
943 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
944 createInfo.pCode = malloc(createInfo.codeSize);
945 createInfo.flags = 0;
946
947 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600948 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700949 ((uint32_t *) createInfo.pCode)[1] = 0;
950 ((uint32_t *) createInfo.pCode)[2] = stage;
951 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
952
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800953 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700954 }
955
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600956 if (framework->m_use_spv || err) {
957 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700958 err = XGL_SUCCESS;
959
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600960 // Use Reference GLSL to SPV compiler
961 framework->GLSLtoSPV(stage, shader_code, spv);
962 createInfo.pCode = spv.data();
963 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700964 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700965
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800966 init(*m_device, createInfo);
967 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700968}
Tony Barbour82c39522014-12-04 14:33:33 -0700969
Tony Barboure2c58df2014-11-25 13:18:32 -0700970XglPipelineObj::XglPipelineObj(XglDevice *device)
971{
Tony Barboure2c58df2014-11-25 13:18:32 -0700972 m_device = device;
973 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
974 m_vertexBufferCount = 0;
975
976 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
977 m_ia_state.pNext = XGL_NULL_HANDLE;
978 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
979 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700980 m_ia_state.primitiveRestartEnable = XGL_FALSE;
981 m_ia_state.primitiveRestartIndex = 0;
982
983 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
984 m_rs_state.pNext = &m_ia_state;
985 m_rs_state.depthClipEnable = XGL_FALSE;
986 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Chia-I Wuc8d1ec52015-03-24 11:01:50 +0800987 m_rs_state.programPointSize = XGL_FALSE;
988 m_rs_state.pointOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700989 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
990 m_rs_state.fillMode = XGL_FILL_SOLID;
991 m_rs_state.cullMode = XGL_CULL_NONE;
992 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700993
Tony Barboure2c58df2014-11-25 13:18:32 -0700994 memset(&m_cb_state,0,sizeof(m_cb_state));
995 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
996 m_cb_state.pNext = &m_rs_state;
997 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700998 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
999
Tony Barbourf52346d2015-01-16 14:27:35 -07001000 m_ms_state.pNext = &m_cb_state;
1001 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1002 m_ms_state.multisampleEnable = XGL_FALSE;
1003 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1004 m_ms_state.samples = 1;
1005 m_ms_state.minSampleShading = 0;
1006 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001007
Tony Barbourf52346d2015-01-16 14:27:35 -07001008 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1009 m_ds_state.pNext = &m_ms_state,
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001010 m_ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourf52346d2015-01-16 14:27:35 -07001011 m_ds_state.depthTestEnable = XGL_FALSE;
1012 m_ds_state.depthWriteEnable = XGL_FALSE;
1013 m_ds_state.depthBoundsEnable = XGL_FALSE;
1014 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
1015 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
1016 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1017 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1018 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
1019 m_ds_state.stencilTestEnable = XGL_FALSE;
1020 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001021
Tony Barbourf52346d2015-01-16 14:27:35 -07001022 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
1023 att.blendEnable = XGL_FALSE;
Tony Barboura53a6942015-02-25 11:25:11 -07001024 att.format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001025 att.channelWriteMask = 0xf;
1026 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001027
1028};
1029
1030void XglPipelineObj::AddShader(XglShaderObj* shader)
1031{
1032 m_shaderObjs.push_back(shader);
1033}
1034
1035void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
1036{
1037 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1038 m_vi_state.attributeCount = count;
1039}
1040
1041void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
1042{
1043 m_vi_state.pVertexBindingDescriptions = vi_binding;
1044 m_vi_state.bindingCount = count;
1045}
1046
1047void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
1048{
1049 m_vertexBufferObjs.push_back(vertexDataBuffer);
1050 m_vertexBufferBindings.push_back(binding);
1051 m_vertexBufferCount++;
1052}
1053
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001054void XglPipelineObj::AddColorAttachment(uint32_t binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001055{
Tony Barbourf52346d2015-01-16 14:27:35 -07001056 if (binding+1 > m_colorAttachments.size())
1057 {
1058 m_colorAttachments.resize(binding+1);
1059 }
1060 m_colorAttachments[binding] = *att;
1061}
1062
1063void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
1064{
1065 m_ds_state.format = ds_state->format;
1066 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1067 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1068 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
1069 m_ds_state.depthFunc = ds_state->depthFunc;
1070 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1071 m_ds_state.back = ds_state->back;
1072 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001073}
1074
Tony Barbour976e1cf2014-12-17 11:57:31 -07001075void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
1076{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001077 void* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001078 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1079
1080 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
1081
1082 for (int i=0; i<m_shaderObjs.size(); i++)
1083 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001084 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001085 shaderCreateInfo->pNext = head_ptr;
1086 head_ptr = shaderCreateInfo;
1087 }
1088
1089 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1090 {
1091 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1092 m_vi_state.pNext = head_ptr;
1093 head_ptr = &m_vi_state;
1094 }
1095
1096 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1097 info.pNext = head_ptr;
1098 info.flags = 0;
Chia-I Wu41126e52015-03-26 15:27:55 +08001099 info.pSetLayoutChain = descriptorSet->GetLayoutChain();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001100
Tony Barbourf52346d2015-01-16 14:27:35 -07001101 m_cb_state.attachmentCount = m_colorAttachments.size();
1102 m_cb_state.pAttachments = &m_colorAttachments[0];
1103
Chia-I Wu2648d092014-12-29 14:24:14 +08001104 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001105}
Chia-I Wu2648d092014-12-29 14:24:14 +08001106
Tony Barbour976e1cf2014-12-17 11:57:31 -07001107XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
1108{
Chia-I Wu2648d092014-12-29 14:24:14 +08001109 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001110}
1111
Tony Barbour5420af02014-12-03 13:58:15 -07001112void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -07001113{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001114 void* head_ptr = &m_ds_state;
Tony Barboure2c58df2014-11-25 13:18:32 -07001115 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1116
1117 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -07001118
1119 for (int i=0; i<m_shaderObjs.size(); i++)
1120 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001121 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barboure2c58df2014-11-25 13:18:32 -07001122 shaderCreateInfo->pNext = head_ptr;
1123 head_ptr = shaderCreateInfo;
1124 }
1125
1126 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1127 {
1128 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1129 m_vi_state.pNext = head_ptr;
1130 head_ptr = &m_vi_state;
1131 }
1132
1133 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1134 info.pNext = head_ptr;
1135 info.flags = 0;
Chia-I Wu41126e52015-03-26 15:27:55 +08001136 info.pSetLayoutChain = descriptorSet->GetLayoutChain();
Tony Barboure2c58df2014-11-25 13:18:32 -07001137
Chia-I Wu2648d092014-12-29 14:24:14 +08001138 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -07001139
Chia-I Wu2648d092014-12-29 14:24:14 +08001140 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -07001141
1142
1143 for (int i=0; i < m_vertexBufferCount; i++)
1144 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001145 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001146 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001147}
Tony Barbour82c39522014-12-04 14:33:33 -07001148
Tony Barboure2c58df2014-11-25 13:18:32 -07001149XglMemoryRefManager::XglMemoryRefManager() {
1150
1151}
Tony Barbour82c39522014-12-04 14:33:33 -07001152
Tony Barbour1c45ce02015-03-27 17:03:18 -06001153void XglMemoryRefManager::AddMemoryRef(xgl_testing::Object *xglObject) {
1154 const std::vector<XGL_GPU_MEMORY> mems = xglObject->memories();
1155 for (size_t i = 0; i < mems.size(); i++) {
1156 m_bufferObjs.push_back(mems[i]);
1157 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001158}
Tony Barbour82c39522014-12-04 14:33:33 -07001159
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001160void XglMemoryRefManager::AddMemoryRef(XGL_GPU_MEMORY *mem, uint32_t refCount) {
1161 for (size_t i = 0; i < refCount; i++) {
1162 m_bufferObjs.push_back(mem[i]);
1163 }
1164}
1165
1166void XglMemoryRefManager::AddRTMemoryRefs(vector<XglImage*>images, uint32_t rtCount) {
1167 for (uint32_t i = 0; i < rtCount; i++) {
1168 const std::vector<XGL_GPU_MEMORY> mems = images[i]->memories();
1169 if (!mems.empty())
1170 m_bufferObjs.push_back(mems[0]);
1171 }
1172}
1173
Tony Barboure2c58df2014-11-25 13:18:32 -07001174XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
1175
1176 XGL_MEMORY_REF *localRefs;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001177 uint32_t numRefs=m_bufferObjs.size();
Tony Barboure2c58df2014-11-25 13:18:32 -07001178
1179 if (numRefs <= 0)
1180 return NULL;
1181
1182 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
1183 for (int i=0; i<numRefs; i++)
1184 {
1185 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +08001186 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -07001187 }
1188 return localRefs;
1189}
1190int XglMemoryRefManager::GetNumRefs() {
1191 return m_bufferObjs.size();
1192}
Tony Barbour6d047bf2014-12-10 14:34:45 -07001193
1194XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -07001195 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001196{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001197 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001198}
Tony Barbour471338d2014-12-10 17:28:39 -07001199
Tony Barbour6d047bf2014-12-10 14:34:45 -07001200XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1201{
Chia-I Wud28343c2014-12-28 15:12:48 +08001202 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001203}
Tony Barbour471338d2014-12-10 17:28:39 -07001204
Jon Ashburnc4164b12014-12-31 17:10:47 -07001205XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001206{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001207 begin(pInfo);
1208 return XGL_SUCCESS;
1209}
1210
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001211XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj, XGL_FRAMEBUFFER framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001212{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001213 begin(renderpass_obj, framebuffer_obj);
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001214 return XGL_SUCCESS;
1215}
1216
1217XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
1218{
1219 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +08001220 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001221}
1222
1223XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1224{
Chia-I Wud28343c2014-12-28 15:12:48 +08001225 end();
1226 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001227}
1228
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001229void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001230{
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001231 xglCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001232}
1233
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001234void XglCommandBufferObj::ClearAllBuffers(XGL_CLEAR_COLOR clear_color, float depth_clear_color, uint32_t stencil_clear_color,
1235 XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001236{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001237 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001238 const XGL_FLAGS output_mask =
1239 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1240 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1241 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1242 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1243 XGL_MEMORY_OUTPUT_COPY_BIT;
1244 const XGL_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001245
1246 // whatever we want to do, we do it to the whole buffer
1247 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1248 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1249 srRange.baseMipLevel = 0;
1250 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1251 srRange.baseArraySlice = 0;
1252 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1253
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001254 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1255 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1256 memory_barrier.outputMask = output_mask;
1257 memory_barrier.inputMask = input_mask;
1258 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1259 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001260 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001261
Courtney Goeltzenleuchter1e8f3be2015-03-24 18:02:34 -06001262 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001263 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1264 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1265 pipeline_barrier.eventCount = 1;
1266 pipeline_barrier.pEvents = set_events;
1267 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1268 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001269 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001270
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001271 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001272 memory_barrier.image = m_renderTargets[i]->image();
1273 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1274 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1275 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001276
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -06001277 xglCmdClearColorImage(obj(),
1278 m_renderTargets[i]->image(), XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
1279 clear_color, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001280 }
1281
1282 if (depthStencilImage)
1283 {
1284 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1285 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1286 dsRange.baseMipLevel = 0;
1287 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1288 dsRange.baseArraySlice = 0;
1289 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1290
1291 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001292
1293 memory_barrier.oldLayout = depthStencilBinding->layout;
1294 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1295 memory_barrier.image = depthStencilImage;
1296 memory_barrier.subresourceRange = dsRange;
1297
1298 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1299 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001300
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -06001301 xglCmdClearDepthStencil(obj(),
1302 depthStencilImage, XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001303 depth_clear_color, stencil_clear_color,
1304 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001305
1306 // prepare depth buffer for rendering
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001307 memory_barrier.image = depthStencilImage;
1308 memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1309 memory_barrier.newLayout = depthStencilBinding->layout;
1310 memory_barrier.subresourceRange = dsRange;
1311 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1312 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001313 }
1314}
1315
Jon Ashburncdc40be2015-01-02 18:27:14 -07001316void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001317{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001318 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001319 const XGL_FLAGS output_mask =
1320 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1321 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1322 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1323 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1324 XGL_MEMORY_OUTPUT_COPY_BIT;
1325 const XGL_FLAGS input_mask =
1326 XGL_MEMORY_INPUT_CPU_READ_BIT |
1327 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1328 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
1329 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1330 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
1331 XGL_MEMORY_INPUT_SHADER_READ_BIT |
1332 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1333 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1334 XGL_MEMORY_INPUT_COPY_BIT;
1335
Tony Barbour30cc9e82014-12-17 11:53:55 -07001336 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1337 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1338 srRange.baseMipLevel = 0;
1339 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1340 srRange.baseArraySlice = 0;
1341 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1342
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001343 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1344 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1345 memory_barrier.outputMask = output_mask;
1346 memory_barrier.inputMask = input_mask;
1347 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1348 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001349 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001350
Courtney Goeltzenleuchter1e8f3be2015-03-24 18:02:34 -06001351 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001352 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1353 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1354 pipeline_barrier.eventCount = 1;
1355 pipeline_barrier.pEvents = set_events;
1356 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1357 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001358 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001359
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001360 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001361 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001362 memory_barrier.image = m_renderTargets[i]->image();
1363 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1364 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1365 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001366 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001367}
1368
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001369void XglCommandBufferObj::BeginRenderPass(XGL_RENDER_PASS renderpass, XGL_FRAMEBUFFER framebuffer)
1370{
1371 XGL_RENDER_PASS_BEGIN rp_begin = {
1372 renderpass,
1373 framebuffer,
1374 };
1375
1376 xglCmdBeginRenderPass( obj(), &rp_begin);
1377}
1378
1379void XglCommandBufferObj::EndRenderPass(XGL_RENDER_PASS renderpass)
1380{
1381 xglCmdEndRenderPass( obj(), renderpass);
1382}
1383
Tony Barbourf52346d2015-01-16 14:27:35 -07001384void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001385{
Tony Barbourf52346d2015-01-16 14:27:35 -07001386 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001387}
1388
1389void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1390{
1391 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001392}
1393
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001394void XglCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001395{
Chia-I Wud28343c2014-12-28 15:12:48 +08001396 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001397}
1398
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001399void XglCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001400{
Chia-I Wud28343c2014-12-28 15:12:48 +08001401 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001402}
1403
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001404void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, uint32_t numMemRefs)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001405{
1406 XGL_RESULT err = XGL_SUCCESS;
1407
1408 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001409 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001410 ASSERT_XGL_SUCCESS( err );
1411
1412 err = xglQueueWaitIdle( m_device->m_queue );
1413 ASSERT_XGL_SUCCESS( err );
1414
1415 // Wait for work to finish before cleaning up.
1416 xglDeviceWaitIdle(m_device->device());
1417
1418}
1419void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1420{
Chia-I Wud28343c2014-12-28 15:12:48 +08001421 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001422}
1423
Chia-I Wu53f07d72015-03-28 15:23:55 +08001424void XglCommandBufferObj::BindDescriptorSet(const XglDescriptorSetObj *set)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001425{
Chia-I Wu53f07d72015-03-28 15:23:55 +08001426 XGL_DESCRIPTOR_SET set_obj = set->GetDescriptorSetHandle();
1427
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001428 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wu53f07d72015-03-28 15:23:55 +08001429 xglCmdBindDescriptorSets(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS,
1430 set->GetLayoutChain(), 0, 1, &set_obj, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001431}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001432void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001433{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001434 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001435}
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001436void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001437{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001438 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001439}
Tony Barbour1c45ce02015-03-27 17:03:18 -06001440XglDepthStencilObj::XglDepthStencilObj()
1441{
1442 m_initialized = false;
1443}
1444bool XglDepthStencilObj::Initialized()
1445{
1446 return m_initialized;
1447}
1448
1449XGL_DEPTH_STENCIL_BIND_INFO* XglDepthStencilObj::BindInfo()
1450{
1451 return &m_depthStencilBindInfo;
1452}
1453
1454void XglDepthStencilObj::Init(XglDevice *device, int32_t width, int32_t height)
1455{
1456 XGL_IMAGE_CREATE_INFO image_info;
1457 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view_info;
1458
1459 m_device = device;
1460 m_initialized = true;
1461 m_depth_stencil_fmt = XGL_FMT_D16_UNORM;
1462
1463 image_info.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1464 image_info.pNext = NULL;
1465 image_info.imageType = XGL_IMAGE_2D;
1466 image_info.format = m_depth_stencil_fmt;
1467 image_info.extent.width = width;
1468 image_info.extent.height = height;
1469 image_info.extent.depth = 1;
1470 image_info.mipLevels = 1;
1471 image_info.arraySize = 1;
1472 image_info.samples = 1;
1473 image_info.tiling = XGL_OPTIMAL_TILING;
1474 image_info.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
1475 image_info.flags = 0;
1476 init(*m_device, image_info);
1477
1478 view_info.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
1479 view_info.pNext = NULL;
1480 view_info.image = XGL_NULL_HANDLE;
1481 view_info.mipLevel = 0;
1482 view_info.baseArraySlice = 0;
1483 view_info.arraySize = 1;
1484 view_info.flags = 0;
1485 view_info.image = obj();
1486 m_depthStencilView.init(*m_device, view_info);
1487
1488 m_depthStencilBindInfo.view = m_depthStencilView.obj();
1489 m_depthStencilBindInfo.layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
1490}