blob: 620b229a2f3adf904074863f6e54b35401a65a6b [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;
Jon Ashburnb317fad2015-04-04 14:52:07 -060060 XGL_INSTANCE_CREATE_INFO instInfo = {};
61 instInfo.sType = XGL_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
62 instInfo.pNext = NULL;
63 instInfo.pAppInfo = &app_info;
64 instInfo.pAllocCb = NULL;
65 instInfo.extensionCount = 0;
66 instInfo.ppEnabledExtensionNames = NULL;
67 err = xglCreateInstance(&instInfo, &this->inst);
Jon Ashburn1e464892015-01-29 15:48:00 -070068 ASSERT_XGL_SUCCESS(err);
69 err = xglEnumerateGpus(inst, XGL_MAX_PHYSICAL_GPUS, &this->gpu_count,
70 objs);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060071 ASSERT_XGL_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070072 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060073
74 m_device = new XglDevice(0, objs[0]);
75 m_device->get_device_queue();
Tony Barbour1c45ce02015-03-27 17:03:18 -060076
77 m_depthStencil = new XglDepthStencilObj();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060078}
79
80void XglRenderFramework::ShutdownFramework()
81{
82 if (m_colorBlend) xglDestroyObject(m_colorBlend);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060083 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
84 if (m_stateRaster) xglDestroyObject(m_stateRaster);
85 if (m_cmdBuffer) xglDestroyObject(m_cmdBuffer);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -060086 if (m_framebuffer) xglDestroyObject(m_framebuffer);
Tobin Ehlis976fc162015-03-26 08:23:25 -060087 if (m_renderPass) xglDestroyObject(m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060088
89 if (m_stateViewport) {
90 xglDestroyObject(m_stateViewport);
91 }
Tobin Ehlis976fc162015-03-26 08:23:25 -060092 while (!m_renderTargets.empty()) {
93 xglDestroyObject(m_renderTargets.back()->targetView());
94 xglBindObjectMemory(m_renderTargets.back()->image(), 0, XGL_NULL_HANDLE, 0);
95 xglDestroyObject(m_renderTargets.back()->image());
96 xglFreeMemory(m_renderTargets.back()->memory());
97 m_renderTargets.pop_back();
98 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060099
Tony Barbour1c45ce02015-03-27 17:03:18 -0600100 delete m_depthStencil;
101
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600102 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800103 delete m_device;
Jon Ashburn1e464892015-01-29 15:48:00 -0700104 xglDestroyInstance(this->inst);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600105}
106
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -0600107void XglRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600108{
109 XGL_RESULT err;
110
Tony Barboura53a6942015-02-25 11:25:11 -0700111 m_render_target_fmt = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600112
113 // create a raster state (solid, back-face culling)
Tony Barbourf52346d2015-01-16 14:27:35 -0700114 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster = {};
115 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
116 raster.pointSize = 1.0;
117
118 err = xglCreateDynamicRasterState( device(), &raster, &m_stateRaster );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600119 ASSERT_XGL_SUCCESS(err);
120
Tony Barbourf52346d2015-01-16 14:27:35 -0700121 XGL_DYNAMIC_CB_STATE_CREATE_INFO blend = {};
122 blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
123 err = xglCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600124 ASSERT_XGL_SUCCESS( err );
125
Tony Barbourf52346d2015-01-16 14:27:35 -0700126 XGL_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {};
127 depthStencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600128 depthStencil.minDepth = 0.f;
129 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700130 depthStencil.stencilFrontRef = 0;
131 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600132
Tony Barbourf52346d2015-01-16 14:27:35 -0700133 err = xglCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600134 ASSERT_XGL_SUCCESS( err );
135
136 XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {};
137
138 cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700139 cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_;
140
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600141 err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
142 ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed";
143}
144
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600145void XglRenderFramework::InitViewport(float width, float height)
146{
147 XGL_RESULT err;
148
Tony Barbourf52346d2015-01-16 14:27:35 -0700149 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700150 XGL_RECT scissor;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600151
Tony Barbourf52346d2015-01-16 14:27:35 -0700152 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewportCreate = {};
153 viewportCreate.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700154 viewportCreate.viewportAndScissorCount = 1;
Tony Barbourf52346d2015-01-16 14:27:35 -0700155 viewport.originX = 0;
156 viewport.originY = 0;
157 viewport.width = 1.f * width;
158 viewport.height = 1.f * height;
159 viewport.minDepth = 0.f;
160 viewport.maxDepth = 1.f;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700161 scissor.extent.width = width;
162 scissor.extent.height = height;
163 scissor.offset.x = 0;
164 scissor.offset.y = 0;
Tony Barbourf52346d2015-01-16 14:27:35 -0700165 viewportCreate.pViewports = &viewport;
Courtney Goeltzenleuchterbbe3cc52015-02-11 14:13:34 -0700166 viewportCreate.pScissors = &scissor;
Tony Barbourf52346d2015-01-16 14:27:35 -0700167
168 err = xglCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600169 ASSERT_XGL_SUCCESS( err );
170 m_width = width;
171 m_height = height;
172}
173
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600174void XglRenderFramework::InitViewport()
175{
176 InitViewport(m_width, m_height);
177}
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600178void XglRenderFramework::InitRenderTarget()
179{
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700180 InitRenderTarget(1);
181}
182
183void XglRenderFramework::InitRenderTarget(uint32_t targets)
184{
Tony Barbour1c45ce02015-03-27 17:03:18 -0600185 InitRenderTarget(targets, NULL);
186}
187
188void XglRenderFramework::InitRenderTarget(XGL_DEPTH_STENCIL_BIND_INFO *dsBinding)
189{
190 InitRenderTarget(1, dsBinding);
191}
192
193void XglRenderFramework::InitRenderTarget(uint32_t targets, XGL_DEPTH_STENCIL_BIND_INFO *dsBinding)
194{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600195 std::vector<XGL_ATTACHMENT_LOAD_OP> load_ops;
196 std::vector<XGL_ATTACHMENT_STORE_OP> store_ops;
197 std::vector<XGL_CLEAR_COLOR> clear_colors;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600198
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600199 uint32_t i;
Chia-I Wuecebf752014-12-05 10:45:15 +0800200
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700201 for (i = 0; i < targets; i++) {
Chia-I Wuf50ee212014-12-29 14:31:52 +0800202 XglImage *img = new XglImage(m_device);
203 img->init(m_width, m_height, m_render_target_fmt,
204 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
205 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700206 m_colorBindings[i].view = img->targetView();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000207 m_colorBindings[i].layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700208 m_renderTargets.push_back(img);
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600209 load_ops.push_back(XGL_ATTACHMENT_LOAD_OP_LOAD);
210 store_ops.push_back(XGL_ATTACHMENT_STORE_OP_STORE);
211 clear_colors.push_back(m_clear_color);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600212// m_mem_ref_mgr.AddMemoryRefs(*img);
Chia-I Wuecebf752014-12-05 10:45:15 +0800213 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700214 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
Tony Barbourbdf0a312015-04-01 17:10:07 -0600215 XGL_FRAMEBUFFER_CREATE_INFO fb_info = {};
216 fb_info.sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
217 fb_info.pNext = NULL;
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700218 fb_info.colorAttachmentCount = m_renderTargets.size();
Tony Barbourbdf0a312015-04-01 17:10:07 -0600219 fb_info.pColorAttachments = m_colorBindings;
220 fb_info.pDepthStencilAttachment = dsBinding;
221 fb_info.sampleCount = 1;
222 fb_info.width = (uint32_t)m_width;
223 fb_info.height = (uint32_t)m_height;
224 fb_info.layers = 1;
225
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600226 xglCreateFramebuffer(device(), &fb_info, &m_framebuffer);
227
Tony Barbour1c45ce02015-03-27 17:03:18 -0600228 XGL_RENDER_PASS_CREATE_INFO rp_info = {};
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700229 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
230 rp_info.renderArea.extent.width = m_width;
231 rp_info.renderArea.extent.height = m_height;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600232
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -0700233 rp_info.colorAttachmentCount = m_renderTargets.size();
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600234 rp_info.pColorFormats = &m_render_target_fmt;
235 rp_info.pColorLayouts = &m_colorBindings[0].layout;
236 rp_info.pColorLoadOps = &load_ops[0];
237 rp_info.pColorStoreOps = &store_ops[0];
238 rp_info.pColorLoadClearValues = &clear_colors[0];
239 rp_info.depthStencilFormat = m_depth_stencil_fmt;
Tony Barbour1c45ce02015-03-27 17:03:18 -0600240 if (dsBinding) {
241 rp_info.depthStencilLayout = dsBinding->layout;
242 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700243 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600244 rp_info.depthLoadClearValue = m_depth_clear_color;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700245 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
246 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600247 rp_info.stencilLoadClearValue = m_stencil_clear_color;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700248 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
249 xglCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600250}
251
Mark Lobodzinskic52b7752015-02-18 16:38:17 -0600252
253
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600254XglDevice::XglDevice(uint32_t id, XGL_PHYSICAL_GPU obj) :
Chia-I Wufb1459b2014-12-29 15:23:20 +0800255 xgl_testing::Device(obj), id(id)
256{
257 init();
258
259 props = gpu().properties();
260 queue_props = &gpu().queue_properties()[0];
261}
262
263void XglDevice::get_device_queue()
264{
265 ASSERT_NE(true, graphics_queues().empty());
266 m_queue = graphics_queues()[0]->obj();
267}
268
Tony Barboure2c58df2014-11-25 13:18:32 -0700269XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
270{
271 m_device = device;
272 m_nextSlot = 0;
273
274}
275
Chia-I Wu11078b02015-01-04 16:27:24 +0800276XglDescriptorSetObj::~XglDescriptorSetObj()
Tony Barboure2c58df2014-11-25 13:18:32 -0700277{
Chia-I Wu11078b02015-01-04 16:27:24 +0800278 delete m_set;
Tony Barboure2c58df2014-11-25 13:18:32 -0700279}
Tony Barbour82c39522014-12-04 14:33:33 -0700280
Chia-I Wu11078b02015-01-04 16:27:24 +0800281int XglDescriptorSetObj::AppendDummy()
Tony Barboure2c58df2014-11-25 13:18:32 -0700282{
Chia-I Wu11078b02015-01-04 16:27:24 +0800283 /* request a descriptor but do not update it */
284 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
Chia-I Wu47a5d6f2015-03-26 15:26:05 +0800285 tc.type = XGL_DESCRIPTOR_TYPE_SHADER_STORAGE_BUFFER;
Chia-I Wu11078b02015-01-04 16:27:24 +0800286 tc.count = 1;
287 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700288
Chia-I Wu11078b02015-01-04 16:27:24 +0800289 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700290}
Tony Barbour82c39522014-12-04 14:33:33 -0700291
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600292int XglDescriptorSetObj::AppendBuffer(XGL_DESCRIPTOR_TYPE type, XglConstantBufferObj &constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700293{
Chia-I Wu11078b02015-01-04 16:27:24 +0800294 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
295 tc.type = type;
296 tc.count = 1;
297 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700298
Chia-I Wu41126e52015-03-26 15:27:55 +0800299 m_updateBuffers.push_back(xgl_testing::DescriptorSet::update(type,
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600300 m_nextSlot, 0, 1, &constantBuffer.m_bufferViewInfo));
301
302 // Track mem references for this descriptor set object
303 mem_ref_mgr.AddMemoryRefs(constantBuffer);
Chia-I Wu11078b02015-01-04 16:27:24 +0800304
305 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700306}
Tony Barbour82c39522014-12-04 14:33:33 -0700307
Chia-I Wu11078b02015-01-04 16:27:24 +0800308int XglDescriptorSetObj::AppendSamplerTexture( XglSamplerObj* sampler, XglTextureObj* texture)
Tony Barboure2c58df2014-11-25 13:18:32 -0700309{
Chia-I Wu11078b02015-01-04 16:27:24 +0800310 XGL_DESCRIPTOR_TYPE_COUNT tc = {};
311 tc.type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE;
312 tc.count = 1;
313 m_type_counts.push_back(tc);
Tony Barboure2c58df2014-11-25 13:18:32 -0700314
Chia-I Wu11078b02015-01-04 16:27:24 +0800315 XGL_SAMPLER_IMAGE_VIEW_INFO tmp = {};
Chia-I Wu41126e52015-03-26 15:27:55 +0800316 tmp.sampler = sampler->obj();
Chia-I Wu11078b02015-01-04 16:27:24 +0800317 tmp.pImageView = &texture->m_textureViewInfo;
318 m_samplerTextureInfo.push_back(tmp);
Tony Barboure2c58df2014-11-25 13:18:32 -0700319
Chia-I Wu41126e52015-03-26 15:27:55 +0800320 m_updateSamplerTextures.push_back(xgl_testing::DescriptorSet::update(m_nextSlot, 0, 1,
Chia-I Wu11078b02015-01-04 16:27:24 +0800321 (const XGL_SAMPLER_IMAGE_VIEW_INFO *) NULL));
Tony Barboure2c58df2014-11-25 13:18:32 -0700322
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600323 // Track mem references for the texture referenced here
324 mem_ref_mgr.AddMemoryRefs(*texture);
325
Chia-I Wu11078b02015-01-04 16:27:24 +0800326 return m_nextSlot++;
Tony Barboure2c58df2014-11-25 13:18:32 -0700327}
Chia-I Wu11078b02015-01-04 16:27:24 +0800328
Chia-I Wu53f07d72015-03-28 15:23:55 +0800329XGL_DESCRIPTOR_SET_LAYOUT_CHAIN XglDescriptorSetObj::GetLayoutChain() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700330{
Chia-I Wu41126e52015-03-26 15:27:55 +0800331 return m_layout_chain.obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700332}
333
Chia-I Wu53f07d72015-03-28 15:23:55 +0800334XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle() const
Tony Barbourb5f4d082014-12-17 10:54:03 -0700335{
Chia-I Wu11078b02015-01-04 16:27:24 +0800336 return m_set->obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700337}
Tony Barboure2c58df2014-11-25 13:18:32 -0700338
Chia-I Wu11078b02015-01-04 16:27:24 +0800339void XglDescriptorSetObj::CreateXGLDescriptorSet(XglCommandBufferObj *cmdBuffer)
Tony Barbour824b7712014-12-18 17:06:21 -0700340{
Chia-I Wu985ba162015-03-26 13:14:16 +0800341 // create XGL_DESCRIPTOR_POOL
342 XGL_DESCRIPTOR_POOL_CREATE_INFO pool = {};
343 pool.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
344 pool.count = m_type_counts.size();
345 pool.pTypeCount = &m_type_counts[0];
346 init(*m_device, XGL_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool);
Tony Barbour824b7712014-12-18 17:06:21 -0700347
Chia-I Wu11078b02015-01-04 16:27:24 +0800348 // create XGL_DESCRIPTOR_SET_LAYOUT
Chia-I Wub41393e2015-03-26 15:04:41 +0800349 vector<XGL_DESCRIPTOR_SET_LAYOUT_BINDING> bindings;
350 bindings.resize(m_type_counts.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800351 for (int i = 0; i < m_type_counts.size(); i++) {
Chia-I Wub41393e2015-03-26 15:04:41 +0800352 bindings[i].descriptorType = m_type_counts[i].type;
353 bindings[i].count = m_type_counts[i].count;
354 bindings[i].stageFlags = XGL_SHADER_STAGE_FLAGS_ALL;
Chia-I Wu0743e832015-03-27 12:56:09 +0800355 bindings[i].pImmutableSamplers = NULL;
Tony Barboure2c58df2014-11-25 13:18:32 -0700356 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800357
Chia-I Wub41393e2015-03-26 15:04:41 +0800358 // create XGL_DESCRIPTOR_SET_LAYOUT
359 XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO layout = {};
360 layout.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
361 layout.count = bindings.size();
362 layout.pBinding = &bindings[0];
363
Chia-I Wu41126e52015-03-26 15:27:55 +0800364 m_layout.init(*m_device, layout);
365
366 vector<const xgl_testing::DescriptorSetLayout *> layouts;
367 layouts.push_back(&m_layout);
368 m_layout_chain.init(*m_device, layouts);
Tony Barboure2c58df2014-11-25 13:18:32 -0700369
Chia-I Wu11078b02015-01-04 16:27:24 +0800370 // create XGL_DESCRIPTOR_SET
371 m_set = alloc_sets(XGL_DESCRIPTOR_SET_USAGE_STATIC, m_layout);
372
Chia-I Wu41126e52015-03-26 15:27:55 +0800373 // build the update array
374 vector<const void *> update_array;
Chia-I Wu11078b02015-01-04 16:27:24 +0800375
Chia-I Wu41126e52015-03-26 15:27:55 +0800376 for (int i = 0; i < m_updateBuffers.size(); i++) {
377 update_array.push_back(&m_updateBuffers[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800378 }
379 for (int i = 0; i < m_updateSamplerTextures.size(); i++) {
380 m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i];
Chia-I Wu41126e52015-03-26 15:27:55 +0800381 update_array.push_back(&m_updateSamplerTextures[i]);
Chia-I Wu11078b02015-01-04 16:27:24 +0800382 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800383
384 // do the updates
Chia-I Wu985ba162015-03-26 13:14:16 +0800385 m_device->begin_descriptor_pool_update(XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu11078b02015-01-04 16:27:24 +0800386 clear_sets(*m_set);
Chia-I Wu41126e52015-03-26 15:27:55 +0800387 m_set->update(update_array);
Chia-I Wu985ba162015-03-26 13:14:16 +0800388 m_device->end_descriptor_pool_update(*cmdBuffer);
Tony Barbour25ef8a62014-12-03 13:59:18 -0700389}
Tony Barboure2c58df2014-11-25 13:18:32 -0700390
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800391XglImage::XglImage(XglDevice *dev)
392{
393 m_device = dev;
394 m_imageInfo.view = XGL_NULL_HANDLE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000395 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800396}
397
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600398void XglImage::ImageMemoryBarrier(
399 XglCommandBufferObj *cmd_buf,
400 XGL_IMAGE_ASPECT aspect,
401 XGL_FLAGS output_mask /*=
402 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
403 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
404 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
405 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
406 XGL_MEMORY_OUTPUT_COPY_BIT*/,
407 XGL_FLAGS input_mask /*=
408 XGL_MEMORY_INPUT_CPU_READ_BIT |
409 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
410 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
411 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
412 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
413 XGL_MEMORY_INPUT_SHADER_READ_BIT |
414 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
415 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
416 XGL_MEMORY_INPUT_COPY_BIT*/,
417 XGL_IMAGE_LAYOUT image_layout)
418{
419 const XGL_IMAGE_SUBRESOURCE_RANGE subresourceRange = subresource_range(aspect, 0, 1, 0, 1);
420 XGL_IMAGE_MEMORY_BARRIER barrier;
421 barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout,
422 subresourceRange);
423
424 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &barrier;
425
426 XGL_PIPE_EVENT pipe_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
427 XGL_PIPELINE_BARRIER pipeline_barrier = {};
428 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
429 pipeline_barrier.pNext = NULL;
430 pipeline_barrier.eventCount = 1;
431 pipeline_barrier.pEvents = pipe_events;
432 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
433 pipeline_barrier.memBarrierCount = 1;
434 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
435
436 // write barrier to the command buffer
437 xglCmdPipelineBarrier(cmd_buf->obj(), &pipeline_barrier);
438}
439
440void XglImage::SetLayout(XglCommandBufferObj *cmd_buf,
441 XGL_IMAGE_ASPECT aspect,
442 XGL_IMAGE_LAYOUT image_layout)
443{
444 XGL_FLAGS output_mask, input_mask;
445 const XGL_FLAGS all_cache_outputs =
446 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
447 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
448 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
449 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
450 XGL_MEMORY_OUTPUT_COPY_BIT;
451 const XGL_FLAGS all_cache_inputs =
452 XGL_MEMORY_INPUT_CPU_READ_BIT |
453 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
454 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
455 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
456 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
457 XGL_MEMORY_INPUT_SHADER_READ_BIT |
458 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
459 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
460 XGL_MEMORY_INPUT_COPY_BIT;
461
462 if (image_layout == m_imageInfo.layout) {
463 return;
464 }
465
466 switch (image_layout) {
467 case XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL:
468 output_mask = XGL_MEMORY_OUTPUT_COPY_BIT;
469 input_mask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
470 break;
471
472 case XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL:
473 output_mask = XGL_MEMORY_OUTPUT_COPY_BIT;
474 input_mask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
475 break;
476
477 case XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL:
478 break;
479
480 case XGL_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
481 output_mask = XGL_MEMORY_OUTPUT_COPY_BIT;
482 input_mask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT;
483 break;
484
485 default:
486 output_mask = all_cache_outputs;
487 input_mask = all_cache_inputs;
488 break;
489 }
490
491 ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout);
492 m_imageInfo.layout = image_layout;
493}
494
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600495void XglImage::SetLayout(XGL_IMAGE_ASPECT aspect,
496 XGL_IMAGE_LAYOUT image_layout)
497{
498 XGL_RESULT err;
499 XglCommandBufferObj cmd_buf(m_device);
500
501 /* Build command buffer to set image layout in the driver */
502 err = cmd_buf.BeginCommandBuffer();
503 assert(!err);
504
505 SetLayout(&cmd_buf, aspect, image_layout);
506
507 err = cmd_buf.EndCommandBuffer();
508 assert(!err);
509
510 cmd_buf.QueueCommandBuffer();
511}
512
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600513bool XglImage::IsCompatible(XGL_FLAGS usage, XGL_FLAGS features)
Tony Barbour579f7802015-04-03 15:11:43 -0600514{
515 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) &&
516 !(features & XGL_FORMAT_IMAGE_SHADER_READ_BIT))
517 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600518
Tony Barbour579f7802015-04-03 15:11:43 -0600519 if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT) &&
520 !(features & XGL_FORMAT_IMAGE_SHADER_WRITE_BIT))
521 return false;
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600522
Tony Barbour579f7802015-04-03 15:11:43 -0600523 return true;
524}
525
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600526void XglImage::init(uint32_t w, uint32_t h,
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800527 XGL_FORMAT fmt, XGL_FLAGS usage,
Tony Barbour579f7802015-04-03 15:11:43 -0600528 XGL_IMAGE_TILING requested_tiling)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800529{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600530 uint32_t mipCount;
Tony Barbour579f7802015-04-03 15:11:43 -0600531 XGL_FORMAT_PROPERTIES image_fmt;
532 XGL_IMAGE_TILING tiling;
533 XGL_RESULT err;
534 size_t size;
535
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800536 mipCount = 0;
537
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600538 uint32_t _w = w;
539 uint32_t _h = h;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800540 while( ( _w > 0 ) || ( _h > 0 ) )
541 {
542 _w >>= 1;
543 _h >>= 1;
544 mipCount++;
545 }
546
Tony Barbour579f7802015-04-03 15:11:43 -0600547 size = sizeof(image_fmt);
548 err = xglGetFormatInfo(m_device->obj(), fmt,
549 XGL_INFO_TYPE_FORMAT_PROPERTIES,
550 &size, &image_fmt);
551 ASSERT_XGL_SUCCESS(err);
552
553 if (requested_tiling == XGL_LINEAR_TILING) {
554 if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
555 tiling = XGL_LINEAR_TILING;
556 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
557 tiling = XGL_OPTIMAL_TILING;
558 } else {
559 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
560 }
561 } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) {
562 tiling = XGL_OPTIMAL_TILING;
563 } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) {
564 tiling = XGL_LINEAR_TILING;
565 } else {
566 ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration";
567 }
568
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800569 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
570 imageCreateInfo.imageType = XGL_IMAGE_2D;
571 imageCreateInfo.format = fmt;
572 imageCreateInfo.extent.width = w;
573 imageCreateInfo.extent.height = h;
574 imageCreateInfo.mipLevels = mipCount;
575 imageCreateInfo.tiling = tiling;
576
577 imageCreateInfo.usage = usage;
578
579 xgl_testing::Image::init(*m_device, imageCreateInfo);
580
Courtney Goeltzenleuchterbe4f0cd2015-04-07 08:57:30 -0600581 if (usage & XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) {
582 SetLayout(XGL_IMAGE_ASPECT_COLOR, XGL_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
583 } else {
584 SetLayout(XGL_IMAGE_ASPECT_COLOR, XGL_IMAGE_LAYOUT_GENERAL);
585 }
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800586}
587
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600588XGL_RESULT XglImage::MapMemory(void** ptr)
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800589{
590 *ptr = map();
591 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
592}
593
594XGL_RESULT XglImage::UnmapMemory()
595{
596 unmap();
597 return XGL_SUCCESS;
598}
599
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600600XGL_RESULT XglImage::CopyImage(XglImage &src_image)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600601{
602 XGL_RESULT err;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600603 XglCommandBufferObj cmd_buf(m_device);
604 XGL_IMAGE_LAYOUT src_image_layout, dest_image_layout;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600605
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600606 /* Build command buffer to copy staging texture to usable texture */
607 err = cmd_buf.BeginCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600608 assert(!err);
609
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600610 /* TODO: Can we determine image aspect from image object? */
611 src_image_layout = src_image.layout();
612 src_image.SetLayout(&cmd_buf, XGL_IMAGE_ASPECT_COLOR, XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600613
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600614 dest_image_layout = this->layout();
615 this->SetLayout(&cmd_buf, XGL_IMAGE_ASPECT_COLOR, XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600616
Tony Barbour5dc515d2015-04-01 17:47:06 -0600617 XGL_IMAGE_COPY copy_region = {};
618 copy_region.srcSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
619 copy_region.srcSubresource.arraySlice = 0;
620 copy_region.srcSubresource.mipLevel = 0;
621 copy_region.srcOffset.x = 0;
622 copy_region.srcOffset.y = 0;
623 copy_region.srcOffset.z = 0;
624 copy_region.destSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
625 copy_region.destSubresource.arraySlice = 0;
626 copy_region.destSubresource.mipLevel = 0;
627 copy_region.destOffset.x = 0;
628 copy_region.destOffset.y = 0;
629 copy_region.destOffset.z = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600630 copy_region.extent = src_image.extent();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600631
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600632 xglCmdCopyImage(cmd_buf.obj(),
633 src_image.obj(), src_image.layout(),
634 obj(), layout(),
Tony Barbour1c45ce02015-03-27 17:03:18 -0600635 1, &copy_region);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600636 cmd_buf.mem_ref_mgr.AddMemoryRefs(src_image);
637 cmd_buf.mem_ref_mgr.AddMemoryRefs(*this);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600638
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600639 src_image.SetLayout(&cmd_buf, XGL_IMAGE_ASPECT_COLOR, src_image_layout);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600640
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600641 this->SetLayout(&cmd_buf, XGL_IMAGE_ASPECT_COLOR, dest_image_layout);
Tony Barbour1c45ce02015-03-27 17:03:18 -0600642
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600643 err = cmd_buf.EndCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600644 assert(!err);
645
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600646 cmd_buf.QueueCommandBuffer();
Tony Barbour5dc515d2015-04-01 17:47:06 -0600647
Tony Barbour5dc515d2015-04-01 17:47:06 -0600648 return XGL_SUCCESS;
649}
650
Tony Barbourebc093f2015-04-01 16:38:10 -0600651XglTextureObj::XglTextureObj(XglDevice *device, uint32_t *colors)
Tony Barbour5dc515d2015-04-01 17:47:06 -0600652 :XglImage(device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700653{
654 m_device = device;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700655 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourebc093f2015-04-01 16:38:10 -0600656 uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barbour5dc515d2015-04-01 17:47:06 -0600657 void *data;
658 int32_t x, y;
659 XglImage stagingImage(device);
660
661 stagingImage.init(16, 16, tex_format, 0, XGL_LINEAR_TILING);
662 XGL_SUBRESOURCE_LAYOUT layout = stagingImage.subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
Tony Barbourebc093f2015-04-01 16:38:10 -0600663
664 if (colors == NULL)
665 colors = tex_colors;
Tony Barboure2c58df2014-11-25 13:18:32 -0700666
667 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
668
669 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
670
Tony Barbour5dc515d2015-04-01 17:47:06 -0600671 XGL_IMAGE_VIEW_CREATE_INFO view = {};
Tony Barbourbdf0a312015-04-01 17:10:07 -0600672 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
673 view.pNext = NULL;
674 view.image = XGL_NULL_HANDLE;
675 view.viewType = XGL_IMAGE_VIEW_2D;
Tony Barbour5dc515d2015-04-01 17:47:06 -0600676 view.format = tex_format;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600677 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
678 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
679 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
680 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
681 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
682 view.subresourceRange.baseMipLevel = 0;
683 view.subresourceRange.mipLevels = 1;
684 view.subresourceRange.baseArraySlice = 0;
685 view.subresourceRange.arraySize = 1;
686 view.minLod = 0.0f;
Tony Barboure2c58df2014-11-25 13:18:32 -0700687
Tony Barboure2c58df2014-11-25 13:18:32 -0700688 /* create image */
Tony Barbour5dc515d2015-04-01 17:47:06 -0600689 init(16, 16, tex_format, XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, XGL_OPTIMAL_TILING);
Tony Barboure2c58df2014-11-25 13:18:32 -0700690
691 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800692 view.image = obj();
693 m_textureView.init(*m_device, view);
Tony Barbour5dc515d2015-04-01 17:47:06 -0600694 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700695
Tony Barbour5dc515d2015-04-01 17:47:06 -0600696 data = stagingImage.map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700697
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800698 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700699 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800700 for (x = 0; x < extent().width; x++)
Tony Barbourebc093f2015-04-01 16:38:10 -0600701 row[x] = colors[(x & 1) ^ (y & 1)];
Tony Barboure2c58df2014-11-25 13:18:32 -0700702 }
Tony Barbour5dc515d2015-04-01 17:47:06 -0600703 stagingImage.unmap();
704 XglImage::CopyImage(stagingImage);
Tony Barboure2c58df2014-11-25 13:18:32 -0700705}
Tony Barbour82c39522014-12-04 14:33:33 -0700706
Tony Barboure2c58df2014-11-25 13:18:32 -0700707XglSamplerObj::XglSamplerObj(XglDevice *device)
708{
Tony Barboure2c58df2014-11-25 13:18:32 -0700709 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700710
Chia-I Wue9864b52014-12-28 16:32:24 +0800711 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
712 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
713 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
714 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
715 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
716 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
717 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
718 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
719 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
720 samplerCreateInfo.mipLodBias = 0.0;
721 samplerCreateInfo.maxAnisotropy = 0.0;
722 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
723 samplerCreateInfo.minLod = 0.0;
724 samplerCreateInfo.maxLod = 0.0;
725 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700726
Chia-I Wue9864b52014-12-28 16:32:24 +0800727 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700728}
Tony Barboure2c58df2014-11-25 13:18:32 -0700729
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700730/*
731 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
732 */
733XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
734{
735 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700736 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700737
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800738 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700739}
740
Tony Barbour044703b2015-03-26 12:37:52 -0600741XglConstantBufferObj::~XglConstantBufferObj()
742{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600743 // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here?
Tony Barbour044703b2015-03-26 12:37:52 -0600744 if (m_commandBuffer) {
745 delete m_commandBuffer;
746 }
747}
748
Tony Barboure2c58df2014-11-25 13:18:32 -0700749XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
750{
Tony Barboure2c58df2014-11-25 13:18:32 -0700751 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800752 m_commandBuffer = 0;
753
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800754 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700755 m_numVertices = constantCount;
756 m_stride = constantSize;
757
Chia-I Wua07fee62014-12-28 15:26:08 +0800758 const size_t allocationSize = constantCount * constantSize;
759 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700760
Chia-I Wua07fee62014-12-28 15:26:08 +0800761 void *pData = map();
762 memcpy(pData, data, allocationSize);
763 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700764
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800765 // set up the buffer view for the constant buffer
766 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
767 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
768 view_info.buffer = obj();
Chia-I Wubb0c8d22015-01-16 22:31:25 +0800769 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800770 view_info.offset = 0;
771 view_info.range = allocationSize;
772 m_bufferView.init(*m_device, view_info);
773
774 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
775 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700776}
Tony Barbour82c39522014-12-04 14:33:33 -0700777
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600778void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, uint32_t binding)
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700779{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800780 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700781}
782
783
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000784void XglConstantBufferObj::BufferMemoryBarrier(
785 XGL_FLAGS outputMask /*=
786 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
787 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
788 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
789 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
790 XGL_MEMORY_OUTPUT_COPY_BIT*/,
791 XGL_FLAGS inputMask /*=
792 XGL_MEMORY_INPUT_CPU_READ_BIT |
793 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
794 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
795 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
796 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
797 XGL_MEMORY_INPUT_SHADER_READ_BIT |
798 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
799 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
800 XGL_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700801{
Tony Barbour38422802014-12-10 14:36:31 -0700802 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700803
Tony Barbour38422802014-12-10 14:36:31 -0700804 if (!m_commandBuffer)
805 {
Mark Lobodzinski148e1582015-04-07 16:07:57 -0500806 m_fence.init(*m_device, xgl_testing::Fence::create_info());
Tony Barbour38422802014-12-10 14:36:31 -0700807
808 m_commandBuffer = new XglCommandBufferObj(m_device);
Tony Barbour38422802014-12-10 14:36:31 -0700809 }
810 else
811 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800812 m_device->wait(m_fence);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600813 m_commandBuffer->mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour38422802014-12-10 14:36:31 -0700814 }
815
Tony Barboure2c58df2014-11-25 13:18:32 -0700816 // open the command buffer
Tony Barbourbdf0a312015-04-01 17:10:07 -0600817 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {};
818 cmd_buf_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
819 cmd_buf_info.pNext = NULL;
820 cmd_buf_info.flags = 0;
821
Jon Ashburnc4164b12014-12-31 17:10:47 -0700822 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700823 ASSERT_XGL_SUCCESS(err);
824
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000825 XGL_BUFFER_MEMORY_BARRIER memory_barrier =
826 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Mark Lobodzinski837ef922015-01-29 14:24:14 -0600827 XGL_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Tony Barboure2c58df2014-11-25 13:18:32 -0700828
Courtney Goeltzenleuchter1e8f3be2015-03-24 18:02:34 -0600829 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000830 XGL_PIPELINE_BARRIER pipeline_barrier = {};
831 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
832 pipeline_barrier.eventCount = 1;
833 pipeline_barrier.pEvents = set_events;
834 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
835 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -0600836 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000837
838 // write barrier to the command buffer
839 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700840
841 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700842 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700843 ASSERT_XGL_SUCCESS(err);
844
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600845 /*
846 * Tell driver about memory references made in this command buffer
847 * Note: Since this command buffer only has a PipelineBarrier
848 * command there really aren't any memory refs to worry about.
849 */
850 m_commandBuffer->mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
Tony Barboure2c58df2014-11-25 13:18:32 -0700851
852 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700853 XGL_CMD_BUFFER bufferArray[1];
854 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600855 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700856 ASSERT_XGL_SUCCESS(err);
857}
858
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700859XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
860 : XglConstantBufferObj(device)
861{
862
863}
864
865void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
866{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700867 XGL_FORMAT viewFormat;
868
869 m_numVertices = numIndexes;
870 m_indexType = indexType;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700871 switch (indexType) {
872 case XGL_INDEX_8:
873 m_stride = 1;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700874 viewFormat = XGL_FMT_R8_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700875 break;
876 case XGL_INDEX_16:
877 m_stride = 2;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700878 viewFormat = XGL_FMT_R16_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700879 break;
880 case XGL_INDEX_32:
881 m_stride = 4;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700882 viewFormat = XGL_FMT_R32_UINT;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700883 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800884 default:
885 assert(!"unknown index type");
886 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700887 }
888
Chia-I Wua07fee62014-12-28 15:26:08 +0800889 const size_t allocationSize = numIndexes * m_stride;
890 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700891
Chia-I Wua07fee62014-12-28 15:26:08 +0800892 void *pData = map();
893 memcpy(pData, data, allocationSize);
894 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700895
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800896 // set up the buffer view for the constant buffer
897 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
898 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
899 view_info.buffer = obj();
900 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700901 view_info.format = viewFormat;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800902 view_info.offset = 0;
903 view_info.range = allocationSize;
904 m_bufferView.init(*m_device, view_info);
905
906 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
907 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700908}
909
910void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
911{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800912 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700913}
Tony Barboure2c58df2014-11-25 13:18:32 -0700914
Tony Barbouraf1f9192014-12-17 10:57:58 -0700915XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
916{
917 return m_indexType;
918}
919
Chia-I Wu11078b02015-01-04 16:27:24 +0800920XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo()
Tony Barboure2c58df2014-11-25 13:18:32 -0700921{
Tony Barboure2c58df2014-11-25 13:18:32 -0700922 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
923 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
924 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800925 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700926 stageInfo->shader.linkConstBufferCount = 0;
927 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700928
Tony Barboure2c58df2014-11-25 13:18:32 -0700929 return stageInfo;
930}
931
Tony Barboure2c58df2014-11-25 13:18:32 -0700932XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
933{
934 XGL_RESULT err = XGL_SUCCESS;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600935 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700936 XGL_SHADER_CREATE_INFO createInfo;
937 size_t shader_len;
938
939 m_stage = stage;
940 m_device = device;
941
942 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
943 createInfo.pNext = NULL;
944
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600945 if (!framework->m_use_spv) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700946
947 shader_len = strlen(shader_code);
948 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
949 createInfo.pCode = malloc(createInfo.codeSize);
950 createInfo.flags = 0;
951
952 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600953 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Tony Barboure2c58df2014-11-25 13:18:32 -0700954 ((uint32_t *) createInfo.pCode)[1] = 0;
955 ((uint32_t *) createInfo.pCode)[2] = stage;
956 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
957
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800958 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700959 }
960
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600961 if (framework->m_use_spv || err) {
962 std::vector<unsigned int> spv;
Tony Barboure2c58df2014-11-25 13:18:32 -0700963 err = XGL_SUCCESS;
964
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600965 // Use Reference GLSL to SPV compiler
966 framework->GLSLtoSPV(stage, shader_code, spv);
967 createInfo.pCode = spv.data();
968 createInfo.codeSize = spv.size() * sizeof(unsigned int);
Tony Barboure2c58df2014-11-25 13:18:32 -0700969 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700970
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800971 init(*m_device, createInfo);
972 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700973}
Tony Barbour82c39522014-12-04 14:33:33 -0700974
Tony Barboure2c58df2014-11-25 13:18:32 -0700975XglPipelineObj::XglPipelineObj(XglDevice *device)
976{
Tony Barboure2c58df2014-11-25 13:18:32 -0700977 m_device = device;
978 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
979 m_vertexBufferCount = 0;
980
981 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
982 m_ia_state.pNext = XGL_NULL_HANDLE;
983 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
984 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700985 m_ia_state.primitiveRestartEnable = XGL_FALSE;
986 m_ia_state.primitiveRestartIndex = 0;
987
988 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
989 m_rs_state.pNext = &m_ia_state;
990 m_rs_state.depthClipEnable = XGL_FALSE;
991 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Chia-I Wuc8d1ec52015-03-24 11:01:50 +0800992 m_rs_state.programPointSize = XGL_FALSE;
993 m_rs_state.pointOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourf52346d2015-01-16 14:27:35 -0700994 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
995 m_rs_state.fillMode = XGL_FILL_SOLID;
996 m_rs_state.cullMode = XGL_CULL_NONE;
997 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700998
Tony Barboure2c58df2014-11-25 13:18:32 -0700999 memset(&m_cb_state,0,sizeof(m_cb_state));
1000 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
1001 m_cb_state.pNext = &m_rs_state;
1002 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -07001003 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
1004
Tony Barbourf52346d2015-01-16 14:27:35 -07001005 m_ms_state.pNext = &m_cb_state;
1006 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1007 m_ms_state.multisampleEnable = XGL_FALSE;
1008 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
1009 m_ms_state.samples = 1;
1010 m_ms_state.minSampleShading = 0;
1011 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -07001012
Tony Barbourf52346d2015-01-16 14:27:35 -07001013 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1014 m_ds_state.pNext = &m_ms_state,
Jeremy Hayesa058eee2015-01-23 08:51:43 -07001015 m_ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourf52346d2015-01-16 14:27:35 -07001016 m_ds_state.depthTestEnable = XGL_FALSE;
1017 m_ds_state.depthWriteEnable = XGL_FALSE;
1018 m_ds_state.depthBoundsEnable = XGL_FALSE;
1019 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
1020 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
1021 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1022 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1023 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
1024 m_ds_state.stencilTestEnable = XGL_FALSE;
1025 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -07001026
Tony Barbourf52346d2015-01-16 14:27:35 -07001027 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
1028 att.blendEnable = XGL_FALSE;
Tony Barboura53a6942015-02-25 11:25:11 -07001029 att.format = XGL_FMT_B8G8R8A8_UNORM;
Tony Barbourf52346d2015-01-16 14:27:35 -07001030 att.channelWriteMask = 0xf;
1031 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -07001032
1033};
1034
1035void XglPipelineObj::AddShader(XglShaderObj* shader)
1036{
1037 m_shaderObjs.push_back(shader);
1038}
1039
1040void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
1041{
1042 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
1043 m_vi_state.attributeCount = count;
1044}
1045
1046void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
1047{
1048 m_vi_state.pVertexBindingDescriptions = vi_binding;
1049 m_vi_state.bindingCount = count;
1050}
1051
1052void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
1053{
1054 m_vertexBufferObjs.push_back(vertexDataBuffer);
1055 m_vertexBufferBindings.push_back(binding);
1056 m_vertexBufferCount++;
1057}
1058
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001059void XglPipelineObj::AddColorAttachment(uint32_t binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +08001060{
Tony Barbourf52346d2015-01-16 14:27:35 -07001061 if (binding+1 > m_colorAttachments.size())
1062 {
1063 m_colorAttachments.resize(binding+1);
1064 }
1065 m_colorAttachments[binding] = *att;
1066}
1067
1068void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
1069{
1070 m_ds_state.format = ds_state->format;
1071 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
1072 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
1073 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
1074 m_ds_state.depthFunc = ds_state->depthFunc;
1075 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
1076 m_ds_state.back = ds_state->back;
1077 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +08001078}
1079
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001080void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj &descriptorSet)
Tony Barbour976e1cf2014-12-17 11:57:31 -07001081{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001082 void* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001083 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1084
1085 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
1086
1087 for (int i=0; i<m_shaderObjs.size(); i++)
1088 {
Chia-I Wu11078b02015-01-04 16:27:24 +08001089 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001090 shaderCreateInfo->pNext = head_ptr;
1091 head_ptr = shaderCreateInfo;
1092 }
1093
1094 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1095 {
1096 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1097 m_vi_state.pNext = head_ptr;
1098 head_ptr = &m_vi_state;
1099 }
1100
1101 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1102 info.pNext = head_ptr;
1103 info.flags = 0;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001104 info.pSetLayoutChain = descriptorSet.GetLayoutChain();
Tony Barbour976e1cf2014-12-17 11:57:31 -07001105
Tony Barbourf52346d2015-01-16 14:27:35 -07001106 m_cb_state.attachmentCount = m_colorAttachments.size();
1107 m_cb_state.pAttachments = &m_colorAttachments[0];
1108
Chia-I Wu2648d092014-12-29 14:24:14 +08001109 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -07001110}
Chia-I Wu2648d092014-12-29 14:24:14 +08001111
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001112vector<XGL_GPU_MEMORY> XglMemoryRefManager::mem_refs() const
Tony Barbour976e1cf2014-12-17 11:57:31 -07001113{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001114 std::vector<XGL_GPU_MEMORY> mems;
1115 if (this->mem_refs_.size()) {
1116 mems.reserve(this->mem_refs_.size());
1117 for (uint32_t i = 0; i < this->mem_refs_.size(); i++)
1118 mems.push_back(this->mem_refs_[i]);
1119 }
1120
1121 return mems;
Tony Barbour976e1cf2014-12-17 11:57:31 -07001122}
1123
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001124void XglMemoryRefManager::AddMemoryRefs(xgl_testing::Object &xglObject)
Tony Barboure2c58df2014-11-25 13:18:32 -07001125{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001126 const std::vector<XGL_GPU_MEMORY> mems = xglObject.memories();
1127 AddMemoryRefs(mems);
1128}
Tony Barboure2c58df2014-11-25 13:18:32 -07001129
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001130void XglMemoryRefManager::AddMemoryRefs(vector<XGL_GPU_MEMORY> mem)
1131{
1132 for (size_t i = 0; i < mem.size(); i++) {
1133 if (mem[i] != NULL) {
1134 this->mem_refs_.push_back(mem[i]);
1135 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001136 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001137}
Tony Barbour82c39522014-12-04 14:33:33 -07001138
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001139void XglMemoryRefManager::EmitAddMemoryRefs(XGL_QUEUE queue)
1140{
1141 for (uint32_t i = 0; i < mem_refs_.size(); i++) {
1142 xglQueueAddMemReference(queue, mem_refs_[i]);
Tony Barbour1c45ce02015-03-27 17:03:18 -06001143 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001144}
Tony Barbour82c39522014-12-04 14:33:33 -07001145
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001146void XglMemoryRefManager::EmitRemoveMemoryRefs(XGL_QUEUE queue)
1147{
1148 for (uint32_t i = 0; i < mem_refs_.size(); i++) {
1149 xglQueueRemoveMemReference(queue, mem_refs_[i]);
Mark Lobodzinskic52b7752015-02-18 16:38:17 -06001150 }
1151}
1152
Tony Barbour6d047bf2014-12-10 14:34:45 -07001153XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -07001154 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(device->graphics_queue_node_index_))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001155{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001156 m_device = device;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001157}
Tony Barbour471338d2014-12-10 17:28:39 -07001158
Tony Barbour6d047bf2014-12-10 14:34:45 -07001159XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1160{
Chia-I Wud28343c2014-12-28 15:12:48 +08001161 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001162}
Tony Barbour471338d2014-12-10 17:28:39 -07001163
Jon Ashburnc4164b12014-12-31 17:10:47 -07001164XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001165{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001166 begin(pInfo);
1167 return XGL_SUCCESS;
1168}
1169
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001170XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj, XGL_FRAMEBUFFER framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001171{
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001172 begin(renderpass_obj, framebuffer_obj);
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001173 return XGL_SUCCESS;
1174}
1175
1176XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
1177{
1178 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +08001179 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001180}
1181
1182XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1183{
Chia-I Wud28343c2014-12-28 15:12:48 +08001184 end();
1185 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001186}
1187
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001188void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001189{
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001190 xglCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001191}
1192
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001193void XglCommandBufferObj::ClearAllBuffers(XGL_CLEAR_COLOR clear_color, float depth_clear_color, uint32_t stencil_clear_color,
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001194 XglDepthStencilObj *depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001195{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001196 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001197 const XGL_FLAGS output_mask =
1198 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1199 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1200 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1201 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1202 XGL_MEMORY_OUTPUT_COPY_BIT;
1203 const XGL_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001204
1205 // whatever we want to do, we do it to the whole buffer
1206 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1207 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1208 srRange.baseMipLevel = 0;
1209 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1210 srRange.baseArraySlice = 0;
1211 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1212
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001213 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1214 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1215 memory_barrier.outputMask = output_mask;
1216 memory_barrier.inputMask = input_mask;
1217 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1218 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001219 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001220
Courtney Goeltzenleuchter1e8f3be2015-03-24 18:02:34 -06001221 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001222 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1223 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1224 pipeline_barrier.eventCount = 1;
1225 pipeline_barrier.pEvents = set_events;
1226 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1227 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001228 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001229
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001230 for (i = 0; i < m_renderTargets.size(); i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001231 memory_barrier.image = m_renderTargets[i]->image();
1232 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1233 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1234 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001235
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -06001236 xglCmdClearColorImage(obj(),
1237 m_renderTargets[i]->image(), XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
1238 clear_color, 1, &srRange );
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001239
1240 mem_ref_mgr.AddMemoryRefs(*m_renderTargets[i]);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001241 }
1242
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001243 if (depthStencilObj)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001244 {
1245 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1246 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1247 dsRange.baseMipLevel = 0;
1248 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1249 dsRange.baseArraySlice = 0;
1250 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1251
1252 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001253
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001254 memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001255 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001256 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001257 memory_barrier.subresourceRange = dsRange;
1258
1259 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001260
Courtney Goeltzenleuchterb3efe9b2015-03-25 11:25:10 -06001261 xglCmdClearDepthStencil(obj(),
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001262 depthStencilObj->obj(), XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter679bbfa2015-03-05 17:26:38 -07001263 depth_clear_color, stencil_clear_color,
1264 1, &dsRange);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001265 mem_ref_mgr.AddMemoryRefs(*depthStencilObj);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001266
1267 // prepare depth buffer for rendering
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001268 memory_barrier.image = depthStencilObj->obj();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001269 memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001270 memory_barrier.newLayout = depthStencilObj->BindInfo()->layout;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001271 memory_barrier.subresourceRange = dsRange;
1272 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001273 }
1274}
1275
Jon Ashburncdc40be2015-01-02 18:27:14 -07001276void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001277{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001278 uint32_t i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001279 const XGL_FLAGS output_mask =
1280 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1281 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1282 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1283 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1284 XGL_MEMORY_OUTPUT_COPY_BIT;
1285 const XGL_FLAGS input_mask =
1286 XGL_MEMORY_INPUT_CPU_READ_BIT |
1287 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1288 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
1289 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1290 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
1291 XGL_MEMORY_INPUT_SHADER_READ_BIT |
1292 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1293 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1294 XGL_MEMORY_INPUT_COPY_BIT;
1295
Tony Barbour30cc9e82014-12-17 11:53:55 -07001296 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1297 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1298 srRange.baseMipLevel = 0;
1299 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1300 srRange.baseArraySlice = 0;
1301 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1302
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001303 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1304 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1305 memory_barrier.outputMask = output_mask;
1306 memory_barrier.inputMask = input_mask;
1307 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1308 memory_barrier.subresourceRange = srRange;
Mark Lobodzinski837ef922015-01-29 14:24:14 -06001309 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001310
Courtney Goeltzenleuchter1e8f3be2015-03-24 18:02:34 -06001311 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001312 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1313 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1314 pipeline_barrier.eventCount = 1;
1315 pipeline_barrier.pEvents = set_events;
1316 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1317 pipeline_barrier.memBarrierCount = 1;
Mark Lobodzinskid5d83ed2015-02-02 11:55:52 -06001318 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001319
Courtney Goeltzenleuchterb4337c12015-03-05 16:47:18 -07001320 for(i=0; i<m_renderTargets.size(); i++)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001321 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001322 memory_barrier.image = m_renderTargets[i]->image();
1323 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1324 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1325 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001326 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001327}
1328
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -06001329void XglCommandBufferObj::BeginRenderPass(XGL_RENDER_PASS renderpass, XGL_FRAMEBUFFER framebuffer)
1330{
1331 XGL_RENDER_PASS_BEGIN rp_begin = {
1332 renderpass,
1333 framebuffer,
1334 };
1335
1336 xglCmdBeginRenderPass( obj(), &rp_begin);
1337}
1338
1339void XglCommandBufferObj::EndRenderPass(XGL_RENDER_PASS renderpass)
1340{
1341 xglCmdEndRenderPass( obj(), renderpass);
1342}
1343
Tony Barbourf52346d2015-01-16 14:27:35 -07001344void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001345{
Tony Barbourf52346d2015-01-16 14:27:35 -07001346 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001347}
1348
1349void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1350{
1351 m_renderTargets.push_back(renderTarget);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001352}
1353
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001354void XglCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001355{
Chia-I Wud28343c2014-12-28 15:12:48 +08001356 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001357}
1358
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001359void XglCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001360{
Chia-I Wud28343c2014-12-28 15:12:48 +08001361 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001362}
1363
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001364void XglCommandBufferObj::QueueCommandBuffer()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001365{
Tony Barbour09b7ac32015-04-08 10:11:29 -06001366 QueueCommandBuffer(NULL);
1367}
1368
1369void XglCommandBufferObj::QueueCommandBuffer(XGL_FENCE fence)
1370{
Tony Barbour30cc9e82014-12-17 11:53:55 -07001371 XGL_RESULT err = XGL_SUCCESS;
1372
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001373 mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue);
1374
Tony Barbour30cc9e82014-12-17 11:53:55 -07001375 // submit the command buffer to the universal queue
Tony Barbour09b7ac32015-04-08 10:11:29 -06001376 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), fence );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001377 ASSERT_XGL_SUCCESS( err );
1378
1379 err = xglQueueWaitIdle( m_device->m_queue );
1380 ASSERT_XGL_SUCCESS( err );
1381
1382 // Wait for work to finish before cleaning up.
1383 xglDeviceWaitIdle(m_device->device());
1384
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001385 /*
1386 * Now that processing on this command buffer is complete
1387 * we can remove the memory references.
1388 */
1389 mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001390}
1391
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001392void XglCommandBufferObj::BindPipeline(XglPipelineObj &pipeline)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001393{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001394 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() );
1395 mem_ref_mgr.AddMemoryRefs(pipeline);
1396}
1397
1398void XglCommandBufferObj::BindDescriptorSet(XglDescriptorSetObj &descriptorSet)
1399{
1400 XGL_DESCRIPTOR_SET set_obj = descriptorSet.GetDescriptorSetHandle();
Chia-I Wu53f07d72015-03-28 15:23:55 +08001401
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001402 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wu53f07d72015-03-28 15:23:55 +08001403 xglCmdBindDescriptorSets(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001404 descriptorSet.GetLayoutChain(), 0, 1, &set_obj, NULL );
1405
1406 // Add descriptor set mem refs to command buffer's list
1407 mem_ref_mgr.AddMemoryRefs(descriptorSet.memories());
1408 mem_ref_mgr.AddMemoryRefs(descriptorSet.mem_ref_mgr.mem_refs());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001409}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001410
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001411void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001412{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001413 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001414 mem_ref_mgr.AddMemoryRefs(*indexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001415}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001416
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001417void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001418{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001419 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001420 mem_ref_mgr.AddMemoryRefs(*vertexBuffer);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001421}
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -06001422
Tony Barbour1c45ce02015-03-27 17:03:18 -06001423XglDepthStencilObj::XglDepthStencilObj()
1424{
1425 m_initialized = false;
1426}
1427bool XglDepthStencilObj::Initialized()
1428{
1429 return m_initialized;
1430}
1431
1432XGL_DEPTH_STENCIL_BIND_INFO* XglDepthStencilObj::BindInfo()
1433{
1434 return &m_depthStencilBindInfo;
1435}
1436
1437void XglDepthStencilObj::Init(XglDevice *device, int32_t width, int32_t height)
1438{
1439 XGL_IMAGE_CREATE_INFO image_info;
1440 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view_info;
1441
1442 m_device = device;
1443 m_initialized = true;
1444 m_depth_stencil_fmt = XGL_FMT_D16_UNORM;
1445
1446 image_info.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1447 image_info.pNext = NULL;
1448 image_info.imageType = XGL_IMAGE_2D;
1449 image_info.format = m_depth_stencil_fmt;
1450 image_info.extent.width = width;
1451 image_info.extent.height = height;
1452 image_info.extent.depth = 1;
1453 image_info.mipLevels = 1;
1454 image_info.arraySize = 1;
1455 image_info.samples = 1;
1456 image_info.tiling = XGL_OPTIMAL_TILING;
1457 image_info.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
1458 image_info.flags = 0;
1459 init(*m_device, image_info);
1460
1461 view_info.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
1462 view_info.pNext = NULL;
1463 view_info.image = XGL_NULL_HANDLE;
1464 view_info.mipLevel = 0;
1465 view_info.baseArraySlice = 0;
1466 view_info.arraySize = 1;
1467 view_info.flags = 0;
1468 view_info.image = obj();
1469 m_depthStencilView.init(*m_device, view_info);
1470
1471 m_depthStencilBindInfo.view = m_depthStencilView.obj();
1472 m_depthStencilBindInfo.layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
1473}