public static unsafe void EncodeLine(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, float3 p0, float3 p1, float4 color, float2 width, ref float4x4 objTx, ref float4x4 viewTx, ref float4x4 projTx)
        {
            float4 p0t = math.mul(projTx,
                                  math.mul(viewTx,
                                           math.mul(objTx, new float4(p0, 1))));
            float4 p1t = math.mul(projTx,
                                  math.mul(viewTx,
                                           math.mul(objTx, new float4(p1, 1))));

            for (int i = 0; i < 3; i++)   // really only need to clip z near, but clip all to make sure clipping works
            {
                if (!ClipLinePositive(ref p0t, ref p1t, i))
                {
                    return;
                }
                if (!ClipLineNegative(ref p0t, ref p1t, i))
                {
                    return;
                }
            }
            SimpleVertex *buf = stackalloc SimpleVertex[4];

            p0t.xyz *= 1.0f / p0t.w;
            p1t.xyz *= 1.0f / p1t.w;
            float2 dp     = math.normalizesafe(p1t.xy - p0t.xy);
            float2 dprefl = new float2(-dp.y, dp.x);
            float3 dv     = new float3(dprefl * width, 0);
            float3 du     = new float3(dp * width * .5f, 0);

            buf[0].Position = p0t.xyz + dv - du; buf[0].Color = color; buf[0].TexCoord0 = new float2(0, 1);
            buf[1].Position = p0t.xyz - dv - du; buf[1].Color = color; buf[1].TexCoord0 = new float2(0, -1);
            buf[2].Position = p1t.xyz - dv + du; buf[2].Color = color; buf[2].TexCoord0 = new float2(1, -1);
            buf[3].Position = p1t.xyz + dv + du; buf[3].Color = color; buf[3].TexCoord0 = new float2(1, 1);
            EncodeLinePreTransformed(sys, encoder, viewId, buf, 4);
        }
Exemple #2
0
        public static unsafe MeshBGFX CreateStaticMeshFromBlobAsset(RendererBGFXInstance *inst, SimpleMeshRenderData meshData)
        {
            ushort *      indices   = (ushort *)meshData.Mesh.Value.Indices.GetUnsafePtr();
            SimpleVertex *vertices  = (SimpleVertex *)meshData.Mesh.Value.Vertices.GetUnsafePtr();
            int           nindices  = meshData.Mesh.Value.Indices.Length;
            int           nvertices = meshData.Mesh.Value.Vertices.Length;

            return(CreateStaticMesh(inst, indices, nindices, vertices, nvertices));
        }
Exemple #3
0
 public unsafe void CheckVertexLayout()
 {
     SimpleVertex  tv;
     SimpleVertex *p = &tv;
     {
         Debug.Assert((long)&(p->Position) - (long)p == 0);
         Debug.Assert((long)&(p->TexCoord0) - (long)p == 12);
         Debug.Assert((long)&(p->Color) - (long)p == 20);
         Debug.Assert((long)&(p->BillboardPos) - (long)p == 36);
     }
 }
Exemple #4
0
        public static unsafe void EncodeSimpleTransient(RendererBGFXSystem sys, bgfx.Encoder *encoder, ushort viewId, SimpleVertex *vertices, int nvertices, ushort *indices, int nindices, ref float4x4 tx, float4 color, bgfx.TextureHandle texture, ulong state)
        {
            // upload
            SimpleVertex *destVertices = null;
            ushort *      destIndices  = null;

            if (!EncodeSimpleTransientAlloc(sys, encoder, nindices, nvertices, &destVertices, &destIndices))
            {
                return;
            }
            UnsafeUtility.MemCpy(destIndices, indices, nindices * 2);
            UnsafeUtility.MemCpy(destVertices, vertices, nvertices * sizeof(SimpleVertex));
            // material uniforms setup
            EncodeSimpleTransient(sys, encoder, viewId, ref tx, color, texture, state);
        }
        public static unsafe void EncodeLinePreTransformed(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, SimpleVertex *vertices, int n)
        {
            bgfx.TransientIndexBuffer  tib;
            bgfx.TransientVertexBuffer tvb;
            int ni = (n / 4) * 6;

            if (!bgfx.alloc_transient_buffers(&tvb, &sys->m_simpleVertexBufferDecl, (uint)n, &tib, (uint)ni))
            {
                throw new InvalidOperationException("Out of transient bgfx memory!");
            }
            UnsafeUtility.MemCpy((SimpleVertex *)tvb.data, vertices, sizeof(SimpleVertex) * n);
            ushort *indices = (ushort *)tib.data;

            for (int i = 0; i < n; i += 4)
            {
                indices[0] = (ushort)i; indices[1] = (ushort)(i + 1); indices[2] = (ushort)(i + 2);
                indices[3] = (ushort)(i + 2); indices[4] = (ushort)(i + 3); indices[5] = (ushort)i;
                indices   += 6;
            }
            bgfx.encoder_set_transient_index_buffer(encoder, &tib, 0, (uint)ni);
            bgfx.encoder_set_transient_vertex_buffer(encoder, 0, &tvb, 0, (uint)n, sys->m_simpleVertexBufferDeclHandle);

            // material uniforms setup
            ulong state = (ulong)(bgfx.StateFlags.DepthTestLess | bgfx.StateFlags.WriteRgb) | RendererBGFXStatic.MakeBGFXBlend(bgfx.StateFlags.BlendOne, bgfx.StateFlags.BlendInvSrcAlpha);

            bgfx.encoder_set_state(encoder, state, 0);
            bgfx.encoder_submit(encoder, viewId, sys->m_lineShader.m_prog, 0, (byte)bgfx.DiscardFlags.All);
        }
Exemple #6
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),
                });
            }
        }
Exemple #7
0
 public static unsafe void EncodeSimpleTransient(RendererBGFXSystem sys, bgfx.Encoder *encoder, ushort viewId, SimpleVertex *vertices, int nvertices, ushort *indices, int nindices, ref float4x4 tx, ref SimpleMaterialBGFX mat)
 {
     EncodeSimpleTransient(sys, encoder, viewId, vertices, nvertices, indices, nindices, ref tx, mat.constAlbedo_Opacity, mat.texAlbedo, mat.state);
 }
Exemple #8
0
 public static unsafe void SubmitSimpleTransientDirect(RendererBGFXSystem sys, ushort viewId, SimpleVertex *vertices, int nvertices, ushort *indices, int nindices, ref float4x4 tx, float4 color, bgfx.TextureHandle texture, ulong state)
 {
     bgfx.Encoder *encoder = bgfx.encoder_begin(false);
     EncodeSimpleTransient(sys, encoder, viewId, vertices, nvertices, indices, nindices, ref tx, color, texture, state);
     bgfx.encoder_end(encoder);
 }
Exemple #9
0
 // ---------------- simple, transient, for ui/text ----------------------------------------------------------------------------------------------------------------------
 public static unsafe void SubmitSimpleTransientDirect(RendererBGFXSystem sys, ushort viewId, SimpleVertex *vertices, int nvertices, ushort *indices, int nindices, ref float4x4 tx, ref SimpleMaterialBGFX mat)
 {
     bgfx.Encoder *encoder = bgfx.encoder_begin(false);
     EncodeSimpleTransient(sys, encoder, viewId, vertices, nvertices, indices, nindices, ref tx, ref mat);
     bgfx.encoder_end(encoder);
 }
        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),
            });
        }