blob: c5f477aa1dbf3290dbd0cff4b8138f1302801021 [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 Barbour5420af02014-12-03 13:58:15 -0700737XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo(XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700738{
739 XGL_DESCRIPTOR_SLOT_INFO *slotInfo;
740 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
741 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
742 stageInfo->shader.stage = m_stage;
743 stageInfo->shader.shader = m_shader;
744 stageInfo->shader.descriptorSetMapping[0].descriptorCount = 0;
745 stageInfo->shader.linkConstBufferCount = 0;
746 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
747 stageInfo->shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
748 stageInfo->shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
749
750 stageInfo->shader.descriptorSetMapping[0].descriptorCount = m_memSlots.size() + m_imageSlots.size() + m_samplerSlots.size();
751 if (stageInfo->shader.descriptorSetMapping[0].descriptorCount)
752 {
753 vector<int> allSlots;
754 vector<XGL_DESCRIPTOR_SET_SLOT_TYPE> allTypes;
755 vector<XGL_OBJECT> allObjs;
756
757 allSlots.reserve(m_memSlots.size() + m_imageSlots.size() + m_samplerSlots.size());
758 allTypes.reserve(m_memTypes.size() + m_imageTypes.size() + m_samplerTypes.size());
759 allObjs.reserve(m_memObjs.size() + m_imageObjs.size() + m_samplerObjs.size());
760
761 if (m_memSlots.size())
762 {
763 allSlots.insert(allSlots.end(), m_memSlots.begin(), m_memSlots.end());
764 allTypes.insert(allTypes.end(), m_memTypes.begin(), m_memTypes.end());
765 allObjs.insert(allObjs.end(), m_memObjs.begin(), m_memObjs.end());
766 }
767 if (m_imageSlots.size())
768 {
769 allSlots.insert(allSlots.end(), m_imageSlots.begin(), m_imageSlots.end());
770 allTypes.insert(allTypes.end(), m_imageTypes.begin(), m_imageTypes.end());
771 allObjs.insert(allObjs.end(), m_imageObjs.begin(), m_imageObjs.end());
772 }
773 if (m_samplerSlots.size())
774 {
775 allSlots.insert(allSlots.end(), m_samplerSlots.begin(), m_samplerSlots.end());
776 allTypes.insert(allTypes.end(), m_samplerTypes.begin(), m_samplerTypes.end());
777 allObjs.insert(allObjs.end(), m_samplerObjs.begin(), m_samplerObjs.end());
778 }
779
Tony Barbour5420af02014-12-03 13:58:15 -0700780 slotInfo = descriptorSet->GetSlotInfo(allSlots, allTypes, allObjs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700781 stageInfo->shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
782 }
783 return stageInfo;
784}
785
786void XglShaderObj::BindShaderEntitySlotToMemory(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglConstantBufferObj *constantBuffer)
787{
788 m_memSlots.push_back(slot);
789 m_memTypes.push_back(type);
790 m_memObjs.push_back((XGL_OBJECT) &constantBuffer->m_constantBufferView);
791
792}
Tony Barbour82c39522014-12-04 14:33:33 -0700793
Tony Barboure2c58df2014-11-25 13:18:32 -0700794void XglShaderObj::BindShaderEntitySlotToImage(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglTextureObj *texture)
795{
796 m_imageSlots.push_back(slot);
797 m_imageTypes.push_back(type);
798 m_imageObjs.push_back((XGL_OBJECT) &texture->m_textureViewInfo);
799
800}
Tony Barbour82c39522014-12-04 14:33:33 -0700801
Tony Barboure2c58df2014-11-25 13:18:32 -0700802void XglShaderObj::BindShaderEntitySlotToSampler(int slot, XglSamplerObj *sampler)
803{
804 m_samplerSlots.push_back(slot);
805 m_samplerTypes.push_back(XGL_SLOT_SHADER_SAMPLER);
806 m_samplerObjs.push_back(sampler->m_sampler);
807
808}
Tony Barbour82c39522014-12-04 14:33:33 -0700809
Tony Barboure2c58df2014-11-25 13:18:32 -0700810XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
811{
812 XGL_RESULT err = XGL_SUCCESS;
813 std::vector<unsigned int> bil;
814 XGL_SHADER_CREATE_INFO createInfo;
815 size_t shader_len;
816
817 m_stage = stage;
818 m_device = device;
819
820 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
821 createInfo.pNext = NULL;
822
823 if (!framework->m_use_bil) {
824
825 shader_len = strlen(shader_code);
826 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
827 createInfo.pCode = malloc(createInfo.codeSize);
828 createInfo.flags = 0;
829
830 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
831 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
832 ((uint32_t *) createInfo.pCode)[1] = 0;
833 ((uint32_t *) createInfo.pCode)[2] = stage;
834 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
835
836 err = xglCreateShader(m_device->device(), &createInfo, &m_shader);
837 if (err) {
838 free((void *) createInfo.pCode);
839 }
840 }
841
842 if (framework->m_use_bil || err) {
843 std::vector<unsigned int> bil;
844 err = XGL_SUCCESS;
845
846 // Use Reference GLSL to BIL compiler
847 framework->GLSLtoBIL(stage, shader_code, bil);
848 createInfo.pCode = bil.data();
849 createInfo.codeSize = bil.size() * sizeof(unsigned int);
850 createInfo.flags = 0;
851 err = xglCreateShader(m_device->device(), &createInfo, &m_shader);
Tony Barbourba2a1062014-12-03 09:24:05 -0700852 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700853 }
854}
Tony Barbour82c39522014-12-04 14:33:33 -0700855
Tony Barbourf325bf12014-12-03 15:59:38 -0700856XglShaderObj::~XglShaderObj()
857{
858 if (m_shader != XGL_NULL_HANDLE) xglDestroyObject(m_shader);
859}
Tony Barbour82c39522014-12-04 14:33:33 -0700860
Tony Barboure2c58df2014-11-25 13:18:32 -0700861XglPipelineObj::XglPipelineObj(XglDevice *device)
862{
Tony Barboure2c58df2014-11-25 13:18:32 -0700863 m_device = device;
864 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
865 m_vertexBufferCount = 0;
866
867 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
868 m_ia_state.pNext = XGL_NULL_HANDLE;
869 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
870 m_ia_state.disableVertexReuse = XGL_FALSE;
871 m_ia_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
872 m_ia_state.primitiveRestartEnable = XGL_FALSE;
873 m_ia_state.primitiveRestartIndex = 0;
874
875 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
876 m_rs_state.pNext = &m_ia_state;
877 m_rs_state.depthClipEnable = XGL_FALSE;
878 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
879 m_rs_state.pointSize = 1.0;
880
Tony Barboure2c58df2014-11-25 13:18:32 -0700881 memset(&m_cb_state,0,sizeof(m_cb_state));
882 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
883 m_cb_state.pNext = &m_rs_state;
884 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
885 m_cb_state.dualSourceBlendEnable = XGL_FALSE;
886 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
887
Chia-I Wu62bf1dc2014-12-05 11:12:13 +0800888 m_cb_state.attachment[0].blendEnable = XGL_FALSE;
889 m_cb_state.attachment[0].format.channelFormat = XGL_CH_FMT_R8G8B8A8;
890 m_cb_state.attachment[0].format.numericFormat = XGL_NUM_FMT_UNORM;
891 m_cb_state.attachment[0].channelWriteMask = 0xF;
Tony Barboure2c58df2014-11-25 13:18:32 -0700892
893 m_db_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
894 m_db_state.pNext = &m_cb_state,
895 m_db_state.format.channelFormat = XGL_CH_FMT_R32;
896 m_db_state.format.numericFormat = XGL_NUM_FMT_DS;
897
898
899};
900
901void XglPipelineObj::AddShader(XglShaderObj* shader)
902{
903 m_shaderObjs.push_back(shader);
904}
905
906void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
907{
908 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
909 m_vi_state.attributeCount = count;
910}
911
912void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
913{
914 m_vi_state.pVertexBindingDescriptions = vi_binding;
915 m_vi_state.bindingCount = count;
916}
917
918void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
919{
920 m_vertexBufferObjs.push_back(vertexDataBuffer);
921 m_vertexBufferBindings.push_back(binding);
922 m_vertexBufferCount++;
923}
924
Chia-I Wuecebf752014-12-05 10:45:15 +0800925void XglPipelineObj::SetColorAttachment(XGL_UINT binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
926{
927 m_cb_state.attachment[binding] = *att;
928}
929
Tony Barbour976e1cf2014-12-17 11:57:31 -0700930void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
931{
932 XGL_RESULT err;
933 XGL_VOID* head_ptr = &m_db_state;
934 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
935
936 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
937
938 for (int i=0; i<m_shaderObjs.size(); i++)
939 {
940 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet);
941 shaderCreateInfo->pNext = head_ptr;
942 head_ptr = shaderCreateInfo;
943 }
944
945 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
946 {
947 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
948 m_vi_state.pNext = head_ptr;
949 head_ptr = &m_vi_state;
950 }
951
952 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
953 info.pNext = head_ptr;
954 info.flags = 0;
955
956 err = xglCreateGraphicsPipeline(m_device->device(), &info, &m_pipeline);
957 assert(!err);
958
959 err = m_device->AllocAndBindGpuMemory(m_pipeline, "Pipeline", &m_pipe_mem);
960 assert(!err);
961}
962XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
963{
964 return m_pipeline;
965}
966
Tony Barbour5420af02014-12-03 13:58:15 -0700967void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700968{
969 XGL_RESULT err;
970 XGL_VOID* head_ptr = &m_db_state;
971 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
972
973 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -0700974
975 for (int i=0; i<m_shaderObjs.size(); i++)
976 {
977 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet);
978 shaderCreateInfo->pNext = head_ptr;
979 head_ptr = shaderCreateInfo;
980 }
981
982 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
983 {
984 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
985 m_vi_state.pNext = head_ptr;
986 head_ptr = &m_vi_state;
987 }
988
989 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
990 info.pNext = head_ptr;
991 info.flags = 0;
992
993 err = xglCreateGraphicsPipeline(m_device->device(), &info, &m_pipeline);
Tony Barbourba2a1062014-12-03 09:24:05 -0700994 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700995
996 err = m_device->AllocAndBindGpuMemory(m_pipeline, "Pipeline", &m_pipe_mem);
Tony Barbourba2a1062014-12-03 09:24:05 -0700997 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700998
999 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, m_pipeline );
1000
1001
1002 for (int i=0; i < m_vertexBufferCount; i++)
1003 {
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -07001004 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, m_vertexBufferObjs[i]->m_constantBufferView.offset, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001005 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001006}
Tony Barbour82c39522014-12-04 14:33:33 -07001007
Tony Barbourf325bf12014-12-03 15:59:38 -07001008XglPipelineObj::~XglPipelineObj()
1009{
1010 if (m_pipeline != XGL_NULL_HANDLE) xglDestroyObject(m_pipeline);
1011}
Tony Barboure2c58df2014-11-25 13:18:32 -07001012
1013XglMemoryRefManager::XglMemoryRefManager() {
1014
1015}
Tony Barbour82c39522014-12-04 14:33:33 -07001016
Tony Barboure2c58df2014-11-25 13:18:32 -07001017void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
1018 m_bufferObjs.push_back(&constantBuffer->m_constantBufferMem);
1019}
Tony Barbour82c39522014-12-04 14:33:33 -07001020
Tony Barboure2c58df2014-11-25 13:18:32 -07001021void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
1022 m_bufferObjs.push_back(&texture->m_textureMem);
1023}
Tony Barbour82c39522014-12-04 14:33:33 -07001024
Tony Barboure2c58df2014-11-25 13:18:32 -07001025XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
1026
1027 XGL_MEMORY_REF *localRefs;
1028 XGL_UINT32 numRefs=m_bufferObjs.size();
1029
1030 if (numRefs <= 0)
1031 return NULL;
1032
1033 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
1034 for (int i=0; i<numRefs; i++)
1035 {
1036 localRefs[i].flags = 0;
1037 localRefs[i].mem = *m_bufferObjs[i];
1038 }
1039 return localRefs;
1040}
1041int XglMemoryRefManager::GetNumRefs() {
1042 return m_bufferObjs.size();
1043}
Tony Barbour6d047bf2014-12-10 14:34:45 -07001044
1045XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
1046{
1047 XGL_RESULT err;
1048
1049 memset(&m_cmdInfo,0,sizeof(m_cmdInfo));
1050
1051 m_device = device;
1052 m_cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
1053 m_cmdInfo.queueType = XGL_QUEUE_TYPE_GRAPHICS;
1054 err = xglCreateCommandBuffer(m_device->device(), &m_cmdInfo, &m_cmdBuffer);
1055 assert(!err);
1056}
Tony Barbour471338d2014-12-10 17:28:39 -07001057
Tony Barbour6d047bf2014-12-10 14:34:45 -07001058XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1059{
1060 return m_cmdBuffer;
1061}
Tony Barbour471338d2014-12-10 17:28:39 -07001062
1063XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_FLAGS flags)
1064{
1065 return xglBeginCommandBuffer(m_cmdBuffer, flags);
1066}
1067
1068XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1069{
1070 return xglEndCommandBuffer(m_cmdBuffer);
1071}
1072
1073void XglCommandBufferObj::PrepareMemoryRegions(int transitionCount, XGL_MEMORY_STATE_TRANSITION *transitionPtr)
1074{
1075 xglCmdPrepareMemoryRegions(m_cmdBuffer, transitionCount, transitionPtr);
1076}
1077
Tony Barbour6d047bf2014-12-10 14:34:45 -07001078XglCommandBufferObj::~XglCommandBufferObj()
1079{
1080 if (m_cmdBuffer != XGL_NULL_HANDLE) xglDestroyObject(m_cmdBuffer);
1081}