blob: ef1a949d3e007e425975b5cf9a61a803a2a99cb1 [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();
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700158 m_colorBindings[i].colorAttachmentState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_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);
190
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600191}
192
Chia-I Wufb1459b2014-12-29 15:23:20 +0800193XglDevice::XglDevice(XGL_UINT id, XGL_PHYSICAL_GPU obj) :
194 xgl_testing::Device(obj), id(id)
195{
196 init();
197
198 props = gpu().properties();
199 queue_props = &gpu().queue_properties()[0];
200}
201
202void XglDevice::get_device_queue()
203{
204 ASSERT_NE(true, graphics_queues().empty());
205 m_queue = graphics_queues()[0]->obj();
206}
207
Tony Barboure2c58df2014-11-25 13:18:32 -0700208XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
209{
210 m_device = device;
211 m_nextSlot = 0;
212
213}
214
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800215void XglDescriptorSetObj::AttachBufferView(XglConstantBufferObj *constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700216{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800217 m_bufferViews.push_back(&constantBuffer->m_bufferViewInfo);
218 m_bufferSlots.push_back(m_nextSlot);
Tony Barboure2c58df2014-11-25 13:18:32 -0700219 m_nextSlot++;
220
221}
Tony Barbour82c39522014-12-04 14:33:33 -0700222
Tony Barboure2c58df2014-11-25 13:18:32 -0700223void XglDescriptorSetObj::AttachSampler(XglSamplerObj *sampler)
224{
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800225 m_samplers.push_back(sampler);
Tony Barboure2c58df2014-11-25 13:18:32 -0700226 m_samplerSlots.push_back(m_nextSlot);
227 m_nextSlot++;
228
229}
Tony Barbour82c39522014-12-04 14:33:33 -0700230
Tony Barboure2c58df2014-11-25 13:18:32 -0700231void XglDescriptorSetObj::AttachImageView(XglTextureObj *texture)
232{
233 m_imageViews.push_back(&texture->m_textureViewInfo);
234 m_imageSlots.push_back(m_nextSlot);
235 m_nextSlot++;
236
237}
Tony Barbour82c39522014-12-04 14:33:33 -0700238
Tony Barboure2c58df2014-11-25 13:18:32 -0700239XGL_DESCRIPTOR_SLOT_INFO* XglDescriptorSetObj::GetSlotInfo(vector<int>slots,
240 vector<XGL_DESCRIPTOR_SET_SLOT_TYPE>types,
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800241 vector<void *>objs )
Tony Barboure2c58df2014-11-25 13:18:32 -0700242{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800243 int nSlots = m_bufferSlots.size() + m_imageSlots.size() + m_samplerSlots.size();
Tony Barboure2c58df2014-11-25 13:18:32 -0700244 m_slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( nSlots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
245 memset(m_slotInfo,0,nSlots*sizeof(XGL_DESCRIPTOR_SLOT_INFO));
246
247 for (int i=0; i<nSlots; i++)
248 {
249 m_slotInfo[i].slotObjectType = XGL_SLOT_UNUSED;
250 }
251
252 for (int i=0; i<slots.size(); i++)
253 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800254 for (int j=0; j<m_bufferSlots.size(); j++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700255 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800256 if ( m_bufferViews[j] == objs[i])
Tony Barboure2c58df2014-11-25 13:18:32 -0700257 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800258 m_slotInfo[m_bufferSlots[j]].shaderEntityIndex = slots[i];
259 m_slotInfo[m_bufferSlots[j]].slotObjectType = types[i];
Tony Barboure2c58df2014-11-25 13:18:32 -0700260 }
261 }
262 for (int j=0; j<m_imageSlots.size(); j++)
263 {
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800264 if ( m_imageViews[j] == objs[i])
Tony Barboure2c58df2014-11-25 13:18:32 -0700265 {
266 m_slotInfo[m_imageSlots[j]].shaderEntityIndex = slots[i];
267 m_slotInfo[m_imageSlots[j]].slotObjectType = types[i];
268 }
269 }
270 for (int j=0; j<m_samplerSlots.size(); j++)
271 {
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800272 if ( m_samplers[j] == objs[i])
Tony Barboure2c58df2014-11-25 13:18:32 -0700273 {
274 m_slotInfo[m_samplerSlots[j]].shaderEntityIndex = slots[i];
275 m_slotInfo[m_samplerSlots[j]].slotObjectType = types[i];
276 }
277 }
278 }
279
280 // for (int i=0;i<nSlots;i++)
281 // {
282 // printf("SlotInfo[%d]: Index = %d, Type = %d\n",i,m_slotInfo[i].shaderEntityIndex, m_slotInfo[i].slotObjectType);
283 // fflush(stdout);
284 // }
285
286 return(m_slotInfo);
287
288}
Tony Barbourb5f4d082014-12-17 10:54:03 -0700289void XglDescriptorSetObj::CreateXGLDescriptorSet()
290{
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800291 init(*m_device, xgl_testing::DescriptorSet::create_info(m_nextSlot));
Tony Barbourb5f4d082014-12-17 10:54:03 -0700292
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800293 begin();
294 clear();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700295
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800296 for (int i=0; i<m_bufferViews.size();i++)
Tony Barbourb5f4d082014-12-17 10:54:03 -0700297 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800298 attach(m_bufferSlots[i], *m_bufferViews[i]);
Tony Barbourb5f4d082014-12-17 10:54:03 -0700299 }
300 for (int i=0; i<m_samplers.size();i++)
301 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800302 attach(m_samplerSlots[i], *m_samplers[i]);
Tony Barbourb5f4d082014-12-17 10:54:03 -0700303 }
304 for (int i=0; i<m_imageViews.size();i++)
305 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800306 attach(m_imageSlots[i], *m_imageViews[i]);
Tony Barbourb5f4d082014-12-17 10:54:03 -0700307 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800308
309 end();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700310}
311
312XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle()
313{
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800314 return obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700315}
Tony Barboure2c58df2014-11-25 13:18:32 -0700316
Tony Barbour824b7712014-12-18 17:06:21 -0700317int XglDescriptorSetObj::GetTotalSlots()
318{
319 return m_nextSlot;
320}
321
Tony Barboure2c58df2014-11-25 13:18:32 -0700322void XglDescriptorSetObj::BindCommandBuffer(XGL_CMD_BUFFER commandBuffer)
323{
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800324 init(*m_device, xgl_testing::DescriptorSet::create_info(m_nextSlot));
Tony Barboure2c58df2014-11-25 13:18:32 -0700325
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800326 begin();
327 clear();
Tony Barboure2c58df2014-11-25 13:18:32 -0700328
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800329 for (int i=0; i<m_bufferViews.size();i++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700330 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800331 attach(m_bufferSlots[i], *m_bufferViews[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700332 }
333 for (int i=0; i<m_samplers.size();i++)
334 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800335 attach(m_samplerSlots[i], *m_samplers[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700336 }
337 for (int i=0; i<m_imageViews.size();i++)
338 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800339 attach(m_imageSlots[i], *m_imageViews[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700340 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800341
342 end();
Tony Barboure2c58df2014-11-25 13:18:32 -0700343
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800344 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800345 xglCmdBindDescriptorSet(commandBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, obj(), 0 );
Tony Barbour25ef8a62014-12-03 13:59:18 -0700346}
Tony Barboure2c58df2014-11-25 13:18:32 -0700347
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800348XglImage::XglImage(XglDevice *dev)
349{
350 m_device = dev;
351 m_imageInfo.view = XGL_NULL_HANDLE;
352 m_imageInfo.state = XGL_IMAGE_STATE_UNINITIALIZED_TARGET;
353}
354
355void XglImage::init(XGL_UINT32 w, XGL_UINT32 h,
356 XGL_FORMAT fmt, XGL_FLAGS usage,
357 XGL_IMAGE_TILING tiling)
358{
359 XGL_UINT mipCount;
360
361 mipCount = 0;
362
363 XGL_UINT _w = w;
364 XGL_UINT _h = h;
365 while( ( _w > 0 ) || ( _h > 0 ) )
366 {
367 _w >>= 1;
368 _h >>= 1;
369 mipCount++;
370 }
371
372 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
373 imageCreateInfo.imageType = XGL_IMAGE_2D;
374 imageCreateInfo.format = fmt;
375 imageCreateInfo.extent.width = w;
376 imageCreateInfo.extent.height = h;
377 imageCreateInfo.mipLevels = mipCount;
378 imageCreateInfo.tiling = tiling;
379
380 imageCreateInfo.usage = usage;
381
382 xgl_testing::Image::init(*m_device, imageCreateInfo);
383
384 m_imageInfo.state = XGL_IMAGE_STATE_UNINITIALIZED_TARGET;
385
386 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO createView = {
387 XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
388 XGL_NULL_HANDLE,
389 obj(),
390 {XGL_CH_FMT_R8G8B8A8, XGL_NUM_FMT_UNORM},
391 0,
392 0,
393 1
394 };
395
396 m_targetView.init(*m_device, createView);
397}
398
399XGL_RESULT XglImage::MapMemory(XGL_VOID** ptr)
400{
401 *ptr = map();
402 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
403}
404
405XGL_RESULT XglImage::UnmapMemory()
406{
407 unmap();
408 return XGL_SUCCESS;
409}
410
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800411XglTextureObj::XglTextureObj(XglDevice *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700412{
413 m_device = device;
414 const XGL_FORMAT tex_format = { XGL_CH_FMT_B8G8R8A8, XGL_NUM_FMT_UNORM };
Tony Barboure2c58df2014-11-25 13:18:32 -0700415 const uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barboure2c58df2014-11-25 13:18:32 -0700416
417 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
418
419 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
420
421 const XGL_IMAGE_CREATE_INFO image = {
422 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
423 .pNext = NULL,
424 .imageType = XGL_IMAGE_2D,
425 .format = tex_format,
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800426 .extent = { 16, 16, 1 },
Tony Barboure2c58df2014-11-25 13:18:32 -0700427 .mipLevels = 1,
428 .arraySize = 1,
429 .samples = 1,
430 .tiling = XGL_LINEAR_TILING,
431 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
432 .flags = 0,
433 };
434
Tony Barboure2c58df2014-11-25 13:18:32 -0700435 XGL_IMAGE_VIEW_CREATE_INFO view;
436 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
437 view.pNext = NULL;
438 view.image = XGL_NULL_HANDLE;
439 view.viewType = XGL_IMAGE_VIEW_2D;
440 view.format = image.format;
441 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
442 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
443 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
444 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
445 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
446 view.subresourceRange.baseMipLevel = 0;
447 view.subresourceRange.mipLevels = 1;
448 view.subresourceRange.baseArraySlice = 0;
449 view.subresourceRange.arraySize = 1;
450 view.minLod = 0.0f;
451
Tony Barboure2c58df2014-11-25 13:18:32 -0700452 /* create image */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800453 init(*m_device, image);
Tony Barboure2c58df2014-11-25 13:18:32 -0700454
455 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800456 view.image = obj();
457 m_textureView.init(*m_device, view);
Tony Barboure2c58df2014-11-25 13:18:32 -0700458
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800459 XGL_SUBRESOURCE_LAYOUT layout =
460 subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
461 m_rowPitch = layout.rowPitch;
Tony Barboure2c58df2014-11-25 13:18:32 -0700462
Tony Barboure2c58df2014-11-25 13:18:32 -0700463 XGL_VOID *data;
464 XGL_INT x, y;
465
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800466 data = map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700467
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800468 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700469 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800470 for (x = 0; x < extent().width; x++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700471 row[x] = tex_colors[(x & 1) ^ (y & 1)];
472 }
473
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800474 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700475
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800476 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700477
478}
Tony Barbour82c39522014-12-04 14:33:33 -0700479
Tony Barboure2c58df2014-11-25 13:18:32 -0700480void XglTextureObj::ChangeColors(uint32_t color1, uint32_t color2)
481{
Tony Barboure2c58df2014-11-25 13:18:32 -0700482 const uint32_t tex_colors[2] = { color1, color2 };
483 XGL_VOID *data;
484
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800485 data = map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700486
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800487 for (int y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700488 uint32_t *row = (uint32_t *) ((char *) data + m_rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800489 for (int x = 0; x < extent().width; x++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700490 row[x] = tex_colors[(x & 1) ^ (y & 1)];
491 }
492
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800493 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700494}
495
496XglSamplerObj::XglSamplerObj(XglDevice *device)
497{
Tony Barboure2c58df2014-11-25 13:18:32 -0700498 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700499
Chia-I Wue9864b52014-12-28 16:32:24 +0800500 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
501 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
502 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
503 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
504 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
505 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
506 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
507 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
508 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
509 samplerCreateInfo.mipLodBias = 0.0;
510 samplerCreateInfo.maxAnisotropy = 0.0;
511 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
512 samplerCreateInfo.minLod = 0.0;
513 samplerCreateInfo.maxLod = 0.0;
514 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700515
Chia-I Wue9864b52014-12-28 16:32:24 +0800516 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700517}
Tony Barboure2c58df2014-11-25 13:18:32 -0700518
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700519/*
520 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
521 */
522XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
523{
524 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700525 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700526
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800527 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700528}
529
Tony Barboure2c58df2014-11-25 13:18:32 -0700530XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
531{
Tony Barboure2c58df2014-11-25 13:18:32 -0700532 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800533 m_commandBuffer = 0;
534
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800535 memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo));
Tony Barboure2c58df2014-11-25 13:18:32 -0700536 m_numVertices = constantCount;
537 m_stride = constantSize;
538
Chia-I Wua07fee62014-12-28 15:26:08 +0800539 const size_t allocationSize = constantCount * constantSize;
540 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700541
Chia-I Wua07fee62014-12-28 15:26:08 +0800542 void *pData = map();
543 memcpy(pData, data, allocationSize);
544 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700545
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800546 // set up the buffer view for the constant buffer
547 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
548 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
549 view_info.buffer = obj();
550 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
551 view_info.stride = 16;
552 view_info.format.channelFormat = XGL_CH_FMT_R32G32B32A32;
553 view_info.format.numericFormat = XGL_NUM_FMT_FLOAT;
554 view_info.channels.r = XGL_CHANNEL_SWIZZLE_R;
555 view_info.channels.g = XGL_CHANNEL_SWIZZLE_G;
556 view_info.channels.b = XGL_CHANNEL_SWIZZLE_B;
557 view_info.channels.a = XGL_CHANNEL_SWIZZLE_A;
558 view_info.offset = 0;
559 view_info.range = allocationSize;
560 m_bufferView.init(*m_device, view_info);
561
562 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
563 this->m_bufferViewInfo.view = m_bufferView.obj();
564 this->m_bufferViewInfo.state = XGL_BUFFER_STATE_DATA_TRANSFER;
Tony Barboure2c58df2014-11-25 13:18:32 -0700565}
Tony Barbour82c39522014-12-04 14:33:33 -0700566
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700567void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, XGL_UINT binding)
568{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800569 xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700570}
571
572
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800573void XglConstantBufferObj::SetBufferState(XGL_BUFFER_STATE newState)
Tony Barboure2c58df2014-11-25 13:18:32 -0700574{
Tony Barbour38422802014-12-10 14:36:31 -0700575 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700576
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800577 if (this->m_bufferViewInfo.state == newState)
Tony Barboure2c58df2014-11-25 13:18:32 -0700578 return;
579
Tony Barbour38422802014-12-10 14:36:31 -0700580 if (!m_commandBuffer)
581 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800582 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700583
584 m_commandBuffer = new XglCommandBufferObj(m_device);
585
586 }
587 else
588 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800589 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700590 }
591
Tony Barboure2c58df2014-11-25 13:18:32 -0700592 // open the command buffer
Jon Ashburnc4164b12014-12-31 17:10:47 -0700593 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
594 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
595 .pNext = NULL,
596 .flags = 0,
597 };
598 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700599 ASSERT_XGL_SUCCESS(err);
600
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800601 XGL_BUFFER_STATE_TRANSITION transition =
602 state_transition(XGL_BUFFER_STATE_DATA_TRANSFER, newState, 0, m_numVertices * m_stride);
Tony Barboure2c58df2014-11-25 13:18:32 -0700603
604 // write transition to the command buffer
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800605 m_commandBuffer->PrepareBufferRegions(1, &transition);
606 this->m_bufferViewInfo.state = newState;
Tony Barboure2c58df2014-11-25 13:18:32 -0700607
608 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700609 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700610 ASSERT_XGL_SUCCESS(err);
611
612 XGL_UINT32 numMemRefs=1;
613 XGL_MEMORY_REF memRefs;
614 // this command buffer only uses the vertex buffer memory
615 memRefs.flags = 0;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800616 memRefs.mem = memories()[0];
Tony Barboure2c58df2014-11-25 13:18:32 -0700617
618 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700619 XGL_CMD_BUFFER bufferArray[1];
620 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800621 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700622 ASSERT_XGL_SUCCESS(err);
623}
624
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700625XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
626 : XglConstantBufferObj(device)
627{
628
629}
630
631void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
632{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700633 XGL_FORMAT viewFormat;
634
635 m_numVertices = numIndexes;
636 m_indexType = indexType;
637 viewFormat.numericFormat = XGL_NUM_FMT_UINT;
638 switch (indexType) {
639 case XGL_INDEX_8:
640 m_stride = 1;
641 viewFormat.channelFormat = XGL_CH_FMT_R8;
642 break;
643 case XGL_INDEX_16:
644 m_stride = 2;
645 viewFormat.channelFormat = XGL_CH_FMT_R16;
646 break;
647 case XGL_INDEX_32:
648 m_stride = 4;
649 viewFormat.channelFormat = XGL_CH_FMT_R32;
650 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800651 default:
652 assert(!"unknown index type");
653 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700654 }
655
Chia-I Wua07fee62014-12-28 15:26:08 +0800656 const size_t allocationSize = numIndexes * m_stride;
657 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700658
Chia-I Wua07fee62014-12-28 15:26:08 +0800659 void *pData = map();
660 memcpy(pData, data, allocationSize);
661 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700662
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800663 // set up the buffer view for the constant buffer
664 XGL_BUFFER_VIEW_CREATE_INFO view_info = {};
665 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
666 view_info.buffer = obj();
667 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
668 view_info.stride = m_stride;
669 view_info.format.channelFormat = viewFormat.channelFormat;
670 view_info.format.numericFormat = viewFormat.numericFormat;
671 view_info.channels.r = XGL_CHANNEL_SWIZZLE_R;
672 view_info.channels.g = XGL_CHANNEL_SWIZZLE_G;
673 view_info.channels.b = XGL_CHANNEL_SWIZZLE_B;
674 view_info.channels.a = XGL_CHANNEL_SWIZZLE_A;
675 view_info.offset = 0;
676 view_info.range = allocationSize;
677 m_bufferView.init(*m_device, view_info);
678
679 this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
680 this->m_bufferViewInfo.view = m_bufferView.obj();
681 this->m_bufferViewInfo.state = XGL_BUFFER_STATE_DATA_TRANSFER;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700682}
683
684void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
685{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800686 xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700687}
Tony Barboure2c58df2014-11-25 13:18:32 -0700688
Tony Barbouraf1f9192014-12-17 10:57:58 -0700689XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
690{
691 return m_indexType;
692}
693
Tony Barbour5420af02014-12-03 13:58:15 -0700694XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo(XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700695{
696 XGL_DESCRIPTOR_SLOT_INFO *slotInfo;
697 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
698 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
699 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800700 stageInfo->shader.shader = obj();
Tony Barbourf52346d2015-01-16 14:27:35 -0700701 stageInfo->shader.descriptorSetMappingCount = 1;
702 stageInfo->shader.pDescriptorSetMapping = (XGL_DESCRIPTOR_SET_MAPPING *)malloc(sizeof(XGL_DESCRIPTOR_SET_MAPPING));
703 stageInfo->shader.pDescriptorSetMapping->descriptorCount = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700704 stageInfo->shader.linkConstBufferCount = 0;
705 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800706 stageInfo->shader.dynamicBufferViewMapping.slotObjectType = XGL_SLOT_UNUSED;
707 stageInfo->shader.dynamicBufferViewMapping.shaderEntityIndex = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700708
Tony Barbourf52346d2015-01-16 14:27:35 -0700709 stageInfo->shader.pDescriptorSetMapping->descriptorCount = descriptorSet->GetTotalSlots();
710 if (stageInfo->shader.pDescriptorSetMapping->descriptorCount)
Tony Barboure2c58df2014-11-25 13:18:32 -0700711 {
712 vector<int> allSlots;
713 vector<XGL_DESCRIPTOR_SET_SLOT_TYPE> allTypes;
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800714 vector<void *> allObjs;
Tony Barboure2c58df2014-11-25 13:18:32 -0700715
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800716 allSlots.reserve(m_bufferSlots.size() + m_imageSlots.size() + m_samplerSlots.size());
717 allTypes.reserve(m_bufferTypes.size() + m_imageTypes.size() + m_samplerTypes.size());
718 allObjs.reserve(m_bufferObjs.size() + m_imageObjs.size() + m_samplerObjs.size());
Tony Barboure2c58df2014-11-25 13:18:32 -0700719
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800720 if (m_bufferSlots.size())
Tony Barboure2c58df2014-11-25 13:18:32 -0700721 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800722 allSlots.insert(allSlots.end(), m_bufferSlots.begin(), m_bufferSlots.end());
723 allTypes.insert(allTypes.end(), m_bufferTypes.begin(), m_bufferTypes.end());
724 allObjs.insert(allObjs.end(), m_bufferObjs.begin(), m_bufferObjs.end());
Tony Barboure2c58df2014-11-25 13:18:32 -0700725 }
726 if (m_imageSlots.size())
727 {
728 allSlots.insert(allSlots.end(), m_imageSlots.begin(), m_imageSlots.end());
729 allTypes.insert(allTypes.end(), m_imageTypes.begin(), m_imageTypes.end());
730 allObjs.insert(allObjs.end(), m_imageObjs.begin(), m_imageObjs.end());
731 }
732 if (m_samplerSlots.size())
733 {
734 allSlots.insert(allSlots.end(), m_samplerSlots.begin(), m_samplerSlots.end());
735 allTypes.insert(allTypes.end(), m_samplerTypes.begin(), m_samplerTypes.end());
736 allObjs.insert(allObjs.end(), m_samplerObjs.begin(), m_samplerObjs.end());
737 }
738
Tony Barbour5420af02014-12-03 13:58:15 -0700739 slotInfo = descriptorSet->GetSlotInfo(allSlots, allTypes, allObjs);
Tony Barbourf52346d2015-01-16 14:27:35 -0700740 stageInfo->shader.pDescriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700741 }
742 return stageInfo;
743}
744
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800745void XglShaderObj::BindShaderEntitySlotToBuffer(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglConstantBufferObj *constantBuffer)
Tony Barboure2c58df2014-11-25 13:18:32 -0700746{
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800747 m_bufferSlots.push_back(slot);
748 m_bufferTypes.push_back(type);
749 m_bufferObjs.push_back(&constantBuffer->m_bufferViewInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700750
751}
Tony Barbour82c39522014-12-04 14:33:33 -0700752
Tony Barboure2c58df2014-11-25 13:18:32 -0700753void XglShaderObj::BindShaderEntitySlotToImage(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglTextureObj *texture)
754{
755 m_imageSlots.push_back(slot);
756 m_imageTypes.push_back(type);
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800757 m_imageObjs.push_back(&texture->m_textureViewInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700758
759}
Tony Barbour82c39522014-12-04 14:33:33 -0700760
Tony Barboure2c58df2014-11-25 13:18:32 -0700761void XglShaderObj::BindShaderEntitySlotToSampler(int slot, XglSamplerObj *sampler)
762{
763 m_samplerSlots.push_back(slot);
764 m_samplerTypes.push_back(XGL_SLOT_SHADER_SAMPLER);
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800765 m_samplerObjs.push_back(sampler);
Tony Barboure2c58df2014-11-25 13:18:32 -0700766
767}
Tony Barbour82c39522014-12-04 14:33:33 -0700768
Tony Barboure2c58df2014-11-25 13:18:32 -0700769XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
770{
771 XGL_RESULT err = XGL_SUCCESS;
772 std::vector<unsigned int> bil;
773 XGL_SHADER_CREATE_INFO createInfo;
774 size_t shader_len;
775
776 m_stage = stage;
777 m_device = device;
778
779 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
780 createInfo.pNext = NULL;
781
782 if (!framework->m_use_bil) {
783
784 shader_len = strlen(shader_code);
785 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
786 createInfo.pCode = malloc(createInfo.codeSize);
787 createInfo.flags = 0;
788
789 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
790 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
791 ((uint32_t *) createInfo.pCode)[1] = 0;
792 ((uint32_t *) createInfo.pCode)[2] = stage;
793 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
794
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800795 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700796 }
797
798 if (framework->m_use_bil || err) {
799 std::vector<unsigned int> bil;
800 err = XGL_SUCCESS;
801
802 // Use Reference GLSL to BIL compiler
803 framework->GLSLtoBIL(stage, shader_code, bil);
804 createInfo.pCode = bil.data();
805 createInfo.codeSize = bil.size() * sizeof(unsigned int);
806 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700807
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800808 init(*m_device, createInfo);
809 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700810}
Tony Barbour82c39522014-12-04 14:33:33 -0700811
Tony Barboure2c58df2014-11-25 13:18:32 -0700812XglPipelineObj::XglPipelineObj(XglDevice *device)
813{
Tony Barboure2c58df2014-11-25 13:18:32 -0700814 m_device = device;
815 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
816 m_vertexBufferCount = 0;
817
818 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
819 m_ia_state.pNext = XGL_NULL_HANDLE;
820 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
821 m_ia_state.disableVertexReuse = XGL_FALSE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700822 m_ia_state.primitiveRestartEnable = XGL_FALSE;
823 m_ia_state.primitiveRestartIndex = 0;
824
825 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
826 m_rs_state.pNext = &m_ia_state;
827 m_rs_state.depthClipEnable = XGL_FALSE;
828 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
Tony Barbourf52346d2015-01-16 14:27:35 -0700829 m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
830 m_rs_state.fillMode = XGL_FILL_SOLID;
831 m_rs_state.cullMode = XGL_CULL_NONE;
832 m_rs_state.frontFace = XGL_FRONT_FACE_CCW;
Tony Barboure2c58df2014-11-25 13:18:32 -0700833
Tony Barboure2c58df2014-11-25 13:18:32 -0700834 memset(&m_cb_state,0,sizeof(m_cb_state));
835 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
836 m_cb_state.pNext = &m_rs_state;
837 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
838 m_cb_state.dualSourceBlendEnable = XGL_FALSE;
839 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
840
Tony Barbourf52346d2015-01-16 14:27:35 -0700841 m_ms_state.pNext = &m_cb_state;
842 m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
843 m_ms_state.multisampleEnable = XGL_FALSE;
844 m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it?
845 m_ms_state.samples = 1;
846 m_ms_state.minSampleShading = 0;
847 m_ms_state.sampleShadingEnable = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700848
Tony Barbourf52346d2015-01-16 14:27:35 -0700849 m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
850 m_ds_state.pNext = &m_ms_state,
851 m_ds_state.format.channelFormat = XGL_CH_FMT_R32;
852 m_ds_state.format.numericFormat = XGL_NUM_FMT_DS;
853 m_ds_state.depthTestEnable = XGL_FALSE;
854 m_ds_state.depthWriteEnable = XGL_FALSE;
855 m_ds_state.depthBoundsEnable = XGL_FALSE;
856 m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
857 m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
858 m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
859 m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
860 m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
861 m_ds_state.stencilTestEnable = XGL_FALSE;
862 m_ds_state.front = m_ds_state.back;
Tony Barboure2c58df2014-11-25 13:18:32 -0700863
Tony Barbourf52346d2015-01-16 14:27:35 -0700864 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
865 att.blendEnable = XGL_FALSE;
866 att.format.channelFormat = XGL_CH_FMT_R8G8B8A8;
867 att.format.numericFormat = XGL_NUM_FMT_UNORM;
868 att.channelWriteMask = 0xf;
869 AddColorAttachment(0, &att);
Tony Barboure2c58df2014-11-25 13:18:32 -0700870
871};
872
873void XglPipelineObj::AddShader(XglShaderObj* shader)
874{
875 m_shaderObjs.push_back(shader);
876}
877
878void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
879{
880 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
881 m_vi_state.attributeCount = count;
882}
883
884void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
885{
886 m_vi_state.pVertexBindingDescriptions = vi_binding;
887 m_vi_state.bindingCount = count;
888}
889
890void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
891{
892 m_vertexBufferObjs.push_back(vertexDataBuffer);
893 m_vertexBufferBindings.push_back(binding);
894 m_vertexBufferCount++;
895}
896
Tony Barbourf52346d2015-01-16 14:27:35 -0700897void XglPipelineObj::AddColorAttachment(XGL_UINT binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
Chia-I Wuecebf752014-12-05 10:45:15 +0800898{
Tony Barbourf52346d2015-01-16 14:27:35 -0700899 if (binding+1 > m_colorAttachments.size())
900 {
901 m_colorAttachments.resize(binding+1);
902 }
903 m_colorAttachments[binding] = *att;
904}
905
906void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state)
907{
908 m_ds_state.format = ds_state->format;
909 m_ds_state.depthTestEnable = ds_state->depthTestEnable;
910 m_ds_state.depthWriteEnable = ds_state->depthWriteEnable;
911 m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable;
912 m_ds_state.depthFunc = ds_state->depthFunc;
913 m_ds_state.stencilTestEnable = ds_state->stencilTestEnable;
914 m_ds_state.back = ds_state->back;
915 m_ds_state.front = ds_state->front;
Chia-I Wuecebf752014-12-05 10:45:15 +0800916}
917
Tony Barbour976e1cf2014-12-17 11:57:31 -0700918void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
919{
Tony Barbourf52346d2015-01-16 14:27:35 -0700920 XGL_VOID* head_ptr = &m_ds_state;
Tony Barbour976e1cf2014-12-17 11:57:31 -0700921 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
922
923 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
924
925 for (int i=0; i<m_shaderObjs.size(); i++)
926 {
927 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet);
928 shaderCreateInfo->pNext = head_ptr;
929 head_ptr = shaderCreateInfo;
930 }
931
932 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
933 {
934 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
935 m_vi_state.pNext = head_ptr;
936 head_ptr = &m_vi_state;
937 }
938
939 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
940 info.pNext = head_ptr;
941 info.flags = 0;
942
Tony Barbourf52346d2015-01-16 14:27:35 -0700943 m_cb_state.attachmentCount = m_colorAttachments.size();
944 m_cb_state.pAttachments = &m_colorAttachments[0];
945
Chia-I Wu2648d092014-12-29 14:24:14 +0800946 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -0700947}
Chia-I Wu2648d092014-12-29 14:24:14 +0800948
Tony Barbour976e1cf2014-12-17 11:57:31 -0700949XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
950{
Chia-I Wu2648d092014-12-29 14:24:14 +0800951 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700952}
953
Tony Barbour5420af02014-12-03 13:58:15 -0700954void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700955{
Tony Barbourf52346d2015-01-16 14:27:35 -0700956 XGL_VOID* head_ptr = &m_ds_state;
Tony Barboure2c58df2014-11-25 13:18:32 -0700957 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
958
959 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700960
961 for (int i=0; i<m_shaderObjs.size(); i++)
962 {
963 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet);
964 shaderCreateInfo->pNext = head_ptr;
965 head_ptr = shaderCreateInfo;
966 }
967
968 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
969 {
970 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
971 m_vi_state.pNext = head_ptr;
972 head_ptr = &m_vi_state;
973 }
974
975 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
976 info.pNext = head_ptr;
977 info.flags = 0;
978
Chia-I Wu2648d092014-12-29 14:24:14 +0800979 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700980
Chia-I Wu2648d092014-12-29 14:24:14 +0800981 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700982
983
984 for (int i=0; i < m_vertexBufferCount; i++)
985 {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800986 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700987 }
Tony Barboure2c58df2014-11-25 13:18:32 -0700988}
Tony Barbour82c39522014-12-04 14:33:33 -0700989
Tony Barboure2c58df2014-11-25 13:18:32 -0700990XglMemoryRefManager::XglMemoryRefManager() {
991
992}
Tony Barbour82c39522014-12-04 14:33:33 -0700993
Tony Barboure2c58df2014-11-25 13:18:32 -0700994void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800995 const std::vector<XGL_GPU_MEMORY> mems = constantBuffer->memories();
996 if (!mems.empty())
997 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700998}
Tony Barbour82c39522014-12-04 14:33:33 -0700999
Tony Barboure2c58df2014-11-25 13:18:32 -07001000void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
Chia-I Wu13a3aa82014-12-28 15:55:09 +08001001 const std::vector<XGL_GPU_MEMORY> mems = texture->memories();
1002 if (!mems.empty())
1003 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001004}
Tony Barbour82c39522014-12-04 14:33:33 -07001005
Tony Barboure2c58df2014-11-25 13:18:32 -07001006XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
1007
1008 XGL_MEMORY_REF *localRefs;
1009 XGL_UINT32 numRefs=m_bufferObjs.size();
1010
1011 if (numRefs <= 0)
1012 return NULL;
1013
1014 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
1015 for (int i=0; i<numRefs; i++)
1016 {
1017 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +08001018 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -07001019 }
1020 return localRefs;
1021}
1022int XglMemoryRefManager::GetNumRefs() {
1023 return m_bufferObjs.size();
1024}
Tony Barbour6d047bf2014-12-10 14:34:45 -07001025
1026XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Chia-I Wud28343c2014-12-28 15:12:48 +08001027 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(XGL_QUEUE_TYPE_GRAPHICS))
Tony Barbour6d047bf2014-12-10 14:34:45 -07001028{
Tony Barbour6d047bf2014-12-10 14:34:45 -07001029 m_device = device;
Chia-I Wud28343c2014-12-28 15:12:48 +08001030 m_renderTargetCount = 0;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001031}
Tony Barbour471338d2014-12-10 17:28:39 -07001032
Tony Barbour6d047bf2014-12-10 14:34:45 -07001033XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1034{
Chia-I Wud28343c2014-12-28 15:12:48 +08001035 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -07001036}
Tony Barbour471338d2014-12-10 17:28:39 -07001037
Jon Ashburnc4164b12014-12-31 17:10:47 -07001038XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -07001039{
Jeremy Hayesd65ae082015-01-14 16:17:08 -07001040 begin(pInfo);
1041 return XGL_SUCCESS;
1042}
1043
1044XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj)
1045{
1046 begin(renderpass_obj);
1047 return XGL_SUCCESS;
1048}
1049
1050XGL_RESULT XglCommandBufferObj::BeginCommandBuffer()
1051{
1052 begin();
Chia-I Wud28343c2014-12-28 15:12:48 +08001053 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001054}
1055
1056XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1057{
Chia-I Wud28343c2014-12-28 15:12:48 +08001058 end();
1059 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -07001060}
1061
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001062void XglCommandBufferObj::PrepareBufferRegions(int transitionCount, XGL_BUFFER_STATE_TRANSITION *transitionPtr)
Tony Barbour471338d2014-12-10 17:28:39 -07001063{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001064 xglCmdPrepareBufferRegions(obj(), transitionCount, transitionPtr);
Tony Barbour471338d2014-12-10 17:28:39 -07001065}
1066
Tony Barbour30cc9e82014-12-17 11:53:55 -07001067void XglCommandBufferObj::ClearAllBuffers(XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
1068{
1069 XGL_UINT i;
1070
1071 // whatever we want to do, we do it to the whole buffer
1072 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1073 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1074 srRange.baseMipLevel = 0;
1075 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1076 srRange.baseArraySlice = 0;
1077 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1078
1079 // clear the back buffer to dark grey
1080 XGL_UINT clearColor[4] = {64, 64, 64, 0};
1081 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
1082 for (i = 0; i < m_renderTargetCount; i++) {
1083 transitionToClear.image = m_renderTargets[i]->image();
1084 transitionToClear.oldState = m_renderTargets[i]->state();
1085 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
1086 transitionToClear.subresourceRange = srRange;
Chia-I Wud28343c2014-12-28 15:12:48 +08001087 xglCmdPrepareImages( obj(), 1, &transitionToClear );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001088 m_renderTargets[i]->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
1089
Chia-I Wud28343c2014-12-28 15:12:48 +08001090 xglCmdClearColorImageRaw( obj(), m_renderTargets[i]->image(), clearColor, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001091 }
1092
1093 if (depthStencilImage)
1094 {
1095 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1096 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1097 dsRange.baseMipLevel = 0;
1098 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1099 dsRange.baseArraySlice = 0;
1100 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1101
1102 // prepare the depth buffer for clear
1103 memset(&transitionToClear,0,sizeof(transitionToClear));
1104 transitionToClear.image = depthStencilImage;
1105 transitionToClear.oldState = depthStencilBinding->depthState;
1106 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
1107 transitionToClear.subresourceRange = dsRange;
Chia-I Wud28343c2014-12-28 15:12:48 +08001108 xglCmdPrepareImages( obj(), 1, &transitionToClear );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001109 depthStencilBinding->depthState = transitionToClear.newState;
1110
Chia-I Wud28343c2014-12-28 15:12:48 +08001111 xglCmdClearDepthStencil(obj(), depthStencilImage, 1.0f, 0, 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001112
1113 // prepare depth buffer for rendering
1114 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
1115 transitionToRender.image = depthStencilImage;
1116 transitionToRender.oldState = XGL_IMAGE_STATE_CLEAR;
1117 transitionToRender.newState = depthStencilBinding->depthState;
1118 transitionToRender.subresourceRange = dsRange;
Chia-I Wud28343c2014-12-28 15:12:48 +08001119 xglCmdPrepareImages( obj(), 1, &transitionToRender );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001120 depthStencilBinding->depthState = transitionToClear.newState;
1121 }
1122}
1123
Jon Ashburncdc40be2015-01-02 18:27:14 -07001124void XglCommandBufferObj::PrepareAttachments()
Tony Barbour30cc9e82014-12-17 11:53:55 -07001125{
1126 XGL_UINT i;
Tony Barbour30cc9e82014-12-17 11:53:55 -07001127 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1128 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1129 srRange.baseMipLevel = 0;
1130 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1131 srRange.baseArraySlice = 0;
1132 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1133
1134 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
1135 for(i=0; i<m_renderTargetCount; i++)
1136 {
1137 transitionToRender.image = m_renderTargets[i]->image();
1138 transitionToRender.oldState = m_renderTargets[i]->state();
1139 transitionToRender.newState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
1140 transitionToRender.subresourceRange = srRange;
Chia-I Wud28343c2014-12-28 15:12:48 +08001141 xglCmdPrepareImages(obj(), 1, &transitionToRender );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001142 m_renderTargets[i]->state(( XGL_IMAGE_STATE ) transitionToRender.newState);
1143 }
Tony Barbour30cc9e82014-12-17 11:53:55 -07001144}
1145
Tony Barbourf52346d2015-01-16 14:27:35 -07001146void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001147{
Tony Barbourf52346d2015-01-16 14:27:35 -07001148 xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001149}
1150
1151void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1152{
1153 m_renderTargets.push_back(renderTarget);
1154 m_renderTargetCount++;
1155}
1156
1157void XglCommandBufferObj::DrawIndexed(XGL_UINT firstIndex, XGL_UINT indexCount, XGL_INT vertexOffset, XGL_UINT firstInstance, XGL_UINT instanceCount)
1158{
Chia-I Wud28343c2014-12-28 15:12:48 +08001159 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001160}
1161
1162void XglCommandBufferObj::Draw(XGL_UINT firstVertex, XGL_UINT vertexCount, XGL_UINT firstInstance, XGL_UINT instanceCount)
1163{
Chia-I Wud28343c2014-12-28 15:12:48 +08001164 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001165}
1166
1167void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs)
1168{
1169 XGL_RESULT err = XGL_SUCCESS;
1170
1171 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001172 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001173 ASSERT_XGL_SUCCESS( err );
1174
1175 err = xglQueueWaitIdle( m_device->m_queue );
1176 ASSERT_XGL_SUCCESS( err );
1177
1178 // Wait for work to finish before cleaning up.
1179 xglDeviceWaitIdle(m_device->device());
1180
1181}
1182void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1183{
Chia-I Wud28343c2014-12-28 15:12:48 +08001184 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001185}
1186
1187void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1188{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001189 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view)
Chia-I Wud28343c2014-12-28 15:12:48 +08001190 xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, descriptorSet, 0 );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001191}
1192void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, XGL_UINT offset)
1193{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001194 xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001195}
1196void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, XGL_UINT offset, XGL_UINT binding)
1197{
Chia-I Wu1a28fe02015-01-01 07:55:04 +08001198 xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001199}