blob: 0309cfe155fbd02eccabc0d2bafb7264faf0e743 [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() :
31 m_colorBlend( XGL_NULL_HANDLE ),
32 m_stateMsaa( XGL_NULL_HANDLE ),
33 m_stateDepthStencil( XGL_NULL_HANDLE ),
34 m_stateRaster( XGL_NULL_HANDLE ),
35 m_cmdBuffer( XGL_NULL_HANDLE ),
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -060036 m_stateViewport( XGL_NULL_HANDLE ),
37 m_width( 256.0 ), // default window width
38 m_height( 256.0 ) // default window height
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060039{
40 m_render_target_fmt.channelFormat = XGL_CH_FMT_R8G8B8A8;
41 m_render_target_fmt.numericFormat = XGL_NUM_FMT_UNORM;
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -060042
43 m_colorBinding.view = XGL_NULL_HANDLE;
44 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,
57 MAX_GPUS, &this->gpu_count, objs);
58 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);
68 if (m_stateMsaa) xglDestroyObject(m_stateMsaa);
69 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
70 if (m_stateRaster) xglDestroyObject(m_stateRaster);
71 if (m_cmdBuffer) xglDestroyObject(m_cmdBuffer);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060072
73 if (m_stateViewport) {
74 xglDestroyObject(m_stateViewport);
75 }
76
77 if (m_renderTarget) {
78 // TODO: XglImage should be able to destroy itself
79// m_renderTarget->
80// xglDestroyObject(*m_renderTarget);
81 }
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060082
83 // reset the driver
Tobin Ehlisa3fdbec2014-10-23 13:45:13 -060084 m_device->destroy_device();
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060085 xglInitAndEnumerateGpus(&this->app_info, XGL_NULL_HANDLE, 0, &gpu_count, XGL_NULL_HANDLE);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060086}
87
Courtney Goeltzenleuchter53d8d892014-10-08 12:20:26 -060088void XglRenderFramework::InitState()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -060089{
90 XGL_RESULT err;
91
92 m_render_target_fmt.channelFormat = XGL_CH_FMT_R8G8B8A8;
93 m_render_target_fmt.numericFormat = XGL_NUM_FMT_UNORM;
94
95 // create a raster state (solid, back-face culling)
96 XGL_RASTER_STATE_CREATE_INFO raster = {};
97 raster.sType = XGL_STRUCTURE_TYPE_RASTER_STATE_CREATE_INFO;
98 raster.fillMode = XGL_FILL_SOLID;
99 raster.cullMode = XGL_CULL_NONE;
100 raster.frontFace = XGL_FRONT_FACE_CCW;
101 err = xglCreateRasterState( device(), &raster, &m_stateRaster );
102 ASSERT_XGL_SUCCESS(err);
103
104 XGL_COLOR_BLEND_STATE_CREATE_INFO blend = {};
105 blend.sType = XGL_STRUCTURE_TYPE_COLOR_BLEND_STATE_CREATE_INFO;
106 err = xglCreateColorBlendState(device(), &blend, &m_colorBlend);
107 ASSERT_XGL_SUCCESS( err );
108
109 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {};
110 depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
111 depthStencil.depthTestEnable = XGL_FALSE;
112 depthStencil.depthWriteEnable = XGL_FALSE;
113 depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
114 depthStencil.depthBoundsEnable = XGL_FALSE;
115 depthStencil.minDepth = 0.f;
116 depthStencil.maxDepth = 1.f;
117 depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
118 depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
119 depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
120 depthStencil.back.stencilRef = 0x00;
121 depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS;
122 depthStencil.front = depthStencil.back;
123
124 err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
125 ASSERT_XGL_SUCCESS( err );
126
127 XGL_MSAA_STATE_CREATE_INFO msaa = {};
128 msaa.sType = XGL_STRUCTURE_TYPE_MSAA_STATE_CREATE_INFO;
129 msaa.sampleMask = 1;
130 msaa.samples = 1;
131
132 err = xglCreateMsaaState( device(), &msaa, &m_stateMsaa );
133 ASSERT_XGL_SUCCESS( err );
134
135 XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {};
136
137 cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
138 cmdInfo.queueType = XGL_QUEUE_TYPE_GRAPHICS;
139 err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
140 ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed";
141}
142
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600143void XglRenderFramework::InitViewport(float width, float height)
144{
145 XGL_RESULT err;
146
147 XGL_VIEWPORT_STATE_CREATE_INFO viewport = {};
148 viewport.viewportCount = 1;
149 viewport.scissorEnable = XGL_FALSE;
150 viewport.viewports[0].originX = 0;
151 viewport.viewports[0].originY = 0;
152 viewport.viewports[0].width = 1.f * width;
153 viewport.viewports[0].height = 1.f * height;
154 viewport.viewports[0].minDepth = 0.f;
155 viewport.viewports[0].maxDepth = 1.f;
156
157 err = xglCreateViewportState( device(), &viewport, &m_stateViewport );
158 ASSERT_XGL_SUCCESS( err );
159 m_width = width;
160 m_height = height;
161}
162
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600163void XglRenderFramework::InitViewport()
164{
165 InitViewport(m_width, m_height);
166}
167
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600168void XglRenderFramework::InitRenderTarget()
169{
170 m_device->CreateImage(m_width, m_height, m_render_target_fmt,
171 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
172 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
173 &m_renderTarget);
174}
175
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600176void XglRenderFramework::GenerateBindRenderTargetCmd()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600177{
178 // bind render target
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -0600179 m_colorBinding.view = m_renderTarget->targetView();
180 m_colorBinding.colorAttachmentState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
181 if (m_depthStencilBinding.view) {
182 xglCmdBindAttachments(m_cmdBuffer, 1, &m_colorBinding, &m_depthStencilBinding );
183 } else {
184 xglCmdBindAttachments(m_cmdBuffer, 1, &m_colorBinding, XGL_NULL_HANDLE );
185 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600186}
187
Tony Barboure2c58df2014-11-25 13:18:32 -0700188void XglRenderFramework::GenerateBindStateAndPipelineCmds()
189{
190 // set all states
191 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_RASTER, m_stateRaster );
192 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_VIEWPORT, m_stateViewport );
193 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_COLOR_BLEND, m_colorBlend);
194 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_DEPTH_STENCIL, m_stateDepthStencil );
195 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_MSAA, m_stateMsaa );
196}
197
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600198void XglRenderFramework::GenerateClearAndPrepareBufferCmds()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600199{
200 // whatever we want to do, we do it to the whole buffer
201 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
202 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
203 srRange.baseMipLevel = 0;
204 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
205 srRange.baseArraySlice = 0;
206 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
207
208 // prepare the whole back buffer for clear
209 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
210 transitionToClear.image = m_renderTarget->image();
211 transitionToClear.oldState = m_renderTarget->state();
212 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
213 transitionToClear.subresourceRange = srRange;
214 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear );
215 m_renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
216
217 // clear the back buffer to dark grey
218 XGL_UINT clearColor[4] = {64, 64, 64, 0};
219 xglCmdClearColorImageRaw( m_cmdBuffer, m_renderTarget->image(), clearColor, 1, &srRange );
220
221 // prepare back buffer for rendering
222 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
223 transitionToRender.image = m_renderTarget->image();
224 transitionToRender.oldState = m_renderTarget->state();
225 transitionToRender.newState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
226 transitionToRender.subresourceRange = srRange;
227 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender );
228 m_renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600229}
Tony Barbour82c39522014-12-04 14:33:33 -0700230
Tony Barboure2c58df2014-11-25 13:18:32 -0700231XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
232{
233 m_device = device;
234 m_nextSlot = 0;
235
236}
237
238void XglDescriptorSetObj::AttachMemoryView(XglConstantBufferObj *constantBuffer)
239{
240 m_memoryViews.push_back(&constantBuffer->m_constantBufferView);
241 m_memorySlots.push_back(m_nextSlot);
242 m_nextSlot++;
243
244}
Tony Barbour82c39522014-12-04 14:33:33 -0700245
Tony Barboure2c58df2014-11-25 13:18:32 -0700246void XglDescriptorSetObj::AttachSampler(XglSamplerObj *sampler)
247{
248 m_samplers.push_back(&sampler->m_sampler);
249 m_samplerSlots.push_back(m_nextSlot);
250 m_nextSlot++;
251
252}
Tony Barbour82c39522014-12-04 14:33:33 -0700253
Tony Barboure2c58df2014-11-25 13:18:32 -0700254void XglDescriptorSetObj::AttachImageView(XglTextureObj *texture)
255{
256 m_imageViews.push_back(&texture->m_textureViewInfo);
257 m_imageSlots.push_back(m_nextSlot);
258 m_nextSlot++;
259
260}
Tony Barbour82c39522014-12-04 14:33:33 -0700261
Tony Barboure2c58df2014-11-25 13:18:32 -0700262XGL_DESCRIPTOR_SLOT_INFO* XglDescriptorSetObj::GetSlotInfo(vector<int>slots,
263 vector<XGL_DESCRIPTOR_SET_SLOT_TYPE>types,
264 vector<XGL_OBJECT>objs )
265{
266 int nSlots = m_memorySlots.size() + m_imageSlots.size() + m_samplerSlots.size();
267 m_slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( nSlots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
268 memset(m_slotInfo,0,nSlots*sizeof(XGL_DESCRIPTOR_SLOT_INFO));
269
270 for (int i=0; i<nSlots; i++)
271 {
272 m_slotInfo[i].slotObjectType = XGL_SLOT_UNUSED;
273 }
274
275 for (int i=0; i<slots.size(); i++)
276 {
277 for (int j=0; j<m_memorySlots.size(); j++)
278 {
279 if ( (XGL_OBJECT) m_memoryViews[j] == objs[i])
280 {
281 m_slotInfo[m_memorySlots[j]].shaderEntityIndex = slots[i];
282 m_slotInfo[m_memorySlots[j]].slotObjectType = types[i];
283 }
284 }
285 for (int j=0; j<m_imageSlots.size(); j++)
286 {
287 if ( (XGL_OBJECT) m_imageViews[j] == objs[i])
288 {
289 m_slotInfo[m_imageSlots[j]].shaderEntityIndex = slots[i];
290 m_slotInfo[m_imageSlots[j]].slotObjectType = types[i];
291 }
292 }
293 for (int j=0; j<m_samplerSlots.size(); j++)
294 {
295 if ( (XGL_OBJECT) m_samplers[j] == objs[i])
296 {
297 m_slotInfo[m_samplerSlots[j]].shaderEntityIndex = slots[i];
298 m_slotInfo[m_samplerSlots[j]].slotObjectType = types[i];
299 }
300 }
301 }
302
303 // for (int i=0;i<nSlots;i++)
304 // {
305 // printf("SlotInfo[%d]: Index = %d, Type = %d\n",i,m_slotInfo[i].shaderEntityIndex, m_slotInfo[i].slotObjectType);
306 // fflush(stdout);
307 // }
308
309 return(m_slotInfo);
310
311}
312
313void XglDescriptorSetObj::BindCommandBuffer(XGL_CMD_BUFFER commandBuffer)
314{
315 XGL_RESULT err;
316
317 // Create descriptor set for a uniform resource
318 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
319 m_descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
320 m_descriptorInfo.slots = m_nextSlot;
321
322 // Create a descriptor set with requested number of slots
323 err = xglCreateDescriptorSet( m_device->device(), &m_descriptorInfo, &m_rsrcDescSet );
Tony Barbourba2a1062014-12-03 09:24:05 -0700324 ASSERT_XGL_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700325
326 // Bind memory to the descriptor set
327 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
Tony Barbourba2a1062014-12-03 09:24:05 -0700328 ASSERT_XGL_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700329
330 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
331 xglClearDescriptorSetSlots(m_rsrcDescSet, 0, m_nextSlot);
332 for (int i=0; i<m_memoryViews.size();i++)
333 {
334 xglAttachMemoryViewDescriptors( m_rsrcDescSet, m_memorySlots[i], 1, m_memoryViews[i] );
335 }
336 for (int i=0; i<m_samplers.size();i++)
337 {
338 xglAttachSamplerDescriptors( m_rsrcDescSet, m_samplerSlots[i], 1, m_samplers[i] );
339 }
340 for (int i=0; i<m_imageViews.size();i++)
341 {
342 xglAttachImageViewDescriptors( m_rsrcDescSet, m_imageSlots[i], 1, m_imageViews[i] );
343 }
344 xglEndDescriptorSetUpdate( m_rsrcDescSet );
345
346 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic memory view)
347 xglCmdBindDescriptorSet(commandBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
348}
Tony Barbour82c39522014-12-04 14:33:33 -0700349
Tony Barbour25ef8a62014-12-03 13:59:18 -0700350XglDescriptorSetObj::~XglDescriptorSetObj()
351{
352 if (m_rsrcDescSet != XGL_NULL_HANDLE) xglDestroyObject(m_rsrcDescSet);
353}
Tony Barboure2c58df2014-11-25 13:18:32 -0700354
355XglTextureObj::XglTextureObj(XglDevice *device):
356 m_texture(XGL_NULL_HANDLE),
357 m_textureMem(XGL_NULL_HANDLE),
358 m_textureView(XGL_NULL_HANDLE)
359{
360 m_device = device;
361 const XGL_FORMAT tex_format = { XGL_CH_FMT_B8G8R8A8, XGL_NUM_FMT_UNORM };
362 m_texWidth = 16;
363 m_texHeight = 16;
364 const uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
365 XGL_RESULT err;
366 XGL_UINT i;
367
368 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
369
370 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
371
372 const XGL_IMAGE_CREATE_INFO image = {
373 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
374 .pNext = NULL,
375 .imageType = XGL_IMAGE_2D,
376 .format = tex_format,
377 .extent = { m_texWidth, m_texHeight, 1 },
378 .mipLevels = 1,
379 .arraySize = 1,
380 .samples = 1,
381 .tiling = XGL_LINEAR_TILING,
382 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
383 .flags = 0,
384 };
385
386 XGL_MEMORY_ALLOC_INFO mem_alloc;
387 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
388 mem_alloc.pNext = NULL;
389 mem_alloc.allocationSize = 0;
390 mem_alloc.alignment = 0;
391 mem_alloc.flags = 0;
392 mem_alloc.heapCount = 0;
393 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
394
395 XGL_IMAGE_VIEW_CREATE_INFO view;
396 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
397 view.pNext = NULL;
398 view.image = XGL_NULL_HANDLE;
399 view.viewType = XGL_IMAGE_VIEW_2D;
400 view.format = image.format;
401 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
402 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
403 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
404 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
405 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
406 view.subresourceRange.baseMipLevel = 0;
407 view.subresourceRange.mipLevels = 1;
408 view.subresourceRange.baseArraySlice = 0;
409 view.subresourceRange.arraySize = 1;
410 view.minLod = 0.0f;
411
412 XGL_MEMORY_REQUIREMENTS mem_reqs;
413 XGL_SIZE mem_reqs_size=sizeof(XGL_MEMORY_REQUIREMENTS);
414
415 /* create image */
416 err = xglCreateImage(m_device->device(), &image, &m_texture);
417 assert(!err);
418
419 err = xglGetObjectInfo(m_texture,
420 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
421 &mem_reqs_size, &mem_reqs);
422 assert(!err && mem_reqs_size == sizeof(mem_reqs));
423
424 mem_alloc.allocationSize = mem_reqs.size;
425 mem_alloc.alignment = mem_reqs.alignment;
426 mem_alloc.heapCount = mem_reqs.heapCount;
427 memcpy(mem_alloc.heaps, mem_reqs.heaps,
428 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
429
430 /* allocate memory */
431 err = xglAllocMemory(m_device->device(), &mem_alloc, &m_textureMem);
432 assert(!err);
433
434 /* bind memory */
435 err = xglBindObjectMemory(m_texture, m_textureMem, 0);
436 assert(!err);
437
438 /* create image view */
439 view.image = m_texture;
440 err = xglCreateImageView(m_device->device(), &view, &m_textureView);
441 assert(!err);
442
443
444 const XGL_IMAGE_SUBRESOURCE subres = {
445 .aspect = XGL_IMAGE_ASPECT_COLOR,
446 .mipLevel = 0,
447 .arraySlice = 0,
448 };
449 XGL_SUBRESOURCE_LAYOUT layout;
450 XGL_SIZE layout_size=sizeof(layout);
451 XGL_VOID *data;
452 XGL_INT x, y;
453
454 err = xglGetImageSubresourceInfo(m_texture, &subres,
455 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
456 assert(!err && layout_size == sizeof(layout));
457 m_rowPitch = layout.rowPitch;
458
459 err = xglMapMemory(m_textureMem, 0, &data);
460 assert(!err);
461
462 for (y = 0; y < m_texHeight; y++) {
463 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
464 for (x = 0; x < m_texWidth; x++)
465 row[x] = tex_colors[(x & 1) ^ (y & 1)];
466 }
467
468 err = xglUnmapMemory(m_textureMem);
469 assert(!err);
470
471 m_textureViewInfo.view = m_textureView;
472
473}
Tony Barbour82c39522014-12-04 14:33:33 -0700474
Tony Barbourf325bf12014-12-03 15:59:38 -0700475XglTextureObj::~XglTextureObj()
476{
477 if (m_texture != XGL_NULL_HANDLE) xglDestroyObject(m_texture);
478}
Tony Barboure2c58df2014-11-25 13:18:32 -0700479
480void XglTextureObj::ChangeColors(uint32_t color1, uint32_t color2)
481{
482 XGL_RESULT err;
483 const uint32_t tex_colors[2] = { color1, color2 };
484 XGL_VOID *data;
485
486 err = xglMapMemory(m_textureMem, 0, &data);
487 assert(!err);
488
489 for (int y = 0; y < m_texHeight; y++) {
490 uint32_t *row = (uint32_t *) ((char *) data + m_rowPitch * y);
491 for (int x = 0; x < m_texWidth; x++)
492 row[x] = tex_colors[(x & 1) ^ (y & 1)];
493 }
494
495 err = xglUnmapMemory(m_textureMem);
496 assert(!err);
497}
498
499XglSamplerObj::XglSamplerObj(XglDevice *device)
500{
501 XGL_RESULT err = XGL_SUCCESS;
502
503 m_device = device;
504 memset(&m_samplerCreateInfo,0,sizeof(m_samplerCreateInfo));
505 m_samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
506 m_samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
507 m_samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
508 m_samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
509 m_samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
510 m_samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
511 m_samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
512 m_samplerCreateInfo.mipLodBias = 0.0;
513 m_samplerCreateInfo.maxAnisotropy = 0.0;
514 m_samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
515 m_samplerCreateInfo.minLod = 0.0;
516 m_samplerCreateInfo.maxLod = 0.0;
517 m_samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
518
519 err = xglCreateSampler(m_device->device(),&m_samplerCreateInfo, &m_sampler);
Tony Barbourba2a1062014-12-03 09:24:05 -0700520 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700521
522}
Tony Barbour82c39522014-12-04 14:33:33 -0700523
Tony Barbourf325bf12014-12-03 15:59:38 -0700524XglSamplerObj::~XglSamplerObj()
525{
526 if (m_sampler != XGL_NULL_HANDLE) xglDestroyObject(m_sampler);
527}
Tony Barboure2c58df2014-11-25 13:18:32 -0700528
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700529/*
530 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
531 */
532XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
533{
534 m_device = device;
535
536 memset(&m_constantBufferView,0,sizeof(m_constantBufferView));
537 memset(&m_constantBufferMem,0,sizeof(m_constantBufferMem));
538}
539
Tony Barboure2c58df2014-11-25 13:18:32 -0700540XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
541{
542 XGL_RESULT err = XGL_SUCCESS;
543 XGL_UINT8 *pData;
544 XGL_MEMORY_ALLOC_INFO alloc_info = {};
545 m_device = device;
546 m_numVertices = constantCount;
547 m_stride = constantSize;
548
549 memset(&m_constantBufferView,0,sizeof(m_constantBufferView));
550 memset(&m_constantBufferMem,0,sizeof(m_constantBufferMem));
551
552 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
553 alloc_info.allocationSize = constantCount * constantSize;
554 alloc_info.alignment = 0;
555 alloc_info.heapCount = 1;
556 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
557
558 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
559 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
560
561 err = xglAllocMemory(m_device->device(), &alloc_info, &m_constantBufferMem);
Tony Barbourba2a1062014-12-03 09:24:05 -0700562 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700563
564 err = xglMapMemory(m_constantBufferMem, 0, (XGL_VOID **) &pData);
Tony Barbourba2a1062014-12-03 09:24:05 -0700565 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700566
567 memcpy(pData, data, alloc_info.allocationSize);
568
569 err = xglUnmapMemory(m_constantBufferMem);
Tony Barbourba2a1062014-12-03 09:24:05 -0700570 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700571
572 // set up the memory view for the constant buffer
573 this->m_constantBufferView.stride = 16;
574 this->m_constantBufferView.range = alloc_info.allocationSize;
575 this->m_constantBufferView.offset = 0;
576 this->m_constantBufferView.mem = m_constantBufferMem;
577 this->m_constantBufferView.format.channelFormat = XGL_CH_FMT_R32G32B32A32;
578 this->m_constantBufferView.format.numericFormat = XGL_NUM_FMT_FLOAT;
579 this->m_constantBufferView.state = XGL_MEMORY_STATE_DATA_TRANSFER;
580}
Tony Barbour82c39522014-12-04 14:33:33 -0700581
Tony Barbour07e80dc2014-12-03 16:23:43 -0700582XglConstantBufferObj::~XglConstantBufferObj()
583{
584 if (m_constantBufferMem != XGL_NULL_HANDLE) xglFreeMemory(m_constantBufferMem);
585}
Tony Barboure2c58df2014-11-25 13:18:32 -0700586
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700587void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, XGL_UINT binding)
588{
589 xglCmdBindVertexData(cmdBuffer, this->m_constantBufferMem, offset, binding);
590}
591
592
Tony Barboure2c58df2014-11-25 13:18:32 -0700593void XglConstantBufferObj::SetMemoryState(XGL_CMD_BUFFER cmdBuffer, XGL_MEMORY_STATE newState)
594{
595 if (this->m_constantBufferView.state == newState)
596 return;
597
598 // open the command buffer
599 XGL_RESULT err = xglBeginCommandBuffer( cmdBuffer, 0 );
600 ASSERT_XGL_SUCCESS(err);
601
602 XGL_MEMORY_STATE_TRANSITION transition = {};
603 transition.mem = m_constantBufferMem;
604 transition.oldState = XGL_MEMORY_STATE_DATA_TRANSFER;
605 transition.newState = newState;
606 transition.offset = 0;
607 transition.regionSize = m_numVertices * m_stride;
608
609 // write transition to the command buffer
610 xglCmdPrepareMemoryRegions( cmdBuffer, 1, &transition );
611 this->m_constantBufferView.state = newState;
612
613 // finish recording the command buffer
614 err = xglEndCommandBuffer( cmdBuffer );
615 ASSERT_XGL_SUCCESS(err);
616
617 XGL_UINT32 numMemRefs=1;
618 XGL_MEMORY_REF memRefs;
619 // this command buffer only uses the vertex buffer memory
620 memRefs.flags = 0;
621 memRefs.mem = m_constantBufferMem;
622
623 // submit the command buffer to the universal queue
624 err = xglQueueSubmit( m_device->m_queue, 1, &cmdBuffer, numMemRefs, &memRefs, NULL );
625 ASSERT_XGL_SUCCESS(err);
626}
627
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700628XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
629 : XglConstantBufferObj(device)
630{
631
632}
633
634void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
635{
636 XGL_RESULT err = XGL_SUCCESS;
637 XGL_UINT8 *pData;
638 XGL_MEMORY_ALLOC_INFO alloc_info = {};
639 XGL_FORMAT viewFormat;
640
641 m_numVertices = numIndexes;
642 m_indexType = indexType;
643 viewFormat.numericFormat = XGL_NUM_FMT_UINT;
644 switch (indexType) {
645 case XGL_INDEX_8:
646 m_stride = 1;
647 viewFormat.channelFormat = XGL_CH_FMT_R8;
648 break;
649 case XGL_INDEX_16:
650 m_stride = 2;
651 viewFormat.channelFormat = XGL_CH_FMT_R16;
652 break;
653 case XGL_INDEX_32:
654 m_stride = 4;
655 viewFormat.channelFormat = XGL_CH_FMT_R32;
656 break;
657 }
658
659 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
660 alloc_info.allocationSize = numIndexes * m_stride;
661 alloc_info.alignment = 0;
662 alloc_info.heapCount = 1;
663 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
664
665 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
666 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
667
668 err = xglAllocMemory(m_device->device(), &alloc_info, &m_constantBufferMem);
669 ASSERT_XGL_SUCCESS(err);
670
671 err = xglMapMemory(m_constantBufferMem, 0, (XGL_VOID **) &pData);
672 ASSERT_XGL_SUCCESS(err);
673
674 memcpy(pData, data, alloc_info.allocationSize);
675
676 err = xglUnmapMemory(m_constantBufferMem);
677 ASSERT_XGL_SUCCESS(err);
678
679 // set up the memory view for the constant buffer
680 this->m_constantBufferView.stride = m_stride;
681 this->m_constantBufferView.range = alloc_info.allocationSize;
682 this->m_constantBufferView.offset = 0;
683 this->m_constantBufferView.mem = m_constantBufferMem;
684 this->m_constantBufferView.format.channelFormat = viewFormat.channelFormat;
685 this->m_constantBufferView.format.numericFormat = viewFormat.numericFormat;
686 this->m_constantBufferView.state = XGL_MEMORY_STATE_DATA_TRANSFER;
687}
688
689void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
690{
691 xglCmdBindIndexData(cmdBuffer, this->m_constantBufferMem, offset, m_indexType);
692}
Tony Barboure2c58df2014-11-25 13:18:32 -0700693
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;
700 stageInfo->shader.shader = m_shader;
701 stageInfo->shader.descriptorSetMapping[0].descriptorCount = 0;
702 stageInfo->shader.linkConstBufferCount = 0;
703 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
704 stageInfo->shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
705 stageInfo->shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
706
707 stageInfo->shader.descriptorSetMapping[0].descriptorCount = m_memSlots.size() + m_imageSlots.size() + m_samplerSlots.size();
708 if (stageInfo->shader.descriptorSetMapping[0].descriptorCount)
709 {
710 vector<int> allSlots;
711 vector<XGL_DESCRIPTOR_SET_SLOT_TYPE> allTypes;
712 vector<XGL_OBJECT> allObjs;
713
714 allSlots.reserve(m_memSlots.size() + m_imageSlots.size() + m_samplerSlots.size());
715 allTypes.reserve(m_memTypes.size() + m_imageTypes.size() + m_samplerTypes.size());
716 allObjs.reserve(m_memObjs.size() + m_imageObjs.size() + m_samplerObjs.size());
717
718 if (m_memSlots.size())
719 {
720 allSlots.insert(allSlots.end(), m_memSlots.begin(), m_memSlots.end());
721 allTypes.insert(allTypes.end(), m_memTypes.begin(), m_memTypes.end());
722 allObjs.insert(allObjs.end(), m_memObjs.begin(), m_memObjs.end());
723 }
724 if (m_imageSlots.size())
725 {
726 allSlots.insert(allSlots.end(), m_imageSlots.begin(), m_imageSlots.end());
727 allTypes.insert(allTypes.end(), m_imageTypes.begin(), m_imageTypes.end());
728 allObjs.insert(allObjs.end(), m_imageObjs.begin(), m_imageObjs.end());
729 }
730 if (m_samplerSlots.size())
731 {
732 allSlots.insert(allSlots.end(), m_samplerSlots.begin(), m_samplerSlots.end());
733 allTypes.insert(allTypes.end(), m_samplerTypes.begin(), m_samplerTypes.end());
734 allObjs.insert(allObjs.end(), m_samplerObjs.begin(), m_samplerObjs.end());
735 }
736
Tony Barbour5420af02014-12-03 13:58:15 -0700737 slotInfo = descriptorSet->GetSlotInfo(allSlots, allTypes, allObjs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700738 stageInfo->shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
739 }
740 return stageInfo;
741}
742
743void XglShaderObj::BindShaderEntitySlotToMemory(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglConstantBufferObj *constantBuffer)
744{
745 m_memSlots.push_back(slot);
746 m_memTypes.push_back(type);
747 m_memObjs.push_back((XGL_OBJECT) &constantBuffer->m_constantBufferView);
748
749}
Tony Barbour82c39522014-12-04 14:33:33 -0700750
Tony Barboure2c58df2014-11-25 13:18:32 -0700751void XglShaderObj::BindShaderEntitySlotToImage(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglTextureObj *texture)
752{
753 m_imageSlots.push_back(slot);
754 m_imageTypes.push_back(type);
755 m_imageObjs.push_back((XGL_OBJECT) &texture->m_textureViewInfo);
756
757}
Tony Barbour82c39522014-12-04 14:33:33 -0700758
Tony Barboure2c58df2014-11-25 13:18:32 -0700759void XglShaderObj::BindShaderEntitySlotToSampler(int slot, XglSamplerObj *sampler)
760{
761 m_samplerSlots.push_back(slot);
762 m_samplerTypes.push_back(XGL_SLOT_SHADER_SAMPLER);
763 m_samplerObjs.push_back(sampler->m_sampler);
764
765}
Tony Barbour82c39522014-12-04 14:33:33 -0700766
Tony Barboure2c58df2014-11-25 13:18:32 -0700767XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
768{
769 XGL_RESULT err = XGL_SUCCESS;
770 std::vector<unsigned int> bil;
771 XGL_SHADER_CREATE_INFO createInfo;
772 size_t shader_len;
773
774 m_stage = stage;
775 m_device = device;
776
777 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
778 createInfo.pNext = NULL;
779
780 if (!framework->m_use_bil) {
781
782 shader_len = strlen(shader_code);
783 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
784 createInfo.pCode = malloc(createInfo.codeSize);
785 createInfo.flags = 0;
786
787 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
788 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
789 ((uint32_t *) createInfo.pCode)[1] = 0;
790 ((uint32_t *) createInfo.pCode)[2] = stage;
791 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
792
793 err = xglCreateShader(m_device->device(), &createInfo, &m_shader);
794 if (err) {
795 free((void *) createInfo.pCode);
796 }
797 }
798
799 if (framework->m_use_bil || err) {
800 std::vector<unsigned int> bil;
801 err = XGL_SUCCESS;
802
803 // Use Reference GLSL to BIL compiler
804 framework->GLSLtoBIL(stage, shader_code, bil);
805 createInfo.pCode = bil.data();
806 createInfo.codeSize = bil.size() * sizeof(unsigned int);
807 createInfo.flags = 0;
808 err = xglCreateShader(m_device->device(), &createInfo, &m_shader);
Tony Barbourba2a1062014-12-03 09:24:05 -0700809 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700810 }
811}
Tony Barbour82c39522014-12-04 14:33:33 -0700812
Tony Barbourf325bf12014-12-03 15:59:38 -0700813XglShaderObj::~XglShaderObj()
814{
815 if (m_shader != XGL_NULL_HANDLE) xglDestroyObject(m_shader);
816}
Tony Barbour82c39522014-12-04 14:33:33 -0700817
Tony Barboure2c58df2014-11-25 13:18:32 -0700818XglPipelineObj::XglPipelineObj(XglDevice *device)
819{
820 XGL_RESULT err;
821
822 m_device = device;
823 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
824 m_vertexBufferCount = 0;
825
826 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
827 m_ia_state.pNext = XGL_NULL_HANDLE;
828 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
829 m_ia_state.disableVertexReuse = XGL_FALSE;
830 m_ia_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
831 m_ia_state.primitiveRestartEnable = XGL_FALSE;
832 m_ia_state.primitiveRestartIndex = 0;
833
834 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
835 m_rs_state.pNext = &m_ia_state;
836 m_rs_state.depthClipEnable = XGL_FALSE;
837 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
838 m_rs_state.pointSize = 1.0;
839
840 m_render_target_format.channelFormat = XGL_CH_FMT_R8G8B8A8;
841 m_render_target_format.numericFormat = XGL_NUM_FMT_UNORM;
842
843 memset(&m_cb_state,0,sizeof(m_cb_state));
844 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
845 m_cb_state.pNext = &m_rs_state;
846 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
847 m_cb_state.dualSourceBlendEnable = XGL_FALSE;
848 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
849
850 m_cb_attachment_state.blendEnable = XGL_FALSE;
851 m_cb_attachment_state.format = m_render_target_format;
852 m_cb_attachment_state.channelWriteMask = 0xF;
853 m_cb_state.attachment[0] = m_cb_attachment_state;
854
855 m_db_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
856 m_db_state.pNext = &m_cb_state,
857 m_db_state.format.channelFormat = XGL_CH_FMT_R32;
858 m_db_state.format.numericFormat = XGL_NUM_FMT_DS;
859
860
861};
862
863void XglPipelineObj::AddShader(XglShaderObj* shader)
864{
865 m_shaderObjs.push_back(shader);
866}
867
868void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
869{
870 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
871 m_vi_state.attributeCount = count;
872}
873
874void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
875{
876 m_vi_state.pVertexBindingDescriptions = vi_binding;
877 m_vi_state.bindingCount = count;
878}
879
880void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
881{
882 m_vertexBufferObjs.push_back(vertexDataBuffer);
883 m_vertexBufferBindings.push_back(binding);
884 m_vertexBufferCount++;
885}
886
Tony Barbour5420af02014-12-03 13:58:15 -0700887void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700888{
889 XGL_RESULT err;
890 XGL_VOID* head_ptr = &m_db_state;
891 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
892
893 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
894 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vertexInputAttrib;
895
896 for (int i=0; i<m_shaderObjs.size(); i++)
897 {
898 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet);
899 shaderCreateInfo->pNext = head_ptr;
900 head_ptr = shaderCreateInfo;
901 }
902
903 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
904 {
905 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
906 m_vi_state.pNext = head_ptr;
907 head_ptr = &m_vi_state;
908 }
909
910 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
911 info.pNext = head_ptr;
912 info.flags = 0;
913
914 err = xglCreateGraphicsPipeline(m_device->device(), &info, &m_pipeline);
Tony Barbourba2a1062014-12-03 09:24:05 -0700915 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700916
917 err = m_device->AllocAndBindGpuMemory(m_pipeline, "Pipeline", &m_pipe_mem);
Tony Barbourba2a1062014-12-03 09:24:05 -0700918 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700919
920 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, m_pipeline );
921
922
923 for (int i=0; i < m_vertexBufferCount; i++)
924 {
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700925 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, m_vertexBufferObjs[i]->m_constantBufferView.offset, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -0700926 }
Tony Barboure2c58df2014-11-25 13:18:32 -0700927}
Tony Barbour82c39522014-12-04 14:33:33 -0700928
Tony Barbourf325bf12014-12-03 15:59:38 -0700929XglPipelineObj::~XglPipelineObj()
930{
931 if (m_pipeline != XGL_NULL_HANDLE) xglDestroyObject(m_pipeline);
932}
Tony Barboure2c58df2014-11-25 13:18:32 -0700933
934XglMemoryRefManager::XglMemoryRefManager() {
935
936}
Tony Barbour82c39522014-12-04 14:33:33 -0700937
Tony Barboure2c58df2014-11-25 13:18:32 -0700938void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
939 m_bufferObjs.push_back(&constantBuffer->m_constantBufferMem);
940}
Tony Barbour82c39522014-12-04 14:33:33 -0700941
Tony Barboure2c58df2014-11-25 13:18:32 -0700942void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
943 m_bufferObjs.push_back(&texture->m_textureMem);
944}
Tony Barbour82c39522014-12-04 14:33:33 -0700945
Tony Barboure2c58df2014-11-25 13:18:32 -0700946XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
947
948 XGL_MEMORY_REF *localRefs;
949 XGL_UINT32 numRefs=m_bufferObjs.size();
950
951 if (numRefs <= 0)
952 return NULL;
953
954 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
955 for (int i=0; i<numRefs; i++)
956 {
957 localRefs[i].flags = 0;
958 localRefs[i].mem = *m_bufferObjs[i];
959 }
960 return localRefs;
961}
962int XglMemoryRefManager::GetNumRefs() {
963 return m_bufferObjs.size();
964}