blob: 2e7ff8f37e28f145058a1b787bb6161e194e6bb6 [file] [log] [blame]
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -06001/*
2 * XGL Tests
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
28#include "xglrenderframework.h"
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060029
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060030XglRenderFramework::XglRenderFramework() :
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060031 m_cmdBuffer( XGL_NULL_HANDLE ),
Chia-I Wu837f9952014-12-15 23:29:34 +080032 m_stateRaster( XGL_NULL_HANDLE ),
33 m_colorBlend( XGL_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060034 m_stateViewport( XGL_NULL_HANDLE ),
Chia-I Wu837f9952014-12-15 23:29:34 +080035 m_stateDepthStencil( XGL_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060036 m_width( 256.0 ), // default window width
37 m_height( 256.0 ) // default window height
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060038{
Chia-I Wuecebf752014-12-05 10:45:15 +080039 m_renderTargetCount = 1;
40
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060041 m_render_target_fmt.channelFormat = XGL_CH_FMT_R8G8B8A8;
42 m_render_target_fmt.numericFormat = XGL_NUM_FMT_UNORM;
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060043
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060044 m_depthStencilBinding.view = XGL_NULL_HANDLE;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060045}
46
47XglRenderFramework::~XglRenderFramework()
48{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060049
50}
51
52void XglRenderFramework::InitFramework()
53{
54 XGL_RESULT err;
55
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060056 err = xglInitAndEnumerateGpus(&app_info, NULL,
Chia-I Wuaf11d922014-12-28 14:37:25 +080057 XGL_MAX_PHYSICAL_GPUS, &this->gpu_count, objs);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060058 ASSERT_XGL_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070059 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060060
61 m_device = new XglDevice(0, objs[0]);
62 m_device->get_device_queue();
63}
64
65void XglRenderFramework::ShutdownFramework()
66{
67 if (m_colorBlend) xglDestroyObject(m_colorBlend);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060068 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
69 if (m_stateRaster) xglDestroyObject(m_stateRaster);
70 if (m_cmdBuffer) xglDestroyObject(m_cmdBuffer);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060071
72 if (m_stateViewport) {
73 xglDestroyObject(m_stateViewport);
74 }
75
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060076 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +080077 delete m_device;
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060078 xglInitAndEnumerateGpus(&this->app_info, XGL_NULL_HANDLE, 0, &gpu_count, XGL_NULL_HANDLE);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060079}
80
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060081void XglRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060082{
83 XGL_RESULT err;
84
85 m_render_target_fmt.channelFormat = XGL_CH_FMT_R8G8B8A8;
86 m_render_target_fmt.numericFormat = XGL_NUM_FMT_UNORM;
87
88 // create a raster state (solid, back-face culling)
Tony Barbourf52346d2015-01-16 14:27:35 -070089 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster = {};
90 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
91 raster.pointSize = 1.0;
92
93 err = xglCreateDynamicRasterState( device(), &raster, &m_stateRaster );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060094 ASSERT_XGL_SUCCESS(err);
95
Tony Barbourf52346d2015-01-16 14:27:35 -070096 XGL_DYNAMIC_CB_STATE_CREATE_INFO blend = {};
97 blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
98 err = xglCreateDynamicColorBlendState(device(), &blend, &m_colorBlend);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060099 ASSERT_XGL_SUCCESS( err );
100
Tony Barbourf52346d2015-01-16 14:27:35 -0700101 XGL_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {};
102 depthStencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600103 depthStencil.minDepth = 0.f;
104 depthStencil.maxDepth = 1.f;
Tony Barbourf52346d2015-01-16 14:27:35 -0700105 depthStencil.stencilFrontRef = 0;
106 depthStencil.stencilBackRef = 0;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600107
Tony Barbourf52346d2015-01-16 14:27:35 -0700108 err = xglCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600109 ASSERT_XGL_SUCCESS( err );
110
111 XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {};
112
113 cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
114 cmdInfo.queueType = XGL_QUEUE_TYPE_GRAPHICS;
115 err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
116 ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed";
117}
118
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600119void XglRenderFramework::InitViewport(float width, float height)
120{
121 XGL_RESULT err;
122
Tony Barbourf52346d2015-01-16 14:27:35 -0700123 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600124
Tony Barbourf52346d2015-01-16 14:27:35 -0700125 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewportCreate = {};
126 viewportCreate.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
127 viewportCreate.viewportCount = 1;
128 viewportCreate.scissorCount = 0;
129 viewport.originX = 0;
130 viewport.originY = 0;
131 viewport.width = 1.f * width;
132 viewport.height = 1.f * height;
133 viewport.minDepth = 0.f;
134 viewport.maxDepth = 1.f;
135 viewportCreate.pViewports = &viewport;
136
137 err = xglCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport );
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600138 ASSERT_XGL_SUCCESS( err );
139 m_width = width;
140 m_height = height;
141}
142
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600143void XglRenderFramework::InitViewport()
144{
145 InitViewport(m_width, m_height);
146}
147
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600148void XglRenderFramework::InitRenderTarget()
149{
Chia-I Wuecebf752014-12-05 10:45:15 +0800150 XGL_UINT i;
151
152 for (i = 0; i < m_renderTargetCount; i++) {
Chia-I Wuf50ee212014-12-29 14:31:52 +0800153 XglImage *img = new XglImage(m_device);
154 img->init(m_width, m_height, m_render_target_fmt,
155 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
156 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
Tony Barbourf52346d2015-01-16 14:27:35 -0700157 m_colorBindings[i].view = img->targetView();
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000158 m_colorBindings[i].layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
Tony Barbourf52346d2015-01-16 14:27:35 -0700159 m_renderTargets.push_back(img);
Chia-I Wuecebf752014-12-05 10:45:15 +0800160 }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700161 // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment
162 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_LOAD;
163 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_STORE;
164 XGL_DEPTH_STENCIL_BIND_INFO *dsBinding;
165 if (m_depthStencilBinding.view)
166 dsBinding = &m_depthStencilBinding;
167 else
168 dsBinding = NULL;
169 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
170 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
171 .pNext = NULL,
172 .colorAttachmentCount = m_renderTargetCount,
173 .pColorAttachments = m_colorBindings,
174 .pDepthStencilAttachment = dsBinding,
175 .sampleCount = 1,
176 };
177 XGL_RENDER_PASS_CREATE_INFO rp_info;
178 memset(&rp_info, 0 , sizeof(rp_info));
179 xglCreateFramebuffer(device(), &fb_info, &(rp_info.framebuffer));
180 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
181 rp_info.renderArea.extent.width = m_width;
182 rp_info.renderArea.extent.height = m_height;
183 rp_info.pColorLoadOps = &load_op;
184 rp_info.pColorStoreOps = &store_op;
185 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
186 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
187 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD;
188 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_STORE;
189 xglCreateRenderPass(device(), &rp_info, &m_renderPass);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600190}
191
Chia-I Wufb1459b2014-12-29 15:23:20 +0800192XglDevice::XglDevice(XGL_UINT id, XGL_PHYSICAL_GPU obj) :
193 xgl_testing::Device(obj), id(id)
194{
195 init();
196
197 props = gpu().properties();
198 queue_props = &gpu().queue_properties()[0];
199}
200
201void XglDevice::get_device_queue()
202{
203 ASSERT_NE(true, graphics_queues().empty());
204 m_queue = graphics_queues()[0]->obj();
205}
206
Tony Barboure2c58df2014-11-25 13:18:32 -0700207XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
208{
209 m_device = device;
210 m_nextSlot = 0;
211
212}
213
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800214void XglDescriptorSetObj::AttachBufferView(XglConstantBufferObj *constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700215{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800216 m_bufferViews.push_back(&constantBuffer->m_bufferViewInfo);
217 m_bufferSlots.push_back(m_nextSlot);
Tony Barboure2c58df2014-11-25 13:18:32 -0700218 m_nextSlot++;
219
220}
Tony Barbour82c39522014-12-04 14:33:33 -0700221
Tony Barboure2c58df2014-11-25 13:18:32 -0700222void XglDescriptorSetObj::AttachSampler(XglSamplerObj *sampler)
223{
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800224 m_samplers.push_back(sampler);
Tony Barboure2c58df2014-11-25 13:18:32 -0700225 m_samplerSlots.push_back(m_nextSlot);
226 m_nextSlot++;
227
228}
Tony Barbour82c39522014-12-04 14:33:33 -0700229
Tony Barboure2c58df2014-11-25 13:18:32 -0700230void XglDescriptorSetObj::AttachImageView(XglTextureObj *texture)
231{
232 m_imageViews.push_back(&texture->m_textureViewInfo);
233 m_imageSlots.push_back(m_nextSlot);
234 m_nextSlot++;
235
236}
Tony Barbour82c39522014-12-04 14:33:33 -0700237
Tony Barboure2c58df2014-11-25 13:18:32 -0700238XGL_DESCRIPTOR_SLOT_INFO* XglDescriptorSetObj::GetSlotInfo(vector<int>slots,
239 vector<XGL_DESCRIPTOR_SET_SLOT_TYPE>types,
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800240 vector<void *>objs )
Tony Barboure2c58df2014-11-25 13:18:32 -0700241{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800242 int nSlots = m_bufferSlots.size() + m_imageSlots.size() + m_samplerSlots.size();
Tony Barboure2c58df2014-11-25 13:18:32 -0700243 m_slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( nSlots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
244 memset(m_slotInfo,0,nSlots*sizeof(XGL_DESCRIPTOR_SLOT_INFO));
245
246 for (int i=0; i<nSlots; i++)
247 {
248 m_slotInfo[i].slotObjectType = XGL_SLOT_UNUSED;
249 }
250
251 for (int i=0; i<slots.size(); i++)
252 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800253 for (int j=0; j<m_bufferSlots.size(); j++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700254 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800255 if ( m_bufferViews[j] == objs[i])
Tony Barboure2c58df2014-11-25 13:18:32 -0700256 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800257 m_slotInfo[m_bufferSlots[j]].shaderEntityIndex = slots[i];
258 m_slotInfo[m_bufferSlots[j]].slotObjectType = types[i];
Tony Barboure2c58df2014-11-25 13:18:32 -0700259 }
260 }
261 for (int j=0; j<m_imageSlots.size(); j++)
262 {
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800263 if ( m_imageViews[j] == objs[i])
Tony Barboure2c58df2014-11-25 13:18:32 -0700264 {
265 m_slotInfo[m_imageSlots[j]].shaderEntityIndex = slots[i];
266 m_slotInfo[m_imageSlots[j]].slotObjectType = types[i];
267 }
268 }
269 for (int j=0; j<m_samplerSlots.size(); j++)
270 {
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800271 if ( m_samplers[j] == objs[i])
Tony Barboure2c58df2014-11-25 13:18:32 -0700272 {
273 m_slotInfo[m_samplerSlots[j]].shaderEntityIndex = slots[i];
274 m_slotInfo[m_samplerSlots[j]].slotObjectType = types[i];
275 }
276 }
277 }
278
279 // for (int i=0;i<nSlots;i++)
280 // {
281 // printf("SlotInfo[%d]: Index = %d, Type = %d\n",i,m_slotInfo[i].shaderEntityIndex, m_slotInfo[i].slotObjectType);
282 // fflush(stdout);
283 // }
284
285 return(m_slotInfo);
286
287}
Tony Barbourb5f4d082014-12-17 10:54:03 -0700288void XglDescriptorSetObj::CreateXGLDescriptorSet()
289{
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800290 init(*m_device, xgl_testing::DescriptorSet::create_info(m_nextSlot));
Tony Barbourb5f4d082014-12-17 10:54:03 -0700291
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800292 begin();
293 clear();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700294
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800295 for (int i=0; i<m_bufferViews.size();i++)
Tony Barbourb5f4d082014-12-17 10:54:03 -0700296 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800297 attach(m_bufferSlots[i], *m_bufferViews[i]);
Tony Barbourb5f4d082014-12-17 10:54:03 -0700298 }
299 for (int i=0; i<m_samplers.size();i++)
300 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800301 attach(m_samplerSlots[i], *m_samplers[i]);
Tony Barbourb5f4d082014-12-17 10:54:03 -0700302 }
303 for (int i=0; i<m_imageViews.size();i++)
304 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800305 attach(m_imageSlots[i], *m_imageViews[i]);
Tony Barbourb5f4d082014-12-17 10:54:03 -0700306 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800307
308 end();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700309}
310
311XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle()
312{
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800313 return obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700314}
Tony Barboure2c58df2014-11-25 13:18:32 -0700315
Tony Barbour824b7712014-12-18 17:06:21 -0700316int XglDescriptorSetObj::GetTotalSlots()
317{
318 return m_nextSlot;
319}
320
Tony Barboure2c58df2014-11-25 13:18:32 -0700321void XglDescriptorSetObj::BindCommandBuffer(XGL_CMD_BUFFER commandBuffer)
322{
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800323 init(*m_device, xgl_testing::DescriptorSet::create_info(m_nextSlot));
Tony Barboure2c58df2014-11-25 13:18:32 -0700324
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800325 begin();
326 clear();
Tony Barboure2c58df2014-11-25 13:18:32 -0700327
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800328 for (int i=0; i<m_bufferViews.size();i++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700329 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800330 attach(m_bufferSlots[i], *m_bufferViews[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700331 }
332 for (int i=0; i<m_samplers.size();i++)
333 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800334 attach(m_samplerSlots[i], *m_samplers[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700335 }
336 for (int i=0; i<m_imageViews.size();i++)
337 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800338 attach(m_imageSlots[i], *m_imageViews[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700339 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800340
341 end();
Tony Barboure2c58df2014-11-25 13:18:32 -0700342
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800343 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800344 xglCmdBindDescriptorSet(commandBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, obj(), 0 );
Tony Barbour25ef8a62014-12-03 13:59:18 -0700345}
Tony Barboure2c58df2014-11-25 13:18:32 -0700346
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800347XglImage::XglImage(XglDevice *dev)
348{
349 m_device = dev;
350 m_imageInfo.view = XGL_NULL_HANDLE;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000351 m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL;
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800352}
353
354void XglImage::init(XGL_UINT32 w, XGL_UINT32 h,
355 XGL_FORMAT fmt, XGL_FLAGS usage,
356 XGL_IMAGE_TILING tiling)
357{
358 XGL_UINT mipCount;
359
360 mipCount = 0;
361
362 XGL_UINT _w = w;
363 XGL_UINT _h = h;
364 while( ( _w > 0 ) || ( _h > 0 ) )
365 {
366 _w >>= 1;
367 _h >>= 1;
368 mipCount++;
369 }
370
371 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
372 imageCreateInfo.imageType = XGL_IMAGE_2D;
373 imageCreateInfo.format = fmt;
374 imageCreateInfo.extent.width = w;
375 imageCreateInfo.extent.height = h;
376 imageCreateInfo.mipLevels = mipCount;
377 imageCreateInfo.tiling = tiling;
378
379 imageCreateInfo.usage = usage;
380
381 xgl_testing::Image::init(*m_device, imageCreateInfo);
382
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 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO createView = {
386 XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
387 XGL_NULL_HANDLE,
388 obj(),
389 {XGL_CH_FMT_R8G8B8A8, XGL_NUM_FMT_UNORM},
390 0,
391 0,
392 1
393 };
394
395 m_targetView.init(*m_device, createView);
396}
397
398XGL_RESULT XglImage::MapMemory(XGL_VOID** ptr)
399{
400 *ptr = map();
401 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
402}
403
404XGL_RESULT XglImage::UnmapMemory()
405{
406 unmap();
407 return XGL_SUCCESS;
408}
409
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800410XglTextureObj::XglTextureObj(XglDevice *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700411{
412 m_device = device;
413 const XGL_FORMAT tex_format = { XGL_CH_FMT_B8G8R8A8, XGL_NUM_FMT_UNORM };
Tony Barboure2c58df2014-11-25 13:18:32 -0700414 const uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barboure2c58df2014-11-25 13:18:32 -0700415
416 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
417
418 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
419
420 const XGL_IMAGE_CREATE_INFO image = {
421 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
422 .pNext = NULL,
423 .imageType = XGL_IMAGE_2D,
424 .format = tex_format,
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800425 .extent = { 16, 16, 1 },
Tony Barboure2c58df2014-11-25 13:18:32 -0700426 .mipLevels = 1,
427 .arraySize = 1,
428 .samples = 1,
429 .tiling = XGL_LINEAR_TILING,
430 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
431 .flags = 0,
432 };
433
Tony Barboure2c58df2014-11-25 13:18:32 -0700434 XGL_IMAGE_VIEW_CREATE_INFO view;
435 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
436 view.pNext = NULL;
437 view.image = XGL_NULL_HANDLE;
438 view.viewType = XGL_IMAGE_VIEW_2D;
439 view.format = image.format;
440 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
441 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
442 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
443 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
444 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
445 view.subresourceRange.baseMipLevel = 0;
446 view.subresourceRange.mipLevels = 1;
447 view.subresourceRange.baseArraySlice = 0;
448 view.subresourceRange.arraySize = 1;
449 view.minLod = 0.0f;
450
Tony Barboure2c58df2014-11-25 13:18:32 -0700451 /* create image */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800452 init(*m_device, image);
Tony Barboure2c58df2014-11-25 13:18:32 -0700453
454 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800455 view.image = obj();
456 m_textureView.init(*m_device, view);
Tony Barboure2c58df2014-11-25 13:18:32 -0700457
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800458 XGL_SUBRESOURCE_LAYOUT layout =
459 subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
460 m_rowPitch = layout.rowPitch;
Tony Barboure2c58df2014-11-25 13:18:32 -0700461
Tony Barboure2c58df2014-11-25 13:18:32 -0700462 XGL_VOID *data;
463 XGL_INT x, y;
464
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800465 data = map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700466
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800467 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700468 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800469 for (x = 0; x < extent().width; x++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700470 row[x] = tex_colors[(x & 1) ^ (y & 1)];
471 }
472
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800473 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700474
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800475 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700476
477}
Tony Barbour82c39522014-12-04 14:33:33 -0700478
Tony Barboure2c58df2014-11-25 13:18:32 -0700479void XglTextureObj::ChangeColors(uint32_t color1, uint32_t color2)
480{
Tony Barboure2c58df2014-11-25 13:18:32 -0700481 const uint32_t tex_colors[2] = { color1, color2 };
482 XGL_VOID *data;
483
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800484 data = map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700485
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800486 for (int y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700487 uint32_t *row = (uint32_t *) ((char *) data + m_rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800488 for (int x = 0; x < extent().width; x++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700489 row[x] = tex_colors[(x & 1) ^ (y & 1)];
490 }
491
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800492 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700493}
494
495XglSamplerObj::XglSamplerObj(XglDevice *device)
496{
Tony Barboure2c58df2014-11-25 13:18:32 -0700497 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700498
Chia-I Wue9864b52014-12-28 16:32:24 +0800499 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
500 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
501 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
502 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
503 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
504 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
505 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
506 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
507 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
508 samplerCreateInfo.mipLodBias = 0.0;
509 samplerCreateInfo.maxAnisotropy = 0.0;
510 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
511 samplerCreateInfo.minLod = 0.0;
512 samplerCreateInfo.maxLod = 0.0;
513 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700514
Chia-I Wue9864b52014-12-28 16:32:24 +0800515 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700516}
Tony Barboure2c58df2014-11-25 13:18:32 -0700517
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700518/*
519 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
520 */
521XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
522{
523 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700524 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700525
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800526 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700527}
528
Tony Barboure2c58df2014-11-25 13:18:32 -0700529XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
530{
Tony Barboure2c58df2014-11-25 13:18:32 -0700531 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800532 m_commandBuffer = 0;
533
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800534 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700535 m_numVertices = constantCount;
536 m_stride = constantSize;
537
Chia-I Wua07fee62014-12-28 15:26:08 +0800538 const size_t allocationSize = constantCount * constantSize;
539 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700540
Chia-I Wua07fee62014-12-28 15:26:08 +0800541 void *pData = map();
542 memcpy(pData, data, allocationSize);
543 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700544
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800545 // set up the buffer view for the constant buffer
546 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
547 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
548 view_info.buffer = obj();
549 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
550 view_info.stride = 16;
551 view_info.format.channelFormat = XGL_CH_FMT_R32G32B32A32;
552 view_info.format.numericFormat = XGL_NUM_FMT_FLOAT;
553 view_info.channels.r = XGL_CHANNEL_SWIZZLE_R;
554 view_info.channels.g = XGL_CHANNEL_SWIZZLE_G;
555 view_info.channels.b = XGL_CHANNEL_SWIZZLE_B;
556 view_info.channels.a = XGL_CHANNEL_SWIZZLE_A;
557 view_info.offset = 0;
558 view_info.range = allocationSize;
559 m_bufferView.init(*m_device, view_info);
560
561 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
562 this->m_bufferViewInfo.view = m_bufferView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700563}
Tony Barbour82c39522014-12-04 14:33:33 -0700564
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700565void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, XGL_UINT binding)
566{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800567 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700568}
569
570
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000571void XglConstantBufferObj::BufferMemoryBarrier(
572 XGL_FLAGS outputMask /*=
573 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
574 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
575 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
576 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
577 XGL_MEMORY_OUTPUT_COPY_BIT*/,
578 XGL_FLAGS inputMask /*=
579 XGL_MEMORY_INPUT_CPU_READ_BIT |
580 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
581 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
582 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
583 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
584 XGL_MEMORY_INPUT_SHADER_READ_BIT |
585 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
586 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
587 XGL_MEMORY_INPUT_COPY_BIT*/)
Tony Barboure2c58df2014-11-25 13:18:32 -0700588{
Tony Barbour38422802014-12-10 14:36:31 -0700589 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700590
Tony Barbour38422802014-12-10 14:36:31 -0700591 if (!m_commandBuffer)
592 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800593 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700594
595 m_commandBuffer = new XglCommandBufferObj(m_device);
596
597 }
598 else
599 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800600 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700601 }
602
Tony Barboure2c58df2014-11-25 13:18:32 -0700603 // open the command buffer
Jon Ashburnc4164b12014-12-31 17:10:47 -0700604 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
605 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
606 .pNext = NULL,
607 .flags = 0,
608 };
609 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700610 ASSERT_XGL_SUCCESS(err);
611
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000612 XGL_BUFFER_MEMORY_BARRIER memory_barrier =
613 buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride);
Tony Barboure2c58df2014-11-25 13:18:32 -0700614
Mike Stroyanfb80d5f2014-12-04 11:08:39 +0000615 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
616 XGL_PIPELINE_BARRIER pipeline_barrier = {};
617 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
618 pipeline_barrier.eventCount = 1;
619 pipeline_barrier.pEvents = set_events;
620 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
621 pipeline_barrier.memBarrierCount = 1;
622 pipeline_barrier.pMemBarriers = &memory_barrier;
623
624 // write barrier to the command buffer
625 m_commandBuffer->PipelineBarrier(&pipeline_barrier);
Tony Barboure2c58df2014-11-25 13:18:32 -0700626
627 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700628 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700629 ASSERT_XGL_SUCCESS(err);
630
631 XGL_UINT32 numMemRefs=1;
632 XGL_MEMORY_REF memRefs;
633 // this command buffer only uses the vertex buffer memory
634 memRefs.flags = 0;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800635 memRefs.mem = memories()[0];
Tony Barboure2c58df2014-11-25 13:18:32 -0700636
637 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700638 XGL_CMD_BUFFER bufferArray[1];
639 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800640 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700641 ASSERT_XGL_SUCCESS(err);
642}
643
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700644XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
645 : XglConstantBufferObj(device)
646{
647
648}
649
650void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
651{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700652 XGL_FORMAT viewFormat;
653
654 m_numVertices = numIndexes;
655 m_indexType = indexType;
656 viewFormat.numericFormat = XGL_NUM_FMT_UINT;
657 switch (indexType) {
658 case XGL_INDEX_8:
659 m_stride = 1;
660 viewFormat.channelFormat = XGL_CH_FMT_R8;
661 break;
662 case XGL_INDEX_16:
663 m_stride = 2;
664 viewFormat.channelFormat = XGL_CH_FMT_R16;
665 break;
666 case XGL_INDEX_32:
667 m_stride = 4;
668 viewFormat.channelFormat = XGL_CH_FMT_R32;
669 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800670 default:
671 assert(!"unknown index type");
672 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700673 }
674
Chia-I Wua07fee62014-12-28 15:26:08 +0800675 const size_t allocationSize = numIndexes * m_stride;
676 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700677
Chia-I Wua07fee62014-12-28 15:26:08 +0800678 void *pData = map();
679 memcpy(pData, data, allocationSize);
680 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700681
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800682 // set up the buffer view for the constant buffer
683 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
684 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
685 view_info.buffer = obj();
686 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
687 view_info.stride = m_stride;
688 view_info.format.channelFormat = viewFormat.channelFormat;
689 view_info.format.numericFormat = viewFormat.numericFormat;
690 view_info.channels.r = XGL_CHANNEL_SWIZZLE_R;
691 view_info.channels.g = XGL_CHANNEL_SWIZZLE_G;
692 view_info.channels.b = XGL_CHANNEL_SWIZZLE_B;
693 view_info.channels.a = XGL_CHANNEL_SWIZZLE_A;
694 view_info.offset = 0;
695 view_info.range = allocationSize;
696 m_bufferView.init(*m_device, view_info);
697
698 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
699 this->m_bufferViewInfo.view = m_bufferView.obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700700}
701
702void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
703{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800704 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700705}
Tony Barboure2c58df2014-11-25 13:18:32 -0700706
Tony Barbouraf1f9192014-12-17 10:57:58 -0700707XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
708{
709 return m_indexType;
710}
711
Tony Barbour5420af02014-12-03 13:58:15 -0700712XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo(XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700713{
714 XGL_DESCRIPTOR_SLOT_INFO *slotInfo;
715 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
716 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
717 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800718 stageInfo->shader.shader = obj();
Tony Barbourf52346d2015-01-16 14:27:35 -0700719 stageInfo->shader.descriptorSetMappingCount = 1;
720 stageInfo->shader.pDescriptorSetMapping = (XGL_DESCRIPTOR_SET_MAPPING *)malloc(sizeof(XGL_DESCRIPTOR_SET_MAPPING));
721 stageInfo->shader.pDescriptorSetMapping->descriptorCount = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700722 stageInfo->shader.linkConstBufferCount = 0;
723 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800724 stageInfo->shader.dynamicBufferViewMapping.slotObjectType = XGL_SLOT_UNUSED;
725 stageInfo->shader.dynamicBufferViewMapping.shaderEntityIndex = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700726
Tony Barbourf52346d2015-01-16 14:27:35 -0700727 stageInfo->shader.pDescriptorSetMapping->descriptorCount = descriptorSet->GetTotalSlots();
728 if (stageInfo->shader.pDescriptorSetMapping->descriptorCount)
Tony Barboure2c58df2014-11-25 13:18:32 -0700729 {
730 vector<int> allSlots;
731 vector<XGL_DESCRIPTOR_SET_SLOT_TYPE> allTypes;
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800732 vector<void *> allObjs;
Tony Barboure2c58df2014-11-25 13:18:32 -0700733
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800734 allSlots.reserve(m_bufferSlots.size() + m_imageSlots.size() + m_samplerSlots.size());
735 allTypes.reserve(m_bufferTypes.size() + m_imageTypes.size() + m_samplerTypes.size());
736 allObjs.reserve(m_bufferObjs.size() + m_imageObjs.size() + m_samplerObjs.size());
Tony Barboure2c58df2014-11-25 13:18:32 -0700737
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800738 if (m_bufferSlots.size())
Tony Barboure2c58df2014-11-25 13:18:32 -0700739 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800740 allSlots.insert(allSlots.end(), m_bufferSlots.begin(), m_bufferSlots.end());
741 allTypes.insert(allTypes.end(), m_bufferTypes.begin(), m_bufferTypes.end());
742 allObjs.insert(allObjs.end(), m_bufferObjs.begin(), m_bufferObjs.end());
Tony Barboure2c58df2014-11-25 13:18:32 -0700743 }
744 if (m_imageSlots.size())
745 {
746 allSlots.insert(allSlots.end(), m_imageSlots.begin(), m_imageSlots.end());
747 allTypes.insert(allTypes.end(), m_imageTypes.begin(), m_imageTypes.end());
748 allObjs.insert(allObjs.end(), m_imageObjs.begin(), m_imageObjs.end());
749 }
750 if (m_samplerSlots.size())
751 {
752 allSlots.insert(allSlots.end(), m_samplerSlots.begin(), m_samplerSlots.end());
753 allTypes.insert(allTypes.end(), m_samplerTypes.begin(), m_samplerTypes.end());
754 allObjs.insert(allObjs.end(), m_samplerObjs.begin(), m_samplerObjs.end());
755 }
756
Tony Barbour5420af02014-12-03 13:58:15 -0700757 slotInfo = descriptorSet->GetSlotInfo(allSlots, allTypes, allObjs);
Tony Barbourf52346d2015-01-16 14:27:35 -0700758 stageInfo->shader.pDescriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700759 }
760 return stageInfo;
761}
762
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800763void XglShaderObj::BindShaderEntitySlotToBuffer(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglConstantBufferObj *constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700764{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800765 m_bufferSlots.push_back(slot);
766 m_bufferTypes.push_back(type);
767 m_bufferObjs.push_back(&constantBuffer->m_bufferViewInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700768
769}
Tony Barbour82c39522014-12-04 14:33:33 -0700770
Tony Barboure2c58df2014-11-25 13:18:32 -0700771void XglShaderObj::BindShaderEntitySlotToImage(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglTextureObj *texture)
772{
773 m_imageSlots.push_back(slot);
774 m_imageTypes.push_back(type);
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800775 m_imageObjs.push_back(&texture->m_textureViewInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700776
777}
Tony Barbour82c39522014-12-04 14:33:33 -0700778
Tony Barboure2c58df2014-11-25 13:18:32 -0700779void XglShaderObj::BindShaderEntitySlotToSampler(int slot, XglSamplerObj *sampler)
780{
781 m_samplerSlots.push_back(slot);
782 m_samplerTypes.push_back(XGL_SLOT_SHADER_SAMPLER);
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800783 m_samplerObjs.push_back(sampler);
Tony Barboure2c58df2014-11-25 13:18:32 -0700784
785}
Tony Barbour82c39522014-12-04 14:33:33 -0700786
Tony Barboure2c58df2014-11-25 13:18:32 -0700787XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
788{
789 XGL_RESULT err = XGL_SUCCESS;
790 std::vector<unsigned int> bil;
791 XGL_SHADER_CREATE_INFO createInfo;
792 size_t shader_len;
793
794 m_stage = stage;
795 m_device = device;
796
797 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
798 createInfo.pNext = NULL;
799
800 if (!framework->m_use_bil) {
801
802 shader_len = strlen(shader_code);
803 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
804 createInfo.pCode = malloc(createInfo.codeSize);
805 createInfo.flags = 0;
806
807 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
808 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
809 ((uint32_t *) createInfo.pCode)[1] = 0;
810 ((uint32_t *) createInfo.pCode)[2] = stage;
811 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
812
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800813 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700814 }
815
816 if (framework->m_use_bil || err) {
817 std::vector<unsigned int> bil;
818 err = XGL_SUCCESS;
819
820 // Use Reference GLSL to BIL compiler
821 framework->GLSLtoBIL(stage, shader_code, bil);
822 createInfo.pCode = bil.data();
823 createInfo.codeSize = bil.size() * sizeof(unsigned int);
824 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700825
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800826 init(*m_device, createInfo);
827 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700828}
Tony Barbour82c39522014-12-04 14:33:33 -0700829
Tony Barboure2c58df2014-11-25 13:18:32 -0700830XglPipelineObj::XglPipelineObj(XglDevice *device)
831{
Tony Barboure2c58df2014-11-25 13:18:32 -0700832 m_device = device;
833 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
834 m_vertexBufferCount = 0;
835
836 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
837 m_ia_state.pNext = XGL_NULL_HANDLE;
838 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
839 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700840 m_ia_state.primitiveRestartEnable = XGL_FALSE;
841 m_ia_state.primitiveRestartIndex = 0;
842
843 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
844 m_rs_state.pNext = &m_ia_state;
845 m_rs_state.depthClipEnable = XGL_FALSE;
846 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -0700847 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
848 m_rs_state.fillMode = XGL_FILL_SOLID;
849 m_rs_state.cullMode = XGL_CULL_NONE;
850 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700851
Tony Barboure2c58df2014-11-25 13:18:32 -0700852 memset(&m_cb_state,0,sizeof(m_cb_state));
853 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
854 m_cb_state.pNext = &m_rs_state;
855 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
856 m_cb_state.dualSourceBlendEnable = XGL_FALSE;
857 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
858
Tony Barbourf52346d2015-01-16 14:27:35 -0700859 m_ms_state.pNext = &m_cb_state;
860 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
861 m_ms_state.multisampleEnable = XGL_FALSE;
862 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
863 m_ms_state.samples = 1;
864 m_ms_state.minSampleShading = 0;
865 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700866
Tony Barbourf52346d2015-01-16 14:27:35 -0700867 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
868 m_ds_state.pNext = &m_ms_state,
869 m_ds_state.format.channelFormat = XGL_CH_FMT_R32;
870 m_ds_state.format.numericFormat = XGL_NUM_FMT_DS;
871 m_ds_state.depthTestEnable = XGL_FALSE;
872 m_ds_state.depthWriteEnable = XGL_FALSE;
873 m_ds_state.depthBoundsEnable = XGL_FALSE;
874 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
875 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
876 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
877 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
878 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
879 m_ds_state.stencilTestEnable = XGL_FALSE;
880 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -0700881
Tony Barbourf52346d2015-01-16 14:27:35 -0700882 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
883 att.blendEnable = XGL_FALSE;
884 att.format.channelFormat = XGL_CH_FMT_R8G8B8A8;
885 att.format.numericFormat = XGL_NUM_FMT_UNORM;
886 att.channelWriteMask = 0xf;
887 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -0700888
889};
890
891void XglPipelineObj::AddShader(XglShaderObj* shader)
892{
893 m_shaderObjs.push_back(shader);
894}
895
896void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
897{
898 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
899 m_vi_state.attributeCount = count;
900}
901
902void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
903{
904 m_vi_state.pVertexBindingDescriptions = vi_binding;
905 m_vi_state.bindingCount = count;
906}
907
908void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
909{
910 m_vertexBufferObjs.push_back(vertexDataBuffer);
911 m_vertexBufferBindings.push_back(binding);
912 m_vertexBufferCount++;
913}
914
Tony Barbourf52346d2015-01-16 14:27:35 -0700915void XglPipelineObj::AddColorAttachment(XGL_UINT binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +0800916{
Tony Barbourf52346d2015-01-16 14:27:35 -0700917 if (binding+1 > m_colorAttachments.size())
918 {
919 m_colorAttachments.resize(binding+1);
920 }
921 m_colorAttachments[binding] = *att;
922}
923
924void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
925{
926 m_ds_state.format = ds_state->format;
927 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
928 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
929 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
930 m_ds_state.depthFunc = ds_state->depthFunc;
931 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
932 m_ds_state.back = ds_state->back;
933 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +0800934}
935
Tony Barbour976e1cf2014-12-17 11:57:31 -0700936void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
937{
Tony Barbourf52346d2015-01-16 14:27:35 -0700938 XGL_VOID* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -0700939 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
940
941 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
942
943 for (int i=0; i<m_shaderObjs.size(); i++)
944 {
945 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet);
946 shaderCreateInfo->pNext = head_ptr;
947 head_ptr = shaderCreateInfo;
948 }
949
950 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
951 {
952 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
953 m_vi_state.pNext = head_ptr;
954 head_ptr = &m_vi_state;
955 }
956
957 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
958 info.pNext = head_ptr;
959 info.flags = 0;
960
Tony Barbourf52346d2015-01-16 14:27:35 -0700961 m_cb_state.attachmentCount = m_colorAttachments.size();
962 m_cb_state.pAttachments = &m_colorAttachments[0];
963
Chia-I Wu2648d092014-12-29 14:24:14 +0800964 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -0700965}
Chia-I Wu2648d092014-12-29 14:24:14 +0800966
Tony Barbour976e1cf2014-12-17 11:57:31 -0700967XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
968{
Chia-I Wu2648d092014-12-29 14:24:14 +0800969 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700970}
971
Tony Barbour5420af02014-12-03 13:58:15 -0700972void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700973{
Tony Barbourf52346d2015-01-16 14:27:35 -0700974 XGL_VOID* head_ptr = &m_ds_state;
Tony Barboure2c58df2014-11-25 13:18:32 -0700975 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
976
977 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700978
979 for (int i=0; i<m_shaderObjs.size(); i++)
980 {
981 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet);
982 shaderCreateInfo->pNext = head_ptr;
983 head_ptr = shaderCreateInfo;
984 }
985
986 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
987 {
988 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
989 m_vi_state.pNext = head_ptr;
990 head_ptr = &m_vi_state;
991 }
992
993 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
994 info.pNext = head_ptr;
995 info.flags = 0;
996
Chia-I Wu2648d092014-12-29 14:24:14 +0800997 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700998
Chia-I Wu2648d092014-12-29 14:24:14 +0800999 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -07001000
1001
1002 for (int i=0; i < m_vertexBufferCount; i++)
1003 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001004 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001005 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001006}
Tony Barbour82c39522014-12-04 14:33:33 -07001007
Tony Barboure2c58df2014-11-25 13:18:32 -07001008XglMemoryRefManager::XglMemoryRefManager() {
1009
1010}
Tony Barbour82c39522014-12-04 14:33:33 -07001011
Tony Barboure2c58df2014-11-25 13:18:32 -07001012void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001013 const std::vector<XGL_GPU_MEMORY> mems = constantBuffer->memories();
1014 if (!mems.empty())
1015 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001016}
Tony Barbour82c39522014-12-04 14:33:33 -07001017
Tony Barboure2c58df2014-11-25 13:18:32 -07001018void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
Chia-I Wu13a3aa82014-12-28 15:55:09 +08001019 const std::vector<XGL_GPU_MEMORY> mems = texture->memories();
1020 if (!mems.empty())
1021 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001022}
Tony Barbour82c39522014-12-04 14:33:33 -07001023
Tony Barboure2c58df2014-11-25 13:18:32 -07001024XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
1025
1026 XGL_MEMORY_REF *localRefs;
1027 XGL_UINT32 numRefs=m_bufferObjs.size();
1028
1029 if (numRefs <= 0)
1030 return NULL;
1031
1032 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
1033 for (int i=0; i<numRefs; i++)
1034 {
1035 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +08001036 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -07001037 }
1038 return localRefs;
1039}
1040int XglMemoryRefManager::GetNumRefs() {
1041 return m_bufferObjs.size();
1042}
Tony Barbour6d047bf2014-12-10 14:34:45 -07001043
1044XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Chia-I Wud28343c2014-12-28 15:12:48 +08001045 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(XGL_QUEUE_TYPE_GRAPHICS))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001046{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001047 m_device = device;
Chia-I Wud28343c2014-12-28 15:12:48 +08001048 m_renderTargetCount = 0;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001049}
Tony Barbour471338d2014-12-10 17:28:39 -07001050
Tony Barbour6d047bf2014-12-10 14:34:45 -07001051XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1052{
Chia-I Wud28343c2014-12-28 15:12:48 +08001053 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001054}
Tony Barbour471338d2014-12-10 17:28:39 -07001055
Jon Ashburnc4164b12014-12-31 17:10:47 -07001056XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001057{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001058 begin(pInfo);
1059 return XGL_SUCCESS;
1060}
1061
1062XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj)
1063{
1064 begin(renderpass_obj);
1065 return XGL_SUCCESS;
1066}
1067
1068XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
1069{
1070 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +08001071 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001072}
1073
1074XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1075{
Chia-I Wud28343c2014-12-28 15:12:48 +08001076 end();
1077 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001078}
1079
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001080void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001081{
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001082 xglCmdPipelineBarrier(obj(), barrierPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001083}
1084
Tony Barbour30cc9e82014-12-17 11:53:55 -07001085void XglCommandBufferObj::ClearAllBuffers(XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
1086{
1087 XGL_UINT i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001088 const XGL_FLAGS output_mask =
1089 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1090 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1091 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1092 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1093 XGL_MEMORY_OUTPUT_COPY_BIT;
1094 const XGL_FLAGS input_mask = 0;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001095
1096 // whatever we want to do, we do it to the whole buffer
1097 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1098 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1099 srRange.baseMipLevel = 0;
1100 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1101 srRange.baseArraySlice = 0;
1102 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1103
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001104 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1105 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1106 memory_barrier.outputMask = output_mask;
1107 memory_barrier.inputMask = input_mask;
1108 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1109 memory_barrier.subresourceRange = srRange;
1110
1111 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1112 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1113 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1114 pipeline_barrier.eventCount = 1;
1115 pipeline_barrier.pEvents = set_events;
1116 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1117 pipeline_barrier.memBarrierCount = 1;
1118 pipeline_barrier.pMemBarriers = &memory_barrier;
1119
Tony Barbour30cc9e82014-12-17 11:53:55 -07001120 // clear the back buffer to dark grey
1121 XGL_UINT clearColor[4] = {64, 64, 64, 0};
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001122
Tony Barbour30cc9e82014-12-17 11:53:55 -07001123 for (i = 0; i < m_renderTargetCount; i++) {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001124 memory_barrier.image = m_renderTargets[i]->image();
1125 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1126 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1127 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001128
Chia-I Wud28343c2014-12-28 15:12:48 +08001129 xglCmdClearColorImageRaw( obj(), m_renderTargets[i]->image(), clearColor, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001130 }
1131
1132 if (depthStencilImage)
1133 {
1134 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1135 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1136 dsRange.baseMipLevel = 0;
1137 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1138 dsRange.baseArraySlice = 0;
1139 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1140
1141 // prepare the depth buffer for clear
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001142
1143 memory_barrier.oldLayout = depthStencilBinding->layout;
1144 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1145 memory_barrier.image = depthStencilImage;
1146 memory_barrier.subresourceRange = dsRange;
1147
1148 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1149 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001150
Chia-I Wud28343c2014-12-28 15:12:48 +08001151 xglCmdClearDepthStencil(obj(), depthStencilImage, 1.0f, 0, 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001152
1153 // prepare depth buffer for rendering
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001154 memory_barrier.image = depthStencilImage;
1155 memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL;
1156 memory_barrier.newLayout = depthStencilBinding->layout;
1157 memory_barrier.subresourceRange = dsRange;
1158 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1159 depthStencilBinding->layout = memory_barrier.newLayout;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001160 }
1161}
1162
Jon Ashburncdc40be2015-01-02 18:27:14 -07001163void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001164{
1165 XGL_UINT i;
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001166 const XGL_FLAGS output_mask =
1167 XGL_MEMORY_OUTPUT_CPU_WRITE_BIT |
1168 XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1169 XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1170 XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1171 XGL_MEMORY_OUTPUT_COPY_BIT;
1172 const XGL_FLAGS input_mask =
1173 XGL_MEMORY_INPUT_CPU_READ_BIT |
1174 XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1175 XGL_MEMORY_INPUT_INDEX_FETCH_BIT |
1176 XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1177 XGL_MEMORY_INPUT_UNIFORM_READ_BIT |
1178 XGL_MEMORY_INPUT_SHADER_READ_BIT |
1179 XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1180 XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
1181 XGL_MEMORY_INPUT_COPY_BIT;
1182
Tony Barbour30cc9e82014-12-17 11:53:55 -07001183 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1184 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1185 srRange.baseMipLevel = 0;
1186 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1187 srRange.baseArraySlice = 0;
1188 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1189
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001190 XGL_IMAGE_MEMORY_BARRIER memory_barrier = {};
1191 memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1192 memory_barrier.outputMask = output_mask;
1193 memory_barrier.inputMask = input_mask;
1194 memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1195 memory_barrier.subresourceRange = srRange;
1196
1197 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
1198 XGL_PIPELINE_BARRIER pipeline_barrier = {};
1199 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
1200 pipeline_barrier.eventCount = 1;
1201 pipeline_barrier.pEvents = set_events;
1202 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
1203 pipeline_barrier.memBarrierCount = 1;
1204 pipeline_barrier.pMemBarriers = &memory_barrier;
1205
Tony Barbour30cc9e82014-12-17 11:53:55 -07001206 for(i=0; i<m_renderTargetCount; i++)
1207 {
Mike Stroyanfb80d5f2014-12-04 11:08:39 +00001208 memory_barrier.image = m_renderTargets[i]->image();
1209 memory_barrier.oldLayout = m_renderTargets[i]->layout();
1210 xglCmdPipelineBarrier( obj(), &pipeline_barrier);
1211 m_renderTargets[i]->layout(memory_barrier.newLayout);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001212 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001213}
1214
Tony Barbourf52346d2015-01-16 14:27:35 -07001215void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001216{
Tony Barbourf52346d2015-01-16 14:27:35 -07001217 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001218}
1219
1220void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1221{
1222 m_renderTargets.push_back(renderTarget);
1223 m_renderTargetCount++;
1224}
1225
1226void XglCommandBufferObj::DrawIndexed(XGL_UINT firstIndex, XGL_UINT indexCount, XGL_INT vertexOffset, XGL_UINT firstInstance, XGL_UINT instanceCount)
1227{
Chia-I Wud28343c2014-12-28 15:12:48 +08001228 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001229}
1230
1231void XglCommandBufferObj::Draw(XGL_UINT firstVertex, XGL_UINT vertexCount, XGL_UINT firstInstance, XGL_UINT instanceCount)
1232{
Chia-I Wud28343c2014-12-28 15:12:48 +08001233 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001234}
1235
1236void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs)
1237{
1238 XGL_RESULT err = XGL_SUCCESS;
1239
1240 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001241 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001242 ASSERT_XGL_SUCCESS( err );
1243
1244 err = xglQueueWaitIdle( m_device->m_queue );
1245 ASSERT_XGL_SUCCESS( err );
1246
1247 // Wait for work to finish before cleaning up.
1248 xglDeviceWaitIdle(m_device->device());
1249
1250}
1251void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1252{
Chia-I Wud28343c2014-12-28 15:12:48 +08001253 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001254}
1255
1256void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1257{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001258 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wud28343c2014-12-28 15:12:48 +08001259 xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, descriptorSet, 0 );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001260}
1261void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, XGL_UINT offset)
1262{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001263 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001264}
1265void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, XGL_UINT offset, XGL_UINT binding)
1266{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001267 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001268}