blob: 1afc8d98b7d8bdb5084f5252c842a7a9b16a4793 [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,
59 MAX_GPUS, &this->gpu_count, objs);
60 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
Tobin Ehlisa3fdbec2014-10-23 13:45:13 -060088 m_device->destroy_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++) {
177 m_device->CreateImage(m_width, m_height, m_render_target_fmt,
178 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
179 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
180 &m_renderTargets[i]);
181 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600182}
183
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600184void XglRenderFramework::GenerateBindRenderTargetCmd()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600185{
Chia-I Wuecebf752014-12-05 10:45:15 +0800186 XGL_UINT i;
187
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600188 // bind render target
Chia-I Wuecebf752014-12-05 10:45:15 +0800189 for (i = 0; i < m_renderTargetCount; i++) {
190 m_colorBindings[i].view = m_renderTargets[i]->targetView();
191 m_colorBindings[i].colorAttachmentState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
192 }
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -0600193 if (m_depthStencilBinding.view) {
Chia-I Wuecebf752014-12-05 10:45:15 +0800194 xglCmdBindAttachments(m_cmdBuffer, m_renderTargetCount, m_colorBindings, &m_depthStencilBinding );
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -0600195 } else {
Chia-I Wuecebf752014-12-05 10:45:15 +0800196 xglCmdBindAttachments(m_cmdBuffer, m_renderTargetCount, m_colorBindings, XGL_NULL_HANDLE );
Courtney Goeltzenleuchter32e486c2014-10-22 14:12:38 -0600197 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600198}
199
Tony Barboure2c58df2014-11-25 13:18:32 -0700200void XglRenderFramework::GenerateBindStateAndPipelineCmds()
201{
202 // set all states
203 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_RASTER, m_stateRaster );
204 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_VIEWPORT, m_stateViewport );
205 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_COLOR_BLEND, m_colorBlend);
206 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_DEPTH_STENCIL, m_stateDepthStencil );
207 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_MSAA, m_stateMsaa );
208}
209
Courtney Goeltzenleuchter02d33c12014-10-08 14:26:40 -0600210void XglRenderFramework::GenerateClearAndPrepareBufferCmds()
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600211{
Chia-I Wuecebf752014-12-05 10:45:15 +0800212 XGL_UINT i;
213
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600214 // whatever we want to do, we do it to the whole buffer
215 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
216 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
217 srRange.baseMipLevel = 0;
218 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
219 srRange.baseArraySlice = 0;
220 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
221
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600222 // clear the back buffer to dark grey
223 XGL_UINT clearColor[4] = {64, 64, 64, 0};
Chia-I Wuecebf752014-12-05 10:45:15 +0800224 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
225 for (i = 0; i < m_renderTargetCount; i++) {
226 transitionToClear.image = m_renderTargets[i]->image();
227 transitionToClear.oldState = m_renderTargets[i]->state();
228 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
229 transitionToClear.subresourceRange = srRange;
230 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear );
231 m_renderTargets[i]->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
232
233 xglCmdClearColorImageRaw( m_cmdBuffer, m_renderTargets[i]->image(), clearColor, 1, &srRange );
234 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600235
236 // prepare back buffer for rendering
237 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
Chia-I Wuecebf752014-12-05 10:45:15 +0800238 for (i = 0; i < m_renderTargetCount; i++) {
239 transitionToRender.image = m_renderTargets[i]->image();
240 transitionToRender.oldState = m_renderTargets[i]->state();
241 transitionToRender.newState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
242 transitionToRender.subresourceRange = srRange;
243 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender );
244 m_renderTargets[i]->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
245 }
Courtney Goeltzenleuchtera4b278b2014-10-08 08:50:49 -0600246}
Tony Barbour82c39522014-12-04 14:33:33 -0700247
Tony Barboure2c58df2014-11-25 13:18:32 -0700248XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device)
249{
250 m_device = device;
251 m_nextSlot = 0;
252
253}
254
255void XglDescriptorSetObj::AttachMemoryView(XglConstantBufferObj *constantBuffer)
256{
257 m_memoryViews.push_back(&constantBuffer->m_constantBufferView);
258 m_memorySlots.push_back(m_nextSlot);
259 m_nextSlot++;
260
261}
Tony Barbour82c39522014-12-04 14:33:33 -0700262
Tony Barboure2c58df2014-11-25 13:18:32 -0700263void XglDescriptorSetObj::AttachSampler(XglSamplerObj *sampler)
264{
265 m_samplers.push_back(&sampler->m_sampler);
266 m_samplerSlots.push_back(m_nextSlot);
267 m_nextSlot++;
268
269}
Tony Barbour82c39522014-12-04 14:33:33 -0700270
Tony Barboure2c58df2014-11-25 13:18:32 -0700271void XglDescriptorSetObj::AttachImageView(XglTextureObj *texture)
272{
273 m_imageViews.push_back(&texture->m_textureViewInfo);
274 m_imageSlots.push_back(m_nextSlot);
275 m_nextSlot++;
276
277}
Tony Barbour82c39522014-12-04 14:33:33 -0700278
Tony Barboure2c58df2014-11-25 13:18:32 -0700279XGL_DESCRIPTOR_SLOT_INFO* XglDescriptorSetObj::GetSlotInfo(vector<int>slots,
280 vector<XGL_DESCRIPTOR_SET_SLOT_TYPE>types,
281 vector<XGL_OBJECT>objs )
282{
283 int nSlots = m_memorySlots.size() + m_imageSlots.size() + m_samplerSlots.size();
284 m_slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( nSlots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
285 memset(m_slotInfo,0,nSlots*sizeof(XGL_DESCRIPTOR_SLOT_INFO));
286
287 for (int i=0; i<nSlots; i++)
288 {
289 m_slotInfo[i].slotObjectType = XGL_SLOT_UNUSED;
290 }
291
292 for (int i=0; i<slots.size(); i++)
293 {
294 for (int j=0; j<m_memorySlots.size(); j++)
295 {
296 if ( (XGL_OBJECT) m_memoryViews[j] == objs[i])
297 {
298 m_slotInfo[m_memorySlots[j]].shaderEntityIndex = slots[i];
299 m_slotInfo[m_memorySlots[j]].slotObjectType = types[i];
300 }
301 }
302 for (int j=0; j<m_imageSlots.size(); j++)
303 {
304 if ( (XGL_OBJECT) m_imageViews[j] == objs[i])
305 {
306 m_slotInfo[m_imageSlots[j]].shaderEntityIndex = slots[i];
307 m_slotInfo[m_imageSlots[j]].slotObjectType = types[i];
308 }
309 }
310 for (int j=0; j<m_samplerSlots.size(); j++)
311 {
312 if ( (XGL_OBJECT) m_samplers[j] == objs[i])
313 {
314 m_slotInfo[m_samplerSlots[j]].shaderEntityIndex = slots[i];
315 m_slotInfo[m_samplerSlots[j]].slotObjectType = types[i];
316 }
317 }
318 }
319
320 // for (int i=0;i<nSlots;i++)
321 // {
322 // printf("SlotInfo[%d]: Index = %d, Type = %d\n",i,m_slotInfo[i].shaderEntityIndex, m_slotInfo[i].slotObjectType);
323 // fflush(stdout);
324 // }
325
326 return(m_slotInfo);
327
328}
329
330void XglDescriptorSetObj::BindCommandBuffer(XGL_CMD_BUFFER commandBuffer)
331{
332 XGL_RESULT err;
333
334 // Create descriptor set for a uniform resource
335 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
336 m_descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
337 m_descriptorInfo.slots = m_nextSlot;
338
339 // Create a descriptor set with requested number of slots
340 err = xglCreateDescriptorSet( m_device->device(), &m_descriptorInfo, &m_rsrcDescSet );
Tony Barbourba2a1062014-12-03 09:24:05 -0700341 ASSERT_XGL_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700342
343 // Bind memory to the descriptor set
344 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
Tony Barbourba2a1062014-12-03 09:24:05 -0700345 ASSERT_XGL_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700346
347 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
348 xglClearDescriptorSetSlots(m_rsrcDescSet, 0, m_nextSlot);
349 for (int i=0; i<m_memoryViews.size();i++)
350 {
351 xglAttachMemoryViewDescriptors( m_rsrcDescSet, m_memorySlots[i], 1, m_memoryViews[i] );
352 }
353 for (int i=0; i<m_samplers.size();i++)
354 {
355 xglAttachSamplerDescriptors( m_rsrcDescSet, m_samplerSlots[i], 1, m_samplers[i] );
356 }
357 for (int i=0; i<m_imageViews.size();i++)
358 {
359 xglAttachImageViewDescriptors( m_rsrcDescSet, m_imageSlots[i], 1, m_imageViews[i] );
360 }
361 xglEndDescriptorSetUpdate( m_rsrcDescSet );
362
363 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic memory view)
364 xglCmdBindDescriptorSet(commandBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
365}
Tony Barbour82c39522014-12-04 14:33:33 -0700366
Tony Barbour25ef8a62014-12-03 13:59:18 -0700367XglDescriptorSetObj::~XglDescriptorSetObj()
368{
369 if (m_rsrcDescSet != XGL_NULL_HANDLE) xglDestroyObject(m_rsrcDescSet);
370}
Tony Barboure2c58df2014-11-25 13:18:32 -0700371
372XglTextureObj::XglTextureObj(XglDevice *device):
373 m_texture(XGL_NULL_HANDLE),
374 m_textureMem(XGL_NULL_HANDLE),
375 m_textureView(XGL_NULL_HANDLE)
376{
377 m_device = device;
378 const XGL_FORMAT tex_format = { XGL_CH_FMT_B8G8R8A8, XGL_NUM_FMT_UNORM };
379 m_texWidth = 16;
380 m_texHeight = 16;
381 const uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
382 XGL_RESULT err;
Tony Barboure2c58df2014-11-25 13:18:32 -0700383
384 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
385
386 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
387
388 const XGL_IMAGE_CREATE_INFO image = {
389 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
390 .pNext = NULL,
391 .imageType = XGL_IMAGE_2D,
392 .format = tex_format,
393 .extent = { m_texWidth, m_texHeight, 1 },
394 .mipLevels = 1,
395 .arraySize = 1,
396 .samples = 1,
397 .tiling = XGL_LINEAR_TILING,
398 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
399 .flags = 0,
400 };
401
402 XGL_MEMORY_ALLOC_INFO mem_alloc;
403 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
404 mem_alloc.pNext = NULL;
405 mem_alloc.allocationSize = 0;
406 mem_alloc.alignment = 0;
407 mem_alloc.flags = 0;
408 mem_alloc.heapCount = 0;
409 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
410
411 XGL_IMAGE_VIEW_CREATE_INFO view;
412 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
413 view.pNext = NULL;
414 view.image = XGL_NULL_HANDLE;
415 view.viewType = XGL_IMAGE_VIEW_2D;
416 view.format = image.format;
417 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
418 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
419 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
420 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
421 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
422 view.subresourceRange.baseMipLevel = 0;
423 view.subresourceRange.mipLevels = 1;
424 view.subresourceRange.baseArraySlice = 0;
425 view.subresourceRange.arraySize = 1;
426 view.minLod = 0.0f;
427
428 XGL_MEMORY_REQUIREMENTS mem_reqs;
429 XGL_SIZE mem_reqs_size=sizeof(XGL_MEMORY_REQUIREMENTS);
430
431 /* create image */
432 err = xglCreateImage(m_device->device(), &image, &m_texture);
433 assert(!err);
434
435 err = xglGetObjectInfo(m_texture,
436 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
437 &mem_reqs_size, &mem_reqs);
438 assert(!err && mem_reqs_size == sizeof(mem_reqs));
439
440 mem_alloc.allocationSize = mem_reqs.size;
441 mem_alloc.alignment = mem_reqs.alignment;
442 mem_alloc.heapCount = mem_reqs.heapCount;
443 memcpy(mem_alloc.heaps, mem_reqs.heaps,
444 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
445
446 /* allocate memory */
447 err = xglAllocMemory(m_device->device(), &mem_alloc, &m_textureMem);
448 assert(!err);
449
450 /* bind memory */
451 err = xglBindObjectMemory(m_texture, m_textureMem, 0);
452 assert(!err);
453
454 /* create image view */
455 view.image = m_texture;
456 err = xglCreateImageView(m_device->device(), &view, &m_textureView);
457 assert(!err);
458
459
460 const XGL_IMAGE_SUBRESOURCE subres = {
461 .aspect = XGL_IMAGE_ASPECT_COLOR,
462 .mipLevel = 0,
463 .arraySlice = 0,
464 };
465 XGL_SUBRESOURCE_LAYOUT layout;
466 XGL_SIZE layout_size=sizeof(layout);
467 XGL_VOID *data;
468 XGL_INT x, y;
469
470 err = xglGetImageSubresourceInfo(m_texture, &subres,
471 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
472 assert(!err && layout_size == sizeof(layout));
473 m_rowPitch = layout.rowPitch;
474
475 err = xglMapMemory(m_textureMem, 0, &data);
476 assert(!err);
477
478 for (y = 0; y < m_texHeight; y++) {
479 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
480 for (x = 0; x < m_texWidth; x++)
481 row[x] = tex_colors[(x & 1) ^ (y & 1)];
482 }
483
484 err = xglUnmapMemory(m_textureMem);
485 assert(!err);
486
487 m_textureViewInfo.view = m_textureView;
488
489}
Tony Barbour82c39522014-12-04 14:33:33 -0700490
Tony Barbourf325bf12014-12-03 15:59:38 -0700491XglTextureObj::~XglTextureObj()
492{
493 if (m_texture != XGL_NULL_HANDLE) xglDestroyObject(m_texture);
494}
Tony Barboure2c58df2014-11-25 13:18:32 -0700495
496void XglTextureObj::ChangeColors(uint32_t color1, uint32_t color2)
497{
498 XGL_RESULT err;
499 const uint32_t tex_colors[2] = { color1, color2 };
500 XGL_VOID *data;
501
502 err = xglMapMemory(m_textureMem, 0, &data);
503 assert(!err);
504
505 for (int y = 0; y < m_texHeight; y++) {
506 uint32_t *row = (uint32_t *) ((char *) data + m_rowPitch * y);
507 for (int x = 0; x < m_texWidth; x++)
508 row[x] = tex_colors[(x & 1) ^ (y & 1)];
509 }
510
511 err = xglUnmapMemory(m_textureMem);
512 assert(!err);
513}
514
515XglSamplerObj::XglSamplerObj(XglDevice *device)
516{
517 XGL_RESULT err = XGL_SUCCESS;
518
519 m_device = device;
520 memset(&m_samplerCreateInfo,0,sizeof(m_samplerCreateInfo));
521 m_samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
522 m_samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
523 m_samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
524 m_samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
525 m_samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
526 m_samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
527 m_samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
528 m_samplerCreateInfo.mipLodBias = 0.0;
529 m_samplerCreateInfo.maxAnisotropy = 0.0;
530 m_samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
531 m_samplerCreateInfo.minLod = 0.0;
532 m_samplerCreateInfo.maxLod = 0.0;
533 m_samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
534
535 err = xglCreateSampler(m_device->device(),&m_samplerCreateInfo, &m_sampler);
Tony Barbourba2a1062014-12-03 09:24:05 -0700536 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700537
538}
Tony Barbour82c39522014-12-04 14:33:33 -0700539
Tony Barbourf325bf12014-12-03 15:59:38 -0700540XglSamplerObj::~XglSamplerObj()
541{
542 if (m_sampler != XGL_NULL_HANDLE) xglDestroyObject(m_sampler);
543}
Tony Barboure2c58df2014-11-25 13:18:32 -0700544
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700545/*
546 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
547 */
548XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
549{
550 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700551 m_commandBuffer = 0;
552 m_fence = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700553
554 memset(&m_constantBufferView,0,sizeof(m_constantBufferView));
555 memset(&m_constantBufferMem,0,sizeof(m_constantBufferMem));
556}
557
Tony Barboure2c58df2014-11-25 13:18:32 -0700558XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
559{
560 XGL_RESULT err = XGL_SUCCESS;
561 XGL_UINT8 *pData;
562 XGL_MEMORY_ALLOC_INFO alloc_info = {};
563 m_device = device;
564 m_numVertices = constantCount;
565 m_stride = constantSize;
566
567 memset(&m_constantBufferView,0,sizeof(m_constantBufferView));
568 memset(&m_constantBufferMem,0,sizeof(m_constantBufferMem));
Tony Barbour38422802014-12-10 14:36:31 -0700569 m_commandBuffer = 0;
570 m_fence = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700571
572 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
573 alloc_info.allocationSize = constantCount * constantSize;
574 alloc_info.alignment = 0;
575 alloc_info.heapCount = 1;
576 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
577
578 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
579 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
580
581 err = xglAllocMemory(m_device->device(), &alloc_info, &m_constantBufferMem);
Tony Barbourba2a1062014-12-03 09:24:05 -0700582 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700583
584 err = xglMapMemory(m_constantBufferMem, 0, (XGL_VOID **) &pData);
Tony Barbourba2a1062014-12-03 09:24:05 -0700585 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700586
587 memcpy(pData, data, alloc_info.allocationSize);
588
589 err = xglUnmapMemory(m_constantBufferMem);
Tony Barbourba2a1062014-12-03 09:24:05 -0700590 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700591
592 // set up the memory view for the constant buffer
593 this->m_constantBufferView.stride = 16;
594 this->m_constantBufferView.range = alloc_info.allocationSize;
595 this->m_constantBufferView.offset = 0;
596 this->m_constantBufferView.mem = m_constantBufferMem;
597 this->m_constantBufferView.format.channelFormat = XGL_CH_FMT_R32G32B32A32;
598 this->m_constantBufferView.format.numericFormat = XGL_NUM_FMT_FLOAT;
599 this->m_constantBufferView.state = XGL_MEMORY_STATE_DATA_TRANSFER;
600}
Tony Barbour82c39522014-12-04 14:33:33 -0700601
Tony Barbour07e80dc2014-12-03 16:23:43 -0700602XglConstantBufferObj::~XglConstantBufferObj()
603{
604 if (m_constantBufferMem != XGL_NULL_HANDLE) xglFreeMemory(m_constantBufferMem);
Tony Barbour38422802014-12-10 14:36:31 -0700605 if (m_fence != XGL_NULL_HANDLE) xglDestroyObject(m_fence);
Tony Barbour07e80dc2014-12-03 16:23:43 -0700606}
Tony Barboure2c58df2014-11-25 13:18:32 -0700607
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700608void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, XGL_UINT binding)
609{
610 xglCmdBindVertexData(cmdBuffer, this->m_constantBufferMem, offset, binding);
611}
612
613
Tony Barbour099a9eb2014-12-10 17:40:15 -0700614void XglConstantBufferObj::SetMemoryState(XGL_MEMORY_STATE newState)
Tony Barboure2c58df2014-11-25 13:18:32 -0700615{
Tony Barbour38422802014-12-10 14:36:31 -0700616 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700617
Tony Barboure2c58df2014-11-25 13:18:32 -0700618 if (this->m_constantBufferView.state == newState)
619 return;
620
Tony Barbour38422802014-12-10 14:36:31 -0700621 if (!m_commandBuffer)
622 {
623 XGL_FENCE_CREATE_INFO fence_info;
624 memset(&fence_info, 0, sizeof(fence_info));
625 fence_info.sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO;
626 err = xglCreateFence(m_device->device(), &fence_info, &m_fence);
627
628 m_commandBuffer = new XglCommandBufferObj(m_device);
629
630 }
631 else
632 {
633 err = xglWaitForFences(m_device->device(), 1, &m_fence, XGL_TRUE, 0);
634 }
635
Tony Barboure2c58df2014-11-25 13:18:32 -0700636 // open the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700637 err = m_commandBuffer->BeginCommandBuffer(0);
Tony Barboure2c58df2014-11-25 13:18:32 -0700638 ASSERT_XGL_SUCCESS(err);
639
640 XGL_MEMORY_STATE_TRANSITION transition = {};
641 transition.mem = m_constantBufferMem;
642 transition.oldState = XGL_MEMORY_STATE_DATA_TRANSFER;
643 transition.newState = newState;
644 transition.offset = 0;
645 transition.regionSize = m_numVertices * m_stride;
646
647 // write transition to the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700648 m_commandBuffer->PrepareMemoryRegions(1, &transition);
Tony Barboure2c58df2014-11-25 13:18:32 -0700649 this->m_constantBufferView.state = newState;
650
651 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700652 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700653 ASSERT_XGL_SUCCESS(err);
654
655 XGL_UINT32 numMemRefs=1;
656 XGL_MEMORY_REF memRefs;
657 // this command buffer only uses the vertex buffer memory
658 memRefs.flags = 0;
659 memRefs.mem = m_constantBufferMem;
660
661 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700662 XGL_CMD_BUFFER bufferArray[1];
663 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Tony Barbour38422802014-12-10 14:36:31 -0700664 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence );
Tony Barboure2c58df2014-11-25 13:18:32 -0700665 ASSERT_XGL_SUCCESS(err);
666}
667
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700668XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
669 : XglConstantBufferObj(device)
670{
671
672}
673
674void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
675{
676 XGL_RESULT err = XGL_SUCCESS;
677 XGL_UINT8 *pData;
678 XGL_MEMORY_ALLOC_INFO alloc_info = {};
679 XGL_FORMAT viewFormat;
680
681 m_numVertices = numIndexes;
682 m_indexType = indexType;
683 viewFormat.numericFormat = XGL_NUM_FMT_UINT;
684 switch (indexType) {
685 case XGL_INDEX_8:
686 m_stride = 1;
687 viewFormat.channelFormat = XGL_CH_FMT_R8;
688 break;
689 case XGL_INDEX_16:
690 m_stride = 2;
691 viewFormat.channelFormat = XGL_CH_FMT_R16;
692 break;
693 case XGL_INDEX_32:
694 m_stride = 4;
695 viewFormat.channelFormat = XGL_CH_FMT_R32;
696 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800697 default:
698 assert(!"unknown index type");
699 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700700 }
701
702 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
703 alloc_info.allocationSize = numIndexes * m_stride;
704 alloc_info.alignment = 0;
705 alloc_info.heapCount = 1;
706 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
707
708 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
709 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
710
711 err = xglAllocMemory(m_device->device(), &alloc_info, &m_constantBufferMem);
712 ASSERT_XGL_SUCCESS(err);
713
714 err = xglMapMemory(m_constantBufferMem, 0, (XGL_VOID **) &pData);
715 ASSERT_XGL_SUCCESS(err);
716
717 memcpy(pData, data, alloc_info.allocationSize);
718
719 err = xglUnmapMemory(m_constantBufferMem);
720 ASSERT_XGL_SUCCESS(err);
721
722 // set up the memory view for the constant buffer
723 this->m_constantBufferView.stride = m_stride;
724 this->m_constantBufferView.range = alloc_info.allocationSize;
725 this->m_constantBufferView.offset = 0;
726 this->m_constantBufferView.mem = m_constantBufferMem;
727 this->m_constantBufferView.format.channelFormat = viewFormat.channelFormat;
728 this->m_constantBufferView.format.numericFormat = viewFormat.numericFormat;
729 this->m_constantBufferView.state = XGL_MEMORY_STATE_DATA_TRANSFER;
730}
731
732void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
733{
734 xglCmdBindIndexData(cmdBuffer, this->m_constantBufferMem, offset, m_indexType);
735}
Tony Barboure2c58df2014-11-25 13:18:32 -0700736
Tony Barbouraf1f9192014-12-17 10:57:58 -0700737XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
738{
739 return m_indexType;
740}
741
Tony Barbour5420af02014-12-03 13:58:15 -0700742XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo(XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700743{
744 XGL_DESCRIPTOR_SLOT_INFO *slotInfo;
745 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
746 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
747 stageInfo->shader.stage = m_stage;
748 stageInfo->shader.shader = m_shader;
749 stageInfo->shader.descriptorSetMapping[0].descriptorCount = 0;
750 stageInfo->shader.linkConstBufferCount = 0;
751 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
752 stageInfo->shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
753 stageInfo->shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
754
755 stageInfo->shader.descriptorSetMapping[0].descriptorCount = m_memSlots.size() + m_imageSlots.size() + m_samplerSlots.size();
756 if (stageInfo->shader.descriptorSetMapping[0].descriptorCount)
757 {
758 vector<int> allSlots;
759 vector<XGL_DESCRIPTOR_SET_SLOT_TYPE> allTypes;
760 vector<XGL_OBJECT> allObjs;
761
762 allSlots.reserve(m_memSlots.size() + m_imageSlots.size() + m_samplerSlots.size());
763 allTypes.reserve(m_memTypes.size() + m_imageTypes.size() + m_samplerTypes.size());
764 allObjs.reserve(m_memObjs.size() + m_imageObjs.size() + m_samplerObjs.size());
765
766 if (m_memSlots.size())
767 {
768 allSlots.insert(allSlots.end(), m_memSlots.begin(), m_memSlots.end());
769 allTypes.insert(allTypes.end(), m_memTypes.begin(), m_memTypes.end());
770 allObjs.insert(allObjs.end(), m_memObjs.begin(), m_memObjs.end());
771 }
772 if (m_imageSlots.size())
773 {
774 allSlots.insert(allSlots.end(), m_imageSlots.begin(), m_imageSlots.end());
775 allTypes.insert(allTypes.end(), m_imageTypes.begin(), m_imageTypes.end());
776 allObjs.insert(allObjs.end(), m_imageObjs.begin(), m_imageObjs.end());
777 }
778 if (m_samplerSlots.size())
779 {
780 allSlots.insert(allSlots.end(), m_samplerSlots.begin(), m_samplerSlots.end());
781 allTypes.insert(allTypes.end(), m_samplerTypes.begin(), m_samplerTypes.end());
782 allObjs.insert(allObjs.end(), m_samplerObjs.begin(), m_samplerObjs.end());
783 }
784
Tony Barbour5420af02014-12-03 13:58:15 -0700785 slotInfo = descriptorSet->GetSlotInfo(allSlots, allTypes, allObjs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700786 stageInfo->shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
787 }
788 return stageInfo;
789}
790
791void XglShaderObj::BindShaderEntitySlotToMemory(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglConstantBufferObj *constantBuffer)
792{
793 m_memSlots.push_back(slot);
794 m_memTypes.push_back(type);
795 m_memObjs.push_back((XGL_OBJECT) &constantBuffer->m_constantBufferView);
796
797}
Tony Barbour82c39522014-12-04 14:33:33 -0700798
Tony Barboure2c58df2014-11-25 13:18:32 -0700799void XglShaderObj::BindShaderEntitySlotToImage(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglTextureObj *texture)
800{
801 m_imageSlots.push_back(slot);
802 m_imageTypes.push_back(type);
803 m_imageObjs.push_back((XGL_OBJECT) &texture->m_textureViewInfo);
804
805}
Tony Barbour82c39522014-12-04 14:33:33 -0700806
Tony Barboure2c58df2014-11-25 13:18:32 -0700807void XglShaderObj::BindShaderEntitySlotToSampler(int slot, XglSamplerObj *sampler)
808{
809 m_samplerSlots.push_back(slot);
810 m_samplerTypes.push_back(XGL_SLOT_SHADER_SAMPLER);
811 m_samplerObjs.push_back(sampler->m_sampler);
812
813}
Tony Barbour82c39522014-12-04 14:33:33 -0700814
Tony Barboure2c58df2014-11-25 13:18:32 -0700815XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
816{
817 XGL_RESULT err = XGL_SUCCESS;
818 std::vector<unsigned int> bil;
819 XGL_SHADER_CREATE_INFO createInfo;
820 size_t shader_len;
821
822 m_stage = stage;
823 m_device = device;
824
825 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
826 createInfo.pNext = NULL;
827
828 if (!framework->m_use_bil) {
829
830 shader_len = strlen(shader_code);
831 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
832 createInfo.pCode = malloc(createInfo.codeSize);
833 createInfo.flags = 0;
834
835 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
836 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
837 ((uint32_t *) createInfo.pCode)[1] = 0;
838 ((uint32_t *) createInfo.pCode)[2] = stage;
839 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
840
841 err = xglCreateShader(m_device->device(), &createInfo, &m_shader);
842 if (err) {
843 free((void *) createInfo.pCode);
844 }
845 }
846
847 if (framework->m_use_bil || err) {
848 std::vector<unsigned int> bil;
849 err = XGL_SUCCESS;
850
851 // Use Reference GLSL to BIL compiler
852 framework->GLSLtoBIL(stage, shader_code, bil);
853 createInfo.pCode = bil.data();
854 createInfo.codeSize = bil.size() * sizeof(unsigned int);
855 createInfo.flags = 0;
856 err = xglCreateShader(m_device->device(), &createInfo, &m_shader);
Tony Barbourba2a1062014-12-03 09:24:05 -0700857 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700858 }
859}
Tony Barbour82c39522014-12-04 14:33:33 -0700860
Tony Barbourf325bf12014-12-03 15:59:38 -0700861XglShaderObj::~XglShaderObj()
862{
863 if (m_shader != XGL_NULL_HANDLE) xglDestroyObject(m_shader);
864}
Tony Barbour82c39522014-12-04 14:33:33 -0700865
Tony Barboure2c58df2014-11-25 13:18:32 -0700866XglPipelineObj::XglPipelineObj(XglDevice *device)
867{
Tony Barboure2c58df2014-11-25 13:18:32 -0700868 m_device = device;
869 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
870 m_vertexBufferCount = 0;
871
872 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
873 m_ia_state.pNext = XGL_NULL_HANDLE;
874 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
875 m_ia_state.disableVertexReuse = XGL_FALSE;
876 m_ia_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
877 m_ia_state.primitiveRestartEnable = XGL_FALSE;
878 m_ia_state.primitiveRestartIndex = 0;
879
880 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
881 m_rs_state.pNext = &m_ia_state;
882 m_rs_state.depthClipEnable = XGL_FALSE;
883 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
884 m_rs_state.pointSize = 1.0;
885
Tony Barboure2c58df2014-11-25 13:18:32 -0700886 memset(&m_cb_state,0,sizeof(m_cb_state));
887 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
888 m_cb_state.pNext = &m_rs_state;
889 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
890 m_cb_state.dualSourceBlendEnable = XGL_FALSE;
891 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
892
Chia-I Wu62bf1dc2014-12-05 11:12:13 +0800893 m_cb_state.attachment[0].blendEnable = XGL_FALSE;
894 m_cb_state.attachment[0].format.channelFormat = XGL_CH_FMT_R8G8B8A8;
895 m_cb_state.attachment[0].format.numericFormat = XGL_NUM_FMT_UNORM;
896 m_cb_state.attachment[0].channelWriteMask = 0xF;
Tony Barboure2c58df2014-11-25 13:18:32 -0700897
898 m_db_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
899 m_db_state.pNext = &m_cb_state,
900 m_db_state.format.channelFormat = XGL_CH_FMT_R32;
901 m_db_state.format.numericFormat = XGL_NUM_FMT_DS;
902
903
904};
905
906void XglPipelineObj::AddShader(XglShaderObj* shader)
907{
908 m_shaderObjs.push_back(shader);
909}
910
911void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
912{
913 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
914 m_vi_state.attributeCount = count;
915}
916
917void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
918{
919 m_vi_state.pVertexBindingDescriptions = vi_binding;
920 m_vi_state.bindingCount = count;
921}
922
923void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
924{
925 m_vertexBufferObjs.push_back(vertexDataBuffer);
926 m_vertexBufferBindings.push_back(binding);
927 m_vertexBufferCount++;
928}
929
Chia-I Wuecebf752014-12-05 10:45:15 +0800930void XglPipelineObj::SetColorAttachment(XGL_UINT binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
931{
932 m_cb_state.attachment[binding] = *att;
933}
934
Tony Barbour976e1cf2014-12-17 11:57:31 -0700935void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
936{
937 XGL_RESULT err;
938 XGL_VOID* head_ptr = &m_db_state;
939 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
940
941 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
942
943 for (int i=0; i<m_shaderObjs.size(); i++)
944 {
945 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet);
946 shaderCreateInfo->pNext = head_ptr;
947 head_ptr = shaderCreateInfo;
948 }
949
950 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
951 {
952 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
953 m_vi_state.pNext = head_ptr;
954 head_ptr = &m_vi_state;
955 }
956
957 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
958 info.pNext = head_ptr;
959 info.flags = 0;
960
961 err = xglCreateGraphicsPipeline(m_device->device(), &info, &m_pipeline);
962 assert(!err);
963
964 err = m_device->AllocAndBindGpuMemory(m_pipeline, "Pipeline", &m_pipe_mem);
965 assert(!err);
966}
967XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
968{
969 return m_pipeline;
970}
971
Tony Barbour5420af02014-12-03 13:58:15 -0700972void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700973{
974 XGL_RESULT err;
975 XGL_VOID* head_ptr = &m_db_state;
976 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
977
978 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700979
980 for (int i=0; i<m_shaderObjs.size(); i++)
981 {
982 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet);
983 shaderCreateInfo->pNext = head_ptr;
984 head_ptr = shaderCreateInfo;
985 }
986
987 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
988 {
989 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
990 m_vi_state.pNext = head_ptr;
991 head_ptr = &m_vi_state;
992 }
993
994 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
995 info.pNext = head_ptr;
996 info.flags = 0;
997
998 err = xglCreateGraphicsPipeline(m_device->device(), &info, &m_pipeline);
Tony Barbourba2a1062014-12-03 09:24:05 -0700999 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001000
1001 err = m_device->AllocAndBindGpuMemory(m_pipeline, "Pipeline", &m_pipe_mem);
Tony Barbourba2a1062014-12-03 09:24:05 -07001002 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001003
1004 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, m_pipeline );
1005
1006
1007 for (int i=0; i < m_vertexBufferCount; i++)
1008 {
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -07001009 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, m_vertexBufferObjs[i]->m_constantBufferView.offset, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001010 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001011}
Tony Barbour82c39522014-12-04 14:33:33 -07001012
Tony Barbourf325bf12014-12-03 15:59:38 -07001013XglPipelineObj::~XglPipelineObj()
1014{
1015 if (m_pipeline != XGL_NULL_HANDLE) xglDestroyObject(m_pipeline);
1016}
Tony Barboure2c58df2014-11-25 13:18:32 -07001017
1018XglMemoryRefManager::XglMemoryRefManager() {
1019
1020}
Tony Barbour82c39522014-12-04 14:33:33 -07001021
Tony Barboure2c58df2014-11-25 13:18:32 -07001022void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
1023 m_bufferObjs.push_back(&constantBuffer->m_constantBufferMem);
1024}
Tony Barbour82c39522014-12-04 14:33:33 -07001025
Tony Barboure2c58df2014-11-25 13:18:32 -07001026void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
1027 m_bufferObjs.push_back(&texture->m_textureMem);
1028}
Tony Barbour82c39522014-12-04 14:33:33 -07001029
Tony Barboure2c58df2014-11-25 13:18:32 -07001030XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
1031
1032 XGL_MEMORY_REF *localRefs;
1033 XGL_UINT32 numRefs=m_bufferObjs.size();
1034
1035 if (numRefs <= 0)
1036 return NULL;
1037
1038 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
1039 for (int i=0; i<numRefs; i++)
1040 {
1041 localRefs[i].flags = 0;
1042 localRefs[i].mem = *m_bufferObjs[i];
1043 }
1044 return localRefs;
1045}
1046int XglMemoryRefManager::GetNumRefs() {
1047 return m_bufferObjs.size();
1048}
Tony Barbour6d047bf2014-12-10 14:34:45 -07001049
1050XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
1051{
1052 XGL_RESULT err;
1053
1054 memset(&m_cmdInfo,0,sizeof(m_cmdInfo));
1055
1056 m_device = device;
1057 m_cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
1058 m_cmdInfo.queueType = XGL_QUEUE_TYPE_GRAPHICS;
1059 err = xglCreateCommandBuffer(m_device->device(), &m_cmdInfo, &m_cmdBuffer);
1060 assert(!err);
1061}
Tony Barbour471338d2014-12-10 17:28:39 -07001062
Tony Barbour6d047bf2014-12-10 14:34:45 -07001063XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1064{
1065 return m_cmdBuffer;
1066}
Tony Barbour471338d2014-12-10 17:28:39 -07001067
1068XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_FLAGS flags)
1069{
1070 return xglBeginCommandBuffer(m_cmdBuffer, flags);
1071}
1072
1073XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1074{
1075 return xglEndCommandBuffer(m_cmdBuffer);
1076}
1077
1078void XglCommandBufferObj::PrepareMemoryRegions(int transitionCount, XGL_MEMORY_STATE_TRANSITION *transitionPtr)
1079{
1080 xglCmdPrepareMemoryRegions(m_cmdBuffer, transitionCount, transitionPtr);
1081}
1082
Tony Barbour6d047bf2014-12-10 14:34:45 -07001083XglCommandBufferObj::~XglCommandBufferObj()
1084{
1085 if (m_cmdBuffer != XGL_NULL_HANDLE) xglDestroyObject(m_cmdBuffer);
1086}