Exemplo n.º 1
0
        /// <summary>
        /// Map memory, then copies data then unmaps device memory
        /// </summary>
        /// <returns>The data.</returns>
        /// <param name="device">Device.</param>
        /// <param name="sizeInBytes">Size in bytes.</param>
        /// <param name="data">Data.</param>
        /// <param name="startIndex">Start index.</param>
        /// <param name="elementCount">Element count.</param>
        /// <typeparam name="TData">The 1st type parameter.</typeparam>
        public Result SetData <TData>(uint sizeInBytes, TData[] data, int startIndex, int elementCount)
            where TData : struct
        {
            if (data == null)
            {
                return(Result.SUCCESS);
            }

            int stride = Marshal.SizeOf(typeof(TData));

            if (sizeInBytes < (stride * elementCount))
            {
                throw new ArgumentOutOfRangeException("sizeInBytes");
            }

            IntPtr dest;
            var    result = mDeviceMemory.MapMemory(mDevice, 0, sizeInBytes, 0, out dest);

            Debug.Assert(result == Result.SUCCESS);


            // Copy the struct to unmanaged memory.
            int offset = 0;

            for (int i = 0; i < elementCount; ++i)
            {
                IntPtr localDest = IntPtr.Add(dest, offset);
                Marshal.StructureToPtr(data[i + startIndex], localDest, false);
                offset += stride;
            }

            mDeviceMemory.UnmapMemory(mDevice);

            return(Result.SUCCESS);
        }
Exemplo n.º 2
0
        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,
            };
        }