Ejemplo n.º 1
0
        // ---------------- simple, lit, with mesh ----------------------------------------------------------------------------------------------------------------------
        public unsafe static void SubmitLitMeshDirect(RendererBGFXInstance *sys, ushort viewId, ref MeshBGFX mesh, ref float4x4 tx,
                                                      ref LitMaterialBGFX mat, ref LightingBGFX lighting,
                                                      ref float4x4 viewTx, int startIndex, int indexCount, byte flipCulling, uint depth)
        {
            bgfx.Encoder *        encoder = bgfx.encoder_begin(false);
            LightingViewSpaceBGFX vsLight = default;

            vsLight.cacheTag = -1;
            EncodeLitMesh(sys, encoder, viewId, ref mesh, ref tx, ref mat, ref lighting, ref viewTx, startIndex, indexCount, flipCulling, ref vsLight, depth);
            bgfx.encoder_end(encoder);
        }
Ejemplo n.º 2
0
        public static unsafe void EncodeDebugTangents(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, float2 width, float length, ref LitMeshRenderData mesh, ref float4x4 objTx, ref float4x4 viewTx, ref float4x4 projTx)
        {
            int        nv       = (int)mesh.Mesh.Value.Vertices.Length;
            LitVertex *vertices = (LitVertex *)mesh.Mesh.Value.Vertices.GetUnsafePtr();

            for (int i = 0; i < nv; i++)
            {
                EncodeLine(sys, encoder, viewId, vertices[i].Position, vertices[i].Position + vertices[i].Normal * length, new float4(0, 0, 1, 1), width, ref objTx, ref viewTx, ref projTx);
                EncodeLine(sys, encoder, viewId, vertices[i].Position, vertices[i].Position + vertices[i].Tangent * length, new float4(1, 0, 0, 1), width, ref objTx, ref viewTx, ref projTx);
            }
        }
        public static unsafe void EncodeLitSkinnedMesh(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, ref MeshBGFX mesh, ref float4x4 tx,
                                                       ref LitMaterialBGFX mat, ref LightingBGFX lighting, ref float4x4 viewTx, int startIndex, int indexCount,
                                                       byte flipCulling, ref LightingViewSpaceBGFX viewSpaceLightCache, uint depth, float4x4[] boneMatrices)
        {
            mesh.SetForSubmit(encoder, startIndex, indexCount);
            fixed(float4x4 *p = boneMatrices)
            {
                bgfx.encoder_set_uniform(encoder, sys->m_litSkinnedMeshShader.m_uniformBoneMatrices, p, (ushort)boneMatrices.Length);
            }

            EncodeLit(sys, encoder, ref sys->m_litSkinnedMeshShader.m_litShader, viewId, ref tx, ref mat, ref lighting, ref viewTx, flipCulling, ref viewSpaceLightCache, depth);
        }
        // ---------------- shadow map ----------------------------------------------------------------------------------------------------------------------
        public static unsafe void EncodeShadowMapMesh(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, ref MeshBGFX mesh, ref float4x4 tx,
                                                      int startIndex, int indexCount, byte flipCulling, float4 bias)
        {
#if DEBUG
            float4 cd = GetShadowDebugColor(mesh.DebugIndex() + startIndex);
#endif
            mesh.SetForSubmit(encoder, startIndex, indexCount);
            EncodeShadowMap(sys, encoder, ref sys->m_shadowMapShader, viewId, ref tx, flipCulling, bias
#if DEBUG
                            , cd
#endif
                            );
        }
        private void EncodeBox(RendererBGFXInstance *sys, bgfx.Encoder *encoder, Entity ePass, ref float4x4 tx, float3 cMin, float3 cMax, float width, float4 color)
        {
            var      pass = EntityManager.GetComponentData <RenderPass>(ePass);
            float4x4 adjustedProjection = sys->GetAdjustedProjection(ref pass);
            float2   normWidth          = new float2(width / pass.viewport.w, width / pass.viewport.h);

            for (int j = 0; j < Culling.EdgeTable.Length; j++)
            {
                float3 p0 = Culling.SelectCoordsMinMax(cMin, cMax, Culling.EdgeTable[j] & 7);
                float3 p1 = Culling.SelectCoordsMinMax(cMin, cMax, Culling.EdgeTable[j] >> 3);
                SubmitHelper.EncodeLine(sys, encoder, pass.viewId, p0, p1, color, normWidth,
                                        ref tx, ref pass.viewTransform, ref adjustedProjection);
            }
        }
        private unsafe static void EncodeMappedLight(bgfx.Encoder *encoder, ref MappedLightBGFX light, ref LitShader.MappedLight shader, byte samplerOffset, float4 viewPosOrDir)
        {
            fixed(float4x4 *p = &light.projection)
            bgfx.encoder_set_uniform(encoder, shader.m_uniformMatrix, p, 1);

            fixed(float4 *p = &light.color_invrangesqr)
            bgfx.encoder_set_uniform(encoder, shader.m_uniformColorIVR, p, 1);

            fixed(float4 *p = &light.mask)
            bgfx.encoder_set_uniform(encoder, shader.m_uniformLightMask, p, 1);

            bgfx.encoder_set_uniform(encoder, shader.m_uniformViewPosOrDir, &viewPosOrDir, 1);
            bgfx.encoder_set_texture(encoder, (byte)(6 + samplerOffset), shader.m_samplerShadow, light.shadowMap, UInt32.MaxValue);
        }
        static private void Circle(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, float3 org, float3 du, float3 dv, float r, int n, float4 color, float2 normWidth,
                                   ref float4x4 tx, ref float4x4 txView, ref float4x4 txProj)
        {
            float3 pprev = org + dv * r;

            for (int i = 1; i <= n; i++)
            {
                float  a = ((float)i / (float)n) * math.PI * 2.0f;
                float  u = math.sin(a) * r;
                float  v = math.cos(a) * r;
                float3 p = org + du * u + dv * v;
                SubmitHelper.EncodeLine(sys, encoder, viewId, pprev, p, color, normWidth, ref tx, ref txView, ref txProj);
                pprev = p;
            }
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        // ---------------- shadow map ----------------------------------------------------------------------------------------------------------------------
        public unsafe static void EncodeShadowMap(RendererBGFXSystem sys, bgfx.Encoder *encoder, ushort viewId, ref SimpleMeshBGFX mesh, ref float4x4 tx, int startIndex, int indexCount, byte flipCulling, float4 bias)
        {
            ulong state = (ulong)(bgfx.StateFlags.WriteZ | bgfx.StateFlags.DepthTestLess | bgfx.StateFlags.CullCcw);

            if (flipCulling != 0)
            {
                state = FlipCulling(state);
            }
            bgfx.encoder_set_state(encoder, state, 0);
            unsafe { fixed(float4x4 *p = &tx) bgfx.encoder_set_transform(encoder, p, 1); }
            bgfx.encoder_set_index_buffer(encoder, mesh.indexBufferHandle, (uint)startIndex, (uint)indexCount);
            bgfx.encoder_set_vertex_buffer(encoder, 0, mesh.vertexBufferHandle, (uint)mesh.vertexFirst, (uint)mesh.vertexCount, mesh.vertexDeclHandle);
            bgfx.encoder_set_uniform(encoder, sys.ShadowMapShader.m_uniformBias, &bias, 1);
            bgfx.encoder_submit(encoder, viewId, sys.ShadowMapShader.m_prog, 0, false);
        }
        // For uniforms and shaders setup. Does not handle vertex/index buffers
        private unsafe static void EncodeZOnly(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, ref float4x4 tx, byte flipCulling)
        {
            ulong state = (ulong)(bgfx.StateFlags.WriteZ | bgfx.StateFlags.DepthTestLess | bgfx.StateFlags.CullCcw);

            if (flipCulling != 0)
            {
                state = FlipCulling(state);
            }
            bgfx.encoder_set_state(encoder, state, 0);
            unsafe { fixed(float4x4 *p = &tx) bgfx.encoder_set_transform(encoder, p, 1); }
            float4 color = new float4(1, 0, 0, 1);

            bgfx.encoder_set_uniform(encoder, sys->m_zOnlyShader.m_uniformDebugColor, &color, 1);
            bgfx.encoder_submit(encoder, viewId, sys->m_zOnlyShader.m_prog, 0, (byte)bgfx.DiscardFlags.All);
        }
Ejemplo n.º 11
0
        public static unsafe void EncodeSimpleTransient(RendererBGFXSystem sys, bgfx.Encoder *encoder, ushort viewId, ref float4x4 tx, float4 color, bgfx.TextureHandle texture, ulong state)
        {
            // must call EncodeSimpleTransientAlloc before
            // material uniforms setup
            bgfx.encoder_set_state(encoder, state, 0);

            fixed(float4x4 *p = &tx)
            bgfx.encoder_set_transform(encoder, p, 1);

            bgfx.encoder_set_uniform(encoder, sys.SimpleShader.m_uniformColor0, &color, 1);
            float4 noTexMad = new float4(1, 1, 0, 0);

            bgfx.encoder_set_uniform(encoder, sys.SimpleShader.m_uniformTexMad, &noTexMad, 1);
            bgfx.encoder_set_texture(encoder, 0, sys.SimpleShader.m_samplerTexColor0, texture, UInt32.MaxValue);
            bgfx.encoder_submit(encoder, viewId, sys.SimpleShader.m_prog, 0, false);
        }
Ejemplo n.º 12
0
 public unsafe void SetForSubmit(bgfx.Encoder *encoder, int startIndex, int actualIndexCount)
 {
     if (actualIndexCount < 0)
     {
         actualIndexCount = indexCount;
     }
     Assert.IsTrue(startIndex >= 0 && actualIndexCount + startIndex <= indexCount);
     if (isDynamic)
     {
         bgfx.encoder_set_dynamic_index_buffer(encoder, GetDynamicIndexBufferHandle(), (uint)startIndex, (uint)actualIndexCount);
         bgfx.encoder_set_dynamic_vertex_buffer(encoder, 0, GetDynamicVertexBufferHandle(), 0, (uint)vertexCount, vertexLayoutHandle);
     }
     else
     {
         bgfx.encoder_set_index_buffer(encoder, GetIndexBufferHandle(), (uint)startIndex, (uint)actualIndexCount);
         bgfx.encoder_set_vertex_buffer(encoder, 0, GetVertexBufferHandle(), 0, (uint)vertexCount, vertexLayoutHandle);
     }
 }
Ejemplo n.º 13
0
        // For uniforms and shaders setup. Does not handle vertex/index buffers
        private static unsafe void EncodeShadowMap(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ref ShadowMapShader shadowMapShader, ushort viewId, ref float4x4 tx, byte flipCulling, float4 bias)
        {
            ulong state = (ulong)(bgfx.StateFlags.WriteZ | bgfx.StateFlags.DepthTestLess | bgfx.StateFlags.CullCcw);

            if (flipCulling != 0)
            {
                state = FlipCulling(state);
            }
#if DEBUG
            state |= (ulong)bgfx.StateFlags.WriteRgb | (ulong)bgfx.StateFlags.WriteA;
            float4 c = new float4(1);
            bgfx.encoder_set_uniform(encoder, shadowMapShader.m_uniformDebugColor, &c, 1);
#endif
            bgfx.encoder_set_state(encoder, state, 0);
            unsafe { fixed(float4x4 *p = &tx) bgfx.encoder_set_transform(encoder, p, 1); }
            bgfx.encoder_set_uniform(encoder, shadowMapShader.m_uniformBias, &bias, 1);
            bgfx.encoder_submit(encoder, viewId, shadowMapShader.m_prog, 0, (byte)bgfx.DiscardFlags.All);
        }
Ejemplo n.º 14
0
        unsafe void EncodeText(bgfx.Encoder *encoder, ushort viewId, ref MeshBGFX mesh, ref float4x4 tx,
                               ref TextMaterialBGFX mat, int startIndex, int indexCount, uint depth, byte flipCulling)
        {
            bgfx.set_state(mat.state, 0);

            fixed(float4x4 *p = &tx)
            bgfx.encoder_set_transform(encoder, p, 1);

            mesh.SetForSubmit(encoder, startIndex, indexCount);
            // material uniforms setup
            fixed(TextMaterialBGFX *pmat = &mat)
            {
                bgfx.encoder_set_uniform(encoder, m_TextShader.m_clipRect, &pmat->constClipRect, 1);
                bgfx.encoder_set_uniform(encoder, m_TextShader.m_maskSoftness, &pmat->constMaskSoftness, 1);
            }

            bgfx.encoder_set_texture(encoder, 0, m_TextShader.m_mainTex, mat.texAtlas, UInt32.MaxValue);
            bgfx.encoder_submit(encoder, viewId, m_TextShader.m_prog, depth, (byte)bgfx.DiscardFlags.All);
        }
        // For uniforms and shaders setup. Does not handle vertex/index buffers
        private static unsafe void EncodeSimple(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, ref float4x4 tx, ref SimpleMaterialBGFX mat, byte flipCulling, uint depth)
        {
            bgfx.set_state(mat.state, 0);

            fixed(float4x4 *p = &tx)
            bgfx.encoder_set_transform(encoder, p, 1);

            // material uniforms setup
            fixed(float4 *p = &mat.constAlbedo_Opacity)
            bgfx.encoder_set_uniform(encoder, sys->m_simpleShader.m_uniformColor0, p, 1);

            fixed(float4 *p = &mat.mainTextureScaleTranslate)
            bgfx.encoder_set_uniform(encoder, sys->m_simpleShader.m_uniformTexMad, p, 1);

            fixed(float4 *p = &mat.billboarded)
            bgfx.encoder_set_uniform(encoder, sys->m_simpleShader.m_uniformBillboarded, p, 1);

            bgfx.encoder_set_texture(encoder, 0, sys->m_simpleShader.m_samplerTexColor0, mat.texAlbedoOpacity, UInt32.MaxValue);
            bgfx.encoder_submit(encoder, viewId, sys->m_simpleShader.m_prog, depth, (byte)bgfx.DiscardFlags.All);
        }
Ejemplo n.º 16
0
        public static unsafe void EncodeSimple(RendererBGFXSystem sys, bgfx.Encoder *encoder, ushort viewId, ref SimpleMeshBGFX mesh, ref float4x4 tx, ref SimpleMaterialBGFX mat, int startIndex, int indexCount, byte flipCulling)
        {
            bgfx.set_state(mat.state, 0);

            fixed(float4x4 *p = &tx)
            bgfx.encoder_set_transform(encoder, p, 1);

            bgfx.encoder_set_index_buffer(encoder, mesh.indexBufferHandle, (uint)startIndex, (uint)indexCount);
            bgfx.encoder_set_vertex_buffer(encoder, 0, mesh.vertexBufferHandle, (uint)mesh.vertexFirst, (uint)mesh.vertexCount, mesh.vertexDeclHandle);

            // material uniforms setup
            fixed(float4 *p = &mat.constAlbedo_Opacity)
            bgfx.encoder_set_uniform(encoder, sys.SimpleShader.m_uniformColor0, p, 1);

            fixed(float4 *p = &mat.mainTextureScaleTranslate)
            bgfx.encoder_set_uniform(encoder, sys.SimpleShader.m_uniformTexMad, p, 1);

            bgfx.encoder_set_texture(encoder, 0, sys.SimpleShader.m_samplerTexColor0, mat.texAlbedo, UInt32.MaxValue);
            bgfx.encoder_submit(encoder, viewId, sys.SimpleShader.m_prog, 0, false);
        }
Ejemplo n.º 17
0
        public static unsafe bool EncodeSimpleTransientAlloc(RendererBGFXSystem sys, bgfx.Encoder *encoder, int nindices, int nvertices, SimpleVertex **vertices, ushort **indices)
        {
            bgfx.TransientIndexBuffer  tib;
            bgfx.TransientVertexBuffer tvb;
            fixed(bgfx.VertexLayout *declp = sys.SimpleVertexBufferDecl)
            {
                if (!bgfx.alloc_transient_buffers(&tvb, declp, (uint)nvertices, &tib, (uint)nindices))
                {
#if DEBUG
                    // TODO: throw or ignore draw?
                    throw new InvalidOperationException("Out of transient bgfx memory!");
#else
                    return(false);
#endif
                }
            }

            bgfx.encoder_set_transient_index_buffer(encoder, &tib, 0, (uint)nindices);
            bgfx.encoder_set_transient_vertex_buffer(encoder, 0, &tvb, 0, (uint)nvertices, sys.SimpleVertexBufferDeclHandle);
            *vertices = (SimpleVertex *)tvb.data;
            *indices  = (ushort *)tib.data;
            return(true);
        }
        public static unsafe void SubmitBlitDirectExtended(RendererBGFXInstance *sys, ushort viewId, ref float4x4 tx, bgfx.TextureHandle tetxure,
                                                           bool fromSRGB, bool toSRGB, float reinhard, float4 mulColor, float4 addColor, bool premultiply)
        {
            unsafe {
                bgfx.Encoder *encoder = bgfx.encoder_begin(false);
                bgfx.encoder_set_state(encoder, (uint)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA), 0);

                fixed(float4x4 *p = &tx)
                bgfx.encoder_set_transform(encoder, p, 1);

                sys->m_quadMesh.SetForSubmit(encoder, 0, 6);
                // material uniforms setup
                bgfx.encoder_set_uniform(encoder, sys->m_blitShader.m_colormul, &mulColor, 1);
                bgfx.encoder_set_uniform(encoder, sys->m_blitShader.m_coloradd, &addColor, 1);
                float4 noTexMad = new float4(1, 1, 0, 0);
                bgfx.encoder_set_uniform(encoder, sys->m_blitShader.m_uniformTexMad, &noTexMad, 1);
                bgfx.encoder_set_texture(encoder, 0, sys->m_blitShader.m_samplerTexColor0, tetxure, UInt32.MaxValue);
                float4 s = new float4(fromSRGB ? 1.0f : 0.0f, toSRGB ? 1.0f : 0.0f, reinhard, premultiply ? 1.0f : 0.0f);
                bgfx.encoder_set_uniform(encoder, sys->m_blitShader.m_decodeSRGB_encodeSRGB_reinhard_premultiply, &s, 1);
                // submit
                bgfx.encoder_submit(encoder, viewId, sys->m_blitShader.m_prog, 0, (byte)bgfx.DiscardFlags.All);
                bgfx.encoder_end(encoder);
            }
        }
        // ---------------- blit ----------------------------------------------------------------------------------------------------------------------
        public static unsafe void SubmitBlitDirectFast(RendererBGFXInstance *sys, ushort viewId, ref float4x4 tx, float4 color, bgfx.TextureHandle tetxure)
        {
            unsafe {
                bgfx.Encoder *encoder = bgfx.encoder_begin(false);
                bgfx.set_state((uint)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA), 0);

                fixed(float4x4 *p = &tx)
                bgfx.encoder_set_transform(encoder, p, 1);

                sys->m_quadMesh.SetForSubmit(encoder, 0, 6);
                // material uniforms setup
                bgfx.encoder_set_uniform(encoder, sys->m_simpleShader.m_uniformColor0, &color, 1);
                float4 noTexMad = new float4(1, 1, 0, 0);
                bgfx.encoder_set_uniform(encoder, sys->m_simpleShader.m_uniformTexMad, &noTexMad, 1);
                bgfx.encoder_set_texture(encoder, 0, sys->m_simpleShader.m_samplerTexColor0, tetxure, UInt32.MaxValue);

                fixed(float4 *p = &float4.zero)
                bgfx.encoder_set_uniform(encoder, sys->m_simpleShader.m_uniformBillboarded, p, 1);

                // submit
                bgfx.encoder_submit(encoder, viewId, sys->m_simpleShader.m_prog, 0, (byte)bgfx.DiscardFlags.All);
                bgfx.encoder_end(encoder);
            }
        }
 public static unsafe void EncodeLitTransient(RendererBGFXInstance *sys, bgfx.Encoder *encoder, bgfx.TransientIndexBuffer *tib, bgfx.TransientVertexBuffer *tvb, int nvertices, int nindices, ushort viewId, ref float4x4 tx, ref LitMaterialBGFX mat, ref LightingBGFX lighting, ref float4x4 viewTx, byte flipCulling, ref LightingViewSpaceBGFX viewSpaceLightCache, uint depth)
 {
     EncodeLitTransientBuffers(sys, encoder, tib, tvb, nvertices, nindices);
     EncodeLit(sys, encoder, ref sys->m_litShader, viewId, ref tx, ref mat, ref lighting, ref viewTx, flipCulling, ref viewSpaceLightCache, depth);
 }
 public static unsafe void EncodeSimpleTransient(RendererBGFXInstance *sys, bgfx.Encoder *encoder, bgfx.TransientIndexBuffer *tib, bgfx.TransientVertexBuffer *tvb, int nvertices, int nindices, ushort viewId, ref float4x4 tx, ref SimpleMaterialBGFX mat, byte flipCulling, uint depth)
 {
     EncodeSimpleTransientBuffers(sys, encoder, tib, tvb, nvertices, nindices);
     EncodeSimple(sys, encoder, viewId, ref tx, ref mat, flipCulling, depth);
 }
 public static unsafe void SubmitSimpleTransientDirect(RendererBGFXInstance *sys, bgfx.TransientIndexBuffer *tib, bgfx.TransientVertexBuffer *tvb, int nvertices, int nindices, ushort viewId, ref float4x4 tx, ref SimpleMaterialBGFX mat, byte flipCulling, uint depth)
 {
     bgfx.Encoder *encoder = bgfx.encoder_begin(false);
     EncodeSimpleTransient(sys, encoder, tib, tvb, nvertices, nindices, viewId, ref tx, ref mat, flipCulling, depth);
     bgfx.encoder_end(encoder);
 }
 public static unsafe void EncodeTransientBuffers(RendererBGFXInstance *sys, bgfx.Encoder *encoder, bgfx.TransientIndexBuffer *tib, bgfx.TransientVertexBuffer *tvb, int nvertices, int nindices, bgfx.VertexLayoutHandle layoutHandle)
 {
     bgfx.encoder_set_transient_index_buffer(encoder, tib, 0, (uint)nindices);
     bgfx.encoder_set_transient_vertex_buffer(encoder, 0, tvb, 0, (uint)nvertices, layoutHandle);
 }
 public static unsafe void EncodeLitTransientBuffers(RendererBGFXInstance *sys, bgfx.Encoder *encoder, bgfx.TransientIndexBuffer *tib, bgfx.TransientVertexBuffer *tvb, int nvertices, int nindices)
 {
     EncodeTransientBuffers(sys, encoder, tib, tvb, nvertices, nindices, sys->m_litVertexBufferDeclHandle);
 }
        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);
        }
 public static unsafe void EncodeSimpleMesh(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, ref MeshBGFX mesh, ref float4x4 tx, ref SimpleMaterialBGFX mat, int startIndex, int indexCount, byte flipCulling, uint depth)
 {
     mesh.SetForSubmit(encoder, startIndex, indexCount);
     EncodeSimple(sys, encoder, viewId, ref tx, ref mat, flipCulling, depth);
 }
 // ---------------- simple, unlit, with mesh ----------------------------------------------------------------------------------------------------------------------
 public static unsafe void SubmitSimpleMeshDirect(RendererBGFXInstance *sys, ushort viewId, ref MeshBGFX mesh, ref float4x4 tx, ref SimpleMaterialBGFX mat, int startIndex, int indexCount, byte flipCulling, uint depth)
 {
     bgfx.Encoder *encoder = bgfx.encoder_begin(false);
     EncodeSimpleMesh(sys, encoder, viewId, ref mesh, ref tx, ref mat, startIndex, indexCount, flipCulling, depth);
     bgfx.encoder_end(encoder);
 }
 public static unsafe void EncodeLitZOnlyTransient(RendererBGFXInstance *sys, bgfx.Encoder *encoder, bgfx.TransientIndexBuffer *tib, bgfx.TransientVertexBuffer *tvb, int nvertices, int nindices, ushort viewId, ref float4x4 tx, byte flipCulling)
 {
     EncodeLitTransientBuffers(sys, encoder, tib, tvb, nvertices, nindices);
     EncodeZOnly(sys, encoder, viewId, ref tx, flipCulling);
 }
        public static unsafe void EncodeShadowMapSkinnedMesh(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, ref MeshBGFX mesh, ref float4x4 tx,
                                                             int startIndex, int indexCount, byte flipCulling, float4 bias, float4x4[] boneMatrices)
        {
#if DEBUG
            float4 cd = GetShadowDebugColor(mesh.DebugIndex() + startIndex);
#endif
            mesh.SetForSubmit(encoder, startIndex, indexCount);
            fixed(float4x4 *p = boneMatrices)
            {
                bgfx.encoder_set_uniform(encoder, sys->m_skinnedMeshShadowMapShader.m_uniformBoneMatrices, p, (ushort)boneMatrices.Length);
            }

            EncodeShadowMap(sys, encoder, ref sys->m_skinnedMeshShadowMapShader.m_shadowMapShader, viewId, ref tx, flipCulling, bias
#if DEBUG
                            , cd
#endif
                            );
        }
        // For uniforms and shaders setup. Does not handle vertex/index buffers
        private unsafe static void EncodeLit(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ref LitShader litShader, ushort viewId, ref float4x4 tx, ref LitMaterialBGFX mat,
                                             ref LightingBGFX lighting, ref float4x4 viewTx, byte flipCulling, ref LightingViewSpaceBGFX viewSpaceLightCache, uint depth)
        {
            ulong state = mat.state;

            if (flipCulling != 0)
            {
                state = FlipCulling(state);
            }
            bgfx.encoder_set_state(encoder, state, 0);

            fixed(float4x4 *p = &tx)
            bgfx.encoder_set_transform(encoder, p, 1);

            float3x3 minvt = math.transpose(math.inverse(new float3x3(tx.c0.xyz, tx.c1.xyz, tx.c2.xyz)));

            //float3x3 minvt = new float3x3(tx.c0.xyz, tx.c1.xyz, tx.c2.xyz);
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformModelInverseTranspose, &minvt, 1);

            // material uniforms setup
            fixed(float4 *p = &mat.constAlbedo_Opacity)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformAlbedoOpacity, p, 1);

            fixed(float4 *p = &mat.constMetal_Smoothness_Billboarded)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformMetalSmoothnessBillboarded, p, 1);

            fixed(float4 *p = &mat.constEmissive_normalMapZScale)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformEmissiveNormalZScale, p, 1);

            float4 debugVect = sys->m_outputDebugSelect;

            bgfx.encoder_set_uniform(encoder, litShader.m_uniformOutputDebugSelect, &debugVect, 1);

            fixed(float4 *p = &mat.smoothness)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformSmoothness, p, 1);

            // textures
            bgfx.encoder_set_texture(encoder, 0, litShader.m_samplerAlbedoOpacity, mat.texAlbedoOpacity, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 1, litShader.m_samplerMetal, mat.texMetal, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 2, litShader.m_samplerNormal, mat.texNormal, UInt32.MaxValue);

            bgfx.encoder_set_texture(encoder, 4, litShader.m_samplerEmissive, mat.texEmissive, UInt32.MaxValue);

            fixed(float4 *p = &mat.mainTextureScaleTranslate)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformTexMad, p, 1);

            // ambient
            fixed(float4 *p = &lighting.ambient)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformAmbient, p, 1);

            // transform lighting to view space, if needed: this only needs to re-compute if the viewId changed
            // also the lighting view space is per-thread, hence it is passed in
            lighting.TransformToViewSpace(ref viewTx, ref viewSpaceLightCache, viewId);

            // dir or point lights
            fixed(float *p = viewSpaceLightCache.podl_positionOrDirViewSpace)
            bgfx.encoder_set_uniform(encoder, litShader.m_simplelightPosOrDir, p, (ushort)lighting.numPointOrDirLights);

            fixed(float *p = lighting.podl_colorIVR)
            bgfx.encoder_set_uniform(encoder, litShader.m_simplelightColorIVR, p, (ushort)lighting.numPointOrDirLights);

            // mapped lights (always have to set those or there are undefined samplers)
            EncodeMappedLight(encoder, ref lighting.mappedLight0, ref litShader.m_mappedLight0, 0, viewSpaceLightCache.mappedLight0_viewPosOrDir); // sampler 6
            EncodeMappedLight(encoder, ref lighting.mappedLight1, ref litShader.m_mappedLight1, 1, viewSpaceLightCache.mappedLight1_viewPosOrDir); // sampler 7

            fixed(float4 *p = &lighting.mappedLight01sis)
            bgfx.encoder_set_uniform(encoder, litShader.m_texShadow01sis, p, 1);

            // csm
            fixed(float4 *p = &viewSpaceLightCache.csmLight_viewPosOrDir)
            bgfx.encoder_set_uniform(encoder, litShader.m_dirCSM, p, 1);

            fixed(float *p = lighting.csmOffsetScale)
            bgfx.encoder_set_uniform(encoder, litShader.m_offsetScaleCSM, p, 4);

            fixed(float4 *p = &lighting.csmLight.color_invrangesqr)
            bgfx.encoder_set_uniform(encoder, litShader.m_colorCSM, p, 1);

            fixed(float4x4 *p = &lighting.csmLight.projection)
            bgfx.encoder_set_uniform(encoder, litShader.m_matrixCSM, p, 1);

            fixed(float4 *p = &lighting.csmLightsis)
            bgfx.encoder_set_uniform(encoder, litShader.m_sisCSM, p, 1);

            bgfx.encoder_set_texture(encoder, 5, litShader.m_samplerShadowCSM, lighting.csmLight.shadowMap, UInt32.MaxValue);

            float4 numlights = new float4(lighting.numPointOrDirLights, lighting.numMappedLights, lighting.numCsmLights, 0.0f);

            bgfx.encoder_set_uniform(encoder, litShader.m_numLights, &numlights, 1);

            // fog
            fixed(float4 *p = &lighting.fogColor)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformFogColor, p, 1);

            fixed(float4 *p = &lighting.fogParams)
            bgfx.encoder_set_uniform(encoder, litShader.m_uniformFogParams, p, 1);

            // submit
            bgfx.encoder_submit(encoder, viewId, litShader.m_prog, depth, (byte)bgfx.DiscardFlags.All);
        }