コード例 #1
0
ファイル: MeshHelper.cs プロジェクト: ikorin24/Elffy
        public static unsafe bool TryGetMeshRaw(
            Renderable renderable,
            void *vertices, ulong verticesByteSize,
            int *indices, ulong indicesByteSize,
            [MaybeNullWhen(false)] out Type vertexType,
            out ulong verticesByteSizeActual,
            out uint indicesByteSizeActual)
        {
            ArgumentNullException.ThrowIfNull(renderable);

            if (renderable.TryGetScreen(out var screen) == false)
            {
                goto FAILURE;
            }
            if (Engine.CurrentContext != screen)
            {
                goto FAILURE;
            }
            if (renderable.IsLoaded == false)
            {
                goto FAILURE;
            }
            vertexType = renderable.VertexType;
            Debug.Assert(vertexType != null);
            if (VertexMarshalHelper.TryGetVertexTypeData(vertexType, out var vertexTypeData) == false)
            {
                goto FAILURE;
            }

            var vertexSize = (ulong)vertexTypeData.VertexSize;

            Debug.Assert(vertexType != null);

            var vbo = renderable.VBO;
            var ibo = renderable.IBO;

            var verticesCount = vbo.Length;
            var indicesCount  = ibo.Length;

            verticesByteSizeActual = verticesCount * vertexSize;
            indicesByteSizeActual  = indicesCount * sizeof(int);

            if (verticesByteSize < verticesByteSizeActual)
            {
                goto FAILURE;
            }
            if (indicesByteSize < indicesByteSizeActual)
            {
                goto FAILURE;
            }

            try {
                VBO.Bind(vbo);
                var vSource = (void *)VBO.MapBufferReadOnly();
                Buffer.MemoryCopy(vSource, vertices, verticesByteSize, verticesByteSizeActual);
            }
            finally {
                VBO.UnmapBuffer();
                VBO.Unbind();
            }
            try {
                IBO.Bind(ibo);
                var iSource = (void *)IBO.MapBufferReadOnly();
                Buffer.MemoryCopy(iSource, indices, indicesByteSize, indicesByteSizeActual);
            }
            finally {
                IBO.UnmapBuffer();
                IBO.Unbind();
            }
            return(true);

FAILURE:
            vertexType             = null;
            verticesByteSizeActual = 0;
            indicesByteSizeActual  = 0;
            return(false);
        }
コード例 #2
0
ファイル: MeshHelper.cs プロジェクト: ikorin24/Elffy
        public static unsafe bool TryGetMesh(Renderable renderable, [MaybeNullWhen(false)] out Mesh mesh)
        {
            ArgumentNullException.ThrowIfNull(renderable);

            if (renderable.TryGetScreen(out var screen) == false)
            {
                goto FAILURE;
            }
            if (Engine.CurrentContext != screen)
            {
                goto FAILURE;
            }
            if (renderable.IsLoaded == false)
            {
                goto FAILURE;
            }

            const uint IndexSize = sizeof(int);

            var vertexType = renderable.VertexType;

            Debug.Assert(vertexType != null);
            if (VertexMarshalHelper.TryGetVertexTypeData(vertexType, out var vertexTypeData) == false)
            {
                goto FAILURE;
            }

            var vertexSize = (ulong)vertexTypeData.VertexSize;

            Debug.Assert(vertexType != null);

            var vbo = renderable.VBO;
            var ibo = renderable.IBO;

            var verticesCount    = vbo.Length;
            var indicesCount     = ibo.Length;
            var verticesByteSize = verticesCount * vertexSize;
            var indicesByteSize  = indicesCount * IndexSize;

            var bufLen = verticesByteSize + indicesByteSize;
            var buf    = UniquePtr.Malloc(checked ((nuint)bufLen));
            var vDest  = (void *)buf.Ptr;
            var iDest  = buf.GetPtr <byte>() + verticesByteSize;

            try {
                try {
                    VBO.Bind(vbo);
                    var vSource = (void *)VBO.MapBufferReadOnly();
                    Buffer.MemoryCopy(vSource, vDest, bufLen, verticesByteSize);
                }
                finally {
                    VBO.UnmapBuffer();
                    VBO.Unbind();
                }
                try {
                    IBO.Bind(ibo);
                    var iSource = (void *)IBO.MapBufferReadOnly();
                    Buffer.MemoryCopy(iSource, iDest, bufLen, indicesByteSize);
                }
                finally {
                    IBO.UnmapBuffer();
                    IBO.Unbind();
                }
                mesh = Mesh.Create(vertexTypeData, IndexSize, vDest, verticesByteSize, iDest, indicesByteSize, ref buf, static buf => buf.Dispose());
                return(true);
            }
            finally {
                buf.Dispose();
            }

FAILURE:
            mesh = null;
            return(false);
        }