/** * Attach the allocated memory block to the buffer * * @param offset (Optional) Byte offset (from the beginning) for the memory region to bind * * @return VkResult of the bindBufferMemory call */ Result bind(ulong offset = 0) { return(mBuffer.BindBufferMemory(mDevice, mDeviceMemory, offset)); }
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, }; }