blob: d978fff7ebcd2a76db88841e831b0cd3605f2b1a [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}
Tony Barbourb5f4d082014-12-17 10:54:03 -0700329void XglDescriptorSetObj::CreateXGLDescriptorSet()
330{
331 XGL_RESULT err;
332
333 // Create descriptor set for a uniform resource
334 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
335 m_descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
336 m_descriptorInfo.slots = m_nextSlot;
337
338 // Create a descriptor set with requested number of slots
339 err = xglCreateDescriptorSet( m_device->device(), &m_descriptorInfo, &m_rsrcDescSet );
340 ASSERT_XGL_SUCCESS(err);
341
342 // Bind memory to the descriptor set
343 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
344 ASSERT_XGL_SUCCESS(err);
345
346 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
347 xglClearDescriptorSetSlots(m_rsrcDescSet, 0, m_nextSlot);
348 for (int i=0; i<m_memoryViews.size();i++)
349 {
350 xglAttachMemoryViewDescriptors( m_rsrcDescSet, m_memorySlots[i], 1, m_memoryViews[i] );
351 }
352 for (int i=0; i<m_samplers.size();i++)
353 {
354 xglAttachSamplerDescriptors( m_rsrcDescSet, m_samplerSlots[i], 1, m_samplers[i] );
355 }
356 for (int i=0; i<m_imageViews.size();i++)
357 {
358 xglAttachImageViewDescriptors( m_rsrcDescSet, m_imageSlots[i], 1, m_imageViews[i] );
359 }
360 xglEndDescriptorSetUpdate( m_rsrcDescSet );
361}
362
363XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle()
364{
365 return m_rsrcDescSet;
366}
Tony Barboure2c58df2014-11-25 13:18:32 -0700367
368void XglDescriptorSetObj::BindCommandBuffer(XGL_CMD_BUFFER commandBuffer)
369{
370 XGL_RESULT err;
371
372 // Create descriptor set for a uniform resource
373 memset(&m_descriptorInfo,0,sizeof(m_descriptorInfo));
374 m_descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
375 m_descriptorInfo.slots = m_nextSlot;
376
377 // Create a descriptor set with requested number of slots
378 err = xglCreateDescriptorSet( m_device->device(), &m_descriptorInfo, &m_rsrcDescSet );
Tony Barbourba2a1062014-12-03 09:24:05 -0700379 ASSERT_XGL_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700380
381 // Bind memory to the descriptor set
382 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
Tony Barbourba2a1062014-12-03 09:24:05 -0700383 ASSERT_XGL_SUCCESS(err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700384
385 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
386 xglClearDescriptorSetSlots(m_rsrcDescSet, 0, m_nextSlot);
387 for (int i=0; i<m_memoryViews.size();i++)
388 {
389 xglAttachMemoryViewDescriptors( m_rsrcDescSet, m_memorySlots[i], 1, m_memoryViews[i] );
390 }
391 for (int i=0; i<m_samplers.size();i++)
392 {
393 xglAttachSamplerDescriptors( m_rsrcDescSet, m_samplerSlots[i], 1, m_samplers[i] );
394 }
395 for (int i=0; i<m_imageViews.size();i++)
396 {
397 xglAttachImageViewDescriptors( m_rsrcDescSet, m_imageSlots[i], 1, m_imageViews[i] );
398 }
399 xglEndDescriptorSetUpdate( m_rsrcDescSet );
400
401 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic memory view)
402 xglCmdBindDescriptorSet(commandBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
403}
Tony Barbour82c39522014-12-04 14:33:33 -0700404
Tony Barbour25ef8a62014-12-03 13:59:18 -0700405XglDescriptorSetObj::~XglDescriptorSetObj()
406{
407 if (m_rsrcDescSet != XGL_NULL_HANDLE) xglDestroyObject(m_rsrcDescSet);
408}
Tony Barboure2c58df2014-11-25 13:18:32 -0700409
410XglTextureObj::XglTextureObj(XglDevice *device):
411 m_texture(XGL_NULL_HANDLE),
412 m_textureMem(XGL_NULL_HANDLE),
413 m_textureView(XGL_NULL_HANDLE)
414{
415 m_device = device;
416 const XGL_FORMAT tex_format = { XGL_CH_FMT_B8G8R8A8, XGL_NUM_FMT_UNORM };
417 m_texWidth = 16;
418 m_texHeight = 16;
419 const uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 };
420 XGL_RESULT err;
Tony Barboure2c58df2014-11-25 13:18:32 -0700421
422 memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo));
423
424 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
425
426 const XGL_IMAGE_CREATE_INFO image = {
427 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
428 .pNext = NULL,
429 .imageType = XGL_IMAGE_2D,
430 .format = tex_format,
431 .extent = { m_texWidth, m_texHeight, 1 },
432 .mipLevels = 1,
433 .arraySize = 1,
434 .samples = 1,
435 .tiling = XGL_LINEAR_TILING,
436 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
437 .flags = 0,
438 };
439
440 XGL_MEMORY_ALLOC_INFO mem_alloc;
441 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
442 mem_alloc.pNext = NULL;
443 mem_alloc.allocationSize = 0;
444 mem_alloc.alignment = 0;
445 mem_alloc.flags = 0;
446 mem_alloc.heapCount = 0;
447 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
448
449 XGL_IMAGE_VIEW_CREATE_INFO view;
450 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
451 view.pNext = NULL;
452 view.image = XGL_NULL_HANDLE;
453 view.viewType = XGL_IMAGE_VIEW_2D;
454 view.format = image.format;
455 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
456 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
457 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
458 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
459 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
460 view.subresourceRange.baseMipLevel = 0;
461 view.subresourceRange.mipLevels = 1;
462 view.subresourceRange.baseArraySlice = 0;
463 view.subresourceRange.arraySize = 1;
464 view.minLod = 0.0f;
465
466 XGL_MEMORY_REQUIREMENTS mem_reqs;
467 XGL_SIZE mem_reqs_size=sizeof(XGL_MEMORY_REQUIREMENTS);
468
469 /* create image */
470 err = xglCreateImage(m_device->device(), &image, &m_texture);
471 assert(!err);
472
473 err = xglGetObjectInfo(m_texture,
474 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
475 &mem_reqs_size, &mem_reqs);
476 assert(!err && mem_reqs_size == sizeof(mem_reqs));
477
478 mem_alloc.allocationSize = mem_reqs.size;
479 mem_alloc.alignment = mem_reqs.alignment;
480 mem_alloc.heapCount = mem_reqs.heapCount;
481 memcpy(mem_alloc.heaps, mem_reqs.heaps,
482 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
483
484 /* allocate memory */
485 err = xglAllocMemory(m_device->device(), &mem_alloc, &m_textureMem);
486 assert(!err);
487
488 /* bind memory */
489 err = xglBindObjectMemory(m_texture, m_textureMem, 0);
490 assert(!err);
491
492 /* create image view */
493 view.image = m_texture;
494 err = xglCreateImageView(m_device->device(), &view, &m_textureView);
495 assert(!err);
496
497
498 const XGL_IMAGE_SUBRESOURCE subres = {
499 .aspect = XGL_IMAGE_ASPECT_COLOR,
500 .mipLevel = 0,
501 .arraySlice = 0,
502 };
503 XGL_SUBRESOURCE_LAYOUT layout;
504 XGL_SIZE layout_size=sizeof(layout);
505 XGL_VOID *data;
506 XGL_INT x, y;
507
508 err = xglGetImageSubresourceInfo(m_texture, &subres,
509 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
510 assert(!err && layout_size == sizeof(layout));
511 m_rowPitch = layout.rowPitch;
512
513 err = xglMapMemory(m_textureMem, 0, &data);
514 assert(!err);
515
516 for (y = 0; y < m_texHeight; y++) {
517 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
518 for (x = 0; x < m_texWidth; x++)
519 row[x] = tex_colors[(x & 1) ^ (y & 1)];
520 }
521
522 err = xglUnmapMemory(m_textureMem);
523 assert(!err);
524
525 m_textureViewInfo.view = m_textureView;
526
527}
Tony Barbour82c39522014-12-04 14:33:33 -0700528
Tony Barbourf325bf12014-12-03 15:59:38 -0700529XglTextureObj::~XglTextureObj()
530{
531 if (m_texture != XGL_NULL_HANDLE) xglDestroyObject(m_texture);
532}
Tony Barboure2c58df2014-11-25 13:18:32 -0700533
534void XglTextureObj::ChangeColors(uint32_t color1, uint32_t color2)
535{
536 XGL_RESULT err;
537 const uint32_t tex_colors[2] = { color1, color2 };
538 XGL_VOID *data;
539
540 err = xglMapMemory(m_textureMem, 0, &data);
541 assert(!err);
542
543 for (int y = 0; y < m_texHeight; y++) {
544 uint32_t *row = (uint32_t *) ((char *) data + m_rowPitch * y);
545 for (int x = 0; x < m_texWidth; x++)
546 row[x] = tex_colors[(x & 1) ^ (y & 1)];
547 }
548
549 err = xglUnmapMemory(m_textureMem);
550 assert(!err);
551}
552
553XglSamplerObj::XglSamplerObj(XglDevice *device)
554{
555 XGL_RESULT err = XGL_SUCCESS;
556
557 m_device = device;
558 memset(&m_samplerCreateInfo,0,sizeof(m_samplerCreateInfo));
559 m_samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
560 m_samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
561 m_samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
562 m_samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
563 m_samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
564 m_samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
565 m_samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
566 m_samplerCreateInfo.mipLodBias = 0.0;
567 m_samplerCreateInfo.maxAnisotropy = 0.0;
568 m_samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
569 m_samplerCreateInfo.minLod = 0.0;
570 m_samplerCreateInfo.maxLod = 0.0;
571 m_samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
572
573 err = xglCreateSampler(m_device->device(),&m_samplerCreateInfo, &m_sampler);
Tony Barbourba2a1062014-12-03 09:24:05 -0700574 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700575
576}
Tony Barbour82c39522014-12-04 14:33:33 -0700577
Tony Barbourf325bf12014-12-03 15:59:38 -0700578XglSamplerObj::~XglSamplerObj()
579{
580 if (m_sampler != XGL_NULL_HANDLE) xglDestroyObject(m_sampler);
581}
Tony Barboure2c58df2014-11-25 13:18:32 -0700582
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700583/*
584 * Basic ConstantBuffer constructor. Then use create methods to fill in the details.
585 */
586XglConstantBufferObj::XglConstantBufferObj(XglDevice *device)
587{
588 m_device = device;
Tony Barbour38422802014-12-10 14:36:31 -0700589 m_commandBuffer = 0;
590 m_fence = 0;
Courtney Goeltzenleuchter3b0a8152014-12-04 15:18:47 -0700591
592 memset(&m_constantBufferView,0,sizeof(m_constantBufferView));
593 memset(&m_constantBufferMem,0,sizeof(m_constantBufferMem));
594}
595
Tony Barboure2c58df2014-11-25 13:18:32 -0700596XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data)
597{
598 XGL_RESULT err = XGL_SUCCESS;
599 XGL_UINT8 *pData;
600 XGL_MEMORY_ALLOC_INFO alloc_info = {};
601 m_device = device;
602 m_numVertices = constantCount;
603 m_stride = constantSize;
604
605 memset(&m_constantBufferView,0,sizeof(m_constantBufferView));
606 memset(&m_constantBufferMem,0,sizeof(m_constantBufferMem));
Tony Barbour38422802014-12-10 14:36:31 -0700607 m_commandBuffer = 0;
608 m_fence = 0;
Tony Barboure2c58df2014-11-25 13:18:32 -0700609
610 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
611 alloc_info.allocationSize = constantCount * constantSize;
612 alloc_info.alignment = 0;
613 alloc_info.heapCount = 1;
614 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
615
616 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
617 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
618
619 err = xglAllocMemory(m_device->device(), &alloc_info, &m_constantBufferMem);
Tony Barbourba2a1062014-12-03 09:24:05 -0700620 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700621
622 err = xglMapMemory(m_constantBufferMem, 0, (XGL_VOID **) &pData);
Tony Barbourba2a1062014-12-03 09:24:05 -0700623 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700624
625 memcpy(pData, data, alloc_info.allocationSize);
626
627 err = xglUnmapMemory(m_constantBufferMem);
Tony Barbourba2a1062014-12-03 09:24:05 -0700628 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700629
630 // set up the memory view for the constant buffer
631 this->m_constantBufferView.stride = 16;
632 this->m_constantBufferView.range = alloc_info.allocationSize;
633 this->m_constantBufferView.offset = 0;
634 this->m_constantBufferView.mem = m_constantBufferMem;
635 this->m_constantBufferView.format.channelFormat = XGL_CH_FMT_R32G32B32A32;
636 this->m_constantBufferView.format.numericFormat = XGL_NUM_FMT_FLOAT;
637 this->m_constantBufferView.state = XGL_MEMORY_STATE_DATA_TRANSFER;
638}
Tony Barbour82c39522014-12-04 14:33:33 -0700639
Tony Barbour07e80dc2014-12-03 16:23:43 -0700640XglConstantBufferObj::~XglConstantBufferObj()
641{
642 if (m_constantBufferMem != XGL_NULL_HANDLE) xglFreeMemory(m_constantBufferMem);
Tony Barbour38422802014-12-10 14:36:31 -0700643 if (m_fence != XGL_NULL_HANDLE) xglDestroyObject(m_fence);
Tony Barbour07e80dc2014-12-03 16:23:43 -0700644}
Tony Barboure2c58df2014-11-25 13:18:32 -0700645
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -0700646void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, XGL_UINT binding)
647{
648 xglCmdBindVertexData(cmdBuffer, this->m_constantBufferMem, offset, binding);
649}
650
651
Tony Barbour099a9eb2014-12-10 17:40:15 -0700652void XglConstantBufferObj::SetMemoryState(XGL_MEMORY_STATE newState)
Tony Barboure2c58df2014-11-25 13:18:32 -0700653{
Tony Barbour38422802014-12-10 14:36:31 -0700654 XGL_RESULT err = XGL_SUCCESS;
Tony Barbour38422802014-12-10 14:36:31 -0700655
Tony Barboure2c58df2014-11-25 13:18:32 -0700656 if (this->m_constantBufferView.state == newState)
657 return;
658
Tony Barbour38422802014-12-10 14:36:31 -0700659 if (!m_commandBuffer)
660 {
661 XGL_FENCE_CREATE_INFO fence_info;
662 memset(&fence_info, 0, sizeof(fence_info));
663 fence_info.sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO;
664 err = xglCreateFence(m_device->device(), &fence_info, &m_fence);
665
666 m_commandBuffer = new XglCommandBufferObj(m_device);
667
668 }
669 else
670 {
671 err = xglWaitForFences(m_device->device(), 1, &m_fence, XGL_TRUE, 0);
672 }
673
Tony Barboure2c58df2014-11-25 13:18:32 -0700674 // open the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700675 err = m_commandBuffer->BeginCommandBuffer(0);
Tony Barboure2c58df2014-11-25 13:18:32 -0700676 ASSERT_XGL_SUCCESS(err);
677
678 XGL_MEMORY_STATE_TRANSITION transition = {};
679 transition.mem = m_constantBufferMem;
680 transition.oldState = XGL_MEMORY_STATE_DATA_TRANSFER;
681 transition.newState = newState;
682 transition.offset = 0;
683 transition.regionSize = m_numVertices * m_stride;
684
685 // write transition to the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700686 m_commandBuffer->PrepareMemoryRegions(1, &transition);
Tony Barboure2c58df2014-11-25 13:18:32 -0700687 this->m_constantBufferView.state = newState;
688
689 // finish recording the command buffer
Tony Barbour471338d2014-12-10 17:28:39 -0700690 err = m_commandBuffer->EndCommandBuffer();
Tony Barboure2c58df2014-11-25 13:18:32 -0700691 ASSERT_XGL_SUCCESS(err);
692
693 XGL_UINT32 numMemRefs=1;
694 XGL_MEMORY_REF memRefs;
695 // this command buffer only uses the vertex buffer memory
696 memRefs.flags = 0;
697 memRefs.mem = m_constantBufferMem;
698
699 // submit the command buffer to the universal queue
Tony Barbour471338d2014-12-10 17:28:39 -0700700 XGL_CMD_BUFFER bufferArray[1];
701 bufferArray[0] = m_commandBuffer->GetBufferHandle();
Tony Barbour38422802014-12-10 14:36:31 -0700702 err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence );
Tony Barboure2c58df2014-11-25 13:18:32 -0700703 ASSERT_XGL_SUCCESS(err);
704}
705
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700706XglIndexBufferObj::XglIndexBufferObj(XglDevice *device)
707 : XglConstantBufferObj(device)
708{
709
710}
711
712void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data)
713{
714 XGL_RESULT err = XGL_SUCCESS;
715 XGL_UINT8 *pData;
716 XGL_MEMORY_ALLOC_INFO alloc_info = {};
717 XGL_FORMAT viewFormat;
718
719 m_numVertices = numIndexes;
720 m_indexType = indexType;
721 viewFormat.numericFormat = XGL_NUM_FMT_UINT;
722 switch (indexType) {
723 case XGL_INDEX_8:
724 m_stride = 1;
725 viewFormat.channelFormat = XGL_CH_FMT_R8;
726 break;
727 case XGL_INDEX_16:
728 m_stride = 2;
729 viewFormat.channelFormat = XGL_CH_FMT_R16;
730 break;
731 case XGL_INDEX_32:
732 m_stride = 4;
733 viewFormat.channelFormat = XGL_CH_FMT_R32;
734 break;
Chia-I Wub4c2aa42014-12-15 23:50:11 +0800735 default:
736 assert(!"unknown index type");
737 break;
Courtney Goeltzenleuchter8a785932014-12-04 15:24:05 -0700738 }
739
740 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
741 alloc_info.allocationSize = numIndexes * m_stride;
742 alloc_info.alignment = 0;
743 alloc_info.heapCount = 1;
744 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
745
746 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
747 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
748
749 err = xglAllocMemory(m_device->device(), &alloc_info, &m_constantBufferMem);
750 ASSERT_XGL_SUCCESS(err);
751
752 err = xglMapMemory(m_constantBufferMem, 0, (XGL_VOID **) &pData);
753 ASSERT_XGL_SUCCESS(err);
754
755 memcpy(pData, data, alloc_info.allocationSize);
756
757 err = xglUnmapMemory(m_constantBufferMem);
758 ASSERT_XGL_SUCCESS(err);
759
760 // set up the memory view for the constant buffer
761 this->m_constantBufferView.stride = m_stride;
762 this->m_constantBufferView.range = alloc_info.allocationSize;
763 this->m_constantBufferView.offset = 0;
764 this->m_constantBufferView.mem = m_constantBufferMem;
765 this->m_constantBufferView.format.channelFormat = viewFormat.channelFormat;
766 this->m_constantBufferView.format.numericFormat = viewFormat.numericFormat;
767 this->m_constantBufferView.state = XGL_MEMORY_STATE_DATA_TRANSFER;
768}
769
770void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset)
771{
772 xglCmdBindIndexData(cmdBuffer, this->m_constantBufferMem, offset, m_indexType);
773}
Tony Barboure2c58df2014-11-25 13:18:32 -0700774
Tony Barbouraf1f9192014-12-17 10:57:58 -0700775XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType()
776{
777 return m_indexType;
778}
779
Tony Barbour5420af02014-12-03 13:58:15 -0700780XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo(XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -0700781{
782 XGL_DESCRIPTOR_SLOT_INFO *slotInfo;
783 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) );
784 stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
785 stageInfo->shader.stage = m_stage;
786 stageInfo->shader.shader = m_shader;
787 stageInfo->shader.descriptorSetMapping[0].descriptorCount = 0;
788 stageInfo->shader.linkConstBufferCount = 0;
789 stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
790 stageInfo->shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
791 stageInfo->shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
792
793 stageInfo->shader.descriptorSetMapping[0].descriptorCount = m_memSlots.size() + m_imageSlots.size() + m_samplerSlots.size();
794 if (stageInfo->shader.descriptorSetMapping[0].descriptorCount)
795 {
796 vector<int> allSlots;
797 vector<XGL_DESCRIPTOR_SET_SLOT_TYPE> allTypes;
798 vector<XGL_OBJECT> allObjs;
799
800 allSlots.reserve(m_memSlots.size() + m_imageSlots.size() + m_samplerSlots.size());
801 allTypes.reserve(m_memTypes.size() + m_imageTypes.size() + m_samplerTypes.size());
802 allObjs.reserve(m_memObjs.size() + m_imageObjs.size() + m_samplerObjs.size());
803
804 if (m_memSlots.size())
805 {
806 allSlots.insert(allSlots.end(), m_memSlots.begin(), m_memSlots.end());
807 allTypes.insert(allTypes.end(), m_memTypes.begin(), m_memTypes.end());
808 allObjs.insert(allObjs.end(), m_memObjs.begin(), m_memObjs.end());
809 }
810 if (m_imageSlots.size())
811 {
812 allSlots.insert(allSlots.end(), m_imageSlots.begin(), m_imageSlots.end());
813 allTypes.insert(allTypes.end(), m_imageTypes.begin(), m_imageTypes.end());
814 allObjs.insert(allObjs.end(), m_imageObjs.begin(), m_imageObjs.end());
815 }
816 if (m_samplerSlots.size())
817 {
818 allSlots.insert(allSlots.end(), m_samplerSlots.begin(), m_samplerSlots.end());
819 allTypes.insert(allTypes.end(), m_samplerTypes.begin(), m_samplerTypes.end());
820 allObjs.insert(allObjs.end(), m_samplerObjs.begin(), m_samplerObjs.end());
821 }
822
Tony Barbour5420af02014-12-03 13:58:15 -0700823 slotInfo = descriptorSet->GetSlotInfo(allSlots, allTypes, allObjs);
Tony Barboure2c58df2014-11-25 13:18:32 -0700824 stageInfo->shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
825 }
826 return stageInfo;
827}
828
829void XglShaderObj::BindShaderEntitySlotToMemory(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglConstantBufferObj *constantBuffer)
830{
831 m_memSlots.push_back(slot);
832 m_memTypes.push_back(type);
833 m_memObjs.push_back((XGL_OBJECT) &constantBuffer->m_constantBufferView);
834
835}
Tony Barbour82c39522014-12-04 14:33:33 -0700836
Tony Barboure2c58df2014-11-25 13:18:32 -0700837void XglShaderObj::BindShaderEntitySlotToImage(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XglTextureObj *texture)
838{
839 m_imageSlots.push_back(slot);
840 m_imageTypes.push_back(type);
841 m_imageObjs.push_back((XGL_OBJECT) &texture->m_textureViewInfo);
842
843}
Tony Barbour82c39522014-12-04 14:33:33 -0700844
Tony Barboure2c58df2014-11-25 13:18:32 -0700845void XglShaderObj::BindShaderEntitySlotToSampler(int slot, XglSamplerObj *sampler)
846{
847 m_samplerSlots.push_back(slot);
848 m_samplerTypes.push_back(XGL_SLOT_SHADER_SAMPLER);
849 m_samplerObjs.push_back(sampler->m_sampler);
850
851}
Tony Barbour82c39522014-12-04 14:33:33 -0700852
Tony Barboure2c58df2014-11-25 13:18:32 -0700853XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework)
854{
855 XGL_RESULT err = XGL_SUCCESS;
856 std::vector<unsigned int> bil;
857 XGL_SHADER_CREATE_INFO createInfo;
858 size_t shader_len;
859
860 m_stage = stage;
861 m_device = device;
862
863 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
864 createInfo.pNext = NULL;
865
866 if (!framework->m_use_bil) {
867
868 shader_len = strlen(shader_code);
869 createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1;
870 createInfo.pCode = malloc(createInfo.codeSize);
871 createInfo.flags = 0;
872
873 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
874 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
875 ((uint32_t *) createInfo.pCode)[1] = 0;
876 ((uint32_t *) createInfo.pCode)[2] = stage;
877 memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1);
878
879 err = xglCreateShader(m_device->device(), &createInfo, &m_shader);
880 if (err) {
881 free((void *) createInfo.pCode);
882 }
883 }
884
885 if (framework->m_use_bil || err) {
886 std::vector<unsigned int> bil;
887 err = XGL_SUCCESS;
888
889 // Use Reference GLSL to BIL compiler
890 framework->GLSLtoBIL(stage, shader_code, bil);
891 createInfo.pCode = bil.data();
892 createInfo.codeSize = bil.size() * sizeof(unsigned int);
893 createInfo.flags = 0;
894 err = xglCreateShader(m_device->device(), &createInfo, &m_shader);
Tony Barbourba2a1062014-12-03 09:24:05 -0700895 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -0700896 }
897}
Tony Barbour82c39522014-12-04 14:33:33 -0700898
Tony Barbourf325bf12014-12-03 15:59:38 -0700899XglShaderObj::~XglShaderObj()
900{
901 if (m_shader != XGL_NULL_HANDLE) xglDestroyObject(m_shader);
902}
Tony Barbour82c39522014-12-04 14:33:33 -0700903
Tony Barboure2c58df2014-11-25 13:18:32 -0700904XglPipelineObj::XglPipelineObj(XglDevice *device)
905{
Tony Barboure2c58df2014-11-25 13:18:32 -0700906 m_device = device;
907 m_vi_state.attributeCount = m_vi_state.bindingCount = 0;
908 m_vertexBufferCount = 0;
909
910 m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
911 m_ia_state.pNext = XGL_NULL_HANDLE;
912 m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
913 m_ia_state.disableVertexReuse = XGL_FALSE;
914 m_ia_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST;
915 m_ia_state.primitiveRestartEnable = XGL_FALSE;
916 m_ia_state.primitiveRestartIndex = 0;
917
918 m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
919 m_rs_state.pNext = &m_ia_state;
920 m_rs_state.depthClipEnable = XGL_FALSE;
921 m_rs_state.rasterizerDiscardEnable = XGL_FALSE;
922 m_rs_state.pointSize = 1.0;
923
Tony Barboure2c58df2014-11-25 13:18:32 -0700924 memset(&m_cb_state,0,sizeof(m_cb_state));
925 m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
926 m_cb_state.pNext = &m_rs_state;
927 m_cb_state.alphaToCoverageEnable = XGL_FALSE;
928 m_cb_state.dualSourceBlendEnable = XGL_FALSE;
929 m_cb_state.logicOp = XGL_LOGIC_OP_COPY;
930
Chia-I Wu62bf1dc2014-12-05 11:12:13 +0800931 m_cb_state.attachment[0].blendEnable = XGL_FALSE;
932 m_cb_state.attachment[0].format.channelFormat = XGL_CH_FMT_R8G8B8A8;
933 m_cb_state.attachment[0].format.numericFormat = XGL_NUM_FMT_UNORM;
934 m_cb_state.attachment[0].channelWriteMask = 0xF;
Tony Barboure2c58df2014-11-25 13:18:32 -0700935
936 m_db_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
937 m_db_state.pNext = &m_cb_state,
938 m_db_state.format.channelFormat = XGL_CH_FMT_R32;
939 m_db_state.format.numericFormat = XGL_NUM_FMT_DS;
940
941
942};
943
944void XglPipelineObj::AddShader(XglShaderObj* shader)
945{
946 m_shaderObjs.push_back(shader);
947}
948
949void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count)
950{
951 m_vi_state.pVertexAttributeDescriptions = vi_attrib;
952 m_vi_state.attributeCount = count;
953}
954
955void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count)
956{
957 m_vi_state.pVertexBindingDescriptions = vi_binding;
958 m_vi_state.bindingCount = count;
959}
960
961void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding)
962{
963 m_vertexBufferObjs.push_back(vertexDataBuffer);
964 m_vertexBufferBindings.push_back(binding);
965 m_vertexBufferCount++;
966}
967
Chia-I Wuecebf752014-12-05 10:45:15 +0800968void XglPipelineObj::SetColorAttachment(XGL_UINT binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att)
969{
970 m_cb_state.attachment[binding] = *att;
971}
972
Tony Barbour976e1cf2014-12-17 11:57:31 -0700973void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet)
974{
975 XGL_RESULT err;
976 XGL_VOID* head_ptr = &m_db_state;
977 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
978
979 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
980
981 for (int i=0; i<m_shaderObjs.size(); i++)
982 {
983 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet);
984 shaderCreateInfo->pNext = head_ptr;
985 head_ptr = shaderCreateInfo;
986 }
987
988 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
989 {
990 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
991 m_vi_state.pNext = head_ptr;
992 head_ptr = &m_vi_state;
993 }
994
995 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
996 info.pNext = head_ptr;
997 info.flags = 0;
998
999 err = xglCreateGraphicsPipeline(m_device->device(), &info, &m_pipeline);
1000 assert(!err);
1001
1002 err = m_device->AllocAndBindGpuMemory(m_pipeline, "Pipeline", &m_pipe_mem);
1003 assert(!err);
1004}
1005XGL_PIPELINE XglPipelineObj::GetPipelineHandle()
1006{
1007 return m_pipeline;
1008}
1009
Tony Barbour5420af02014-12-03 13:58:15 -07001010void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet)
Tony Barboure2c58df2014-11-25 13:18:32 -07001011{
1012 XGL_RESULT err;
1013 XGL_VOID* head_ptr = &m_db_state;
1014 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1015
1016 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo;
Tony Barboure2c58df2014-11-25 13:18:32 -07001017
1018 for (int i=0; i<m_shaderObjs.size(); i++)
1019 {
1020 shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet);
1021 shaderCreateInfo->pNext = head_ptr;
1022 head_ptr = shaderCreateInfo;
1023 }
1024
1025 if (m_vi_state.attributeCount && m_vi_state.bindingCount)
1026 {
1027 m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
1028 m_vi_state.pNext = head_ptr;
1029 head_ptr = &m_vi_state;
1030 }
1031
1032 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1033 info.pNext = head_ptr;
1034 info.flags = 0;
1035
1036 err = xglCreateGraphicsPipeline(m_device->device(), &info, &m_pipeline);
Tony Barbourba2a1062014-12-03 09:24:05 -07001037 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001038
1039 err = m_device->AllocAndBindGpuMemory(m_pipeline, "Pipeline", &m_pipe_mem);
Tony Barbourba2a1062014-12-03 09:24:05 -07001040 assert(!err);
Tony Barboure2c58df2014-11-25 13:18:32 -07001041
1042 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, m_pipeline );
1043
1044
1045 for (int i=0; i < m_vertexBufferCount; i++)
1046 {
Courtney Goeltzenleuchter37640302014-12-04 15:26:56 -07001047 m_vertexBufferObjs[i]->Bind(m_cmdBuffer, m_vertexBufferObjs[i]->m_constantBufferView.offset, m_vertexBufferBindings[i]);
Tony Barboure2c58df2014-11-25 13:18:32 -07001048 }
Tony Barboure2c58df2014-11-25 13:18:32 -07001049}
Tony Barbour82c39522014-12-04 14:33:33 -07001050
Tony Barbourf325bf12014-12-03 15:59:38 -07001051XglPipelineObj::~XglPipelineObj()
1052{
1053 if (m_pipeline != XGL_NULL_HANDLE) xglDestroyObject(m_pipeline);
1054}
Tony Barboure2c58df2014-11-25 13:18:32 -07001055
1056XglMemoryRefManager::XglMemoryRefManager() {
1057
1058}
Tony Barbour82c39522014-12-04 14:33:33 -07001059
Tony Barboure2c58df2014-11-25 13:18:32 -07001060void XglMemoryRefManager::AddMemoryRef(XglConstantBufferObj *constantBuffer) {
1061 m_bufferObjs.push_back(&constantBuffer->m_constantBufferMem);
1062}
Tony Barbour82c39522014-12-04 14:33:33 -07001063
Tony Barboure2c58df2014-11-25 13:18:32 -07001064void XglMemoryRefManager::AddMemoryRef(XglTextureObj *texture) {
1065 m_bufferObjs.push_back(&texture->m_textureMem);
1066}
Tony Barbour82c39522014-12-04 14:33:33 -07001067
Tony Barboure2c58df2014-11-25 13:18:32 -07001068XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() {
1069
1070 XGL_MEMORY_REF *localRefs;
1071 XGL_UINT32 numRefs=m_bufferObjs.size();
1072
1073 if (numRefs <= 0)
1074 return NULL;
1075
1076 localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) );
1077 for (int i=0; i<numRefs; i++)
1078 {
1079 localRefs[i].flags = 0;
1080 localRefs[i].mem = *m_bufferObjs[i];
1081 }
1082 return localRefs;
1083}
1084int XglMemoryRefManager::GetNumRefs() {
1085 return m_bufferObjs.size();
1086}
Tony Barbour6d047bf2014-12-10 14:34:45 -07001087
1088XglCommandBufferObj::XglCommandBufferObj(XglDevice *device)
1089{
1090 XGL_RESULT err;
1091
1092 memset(&m_cmdInfo,0,sizeof(m_cmdInfo));
Tony Barbour30cc9e82014-12-17 11:53:55 -07001093 m_renderTargetCount = 0;
Tony Barbour6d047bf2014-12-10 14:34:45 -07001094
1095 m_device = device;
1096 m_cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
1097 m_cmdInfo.queueType = XGL_QUEUE_TYPE_GRAPHICS;
1098 err = xglCreateCommandBuffer(m_device->device(), &m_cmdInfo, &m_cmdBuffer);
1099 assert(!err);
1100}
Tony Barbour471338d2014-12-10 17:28:39 -07001101
Tony Barbour6d047bf2014-12-10 14:34:45 -07001102XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle()
1103{
1104 return m_cmdBuffer;
1105}
Tony Barbour471338d2014-12-10 17:28:39 -07001106
1107XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_FLAGS flags)
1108{
1109 return xglBeginCommandBuffer(m_cmdBuffer, flags);
1110}
1111
1112XGL_RESULT XglCommandBufferObj::EndCommandBuffer()
1113{
1114 return xglEndCommandBuffer(m_cmdBuffer);
1115}
1116
1117void XglCommandBufferObj::PrepareMemoryRegions(int transitionCount, XGL_MEMORY_STATE_TRANSITION *transitionPtr)
1118{
1119 xglCmdPrepareMemoryRegions(m_cmdBuffer, transitionCount, transitionPtr);
1120}
1121
Tony Barbour6d047bf2014-12-10 14:34:45 -07001122XglCommandBufferObj::~XglCommandBufferObj()
1123{
1124 if (m_cmdBuffer != XGL_NULL_HANDLE) xglDestroyObject(m_cmdBuffer);
1125}
Tony Barbour30cc9e82014-12-17 11:53:55 -07001126void XglCommandBufferObj::ClearAllBuffers(XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage)
1127{
1128 XGL_UINT i;
1129
1130 // whatever we want to do, we do it to the whole buffer
1131 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1132 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1133 srRange.baseMipLevel = 0;
1134 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1135 srRange.baseArraySlice = 0;
1136 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1137
1138 // clear the back buffer to dark grey
1139 XGL_UINT clearColor[4] = {64, 64, 64, 0};
1140 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
1141 for (i = 0; i < m_renderTargetCount; i++) {
1142 transitionToClear.image = m_renderTargets[i]->image();
1143 transitionToClear.oldState = m_renderTargets[i]->state();
1144 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
1145 transitionToClear.subresourceRange = srRange;
1146 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear );
1147 m_renderTargets[i]->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
1148
1149 xglCmdClearColorImageRaw( m_cmdBuffer, m_renderTargets[i]->image(), clearColor, 1, &srRange );
1150 }
1151
1152 if (depthStencilImage)
1153 {
1154 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
1155 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
1156 dsRange.baseMipLevel = 0;
1157 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1158 dsRange.baseArraySlice = 0;
1159 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1160
1161 // prepare the depth buffer for clear
1162 memset(&transitionToClear,0,sizeof(transitionToClear));
1163 transitionToClear.image = depthStencilImage;
1164 transitionToClear.oldState = depthStencilBinding->depthState;
1165 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
1166 transitionToClear.subresourceRange = dsRange;
1167 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear );
1168 depthStencilBinding->depthState = transitionToClear.newState;
1169
1170 xglCmdClearDepthStencil(m_cmdBuffer, depthStencilImage, 1.0f, 0, 1, &dsRange);
1171
1172 // prepare depth buffer for rendering
1173 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
1174 transitionToRender.image = depthStencilImage;
1175 transitionToRender.oldState = XGL_IMAGE_STATE_CLEAR;
1176 transitionToRender.newState = depthStencilBinding->depthState;
1177 transitionToRender.subresourceRange = dsRange;
1178 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender );
1179 depthStencilBinding->depthState = transitionToClear.newState;
1180 }
1181}
1182
1183void XglCommandBufferObj::BindAttachments(XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding)
1184{
1185 XGL_UINT i;
1186 XGL_COLOR_ATTACHMENT_BIND_INFO colorBindings[XGL_MAX_COLOR_ATTACHMENTS];
1187 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
1188 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
1189 srRange.baseMipLevel = 0;
1190 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
1191 srRange.baseArraySlice = 0;
1192 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
1193
1194 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
1195 for(i=0; i<m_renderTargetCount; i++)
1196 {
1197 transitionToRender.image = m_renderTargets[i]->image();
1198 transitionToRender.oldState = m_renderTargets[i]->state();
1199 transitionToRender.newState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
1200 transitionToRender.subresourceRange = srRange;
1201 xglCmdPrepareImages(m_cmdBuffer, 1, &transitionToRender );
1202 m_renderTargets[i]->state(( XGL_IMAGE_STATE ) transitionToRender.newState);
1203 }
1204 for (i = 0; i < m_renderTargetCount; i++) {
1205 colorBindings[i].view = m_renderTargets[i]->targetView();
1206 colorBindings[i].colorAttachmentState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
1207 }
1208 if (depthStencilBinding) {
1209 xglCmdBindAttachments(m_cmdBuffer, m_renderTargetCount, colorBindings, depthStencilBinding );
1210 } else {
1211 xglCmdBindAttachments(m_cmdBuffer, m_renderTargetCount, colorBindings, XGL_NULL_HANDLE );
1212 }
1213}
1214
1215void XglCommandBufferObj::BindState(XGL_RASTER_STATE_OBJECT stateRaster, XGL_VIEWPORT_STATE_OBJECT stateViewport,
1216 XGL_COLOR_BLEND_STATE_OBJECT colorBlend, XGL_DEPTH_STENCIL_STATE_OBJECT stateDepthStencil,
1217 XGL_MSAA_STATE_OBJECT stateMsaa)
1218{
1219 // set all states
1220 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_RASTER, stateRaster );
1221 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_VIEWPORT, stateViewport );
1222 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_COLOR_BLEND, colorBlend);
1223 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_DEPTH_STENCIL, stateDepthStencil );
1224 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_MSAA, stateMsaa );
1225}
1226
1227void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget)
1228{
1229 m_renderTargets.push_back(renderTarget);
1230 m_renderTargetCount++;
1231}
1232
1233void XglCommandBufferObj::DrawIndexed(XGL_UINT firstIndex, XGL_UINT indexCount, XGL_INT vertexOffset, XGL_UINT firstInstance, XGL_UINT instanceCount)
1234{
1235 xglCmdDrawIndexed(m_cmdBuffer, firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
1236}
1237
1238void XglCommandBufferObj::Draw(XGL_UINT firstVertex, XGL_UINT vertexCount, XGL_UINT firstInstance, XGL_UINT instanceCount)
1239{
1240 xglCmdDraw(m_cmdBuffer, firstVertex, vertexCount, firstInstance, instanceCount);
1241}
1242
1243void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs)
1244{
1245 XGL_RESULT err = XGL_SUCCESS;
1246
1247 // submit the command buffer to the universal queue
1248 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, numMemRefs, memRefs, NULL );
1249 ASSERT_XGL_SUCCESS( err );
1250
1251 err = xglQueueWaitIdle( m_device->m_queue );
1252 ASSERT_XGL_SUCCESS( err );
1253
1254 // Wait for work to finish before cleaning up.
1255 xglDeviceWaitIdle(m_device->device());
1256
1257}
1258void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline)
1259{
1260 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline );
1261}
1262
1263void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet)
1264{
1265 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic memory view)
1266 xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, descriptorSet, 0 );
1267}
1268void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, XGL_UINT offset)
1269{
1270 xglCmdBindIndexData(m_cmdBuffer, indexBuffer->m_constantBufferMem, offset, indexBuffer->GetIndexType());
1271}
1272void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, XGL_UINT offset, XGL_UINT binding)
1273{
1274 xglCmdBindVertexData(m_cmdBuffer, vertexBuffer->m_constantBufferMem, offset, binding);
1275}