blob: a24f3ca5a71a606c446c60c4fa652ee7c0b5ea79 [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 ),
36 m_stateMsaa( XGL_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060037 m_width( 256.0 ), // default window width
38 m_height( 256.0 ) // default window height
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060039{
Chia-I Wuecebf752014-12-05 10:45:15 +080040 m_renderTargetCount = 1;
41
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060042 m_render_target_fmt.channelFormat = XGL_CH_FMT_R8G8B8A8;
43 m_render_target_fmt.numericFormat = XGL_NUM_FMT_UNORM;
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060044
Chia-I Wuecebf752014-12-05 10:45:15 +080045 m_colorBindings[0].view = XGL_NULL_HANDLE;
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060046 m_depthStencilBinding.view = XGL_NULL_HANDLE;
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060047}
48
49XglRenderFramework::~XglRenderFramework()
50{
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060051
52}
53
54void XglRenderFramework::InitFramework()
55{
56 XGL_RESULT err;
57
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060058 err = xglInitAndEnumerateGpus(&app_info, NULL,
Chia-I Wuaf11d922014-12-28 14:37:25 +080059 XGL_MAX_PHYSICAL_GPUS, &this->gpu_count, objs);
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060060 ASSERT_XGL_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -070061 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060062
63 m_device = new XglDevice(0, objs[0]);
64 m_device->get_device_queue();
65}
66
67void XglRenderFramework::ShutdownFramework()
68{
69 if (m_colorBlend) xglDestroyObject(m_colorBlend);
70 if (m_stateMsaa) xglDestroyObject(m_stateMsaa);
71 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
72 if (m_stateRaster) xglDestroyObject(m_stateRaster);
73 if (m_cmdBuffer) xglDestroyObject(m_cmdBuffer);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060074
75 if (m_stateViewport) {
76 xglDestroyObject(m_stateViewport);
77 }
78
Chia-I Wuecebf752014-12-05 10:45:15 +080079 for (XGL_UINT i = 0; i < m_renderTargetCount; i++) {
80 if (m_renderTargets[i]) {
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060081 // TODO: XglImage should be able to destroy itself
82// m_renderTarget->
83// xglDestroyObject(*m_renderTarget);
Chia-I Wuecebf752014-12-05 10:45:15 +080084 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060085 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060086
87 // reset the driver
Chia-I Wub76e0fa2014-12-28 14:27:28 +080088 delete m_device;
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060089 xglInitAndEnumerateGpus(&this->app_info, XGL_NULL_HANDLE, 0, &gpu_count, XGL_NULL_HANDLE);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060090}
91
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060092void XglRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060093{
94 XGL_RESULT err;
95
96 m_render_target_fmt.channelFormat = XGL_CH_FMT_R8G8B8A8;
97 m_render_target_fmt.numericFormat = XGL_NUM_FMT_UNORM;
98
99 // create a raster state (solid, back-face culling)
100 XGL_RASTER_STATE_CREATE_INFO raster = {};
101 raster.sType = XGL_STRUCTURE_TYPE_RASTER_STATE_CREATE_INFO;
102 raster.fillMode = XGL_FILL_SOLID;
103 raster.cullMode = XGL_CULL_NONE;
104 raster.frontFace = XGL_FRONT_FACE_CCW;
105 err = xglCreateRasterState( device(), &raster, &m_stateRaster );
106 ASSERT_XGL_SUCCESS(err);
107
108 XGL_COLOR_BLEND_STATE_CREATE_INFO blend = {};
109 blend.sType = XGL_STRUCTURE_TYPE_COLOR_BLEND_STATE_CREATE_INFO;
110 err = xglCreateColorBlendState(device(), &blend, &m_colorBlend);
111 ASSERT_XGL_SUCCESS( err );
112
113 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {};
114 depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
115 depthStencil.depthTestEnable = XGL_FALSE;
116 depthStencil.depthWriteEnable = XGL_FALSE;
117 depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
118 depthStencil.depthBoundsEnable = XGL_FALSE;
119 depthStencil.minDepth = 0.f;
120 depthStencil.maxDepth = 1.f;
121 depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
122 depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
123 depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
124 depthStencil.back.stencilRef = 0x00;
125 depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS;
126 depthStencil.front = depthStencil.back;
127
128 err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
129 ASSERT_XGL_SUCCESS( err );
130
131 XGL_MSAA_STATE_CREATE_INFO msaa = {};
132 msaa.sType = XGL_STRUCTURE_TYPE_MSAA_STATE_CREATE_INFO;
133 msaa.sampleMask = 1;
134 msaa.samples = 1;
135
136 err = xglCreateMsaaState( device(), &msaa, &m_stateMsaa );
137 ASSERT_XGL_SUCCESS( err );
138
139 XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {};
140
141 cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
142 cmdInfo.queueType = XGL_QUEUE_TYPE_GRAPHICS;
143 err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
144 ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed";
145}
146
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600147void XglRenderFramework::InitViewport(float width, float height)
148{
149 XGL_RESULT err;
150
151 XGL_VIEWPORT_STATE_CREATE_INFO viewport = {};
152 viewport.viewportCount = 1;
153 viewport.scissorEnable = XGL_FALSE;
154 viewport.viewports[0].originX = 0;
155 viewport.viewports[0].originY = 0;
156 viewport.viewports[0].width = 1.f * width;
157 viewport.viewports[0].height = 1.f * height;
158 viewport.viewports[0].minDepth = 0.f;
159 viewport.viewports[0].maxDepth = 1.f;
160
161 err = xglCreateViewportState( device(), &viewport, &m_stateViewport );
162 ASSERT_XGL_SUCCESS( err );
163 m_width = width;
164 m_height = height;
165}
166
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600167void XglRenderFramework::InitViewport()
168{
169 InitViewport(m_width, m_height);
170}
171
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600172void XglRenderFramework::InitRenderTarget()
173{
Chia-I Wuecebf752014-12-05 10:45:15 +0800174 XGL_UINT i;
175
176 for (i = 0; i < m_renderTargetCount; i++) {
Chia-I Wuf50ee212014-12-29 14:31:52 +0800177 XglImage *img = new XglImage(m_device);
178 img->init(m_width, m_height, m_render_target_fmt,
179 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
180 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
181 m_renderTargets[i] = img;
Chia-I Wuecebf752014-12-05 10:45:15 +0800182 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600183}
184
Chia-I Wufb1459b2014-12-29 15:23:20 +0800185XglDevice::XglDevice(XGL_UINT id, XGL_PHYSICAL_GPU obj) :
186 xgl_testing::Device(obj), id(id)
187{
188 init();
189
190 props = gpu().properties();
191 queue_props = &gpu().queue_properties()[0];
192}
193
194void XglDevice::get_device_queue()
195{
196 ASSERT_NE(true, graphics_queues().empty());
197 m_queue = graphics_queues()[0]->obj();
198}
199
Tony Barboure2c58df2014-11-25 13:18:32 -0700200XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
201{
202 m_device = device;
203 m_nextSlot = 0;
204
205}
206
207void XglDescriptorSetObj::AttachMemoryView(XglConstantBufferObj *constantBuffer)
208{
209 m_memoryViews.push_back(&constantBuffer->m_constantBufferView);
210 m_memorySlots.push_back(m_nextSlot);
211 m_nextSlot++;
212
213}
Tony Barbour82c39522014-12-04 14:33:33 -0700214
Tony Barboure2c58df2014-11-25 13:18:32 -0700215void XglDescriptorSetObj::AttachSampler(XglSamplerObj *sampler)
216{
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800217 m_samplers.push_back(sampler);
Tony Barboure2c58df2014-11-25 13:18:32 -0700218 m_samplerSlots.push_back(m_nextSlot);
219 m_nextSlot++;
220
221}
Tony Barbour82c39522014-12-04 14:33:33 -0700222
Tony Barboure2c58df2014-11-25 13:18:32 -0700223void XglDescriptorSetObj::AttachImageView(XglTextureObj *texture)
224{
225 m_imageViews.push_back(&texture->m_textureViewInfo);
226 m_imageSlots.push_back(m_nextSlot);
227 m_nextSlot++;
228
229}
Tony Barbour82c39522014-12-04 14:33:33 -0700230
Tony Barboure2c58df2014-11-25 13:18:32 -0700231XGL_DESCRIPTOR_SLOT_INFO* XglDescriptorSetObj::GetSlotInfo(vector<int>slots,
232 vector<XGL_DESCRIPTOR_SET_SLOT_TYPE>types,
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800233 vector<void *>objs )
Tony Barboure2c58df2014-11-25 13:18:32 -0700234{
235 int nSlots = m_memorySlots.size() + m_imageSlots.size() + m_samplerSlots.size();
236 m_slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( nSlots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
237 memset(m_slotInfo,0,nSlots*sizeof(XGL_DESCRIPTOR_SLOT_INFO));
238
239 for (int i=0; i<nSlots; i++)
240 {
241 m_slotInfo[i].slotObjectType = XGL_SLOT_UNUSED;
242 }
243
244 for (int i=0; i<slots.size(); i++)
245 {
246 for (int j=0; j<m_memorySlots.size(); j++)
247 {
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800248 if ( m_memoryViews[j] == objs[i])
Tony Barboure2c58df2014-11-25 13:18:32 -0700249 {
250 m_slotInfo[m_memorySlots[j]].shaderEntityIndex = slots[i];
251 m_slotInfo[m_memorySlots[j]].slotObjectType = types[i];
252 }
253 }
254 for (int j=0; j<m_imageSlots.size(); j++)
255 {
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800256 if ( m_imageViews[j] == objs[i])
Tony Barboure2c58df2014-11-25 13:18:32 -0700257 {
258 m_slotInfo[m_imageSlots[j]].shaderEntityIndex = slots[i];
259 m_slotInfo[m_imageSlots[j]].slotObjectType = types[i];
260 }
261 }
262 for (int j=0; j<m_samplerSlots.size(); j++)
263 {
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800264 if ( m_samplers[j] == objs[i])
Tony Barboure2c58df2014-11-25 13:18:32 -0700265 {
266 m_slotInfo[m_samplerSlots[j]].shaderEntityIndex = slots[i];
267 m_slotInfo[m_samplerSlots[j]].slotObjectType = types[i];
268 }
269 }
270 }
271
272 // for (int i=0;i<nSlots;i++)
273 // {
274 // printf("SlotInfo[%d]: Index = %d, Type = %d\n",i,m_slotInfo[i].shaderEntityIndex, m_slotInfo[i].slotObjectType);
275 // fflush(stdout);
276 // }
277
278 return(m_slotInfo);
279
280}
Tony Barbourb5f4d082014-12-17 10:54:03 -0700281void XglDescriptorSetObj::CreateXGLDescriptorSet()
282{
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800283 init(*m_device, xgl_testing::DescriptorSet::create_info(m_nextSlot));
Tony Barbourb5f4d082014-12-17 10:54:03 -0700284
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800285 begin();
286 clear();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700287
Tony Barbourb5f4d082014-12-17 10:54:03 -0700288 for (int i=0; i<m_memoryViews.size();i++)
289 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800290 attach(m_memorySlots[i], *m_memoryViews[i]);
Tony Barbourb5f4d082014-12-17 10:54:03 -0700291 }
292 for (int i=0; i<m_samplers.size();i++)
293 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800294 attach(m_samplerSlots[i], *m_samplers[i]);
Tony Barbourb5f4d082014-12-17 10:54:03 -0700295 }
296 for (int i=0; i<m_imageViews.size();i++)
297 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800298 attach(m_imageSlots[i], *m_imageViews[i]);
Tony Barbourb5f4d082014-12-17 10:54:03 -0700299 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800300
301 end();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700302}
303
304XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle()
305{
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800306 return obj();
Tony Barbourb5f4d082014-12-17 10:54:03 -0700307}
Tony Barboure2c58df2014-11-25 13:18:32 -0700308
Tony Barbour824b7712014-12-18 17:06:21 -0700309int XglDescriptorSetObj::GetTotalSlots()
310{
311 return m_nextSlot;
312}
313
Tony Barboure2c58df2014-11-25 13:18:32 -0700314void XglDescriptorSetObj::BindCommandBuffer(XGL_CMD_BUFFER commandBuffer)
315{
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800316 init(*m_device, xgl_testing::DescriptorSet::create_info(m_nextSlot));
Tony Barboure2c58df2014-11-25 13:18:32 -0700317
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800318 begin();
319 clear();
Tony Barboure2c58df2014-11-25 13:18:32 -0700320
Tony Barboure2c58df2014-11-25 13:18:32 -0700321 for (int i=0; i<m_memoryViews.size();i++)
322 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800323 attach(m_memorySlots[i], *m_memoryViews[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700324 }
325 for (int i=0; i<m_samplers.size();i++)
326 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800327 attach(m_samplerSlots[i], *m_samplers[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700328 }
329 for (int i=0; i<m_imageViews.size();i++)
330 {
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800331 attach(m_imageSlots[i], *m_imageViews[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700332 }
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800333
334 end();
Tony Barboure2c58df2014-11-25 13:18:32 -0700335
336 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic memory view)
Chia-I Wuac0f1e72014-12-28 22:32:36 +0800337 xglCmdBindDescriptorSet(commandBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, obj(), 0 );
Tony Barbour25ef8a62014-12-03 13:59:18 -0700338}
Tony Barboure2c58df2014-11-25 13:18:32 -0700339
Chia-I Wua6bc0ce2014-12-29 14:38:28 +0800340XglImage::XglImage(XglDevice *dev)
341{
342 m_device = dev;
343 m_imageInfo.view = XGL_NULL_HANDLE;
344 m_imageInfo.state = XGL_IMAGE_STATE_UNINITIALIZED_TARGET;
345}
346
347void XglImage::init(XGL_UINT32 w, XGL_UINT32 h,
348 XGL_FORMAT fmt, XGL_FLAGS usage,
349 XGL_IMAGE_TILING tiling)
350{
351 XGL_UINT mipCount;
352
353 mipCount = 0;
354
355 XGL_UINT _w = w;
356 XGL_UINT _h = h;
357 while( ( _w > 0 ) || ( _h > 0 ) )
358 {
359 _w >>= 1;
360 _h >>= 1;
361 mipCount++;
362 }
363
364 XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info();
365 imageCreateInfo.imageType = XGL_IMAGE_2D;
366 imageCreateInfo.format = fmt;
367 imageCreateInfo.extent.width = w;
368 imageCreateInfo.extent.height = h;
369 imageCreateInfo.mipLevels = mipCount;
370 imageCreateInfo.tiling = tiling;
371
372 imageCreateInfo.usage = usage;
373
374 xgl_testing::Image::init(*m_device, imageCreateInfo);
375
376 m_imageInfo.state = XGL_IMAGE_STATE_UNINITIALIZED_TARGET;
377
378 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO createView = {
379 XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
380 XGL_NULL_HANDLE,
381 obj(),
382 {XGL_CH_FMT_R8G8B8A8, XGL_NUM_FMT_UNORM},
383 0,
384 0,
385 1
386 };
387
388 m_targetView.init(*m_device, createView);
389}
390
391XGL_RESULT XglImage::MapMemory(XGL_VOID** ptr)
392{
393 *ptr = map();
394 return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
395}
396
397XGL_RESULT XglImage::UnmapMemory()
398{
399 unmap();
400 return XGL_SUCCESS;
401}
402
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800403XglTextureObj::XglTextureObj(XglDevice *device)
Tony Barboure2c58df2014-11-25 13:18:32 -0700404{
405 m_device = device;
406 const XGL_FORMAT tex_format = { XGL_CH_FMT_B8G8R8A8, XGL_NUM_FMT_UNORM };
Tony Barboure2c58df2014-11-25 13:18:32 -0700407 const uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
Tony Barboure2c58df2014-11-25 13:18:32 -0700408
409 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
410
411 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
412
413 const XGL_IMAGE_CREATE_INFO image = {
414 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
415 .pNext = NULL,
416 .imageType = XGL_IMAGE_2D,
417 .format = tex_format,
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800418 .extent = { 16, 16, 1 },
Tony Barboure2c58df2014-11-25 13:18:32 -0700419 .mipLevels = 1,
420 .arraySize = 1,
421 .samples = 1,
422 .tiling = XGL_LINEAR_TILING,
423 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
424 .flags = 0,
425 };
426
Tony Barboure2c58df2014-11-25 13:18:32 -0700427 XGL_IMAGE_VIEW_CREATE_INFO view;
428 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
429 view.pNext = NULL;
430 view.image = XGL_NULL_HANDLE;
431 view.viewType = XGL_IMAGE_VIEW_2D;
432 view.format = image.format;
433 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
434 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
435 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
436 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
437 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
438 view.subresourceRange.baseMipLevel = 0;
439 view.subresourceRange.mipLevels = 1;
440 view.subresourceRange.baseArraySlice = 0;
441 view.subresourceRange.arraySize = 1;
442 view.minLod = 0.0f;
443
Tony Barboure2c58df2014-11-25 13:18:32 -0700444 /* create image */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800445 init(*m_device, image);
Tony Barboure2c58df2014-11-25 13:18:32 -0700446
447 /* create image view */
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800448 view.image = obj();
449 m_textureView.init(*m_device, view);
Tony Barboure2c58df2014-11-25 13:18:32 -0700450
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800451 XGL_SUBRESOURCE_LAYOUT layout =
452 subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0));
453 m_rowPitch = layout.rowPitch;
Tony Barboure2c58df2014-11-25 13:18:32 -0700454
Tony Barboure2c58df2014-11-25 13:18:32 -0700455 XGL_VOID *data;
456 XGL_INT x, y;
457
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800458 data = map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700459
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800460 for (y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700461 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800462 for (x = 0; x < extent().width; x++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700463 row[x] = tex_colors[(x & 1) ^ (y & 1)];
464 }
465
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800466 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700467
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800468 m_textureViewInfo.view = m_textureView.obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700469
470}
Tony Barbour82c39522014-12-04 14:33:33 -0700471
Tony Barboure2c58df2014-11-25 13:18:32 -0700472void XglTextureObj::ChangeColors(uint32_t color1, uint32_t color2)
473{
Tony Barboure2c58df2014-11-25 13:18:32 -0700474 const uint32_t tex_colors[2] = { color1, color2 };
475 XGL_VOID *data;
476
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800477 data = map();
Tony Barboure2c58df2014-11-25 13:18:32 -0700478
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800479 for (int y = 0; y < extent().height; y++) {
Tony Barboure2c58df2014-11-25 13:18:32 -0700480 uint32_t *row = (uint32_t *) ((char *) data + m_rowPitch * y);
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800481 for (int x = 0; x < extent().width; x++)
Tony Barboure2c58df2014-11-25 13:18:32 -0700482 row[x] = tex_colors[(x & 1) ^ (y & 1)];
483 }
484
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800485 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700486}
487
488XglSamplerObj::XglSamplerObj(XglDevice *device)
489{
Tony Barboure2c58df2014-11-25 13:18:32 -0700490 m_device = device;
Tony Barboure2c58df2014-11-25 13:18:32 -0700491
Chia-I Wue9864b52014-12-28 16:32:24 +0800492 XGL_SAMPLER_CREATE_INFO samplerCreateInfo;
493 memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo));
494 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
495 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
496 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
497 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
498 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
499 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
500 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
501 samplerCreateInfo.mipLodBias = 0.0;
502 samplerCreateInfo.maxAnisotropy = 0.0;
503 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
504 samplerCreateInfo.minLod = 0.0;
505 samplerCreateInfo.maxLod = 0.0;
506 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
Tony Barboure2c58df2014-11-25 13:18:32 -0700507
Chia-I Wue9864b52014-12-28 16:32:24 +0800508 init(*m_device, samplerCreateInfo);
Tony Barbourf325bf12014-12-03 15:59:38 -0700509}
Tony Barboure2c58df2014-11-25 13:18:32 -0700510
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700511/*
512 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
513 */
514XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
515{
516 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700517 m_commandBuffer = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700518
519 memset(&m_constantBufferView,0,sizeof(m_constantBufferView));
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700520}
521
Tony Barboure2c58df2014-11-25 13:18:32 -0700522XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
523{
Tony Barboure2c58df2014-11-25 13:18:32 -0700524 m_device = device;
Chia-I Wua07fee62014-12-28 15:26:08 +0800525 m_commandBuffer = 0;
526
527 memset(&m_constantBufferView,0,sizeof(m_constantBufferView));
Tony Barboure2c58df2014-11-25 13:18:32 -0700528 m_numVertices = constantCount;
529 m_stride = constantSize;
530
Chia-I Wua07fee62014-12-28 15:26:08 +0800531 const size_t allocationSize = constantCount * constantSize;
532 init(*m_device, allocationSize);
Tony Barboure2c58df2014-11-25 13:18:32 -0700533
Chia-I Wua07fee62014-12-28 15:26:08 +0800534 void *pData = map();
535 memcpy(pData, data, allocationSize);
536 unmap();
Tony Barboure2c58df2014-11-25 13:18:32 -0700537
538 // set up the memory view for the constant buffer
539 this->m_constantBufferView.stride = 16;
Chia-I Wua07fee62014-12-28 15:26:08 +0800540 this->m_constantBufferView.range = allocationSize;
Tony Barboure2c58df2014-11-25 13:18:32 -0700541 this->m_constantBufferView.offset = 0;
Chia-I Wua07fee62014-12-28 15:26:08 +0800542 this->m_constantBufferView.mem = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700543 this->m_constantBufferView.format.channelFormat = XGL_CH_FMT_R32G32B32A32;
544 this->m_constantBufferView.format.numericFormat = XGL_NUM_FMT_FLOAT;
545 this->m_constantBufferView.state = XGL_MEMORY_STATE_DATA_TRANSFER;
546}
Tony Barbour82c39522014-12-04 14:33:33 -0700547
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700548void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, XGL_UINT binding)
549{
Chia-I Wua07fee62014-12-28 15:26:08 +0800550 xglCmdBindVertexData(cmdBuffer, obj(), offset, binding);
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700551}
552
553
Tony Barbour099a9eb2014-12-10 17:40:15 -0700554void XglConstantBufferObj::SetMemoryState(XGL_MEMORY_STATE newState)
Tony Barboure2c58df2014-11-25 13:18:32 -0700555{
Tony Barbour38422802014-12-10 14:36:31 -0700556 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700557
Tony Barboure2c58df2014-11-25 13:18:32 -0700558 if (this->m_constantBufferView.state == newState)
559 return;
560
Tony Barbour38422802014-12-10 14:36:31 -0700561 if (!m_commandBuffer)
562 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800563 m_fence.init(*m_device, xgl_testing::Fence::create_info(0));
Tony Barbour38422802014-12-10 14:36:31 -0700564
565 m_commandBuffer = new XglCommandBufferObj(m_device);
566
567 }
568 else
569 {
Chia-I Wua07fee62014-12-28 15:26:08 +0800570 m_device->wait(m_fence);
Tony Barbour38422802014-12-10 14:36:31 -0700571 }
572
Tony Barboure2c58df2014-11-25 13:18:32 -0700573 // open the command buffer
Jon Ashburnc4164b12014-12-31 17:10:47 -0700574 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
575 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
576 .pNext = NULL,
577 .flags = 0,
578 };
579 err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700580 ASSERT_XGL_SUCCESS(err);
581
Chia-I Wua07fee62014-12-28 15:26:08 +0800582 XGL_MEMORY_STATE_TRANSITION transition =
583 state_transition(XGL_MEMORY_STATE_DATA_TRANSFER, newState, 0, m_numVertices * m_stride);
Tony Barboure2c58df2014-11-25 13:18:32 -0700584
585 // write transition to the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700586 m_commandBuffer->PrepareMemoryRegions(1, &transition);
Tony Barboure2c58df2014-11-25 13:18:32 -0700587 this->m_constantBufferView.state = newState;
588
589 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700590 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700591 ASSERT_XGL_SUCCESS(err);
592
593 XGL_UINT32 numMemRefs=1;
594 XGL_MEMORY_REF memRefs;
595 // this command buffer only uses the vertex buffer memory
596 memRefs.flags = 0;
Chia-I Wua07fee62014-12-28 15:26:08 +0800597 memRefs.mem = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700598
599 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700600 XGL_CMD_BUFFER bufferArray[1];
601 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Chia-I Wua07fee62014-12-28 15:26:08 +0800602 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700603 ASSERT_XGL_SUCCESS(err);
604}
605
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700606XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
607 : XglConstantBufferObj(device)
608{
609
610}
611
612void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
613{
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700614 XGL_FORMAT viewFormat;
615
616 m_numVertices = numIndexes;
617 m_indexType = indexType;
618 viewFormat.numericFormat = XGL_NUM_FMT_UINT;
619 switch (indexType) {
620 case XGL_INDEX_8:
621 m_stride = 1;
622 viewFormat.channelFormat = XGL_CH_FMT_R8;
623 break;
624 case XGL_INDEX_16:
625 m_stride = 2;
626 viewFormat.channelFormat = XGL_CH_FMT_R16;
627 break;
628 case XGL_INDEX_32:
629 m_stride = 4;
630 viewFormat.channelFormat = XGL_CH_FMT_R32;
631 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800632 default:
633 assert(!"unknown index type");
634 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700635 }
636
Chia-I Wua07fee62014-12-28 15:26:08 +0800637 const size_t allocationSize = numIndexes * m_stride;
638 init(*m_device, allocationSize);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700639
Chia-I Wua07fee62014-12-28 15:26:08 +0800640 void *pData = map();
641 memcpy(pData, data, allocationSize);
642 unmap();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700643
644 // set up the memory view for the constant buffer
645 this->m_constantBufferView.stride = m_stride;
Chia-I Wua07fee62014-12-28 15:26:08 +0800646 this->m_constantBufferView.range = allocationSize;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700647 this->m_constantBufferView.offset = 0;
Chia-I Wua07fee62014-12-28 15:26:08 +0800648 this->m_constantBufferView.mem = obj();
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700649 this->m_constantBufferView.format.channelFormat = viewFormat.channelFormat;
650 this->m_constantBufferView.format.numericFormat = viewFormat.numericFormat;
651 this->m_constantBufferView.state = XGL_MEMORY_STATE_DATA_TRANSFER;
652}
653
654void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
655{
Chia-I Wua07fee62014-12-28 15:26:08 +0800656 xglCmdBindIndexData(cmdBuffer, obj(), offset, m_indexType);
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700657}
Tony Barboure2c58df2014-11-25 13:18:32 -0700658
Tony Barbouraf1f9192014-12-17 10:57:58 -0700659XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
660{
661 return m_indexType;
662}
663
Tony Barbour5420af02014-12-03 13:58:15 -0700664XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo(XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700665{
666 XGL_DESCRIPTOR_SLOT_INFO *slotInfo;
667 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
668 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
669 stageInfo->shader.stage = m_stage;
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800670 stageInfo->shader.shader = obj();
Tony Barboure2c58df2014-11-25 13:18:32 -0700671 stageInfo->shader.descriptorSetMapping[0].descriptorCount = 0;
672 stageInfo->shader.linkConstBufferCount = 0;
673 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
674 stageInfo->shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
675 stageInfo->shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
676
Tony Barbour824b7712014-12-18 17:06:21 -0700677 stageInfo->shader.descriptorSetMapping[0].descriptorCount = descriptorSet->GetTotalSlots();
Tony Barboure2c58df2014-11-25 13:18:32 -0700678 if (stageInfo->shader.descriptorSetMapping[0].descriptorCount)
679 {
680 vector<int> allSlots;
681 vector<XGL_DESCRIPTOR_SET_SLOT_TYPE> allTypes;
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800682 vector<void *> allObjs;
Tony Barboure2c58df2014-11-25 13:18:32 -0700683
684 allSlots.reserve(m_memSlots.size() + m_imageSlots.size() + m_samplerSlots.size());
685 allTypes.reserve(m_memTypes.size() + m_imageTypes.size() + m_samplerTypes.size());
686 allObjs.reserve(m_memObjs.size() + m_imageObjs.size() + m_samplerObjs.size());
687
688 if (m_memSlots.size())
689 {
690 allSlots.insert(allSlots.end(), m_memSlots.begin(), m_memSlots.end());
691 allTypes.insert(allTypes.end(), m_memTypes.begin(), m_memTypes.end());
692 allObjs.insert(allObjs.end(), m_memObjs.begin(), m_memObjs.end());
693 }
694 if (m_imageSlots.size())
695 {
696 allSlots.insert(allSlots.end(), m_imageSlots.begin(), m_imageSlots.end());
697 allTypes.insert(allTypes.end(), m_imageTypes.begin(), m_imageTypes.end());
698 allObjs.insert(allObjs.end(), m_imageObjs.begin(), m_imageObjs.end());
699 }
700 if (m_samplerSlots.size())
701 {
702 allSlots.insert(allSlots.end(), m_samplerSlots.begin(), m_samplerSlots.end());
703 allTypes.insert(allTypes.end(), m_samplerTypes.begin(), m_samplerTypes.end());
704 allObjs.insert(allObjs.end(), m_samplerObjs.begin(), m_samplerObjs.end());
705 }
706
Tony Barbour5420af02014-12-03 13:58:15 -0700707 slotInfo = descriptorSet->GetSlotInfo(allSlots, allTypes, allObjs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700708 stageInfo->shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
709 }
710 return stageInfo;
711}
712
713void XglShaderObj::BindShaderEntitySlotToMemory(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglConstantBufferObj *constantBuffer)
714{
715 m_memSlots.push_back(slot);
716 m_memTypes.push_back(type);
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800717 m_memObjs.push_back(&constantBuffer->m_constantBufferView);
Tony Barboure2c58df2014-11-25 13:18:32 -0700718
719}
Tony Barbour82c39522014-12-04 14:33:33 -0700720
Tony Barboure2c58df2014-11-25 13:18:32 -0700721void XglShaderObj::BindShaderEntitySlotToImage(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglTextureObj *texture)
722{
723 m_imageSlots.push_back(slot);
724 m_imageTypes.push_back(type);
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800725 m_imageObjs.push_back(&texture->m_textureViewInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700726
727}
Tony Barbour82c39522014-12-04 14:33:33 -0700728
Tony Barboure2c58df2014-11-25 13:18:32 -0700729void XglShaderObj::BindShaderEntitySlotToSampler(int slot, XglSamplerObj *sampler)
730{
731 m_samplerSlots.push_back(slot);
732 m_samplerTypes.push_back(XGL_SLOT_SHADER_SAMPLER);
Chia-I Wuc86b54c2014-12-28 16:07:01 +0800733 m_samplerObjs.push_back(sampler);
Tony Barboure2c58df2014-11-25 13:18:32 -0700734
735}
Tony Barbour82c39522014-12-04 14:33:33 -0700736
Tony Barboure2c58df2014-11-25 13:18:32 -0700737XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
738{
739 XGL_RESULT err = XGL_SUCCESS;
740 std::vector<unsigned int> bil;
741 XGL_SHADER_CREATE_INFO createInfo;
742 size_t shader_len;
743
744 m_stage = stage;
745 m_device = device;
746
747 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
748 createInfo.pNext = NULL;
749
750 if (!framework->m_use_bil) {
751
752 shader_len = strlen(shader_code);
753 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
754 createInfo.pCode = malloc(createInfo.codeSize);
755 createInfo.flags = 0;
756
757 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
758 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
759 ((uint32_t *) createInfo.pCode)[1] = 0;
760 ((uint32_t *) createInfo.pCode)[2] = stage;
761 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
762
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800763 err = init_try(*m_device, createInfo);
Tony Barboure2c58df2014-11-25 13:18:32 -0700764 }
765
766 if (framework->m_use_bil || err) {
767 std::vector<unsigned int> bil;
768 err = XGL_SUCCESS;
769
770 // Use Reference GLSL to BIL compiler
771 framework->GLSLtoBIL(stage, shader_code, bil);
772 createInfo.pCode = bil.data();
773 createInfo.codeSize = bil.size() * sizeof(unsigned int);
774 createInfo.flags = 0;
Tony Barbour82c39522014-12-04 14:33:33 -0700775
Chia-I Wubabc0fd2014-12-29 14:14:03 +0800776 init(*m_device, createInfo);
777 }
Tony Barbourf325bf12014-12-03 15:59:38 -0700778}
Tony Barbour82c39522014-12-04 14:33:33 -0700779
Tony Barboure2c58df2014-11-25 13:18:32 -0700780XglPipelineObj::XglPipelineObj(XglDevice *device)
781{
Tony Barboure2c58df2014-11-25 13:18:32 -0700782 m_device = device;
783 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
784 m_vertexBufferCount = 0;
785
786 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
787 m_ia_state.pNext = XGL_NULL_HANDLE;
788 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
789 m_ia_state.disableVertexReuse = XGL_FALSE;
790 m_ia_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
791 m_ia_state.primitiveRestartEnable = XGL_FALSE;
792 m_ia_state.primitiveRestartIndex = 0;
793
794 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
795 m_rs_state.pNext = &m_ia_state;
796 m_rs_state.depthClipEnable = XGL_FALSE;
797 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
798 m_rs_state.pointSize = 1.0;
799
Tony Barboure2c58df2014-11-25 13:18:32 -0700800 memset(&m_cb_state,0,sizeof(m_cb_state));
801 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
802 m_cb_state.pNext = &m_rs_state;
803 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
804 m_cb_state.dualSourceBlendEnable = XGL_FALSE;
805 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
806
Chia-I Wu62bf1dc2014-12-05 11:12:13 +0800807 m_cb_state.attachment[0].blendEnable = XGL_FALSE;
808 m_cb_state.attachment[0].format.channelFormat = XGL_CH_FMT_R8G8B8A8;
809 m_cb_state.attachment[0].format.numericFormat = XGL_NUM_FMT_UNORM;
810 m_cb_state.attachment[0].channelWriteMask = 0xF;
Tony Barboure2c58df2014-11-25 13:18:32 -0700811
812 m_db_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
813 m_db_state.pNext = &m_cb_state,
814 m_db_state.format.channelFormat = XGL_CH_FMT_R32;
815 m_db_state.format.numericFormat = XGL_NUM_FMT_DS;
816
817
818};
819
820void XglPipelineObj::AddShader(XglShaderObj* shader)
821{
822 m_shaderObjs.push_back(shader);
823}
824
825void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
826{
827 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
828 m_vi_state.attributeCount = count;
829}
830
831void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
832{
833 m_vi_state.pVertexBindingDescriptions = vi_binding;
834 m_vi_state.bindingCount = count;
835}
836
837void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
838{
839 m_vertexBufferObjs.push_back(vertexDataBuffer);
840 m_vertexBufferBindings.push_back(binding);
841 m_vertexBufferCount++;
842}
843
Chia-I Wuecebf752014-12-05 10:45:15 +0800844void XglPipelineObj::SetColorAttachment(XGL_UINT binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
845{
846 m_cb_state.attachment[binding] = *att;
847}
848
Tony Barbour976e1cf2014-12-17 11:57:31 -0700849void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
850{
Tony Barbour976e1cf2014-12-17 11:57:31 -0700851 XGL_VOID* head_ptr = &m_db_state;
852 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
853
854 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
855
856 for (int i=0; i<m_shaderObjs.size(); i++)
857 {
858 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet);
859 shaderCreateInfo->pNext = head_ptr;
860 head_ptr = shaderCreateInfo;
861 }
862
863 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
864 {
865 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
866 m_vi_state.pNext = head_ptr;
867 head_ptr = &m_vi_state;
868 }
869
870 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
871 info.pNext = head_ptr;
872 info.flags = 0;
873
Chia-I Wu2648d092014-12-29 14:24:14 +0800874 init(*m_device, info);
Tony Barbour976e1cf2014-12-17 11:57:31 -0700875}
Chia-I Wu2648d092014-12-29 14:24:14 +0800876
Tony Barbour976e1cf2014-12-17 11:57:31 -0700877XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
878{
Chia-I Wu2648d092014-12-29 14:24:14 +0800879 return obj();
Tony Barbour976e1cf2014-12-17 11:57:31 -0700880}
881
Tony Barbour5420af02014-12-03 13:58:15 -0700882void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700883{
Tony Barboure2c58df2014-11-25 13:18:32 -0700884 XGL_VOID* head_ptr = &m_db_state;
885 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
886
887 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700888
889 for (int i=0; i<m_shaderObjs.size(); i++)
890 {
891 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet);
892 shaderCreateInfo->pNext = head_ptr;
893 head_ptr = shaderCreateInfo;
894 }
895
896 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
897 {
898 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
899 m_vi_state.pNext = head_ptr;
900 head_ptr = &m_vi_state;
901 }
902
903 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
904 info.pNext = head_ptr;
905 info.flags = 0;
906
Chia-I Wu2648d092014-12-29 14:24:14 +0800907 init(*m_device, info);
Tony Barboure2c58df2014-11-25 13:18:32 -0700908
Chia-I Wu2648d092014-12-29 14:24:14 +0800909 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() );
Tony Barboure2c58df2014-11-25 13:18:32 -0700910
911
912 for (int i=0; i < m_vertexBufferCount; i++)
913 {
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700914 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, m_vertexBufferObjs[i]->m_constantBufferView.offset, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700915 }
Tony Barboure2c58df2014-11-25 13:18:32 -0700916}
Tony Barbour82c39522014-12-04 14:33:33 -0700917
Tony Barboure2c58df2014-11-25 13:18:32 -0700918XglMemoryRefManager::XglMemoryRefManager() {
919
920}
Tony Barbour82c39522014-12-04 14:33:33 -0700921
Tony Barboure2c58df2014-11-25 13:18:32 -0700922void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
Chia-I Wua07fee62014-12-28 15:26:08 +0800923 m_bufferObjs.push_back(constantBuffer->obj());
Tony Barboure2c58df2014-11-25 13:18:32 -0700924}
Tony Barbour82c39522014-12-04 14:33:33 -0700925
Tony Barboure2c58df2014-11-25 13:18:32 -0700926void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
Chia-I Wu13a3aa82014-12-28 15:55:09 +0800927 const std::vector<XGL_GPU_MEMORY> mems = texture->memories();
928 if (!mems.empty())
929 m_bufferObjs.push_back(mems[0]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700930}
Tony Barbour82c39522014-12-04 14:33:33 -0700931
Tony Barboure2c58df2014-11-25 13:18:32 -0700932XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
933
934 XGL_MEMORY_REF *localRefs;
935 XGL_UINT32 numRefs=m_bufferObjs.size();
936
937 if (numRefs <= 0)
938 return NULL;
939
940 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
941 for (int i=0; i<numRefs; i++)
942 {
943 localRefs[i].flags = 0;
Chia-I Wu283d7a62014-12-28 15:43:42 +0800944 localRefs[i].mem = m_bufferObjs[i];
Tony Barboure2c58df2014-11-25 13:18:32 -0700945 }
946 return localRefs;
947}
948int XglMemoryRefManager::GetNumRefs() {
949 return m_bufferObjs.size();
950}
Tony Barbour6d047bf2014-12-10 14:34:45 -0700951
952XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
Chia-I Wud28343c2014-12-28 15:12:48 +0800953 : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(XGL_QUEUE_TYPE_GRAPHICS))
Tony Barbour6d047bf2014-12-10 14:34:45 -0700954{
Tony Barbour6d047bf2014-12-10 14:34:45 -0700955 m_device = device;
Chia-I Wud28343c2014-12-28 15:12:48 +0800956 m_renderTargetCount = 0;
Tony Barbour6d047bf2014-12-10 14:34:45 -0700957}
Tony Barbour471338d2014-12-10 17:28:39 -0700958
Tony Barbour6d047bf2014-12-10 14:34:45 -0700959XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
960{
Chia-I Wud28343c2014-12-28 15:12:48 +0800961 return obj();
Tony Barbour6d047bf2014-12-10 14:34:45 -0700962}
Tony Barbour471338d2014-12-10 17:28:39 -0700963
Jon Ashburnc4164b12014-12-31 17:10:47 -0700964XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo)
Tony Barbour471338d2014-12-10 17:28:39 -0700965{
Chia-I Wud28343c2014-12-28 15:12:48 +0800966 begin(flags);
967 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -0700968}
969
970XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
971{
Chia-I Wud28343c2014-12-28 15:12:48 +0800972 end();
973 return XGL_SUCCESS;
Tony Barbour471338d2014-12-10 17:28:39 -0700974}
975
976void XglCommandBufferObj::PrepareMemoryRegions(int transitionCount, XGL_MEMORY_STATE_TRANSITION *transitionPtr)
977{
Chia-I Wud28343c2014-12-28 15:12:48 +0800978 xglCmdPrepareMemoryRegions(obj(), transitionCount, transitionPtr);
Tony Barbour471338d2014-12-10 17:28:39 -0700979}
980
Tony Barbour30cc9e82014-12-17 11:53:55 -0700981void XglCommandBufferObj::ClearAllBuffers(XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
982{
983 XGL_UINT i;
984
985 // whatever we want to do, we do it to the whole buffer
986 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
987 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
988 srRange.baseMipLevel = 0;
989 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
990 srRange.baseArraySlice = 0;
991 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
992
993 // clear the back buffer to dark grey
994 XGL_UINT clearColor[4] = {64, 64, 64, 0};
995 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
996 for (i = 0; i < m_renderTargetCount; i++) {
997 transitionToClear.image = m_renderTargets[i]->image();
998 transitionToClear.oldState = m_renderTargets[i]->state();
999 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
1000 transitionToClear.subresourceRange = srRange;
Chia-I Wud28343c2014-12-28 15:12:48 +08001001 xglCmdPrepareImages( obj(), 1, &transitionToClear );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001002 m_renderTargets[i]->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
1003
Chia-I Wud28343c2014-12-28 15:12:48 +08001004 xglCmdClearColorImageRaw( obj(), m_renderTargets[i]->image(), clearColor, 1, &srRange );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001005 }
1006
1007 if (depthStencilImage)
1008 {
1009 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1010 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1011 dsRange.baseMipLevel = 0;
1012 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1013 dsRange.baseArraySlice = 0;
1014 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1015
1016 // prepare the depth buffer for clear
1017 memset(&transitionToClear,0,sizeof(transitionToClear));
1018 transitionToClear.image = depthStencilImage;
1019 transitionToClear.oldState = depthStencilBinding->depthState;
1020 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
1021 transitionToClear.subresourceRange = dsRange;
Chia-I Wud28343c2014-12-28 15:12:48 +08001022 xglCmdPrepareImages( obj(), 1, &transitionToClear );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001023 depthStencilBinding->depthState = transitionToClear.newState;
1024
Chia-I Wud28343c2014-12-28 15:12:48 +08001025 xglCmdClearDepthStencil(obj(), depthStencilImage, 1.0f, 0, 1, &dsRange);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001026
1027 // prepare depth buffer for rendering
1028 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
1029 transitionToRender.image = depthStencilImage;
1030 transitionToRender.oldState = XGL_IMAGE_STATE_CLEAR;
1031 transitionToRender.newState = depthStencilBinding->depthState;
1032 transitionToRender.subresourceRange = dsRange;
Chia-I Wud28343c2014-12-28 15:12:48 +08001033 xglCmdPrepareImages( obj(), 1, &transitionToRender );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001034 depthStencilBinding->depthState = transitionToClear.newState;
1035 }
1036}
1037
1038void XglCommandBufferObj::BindAttachments(XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding)
1039{
1040 XGL_UINT i;
1041 XGL_COLOR_ATTACHMENT_BIND_INFO colorBindings[XGL_MAX_COLOR_ATTACHMENTS];
1042 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1043 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1044 srRange.baseMipLevel = 0;
1045 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1046 srRange.baseArraySlice = 0;
1047 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1048
1049 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
1050 for(i=0; i<m_renderTargetCount; i++)
1051 {
1052 transitionToRender.image = m_renderTargets[i]->image();
1053 transitionToRender.oldState = m_renderTargets[i]->state();
1054 transitionToRender.newState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
1055 transitionToRender.subresourceRange = srRange;
Chia-I Wud28343c2014-12-28 15:12:48 +08001056 xglCmdPrepareImages(obj(), 1, &transitionToRender );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001057 m_renderTargets[i]->state(( XGL_IMAGE_STATE ) transitionToRender.newState);
1058 }
1059 for (i = 0; i < m_renderTargetCount; i++) {
1060 colorBindings[i].view = m_renderTargets[i]->targetView();
1061 colorBindings[i].colorAttachmentState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
1062 }
Tony Barbour22e32a12015-01-08 17:08:28 -07001063 if (depthStencilBinding->view) {
Chia-I Wud28343c2014-12-28 15:12:48 +08001064 xglCmdBindAttachments(obj(), m_renderTargetCount, colorBindings, depthStencilBinding );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001065 } else {
Chia-I Wud28343c2014-12-28 15:12:48 +08001066 xglCmdBindAttachments(obj(), m_renderTargetCount, colorBindings, XGL_NULL_HANDLE );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001067 }
1068}
1069
Tony Barbour22e32a12015-01-08 17:08:28 -07001070void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_STATE_OBJECT stateObject)
Tony Barbour30cc9e82014-12-17 11:53:55 -07001071{
Tony Barbour22e32a12015-01-08 17:08:28 -07001072 xglCmdBindStateObject( obj(), stateBindPoint, stateObject);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001073}
1074
1075void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1076{
1077 m_renderTargets.push_back(renderTarget);
1078 m_renderTargetCount++;
1079}
1080
1081void XglCommandBufferObj::DrawIndexed(XGL_UINT firstIndex, XGL_UINT indexCount, XGL_INT vertexOffset, XGL_UINT firstInstance, XGL_UINT instanceCount)
1082{
Chia-I Wud28343c2014-12-28 15:12:48 +08001083 xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001084}
1085
1086void XglCommandBufferObj::Draw(XGL_UINT firstVertex, XGL_UINT vertexCount, XGL_UINT firstInstance, XGL_UINT instanceCount)
1087{
Chia-I Wud28343c2014-12-28 15:12:48 +08001088 xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001089}
1090
1091void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs)
1092{
1093 XGL_RESULT err = XGL_SUCCESS;
1094
1095 // submit the command buffer to the universal queue
Chia-I Wud28343c2014-12-28 15:12:48 +08001096 err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001097 ASSERT_XGL_SUCCESS( err );
1098
1099 err = xglQueueWaitIdle( m_device->m_queue );
1100 ASSERT_XGL_SUCCESS( err );
1101
1102 // Wait for work to finish before cleaning up.
1103 xglDeviceWaitIdle(m_device->device());
1104
1105}
1106void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1107{
Chia-I Wud28343c2014-12-28 15:12:48 +08001108 xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001109}
1110
1111void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1112{
1113 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic memory view)
Chia-I Wud28343c2014-12-28 15:12:48 +08001114 xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, descriptorSet, 0 );
Tony Barbour30cc9e82014-12-17 11:53:55 -07001115}
1116void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, XGL_UINT offset)
1117{
Chia-I Wua07fee62014-12-28 15:26:08 +08001118 xglCmdBindIndexData(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType());
Tony Barbour30cc9e82014-12-17 11:53:55 -07001119}
1120void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, XGL_UINT offset, XGL_UINT binding)
1121{
Chia-I Wua07fee62014-12-28 15:26:08 +08001122 xglCmdBindVertexData(obj(), vertexBuffer->obj(), offset, binding);
Tony Barbour30cc9e82014-12-17 11:53:55 -07001123}