Tony Barbour | 32f368c | 2014-10-30 14:29:04 -0600 | [diff] [blame] | 1 | class XglDescriptorSetObj |
| 2 | { |
| 3 | public: |
| 4 | XglDescriptorSetObj(XglDevice *device); |
| 5 | void AttachMemoryView( XGL_MEMORY_VIEW_ATTACH_INFO* memoryView); |
| 6 | void AttachSampler( XGL_SAMPLER* sampler); |
| 7 | void AttachImageView( XGL_IMAGE_VIEW_ATTACH_INFO* imageView); |
| 8 | void BindCommandBuffer(XGL_CMD_BUFFER commandBuffer); |
| 9 | XGL_DESCRIPTOR_SLOT_INFO * GetSlotInfo(vector<int>slots, vector<XGL_DESCRIPTOR_SET_SLOT_TYPE>types, vector<XGL_OBJECT>objs ); |
| 10 | |
| 11 | protected: |
| 12 | XGL_DESCRIPTOR_SET_CREATE_INFO m_descriptorInfo; |
| 13 | XGL_DESCRIPTOR_SET m_rsrcDescSet; |
| 14 | XGL_GPU_MEMORY m_descriptor_set_mem; |
| 15 | XglDevice *m_device; |
| 16 | int m_nextSlot; |
| 17 | vector<int> m_memorySlots; |
| 18 | vector<XGL_MEMORY_VIEW_ATTACH_INFO*> m_memoryViews; |
| 19 | vector<int> m_samplerSlots; |
| 20 | vector<XGL_SAMPLER*> m_samplers; |
| 21 | vector<int> m_imageSlots; |
| 22 | vector<XGL_IMAGE_VIEW_ATTACH_INFO*> m_imageViews; |
| 23 | }; |
| 24 | |
| 25 | XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device) |
| 26 | { |
| 27 | m_device = device; |
| 28 | m_nextSlot = 0; |
| 29 | |
| 30 | } |
| 31 | |
| 32 | void XglDescriptorSetObj::AttachMemoryView(XGL_MEMORY_VIEW_ATTACH_INFO* memoryView) |
| 33 | { |
| 34 | m_memoryViews.push_back(memoryView); |
| 35 | m_memorySlots.push_back(m_nextSlot); |
| 36 | m_nextSlot++; |
| 37 | |
| 38 | } |
| 39 | void XglDescriptorSetObj::AttachSampler(XGL_SAMPLER* sampler) |
| 40 | { |
| 41 | m_samplers.push_back(sampler); |
| 42 | m_samplerSlots.push_back(m_nextSlot); |
| 43 | m_nextSlot++; |
| 44 | |
| 45 | } |
| 46 | void XglDescriptorSetObj::AttachImageView(XGL_IMAGE_VIEW_ATTACH_INFO* imageView) |
| 47 | { |
| 48 | m_imageViews.push_back(imageView); |
| 49 | m_imageSlots.push_back(m_nextSlot); |
| 50 | m_nextSlot++; |
| 51 | |
| 52 | } |
| 53 | XGL_DESCRIPTOR_SLOT_INFO* XglDescriptorSetObj::GetSlotInfo(vector<int>slots, |
| 54 | vector<XGL_DESCRIPTOR_SET_SLOT_TYPE>types, |
| 55 | vector<XGL_OBJECT>objs ) |
| 56 | { |
| 57 | int nSlots = m_memorySlots.size() + m_imageSlots.size() + m_samplerSlots.size(); |
| 58 | XGL_DESCRIPTOR_SLOT_INFO *slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( nSlots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) ); |
| 59 | for (int i=0; i<nSlots; i++) |
| 60 | { |
| 61 | slotInfo[i].slotObjectType = XGL_SLOT_UNUSED; |
| 62 | } |
| 63 | |
| 64 | for (int i=0; i<slots.size(); i++) |
| 65 | { |
| 66 | for (int j=0; j<m_memorySlots.size(); j++) |
| 67 | { |
| 68 | if ( (XGL_OBJECT) m_memoryViews[j] == objs[i]) |
| 69 | { |
| 70 | slotInfo[m_memorySlots[j]].shaderEntityIndex = slots[i]; |
| 71 | slotInfo[m_memorySlots[j]].slotObjectType = types[i]; |
| 72 | } |
| 73 | } |
| 74 | for (int j=0; j<m_imageSlots.size(); j++) |
| 75 | { |
| 76 | if ( (XGL_OBJECT) m_imageViews[j] == objs[i]) |
| 77 | { |
| 78 | slotInfo[m_imageSlots[j]].shaderEntityIndex = slots[i]; |
| 79 | slotInfo[m_imageSlots[j]].slotObjectType = types[i]; |
| 80 | } |
| 81 | } |
| 82 | for (int j=0; j<m_samplerSlots.size(); j++) |
| 83 | { |
| 84 | if ( (XGL_OBJECT) m_samplers[j] == objs[i]) |
| 85 | { |
| 86 | slotInfo[m_samplerSlots[j]].shaderEntityIndex = slots[i]; |
| 87 | slotInfo[m_samplerSlots[j]].slotObjectType = types[i]; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | for (int i=0;i<nSlots;i++) |
| 93 | { |
| 94 | printf("SlotInfo[%d]: Index = %d, Type = %d\n",i,slotInfo[i].shaderEntityIndex, slotInfo[i].slotObjectType); |
| 95 | fflush(stdout); |
| 96 | } |
| 97 | |
| 98 | return(slotInfo); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | void XglDescriptorSetObj::BindCommandBuffer(XGL_CMD_BUFFER commandBuffer) |
| 103 | { |
| 104 | XGL_RESULT err; |
| 105 | |
| 106 | // Create descriptor set for a uniform resource |
| 107 | m_descriptorInfo = {}; |
| 108 | m_descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO; |
| 109 | m_descriptorInfo.slots = m_nextSlot; |
| 110 | |
| 111 | // Create a descriptor set with requested number of slots |
| 112 | err = xglCreateDescriptorSet( m_device->device(), &m_descriptorInfo, &m_rsrcDescSet ); |
| 113 | |
| 114 | // Bind memory to the descriptor set |
| 115 | err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem); |
| 116 | |
| 117 | xglBeginDescriptorSetUpdate( m_rsrcDescSet ); |
| 118 | xglClearDescriptorSetSlots(m_rsrcDescSet, 0, m_nextSlot); |
| 119 | for (int i=0; i<m_memoryViews.size();i++) |
| 120 | { |
| 121 | xglAttachMemoryViewDescriptors( m_rsrcDescSet, m_memorySlots[i], 1, m_memoryViews[i] ); |
| 122 | } |
| 123 | for (int i=0; i<m_samplers.size();i++) |
| 124 | { |
| 125 | xglAttachSamplerDescriptors( m_rsrcDescSet, m_samplerSlots[i], 1, m_samplers[i] ); |
| 126 | } |
| 127 | for (int i=0; i<m_imageViews.size();i++) |
| 128 | { |
| 129 | xglAttachImageViewDescriptors( m_rsrcDescSet, m_imageSlots[i], 1, m_imageViews[i] ); |
| 130 | } |
| 131 | xglEndDescriptorSetUpdate( m_rsrcDescSet ); |
| 132 | |
| 133 | // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic memory view) |
| 134 | xglCmdBindDescriptorSet(commandBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 ); |
| 135 | } |
| 136 | |
| 137 | class XglTextureObj |
| 138 | { |
| 139 | public: |
| 140 | XglTextureObj(XglDevice *device); |
| 141 | XGL_IMAGE m_texture; |
| 142 | XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo; |
| 143 | XGL_GPU_MEMORY m_textureMem; |
| 144 | |
| 145 | protected: |
| 146 | XglDevice *m_device; |
| 147 | XGL_IMAGE_VIEW m_textureView; |
| 148 | |
| 149 | }; |
| 150 | XglTextureObj::XglTextureObj(XglDevice *device) |
| 151 | { |
| 152 | m_device = device; |
| 153 | const XGL_FORMAT tex_format = { XGL_CH_FMT_B8G8R8A8, XGL_NUM_FMT_UNORM }; |
| 154 | const XGL_INT tex_width = 16; |
| 155 | const XGL_INT tex_height = 16; |
| 156 | const uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 }; |
| 157 | XGL_RESULT err; |
| 158 | XGL_UINT i; |
| 159 | |
| 160 | |
| 161 | m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO; |
| 162 | |
| 163 | const XGL_IMAGE_CREATE_INFO image = { |
| 164 | .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 165 | .pNext = NULL, |
| 166 | .imageType = XGL_IMAGE_2D, |
| 167 | .format = tex_format, |
| 168 | .extent = { tex_width, tex_height, 1 }, |
| 169 | .mipLevels = 1, |
| 170 | .arraySize = 1, |
| 171 | .samples = 1, |
| 172 | .tiling = XGL_LINEAR_TILING, |
| 173 | .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, |
| 174 | .flags = 0, |
| 175 | }; |
| 176 | |
| 177 | XGL_MEMORY_ALLOC_INFO mem_alloc; |
| 178 | mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 179 | mem_alloc.pNext = NULL; |
| 180 | mem_alloc.allocationSize = 0; |
| 181 | mem_alloc.alignment = 0; |
| 182 | mem_alloc.flags = 0; |
| 183 | mem_alloc.heapCount = 0; |
| 184 | mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 185 | |
| 186 | XGL_IMAGE_VIEW_CREATE_INFO view; |
| 187 | view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 188 | view.pNext = NULL; |
| 189 | view.image = XGL_NULL_HANDLE; |
| 190 | view.viewType = XGL_IMAGE_VIEW_2D; |
| 191 | view.format = image.format; |
| 192 | view.channels.r = XGL_CHANNEL_SWIZZLE_R; |
| 193 | view.channels.g = XGL_CHANNEL_SWIZZLE_G; |
| 194 | view.channels.b = XGL_CHANNEL_SWIZZLE_B; |
| 195 | view.channels.a = XGL_CHANNEL_SWIZZLE_A; |
| 196 | view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 197 | view.subresourceRange.baseMipLevel = 0; |
| 198 | view.subresourceRange.mipLevels = 1; |
| 199 | view.subresourceRange.baseArraySlice = 0; |
| 200 | view.subresourceRange.arraySize = 1; |
| 201 | view.minLod = 0.0f; |
| 202 | |
| 203 | XGL_MEMORY_REQUIREMENTS mem_reqs; |
| 204 | XGL_SIZE mem_reqs_size; |
| 205 | |
| 206 | /* create image */ |
| 207 | err = xglCreateImage(m_device->device(), &image, &m_texture); |
| 208 | assert(!err); |
| 209 | |
| 210 | err = xglGetObjectInfo(m_texture, |
| 211 | XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 212 | &mem_reqs_size, &mem_reqs); |
| 213 | assert(!err && mem_reqs_size == sizeof(mem_reqs)); |
| 214 | |
| 215 | mem_alloc.allocationSize = mem_reqs.size; |
| 216 | mem_alloc.alignment = mem_reqs.alignment; |
| 217 | mem_alloc.heapCount = mem_reqs.heapCount; |
| 218 | memcpy(mem_alloc.heaps, mem_reqs.heaps, |
| 219 | sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount); |
| 220 | |
| 221 | /* allocate memory */ |
| 222 | err = xglAllocMemory(m_device->device(), &mem_alloc, &m_textureMem); |
| 223 | assert(!err); |
| 224 | |
| 225 | /* bind memory */ |
| 226 | err = xglBindObjectMemory(m_texture, m_textureMem, 0); |
| 227 | assert(!err); |
| 228 | |
| 229 | /* create image view */ |
| 230 | view.image = m_texture; |
| 231 | err = xglCreateImageView(m_device->device(), &view, &m_textureView); |
| 232 | assert(!err); |
| 233 | |
| 234 | |
| 235 | const XGL_IMAGE_SUBRESOURCE subres = { |
| 236 | .aspect = XGL_IMAGE_ASPECT_COLOR, |
| 237 | .mipLevel = 0, |
| 238 | .arraySlice = 0, |
| 239 | }; |
| 240 | XGL_SUBRESOURCE_LAYOUT layout; |
| 241 | XGL_SIZE layout_size; |
| 242 | XGL_VOID *data; |
| 243 | XGL_INT x, y; |
| 244 | |
| 245 | err = xglGetImageSubresourceInfo(m_texture, &subres, |
| 246 | XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout); |
| 247 | assert(!err && layout_size == sizeof(layout)); |
| 248 | |
| 249 | err = xglMapMemory(m_textureMem, 0, &data); |
| 250 | assert(!err); |
| 251 | |
| 252 | for (y = 0; y < tex_height; y++) { |
| 253 | uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y); |
| 254 | for (x = 0; x < tex_width; x++) |
| 255 | row[x] = tex_colors[(x & 1) ^ (y & 1)]; |
| 256 | } |
| 257 | |
| 258 | err = xglUnmapMemory(m_textureMem); |
| 259 | assert(!err); |
| 260 | |
| 261 | m_textureViewInfo.view = m_textureView; |
| 262 | |
| 263 | } |
| 264 | |
| 265 | class XglSamplerObj |
| 266 | { |
| 267 | public: |
| 268 | XglSamplerObj(XglDevice *device); |
| 269 | XGL_SAMPLER m_sampler; |
| 270 | |
| 271 | protected: |
| 272 | XGL_SAMPLER_CREATE_INFO m_samplerCreateInfo = {}; |
| 273 | XglDevice *m_device; |
| 274 | |
| 275 | }; |
| 276 | |
| 277 | XglSamplerObj::XglSamplerObj(XglDevice *device) |
| 278 | { |
| 279 | XGL_RESULT err = XGL_SUCCESS; |
| 280 | |
| 281 | m_device = device; |
| 282 | m_samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; |
| 283 | m_samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST; |
| 284 | m_samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST; |
| 285 | m_samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE; |
| 286 | m_samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP; |
| 287 | m_samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP; |
| 288 | m_samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP; |
| 289 | m_samplerCreateInfo.mipLodBias = 0.0; |
| 290 | m_samplerCreateInfo.maxAnisotropy = 0.0; |
| 291 | m_samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER; |
| 292 | m_samplerCreateInfo.minLod = 0.0; |
| 293 | m_samplerCreateInfo.maxLod = 0.0; |
| 294 | m_samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE; |
| 295 | |
| 296 | err = xglCreateSampler(m_device->device(),&m_samplerCreateInfo, &m_sampler); |
| 297 | |
| 298 | } |
| 299 | |
| 300 | class XglConstantBufferObj |
| 301 | { |
| 302 | public: |
| 303 | XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data); |
| 304 | void SetMemoryState(XGL_CMD_BUFFER cmdBuffer, XGL_MEMORY_STATE newState); |
| 305 | XGL_MEMORY_VIEW_ATTACH_INFO m_constantBufferView; |
| 306 | XGL_GPU_MEMORY m_constantBufferMem; |
| 307 | |
| 308 | protected: |
| 309 | XglDevice *m_device; |
| 310 | int m_numVertices; |
| 311 | int m_stride; |
| 312 | }; |
| 313 | XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data) |
| 314 | { |
| 315 | XGL_RESULT err = XGL_SUCCESS; |
| 316 | XGL_UINT8 *pData; |
| 317 | XGL_MEMORY_ALLOC_INFO alloc_info = {}; |
| 318 | m_device = device; |
| 319 | m_numVertices = constantCount; |
| 320 | m_stride = constantSize; |
| 321 | |
| 322 | alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 323 | alloc_info.allocationSize = constantCount * constantSize; |
| 324 | alloc_info.alignment = 0; |
| 325 | alloc_info.heapCount = 1; |
| 326 | alloc_info.heaps[0] = 0; // TODO: Use known existing heap |
| 327 | |
| 328 | alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT; |
| 329 | alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 330 | |
| 331 | err = xglAllocMemory(m_device->device(), &alloc_info, &m_constantBufferMem); |
| 332 | |
| 333 | err = xglMapMemory(m_constantBufferMem, 0, (XGL_VOID **) &pData); |
| 334 | |
| 335 | memcpy(pData, data, alloc_info.allocationSize); |
| 336 | |
| 337 | err = xglUnmapMemory(m_constantBufferMem); |
| 338 | |
| 339 | // set up the memory view for the constant buffer |
| 340 | this->m_constantBufferView.stride = 16; |
| 341 | this->m_constantBufferView.range = alloc_info.allocationSize; |
| 342 | this->m_constantBufferView.offset = 0; |
| 343 | this->m_constantBufferView.mem = m_constantBufferMem; |
| 344 | this->m_constantBufferView.format.channelFormat = XGL_CH_FMT_R32G32B32A32; |
| 345 | this->m_constantBufferView.format.numericFormat = XGL_NUM_FMT_FLOAT; |
| 346 | this->m_constantBufferView.state = XGL_MEMORY_STATE_DATA_TRANSFER; |
| 347 | } |
| 348 | |
| 349 | void XglConstantBufferObj::SetMemoryState(XGL_CMD_BUFFER cmdBuffer, XGL_MEMORY_STATE newState) |
| 350 | { |
| 351 | if (this->m_constantBufferView.state == newState) |
| 352 | return; |
| 353 | |
| 354 | // open the command buffer |
| 355 | XGL_RESULT err = xglBeginCommandBuffer( cmdBuffer, 0 ); |
| 356 | ASSERT_XGL_SUCCESS(err); |
| 357 | |
| 358 | XGL_MEMORY_STATE_TRANSITION transition = {}; |
| 359 | transition.mem = m_constantBufferMem; |
| 360 | transition.oldState = XGL_MEMORY_STATE_DATA_TRANSFER; |
| 361 | transition.newState = newState; |
| 362 | transition.offset = 0; |
| 363 | transition.regionSize = m_numVertices * m_stride; |
| 364 | |
| 365 | // write transition to the command buffer |
| 366 | xglCmdPrepareMemoryRegions( cmdBuffer, 1, &transition ); |
| 367 | this->m_constantBufferView.state = newState; |
| 368 | |
| 369 | // finish recording the command buffer |
| 370 | err = xglEndCommandBuffer( cmdBuffer ); |
| 371 | ASSERT_XGL_SUCCESS(err); |
| 372 | |
| 373 | XGL_UINT32 numMemRefs=1; |
| 374 | XGL_MEMORY_REF memRefs; |
| 375 | // this command buffer only uses the vertex buffer memory |
| 376 | memRefs.flags = 0; |
| 377 | memRefs.mem = m_constantBufferMem; |
| 378 | |
| 379 | // submit the command buffer to the universal queue |
| 380 | err = xglQueueSubmit( m_device->m_queue, 1, &cmdBuffer, numMemRefs, &memRefs, NULL ); |
| 381 | ASSERT_XGL_SUCCESS(err); |
| 382 | } |
| 383 | |
| 384 | class XglShaderObj |
| 385 | { |
| 386 | public: |
| 387 | XglShaderObj(XglDevice *device, const char * shaderText, XGL_PIPELINE_SHADER_STAGE stage ); |
| 388 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* GetStageCreateInfo(XglDescriptorSetObj descriptorSet); |
| 389 | void BindShaderEntitySlotToMemory(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XGL_OBJECT object); |
| 390 | void BindShaderEntitySlotToImage(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XGL_OBJECT object); |
| 391 | void BindShaderEntitySlotToSampler(int slot, XGL_OBJECT object); |
| 392 | |
| 393 | protected: |
| 394 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO stage_info; |
| 395 | XGL_SHADER m_shader; |
| 396 | XGL_PIPELINE_SHADER_STAGE m_stage; |
| 397 | XglDevice *m_device; |
| 398 | vector<int> m_memSlots; |
| 399 | vector<XGL_DESCRIPTOR_SET_SLOT_TYPE> m_memTypes; |
| 400 | vector<XGL_OBJECT> m_memObjs; |
| 401 | vector<int> m_samplerSlots; |
| 402 | vector<XGL_DESCRIPTOR_SET_SLOT_TYPE> m_samplerTypes; |
| 403 | vector<XGL_OBJECT> m_samplerObjs; |
| 404 | vector<int> m_imageSlots; |
| 405 | vector<XGL_DESCRIPTOR_SET_SLOT_TYPE> m_imageTypes; |
| 406 | vector<XGL_OBJECT> m_imageObjs; |
| 407 | |
| 408 | }; |
| 409 | |
| 410 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo(XglDescriptorSetObj descriptorSet) |
| 411 | { |
| 412 | XGL_DESCRIPTOR_SLOT_INFO *slotInfo; |
| 413 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) ); |
| 414 | stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 415 | stageInfo->shader.stage = m_stage; |
| 416 | stageInfo->shader.shader = m_shader; |
| 417 | stageInfo->shader.descriptorSetMapping[0].descriptorCount = 0; |
| 418 | stageInfo->shader.linkConstBufferCount = 0; |
| 419 | stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE; |
| 420 | stageInfo->shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED; |
| 421 | stageInfo->shader.dynamicMemoryViewMapping.shaderEntityIndex = 0; |
| 422 | |
| 423 | stageInfo->shader.descriptorSetMapping[0].descriptorCount = m_memSlots.size() + m_imageSlots.size() + m_samplerSlots.size(); |
| 424 | if (stageInfo->shader.descriptorSetMapping[0].descriptorCount) |
| 425 | { |
| 426 | vector<int> allSlots; |
| 427 | vector<XGL_DESCRIPTOR_SET_SLOT_TYPE> allTypes; |
| 428 | vector<XGL_OBJECT> allObjs; |
| 429 | |
| 430 | allSlots.reserve(m_memSlots.size() + m_imageSlots.size() + m_samplerSlots.size()); |
| 431 | allTypes.reserve(m_memTypes.size() + m_imageTypes.size() + m_samplerTypes.size()); |
| 432 | allObjs.reserve(m_memObjs.size() + m_imageObjs.size() + m_samplerObjs.size()); |
| 433 | |
| 434 | if (m_memSlots.size()) |
| 435 | { |
| 436 | allSlots.insert(allSlots.end(), m_memSlots.begin(), m_memSlots.end()); |
| 437 | allTypes.insert(allTypes.end(), m_memTypes.begin(), m_memTypes.end()); |
| 438 | allObjs.insert(allObjs.end(), m_memObjs.begin(), m_memObjs.end()); |
| 439 | } |
| 440 | if (m_imageSlots.size()) |
| 441 | { |
| 442 | allSlots.insert(allSlots.end(), m_imageSlots.begin(), m_imageSlots.end()); |
| 443 | allTypes.insert(allTypes.end(), m_imageTypes.begin(), m_imageTypes.end()); |
| 444 | allObjs.insert(allObjs.end(), m_imageObjs.begin(), m_imageObjs.end()); |
| 445 | } |
| 446 | if (m_samplerSlots.size()) |
| 447 | { |
| 448 | allSlots.insert(allSlots.end(), m_samplerSlots.begin(), m_samplerSlots.end()); |
| 449 | allTypes.insert(allTypes.end(), m_samplerTypes.begin(), m_samplerTypes.end()); |
| 450 | allObjs.insert(allObjs.end(), m_samplerObjs.begin(), m_samplerObjs.end()); |
| 451 | } |
| 452 | |
| 453 | slotInfo = descriptorSet.GetSlotInfo(allSlots, allTypes, allObjs); |
| 454 | stageInfo->shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo; |
| 455 | } |
| 456 | return stageInfo; |
| 457 | } |
| 458 | |
| 459 | void XglShaderObj::BindShaderEntitySlotToMemory(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XGL_OBJECT object) |
| 460 | { |
| 461 | m_memSlots.push_back(slot); |
| 462 | m_memTypes.push_back(type); |
| 463 | m_memObjs.push_back(object); |
| 464 | |
| 465 | } |
| 466 | void XglShaderObj::BindShaderEntitySlotToImage(int slot, XGL_DESCRIPTOR_SET_SLOT_TYPE type, XGL_OBJECT object) |
| 467 | { |
| 468 | m_imageSlots.push_back(slot); |
| 469 | m_imageTypes.push_back(type); |
| 470 | m_imageObjs.push_back(object); |
| 471 | |
| 472 | } |
| 473 | void XglShaderObj::BindShaderEntitySlotToSampler(int slot, XGL_OBJECT object) |
| 474 | { |
| 475 | m_samplerSlots.push_back(slot); |
| 476 | m_samplerTypes.push_back(XGL_SLOT_SHADER_SAMPLER); |
| 477 | m_samplerObjs.push_back(object); |
| 478 | |
| 479 | } |
| 480 | XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage) |
| 481 | { |
| 482 | XGL_RESULT err = XGL_SUCCESS; |
| 483 | std::vector<unsigned int> bil; |
| 484 | XGL_SHADER_CREATE_INFO createInfo; |
| 485 | size_t shader_len; |
| 486 | |
| 487 | m_stage = stage; |
| 488 | m_device = device; |
| 489 | |
| 490 | createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 491 | createInfo.pNext = NULL; |
| 492 | |
| 493 | shader_len = strlen(shader_code); |
| 494 | createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1; |
| 495 | createInfo.pCode = malloc(createInfo.codeSize); |
| 496 | createInfo.flags = 0; |
| 497 | |
| 498 | /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */ |
| 499 | ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC; |
| 500 | ((uint32_t *) createInfo.pCode)[1] = 0; |
| 501 | ((uint32_t *) createInfo.pCode)[2] = stage; |
| 502 | memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1); |
| 503 | |
| 504 | err = xglCreateShader(m_device->device(), &createInfo, &m_shader); |
| 505 | if (err) { |
| 506 | free((void *) createInfo.pCode); |
| 507 | } |
| 508 | } |
| 509 | #if 0 |
| 510 | void XglShaderObj::CreateShaderBIL(XGL_PIPELINE_SHADER_STAGE stage, |
| 511 | const char *shader_code, |
| 512 | XGL_SHADER *pshader) |
| 513 | { |
| 514 | XGL_RESULT err = XGL_SUCCESS; |
| 515 | std::vector<unsigned int> bil; |
| 516 | XGL_SHADER_CREATE_INFO createInfo; |
| 517 | size_t shader_len; |
| 518 | XGL_SHADER shader; |
| 519 | |
| 520 | createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 521 | createInfo.pNext = NULL; |
| 522 | |
| 523 | // Use Reference GLSL to BIL compiler |
| 524 | GLSLtoBIL(stage, shader_code, bil); |
| 525 | createInfo.pCode = bil.data(); |
| 526 | createInfo.codeSize = bil.size() * sizeof(unsigned int); |
| 527 | createInfo.flags = 0; |
| 528 | err = xglCreateShader(device(), &createInfo, &shader); |
| 529 | |
| 530 | ASSERT_XGL_SUCCESS(err); |
| 531 | |
| 532 | *pshader = shader; |
| 533 | } |
| 534 | #endif |
| 535 | class XglPipelineObj |
| 536 | { |
| 537 | public: |
| 538 | XglPipelineObj(XglDevice *device); |
| 539 | void BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj descriptorSet); |
| 540 | void AddShader(XglShaderObj* shaderObj); |
| 541 | void AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count); |
| 542 | void AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count); |
| 543 | void AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer); |
| 544 | |
| 545 | protected: |
| 546 | XGL_PIPELINE pipeline; |
| 547 | XGL_PIPELINE_VERTEX_INPUT_CREATE_INFO vi_state; |
| 548 | XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state; |
| 549 | XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state; |
| 550 | XGL_PIPELINE_CB_STATE cb_state; |
| 551 | XGL_PIPELINE_DB_STATE_CREATE_INFO db_state; |
| 552 | XGL_FORMAT render_target_format; |
| 553 | XGL_GPU_MEMORY m_pipe_mem; |
| 554 | XglDevice *m_device; |
| 555 | XGL_VERTEX_INPUT_BINDING_DESCRIPTION *m_vi_binding; |
| 556 | int m_vi_binding_count; |
| 557 | XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION *m_vi_attribs; |
| 558 | int m_vi_attrib_count; |
| 559 | vector<XglShaderObj*> m_shaderObjs; |
| 560 | vector<XglConstantBufferObj*> m_vertexBufferObjs; |
| 561 | int m_vertexBufferCount; |
| 562 | |
| 563 | }; |
| 564 | |
| 565 | XglPipelineObj::XglPipelineObj(XglDevice *device) |
| 566 | { |
| 567 | XGL_RESULT err; |
| 568 | |
| 569 | m_device = device; |
| 570 | m_vi_attrib_count = m_vi_binding_count = m_vertexBufferCount = 0; |
| 571 | |
| 572 | ia_state = { |
| 573 | XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType |
| 574 | XGL_NULL_HANDLE, // pNext |
| 575 | XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY |
| 576 | XGL_FALSE, // disableVertexReuse |
| 577 | XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION |
| 578 | XGL_FALSE, // primitiveRestartEnable |
| 579 | 0 // primitiveRestartIndex |
| 580 | }; |
| 581 | |
| 582 | rs_state = { |
| 583 | XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO, |
| 584 | &ia_state, |
| 585 | XGL_FALSE, // depthClipEnable |
| 586 | XGL_FALSE, // rasterizerDiscardEnable |
| 587 | 1.0 // pointSize |
| 588 | }; |
| 589 | |
| 590 | render_target_format.channelFormat = XGL_CH_FMT_R8G8B8A8; |
| 591 | render_target_format.numericFormat = XGL_NUM_FMT_UNORM; |
| 592 | cb_state = { |
| 593 | XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO, |
| 594 | &rs_state, |
| 595 | XGL_FALSE, // alphaToCoverageEnable |
| 596 | XGL_FALSE, // dualSourceBlendEnable |
| 597 | XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP |
| 598 | { // XGL_PIPELINE_CB_ATTACHMENT_STATE |
| 599 | { |
| 600 | XGL_FALSE, // blendEnable |
| 601 | render_target_format, // XGL_FORMAT |
| 602 | 0xF // channelWriteMask |
| 603 | } |
| 604 | } |
| 605 | }; |
| 606 | |
| 607 | db_state = { |
| 608 | XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO, |
| 609 | &cb_state, |
| 610 | {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT |
| 611 | }; |
| 612 | |
| 613 | |
| 614 | }; |
| 615 | |
| 616 | void XglPipelineObj::AddShader(XglShaderObj* shader) |
| 617 | { |
| 618 | m_shaderObjs.push_back(shader); |
| 619 | } |
| 620 | |
| 621 | void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count) |
| 622 | { |
| 623 | m_vi_attribs = vi_attrib; |
| 624 | m_vi_attrib_count = count; |
| 625 | } |
| 626 | |
| 627 | void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count) |
| 628 | { |
| 629 | m_vi_binding = vi_binding; |
| 630 | m_vi_binding_count = count; |
| 631 | } |
| 632 | |
| 633 | void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer) |
| 634 | { |
| 635 | m_vertexBufferObjs.push_back(vertexDataBuffer); |
| 636 | m_vertexBufferCount++; |
| 637 | } |
| 638 | |
| 639 | void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj descriptorSet) |
| 640 | { |
| 641 | XGL_RESULT err; |
| 642 | XGL_VOID* head_ptr = &db_state; |
| 643 | XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {}; |
| 644 | |
| 645 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo; |
| 646 | XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vertexInputAttrib; |
| 647 | |
| 648 | for (int i=0; i<m_shaderObjs.size(); i++) |
| 649 | { |
| 650 | shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(descriptorSet); |
| 651 | shaderCreateInfo->pNext = head_ptr; |
| 652 | head_ptr = shaderCreateInfo; |
| 653 | } |
| 654 | |
| 655 | if (m_vi_attrib_count && m_vi_binding_count) |
| 656 | { |
| 657 | vi_state = { |
| 658 | XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO, // sType; |
| 659 | head_ptr, // pNext; |
| 660 | m_vi_binding_count, // bindingCount |
| 661 | m_vi_binding, // pVertexBindingDescriptions; |
| 662 | m_vi_attrib_count, // attributeCount; // number of attributes |
| 663 | m_vi_attribs // pVertexAttributeDescriptions; |
| 664 | }; |
| 665 | head_ptr = &vi_state; |
| 666 | } |
| 667 | |
| 668 | info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
| 669 | info.pNext = head_ptr; |
| 670 | info.flags = 0; |
| 671 | |
| 672 | err = xglCreateGraphicsPipeline(m_device->device(), &info, &pipeline); |
| 673 | |
| 674 | err = m_device->AllocAndBindGpuMemory(pipeline, "Pipeline", &m_pipe_mem); |
| 675 | |
| 676 | xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline ); |
| 677 | |
| 678 | |
| 679 | for (int i=0; i < m_vertexBufferCount; i++) |
| 680 | { |
| 681 | xglCmdBindVertexData(m_cmdBuffer, m_vertexBufferObjs[i]->m_constantBufferView.mem, m_vertexBufferObjs[i]->m_constantBufferView.offset, i); |
| 682 | } |
| 683 | |
| 684 | |
| 685 | // xglCmdBindVertexData(m_cmdBuffer, meshBuffer.m_constantBufferView.mem, meshBuffer.m_constantBufferView.offset, 0); |
| 686 | } |
| 687 | |
| 688 | class XglMemoryRefManager{ |
| 689 | public: |
| 690 | XglMemoryRefManager(); |
| 691 | void AddMemoryRef(XGL_GPU_MEMORY* memoryRef); |
| 692 | XGL_MEMORY_REF* GetMemoryRefList(); |
| 693 | int GetNumRefs(); |
| 694 | |
| 695 | protected: |
| 696 | int m_numRefs; |
| 697 | vector<XGL_GPU_MEMORY*> m_bufferObjs; |
| 698 | |
| 699 | }; |
| 700 | XglMemoryRefManager::XglMemoryRefManager() { |
| 701 | |
| 702 | } |
| 703 | void XglMemoryRefManager::AddMemoryRef(XGL_GPU_MEMORY *memoryRef) { |
| 704 | m_bufferObjs.push_back(memoryRef); |
| 705 | } |
| 706 | XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() { |
| 707 | |
| 708 | XGL_MEMORY_REF *localRefs; |
| 709 | XGL_UINT32 numRefs=m_bufferObjs.size(); |
| 710 | |
| 711 | if (numRefs <= 0) |
| 712 | return NULL; |
| 713 | |
| 714 | localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) ); |
| 715 | for (int i=0; i<numRefs; i++) |
| 716 | { |
| 717 | localRefs[i].flags = 0; |
| 718 | localRefs[i].mem = *m_bufferObjs[i]; |
| 719 | } |
| 720 | return localRefs; |
| 721 | } |
| 722 | int XglMemoryRefManager::GetNumRefs() { |
| 723 | return m_bufferObjs.size(); |
| 724 | } |