Example #1
0
        public static unsafe MeshBGFX CreateStaticMesh(RendererBGFXInstance *inst, ushort *indices, int nindices, SimpleVertex *vertices, int nvertices, SkinnedMeshVertex *skinningdata = null)
        {
            bool hasSkinningData = skinningdata != null;

#if ENABLE_DOTSRUNTIME_PROFILER
            ProfilerStats.AccumStats.memMeshCount.Accumulate(1);
            long bytes = nvertices * sizeof(SimpleVertex) + nindices * sizeof(ushort);
            if (hasSkinningData)
            {
                bytes += nvertices * sizeof(SkinnedMeshVertex);
            }
            ProfilerStats.AccumStats.memMesh.Accumulate(bytes);
            ProfilerStats.AccumStats.memReservedGFX.Accumulate(bytes);
            ProfilerStats.AccumStats.memUsedGFX.Accumulate(bytes);
#endif
            if (hasSkinningData)
            {
                int   simpleVertexSize   = sizeof(SimpleVertex);
                int   skinningVertexSize = sizeof(SkinnedMeshVertex);
                int   totalVertexSize    = simpleVertexSize + skinningVertexSize;
                byte *tmpBlock           = (byte *)UnsafeUtility.Malloc(totalVertexSize * nvertices, 4, Allocator.Temp);
                UnsafeUtility.MemCpyStride(tmpBlock, totalVertexSize, vertices, simpleVertexSize, simpleVertexSize, nvertices);
                UnsafeUtility.MemCpyStride(tmpBlock + simpleVertexSize, totalVertexSize, skinningdata, skinningVertexSize, skinningVertexSize, nvertices);
                bgfx.Memory *bgfxMemory = RendererBGFXStatic.CreateMemoryBlock((byte *)tmpBlock, nvertices * totalVertexSize);
                UnsafeUtility.Free(tmpBlock, Allocator.Temp);

                return(new MeshBGFX
                {
                    indexBufferHandle = bgfx.create_index_buffer(RendererBGFXStatic.CreateMemoryBlock((byte *)indices, nindices * 2), (ushort)bgfx.BufferFlags.None).idx,
                    vertexBufferHandle = bgfx.create_vertex_buffer(bgfxMemory, &inst->m_simpleSkinnedVertexBufferDecl, (ushort)bgfx.BufferFlags.None).idx,
                    indexCount = nindices,
                    vertexCount = nvertices,
                    maxIndexCount = nindices,
                    maxVertexCount = nvertices,
                    vertexLayoutHandle = inst->m_simpleSkinnedVertexBufferDeclHandle,
                    isDynamic = false,
                    vertexSize = totalVertexSize,
                });
            }
            else
            {
                return(new MeshBGFX
                {
                    indexBufferHandle = bgfx.create_index_buffer(RendererBGFXStatic.CreateMemoryBlock((byte *)indices, nindices * 2), (ushort)bgfx.BufferFlags.None).idx,
                    vertexBufferHandle = bgfx.create_vertex_buffer(RendererBGFXStatic.CreateMemoryBlock((byte *)vertices, nvertices * sizeof(SimpleVertex)), &inst->m_simpleVertexBufferDecl, (ushort)bgfx.BufferFlags.None).idx,
                    indexCount = nindices,
                    vertexCount = nvertices,
                    maxIndexCount = nindices,
                    maxVertexCount = nvertices,
                    vertexLayoutHandle = inst->m_simpleVertexBufferDeclHandle,
                    isDynamic = false,
                    vertexSize = sizeof(SimpleVertex),
                });
            }
        }
        protected override void OnUpdate()
        {
            var shader = World.GetExistingSystem <SpriteRendererSubmitSystem>().DefaultShader;

            if (!shader.IsInitialized)
            {
                return;
            }

            var cmd = m_Barrier.CreateCommandBuffer();

            Dependency = Entities
                         .WithName("CreateMesh2D")
                         .WithoutBurst()
                         .WithNone <SpriteMeshBuffers>()
                         .ForEach((Entity entity, in Sprite sprite) =>
            {
                var blob        = sprite.Mesh;
                var indexCount  = blob.Value.Indices.Length;
                var vertexCount = blob.Value.Vertices.Length;

                unsafe
                {
                    cmd.AddComponent(entity, new SpriteMeshBuffers
                    {
                        IndexCount         = indexCount,
                        VertexCount        = vertexCount,
                        VertexLayoutHandle = shader.LayoutHandle,
                        IndexBufferHandle  = bgfx.create_index_buffer(RendererBGFXStatic.CreateMemoryBlock((byte *)blob.Value.Indices.GetUnsafePtr(), indexCount * 2), (ushort)bgfx.BufferFlags.None).idx,
                        VertexBufferHandle = bgfx.create_vertex_buffer(RendererBGFXStatic.CreateMemoryBlock((byte *)blob.Value.Vertices.GetUnsafePtr(), vertexCount * sizeof(SpriteVertex)), (bgfx.VertexLayout *)shader.VertexLayout.GetUnsafeReadOnlyPtr(), (ushort)bgfx.BufferFlags.None).idx
                    });
                }
            }).Schedule(Dependency);

            Dependency = Entities
                         .WithName("RemoveMesh2D")
                         .WithoutBurst()
                         .WithNone <Sprite>()
                         .ForEach((Entity entity, in SpriteMeshBuffers mesh) =>
            {
                bgfx.destroy_index_buffer(new bgfx.IndexBufferHandle {
                    idx = mesh.IndexBufferHandle
                });
                bgfx.destroy_vertex_buffer(new bgfx.VertexBufferHandle {
                    idx = mesh.VertexBufferHandle
                });
                cmd.RemoveComponent(entity, typeof(SpriteMeshBuffers));
            }).Schedule(Dependency);

            m_Barrier.AddJobHandleForProducer(Dependency);
        }
Example #3
0
        public unsafe void UpdateDynamic(ushort *indexSrc, int numIndices, byte *vertexSrc, int numVertices, int sizeofVertex)
        {
            Assert.IsTrue(isDynamic);
            Assert.IsTrue(numVertices <= maxVertexCount);
            Assert.IsTrue(numIndices <= maxIndexCount);
#if ENABLE_DOTSRUNTIME_PROFILER
            Assert.IsTrue(vertexSize == sizeofVertex);
            ProfilerStats.AccumStats.memUsedGFX.Accumulate(-(vertexCount * vertexSize + indexCount * sizeof(ushort)));
            ProfilerStats.AccumStats.memUsedGFX.Accumulate(numVertices * vertexSize + numIndices * sizeof(ushort));
#endif
            bgfx.update_dynamic_index_buffer(GetDynamicIndexBufferHandle(), 0, RendererBGFXStatic.CreateMemoryBlock((byte *)indexSrc, numIndices * 2));
            indexCount = numIndices;
            bgfx.update_dynamic_vertex_buffer(GetDynamicVertexBufferHandle(), 0, RendererBGFXStatic.CreateMemoryBlock(vertexSrc, numVertices * sizeofVertex));
            vertexCount = numVertices;
        }
        public static unsafe MeshBGFX CreateStaticMesh(RendererBGFXInstance *inst, ushort *indices, int nindices, SimpleVertex *vertices, int nvertices)
        {
#if ENABLE_DOTSRUNTIME_PROFILER
            ProfilerStats.AccumStats.memMeshCount.Accumulate(1);
            long bytes = nvertices * sizeof(SimpleVertex) + nindices * sizeof(ushort);
            ProfilerStats.AccumStats.memMesh.Accumulate(bytes);
            ProfilerStats.AccumStats.memReservedGFX.Accumulate(bytes);
            ProfilerStats.AccumStats.memUsedGFX.Accumulate(bytes);
#endif
            return(new MeshBGFX
            {
                indexBufferHandle = bgfx.create_index_buffer(RendererBGFXStatic.CreateMemoryBlock((byte *)indices, nindices * 2), (ushort)bgfx.BufferFlags.None).idx,
                vertexBufferHandle = bgfx.create_vertex_buffer(RendererBGFXStatic.CreateMemoryBlock((byte *)vertices, nvertices * sizeof(SimpleVertex)), &inst->m_simpleVertexBufferDecl, (ushort)bgfx.BufferFlags.None).idx,
                indexCount = nindices,
                vertexCount = nvertices,
                maxIndexCount = nindices,
                maxVertexCount = nvertices,
                vertexLayoutHandle = inst->m_simpleVertexBufferDeclHandle,
                isDynamic = false,
                vertexSize = sizeof(SimpleVertex),
            });
        }