void SetupDescriptorSet() { var allocInfo = new MgDescriptorSetAllocateInfo { DescriptorPool = mDescriptorPool, DescriptorSetCount = 1, SetLayouts = new[] { mDescriptorSetLayout }, }; IMgDescriptorSet[] dSets; var err = mConfiguration.Device.AllocateDescriptorSets(allocInfo, out dSets); mDescriptorSet = dSets[0]; Debug.Assert(err == Result.SUCCESS); mConfiguration.Device.UpdateDescriptorSets( new [] { new MgWriteDescriptorSet { DstSet = mDescriptorSet, DescriptorCount = 1, DescriptorType = MgDescriptorType.UNIFORM_BUFFER, BufferInfo = new MgDescriptorBufferInfo[] { uniformDataVS.descriptor, }, DstBinding = 0, }, }, null); }
public Result AllocateDescriptorSets(MgDescriptorSetAllocateInfo pAllocateInfo, out IMgDescriptorSet[] pDescriptorSets) { if (pAllocateInfo == null) { throw new ArgumentNullException(nameof(pAllocateInfo)); } var parentPool = (IGLDescriptorPool)pAllocateInfo.DescriptorPool; pDescriptorSets = new IMgDescriptorSet[pAllocateInfo.DescriptorSetCount]; var maxNoOfResources = 0U; var sortedResources = new List <GLDescriptorPoolResourceInfo>(); for (var i = 0; i < pAllocateInfo.DescriptorSetCount; i += 1) { var bSetLayout = (GLDescriptorSetLayout)pAllocateInfo.SetLayouts[i]; sortedResources.Clear(); foreach (var uniform in bSetLayout.Uniforms) { maxNoOfResources = Math.Max(maxNoOfResources, uniform.Binding); GLPoolResourceTicket ticket; switch (uniform.DescriptorType) { case MgDescriptorType.COMBINED_IMAGE_SAMPLER: if (parentPool.CombinedImageSamplers.Allocate(uniform.DescriptorCount, out ticket)) { sortedResources.Add( new GLDescriptorPoolResourceInfo { Binding = uniform.Binding, DescriptorCount = uniform.DescriptorCount, GroupType = GLDescriptorBindingGroup.CombinedImageSampler, Ticket = ticket, } ); } else { // VK_ERROR_FRAGMENTED_POOL = -12 return(Result.ERROR_OUT_OF_HOST_MEMORY); } break; case MgDescriptorType.STORAGE_BUFFER: if (parentPool.StorageBuffers.Allocate(uniform.DescriptorCount, out ticket)) { sortedResources.Add( new GLDescriptorPoolResourceInfo { Binding = uniform.Binding, DescriptorCount = uniform.DescriptorCount, GroupType = GLDescriptorBindingGroup.StorageBuffer, Ticket = ticket, } ); } else { // VK_ERROR_FRAGMENTED_POOL = -12 return(Result.ERROR_OUT_OF_HOST_MEMORY); } break; case MgDescriptorType.UNIFORM_BUFFER: if (parentPool.UniformBuffers.Allocate(uniform.DescriptorCount, out ticket)) { sortedResources.Add( new GLDescriptorPoolResourceInfo { Binding = uniform.Binding, DescriptorCount = uniform.DescriptorCount, GroupType = GLDescriptorBindingGroup.UniformBuffer, Ticket = ticket, } ); } else { // VK_ERROR_FRAGMENTED_POOL = -12 return(Result.ERROR_OUT_OF_HOST_MEMORY); } break; } } var resources = new GLDescriptorPoolResourceInfo[maxNoOfResources]; foreach (var res in sortedResources) { resources[res.Binding] = res; } IGLDescriptorSet item; if (parentPool.TryTake(out item)) { item.Initialise(resources); parentPool.AllocatedSets.Add(item.Key, item); } } return(Result.SUCCESS); }
// Allocate one region of memory for the uniform buffer private void InitializeUniforms() { MgBufferCreateInfo pCreateInfo = new MgBufferCreateInfo { Usage = MgBufferUsageFlagBits.UNIFORM_BUFFER_BIT, Size = MaxBytesPerFrame, }; IMgBuffer buffer; var err = mConfiguration.Device.CreateBuffer(pCreateInfo, null, out buffer); Debug.Assert(err == Result.SUCCESS); //dynamicConstantBuffer = device.CreateBuffer(MaxBytesPerFrame, (MTLResourceOptions)0); //dynamicConstantBuffer.Label = "UniformBuffer"; MgMemoryRequirements uniformsMemReqs; mConfiguration.Device.GetBufferMemoryRequirements(buffer, out uniformsMemReqs); const MgMemoryPropertyFlagBits uniformPropertyFlags = MgMemoryPropertyFlagBits.HOST_COHERENT_BIT; uint uniformMemoryTypeIndex; mConfiguration.Partition.GetMemoryType( uniformsMemReqs.MemoryTypeBits, uniformPropertyFlags, out uniformMemoryTypeIndex); var uniformMemAlloc = new MgMemoryAllocateInfo { MemoryTypeIndex = uniformMemoryTypeIndex, AllocationSize = uniformsMemReqs.Size, }; IMgDeviceMemory deviceMemory; var result = mConfiguration.Device.AllocateMemory(uniformMemAlloc, null, out deviceMemory); Debug.Assert(result == Result.SUCCESS); buffer.BindBufferMemory(mConfiguration.Device, deviceMemory, 0); mUniforms = new BufferInfo { Buffer = buffer, DeviceMemory = deviceMemory, Offset = 0, Length = MaxBytesPerFrame, }; IMgDescriptorSetLayout pSetLayout; var dslCreateInfo = new MgDescriptorSetLayoutCreateInfo { Bindings = new MgDescriptorSetLayoutBinding[] { new MgDescriptorSetLayoutBinding { Binding = 0, DescriptorCount = 1, DescriptorType = MgDescriptorType.UNIFORM_BUFFER_DYNAMIC, StageFlags = MgShaderStageFlagBits.VERTEX_BIT, }, }, }; err = mConfiguration.Device.CreateDescriptorSetLayout(dslCreateInfo, null, out pSetLayout); var poolCreateInfo = new Magnesium.MgDescriptorPoolCreateInfo { MaxSets = 1, PoolSizes = new MgDescriptorPoolSize[] { new MgDescriptorPoolSize { DescriptorCount = 1, Type = MgDescriptorType.COMBINED_IMAGE_SAMPLER, }, }, }; err = mConfiguration.Device.CreateDescriptorPool(poolCreateInfo, null, out mDescriptorPool); IMgDescriptorSet[] dSets; MgDescriptorSetAllocateInfo pAllocateInfo = new MgDescriptorSetAllocateInfo { DescriptorPool = mDescriptorPool, DescriptorSetCount = 1, SetLayouts = new IMgDescriptorSetLayout[] { pSetLayout, }, }; mConfiguration.Device.AllocateDescriptorSets(pAllocateInfo, out dSets); mUniformDescriptorSet = dSets[0]; MgWriteDescriptorSet[] writes = new MgWriteDescriptorSet[] { new MgWriteDescriptorSet { DescriptorCount = 1, DescriptorType = MgDescriptorType.UNIFORM_BUFFER_DYNAMIC, DstSet = mUniformDescriptorSet, BufferInfo = new MgDescriptorBufferInfo[] { new MgDescriptorBufferInfo { Buffer = mUniforms.Buffer, Offset = mUniforms.Offset, Range = mUniforms.Length, }, }, DstBinding = 0, } }; mConfiguration.Device.UpdateDescriptorSets(writes, null); mSetLayout = pSetLayout; }