private uint PushIndexEntries(uint offset) { const GLDeviceMemoryTypeFlagBits SELECTED_TYPE_INDEX = GLDeviceMemoryTypeFlagBits.INDEX | GLDeviceMemoryTypeFlagBits.TRANSFER_DST; const MgMemoryPropertyFlagBits ALL_ON = MgMemoryPropertyFlagBits.DEVICE_LOCAL_BIT | MgMemoryPropertyFlagBits.HOST_VISIBLE_BIT | MgMemoryPropertyFlagBits.HOST_COHERENT_BIT | MgMemoryPropertyFlagBits.HOST_CACHED_BIT | MgMemoryPropertyFlagBits.LAZILY_ALLOCATED_BIT; var info_stream = new GLDeviceMemoryTypeInfo { MemoryTypeIndex = (uint)SELECTED_TYPE_INDEX, Index = offset, IsHosted = false, MemoryPropertyFlags = MgMemoryPropertyFlagBits.DEVICE_LOCAL_BIT | MgMemoryPropertyFlagBits.LAZILY_ALLOCATED_BIT, Hint = STREAM_DRAW, }; mMemoryTypes[info_stream.Index] = info_stream; offset += 1; var info_static = new GLDeviceMemoryTypeInfo { MemoryTypeIndex = (uint)SELECTED_TYPE_INDEX, Index = offset, IsHosted = false, MemoryPropertyFlags = MgMemoryPropertyFlagBits.HOST_CACHED_BIT | MgMemoryPropertyFlagBits.LAZILY_ALLOCATED_BIT, Hint = STATIC_DRAW, }; mMemoryTypes[info_static.Index] = info_static; offset += 1; var info_dynamic = new GLDeviceMemoryTypeInfo { MemoryTypeIndex = (uint)SELECTED_TYPE_INDEX, Index = offset, IsHosted = false, MemoryPropertyFlags = ALL_ON, Hint = DYNAMIC_DRAW, }; this.mMemoryTypes[info_dynamic.Index] = info_dynamic; return(offset + 1); }
public BufferInfo(IMgThreadPartition partition, MgBufferUsageFlagBits usage, MgMemoryPropertyFlagBits propertyFlags, uint bufferSize) { Debug.Assert(partition != null); mDevice = partition.Device; Debug.Assert(mDevice != null); mUsageFlags = usage; mMemoryPropertyFlags = propertyFlags; var bufferCreateInfo = new MgBufferCreateInfo { Usage = usage, Size = bufferSize, }; IMgBuffer buffer; var device = partition.Device; var result = device.CreateBuffer(bufferCreateInfo, null, out buffer); Debug.Assert(result == Result.SUCCESS); MgMemoryRequirements memReqs; device.GetBufferMemoryRequirements(buffer, out memReqs); mAlignment = memReqs.Alignment; mBufferSize = memReqs.Size; uint memoryTypeIndex; partition.GetMemoryType(memReqs.MemoryTypeBits, mMemoryPropertyFlags, out memoryTypeIndex); var memAlloc = new MgMemoryAllocateInfo { MemoryTypeIndex = memoryTypeIndex, AllocationSize = memReqs.Size, }; IMgDeviceMemory deviceMemory; result = device.AllocateMemory(memAlloc, null, out deviceMemory); Debug.Assert(result == Result.SUCCESS); buffer.BindBufferMemory(device, deviceMemory, 0); mBuffer = buffer; mDeviceMemory = deviceMemory; mDescriptor = new MgDescriptorBufferInfo { Buffer = mBuffer, Offset = 0, Range = mBufferSize, }; }
private uint PushImageEntries(uint offset) { const MgMemoryPropertyFlagBits ALL_ON = MgMemoryPropertyFlagBits.DEVICE_LOCAL_BIT | MgMemoryPropertyFlagBits.HOST_VISIBLE_BIT | MgMemoryPropertyFlagBits.HOST_COHERENT_BIT | MgMemoryPropertyFlagBits.HOST_CACHED_BIT | MgMemoryPropertyFlagBits.LAZILY_ALLOCATED_BIT; var info = new GLDeviceMemoryTypeInfo() { Index = offset, IsHosted = true, MemoryTypeIndex = (uint)GLDeviceMemoryTypeFlagBits.IMAGE, MemoryPropertyFlags = ALL_ON, Hint = 0, }; this.mMemoryTypes[info.Index] = info; return(offset + 1); }
public bool GetMemoryType(uint typeBits, MgMemoryPropertyFlagBits memoryPropertyFlags, out uint typeIndex) { uint requirements = (uint)memoryPropertyFlags; // Search memtypes to find first index with those properties for (UInt32 i = 0; i < mDeviceMemoryProperties.MemoryTypes.Length; i++) { if ((typeBits & 1) == 1) { // Type is available, does it match user properties? if ((mDeviceMemoryProperties.MemoryTypes[i].PropertyFlags & requirements) == requirements) { typeIndex = i; return(true); } } typeBits >>= 1; } // No memory types matched, return failure typeIndex = 0; return(false); }
private bool DoPack(MgStorageBlockInfo sector, MgOptimizedStorageCreateInfo createInfo, out MgStorageBufferInstance instance) { MgBufferUsageFlagBits bufferUsage = 0; MgMemoryPropertyFlagBits deviceMemoryProperties = 0; var sortedList = new SortedDictionary <SortedKey, uint>(); foreach (var attr in sector.Attributes) { bufferUsage |= attr.Usage; deviceMemoryProperties |= createInfo.Allocations[attr.Index].MemoryPropertyFlags; var firstGroup = 0U; for (var i = 0U; i < mPackingOrder.Length; i += 1) { firstGroup = i; var first = (mPackingOrder[i] & createInfo.Allocations[attr.Index].Usage) > 0; if (first) { break; } } Debug.Assert(firstGroup <= mPackingOrder.Length); var key = new SortedKey { Group = firstGroup, SubOrder = attr.Index, }; sortedList.Add(key, attr.Index); } ulong overallSize = 0; var pCreateInfo = new MgBufferCreateInfo { SharingMode = createInfo.SharingMode, QueueFamilyIndices = createInfo.QueueFamilyIndices, Size = 0, Usage = bufferUsage, }; // The alignment member is identical for all VkBuffer objects created with the same combination of values // for the usage and flags members in the VkBufferCreateInfo structure passed to vkCreateBuffer. var err = mConfiguration.Device.CreateBuffer(pCreateInfo, null, out IMgBuffer pZeroBuffer); if (err != Result.SUCCESS) { instance = null; return(false); } mConfiguration.Device.GetBufferMemoryRequirements(pZeroBuffer, out MgMemoryRequirements pZeroMemReqs); pZeroBuffer.DestroyBuffer(mConfiguration.Device, null); var requiredAlignment = pZeroMemReqs.Alignment; var mappings = new List <MgStorageBufferOffset>(); foreach (var element in sortedList.Values) { var attr = createInfo.Allocations[element]; var mapping = new MgStorageBufferOffset { Index = element, Offset = UpperBounded(overallSize, requiredAlignment), Size = attr.Size, Usage = attr.Usage, }; mappings.Add(mapping); overallSize = mapping.Offset + mapping.Size; } // Real memory allocation pCreateInfo.Size = overallSize; err = mConfiguration.Device.CreateBuffer(pCreateInfo, null, out IMgBuffer pBuffer); if (err != Result.SUCCESS) { instance = null; return(false); } mConfiguration.Device.GetBufferMemoryRequirements(pBuffer, out MgMemoryRequirements pMemReqs); if (mConfiguration.MemoryProperties.GetMemoryType(pMemReqs.MemoryTypeBits, deviceMemoryProperties, out uint typeIndex)) { instance = new MgStorageBufferInstance { Buffer = pBuffer, Usage = bufferUsage, MemoryPropertyFlags = deviceMemoryProperties, TypeIndex = typeIndex, AllocationSize = pMemReqs.Size, Mappings = mappings.ToArray(), }; return(true); } else { // CLEAN UP pBuffer.DestroyBuffer(mConfiguration.Device, null); instance = null; return(false); } }
public bool GetMemoryType(uint typeBits, MgMemoryPropertyFlagBits memoryPropertyFlags, out uint typeIndex) { typeIndex = ReturnValue; return(IsValid); }
// 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; }
void InitializeMesh() { // Generate meshes // Mg : Buffer var indicesInBytes = (ulong)(sizeof(uint) * indicesVboData.Length); var vertexStride = Marshal.SizeOf(typeof(Vector3)); var verticesInBytes = (ulong)(vertexStride * positionVboData.Length); var bufferSize = indicesInBytes + verticesInBytes; var bufferCreateInfo = new MgBufferCreateInfo { Usage = MgBufferUsageFlagBits.INDEX_BUFFER_BIT | MgBufferUsageFlagBits.VERTEX_BUFFER_BIT, Size = bufferSize, }; var device = mConfiguration.Device; var result = device.CreateBuffer(bufferCreateInfo, null, out mBuffer); Debug.Assert(result == Result.SUCCESS); MgMemoryRequirements memReqs; device.GetBufferMemoryRequirements(mBuffer, out memReqs); const MgMemoryPropertyFlagBits memoryPropertyFlags = MgMemoryPropertyFlagBits.HOST_COHERENT_BIT; uint memoryTypeIndex; mConfiguration.Partition.GetMemoryType( memReqs.MemoryTypeBits, memoryPropertyFlags, out memoryTypeIndex); var memAlloc = new MgMemoryAllocateInfo { MemoryTypeIndex = memoryTypeIndex, AllocationSize = memReqs.Size, }; result = device.AllocateMemory(memAlloc, null, out mDeviceMemory); Debug.Assert(result == Result.SUCCESS); mBuffer.BindBufferMemory(device, mDeviceMemory, 0); // COPY INDEX DATA IntPtr dest; result = mDeviceMemory.MapMemory(device, 0, bufferSize, 0, out dest); Debug.Assert(result == Result.SUCCESS); var tempIndices = new byte[indicesInBytes]; Buffer.BlockCopy(indicesVboData, 0, tempIndices, 0, (int)indicesInBytes); Marshal.Copy(tempIndices, 0, dest, (int)indicesInBytes); // COPY VERTEX DATA var vertexOffset = indicesInBytes; // Copy the struct to unmanaged memory. int offset = (int)vertexOffset; for (int i = 0; i < positionVboData.Length; ++i) { IntPtr localDest = IntPtr.Add(dest, offset); Marshal.StructureToPtr(positionVboData[i], localDest, false); offset += vertexStride; } mDeviceMemory.UnmapMemory(device); mIndices = new BufferInfo { Buffer = mBuffer, DeviceMemory = mDeviceMemory, Offset = 0, Length = indicesInBytes, }; mVertices = new BufferInfo { Buffer = mBuffer, DeviceMemory = mDeviceMemory, Offset = indicesInBytes, Length = verticesInBytes, }; }