Example #1
0
        public bool UpdateSimpleMaterialBGFX(RendererBGFXSystem sys, ref SimpleMaterial mat, ref SimpleMaterialBGFX matBGFX)
        {
            // if constants changed, need to update packed value
            matBGFX.constAlbedo_Opacity = new float4(mat.constAlbedo, mat.constOpacity);
            // if texture entity OR load state changed need to update texture handles
            // content of texture change should transparently update texture referenced by handle
            bool stillLoading = false;

            if (InitTexture(ref matBGFX.texAlbedo, mat.texAlbedo, sys.WhiteTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texOpacity, mat.texOpacity, sys.WhiteTexture))
            {
                stillLoading = true;
            }

            // if twoSided or hasalpha changed, need to update state
            matBGFX.state = (ulong)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA | bgfx.StateFlags.DepthTestLess);
            if (!mat.twoSided)
            {
                matBGFX.state |= (ulong)bgfx.StateFlags.CullCw;
            }
            if (mat.transparent)
            {
                matBGFX.state |= RendererBGFXSystem.MakeBGFXBlend(bgfx.StateFlags.BlendOne, bgfx.StateFlags.BlendInvSrcAlpha);
            }
            else
            {
                matBGFX.state |= (ulong)bgfx.StateFlags.WriteZ;
            }
            matBGFX.mainTextureScaleTranslate = new float4(mat.scale, mat.offset);
            return(!stillLoading);
        }
Example #2
0
        public static unsafe void EncodeLinePreTransformed(RendererBGFXSystem sys, bgfx.Encoder *encoder, ushort viewId, SimpleVertex *vertices, int n)
        {
            bgfx.TransientIndexBuffer  tib;
            bgfx.TransientVertexBuffer tvb;
            int ni = (n / 4) * 6;

            fixed(bgfx.VertexLayout *declp = sys.SimpleVertexBufferDecl)
            {
                if (!bgfx.alloc_transient_buffers(&tvb, declp, (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.SimpleVertexBufferDeclHandle);

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

            bgfx.encoder_set_state(encoder, state, 0);
            bgfx.encoder_submit(encoder, viewId, sys.LineShader.m_prog, 0, false);
        }
Example #3
0
        public static unsafe void EncodeLine(RendererBGFXSystem 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);
        }
Example #4
0
        public void UpdateLitMaterialBGFX(RendererBGFXSystem sys, Entity e)
        {
            var mat     = EntityManager.GetComponentData <LitMaterial>(e);
            var matBGFX = EntityManager.GetComponentData <LitMaterialBGFX>(e);

            UpdateLitMaterialBGFX(sys, ref mat, ref matBGFX);
            EntityManager.SetComponentData(e, matBGFX);
        }
Example #5
0
        // ---------------- simple, lit, with mesh ----------------------------------------------------------------------------------------------------------------------
        public unsafe static void SubmitLitDirect(RendererBGFXSystem sys, ushort viewId, ref SimpleMeshBGFX mesh, ref float4x4 tx,
                                                  ref LitMaterialBGFX mat, ref LightingBGFX lighting,
                                                  ref float4x4 viewTx, int startIndex, int indexCount, byte flipCulling)
        {
            bgfx.Encoder *        encoder = bgfx.encoder_begin(false);
            LightingViewSpaceBGFX vsLight = default;

            vsLight.cacheTag = -1;
            EncodeLit(sys, encoder, viewId, ref mesh, ref tx, ref mat, ref lighting, ref viewTx, startIndex, indexCount, flipCulling, ref vsLight);
            bgfx.encoder_end(encoder);
        }
Example #6
0
        public static unsafe void EncodeDebugTangents(RendererBGFXSystem 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);
                EncodeLine(sys, encoder, viewId, vertices[i].Position, vertices[i].Position + vertices[i].BiTangent * length, new float4(0, 1, 0, 1), width, ref objTx, ref viewTx, ref projTx);
            }
        }
Example #7
0
        protected override void OnStopRunning()
        {
            m_TextShader.Destroy();
            m_TextSDFShader.Destroy();
            m_BGFXSystem   = null;
            m_BGFXInstance = null;
#if UNITY_ANDROID
            PlatformEvents.OnSuspendResume -= OnSuspendResume;
#endif

            base.OnStopRunning();
        }
Example #8
0
        protected override void OnStartRunning()
        {
            base.OnStartRunning();

            m_BGFXSystem = World.GetExistingSystem <RendererBGFXSystem>();

            // the work in EnsureInitialized should be done here, once we have
            // a singleton we can use to indicate that bgfx is initialized
#if UNITY_ANDROID
            PlatformEvents.OnSuspendResume += OnSuspendResume;
#endif
        }
Example #9
0
        public bool UpdateLitMaterialBGFX(RendererBGFXSystem sys, ref LitMaterial mat, ref LitMaterialBGFX matBGFX)
        {
            bool stillLoading = false;

            if (InitTexture(ref matBGFX.texAlbedo, mat.texAlbedo, sys.WhiteTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texOpacity, mat.texOpacity, sys.WhiteTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texNormal, mat.texNormal, sys.UpTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texMetal, mat.texMetal, sys.BlackTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texEmissive, mat.texEmissive, sys.BlackTexture))
            {
                stillLoading = true;
            }
            if (InitTexture(ref matBGFX.texSmoothness, mat.texSmoothness, sys.GreyTexture))
            {
                stillLoading = true;
            }

            matBGFX.constAlbedo_Opacity           = new float4(mat.constAlbedo, mat.constOpacity);
            matBGFX.constMetal_Smoothness         = new float4(mat.constMetal, mat.constSmoothness, 0, 0);
            matBGFX.constEmissive_normalMapZScale = new float4(mat.constEmissive, mat.normalMapZScale);
            matBGFX.mainTextureScaleTranslate     = new float4(mat.scale, mat.offset);

            // if twoSided, need to update state
            matBGFX.state = (ulong)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA | bgfx.StateFlags.DepthTestLess);
            if (!mat.twoSided)
            {
                matBGFX.state |= (ulong)bgfx.StateFlags.CullCcw;
            }
            if (mat.transparent)
            {
                matBGFX.state |= RendererBGFXSystem.MakeBGFXBlend(bgfx.StateFlags.BlendOne, bgfx.StateFlags.BlendInvSrcAlpha);
            }
            else
            {
                matBGFX.state |= (ulong)bgfx.StateFlags.WriteZ;
            }
            return(!stillLoading);
        }
Example #10
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);
        }
Example #11
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);
        }
Example #12
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);
        }
Example #13
0
        public Entity CreateFrontBufferRenderNode(int w, int h, bool primary)
        {
            Entity eNode = EntityManager.CreateEntity();

            EntityManager.AddComponentData(eNode, new RenderNode {
            });
            if (primary)
            {
                EntityManager.AddComponentData(eNode, new RenderNodePrimarySurface {
                });
            }

            Entity ePassBlit = EntityManager.CreateEntity();

            EntityManager.AddComponentData(ePassBlit, new RenderPass
            {
                inNode              = eNode,
                sorting             = RenderPassSort.Unsorted,
                projectionTransform = float4x4.identity,
                viewTransform       = float4x4.identity,
                passType            = RenderPassType.FullscreenQuad,
                viewId              = 0xffff,
                scissor             = new RenderPassRect(),
                viewport            = new RenderPassRect {
                    x = 0, y = 0, w = (ushort)w, h = (ushort)h
                },
                clearFlags   = RenderPassClear.Color,
                clearRGBA    = RendererBGFXSystem.PackColorBGFX(new Color(0, 0, 0, 0)),
                clearDepth   = 1.0f,
                clearStencil = 0
            });
            EntityManager.AddComponent <RenderPassAutoSizeToNode>(ePassBlit);
            EntityManager.AddComponent <Frustum>(ePassBlit);

            DynamicBuffer <RenderNodeRef> nodeRefs = EntityManager.AddBuffer <RenderNodeRef>(eNode);
            DynamicBuffer <RenderPassRef> passRefs = EntityManager.AddBuffer <RenderPassRef>(eNode);

            passRefs.Add(new RenderPassRef {
                e = ePassBlit
            });

            return(eNode);
        }
Example #14
0
        // ---------------- blit ----------------------------------------------------------------------------------------------------------------------
        public static void SubmitBlitDirectFast(RendererBGFXSystem sys, ushort viewId, ref float4x4 tx, float4 color, bgfx.TextureHandle tetxure)
        {
            unsafe {
                bgfx.set_state((uint)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA), 0);

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

                bgfx.set_index_buffer(sys.QuadMesh.indexBufferHandle, 0, 6);
                bgfx.set_vertex_buffer(0, sys.QuadMesh.vertexBufferHandle, 0, 4);
                // material uniforms setup
                bgfx.set_uniform(sys.SimpleShader.m_uniformColor0, &color, 1);
                float4 noTexMad = new float4(1, 1, 0, 0);
                bgfx.set_uniform(sys.SimpleShader.m_uniformTexMad, &noTexMad, 1);
                bgfx.set_texture(0, sys.SimpleShader.m_samplerTexColor0, tetxure, UInt32.MaxValue);
            }
            // submit
            bgfx.submit(viewId, sys.SimpleShader.m_prog, 0, false);
        }
Example #15
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);
        }
Example #16
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);
        }
Example #17
0
        public static void SubmitBlitDirectExtended(RendererBGFXSystem sys, ushort viewId, ref float4x4 tx, bgfx.TextureHandle tetxure,
                                                    bool fromSRGB, bool toSRGB, float reinhard, float4 mulColor, float4 addColor, bool premultiply)
        {
            unsafe {
                bgfx.set_state((uint)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA), 0);

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

                bgfx.set_index_buffer(sys.QuadMesh.indexBufferHandle, 0, 6);
                bgfx.set_vertex_buffer(0, sys.QuadMesh.vertexBufferHandle, 0, 4);
                // material uniforms setup
                bgfx.set_uniform(sys.BlitShader.m_colormul, &mulColor, 1);
                bgfx.set_uniform(sys.BlitShader.m_coloradd, &addColor, 1);
                float4 noTexMad = new float4(1, 1, 0, 0);
                bgfx.set_uniform(sys.BlitShader.m_uniformTexMad, &noTexMad, 1);
                bgfx.set_texture(0, sys.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.set_uniform(sys.BlitShader.m_decodeSRGB_encodeSRGB_reinhard_premultiply, &s, 1);
            }
            // submit
            bgfx.submit(viewId, sys.BlitShader.m_prog, 0, false);
        }
Example #18
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);
 }
Example #19
0
        public unsafe static void EncodeLit(RendererBGFXSystem sys, bgfx.Encoder *encoder, ushort viewId, ref SimpleMeshBGFX mesh, ref float4x4 tx,
                                            ref LitMaterialBGFX mat, ref LightingBGFX lighting,
                                            ref float4x4 viewTx, int startIndex, int indexCount, byte flipCulling, ref LightingViewSpaceBGFX viewSpaceLightCache)
        {
            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, sys.m_litShader.m_uniformModelInverseTranspose, &minvt, 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.m_litShader.m_uniformAlbedoOpacity, p, 1);

            fixed(float4 *p = &mat.constMetal_Smoothness)
            bgfx.encoder_set_uniform(encoder, sys.m_litShader.m_uniformMetalSmoothness, p, 1);

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

            float4 debugVect = sys.OutputDebugSelect;

            bgfx.encoder_set_uniform(encoder, sys.m_litShader.m_uniformOutputDebugSelect, &debugVect, 1);
            // textures
            bgfx.encoder_set_texture(encoder, 0, sys.m_litShader.m_samplerAlbedo, mat.texAlbedo, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 1, sys.m_litShader.m_samplerMetal, mat.texMetal, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 2, sys.m_litShader.m_samplerNormal, mat.texNormal, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 3, sys.m_litShader.m_samplerSmoothness, mat.texSmoothness, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 4, sys.m_litShader.m_samplerEmissive, mat.texEmissive, UInt32.MaxValue);
            bgfx.encoder_set_texture(encoder, 5, sys.m_litShader.m_samplerOpacity, mat.texOpacity, UInt32.MaxValue);

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

            // ambient
            fixed(float4 *p = &lighting.ambient)
            bgfx.encoder_set_uniform(encoder, sys.m_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, sys.m_litShader.m_simplelightPosOrDir, p, (ushort)lighting.numPointOrDirLights);

            fixed(float *p = lighting.podl_colorIVR)
            bgfx.encoder_set_uniform(encoder, sys.m_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 sys.m_litShader.m_mappedLight0, 0, viewSpaceLightCache.mappedLight0_viewPosOrDir);
            EncodeMappedLight(encoder, ref lighting.mappedLight1, ref sys.m_litShader.m_mappedLight1, 1, viewSpaceLightCache.mappedLight1_viewPosOrDir);

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

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

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

            // submit
            bgfx.encoder_submit(encoder, viewId, sys.m_litShader.m_prog, 0, false);
        }
Example #20
0
 // ---------------- simple, unlit, with mesh ----------------------------------------------------------------------------------------------------------------------
 public static unsafe void SubmitSimpleDirect(RendererBGFXSystem sys, ushort viewId, ref SimpleMeshBGFX mesh, ref float4x4 tx, ref SimpleMaterialBGFX mat, int startIndex, int indexCount, byte flipCulling)
 {
     bgfx.Encoder *encoder = bgfx.encoder_begin(false);
     EncodeSimple(sys, encoder, viewId, ref mesh, ref tx, ref mat, startIndex, indexCount, flipCulling);
     bgfx.encoder_end(encoder);
 }
Example #21
0
        // ---------------- debug line rendering helper ----------------------------------------------------------------------------------------------------------------------

        public static unsafe void SubmitLineDirect(RendererBGFXSystem sys, ushort viewId, float3 p0, float3 p1, float4 color, float2 width, ref float4x4 objTx, ref float4x4 viewTx, ref float4x4 projTx)
        {
            bgfx.Encoder *encoder = bgfx.encoder_begin(false);
            EncodeLine(sys, encoder, viewId, p0, p1, color, width, ref objTx, ref viewTx, ref projTx);
            bgfx.encoder_end(encoder);
        }
Example #22
0
        protected override void OnUpdate()
        {
            var bgfxsys = World.GetExistingSystem <RendererBGFXSystem>();

            if (!bgfxsys.Initialized)
            {
                return;
            }

            // make sure passes have viewid, transform, scissor rect and view rect set

            // reset alreadyAdded state
            // we expect < 100 or so passes, so the below code does not need to be crazy great
            Entities.ForEach((ref RenderNode rnode) => { rnode.alreadyAdded = false; });
            Entities.ForEach((ref RenderPass pass) => { pass.viewId = 0xffff; }); // there SHOULD not be any passes around that are not referenced by the graph...

            // get all nodes, sort (bgfx issues in-order per view. a better api could use the render graph to issue without gpu
            // barriers where possible)
            // sort into eval order, assign pass viewId
            ushort nextViewId = 0;

            Entities.WithAll <RenderNodePrimarySurface>().ForEach((Entity eNode) => { RecAddPasses(eNode, ref nextViewId); });

            Entities.WithAll <RenderPassAutoSizeToNode>().ForEach((Entity e, ref RenderPass pass) =>
            {
                if (EntityManager.HasComponent <RenderNodePrimarySurface>(pass.inNode))
                {
                    var di          = World.TinyEnvironment().GetConfigData <DisplayInfo>();
                    pass.viewport.x = 0;
                    pass.viewport.y = 0;
                    pass.viewport.w = (ushort)di.width;
                    pass.viewport.h = (ushort)di.height;
                    return;
                }
                if (EntityManager.HasComponent <RenderNodeTexture>(pass.inNode))
                {
                    var texRef    = EntityManager.GetComponentData <RenderNodeTexture>(pass.inNode);
                    pass.viewport = texRef.rect;
                }
                // TODO: add others like cubemap
            });

            // auto update passes that are matched with a camera
            Entities.ForEach((Entity e, ref RenderPass pass, ref RenderPassUpdateFromCamera fromCam) =>
            {
                Entity eCam              = fromCam.camera;
                CameraMatrices camData   = EntityManager.GetComponentData <CameraMatrices>(eCam);
                pass.viewTransform       = camData.view;
                pass.projectionTransform = camData.projection;
                if (EntityManager.HasComponent <Frustum>(eCam))
                {
                    if (EntityManager.HasComponent <Frustum>(e))
                    {
                        EntityManager.SetComponentData(e, EntityManager.GetComponentData <Frustum>(eCam));
                    }
                }
                else
                {
                    if (EntityManager.HasComponent <Frustum>(e))
                    {
                        EntityManager.SetComponentData(e, new Frustum());
                    }
                }
            });

            // auto update passes that are matched with a light
            Entities.ForEach((Entity e, ref RenderPass pass, ref RenderPassUpdateFromLight fromLight) =>
            {
                Entity eLight            = fromLight.light;
                LightMatrices lightData  = EntityManager.GetComponentData <LightMatrices>(eLight);
                pass.viewTransform       = lightData.view;
                pass.projectionTransform = lightData.projection;
                if (EntityManager.HasComponent <Frustum>(eLight))
                {
                    if (EntityManager.HasComponent <Frustum>(e))
                    {
                        EntityManager.SetComponentData(e, EntityManager.GetComponentData <Frustum>(eLight));
                    }
                }
                else
                {
                    if (EntityManager.HasComponent <Frustum>(e))
                    {
                        EntityManager.SetComponentData(e, new Frustum());
                    }
                }
            });

            // set up extra pass data
            Entities.ForEach((Entity e, ref RenderPass pass) =>
            {
                if (pass.viewId == 0xffff)
                {
                    RenderDebug.LogFormat("Render pass entity {0} on render node entity {1} is not referenced by the render graph. It should be deleted.", e, pass.inNode);
                    Assert.IsTrue(false);
                    return;
                }
                bool rtt = EntityManager.HasComponent <FramebufferBGFX>(pass.inNode);
                // those could be more shared ... (that is, do all passes really need a copy of view & projection?)
                unsafe { fixed(float4x4 * viewp = &pass.viewTransform, projp = &pass.projectionTransform)
                         {
                             if (bgfxsys.m_homogeneousDepth && bgfxsys.m_originBottomLeft) // gl style
                             {
                                 bgfx.set_view_transform(pass.viewId, viewp, projp);
                                 pass.flipCulling = 0;
                             }
                             else // dx style
                             {
                                 bool yflip = !bgfxsys.m_originBottomLeft && rtt;
                                 float4x4 adjustedProjection = RendererBGFXSystem.AdjustProjection(ref pass.projectionTransform, !bgfxsys.m_homogeneousDepth, yflip);
                                 bgfx.set_view_transform(pass.viewId, viewp, &adjustedProjection);
                                 pass.flipCulling = yflip ? (byte)3 : (byte)0;
                             }
                         } }
                bgfx.set_view_mode(pass.viewId, (bgfx.ViewMode)pass.sorting);
                bgfx.set_view_rect(pass.viewId, pass.viewport.x, pass.viewport.y, pass.viewport.w, pass.viewport.h);
                bgfx.set_view_scissor(pass.viewId, pass.scissor.x, pass.scissor.y, pass.scissor.w, pass.scissor.h);
                bgfx.set_view_clear(pass.viewId, (ushort)pass.clearFlags, pass.clearRGBA, pass.clearDepth, pass.clearStencil);
                if (rtt)
                {
                    var rttbgfx = EntityManager.GetComponentData <FramebufferBGFX>(pass.inNode);
                    bgfx.set_view_frame_buffer(pass.viewId, rttbgfx.handle);
                }
                else
                {
                    bgfx.set_view_frame_buffer(pass.viewId, new bgfx.FrameBufferHandle {
                        idx = 0xffff
                    });
                }
                // touch it? needed?
                bgfx.touch(pass.viewId);
            });
        }
Example #23
0
 private void AddMappedLight(ref LightingBGFX r, ref ShadowmappedLight sml, ref Light l, ref float4x4 tx, ref LightMatrices txCache, RendererBGFXSystem sys, bool isSpot)
 {
     if (r.numMappedLights >= LightingBGFX.maxMappedLights)
         throw new InvalidOperationException("Too many mapped lights"); }
Example #24
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);
 }
Example #25
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);
 }
Example #26
0
        public void CreateAllPasses(int w, int h, Entity eCam, Entity eNode)
        {
            Camera     cam        = EntityManager.GetComponentData <Camera>(eCam);
            CameraMask cameraMask = new CameraMask {
                mask = ulong.MaxValue
            };

            if (EntityManager.HasComponent <CameraMask>(eCam))
            {
                cameraMask = EntityManager.GetComponentData <CameraMask>(eCam);
            }

            Entity ePassOpaque = EntityManager.CreateEntity();

            EntityManager.AddComponentData(ePassOpaque, new RenderPass
            {
                inNode              = eNode,
                sorting             = RenderPassSort.Unsorted,
                projectionTransform = float4x4.identity,
                viewTransform       = float4x4.identity,
                passType            = RenderPassType.Opaque,
                viewId              = 0xffff,
                scissor             = new RenderPassRect(),
                viewport            = new RenderPassRect {
                    x = 0, y = 0, w = (ushort)w, h = (ushort)h
                },
                clearFlags   = RenderPassClear.Depth | (cam.clearFlags == CameraClearFlags.SolidColor ? RenderPassClear.Color : 0),
                clearRGBA    = RendererBGFXSystem.PackColorBGFX(cam.backgroundColor),
                clearDepth   = 1.0f,
                clearStencil = 0
            });
            SetPassComponents(ePassOpaque, cameraMask, eCam);

            Entity ePassTransparent = EntityManager.CreateEntity();

            EntityManager.AddComponentData(ePassTransparent, new RenderPass
            {
                inNode              = eNode,
                sorting             = RenderPassSort.SortZLess,
                projectionTransform = float4x4.identity,
                viewTransform       = float4x4.identity,
                passType            = RenderPassType.Transparent,
                viewId              = 0xffff,
                scissor             = new RenderPassRect(),
                viewport            = new RenderPassRect {
                    x = 0, y = 0, w = (ushort)w, h = (ushort)h
                },
                clearFlags   = 0,
                clearRGBA    = 0,
                clearDepth   = 1.0f,
                clearStencil = 0
            });
            SetPassComponents(ePassTransparent, cameraMask, eCam);

            Entity ePassSprites = EntityManager.CreateEntity();

            EntityManager.AddComponentData(ePassSprites, new RenderPass
            {
                inNode              = eNode,
                sorting             = RenderPassSort.Sorted,
                projectionTransform = float4x4.identity,
                viewTransform       = float4x4.identity,
                passType            = RenderPassType.Sprites,
                viewId              = 0xffff,
                scissor             = new RenderPassRect(),
                viewport            = new RenderPassRect {
                    x = 0, y = 0, w = (ushort)w, h = (ushort)h
                },
                clearFlags   = 0,
                clearRGBA    = 0,
                clearDepth   = 1.0f,
                clearStencil = 0
            });
            SetPassComponents(ePassSprites, cameraMask, eCam);
            EntityManager.AddBuffer <SortSpritesEntry>(ePassSprites);

            Entity ePassUI = EntityManager.CreateEntity();

            EntityManager.AddComponentData(ePassUI, new RenderPass
            {
                inNode              = eNode,
                sorting             = RenderPassSort.Sorted,
                projectionTransform = float4x4.identity,
                viewTransform       = float4x4.identity,
                passType            = RenderPassType.UI,
                viewId              = 0xffff,
                scissor             = new RenderPassRect(),
                viewport            = new RenderPassRect {
                    x = 0, y = 0, w = (ushort)w, h = (ushort)h
                },
                clearFlags   = 0,
                clearRGBA    = 0,
                clearDepth   = 1.0f,
                clearStencil = 0
            });
            SetPassComponents(ePassUI, cameraMask, eCam);

            // add passes to node, in order
            DynamicBuffer <RenderPassRef> passRefs = EntityManager.GetBuffer <RenderPassRef>(eNode);

            passRefs.Add(new RenderPassRef {
                e = ePassOpaque
            });
            passRefs.Add(new RenderPassRef {
                e = ePassTransparent
            });
            passRefs.Add(new RenderPassRef {
                e = ePassSprites
            });
            passRefs.Add(new RenderPassRef {
                e = ePassUI
            });
        }